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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 # Collection of variables and functions for the bootstrap.sh
29 CMCLI="/usr/local/bin/cmcli --ip $CM_IP --port $CM_PORT --client-lib cmframework.lib.cmclientimpl.CMClientImpl"
30 CM_ACTIVATORS=/opt/cmframework/activators
32 CM_VALIDATORS=/opt/cmframework/validators
33 CM_INVENTORY_HANDLERS=/opt/cmframework/inventoryhandlers
35 DB_CHECK_CMD="/bin/redis-cli -h $DB_IP --scan --pattern '*'"
37 DB_STARTUP_CMD="/bin/redis-server ./redis.conf"
38 INVENTORY_FILE=/opt/cmframework/inventory.data
39 STATE_FILE=/etc/installation_state
40 USER_CONFIG_HANDLERS=/opt/cmframework/userconfighandlers
43 # Source log variables and functions
45 source $(dirname "${BASH_SOURCE[0]}")/log.sh
56 result=$(eval "$*" 2>&1)
58 if [ $ret -ne 0 ]; then
59 log_error "Failed with error $result"
61 log_info "Command succeeded: $result"
67 function stop_process()
70 log_info "Stopping process $pid"
71 if ! [ -z $pid ]; then
72 if [ -d /proc/$pid ]; then
73 log_info "Shutting down process $pid gracefully"
74 run_cmd "pkill -TERM -g $pid"
75 log_info "Waiting for process $pid to exit"
76 for ((i=0; i<10; i++)); do
77 if ! [ -d /proc/$pid ]; then
78 log_info "Process $pid exited"
81 log_info "Process $pid is still running"
85 if [ -d /proc/$pid ]; then
86 log_error "Process $pid is still running, forcefully shutting it down"
87 run_cmd "pkill -KILL -g $pid"
95 log_info "Cleaning up"
103 log_info "Starting redis db using $DB_STARTUP_CMD"
104 systemctl start redis
105 log_info "Wait till DB is serving"
108 for ((i=0; i<10; i++)); do
109 run_cmd "$DB_CHECK_CMD"
111 if [ $dbok -eq 0 ]; then
114 log_info "DB still not running"
123 rm -f "${STATE_FILE}"
124 log_info "Starting CM server"
125 setsid /usr/local/bin/cmserver --ip $CM_IP --port $CM_PORT \
126 --backend-api cmframework.redisbackend.cmredisdb.CMRedisDB \
127 --backend-uri redis://:@$DB_IP:$DB_PORT \
129 --validators $CM_VALIDATORS \
130 --activators $CM_ACTIVATORS \
131 --disable-remote-activation \
134 --inventory-handlers $CM_INVENTORY_HANDLERS \
135 --inventory-data $INVENTORY_FILE \
136 --activationstate-handler-api cmframework.utils.cmdsshandler.CMDSSHandler \
137 --activationstate-handler-uri /run/.dss-server \
138 --alarmhandler-api cmframework.lib.cmalarmhandler_dummy.AlarmHandler_Dummy \
140 --snapshot-handler-api cmframework.utils.cmdsshandler.CMDSSHandler \
141 --snapshot-handler-uri /run/.dss-server \
142 --verbose 2> "$CM_LOG" 1>&2 &
144 log_info "cmserver pid is $CM_PID"
145 if ! [ -d /proc/$CM_PID ]; then
146 log_error "CM server is not running!"
147 log_info "Check redis.log and $BOOTSTRAP_LOG for details"
151 log_info "Wait till cmserver is ready to serve"
154 out=$($CMCLI get-properties --matching-filter '.*' 2>&1)
155 if [ $? -eq 0 ]; then
158 echo $out | grep "Not found" 2> /dev/null 1>&2
159 if [ $? -eq 0 ]; then
162 log_info "cmserver not ready yet, got error $out"
168 function handle_user_config()
170 log_info "Handling user configuration from file $CONFIG_FILE"
171 run_cmd "$CMCLI bootstrap --config $CONFIG_FILE --plugin_path $USER_CONFIG_HANDLERS"
175 function start_installation()
177 log_info "Start installation"
182 function wait_installation_complete()
184 log_info "Waiting for installation to complete"
186 if [ -f $STATE_FILE ]; then
187 log_info "installation completed"
193 result=$(cat $STATE_FILE)
195 if [ "$result" == "success" ]; then
196 log_info "exiting with success :)"
199 log_error "exiting with failure :("
204 function execute_post_install()
206 log_info "Start post installation"
210 function parameter_exists_in_list()
212 local EXPECTED_PARAM=$1
216 for PARAM in ${PARAM_LIST}
218 if [ "${PARAM}" == "${EXPECTED_PARAM}" ]
226 function get_next_boot_args()
228 GRUB_CMDLINE_LINUX=""
229 GRUB_CMDLINE_LINUX_DEFAULT=""
231 if source /etc/default/grub > /dev/null
233 echo "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
237 function get_current_boot_args()
239 local CURRENT_BOOT_ARGS
240 local CMDLINE_PATTERN='BOOT_IMAGE='
241 CURRENT_BOOT_ARGS=$(grep ${CMDLINE_PATTERN} /proc/cmdline)
242 CURRENT_BOOT_ARGS=${CURRENT_BOOT_ARGS#${CMDLINE_PATTERN}}
243 CURRENT_BOOT_ARGS=${CURRENT_BOOT_ARGS#\"}
244 CURRENT_BOOT_ARGS=${CURRENT_BOOT_ARGS%\"}
245 echo "${CURRENT_BOOT_ARGS}"
248 function has_kernel_parameters_changed()
250 local CURRENT_BOOT_ARGS
251 CURRENT_BOOT_ARGS=$(get_current_boot_args)
253 for NEXT_BOOT_ARG in $(get_next_boot_args)
255 if ! parameter_exists_in_list "${NEXT_BOOT_ARG}" "${CURRENT_BOOT_ARGS}"
257 log_info "kernel parameter <${NEXT_BOOT_ARG}> does not exist in [${CURRENT_BOOT_ARGS}]"
264 function reboot_host()
267 log_info "Reboot the host in ${DELAY} seconds to apply the kernel parameter changes"