This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
common
275 lines (232 loc) · 6.43 KB
/
common
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env bash
# If working directory isn't clean, all changes will be lost if this strategy
# runs all the way to the end. I would be very annoyed if that happened.
#
gitstatus() {
status "Checking local repository"
if [ $(git status | grep -c "working directory clean") = 0 ]
then
error "
Your working directory is not clean.
Either stash or commit before re-running this command.
Stopping so that uncommitted changes won't be lost.\n"
fi
}
nothing_to_do() {
status "Everything up-to-date"
}
authorize_hosts() {
if [ -n "$HOSTS" ]
then
status "Authorizing hosts"
if [[ ! -e ~/.ssh/known_hosts ]]
then
touch ~/.ssh/known_hosts
fi
for _host in $HOSTS
do
if [[ $(ssh-keygen -F $_host | grep -c found) = 0 ]]
then
ssh-keyscan "$_host" 2>/dev/null >> ~/.ssh/known_hosts
fi
done
fi
}
init_app_remotely() {
__exec_if_defined "pre_init_app_remotely"
local git_remote="$(git remote -v)"
status "Ensuring hosts are ready to accept git pushes"
for _host in $HOSTS_APP_USER
do
local _remote_url="$_host:$DELIVER_TO"
if [[ ! "$git_remote" =~ "$_host $_remote_url" ]]; then
[[ "$git_remote" =~ "$_host" ]] && git remote rm "$_host"
git remote add "$_host" "$_remote_url"
fi
done
__remote "
set -e
if [ ! -d $DELIVER_TO ]
then
mkdir -p $DELIVER_TO
cd $DELIVER_TO
git init $SILENCE
git config receive.denyCurrentBranch ignore
fi
"
__exec_if_defined "post_init_app_remotely"
}
git_push() {
__exec_if_defined "pre_git_push"
local _hosts="${1:-"$HOSTS_APP_USER"}"
status "Pushing new commits with git to: $_hosts"
background_jobs_pids=()
background_jobs=()
for _host in $_hosts
do
local _background_job="git push $GIT_PUSH $_host $REFSPEC $SILENCE"
__log "JOB: $_background_job"
( eval "$_background_job" ) &
background_jobs_pids+=("$!")
background_jobs+=("$_background_job")
done
__monitor_background_jobs
__exec_if_defined "post_git_push"
}
git_reset_remote() {
status "Resetting remote hosts to $REVISION"
__remote "
set -e
cd $DELIVER_TO $SILENCE
git reset --hard $REVISION $SILENCE
"
}
git_submodules() {
__exec_if_defined "pre_git_submodules"
status "Updating git submodules"
__remote "
set -e
cd $DELIVER_TO $SILENCE
git submodule init $SILENCE
git submodule sync $SILENCE
if [ -e .gitmodules ]
then
if [ ! -e ~/.ssh/known_hosts ]
then
touch ~/.ssh/known_hosts
fi
cat .gitmodules | awk -F '://|@|:|/' '
/url =/ {
command=\"test \$(ssh-keygen -F \" \$2 \" | grep -c found) = 0 && ssh-keyscan \" \$2 \" >> ~/.ssh/known_hosts $SILENCE\"
system(command)
close(command)
}'
git submodule foreach 'git reset --hard $SILENCE' $SILENCE
git submodule update $SILENCE
fi
"
__exec_if_defined "post_git_submodules"
}
rvmrc_trust() {
if [ -e "$ORIGIN_DIR/.rvmrc" ]
then
__exec_if_defined "pre_rvmrc_trust"
status "Trusting rvmrc in $DELIVER_TO"
__remote "
set -e
source ~/.profile
rvm rvmrc trust $DELIVER_TO $SILENCE
"
__exec_if_defined "post_rvmrc_trust"
fi
}
authorize_remote_hosts() {
if [[ ${#AUTHORIZED_REMOTE_HOSTS[@]} != 0 ]]
then
status "Authorizing remote hosts"
__remote "
set -e
source ~/.profile
for _remote_host in $AUTHORIZED_REMOTE_HOSTS
do
if [[ \$(ssh-keygen -F \$_remote_host | grep -c found) = 0 ]]
then
ssh-keyscan \$_remote_host >> ~/.ssh/known_hosts $SILENCE
fi
done
"
fi
}
bundle_install() {
__exec_if_defined "pre_bundle_install"
status "Installing gems with bundler"
__remote "
set -e
source ~/.profile
cd $DELIVER_TO $SILENCE
if [[ \$APP_ENV = production ]] || [[ \$RACK_ENV = production ]] || [[ \$RAILS_ENV = production ]]
then
bundle install --local --deployment --without development test $SILENCE
else
bundle install --local --deployment $SILENCE
fi
"
__exec_if_defined "post_bundle_install"
}
npm_install() {
__exec_if_defined "pre_npm_install"
status "Installing modules with npm"
__remote "
set -e
source ~/.profile
cd $DELIVER_TO $SILENCE
npm install $SILENCE
"
__exec_if_defined "post_npm_install"
}
foreman_export() {
__exec_if_defined "pre_foreman_export"
status "Exporting services to $SUPERVISOR"
local _foreman="foreman export $SUPERVISOR tmp --user $APP_USER --app $APP"
test -n "$PORT" && _foreman="$_foreman --port $PORT"
test -n "$FOREMAN_EXTRAS" && _foreman="$_foreman $FOREMAN_EXTRAS"
__remote "
set -e
source ~/.profile
cd $DELIVER_TO $SILENCE
$_foreman $SILENCE
"
__exec_if_defined "post_foreman_export"
}
launch() {
__exec_if_defined "pre_launch"
status "Launching app with $SUPERVISOR"
# This command, because of the sudo dependency, will use your local $USER.
# You should be able to log in with $USER to the remote servers, and you
# should be able to run sudo without needing a password.
#
# I will be tackling this shortly, you really shouldn't neeed sudo
# privileges to deliver an app. What was I thinking?!?
if [[ "$SUPERVISOR" == "bluepill" ]]; then
__remote "
set -e
sudo bluepill ${APP} status | grep up && sudo bluepill ${APP} stop $SILENCE
sudo bluepill load $DELIVER_TO/tmp/${APP}.pill $SILENCE
" "$HOSTS"
elif [[ "$SUPERVISOR" == "smf" ]]; then
__remote "
set -e
sudo svccfg import $DELIVER_TO/tmp/${APP}*xml $SILENCE
svcs -H -o FMRI $APP/* | xargs -I {} sudo svcadm enable -t {} $SILENCE
svcs -H -o FMRI $APP/* | xargs -I {} sudo svcadm restart {} $SILENCE
" "$HOSTS"
else
__remote "
set -e
if [[ \$(sudo initctl list | awk '/^'$APP' / { print \$0 }') =~ 'run' ]]; then
sudo stop $APP $SILENCE
fi
sudo rm -f /etc/init/$APP[.-]*
sudo mv -f $DELIVER_TO/tmp/*conf /etc/init/
sudo start $APP $SILENCE
" "$HOSTS"
fi
__exec_if_defined "post_launch"
}
# Aliased, no longer bound to upstart
#
upstart() {
__exec_if_defined "pre_upstart"
launch
__exec_if_defined "post_upstart"
}
permissions() {
__exec_if_defined "pre_permissions"
status "Setting file permissions"
__remote "
[ -n \"$OWNER\" ] && sudo chown -fR $OWNER.$GROUP $DELIVER_TO
[ -n \"$PERMISSIONS\" ] && sudo chmod -fR $PERMISSIONS $DELIVER_TO
exit 0
" "$HOSTS"
__exec_if_defined "post_permissions"
}