-
Notifications
You must be signed in to change notification settings - Fork 4
/
zanata-deploy.sh
executable file
·234 lines (181 loc) · 6.38 KB
/
zanata-deploy.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
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
#!/bin/bash -ex
# Script: zanata-deploy.sh
# Authors: [email protected], [email protected]
# This script deploys zanata.war from the local Maven repo
# to a target machine, based on a Jenkins job name
# and some configuration variables read from ~/.config/zanata-deploy.conf.
# Requirements:
# 1. env var WARNING_EMAIL must be set via zanata-deploy.conf
# 2. Jenkins vars BUILD_TAG, JOB_NAME, GIT_BRANCH set by Jenkins
# NB Jenkins will only set $GIT_BRANCH if the build config specifies a single branch
# 3. Jenkins job name should look like zanata-VER-PROFILE or zanata-build-deploy-VER-PROFILE
# 4. set m2repo or srcdir if you want to override defaults
# 5. minimal zanata-deploy.conf should look something like this:
#
# master_version=1.5
# host_1_4_jaas=jaastest.example.com
# host_1_4_internal=internaltest.example.com
# NB replace . with _ in variable names
# Each target box should be set up in a certain way.
# See the default values for url, user, service and targetfile near the
# end of this script, or override them for each affected host, eg:
# host_1_5_special=specialcase.example.com
# url_1_5_special=https://specialcase.example.com/
# user_1_5_special=jenkins
# service_1_5_special="sudo /etc/init.d/jbossas"
# targetfile_1_5_special="/opt/jboss-as/server/default/deploy/ROOT.war"
if [ -L $0 ] ; then
ME=$(readlink $0)
else
ME=$0
fi
DIR=$(dirname $ME)
source $HOME/.config/zanata-deploy.conf
BUILD_TAG=${BUILD_TAG-<unknown build>}
JOB_NAME=${JOB_NAME-<unknown job>}
WARNING_EMAIL=${[email protected]}
JBOSS_HOME=${JBOSS_HOME-/usr/share/jbossas/}
#JBOSS_PROFILE=${JBOSS_PROFILE-production}
ssh=${ssh-ssh}
scp=${scp-scp}
mail=${mail-mail}
m2repo=${m2repo-$HOME/.m2/repository}
BUILD_TYPES=(autotest kerberos fedora jaas)
# functions:
die() {
echo "zanata-deploy: $1" >&2
echo "zanata-deploy: $1" | $mail -s "zanata-deploy error" $WARNING_EMAIL
exit 0
}
arrayGet() {
local array=$1 index=$2
local i="${array}_${index}"
echo "${!i}"
}
warn() {
echo "WARNING: $1"
}
# main:
echo "BUILD_TAG: $BUILD_TAG"
echo "GIT_BRANCH: $GIT_BRANCH"
#if [[ $JOB_NAME =~ zanata-((build-)?deploy-)?(([^-][^-]*)-)?(.*) ]]; then
#branch_name=${BASH_REMATCH[3]}
#echo "${BASH_REMATCH[0]}"
#echo "${BASH_REMATCH[1]}"
#echo "${BASH_REMATCH[2]}"
#echo "${BASH_REMATCH[3]}" # version
#echo "${BASH_REMATCH[4]}" # type
#else
#die "can't find type of build for job name $JOB_NAME, for $BUILD_TAG"
#fi
branch_name=$GIT_BRANCH
if [[ "$branch_name" == "" ]]; then
die "can't determine branch name for $BUILD_TAG"
fi
if [[ "$branch_name" == "master" ]]; then
version=$master_version
else
version=$branch_name
fi
srcdir=${srcdir-${m2repo}/org/zanata/zanata-war/${version}-SNAPSHOT}
echo "branch: $branch_name"
echo "version: $version"
echo "srcdir: $srcdir"
# replace . with _ in version:
ver=${version//./_}
# attempt to deploy for each authentication type
for buildType in "${BUILD_TYPES[@]}"
do
host=$(arrayGet host ${ver}_${buildType})
if [[ -z $host ]]; then
warn "no host configured for version $ver, type $buildType, and build $BUILD_TAG"
elif [[ "$host" != "skip" ]]; then
echo "=================================================================================="
echo "Deploying: version $ver type $buildType"
echo "=================================================================================="
url=$(arrayGet url ${ver}_${buildType})
if [[ -z $url ]]; then
url=https://$host/
fi
user=$(arrayGet user ${ver}_${buildType})
if [[ -z $user ]]; then
user=jboss
fi
service=$(arrayGet service ${ver}_${buildType})
if [[ -z $service ]]; then
# service="JBOSS_USER=RUNASIS /etc/init.d/jbossas"
service="service jbossas"
fi
targetfile=$(arrayGet targetfile ${ver}_${buildType})
if [[ -z $targetfile ]]; then
targetfile=$JBOSS_HOME/standalone/deployments/ROOT.war
fi
if [[ $targetfile =~ (.*)/deployments/.* ]]; then
logfile=${BASH_REMATCH[1]}/log/server.log
else
logfile=/dev/null
fi
post_stop=$(arrayGet post_stop ${ver}_${buildType})
echo "host: $host"
echo "url: $url"
echo "user: $user"
echo "service: $service"
echo "targetfile: $targetfile"
echo "post_stop: $post_stop"
echo "logfile: $logfile"
skipdeployfile=${targetfile}.skipdeploy
echo "create $skipdeployfile in $host"
$ssh $user@$host touch $skipdeployfile
echo "stopping app server on $host:"
if ! $ssh $user@$host $service stop
then echo "$server stop failed (server not running?); ignoring error"
fi
if [[ -n $post_stop ]]; then
$ssh $user@$host $post_stop
fi
echo "removing previous file/dir $targetfile on $host"
$ssh $user@$host rm -rf $targetfile
# tmp dir will grow forever otherwise:
$ssh $user@$host rm -fr $JBOSS_HOME/standalone/tmp/
warfile=${srcdir}/zanata-war-*.war
echo "copying $warfile to $host:$targetfile"
$scp $warfile $user@$host:$targetfile
echo "remove $skipdeployfile in $host"
$ssh $user@$host rm -rf $skipdeployfile
echo "starting app server on $host"
$ssh $user@$host $service start
fi
done
# Check that each server is up and running
for buildType in "${BUILD_TYPES[@]}"
do
host=$(arrayGet host ${ver}_${buildType})
if [[ -z $host ]]; then
warn "no host configured for version $ver, type $buildType, and build $BUILD_TAG"
elif [[ "$host" != "skip" ]]; then
echo "=================================================================================="
echo "Checking host: $host"
echo "=================================================================================="
url=$(arrayGet url ${ver}_${buildType})
if [[ -z $url ]]; then
url=https://$host/
fi
user=$(arrayGet user ${ver}_${buildType})
if [[ -z $user ]]; then
user=jboss
fi
if [[ $targetfile =~ (.*)/deployments/.* ]]; then
logfile=${BASH_REMATCH[1]}/log/server.log
else
logfile=/dev/null
fi
if $DIR/is_server_up.sh $url ; then
echo "$url has started up; log tail follows:"
ssh $user@$host tail $logfile
else
echo "$url has failed to start; log tail follows:"
ssh $user@$host tail -400 $logfile
exit 1
fi
fi
done