LocalAuthority / scripts / deploy_to_jumper.sh
Newer Older
110 lines | 2.904kb
Bogdan Timofte authored 2 days ago
1
#!/usr/bin/env bash
2
# Deploy Madagascar Local Authority application code to jumper.
3

            
4
set -euo pipefail
5

            
6
TARGET_HOST="${TARGET_HOST:-jumper.madagascar.xdev.ro}"
7
TARGET_DIR="${TARGET_DIR:-/usr/local/xdev-host-manager}"
8
RESTART=1
9
ALLOW_DIRTY=0
10
DRY_RUN=0
11
INCLUDE_CONFIG=0
12

            
13
usage() {
14
    cat <<EOF
15
Usage: $0 [--target host] [--target-dir path] [--no-restart] [--allow-dirty] [--dry-run] [--include-config]
16

            
17
Deploys application code/docs to jumper. Runtime registry data in config/ is not
18
deployed unless --include-config is explicit.
19

            
20
Environment:
21
  TARGET_HOST   Default: jumper.madagascar.xdev.ro
22
  TARGET_DIR    Default: /usr/local/xdev-host-manager
23
EOF
24
}
25

            
26
while [[ $# -gt 0 ]]; do
27
    case "$1" in
28
        --target)
29
            TARGET_HOST="$2"
30
            shift 2
31
            ;;
32
        --target-dir)
33
            TARGET_DIR="$2"
34
            shift 2
35
            ;;
36
        --no-restart)
37
            RESTART=0
38
            shift
39
            ;;
40
        --allow-dirty)
41
            ALLOW_DIRTY=1
42
            shift
43
            ;;
44
        --dry-run)
45
            DRY_RUN=1
46
            shift
47
            ;;
48
        --include-config)
49
            INCLUDE_CONFIG=1
50
            shift
51
            ;;
52
        --help|-h)
53
            usage
54
            exit 0
55
            ;;
56
        *)
57
            echo "Unknown option: $1" >&2
58
            usage >&2
59
            exit 2
60
            ;;
61
    esac
62
done
63

            
64
cd "$(dirname "$0")/.."
65

            
66
if [[ "$ALLOW_DIRTY" -ne 1 ]] && [[ -n "$(git status --porcelain)" ]]; then
67
    echo "Working tree is dirty; commit first or pass --allow-dirty." >&2
68
    git status --short >&2
69
    exit 1
70
fi
71

            
72
command -v rsync >/dev/null 2>&1 || {
73
    echo "rsync is required for deployment." >&2
74
    exit 1
75
}
76
ssh "$TARGET_HOST" "command -v rsync >/dev/null 2>&1" || {
77
    echo "rsync is required on $TARGET_HOST." >&2
78
    exit 1
79
}
80

            
81
perl -c scripts/host_manager.pl >/dev/null
82

            
83
rsync_args=(-az --delete)
84
if [[ "$DRY_RUN" -eq 1 ]]; then
85
    rsync_args+=(--dry-run --itemize-changes)
86
fi
87

            
88
ssh "$TARGET_HOST" "mkdir -p '$TARGET_DIR/scripts' '$TARGET_DIR/deploy' '$TARGET_DIR/.doc'"
89
rsync "${rsync_args[@]}" scripts/ "$TARGET_HOST:$TARGET_DIR/scripts/"
90
rsync "${rsync_args[@]}" deploy/ "$TARGET_HOST:$TARGET_DIR/deploy/"
91
rsync "${rsync_args[@]}" .doc/ "$TARGET_HOST:$TARGET_DIR/.doc/"
92
rsync "${rsync_args[@]}" README.md "$TARGET_HOST:$TARGET_DIR/README.md"
93

            
94
if [[ "$INCLUDE_CONFIG" -eq 1 ]]; then
95
    ssh "$TARGET_HOST" "mkdir -p '$TARGET_DIR/config'"
96
    rsync "${rsync_args[@]}" config/ "$TARGET_HOST:$TARGET_DIR/config/"
97
fi
98

            
99
if [[ "$DRY_RUN" -eq 1 ]]; then
100
    exit 0
101
fi
102

            
103
ssh "$TARGET_HOST" "cd '$TARGET_DIR' && perl -c scripts/host_manager.pl >/dev/null"
104

            
105
if [[ "$RESTART" -eq 1 ]]; then
106
    ssh "$TARGET_HOST" "sudo -n systemctl restart host-manager && systemctl is-active host-manager >/dev/null"
107
fi
108

            
109
ssh "$TARGET_HOST" "curl -fsS http://127.0.0.1:8088/healthz >/dev/null"
110
echo "Deployed to $TARGET_HOST:$TARGET_DIR"