forked from stevelle/jenkins-rpc
/
commit-multinode.sh
173 lines (142 loc) · 4.79 KB
/
commit-multinode.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
#!/usr/bin/env bash
### -------------- [ Variables ] --------------------
TAGS=${TAGS:-prepare,run,test}
OS_ANSIBLE_BRANCH=${OS_ANSIBLE_BRANCH:-master}
GERRIT_REFSPEC=${GERRIT_REFSPEC:-refs/changes/87/139087/14}
ANSIBLE_OPTIONS=${ANSIBLE_OPTIONS:--v}
TEMPEST_SCRIPT_PARAMETERS=${TEMPEST_SCRIPT_PARAMETERS:-scenario}
### -------------- [ Functions ] --------------------
env
cluster_tool(){
python - $@ <<EOP
import requests
import argparse
import sys
import os
def cluster_for_claim(clusters, claim):
for cluster in clusters.json():
if cluster.get('claim') == claim:
print(cluster['short_name'])
return 0
return 1
def check_release(clusters, name):
for cluster in clusters.json():
if cluster['short_name'] == name and cluster['claim'] == "":
return 0
return 1
def main():
parser=argparse.ArgumentParser()
parser.add_argument('command', choices=['cluster_for_claim','check_release'])
parser.add_argument('arg')
args = parser.parse_args()
base_url = "${DJEEP_URL}/api"
clusters = requests.get('%(base_url)s/clusters' % {'base_url': base_url} )
router = {'cluster_for_claim': cluster_for_claim,
'check_release': check_release}
return router[args.command](clusters, args.arg)
if __name__ == "__main__":
sys.exit(main())
EOP
}
run_jenkins_rpc_playbook_tag(){
echo "Running tag ${1} from jenkins-rpc/commit-multinode.yml"
ansible-playbook \
-i inventory/commit-cluster-$CLUSTER_NUMBER\
-e@vars/packages.yml\
-e@vars/pip.yml\
-e@vars/kernel.yml\
-e@vars/commit-multinode.yml\
-e cluster_number=${CLUSTER_NUMBER}\
-e GERRIT_REFSPEC=${GERRIT_REFSPEC}\
-e os_ansible_branch=${OS_ANSIBLE_BRANCH}\
--tags $1\
$ANSIBLE_OPTIONS\
commit-multinode.yml
}
ssh_command(){
#Find the first node ip from the inventory
[[ -z $infra_1_ip ]] && infra_1_ip=$(grep -o -m 1 '10.127.[0-9]\+.[0-9]\+' \
< inventory/commit-cluster-$CLUSTER_NUMBER)
: >> /tmp/env
scp script_env $infra_1_ip:/tmp/env
echo "Running command ${1}"
ssh root@$infra_1_ip ". /tmp/env; ${1}"
}
ssh_osad_script(){
echo "Running script ${1} from os-ansible-deployment/scripts."
ssh_command "cd ~/rpc_repo; bash scripts/${1}.sh"
}
prepare(){
run_jenkins_rpc_playbook_tag prepare
}
run(){
echo "export DEPLOY_TEMPEST=yes" > script_env
ssh_osad_script run-playbooks
}
test(){
echo "export TEMPEST_SCRIPT_PARAMETERS=${TEMPEST_SCRIPT_PARAMETERS}" > script_env
ssh_osad_script run-tempest
}
clean(){
run_jenkins_rpc_playbook_tag clean
}
_claim(){
if [[ ! -z "$CLUSTER_NAME" ]]
then
echo "Claiming name: $CLUSTER_NAME with claim: $CLUSTER_CLAIM"
curl -X POST $DJEEP_URL/api/cluster/claim/$CLUSTER_CLAIM/$CLUSTER_NAME 2>/dev/null
else
echo "Claiming cluster with prefix $CLUSTER_PREFIX with claim: $CLUSTER_CLAIM"
curl -X POST $DJEEP_URL/api/cluster/claim/$CLUSTER_CLAIM/prefix/$CLUSTER_PREFIX 2>/dev/null
fi
}
claim(){
until _claim | tee cluster | grep claimed
do
sleep 5
done
export CLUSTER_NAME=$(awk '/claimed/{print $2}' < cluster)
export CLUSTER_NUMBER=${CLUSTER_NAME#dev_sat6_jenkins_}
# Check cluster status to ensure the claim is correct.
[[ "$CLUSTER_NAME" == "$(cluster_tool cluster_for_claim $CLUSTER_CLAIM)" ]]
}
release(){
echo "Releasing claim $CLUSTER_CLAIM from $CLUSTER_NAME"
curl -X DELETE $DJEEP_URL/api/cluster/claim/$CLUSTER_CLAIM/$CLUSTER_NAME 2>/dev/null
# Check that the claim has been released correctly
cluster_tool check_release $CLUSTER_NAME
}
upgrade(){
ssh_command "cd ~/rpc_repo;
git checkout $UPGRADE_BRANCH;
echo YES | bash scripts/run-upgrade.sh"
}
# A propterties file (Java key=value format) is produced to be read by the
# parent jenkins job. This is then used to inject CLUSTER_{NAME,CLAIM} into
# the env for the cleanup job, so the correct cluster is cleaned and released
write_properties(){
{
for var in $@
do
echo "${var}=${!var}"
done
} > properties
}
### -------------- [ Main ] --------------------
export CLUSTER_NUMBER=${CLUSTER_NAME#dev_sat6_jenkins_}
# Write properties is called early even though claim has not run yet, as
# CLUSTER_NAME may have been passed in. Write properties is also called after
# each tag incase claim has run. Its important that write_properties runs as
# early as possible as cleanup will fail if the necessary info has not been written.
write_properties CLUSTER_NAME CLUSTER_CLAIM
# run the tags that are required until something breaks
rc=0
for tag in ${TAGS}
do
$tag
rc=$(( $rc + $? ))
# must continue to the end incase tags include cleanup or release
#[[ $rc -ne 0 ]] && break
write_properties CLUSTER_NAME CLUSTER_CLAIM
done
exit $rc