@@ -5,15 +5,28 @@ set -eu
55set -x
66set -o pipefail
77
8- INVENTORY=/var/lib/ansible/inventory
9- NODESFILE=/var/lib/ansible/node_list
8+ ANSDIR=/var/lib/ansible
9+ INVENTORY=$ANSDIR /inventory
10+ NODESFILE=$ANSDIR /node_list
11+
12+ function get_new_nodes() {
13+ # compare old and new list of nodes and return all newly added nodes
14+ # separated by comma instead of newline
15+ if [ -e ${ANSDIR} .deployed ]; then
16+ str=$( comm -13 <( sort ${ANSDIR} .deployed/node_list) <( sort ${ANSDIR} /node_list) | sed ' :a;N;$!ba;s/\n/","/g' )
17+ [ -z " $str " ] && echo ' ' || echo " \" $str \" "
18+ else
19+ echo ' '
20+ fi
21+ }
1022
1123function create_metadata_json() {
1224 # $1 - metadata filename
1325 infra_arr=($all_infra_nodes )
1426 infra_count=${# infra_arr[@]}
1527 master_arr=($all_master_nodes )
1628 master_count=${# master_arr[@]}
29+ new_nodes=$( get_new_nodes)
1730 if [ -n " $os_username " ] && [ -n " $os_password " ] && \
1831 [ -n " $os_auth_url " ] && [ -n " $os_tenant_name " ]; then
1932 openstack_cloud_provider=true
@@ -40,6 +53,7 @@ function create_metadata_json() {
4053 "infra_nodes": ["$( echo " $all_infra_nodes " | sed ' s/ /","/g' ) "],
4154 "infra_count": $infra_count ,
4255 "nodes": ["$( sed ' :a;N;$!ba;s/\n/","/g' $NODESFILE ) "],
56+ "new_nodes": [$new_nodes ],
4357 "deploy_router_or_registry": $deploy_router_or_registry ,
4458 "domainname": "$domainname ",
4559 "lb_hostname": "$lb_hostname ",
@@ -64,7 +78,7 @@ function create_metadata_json() {
6478 "ldap_url": "$ldap_url ",
6579 "ldap_preferred_username": "$ldap_preferred_username ",
6680 "bastion_instance_id": "$bastion_instance_id ",
67- "ansible_first_run": $( [ -e ${INVENTORY } .deployed ] && echo false || echo true) ,
81+ "ansible_first_run": $( [ -e ${ANSDIR } .deployed ] && echo false || echo true) ,
6882 "router_vip": "$router_vip ",
6983 "volume_quota": $volume_quota
7084}
@@ -107,11 +121,15 @@ EOF
107121}
108122
109123function is_scaleup() {
110- # check if there are only new openshift nodes added - then we can play the
111- # scaleup playbook, otherwise we run the main playbook
112- [ -e ${INVENTORY} .deployed ] || return 1
113- (diff $INVENTORY ${INVENTORY} .deployed |
114- grep ' ^[<>]' | grep -v ' ^< .*-node' ) && return 1 || return 0
124+ # check if there are only new openshift nodes added - then we can play the
125+ # scaleup playbook, otherwise we run the main playbook
126+ [ -e ${ANSDIR} .deployed ] || return 1
127+ # check if diff between old and new inventory file contains only
128+ # node changes (ignore 'new_nodes' changes because nodes
129+ # are removed from [new_nodes] on the next stack-update run
130+ (diff $ANSDIR /inventory ${ANSDIR} .deployed/inventory | grep ' ^[<>]' |
131+ grep -v new_nodes | grep -v ' [<>] $' |
132+ grep -v ' .*-node' ) && return 1 || return 0
115133}
116134
117135function update_etc_hosts() {
@@ -154,13 +172,14 @@ while pidof -x /bin/ansible-playbook /usr/bin/ansible-playbook; do
154172 sleep 10
155173done
156174
157- if [ -e ${INVENTORY } .deployed ] &&
158- diff $INVENTORY ${INVENTORY } .deployed; then
175+ if [ -e ${ANSDIR } .deployed ] &&
176+ diff $ANSDIR /inventory ${ANSDIR } .deployed/inventory ; then
159177 echo " inventory file has not changed since last ansible run, no need to re-run"
160178 exit 0
161179fi
162180
163- cp ${INVENTORY} ${INVENTORY} .started
181+ [ -e ${ANSDIR} .started ] && rm -rf ${ANSDIR} .started
182+ cp -a ${ANSDIR} ${ANSDIR} .started
164183
165184# crond was stopped in cloud-init before yum update, make sure it's running
166185systemctl status crond && systemctl restart crond
@@ -183,5 +202,6 @@ if ! $cmd > $logfile 2>&1; then
183202 echo " Failed to run '$cmd ', full log is in $( hostname) :$logfile " >&2
184203 exit 1
185204else
186- mv ${INVENTORY} .started ${INVENTORY} .deployed
205+ [ -e ${ANSDIR} .deployed ] && rm -rf ${ANSDIR} .deployed
206+ mv ${ANSDIR} .started ${ANSDIR} .deployed
187207fi
0 commit comments