0b061a46ec4c0dcbfdd7f82754024839e2924905
[policy/engine.git] / packages / base / src / files / bin / policy.sh
1 ###
2 # ============LICENSE_START=======================================================
3 # ECOMP Policy Engine
4 # ================================================================================
5 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10
11 #      http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19 ###
20
21 #!/bin/bash
22
23 function usage() {
24         echo -n "syntax: $(basename $0) "
25         echo -n "[--debug] "
26         echo    "status|start|stop"
27 }
28
29 function check_r_file() {       
30         if [[ $DEBUG == y ]]; then
31                 echo "-- ${FUNCNAME[0]} --"
32                 set -x
33         fi
34
35         FILE=$1
36         if [[ ! -f ${FILE} || ! -r ${FILE} ]]; then
37         return 1
38         fi
39
40         return 0
41 }
42
43 function check_x_file() {       
44         if [[ $DEBUG == y ]]; then
45                 echo "-- ${FUNCNAME[0]} --"
46                 set -x
47         fi
48
49         FILE=$1
50         if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then
51         return 1
52         fi
53
54         return 0
55 }
56
57 function component_status() {
58         COMPONENT=$1
59         output=$("${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status)
60         if [[ $? == 0 ]]; then 
61                 echo "  ${COMPONENT}: UP: ${output}"
62         else
63                 echo "  ${COMPONENT}: DOWN"
64         fi
65 }
66
67 function component_start() {
68         COMPONENT=$1
69         "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status > /dev/null 2>&1
70         if [[ $? == 0 ]]; then 
71                 echo "  ${COMPONENT}: UP: already running .."
72         else
73                 "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" start > /dev/null 2>&1
74                 echo "  ${COMPONENT}: STARTING .."
75         fi
76 }
77
78 function component_stop() {
79         COMPONENT=$1
80         "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status > /dev/null 2>&1
81         if [[ $? != 0 ]]; then 
82                 echo "  ${COMPONENT}: DOWN: already stopped .."
83         else
84                 "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" stop > /dev/null 2>&1
85                 echo "  ${COMPONENT}: STOPPING .."
86         fi
87 }
88
89 function policy_status() {
90         if [[ $DEBUG == y ]]; then
91                 echo "-- ${FUNCNAME[0]} --"
92                 set -x
93         fi
94         
95         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
96                 component_status pap
97         fi
98         
99         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
100                 component_status pdp
101         fi
102
103         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
104                 component_status configs
105         fi
106         
107         if check_x_file "${POLICY_HOME}/etc/init.d/pypdp"; then
108                 component_status pypdp
109         fi
110         
111         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
112                 component_status console
113         fi
114         
115         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
116                 component_status brmsgw
117         fi
118         
119         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
120                 component_status paplp
121         fi
122         
123         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
124                 component_status pdplp
125         fi
126
127         NUM_CRONS=$(crontab -l 2> /dev/null | wc -l)
128         echo "  ${NUM_CRONS} cron jobs installed."
129         
130 }
131
132 function policy_start() {
133         if [[ $DEBUG == y ]]; then
134                 echo "-- ${FUNCNAME[0]} --"
135                 set -x
136         fi
137         
138         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
139                 component_start pap
140         fi
141         
142         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
143                 component_start pdp
144         fi
145
146         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
147                 component_start configs
148         fi
149         
150         if check_x_file "${POLICY_HOME}/etc/init.d/pypdp"; then
151                 component_start pypdp
152         fi
153         
154         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
155                 component_start console
156         fi
157         
158         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
159                 component_start brmsgw
160         fi
161         
162         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
163                 component_start paplp
164         fi
165         
166         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
167                 component_start pdplp
168         fi
169         
170         cat "${POLICY_HOME}"/etc/cron.d/*.cron | crontab
171 }
172
173 function policy_stop() {
174         if [[ $DEBUG == y ]]; then
175                 echo "-- ${FUNCNAME[0]} --"
176                 set -x
177         fi
178         
179         pkill -f "/bin/bash ${POLICY_HOME}/bin/monitor.sh"
180         crontab -r > /dev/null 2>&1
181         sleep 2
182
183         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
184                 component_stop paplp
185         fi
186         
187         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
188                 component_stop pdplp
189         fi
190         
191         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
192                 component_stop brmsgw
193         fi
194         
195         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
196                 component_stop console
197         fi
198                 
199         if check_x_file "${POLICY_HOME}/etc/init.d/pypdp"; then
200                 component_stop pypdp
201         fi
202         
203         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
204                 component_stop configs
205         fi
206         
207         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
208                 component_stop pdp
209         fi
210         
211         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
212                 component_stop pap
213         fi
214         
215 }
216
217 #########################################################################
218 ##
219 ## script execution body
220 ##
221 #########################################################################
222
223 DEBUG=n
224 OPERATION=none
225
226 until [[ -z "$1" ]]; do
227         case $1 in
228                 -d|--debug|debug)       DEBUG=y
229                                                 set -x
230                                                 ;;
231                 -i|--status|status)             OPERATION=status
232                                                                         ;;
233                 -s|--start|start)                       OPERATION=start
234                                                                         ;;
235                 -h|--stop|stop|--halt|halt)     OPERATION=halt
236                                                                         ;;
237                 *)                                                      usage
238                                                                         exit 1
239                                                                         ;;
240         esac
241         shift
242 done
243
244 # operation validation
245 case $OPERATION in
246         status) ;;
247         start)  ;;
248         halt)   ;;
249         *)              echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}";
250                         usage
251                         exit 1
252                         ;;
253 esac
254
255 if [[ -z ${POLICY_HOME} ]]; then
256         echo "error: POLICY_HOME is unset."
257         exit 1
258 fi
259
260 # operation validation
261 case $OPERATION in
262         status) 
263                 policy_status
264                 ;;
265         start)  
266                 policy_start
267                 ;;
268         halt)   
269                 policy_stop
270                 ;;
271         *)              echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}";
272                         usage
273                         exit 1
274                         ;;
275 esac