diff --git a/scripts/deploy-core-service.sh b/scripts/deploy-core-service.sh index 7909670..73c5a00 100755 --- a/scripts/deploy-core-service.sh +++ b/scripts/deploy-core-service.sh @@ -14,6 +14,8 @@ source "$CONFIG_FILE" export DOCKER_CONFIG="/tmp/.docker" mkdir -p "$DOCKER_CONFIG" +DB_FILE="/opt/appfactory/data/appfactory/appfactory.db" + SERVICE_DIR="$WORKSPACE_DIR/$SERVICE_NAME" DEPLOY_DIR="$APPFACTORY_DIR/deploy" @@ -27,28 +29,148 @@ if [ ! -f "$SERVICE_DIR/Dockerfile" ]; then exit 1 fi -echo "Updating source..." -cd "$SERVICE_DIR" +DEPLOY_ID="$(python3 - "$DB_FILE" "$SERVICE_NAME" <<'PY' +import sqlite3 +import sys + +db_file = sys.argv[1] +app_id = sys.argv[2] + +con = sqlite3.connect(db_file) + +cur = con.execute( + """ + INSERT INTO deployments ( + app_id, + kind, + status, + started_at + ) + VALUES (?, ?, ?, CURRENT_TIMESTAMP) + """, + (app_id, "core", "running"), +) + +con.commit() + +print(cur.lastrowid) + +con.close() +PY +)" + +TMP_DIR="$(mktemp -d)" +STDOUT_FILE="$TMP_DIR/stdout.log" +STDERR_FILE="$TMP_DIR/stderr.log" + +finish_deploy() { + local status="$1" + local returncode="$2" + + python3 - "$DB_FILE" "$DEPLOY_ID" "$status" "$returncode" "$STDOUT_FILE" "$STDERR_FILE" <<'PY' +import sqlite3 +import sys +from pathlib import Path + +db_file = sys.argv[1] +deploy_id = sys.argv[2] +status = sys.argv[3] +returncode = int(sys.argv[4]) + +stdout_file = Path(sys.argv[5]) +stderr_file = Path(sys.argv[6]) + +stdout = stdout_file.read_text(errors="replace") if stdout_file.exists() else "" +stderr = stderr_file.read_text(errors="replace") if stderr_file.exists() else "" + +con = sqlite3.connect(db_file) + +con.execute( + """ + UPDATE deployments + SET status = ?, + finished_at = CURRENT_TIMESTAMP, + stdout = ?, + stderr = ?, + returncode = ? + WHERE id = ? + """, + (status, stdout, stderr, returncode, deploy_id), +) + +con.commit() +con.close() +PY + + rm -rf "$TMP_DIR" +} + +run_step() { + local title="$1" + shift + + { + echo "" + echo "==================================================" + echo "$title" + echo "==================================================" + "$@" + } >>"$STDOUT_FILE" 2>>"$STDERR_FILE" +} + +set +e + +run_step "Updating source" bash -c " +cd '$SERVICE_DIR' git \ - -c safe.directory="$SERVICE_DIR" \ + -c safe.directory='$SERVICE_DIR' \ fetch origin main git \ - -c safe.directory="$SERVICE_DIR" \ + -c safe.directory='$SERVICE_DIR' \ reset --hard origin/main +" -echo "Building image..." -docker build --pull --no-cache -t "$SERVICE_NAME:latest" "$SERVICE_DIR" +RESULT=$? -echo "Recreating container..." -cd "$DEPLOY_DIR" +if [ "$RESULT" -eq 0 ]; then + run_step "Building image" docker build --pull --no-cache -t "$SERVICE_NAME:latest" "$SERVICE_DIR" + RESULT=$? +fi + +if [ "$RESULT" -eq 0 ]; then + run_step "Recreating container" bash -c " +cd '$DEPLOY_DIR' docker compose \ -f docker-compose.yml \ -f apps-compose.yml \ - up -d --force-recreate "$SERVICE_NAME" + up -d --force-recreate '$SERVICE_NAME' +" + RESULT=$? +fi -echo "" -echo "DONE" -echo "Service deployed: $SERVICE_NAME" +set -e + +if [ "$RESULT" -eq 0 ]; then + finish_deploy "success" 0 + + cat "$STDOUT_FILE" + cat "$STDERR_FILE" >&2 + + echo "" + echo "DONE" + echo "Core service deployed: $SERVICE_NAME" +else + finish_deploy "failed" "$RESULT" + + cat "$STDOUT_FILE" + cat "$STDERR_FILE" >&2 + + echo "" + echo "FAILED" + echo "Deployment ID: $DEPLOY_ID" + + exit "$RESULT" +fi