-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo
executable file
·121 lines (104 loc) · 2.51 KB
/
do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
set -euo pipefail
readonly DEPLOY_TARGET_HOST="$2"
readonly DEPLOY_TARGET_USER="global_cluster"
readonly DEPLOY_TARGET_SSH_CONFIG="/usr/local/github-runner/.ssh/config"
readonly DEPLOY_TARGET_SSH="${DEPLOY_TARGET_USER}@$DEPLOY_TARGET_HOST"
readonly DEPLOY_TARGET_SCP="${DEPLOY_TARGET_USER}@[$DEPLOY_TARGET_HOST]"
readonly MIX_ENV="${MIX_ENV:-test}"
readonly RELEASE_NAME="global_cluster"
function do_help {
echo "Usage: ./do <thing>"
echo ""
echo " things:"
echo " help -- Show this help"
echo " test -- Run tests"
echo " format -- Check format"
echo " static -- Run a static analysis via credo"
echo " build -- Build tarball"
echo " deploy -- Deploy tarball"
}
function do_test {
mix test
}
function do_format {
mix format --check-formatted
}
function do_static {
mix credo --strict
}
function do_build {
mix release ${RELEASE_NAME}
}
function do_deploy {
# Remote
local r
r="$(_ssh uname -o)"
# Local
local l
l="$(uname -o)"
if [[ "$r" != "$l" ]]
then
echo "Remote target: $r"
echo "You have: $l"
echo "These must be equal for a deployment to work"
exit 1
fi
local timestamp
timestamp="$(date -Iseconds)"
local version
version="${GITHUB_SHA::7}"
local filename
filename="${RELEASE_NAME}-${timestamp}-${version}"
_scp _build/prod/${RELEASE_NAME}*tar.gz "${DEPLOY_TARGET_SCP}:/tmp/${filename}.tar.gz"
_ssh "mkdir -p /usr/local/${RELEASE_NAME}/$filename"
_ssh "tar xzf /tmp/${filename}.tar.gz -C /usr/local/${RELEASE_NAME}/$filename"
_ssh sudo service ${RELEASE_NAME} stop || true
_ssh "ln -sFf /usr/local/${RELEASE_NAME}/${filename} /usr/local/${RELEASE_NAME}/active"
_ssh "sudo service ${RELEASE_NAME} start > /dev/null 2>&1"
_ssh "rm /tmp/${filename}.tar.gz"
}
function _ssh {
local cmd="$*"
if [[ -f "$DEPLOY_TARGET_SSH_CONFIG" ]]; then
ssh -F /usr/local/github-runner/.ssh/config ${DEPLOY_TARGET_SSH} "$cmd"
else
ssh ${DEPLOY_TARGET_SSH} "$cmd"
fi
}
function _scp {
if [[ -f "$DEPLOY_TARGET_SSH_CONFIG" ]]; then
scp -F /usr/local/github-runner/.ssh/config "$@"
else
scp "$@"
fi
}
function main {
echo "Environment: $MIX_ENV"
case "$1" in
"help")
do_help
;;
"test")
do_test
;;
"format")
do_format
;;
"static")
do_static
;;
"build")
do_build
;;
"deploy")
do_deploy
;;
*)
do_help
;;
esac
exit 0
}
# Run command or default to help
main "${1:-help}"