1+ #
2+ # Licensed to the Apache Software Foundation (ASF) under one
3+ # or more contributor license agreements. See the NOTICE file
4+ # distributed with this work for additional information
5+ # regarding copyright ownership. The ASF licenses this file
6+ # to you under the Apache License, Version 2.0 (the
7+ # "License"); you may not use this file except in compliance
8+ # with the License. You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing,
13+ # software distributed under the License is distributed on an
14+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ # KIND, either express or implied. See the License for the
16+ # specific language governing permissions and limitations
17+ # under the License.
18+ #
19+ #! /bin/bash
20+
21+ # Print a spin
22+ #
23+ # Usage:
24+ # spin
25+ # ....
26+ # end_spin
27+ sp=" /-\|"
28+ sc=0
29+ spin () {
30+ printf " \b${sp: sc++: 1} "
31+ (( sc== ${# sp} )) && sc=0
32+ }
33+ end_spin () {
34+ printf " \r%s\n" " $@ "
35+ }
36+
37+ # Print a progress bar like that :
38+ # Progress : [########################################] 100%
39+ #
40+ # Usage:
41+ # progress_bar <progress> <max_progress>
42+ function progress_bar {
43+ if [ $1 -le $2 ]; then
44+ let _progress=(${1} * 100/${2} * 100)/100
45+ else
46+ let _one=($2 - 1)
47+ let _progress=(${_one} * 100/${2} * 100)/100
48+ fi
49+ let _done=(${_progress} * 4)/10
50+ let _left=40-$_done
51+
52+ _fill=$( printf " %${_done} s" )
53+ _empty=$( printf " %${_left} s" )
54+
55+ printf " \rProgress : [${_fill// /# }${_empty// / -} ] ${_progress} %%"
56+
57+ }
58+
59+ # Launch a playbook with retries, timeouts and prints its progress, and check if it succeeds or not
60+ #
61+ # Usage:
62+ # launch_playbook <playbook_name> "Success Message" "Error Message" <Expected_time_to_complete (Set it to 0, to print a spin instead of a progress bar)> <Timeout> <Number of retries> <true or false if it ahs to be launched remotely>
63+ #
64+ function launch_playbook() {
65+ local playbook=$1
66+ local success_message=$2
67+ local error_message=$3
68+ local expected_time=$4
69+ local command_timeout=$5
70+ local retry=$6
71+ local remote_launch=$7
72+
73+ local launch_tried=0
74+ local launch_failed=true
75+ local launch_timeout=false
76+
77+ while [ $launch_tried -le $retry ] ; do
78+ if [ $launch_tried -gt 0 ]; then
79+ logger info " Launching a retry because installation failed, retry is : #bold:$launch_tried out of $FREE_IPA_TRIES #end_bold possible tries"
80+ fi
81+
82+ if [ " ${DEBUG} " = " true" ]
83+ then
84+ if [ " $remote_launch " = " true" ] ; then
85+ logger debug " Command launched on remote node: ansible-playbook -i hosts --extra-vars @environment/extra_vars.yml ${playbook} .yml ${ANSIBLE_PYTHON_3_PARAMS} "
86+ else
87+ logger debug " Command launched: ansible-playbook -i ${HOSTS_FILE} playbooks/${playbook} /main.yml --extra-vars \" @/tmp/${playbook} _extra_vars.yml\" ${ANSIBLE_PYTHON_3_PARAMS} "
88+ fi
89+ fi
90+
91+ if [ " $remote_launch " = " true" ] ; then
92+ log_file=" ${LOG_DIR} /deployment.log"
93+ logger info " Follow progression in: #underline:$log_file "
94+ ssh ${NODE_USER} @${NODE_0} " cd ~/deployment/ansible-repo/ ; ansible-playbook -i hosts --extra-vars @environment/extra_vars.yml ${playbook} .yml ${ANSIBLE_PYTHON_3_PARAMS} " >> $log_file 2>&1 &
95+ else
96+ log_file=" ${LOG_DIR} /${playbook} .log"
97+ cp playbooks/${playbook} /extra_vars.yml /tmp/${playbook} _extra_vars.yml
98+ envsubst < /tmp/${playbook} _extra_vars.yml > /tmp/${playbook} _extra_vars.yml.tmp && mv /tmp/${playbook} _extra_vars.yml.tmp /tmp/${playbook} _extra_vars.yml
99+ logger info " Follow progression in: #underline:$log_file "
100+ ansible-playbook -i ${HOSTS_FILE} playbooks/${playbook} /main.yml --extra-vars " @/tmp/${playbook} _extra_vars.yml" ${ANSIBLE_PYTHON_3_PARAMS} > $log_file 2>&1 &
101+ fi
102+
103+ local pid=$!
104+ local start=$SECONDS
105+
106+ if [ " ${DEBUG} " = " true" ]
107+ then
108+ logger debug " pid is: $pid "
109+ fi
110+
111+ local duration=$(( SECONDS - start ))
112+ while [ $duration -lt $command_timeout ] ; do
113+ if [ $( ps -p $pid | wc -l) -eq 1 ] ; then
114+ break
115+ fi
116+ if [ $expected_time -gt 0 ] ; then
117+ progress_bar $duration $expected_time
118+ else
119+ spin
120+ fi
121+ sleep 0.5
122+ duration=$(( SECONDS - start ))
123+ done
124+ if [ $expected_time -gt 0 ] ; then
125+ end_spin
126+ fi
127+ if [ $duration -gt $command_timeout ] ; then
128+ launch_timeout=true
129+ fi
130+
131+ OUTPUT=$( tail -${ANSIBLE_LINES_NUMBER} $log_file | grep -A${ANSIBLE_LINES_NUMBER} RECAP | grep -v " failed=0" | wc -l | xargs)
132+ if [ " ${OUTPUT} " == " 2" ] && [ $launch_timeout = " false" ]
133+ then
134+ logger success " $success_message "
135+ logger info " "
136+ launch_failed=false
137+ break
138+ else
139+ logger warn " $error_message "
140+ if [ " ${DEBUG} " = " true" ]
141+ then
142+ logger debug " Will kill process with pid: $pid "
143+ fi
144+ kill -9 $pid 2>&1 > /dev/null
145+ fi
146+ launch_tried=$(( launch_tried + 1 ))
147+ # Not elegant but only workaround as of now for postgresql on RHEL 8
148+ if [ " $launch_timeout " = true ] && [ " $playbook " = " create_infrastructure" ] ; then
149+ ssh ${NODE_USER} @${NODE_0} " systemctl enable postresql-14"
150+ fi
151+ done
152+ if [ " ${launch_failed} " == " true" ]
153+ then
154+ logger error " $error_message "
155+ logger error " See details in file: #underline:$log_file "
156+ exit 1
157+ fi
158+
159+ }
0 commit comments