Reflect deploy changes in run-deploy.sh
[arvados-dev.git] / jenkins / run-deploy.sh
1 #!/bin/bash
2
3
4 read -rd "\000" helpmessage <<EOF
5 $(basename $0): Deploy Arvados to a cluster
6
7 Syntax:
8         WORKSPACE=/path/to/arvados $(basename $0) <identifier> <deploy_repo_name>
9
10 Options:
11
12 identifier             Arvados cluster name
13 deploy_repo_name       Name for the repository with the (capistrano) deploy scripts
14
15 WORKSPACE=path         Path to the Arvados source tree to deploy from
16
17 EOF
18
19
20 IDENTIFIER=$1
21 DEPLOY_REPO=$2
22
23 if [[ "$IDENTIFIER" == '' || "$DEPLOY_REPO" == '' ]]; then
24   echo >&2 "$helpmessage"
25   echo >&2
26   exit 1
27 fi
28
29 # Sanity check
30 if ! [[ -n "$WORKSPACE" ]]; then
31   echo >&2 "$helpmessage"
32   echo >&2
33   echo >&2 "Error: WORKSPACE environment variable not set"
34   echo >&2
35   exit 1
36 fi
37
38 EXITCODE=0
39
40 COLUMNS=80
41
42 title () {
43   printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
44 }
45
46 # We only install capistrano in dev mode
47 export RAILS_ENV=development
48
49 source /etc/profile.d/rvm.sh
50 echo $WORKSPACE
51
52 # Weirdly, jenkins/rvm ties itself in a knot.
53 rvm use default
54
55 # Just say what version of ruby we're running
56 ruby --version
57
58 function run_puppet() {
59   node=$1
60   return_var=$2
61
62   TMP_FILE=`mktemp`
63   ssh -t -p2222 -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node.$IDENTIFIER -C "/usr/bin/puppet agent -t" | tee $TMP_FILE
64
65   ECODE=$?
66   RESULT=$(cat $TMP_FILE)
67
68   if [[ "$ECODE" != "255" && ! ("$RESULT" =~ 'already in progress') && "$ECODE" != "2" && "$ECODE" != "0"  ]]; then
69     # Puppet exists 255 if the connection timed out. Just ignore that, it's possible that this node is
70     #   a compute node that was being shut down.
71     # Puppet exits 2 if there are changes. For real!
72     # Puppet prints 'Notice: Run of Puppet configuration client already in progress' if another puppet process
73     #   was already running
74     echo "ERROR updating $node.$IDENTIFIER: exit code $ECODE"
75   fi
76   rm -f $TMP_FILE
77   echo
78   eval "$return_var=$ECODE"
79 }
80
81 function ensure_symlink() {
82   if [[ ! -L $WORKSPACE/$1 ]]; then
83     ln -s $WORKSPACE/$DEPLOY_REPO/$1 $WORKSPACE/$1
84   fi
85 }
86
87 # Check out/update the $DEPLOY_REPO repository
88 if [[ ! -d $DEPLOY_REPO ]]; then
89   mkdir $DEPLOY_REPO
90   git clone git@git.curoverse.com:$DEPLOY_REPO.git
91 else
92   cd $DEPLOY_REPO
93   git pull
94 fi
95
96 # Make sure the necessary symlinks are in place
97 cd "$WORKSPACE"
98 ensure_symlink "apps/workbench/Capfile.workbench.$IDENTIFIER"
99 ensure_symlink "apps/workbench/config/deploy.common.rb"
100 ensure_symlink "apps/workbench/config/deploy.curoverse.rb"
101 ensure_symlink "apps/workbench/config/deploy.workbench.$IDENTIFIER.rb"
102
103 ensure_symlink "services/api/Capfile.$IDENTIFIER"
104 ensure_symlink "services/api/config/deploy.common.rb"
105 ensure_symlink "services/api/config/deploy.curoverse.rb"
106 ensure_symlink "services/api/config/deploy.$IDENTIFIER.rb"
107
108 # Deploy API server
109 title "Deploying API server"
110 cd "$WORKSPACE"
111 cd services/api
112
113 bundle install --deployment
114
115 # make sure we do not print the output of config:check
116 sed -i'' -e "s/RAILS_ENV=production #{rake} config:check/RAILS_ENV=production QUIET=true #{rake} config:check/" $WORKSPACE/$DEPLOY_REPO/services/api/config/deploy.common.rb
117
118 bundle exec cap deploy -f Capfile.$IDENTIFIER
119
120 ECODE=$?
121
122 # restore unaltered deploy.common.rb
123 cd $WORKSPACE/$DEPLOY_REPO
124 git checkout services/api/config/deploy.common.rb
125
126 if [[ "$ECODE" != "0" ]]; then
127   title "!!!!!! DEPLOYING API SERVER FAILED !!!!!!"
128   EXITCODE=$(($EXITCODE + $ECODE))
129   exit $EXITCODE
130 fi
131
132 title "Deploying API server complete"
133
134 # Install updated debian packages
135 title "Deploying updated arvados debian packages"
136
137 ssh -p2222 root@$IDENTIFIER.arvadosapi.com -C "apt-get update && apt-get -qqy install arvados-src python-arvados-fuse python-arvados-python-client"
138
139 if [[ "$ECODE" != "0" ]]; then
140   title "!!!!!! DEPLOYING DEBIAN PACKAGES FAILED !!!!!!"
141   EXITCODE=$(($EXITCODE + $ECODE))
142   exit $EXITCODE
143 fi
144
145 title "Deploying updated arvados debian packages complete"
146
147 # Install updated arvados gems
148 title "Deploying updated arvados gems"
149
150 ssh -p2222 root@$IDENTIFIER.arvadosapi.com -C "/usr/local/rvm/bin/rvm default do gem install arvados arvados-cli && /usr/local/rvm/bin/rvm default do gem clean arvados arvados-cli"
151
152 if [[ "$ECODE" != "0" ]]; then
153   title "!!!!!! DEPLOYING ARVADOS GEMS FAILED !!!!!!"
154   EXITCODE=$(($EXITCODE + $ECODE))
155   exit $EXITCODE
156 fi
157
158 title "Deploying updated arvados gems complete"
159
160 # Deploy Workbench
161 title "Deploying workbench"
162 cd "$WORKSPACE"
163 cd apps/workbench
164 bundle install --deployment
165
166 # make sure we do not print the output of config:check
167 sed -i'' -e "s/RAILS_ENV=production #{rake} config:check/RAILS_ENV=production QUIET=true #{rake} config:check/" $WORKSPACE/$DEPLOY_REPO/apps/workbench/config/deploy.common.rb
168
169 bundle exec cap deploy -f Capfile.workbench.$IDENTIFIER
170
171 ECODE=$?
172
173 # restore unaltered deploy.common.rb
174 cd $WORKSPACE/$DEPLOY_REPO
175 git checkout apps/workbench/config/deploy.common.rb
176
177 if [[ "$ECODE" != "0" ]]; then
178   title "!!!!!! DEPLOYING WORKBENCH FAILED !!!!!!"
179   EXITCODE=$(($EXITCODE + $ECODE))
180   exit $EXITCODE
181 fi
182
183 title "Deploying workbench complete"
184
185 # Update compute node(s)
186 title "Update compute node(s)"
187
188 # Get list of nodes that are up
189 COMPRESSED_NODE_LIST=`ssh -p2222 root@$IDENTIFIER -C "sinfo --long -p crypto -r -o "%N" -h"`
190
191 if [[ "$COMPRESSED_NODE_LIST" != '' ]]; then
192   COMPUTE_NODES=`ssh -p2222 root@$IDENTIFIER -C "scontrol show hostname $COMPRESSED_NODE_LIST"`
193
194   SUM_ECODE=0
195   for node in $COMPUTE_NODES; do
196     echo "Updating $node.$IDENTIFIER"
197     run_puppet $node ECODE
198     SUM_ECODE=$(($SUM_ECODE + $ECODE))
199   done
200
201   if [[ "$SUM_ECODE" != "0" ]]; then
202     title "!!!!!! Update compute node(s) FAILED !!!!!!"
203     EXITCODE=$(($EXITCODE + $SUM_ECODE))
204   fi
205 fi
206
207 title "Update compute node(s) complete"
208
209 title "Update shell"
210
211 run_puppet shell ECODE
212
213 if [[ "$ECODE" == "2" ]]; then
214   # Puppet exits '2' if there are changes. For real!
215   ECODE=0
216 fi
217
218 if [[ "$ECODE" != "0" ]]; then
219   title "!!!!!! Update shell FAILED !!!!!!"
220   EXITCODE=$(($EXITCODE + $ECODE))
221 fi
222
223 title "Update shell complete"
224
225 title "Update keep0"
226
227 run_puppet keep0 ECODE
228
229 if [[ "$ECODE" == "2" ]]; then
230   # Puppet exits '2' if there are changes. For real!
231   ECODE=0
232 fi
233
234 if [[ "$ECODE" != "0" ]]; then
235   title "!!!!!! Update keep0 FAILED !!!!!!"
236   EXITCODE=$(($EXITCODE + $ECODE))
237 fi
238
239 title "Update keep0 complete"
240
241 exit $EXITCODE