Compare commits

..

No commits in common. "96478620f43faff1e91bd95667922d1b14812406" and "801573aadac2b55988f9fe574ef96f24e9c8e6b1" have entirely different histories.

2 changed files with 104 additions and 59 deletions

View File

@ -32,35 +32,91 @@ function update_script() {
msg_error "No ${APP} Installation Found!" msg_error "No ${APP} Installation Found!"
exit exit
fi fi
RELEASE=$(curl -s https://api.github.com/repos/Koenkk/zigbee2mqtt/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }') if [[ "$(node -v | cut -d 'v' -f 2)" == "18."* ]]; then
if [[ ! -f /opt/${APP}_version.txt ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]]; then if ! command -v npm >/dev/null 2>&1; then
msg_info "Stopping Service" echo "Installing NPM..."
systemctl stop zigbee2mqtt apt-get install -y npm >/dev/null 2>&1
msg_ok "Stopped Service" echo "Installed NPM..."
fi
msg_info "Creating Backup"
mkdir -p /opt/z2m_backup
tar -czf /opt/z2m_backup/${APP}_backup_$(date +%Y%m%d%H%M%S).tar.gz -C /opt zigbee2mqtt &>/dev/null
mv /opt/zigbee2mqtt/data /opt/z2m_backup
msg_ok "Backup Created"
msg_info "Updating ${APP} to v${RELEASE}"
cd /opt
wget -q "https://github.com/Koenkk/zigbee2mqtt/archive/refs/tags/${RELEASE}.zip"
unzip -q ${RELEASE}.zip
mv zigbee2mqtt-${RELEASE} /opt/zigbee2mqtt
rm -rf /opt/zigbee2mqtt/data
mv /opt/z2m_backup/data /opt/zigbee2mqtt
cd /opt/zigbee2mqtt
pnpm install --frozen-lockfile &>/dev/null
pnpm build &>/dev/null
msg_info "Starting Service"
systemctl start zigbee2mqtt
msg_ok "Started Service"
echo "${RELEASE}" >/opt/${APP}_version.txt
else
msg_ok "No update required. ${APP} is already at v${RELEASE}."
fi fi
cd /opt/zigbee2mqtt
stop_zigbee2mqtt() {
if which systemctl 2>/dev/null >/dev/null; then
echo "Shutting down Zigbee2MQTT..."
sudo systemctl stop zigbee2mqtt
else
echo "Skipped stopping Zigbee2MQTT, no systemctl found"
fi
}
start_zigbee2mqtt() {
if which systemctl 2>/dev/null >/dev/null; then
echo "Starting Zigbee2MQTT..."
sudo systemctl start zigbee2mqtt
else
echo "Skipped starting Zigbee2MQTT, no systemctl found"
fi
}
set -e
if [ -d data-backup ]; then
echo "ERROR: Backup directory exists. May be previous restoring was failed?"
echo "1. Save 'data-backup' and 'data' dirs to safe location to make possibility to restore config later."
echo "2. Manually delete 'data-backup' dir and try again."
exit 1
fi
stop_zigbee2mqtt
echo "Generating a backup of the configuration..."
cp -R data data-backup || {
echo "Failed to create backup."
exit 1
}
echo "Checking if any changes were made to package-lock.json..."
git checkout package-lock.json || {
echo "Failed to check package-lock.json."
exit 1
}
echo "Initiating update..."
if ! git pull; then
echo "Update failed, temporarily storing changes and trying again."
git stash && git pull || (
echo "Update failed even after storing changes. Aborting."
exit 1
)
fi
echo "Acquiring necessary components..."
npm ci || {
echo "Failed to install necessary components."
exit 1
}
echo "Building..."
npm run build || {
echo "Failed to build new version."
exit 1
}
echo "Restoring configuration..."
cp -R data-backup/* data || {
echo "Failed to restore configuration."
exit 1
}
rm -rf data-backup || {
echo "Failed to remove backup directory."
exit 1
}
start_zigbee2mqtt
echo "Done!"
exit exit
} }
@ -71,4 +127,4 @@ description
msg_ok "Completed Successfully!\n" msg_ok "Completed Successfully!\n"
echo -e "${CREATING}${GN}${APP} setup has been successfully initialized!${CL}" echo -e "${CREATING}${GN}${APP} setup has been successfully initialized!${CL}"
echo -e "${INFO}${YW} Access it using the following URL:${CL}" echo -e "${INFO}${YW} Access it using the following URL:${CL}"
echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:9442${CL}" echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:9442${CL}"

View File

@ -14,22 +14,21 @@ network_check
update_os update_os
msg_info "Installing Dependencies" msg_info "Installing Dependencies"
$STD apt-get install -y \ $STD apt-get install -y curl
curl \ $STD apt-get install -y sudo
sudo \ $STD apt-get install -y mc
mc \ $STD apt-get install -y git
git \ $STD apt-get install -y make
make \ $STD apt-get install -y g++
g++ \ $STD apt-get install -y gcc
gcc \ $STD apt-get install -y ca-certificates
ca-certificates \ $STD apt-get install -y gnupg
gnupg
msg_ok "Installed Dependencies" msg_ok "Installed Dependencies"
msg_info "Setting up Node.js Repository" msg_info "Setting up Node.js Repository"
mkdir -p /etc/apt/keyrings mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" >/etc/apt/sources.list.d/nodesource.list echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" >/etc/apt/sources.list.d/nodesource.list
msg_ok "Set up Node.js Repository" msg_ok "Set up Node.js Repository"
msg_info "Installing Node.js" msg_info "Installing Node.js"
@ -37,47 +36,37 @@ $STD apt-get update
$STD apt-get install -y nodejs $STD apt-get install -y nodejs
msg_ok "Installed Node.js" msg_ok "Installed Node.js"
msg_info "Installing pnpm" msg_info "Setting up Zigbee2MQTT Repository"
$STD npm install -g pnpm $STD git clone --depth 1 https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
msg_ok "Installed pnpm" msg_ok "Set up Zigbee2MQTT Repository"
msg_info "Setting up Zigbee2MQTT" msg_info "Installing Zigbee2MQTT"
cd /opt
$STD corepack enable
RELEASE=$(curl -s https://api.github.com/repos/Koenkk/zigbee2mqtt/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
wget -q "https://github.com/Koenkk/zigbee2mqtt/archive/refs/tags/${RELEASE}.zip"
unzip -q ${RELEASE}.zip
mv zigbee2mqtt-${RELEASE} /opt/zigbee2mqtt
cd /opt/zigbee2mqtt/data
mv configuration.example.yaml configuration.yaml
cd /opt/zigbee2mqtt cd /opt/zigbee2mqtt
$STD pnpm install --frozen-lockfile $STD npm ci
$STD pnpm build
msg_ok "Installed Zigbee2MQTT" msg_ok "Installed Zigbee2MQTT"
msg_info "Creating Service" msg_info "Creating Service"
cat <<EOF >/etc/systemd/system/zigbee2mqtt.service service_path="/etc/systemd/system/zigbee2mqtt.service"
echo "[Unit] echo "[Unit]
Description=zigbee2mqtt Description=zigbee2mqtt
After=network.target After=network.target
[Service] [Service]
Environment=NODE_ENV=production Environment=NODE_ENV=production
ExecStart=/usr/bin/pnpm start ExecStart=/usr/bin/npm start
WorkingDirectory=/opt/zigbee2mqtt WorkingDirectory=/opt/zigbee2mqtt
StandardOutput=inherit StandardOutput=inherit
StandardError=inherit StandardError=inherit
Restart=always Restart=always
User=root User=root
[Install] [Install]
EOF WantedBy=multi-user.target" >$service_path
systemctl enable -q --now zigbee2mqtt.service $STD systemctl enable zigbee2mqtt.service
msg_ok "Created Service" msg_ok "Created Service"
motd_ssh motd_ssh
customize customize
msg_info "Cleaning up" msg_info "Cleaning up"
rm -rf /opt/${RELEASE}.zip
$STD apt-get -y autoremove $STD apt-get -y autoremove
$STD apt-get -y autoclean $STD apt-get -y autoclean
msg_ok "Cleaned" msg_ok "Cleaned"