@@ -53,36 +53,75 @@ chmod 600 "$KEY_DIR/authorized_keys"
5353chown -R " $DEPLOY_USER :$DEPLOY_USER " " $KEY_DIR "
5454
5555# ---------------------------------------------------------------
56- # 3. Create sudoers entry (least privilege)
56+ # 3. Create the server-side deploy script (runs as root via sudo)
57+ #
58+ # Reads User/Group directly from the installed service file so
59+ # this script never needs to hardcode a username.
60+ # ---------------------------------------------------------------
61+ cat > /usr/local/bin/deploy-moon << 'DEPLOY_SCRIPT '
62+ #!/bin/bash
63+ # /usr/local/bin/deploy-moon
64+ # Runs as root (via sudo) during GitHub Actions deployments.
65+
66+ set -e
67+
68+ DEPLOY_SRC="${1:-/tmp/moon-deploy}"
69+ DEPLOY_DIR=/var/www/moon
70+
71+ # Read the service owner from the installed unit — no hardcoded username
72+ SERVICE_USER=$(systemctl show moon --property=User --value)
73+ SERVICE_GROUP=$(systemctl show moon --property=Group --value)
74+
75+ if [ -z "$SERVICE_USER" ]; then
76+ echo "[deploy] ERROR: Could not read User from moon.service"
77+ exit 1
78+ fi
79+
80+ echo "[deploy] Installing binary to $DEPLOY_DIR/moon (owner: $SERVICE_USER:$SERVICE_GROUP)..."
81+ cp "$DEPLOY_SRC/moon" "$DEPLOY_DIR/moon"
82+ chmod +x "$DEPLOY_DIR/moon"
83+
84+ echo "[deploy] Updating web assets..."
85+ cp "$DEPLOY_SRC/index.html" "$DEPLOY_DIR/"
86+ cp "$DEPLOY_SRC/about.html" "$DEPLOY_DIR/"
87+ cp "$DEPLOY_SRC/calendar.html" "$DEPLOY_DIR/"
88+ cp -r "$DEPLOY_SRC/static/" "$DEPLOY_DIR/"
89+ chown -R "$SERVICE_USER:$SERVICE_GROUP" "$DEPLOY_DIR"
90+
91+ echo "[deploy] Restarting service..."
92+ systemctl restart moon
93+
94+ echo "[deploy] Verifying service is active..."
95+ sleep 2
96+ if ! systemctl is-active --quiet moon; then
97+ echo "[deploy] ERROR: Service failed to start. Status:"
98+ systemctl status moon --no-pager --lines=30
99+ exit 1
100+ fi
101+
102+ echo "[deploy] Cleaning up..."
103+ rm -rf "$DEPLOY_SRC"
104+
105+ echo "[deploy] Done — moon is running."
106+ DEPLOY_SCRIPT
107+
108+ chmod +x /usr/local/bin/deploy-moon
109+ echo " [ok] Created /usr/local/bin/deploy-moon"
110+
111+ # ---------------------------------------------------------------
112+ # 4. Configure sudoers — only allow the one deploy script
57113# ---------------------------------------------------------------
58114SUDOERS_FILE=" /etc/sudoers.d/moon-deploy"
59115
60116cat > " $SUDOERS_FILE " << 'EOF '
61- # Allow the deploy user to install the moon app without a password
62- deploy ALL=(ALL) NOPASSWD: \
63- /bin/cp /tmp/moon-deploy/moon /usr/local/bin/moon, \
64- /bin/chmod +x /usr/local/bin/moon, \
65- /bin/cp /tmp/moon-deploy/index.html /var/www/moon/, \
66- /bin/cp /tmp/moon-deploy/about.html /var/www/moon/, \
67- /bin/cp /tmp/moon-deploy/calendar.html /var/www/moon/, \
68- /bin/cp -r /tmp/moon-deploy/static/ /var/www/moon/, \
69- /bin/chown -R www-data\:www-data /var/www/moon, \
70- /usr/bin/systemctl restart moon, \
71- /usr/bin/systemctl is-active moon
117+ # Allow the deploy user to run the moon deployment script as root
118+ deploy ALL=(ALL) NOPASSWD: /usr/local/bin/deploy-moon
72119EOF
73120
74121chmod 440 " $SUDOERS_FILE "
75- # Validate the file
76122visudo -c -f " $SUDOERS_FILE "
77123echo " [ok] sudoers entry created at $SUDOERS_FILE "
78124
79- # ---------------------------------------------------------------
80- # 4. Ensure /var/www/moon exists and is owned correctly
81- # ---------------------------------------------------------------
82- mkdir -p /var/www/moon
83- chown -R www-data:www-data /var/www/moon
84- echo " [ok] /var/www/moon ready"
85-
86125# ---------------------------------------------------------------
87126# 5. Print next steps
88127# ---------------------------------------------------------------
0 commit comments