Added progress reporting
[ta/config-manager.git] / cmframework / scripts / bootstrap.sh
1 #!/bin/bash
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 COMMAND=$(basename "${0}")
18 DO_REBOOT_IF_NEEDED=true
19
20 #
21 # Source log variables and functions
22 #
23 source $(dirname "${BASH_SOURCE[0]}")/log.sh
24
25 function source_common()
26 {
27     local SCRIPT_PATH
28     SCRIPT_PATH=$(dirname "${BASH_SOURCE[0]}")
29     local COMMON_SH_FILE=${SCRIPT_PATH}/common.sh
30     # shellcheck disable=SC1091
31     # shellcheck source=.
32     source "${COMMON_SH_FILE}" && return 0
33     log_error "Failed to source ${COMMON_SH_FILE}"
34     return 1
35 }
36
37 export CONFIG_PHASE="bootstrapping"
38
39 function main()
40 {
41     log_info "Bootstrapping started"
42
43     declare -a FUNCTIONS=(source_common start_db start_cm start_installation)
44     local func
45     for func in "${FUNCTIONS[@]}"
46     do
47         if ! ${func}
48         then
49             cleanup
50             return 1
51         fi
52     done
53
54     local rc
55     wait_installation_complete
56     rc=$?
57
58     if [ $rc -eq 0 ]; then
59         export CONFIG_PHASE="postconfig"
60         log_info "Generate inventory file to prepare for extra playbooks run"
61         run_cmd "$CMCLI ansible-inventory > $INVENTORY_FILE"
62
63         admin="x"
64         for d in $(ls -d /home/*); do 
65             if [ -f "$d/openrc" ]; then
66                 admin=$(basename "$d")
67                 break
68             fi
69         done
70
71         #take a copy of the initial configuration data
72         mkdir /root/.initconfig
73         cp /var/lib/redis/dump.rdb /root/.initconfig/
74
75         su - "$admin" -c "/usr/local/bin/openstack-ansible -b -u $admin /opt/openstack-ansible/playbooks/finalize-playbook.yml" &>> $BOOTSTRAP_LOG
76         rc=$?
77
78         if [ $rc -eq 0 ]; then
79             execute_post_install
80             rc=$?
81         fi
82
83     fi
84
85     cleanup
86
87     log_info "starting redis again"
88     systemctl start redis
89
90     if [ $rc -eq 0 ]; then
91         if has_kernel_parameters_changed;
92         then
93             # The status of the installation will be logged by one of the following services after the host is rebooted.
94             #
95             # 1) finalize-bootstrap.service: When the performance porfile is enabled on the controller-1 and
96             # the network type is "ovs"
97             # 2) enable-dpdk.service: When the performance profile is enabled on the controller-1 and the
98             # network type is "ovs-dpdk"
99             #
100             if [ ${DO_REBOOT_IF_NEEDED} == true ]; then
101                 reboot_host
102             else
103                 log_info "Rebooting of host is skipped as requested."
104             fi
105         else
106             log_installation_success
107             /opt/openstack-ansible/playbooks/report-installation-progress --status completed --description "Installation complete" --percentage 100
108         fi
109     else
110         log_installation_failure
111         /opt/openstack-ansible/playbooks/report-installation-progress --status failed --description "Installation failed"
112     fi
113
114     return $rc
115 }
116
117 function show_help()
118 {
119     echo "Usage:"
120     echo "# ${COMMAND} <full path to user-config-yaml|restore-config-yaml>"
121     echo "Or to skip the controller-1 reboot in the case kernel boot parameters are changed"
122     echo "# ${COMMAND} <full path to user-config-yaml|restore-config-yaml> (--install | --restore) --no-reboot"
123 }
124
125 #
126 # Assume that the first argument is the configuration file to maintain backwards compatibility
127 # so handle it separately.
128 #
129
130 if [ $# -lt 1 ]; then
131     show_help
132     exit 1
133 else
134     CONFIG_FILE=$1
135     shift
136 fi
137
138 if ! [ -f "${CONFIG_FILE}" ]; then
139     log_error "Failed to open file:${CONFIG_FILE}"
140     show_help
141     exit 1
142 fi
143
144
145 #
146 # And then the remaing arguments in any order
147 #
148
149 IS_INSTALL_ARG_SPECIFIED=false
150 for arg in "$@"
151 do
152     case ${arg} in
153         --no-reboot)
154             DO_REBOOT_IF_NEEDED=false
155             shift
156         ;;
157         --install)
158             IS_INSTALL_ARG_SPECIFIED=true
159             shift
160         ;;
161         --help)
162             show_help
163             exit 0
164         ;;
165         *)
166             log_error "Unknown option: ${arg}"
167             show_help
168             exit 1
169         ;;
170     esac
171 done
172
173 log_info "====================================================================="
174 log_info "Boot strapping the environment with $CONFIG_FILE"
175 log_info "====================================================================="
176
177 main