Refactor Guardian installation and update process to utilize Docker Compose, streamline dependency installation, and enhance service management

This commit is contained in:
Vincent 2025-09-30 15:56:23 -04:00
parent 8076c313df
commit 467a81b1e8
2 changed files with 55 additions and 91 deletions

View File

@ -34,56 +34,33 @@ function update_script() {
if [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]] || [[ ! -f /opt/${APP}_version.txt ]]; then
msg_info "Stopping $APP"
systemctl stop guardian-backend guardian-frontend
cd /opt/Guardian
docker compose down
msg_ok "Stopped $APP"
msg_info "Preserving Database"
if [[ -f "/opt/Guardian/backend/plex-guard.db" ]]; then
cp "/opt/Guardian/backend/plex-guard.db" "/tmp/plex-guard.db.backup"
msg_ok "Database backed up"
fi
msg_info "Updating $APP to v${RELEASE}"
cd /tmp
# Download new docker-compose.yml
curl -fsSL -o docker-compose.yml "https://raw.githubusercontent.com/HydroshieldMKII/Guardian/main/docker-compose.yml"
curl -fsSL -o "${RELEASE}.zip" "https://github.com/HydroshieldMKII/Guardian/archive/refs/tags/${RELEASE}.zip"
unzip -q "${RELEASE}.zip"
rm -rf /opt/Guardian
FOLDER_NAME=$(echo "${RELEASE}" | sed 's/^v//')
mv "Guardian-${FOLDER_NAME}/" "/opt/Guardian"
if [[ -f "/tmp/plex-guard.db.backup" ]]; then
msg_info "Restoring Database"
cp "/tmp/plex-guard.db.backup" "/opt/Guardian/backend/plex-guard.db"
rm "/tmp/plex-guard.db.backup"
msg_ok "Database restored"
fi
cd /opt/Guardian/backend
npm ci
npm run build
cd /opt/Guardian/frontend
npm ci
NODE_ENV=development npm run build
# Pull new Docker images
docker compose pull
echo "${RELEASE}" >/opt/${APP}_version.txt
msg_ok "Updated $APP to v${RELEASE}"
msg_info "Starting $APP"
systemctl start guardian-backend guardian-frontend
cd /opt/Guardian
docker compose up -d
msg_ok "Started $APP"
msg_info "Cleaning Up"
rm -rf /tmp/"${RELEASE}.zip" /tmp/"Guardian-${FOLDER_NAME}" /tmp/plex-guard.db.backup
rm -f /tmp/plex-guard.db.backup
docker system prune -f
msg_ok "Cleanup Completed"
msg_ok "Update Successful"
else
msg_ok "No update required. ${APP} is already at v${RELEASE}"
msg_ok "No update required. ${APP} is already at ${RELEASE}"
fi
exit
}

View File

@ -15,83 +15,70 @@ update_os
msg_info "Installing Dependencies"
$STD apt-get install -y \
git \
nodejs \
npm \
sqlite3 \
unzip \
curl
curl \
ca-certificates \
gnupg \
lsb-release
msg_ok "Installed Dependencies"
msg_info "Setup Guardian"
RELEASE=$(curl -fsSL https://api.github.com/repos/HydroshieldMKII/Guardian/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
curl -fsSL -o "${RELEASE}.zip" "https://github.com/HydroshieldMKII/Guardian/archive/refs/tags/${RELEASE}.zip"
unzip -q "${RELEASE}.zip"
# Install Docker
msg_info "Installing Docker"
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
$STD apt-get update
$STD apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
systemctl enable --now docker
msg_ok "Installed Docker"
FOLDER_NAME=$(echo "${RELEASE}" | sed 's/^v//')
mv "Guardian-${FOLDER_NAME}/" "/opt/Guardian"
# Setup Guardian
msg_info "Setting up Guardian"
mkdir -p /opt/Guardian
cd /opt/Guardian
# Download docker-compose.yml from repository
RELEASE=$(curl -fsSL https://api.github.com/repos/HydroshieldMKII/Guardian/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
curl -fsSL -o docker-compose.yml "https://raw.githubusercontent.com/HydroshieldMKII/Guardian/main/docker-compose.yml"
# Create data directory for persistent storage
mkdir -p data
echo "${RELEASE}" >/opt/Guardian_version.txt
msg_ok "Setup Guardian"
# Start Guardian with Docker Compose
msg_info "Starting Guardian"
cd /opt/Guardian
docker compose up -d
msg_ok "Started Guardian"
msg_info "Building backend"
cd /opt/Guardian/backend
npm ci
npm run build
msg_ok "Built backend"
msg_info "Building frontend"
cd /opt/Guardian/frontend
npm ci
NODE_ENV=development npm run build
msg_ok "Built frontend"
msg_info "Creating Backend Service"
cat <<EOF >/etc/systemd/system/guardian-backend.service
# Create systemd service to manage Docker Compose
msg_info "Creating Guardian Service"
cat <<EOF >/etc/systemd/system/guardian.service
[Unit]
Description=Guardian Backend
After=network.target
Description=Guardian Docker Compose
Requires=docker.service
After=docker.service
Wants=network-online.target
After=network-online.target
[Service]
WorkingDirectory=/opt/Guardian/backend
Environment=NODE_ENV=development
ExecStart=/usr/bin/node dist/main.js
Restart=always
RestartSec=3
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/opt/Guardian
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl enable -q --now guardian-backend
msg_ok "Created Backend Service"
msg_info "Creating Frontend Service"
cat <<EOF >/etc/systemd/system/guardian-frontend.service
[Unit]
Description=Guardian Frontend
After=guardian-backend.service network.target
Wants=guardian-backend.service
[Service]
WorkingDirectory=/opt/Guardian/frontend
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/npm run start
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
systemctl enable -q --now guardian-frontend
msg_ok "Created Frontend Service"
systemctl enable guardian
msg_ok "Created Guardian Service"
motd_ssh
customize
msg_info "Cleaning up"
rm -f "${RELEASE}".zip
$STD apt-get -y autoremove
$STD apt-get -y autoclean
msg_ok "Cleaned"