Initial commit
[ta/config-manager.git] / cmframework / scripts / start-private-cmserver.sh
1 #! /bin/bash
2
3
4 # Copyright 2019 Nokia
5
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # 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, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 :'
19 This script can be used to start a private cmserver to test your CM userconfighandler and/or validator plugins.
20
21 To test userconfighandlers and installation time validators do the following:
22 - Copy your userconfighandler to a target system under /opt/cmframework/userconfighandlers/ directory.
23
24 - Copy your validator to a target system under /opt/cmframework/validators directory.
25
26 - Start a private cmserver
27   ./start-private-cmserver.sh <some port number> no-cm-data
28   
29 - The above command will print information about the temporary directory used for the private cmserver, it will also print the command to be used to access the cmserver e.g. the output can look like:
30
31 =======================================================================================================================
32 Use CLI /opt/bin/cmcli --ip 127.0.0.1 --port 51110
33 Root DIR /tmp/tmp.HcyehIQQoQ
34 =======================================================================================================================
35
36 - Test your plugins by running the following command:
37   /opt/bin/cmcli --ip 127.0.0.1 --port <port> bootstrap --config /opt/userconfig/user_config.yaml --plugin_path /<cmserver root>/opt/cmframework/userconfighandlers
38
39 - If the above succeeds then your userconfighandler and validator are working properly. You can view the configuration data by running the following command:
40   /opt/bin/cmcli --ip 127.0.0.1 --port <port> get-properties --matching-filter '.*'
41   
42 To test run-time validation do the following:
43 - Copy your validator to a target system under /opt/cmframework/validators directory.
44
45 - Start a private cmserver
46   ./start-private-cmserver.sh <some port number> no-cm-data
47   
48 - The above command will print information about the temporary directory used for the private cmserver, it will also print the command to be used to access the cmserver e.g. the output can look like:
49
50 =======================================================================================================================
51 Use CLI /opt/bin/cmcli --ip 127.0.0.1 --port 51110
52 Root DIR /tmp/tmp.HcyehIQQoQ
53 =======================================================================================================================
54
55 - Test your plugins by changing the configuration by running the following command:
56   /opt/bin/cmcli --ip 127.0.0.1 --port <port> set-property --property NAME VALUE
57
58 - If the above succeeds then your validator is working properly. You can view the configuration data by running the following command:
59   /opt/bin/cmcli --ip 127.0.0.1 --port <port> get-properties --matching-filter '.*'
60 '
61
62 CM_PORT=
63 CM_IP=127.0.0.1
64 NO_CM_DATA=0
65 ROOT_DIR=$(mktemp -d)
66 CM_VALIDATORS=$ROOT_DIR/opt/cmframework/validators
67 CM_ACTIVATORS=$ROOT_DIR/opt/cmframework/activators
68 CM_LOG=$ROOT_DIR/var/log/cm.log
69 USER_CONFIG_HANDLERS=$ROOT_DIR/opt/cmframework/userconfighandlers
70 INVENTORY_HANDLERS=$ROOT_DIR/opt/cmframework/inventoryhandlers
71 INVENTORY_DATA=$ROOT_DIR/inventory.data
72
73 LOG_FILE=$ROOT_DIR/var/log/cm.log
74
75 DB_DIR=$ROOT_DIR/db
76 DB_FILE=$DB_DIR/db
77
78 CMCLI="/usr/local/bin/cmcli --ip $CM_IP --port $CM_PORT"
79 ENV_FILE=$ROOT_DIR/
80
81 export CONFIG_PHASE="bootstrapping"
82
83 CM_PID=
84
85
86 function log()
87 {
88     priority=$1
89
90     echo "$(date) ($priority) ${FUNCNAME[2]} ${@:2}"
91     echo "$(date) ($priority) ${FUNCNAME[2]} ${@:2}" >> $LOG_FILE
92 }
93
94 function log_info()
95 {
96     log info $@
97 }
98
99 function log_error()
100 {
101     log error $@
102 }
103
104
105 function run_cmd()
106 {
107     log_info "Running $@"
108     result=$(eval $@ 2>&1)
109     ret=$?
110     if [ $ret -ne 0 ]; then
111         log_error "Failed with error $result"
112     else
113         log_info "Command succeeded: $result"
114     fi
115
116     return $ret
117 }
118
119 function prepare_environment()
120 {
121     mkdir -p $(dirname $CM_LOG)
122     log_info "Creating directories under $ROOT_DIR"
123     run_cmd "mkdir -p $USER_CONFIG_HANDLERS"
124     run_cmd "mkdir -p $INVENTORY_HANDLERS"
125     run_cmd "mkdir -p $CM_VALIDATORS"
126     run_cmd "mkdir -p $CM_ACTIVATORS"
127     run_cmd "mkdir -p $DB_DIR"
128
129     log_info "Copying inventory handlers and user config handlers to $ROOT_DIR"
130     cp -f /opt/cmframework/userconfighandlers/* $USER_CONFIG_HANDLERS/
131     cp -f /opt/cmframework/inventoryhandlers/* $INVENTORY_HANDLERS/
132     cp -f /opt/cmframework/validators/* $CM_VALIDATORS/
133
134     if [ $NO_CM_DATA -eq 0 ]; then
135         log_info "Generating DB file under $DB_FILE"
136         /usr/local/bin/cmcli get-properties --matching-filter '.*' > $DB_FILE
137     fi
138     return $?
139 }
140
141 function stop_process()
142 {
143     pid=$1
144     log_info "Stopping process $pid"
145     if ! [ -z $pid ]; then
146         if [ -d /proc/$pid ]; then
147             log_info "Shutting down process $pid gracefully"
148             run_cmd "pkill -TERM -g $pid"
149             log_info "Waiting for process $pid to exit"
150             for ((i=0; i<10; i++)); do
151                 if ! [ -d /proc/$pid ]; then
152                     log_info "Process $pid exited"
153                     break
154                 fi
155                 log_info "Process $pid is still running"
156                 sleep 2
157             done
158
159             if [ -d /proc/$pid ]; then
160                 log_error "Process $pid is still running, forcefully shutting it down"
161                 run_cmd "pkill -KILL -g $pid"
162             fi
163         fi
164     fi
165 }
166
167 function cleanup()
168 {
169     log_info "Cleaning up"
170     stop_process $CM_PID
171     rm -rf $ROOT_DIR
172 }
173
174 function start_cm()
175 {
176     log_info "Starting CM server $CM_IP $CM_PORT $CM_LOG"
177     extra_args=""
178     if [ $NO_CM_DATA -eq 1 ]; then
179         extra_args="--install-phase"
180     fi
181     setsid /usr/local/bin/cmserver --ip $CM_IP --port $CM_PORT --backend-api cmframework.filebackend.cmfilebackend.CMFileBackend --backend-uri $DB_FILE --activationstate-handler-api cmframework.utils.cmstatedummyhandler.CMStateDummyHandler --activationstate-handler-uri dummy_uri --alarmhandler-api cmframework.lib.cmalarmhandler_dummy.AlarmHandler_Dummy --snapshot-handler-api cmframework.utils.cmstatedummyhandler.CMStateDummyHandler --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 $extra_args 2> $CM_LOG 1>&2 &
182     export CM_PID=$!
183     log_info "cmserver pid is $CM_PID"
184     if ! [ -d /proc/$CM_PID ]; then
185         log_error "CM server is not running!"
186         log_info "Check $CM_LOG for details"
187         return 1
188     fi
189     return 0
190 }
191
192 function main()
193 {
194     # prapare private environment
195     prepare_environment
196     if [ $? -ne 0 ]; then
197         cleanup
198         return 1
199     fi
200
201     # start the configuration management server
202     start_cm
203     if [ $? -ne 0 ]; then
204         cleanup
205         return 1
206     fi
207
208     jobs -p
209
210     echo ""
211     echo ""
212     echo "======================================================================================================================="
213     echo "Use CLI $CMCLI"
214     echo "Root DIR $ROOT_DIR"
215     echo "======================================================================================================================="
216     echo ""
217     echo ""
218     while [ 1 ]; do
219         read -n1 -r -p "Press 'q' space to exit..." key
220
221         if [ "$key" = 'q' ]; then
222             echo "exiting..."
223             break
224         fi
225     done
226
227     cleanup
228     return 0
229 }
230
231 if [ $# -lt 1 ]; then
232     echo "Usage:$0 <cmserver port> [no-cm-data]"
233     exit 1
234 fi
235
236 CM_PORT=$1
237 shift 1
238
239 for arg in "$@"; do
240     if [ "$arg" == "no-cm-data" ]; then
241         NO_CM_DATA=1
242     else
243         CONFIG_PHASE=$arg
244     fi
245 done
246
247 CMCLI="/usr/local/bin/cmcli --ip $CM_IP --port $CM_PORT"
248
249 main