From 467a81b1e8ed325b215daca77d1c3b25942d6455 Mon Sep 17 00:00:00 2001 From: Vincent <114195376+HydroshieldMKII@users.noreply.github.com> Date: Tue, 30 Sep 2025 15:56:23 -0400 Subject: [PATCH] Refactor Guardian installation and update process to utilize Docker Compose, streamline dependency installation, and enhance service management --- ct/guardian.sh | 45 ++++------------ install/guardian-install.sh | 101 ++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 91 deletions(-) diff --git a/ct/guardian.sh b/ct/guardian.sh index 2a9a8c15..fda14f81 100644 --- a/ct/guardian.sh +++ b/ct/guardian.sh @@ -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 } diff --git a/install/guardian-install.sh b/install/guardian-install.sh index 0edaa9f8..620f907e 100644 --- a/install/guardian-install.sh +++ b/install/guardian-install.sh @@ -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 </etc/systemd/system/guardian-backend.service +# Create systemd service to manage Docker Compose +msg_info "Creating Guardian Service" +cat </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 </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"