Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment of fullcount.xyz API service #70

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions api/deploy/deploy.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash

# Deployment script - intended to run on Fullcount API server

# Colors
C_RESET='\033[0m'
C_RED='\033[1;31m'
C_GREEN='\033[1;32m'
C_YELLOW='\033[1;33m'

# Logs
PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]"
PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]"
PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]"

# Main
APP_DIR="${APP_DIR:-/home/ubuntu/fullcount.xyz}"
AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-east-1}"
SCRIPT_DIR="$(realpath $(dirname $0))"
SECRETS_DIR="${SECRETS_DIR:-/home/ubuntu/fullcount-secrets}"
PARAMETERS_ENV_PATH="${SECRETS_DIR}/app.env"
USER_SYSTEMD_DIR="${USER_SYSTEMD_DIR:-/home/ubuntu/.config/systemd/user}"

# API server service file
FULLCOUNT_API_SERVICE_FILE="fullcount-api.service"

set -eu

echo
echo
echo -e "${PREFIX_INFO} Source nvm"
. /home/ubuntu/.nvm/nvm.sh

echo
echo
echo -e "${PREFIX_INFO} Installing Node.js dependencies"
EXEC_DIR=$(pwd)
cd "${APP_DIR}/api"
/home/ubuntu/.nvm/versions/node/v19.9.0/bin/npm install
cd "${EXEC_DIR}"

echo
echo
echo -e "${PREFIX_INFO} Build fullcount API server"
EXEC_DIR=$(pwd)
cd "${APP_DIR}/api"
/home/ubuntu/.nvm/versions/node/v19.9.0/bin/npm run build
cd "${EXEC_DIR}"

echo
echo
echo -e "${PREFIX_INFO} Retrieving deployment parameters"
if [ ! -d "${SECRETS_DIR}" ]; then
mkdir "${SECRETS_DIR}"
echo -e "${PREFIX_WARN} Created new secrets directory"
fi

echo
echo
echo -e "${PREFIX_INFO} Prepare user systemd directory"
if [ ! -d "${USER_SYSTEMD_DIR}" ]; then
mkdir -p "${USER_SYSTEMD_DIR}"
echo -e "${PREFIX_WARN} Created new user systemd directory"
fi

echo
echo
echo -e "${PREFIX_INFO} Replacing existing Fullcount API service definition with ${FULLCOUNT_API_SERVICE_FILE}"
chmod 644 "${SCRIPT_DIR}/${FULLCOUNT_API_SERVICE_FILE}"
cp "${SCRIPT_DIR}/${FULLCOUNT_API_SERVICE_FILE}" "${USER_SYSTEMD_DIR}/${FULLCOUNT_API_SERVICE_FILE}"
XDG_RUNTIME_DIR="/run/user/1000" systemctl --user daemon-reload
XDG_RUNTIME_DIR="/run/user/1000" systemctl --user restart "${FULLCOUNT_API_SERVICE_FILE}"
17 changes: 17 additions & 0 deletions api/deploy/fullcount-api.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Fullcount API server
After=network.target
StartLimitIntervalSec=300
StartLimitBurst=3

[Service]
WorkingDirectory=/home/ubuntu/fullcount.xyz/api
EnvironmentFile=/home/ubuntu/fullcount-secrets/app.env
Environment=NODE_VERSION=19.9.0 NODE_ENV=production
Restart=on-failure
RestartSec=15s
ExecStart=/home/ubuntu/.nvm/nvm-exec npm run prod
SyslogIdentifier=fullcount-api

[Install]
WantedBy=multi-user.target