aslan-server/.deploy/test-deploy/sync-and-deploy.sh

56 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
DEPLOY_HOST="${DEPLOY_HOST:-43.160.220.141}"
DEPLOY_USER="${DEPLOY_USER:-root}"
SSH_KEY="${SSH_KEY:-$HOME/.ssh/aslan-test-singapore-ed25519}"
REMOTE_BASE="${REMOTE_BASE:-/opt/aslan-test}"
REMOTE_SRC="${REMOTE_SRC:-$REMOTE_BASE/source/aslan-server}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
usage() {
cat <<'EOF'
Usage:
.deploy/test-deploy/sync-and-deploy.sh [remote deploy args...]
Examples:
.deploy/test-deploy/sync-and-deploy.sh
.deploy/test-deploy/sync-and-deploy.sh status
.deploy/test-deploy/sync-and-deploy.sh --fast other
Environment:
DEPLOY_HOST default 43.160.220.141
DEPLOY_USER default root
SSH_KEY default ~/.ssh/aslan-test-singapore-ed25519
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
ssh_base=(ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new "$DEPLOY_USER@$DEPLOY_HOST")
rsync_ssh="ssh -i $SSH_KEY -o StrictHostKeyChecking=accept-new"
"${ssh_base[@]}" "mkdir -p '$REMOTE_BASE/ops' '$REMOTE_SRC'"
rsync -az \
-e "$rsync_ssh" \
"$SCRIPT_DIR/deploy-likei-services.sh" "$DEPLOY_USER@$DEPLOY_HOST:$REMOTE_BASE/deploy-likei-services.sh"
rsync -az --delete \
--exclude 'runs/' \
-e "$rsync_ssh" \
"$SCRIPT_DIR/ops/" "$DEPLOY_USER@$DEPLOY_HOST:$REMOTE_BASE/ops/"
"${ssh_base[@]}" "flock -n '$REMOTE_BASE/deploy.lock' sh -c 'chmod +x \"\$0/deploy-likei-services.sh\" \"\$0/ops/aslan_ops.py\" && cp \"\$0/ops/aslan-test-ops.service\" /etc/systemd/system/aslan-test-ops.service && systemctl daemon-reload && systemctl enable aslan-test-ops.service >/dev/null && systemctl restart aslan-test-ops.service' '$REMOTE_BASE'"
if [[ "$#" -gt 0 ]]; then
printf -v remote_script_q '%q' "$REMOTE_BASE/deploy-likei-services.sh"
remote_cmd="$remote_script_q"
printf -v remote_args_q ' %q' "$@"
remote_cmd+="$remote_args_q"
"${ssh_base[@]}" "$remote_cmd"
fi