LocalAuthority / scripts / deploy_to_jumper.sh
Newer Older
c80a9aa 22 hours ago History
121 lines | 3.328kb
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
Bogdan Timofte authored 22 hours ago
82
BUILD_REVISION="$(git rev-parse --short=12 HEAD)"
83
BUILD_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
84
BUILD_DIRTY=0
85
if [[ -n "$(git status --porcelain)" ]]; then
86
    BUILD_DIRTY=1
87
fi
88
BUILD_DEPLOYED_AT="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
Bogdan Timofte authored 2 days ago
89

            
90
rsync_args=(-az --delete)
91
if [[ "$DRY_RUN" -eq 1 ]]; then
92
    rsync_args+=(--dry-run --itemize-changes)
93
fi
94

            
95
ssh "$TARGET_HOST" "mkdir -p '$TARGET_DIR/scripts' '$TARGET_DIR/deploy' '$TARGET_DIR/.doc'"
96
rsync "${rsync_args[@]}" scripts/ "$TARGET_HOST:$TARGET_DIR/scripts/"
97
rsync "${rsync_args[@]}" deploy/ "$TARGET_HOST:$TARGET_DIR/deploy/"
98
rsync "${rsync_args[@]}" .doc/ "$TARGET_HOST:$TARGET_DIR/.doc/"
99
rsync "${rsync_args[@]}" README.md "$TARGET_HOST:$TARGET_DIR/README.md"
100

            
101
if [[ "$INCLUDE_CONFIG" -eq 1 ]]; then
102
    ssh "$TARGET_HOST" "mkdir -p '$TARGET_DIR/config'"
103
    rsync "${rsync_args[@]}" config/ "$TARGET_HOST:$TARGET_DIR/config/"
104
fi
105

            
106
if [[ "$DRY_RUN" -eq 1 ]]; then
107
    exit 0
108
fi
109

            
Bogdan Timofte authored 22 hours ago
110
printf 'revision=%s\nbranch=%s\ndirty=%s\ndeployed_at=%s\n' \
111
    "$BUILD_REVISION" "$BUILD_BRANCH" "$BUILD_DIRTY" "$BUILD_DEPLOYED_AT" |
112
    ssh "$TARGET_HOST" "cat > '$TARGET_DIR/BUILD'"
113

            
Bogdan Timofte authored 2 days ago
114
ssh "$TARGET_HOST" "cd '$TARGET_DIR' && perl -c scripts/host_manager.pl >/dev/null"
115

            
116
if [[ "$RESTART" -eq 1 ]]; then
117
    ssh "$TARGET_HOST" "sudo -n systemctl restart host-manager && systemctl is-active host-manager >/dev/null"
118
fi
119

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