-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodeploy.sh
executable file
·152 lines (126 loc) · 3.81 KB
/
autodeploy.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
# Enable job control with -m so that the deployment can be stopped.
set -em
# Default options.
workdir=.
update_cmd=""
# Default for Git.
up_to_date_pattern="Already up.to.date"
deploy_cmd=""
stop_cmd="kill -9"
pass_pid_to_stop_cmd="true"
sleep_between_checks=10m
sleep_after_stop=5s
# Parse options.
show_help() {
echo -e "$0 --cmd COMMAND [--workdir DIR] [--update_cmd UPDATE_COMMAND] [--up_to_date_pattern PATTERN] [--stop_cmd STOP_COMMAND] [--no_pass_pid] [--sleep_time SLEEP_TIME] [--sleep_after_stop SLEEP_TIME]
Navigates to DIR.
Runs UPDATE_COMMAND.
Runs COMMAND.
Every SLEEP_TIME, runs UPDATE_COMMAND and if the output matches PATTERN, then COMMAND runs again.
WARNING: It can be dangerous to automate deployment using code you don't know on your own machine. It is recommended to run this in an isolated environment such as a Docker container.
--cmd COMMAND The command to execute when there are updates.
--workdir DIR The directory to work from. Defaults to \"${workdir}\".
--update_cmd UPDATE_COMMAND The command to update the code. Defaults to Git pulling the current branch from origin.
--up_to_date_pattern PATTERN The regex pattern used by grep to check for in the result of the UPDATE_COMMAND. If the pattern matches, then COMMAND runs. Defaults to \"${up_to_date_pattern}\".
--stop_cmd STOP_COMMAND The command to stop the deployment. Takes the PID of COMMAND as a parameter. Defaults to \"${stop_cmd}\".
--no_pass_pid The PID of the COMMAND will not get passed to the STOP_COMMAND. Defaults to passing the PID to the STOP_COMMAND.
--sleep_time SLEEP_TIME The amount of time to wait between checks. This is passed to the \"sleep\" command. Defaults to \"${sleep_between_checks}\".
--sleep_after_stop SLEEP_TIME The amount of time to wait after stopping before running COMMAND again. This is passed to the \"sleep\" command. Defaults to \"${sleep_after_stop}\".
"
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
show_help
exit 0
;;
--cmd|--command)
deploy_cmd="$2"
shift
shift
;;
--workdir)
workdir="$2"
shift
shift
;;
--stop_cmd)
stop_cmd="$2"
shift
shift
;;
--no_pass_pid)
pass_pid_to_stop_cmd="false"
shift
;;
--sleep_time)
sleep_between_checks="$2"
shift
shift
;;
--sleep_after_stop)
sleep_after_stop="$2"
shift
shift
;;
--update_cmd|--update_command)
update_cmd="$2"
shift
shift
;;
--up_to_date_pattern)
up_to_date_pattern="$2"
shift
shift
;;
*)
echo "Unrecognized option \"${key}\"" >&2
show_help
exit 1
;;
esac
done
pushd ${workdir}
if [[ ${deploy_cmd} == "" ]]; then
echo "The deployment command was not set." >&2
show_help
exit 1
fi
if [[ ${update_cmd} == "" ]]; then
# Default for Git.
update_cmd="git pull origin `git rev-parse --abbrev-ref HEAD`"
fi
run() {
set -x
local deployment_pid
deploy() {
${deploy_cmd} & deployment_pid=$!
trap "${stop_cmd} ${deployment_pid}" EXIT
echo "deploy: deployment_pid: ${deployment_pid}"
}
check() {
if ${update_cmd} | grep -q -E "${up_to_date_pattern}"; then
echo "Up to date."
else
echo "Not up to date."
if [[ ${pass_pid_to_stop_cmd} == "true" ]]; then
${stop_cmd} ${deployment_pid}
else
${stop_cmd}
fi
sleep ${sleep_after_stop}
deploy
fi
}
${update_cmd}
deploy
while true
do
sleep ${sleep_between_checks}
check
done
}
run