Newer
Older
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
#!/bin/bash
REBOOT_TIME=90
NODETYPES=${@}
# make sure you define variable STACKNAME in current environment
if [[ -z $STACKNAME ]]; then
echo "please define STACKNAME variable"
exit 1
fi
# prepend CICD to stack name
if [[ "$STACKNAME" == "CICD"* ]]; then
echo "CICD found in stackname. doing nothing"
else
STACKNAME="CICD"$STACKNAME
fi
function check_stack_exists {
if openstack stack list | grep -w $STACKNAME;
then
echo "stack found";
else
echo "stack not found";
return 1
fi
}
if ! check_stack_exists
then
exit 0
fi
# return and dictionary in json format, which map server name to ansible_host_groups. There will be IndexError if the servers, which names are prefixed by STACKNAME, don't have ansible_host_groups property
host_group_mapping=$(openstack server list --long -f json | python3 -c "import json,sys,re;ivt=json.load(sys.stdin);json.dump({i['Name']: re.search('ansible_host_groups\=\'\[(.+)\]\'', i['Properties']).groups()[0].replace('\"', '').split() for i in ivt if re.match('$STACKNAME',i['Name'])}, fp=sys.stdout)")
# all available ansible_host_groups
available_groups=$(echo $host_group_mapping | python3 -c "import json,sys;mapping=json.load(sys.stdin);output=[];[output.extend(v) for v in mapping.values()];print(output)" | tr -d "[',]")
# if the first input parameter is all then rebuild all groups
if [[ "$1" == "all" ]]; then
NODETYPES=$available_groups
fi
echo "going to update group $NODETYPES"
server_list=$(echo $host_group_mapping | python3 -c "import json,sys;mapping=json.load(sys.stdin);avail_groups='$available_groups'.split();print([k for k in mapping.keys() if len(set(mapping[k]).intersection(set(avail_groups))) > 0])" | tr -d "[,]'" | xargs -n1 | sort -u)
rebuild_func(){
echo "rebuilding server $1"
openstack server rebuild --wait $1
}
# for eaech line in data
for server in $server_list
do
rebuild_func $server & # run parallel in background
done
wait # wait for all servers to be rebuild
# add an extra time for reboot
echo "waiting for reboot"
sleep $REBOOT_TIME
echo "All done"