Initial commit
[ta/config-manager.git] / cmframework / scripts / start-cmserver-db.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 DB_PORT=6379
18 DB_IP=127.0.0.1
19 LOG_BASE_DIR=/srv/deployment/log
20 DB_LOG=${LOG_BASE_DIR}/redis.log
21
22 CM_PORT=61100
23 CM_IP=127.0.0.1
24 CM_VALIDATORS=/opt/cmframework/validators
25 CM_ACTIVATORS=/opt/cmframework/activators
26 CM_LOG=${LOG_BASE_DIR}/cm.log
27 USER_CONFIG_HANDLERS=/opt/cmframework/userconfighandlers
28 INVENTORY_HANDLERS=/opt/cmframework/inventoryhandlers
29 INVENTORY_DATA=/opt/cmframework/inventory.data
30
31
32 BOOTSTRAP_LOG=${LOG_BASE_DIR}/bootstrap.log
33
34 DB_STARTUP_CMD="/bin/redis-server ./redis.conf"
35 DB_CHECK_CMD="/bin/redis-cli -h $DB_IP --scan --pattern '*'"
36 CMCLI="/usr/local/bin/cmcli --ip $CM_IP --port $CM_PORT --client-lib cmframework.lib.cmclientimpl.CMClientImpl"
37 USERCONFIG=
38
39 export CONFIG_PHASE="bootstrapping"
40
41 DB_PID=
42 CM_PID=
43
44 function log()
45 {
46     priority=$1
47
48     echo "$(date) ($priority) ${FUNCNAME[2]} ${@:2}"
49     echo "$(date) ($priority) ${FUNCNAME[2]} ${@:2}" >> $BOOTSTRAP_LOG
50 }
51
52 function log_info()
53 {
54     log info $@
55 }
56
57 function log_error()
58 {
59     log error $@
60 }
61
62
63 function run_cmd()
64 {
65     log_info "Running $@"
66     result=$(eval $@ 2>&1)
67     ret=$?
68     if [ $ret -ne 0 ]; then
69         log_error "Failed with error $result"
70     else
71         log_info "Command succeeded: $result"
72     fi
73
74     return $ret
75 }
76
77 function stop_process()
78 {
79     pid=$1
80     log_info "Stopping process $pid"
81     if ! [ -z $pid ]; then
82         if [ -d /proc/$pid ]; then
83             log_info "Shutting down process $pid gracefully"
84             run_cmd "pkill -TERM -g $pid"
85             log_info "Waiting for process $pid to exit"
86             for ((i=0; i<10; i++)); do
87                 if ! [ -d /proc/$pid ]; then
88                     log_info "Process $pid exited"
89                     break
90                 fi
91                 log_info "Process $pid is still running"
92                 sleep 2
93             done
94
95             if [ -d /proc/$pid ]; then
96                 log_error "Process $pid is still running, forcefully shutting it down"
97                 run_cmd "pkill -KILL -g $pid"
98             fi
99         fi
100     fi
101 }
102
103 function cleanup()
104 {
105     log_info "Cleaning up"
106     #stop_process $DB_PID
107     systemctl stop redis
108     stop_process $CM_PID
109 }
110
111 function start_db()
112 {
113     log_info "Starting redis db using $DB_STARTUP_CMD"
114     #setsid $DB_STARTUP_CMD &
115     #export DB_PID=$!
116     #log_info "DB pid is $DB_PID"
117     #if ! [ -d /proc/$DB_PID ]; then
118     #    log_error "DB is not running!"
119     #    log_info "Check /var/log/redis.log and $BOOTSTRAP_LOG for details"
120     #    return 1
121     #fi
122     systemctl start redis
123     log_info "Wait till DB is serving"
124     dbok=0
125     for ((i=0; i<10; i++)); do
126         run_cmd "$DB_CHECK_CMD"
127         dbok=$?
128         if [ $dbok -eq 0 ]; then
129             break
130         fi
131         log_info "DB still not running"
132         sleep 2
133     done
134
135     return $dbok
136 }
137
138 function start_cm()
139 {
140     log_info "Starting CM server"
141     setsid /usr/local/bin/cmserver --ip $CM_IP --port $CM_PORT --backend-api cmframework.redisbackend.cmredisdb.CMRedisDB --backend-uri redis://:@$DB_IP:$DB_PORT --activationstate-handler-api cmframework.utils.cmstatedummyhandler.CMStateDummyHandler --activationstate-handler-uri dummy_uri --snapshot-handler-api cmframework.utils.cmstatedummyhandler.CMStateDummyHandler --alarmhandler-api cmframework.lib.cmalarmhandler_dummy.AlarmHandler_Dummy --snapshot-handler-uri dummy_uri --inventory-handlers $INVENTORY_HANDLERS --inventory-data $INVENTORY_DATA --validators $CM_VALIDATORS --activators $CM_ACTIVATORS --disable-remote-activation --log-dest console --log-level debug --verbose 2> $CM_LOG 1>&2 &
142     export CM_PID=$!
143     log_info "cmserver pid is $CM_PID"
144     if ! [ -d /proc/$CM_PID ]; then
145         log_error "CM server is not running!"
146         log_info "Check redis.log and $BOOTSTRATP_LOG for details"
147         return 1
148     fi
149     return 0
150 }
151
152 function handle_user_config()
153 {
154     log_info "Handling user configuration from file $USER_CONFIG"
155     run_cmd "$CMCLI bootstrap --config $USER_CONFIG --plugin_path $USER_CONFIG_HANDLERS"
156     return $?
157 }
158
159 function main()
160 {
161     # start the database
162     start_db
163     if [ $? -ne 0 ]; then
164         cleanup
165         return 1
166     fi
167    
168     # start the configuration management server
169     start_cm
170     if [ $? -ne 0 ]; then
171         cleanup
172         return 1
173     fi
174
175     echo "============"
176     jobs -p
177
178     echo "Use CLI $CMCLI"
179
180     while [ 1 ]; do
181         read -n1 -r -p "Press space to exit..." key
182
183         if [ "$key" = '' ]; then
184             echo "exiting..."
185             break
186         fi
187     done
188
189     cleanup
190     return 0
191 }
192
193 if [ $# -ne 1 ]; then
194     echo "Usage:$0 <cmserver ip>"
195     exit 1
196 fi
197
198 CM_IP=$1
199 CMCLI="/usr/local/bin/cmcli --ip $CM_IP --port $CM_PORT --client-lib cmframework.lib.cmclientimpl.CMClientImpl"
200 mkdir -p ${LOG_BASE_DIR}
201
202 main