Send both ubuntu versions to Robot VM
[demo.git] / boot / dcae2_serv.sh
1 #!/bin/sh
2 #############################################################################
3 #
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
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 #        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 #############################################################################
18
19 ### BEGIN INIT INFO
20 # Provides: dcaegen2-bootstrap
21 # Required-Start: $remote_fs $syslog
22 # Required-Stop: $remote_fs $syslog
23 # Default-Start: 2 3 4 5
24 # Default-Stop: 0 1 6
25 # Short-Description: Start daemon at boot time
26 # Description: Enable service provided by daemon.
27 ### END INIT INFO
28
29 dir="/opt"
30 cmd="./dcae2_vm_init.sh"
31 user="root"
32
33 name=$(basename "$0")
34 pid_file="/var/run/$name.pid"
35 stdout_log="/var/log/$name.log"
36 stderr_log="/var/log/$name.err"
37
38 get_pid() {
39     cat "$pid_file"
40 }
41
42 is_running() {
43     CID="$(docker ps | grep 'org.onap.dcaegen2.deployments.bootstrap')"
44     [ ! -z "$CID" ]
45 }
46
47 case "$1" in
48     start)
49     if is_running; then
50         echo "Already started"
51     else
52         echo "Starting $name"
53         cd "$dir"
54         if [ -z "$user" ]; then
55             sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
56         else
57             sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
58         fi
59         echo $! > "$pid_file"
60         if ! is_running; then
61             echo "Unable to start, see $stdout_log and $stderr_log"
62             exit 1
63         fi
64     fi
65     ;;
66     stop)
67     if is_running; then
68         echo -n "Stopping $name.."
69         kill "$(get_pid)"
70         CID=$(docker ps | grep 'nginx' | awk '{ print $1 }')
71         sudo docker stop "$CID"
72
73         CID=$(docker ps | grep 'org.onap.dcaegen2.deployments.bootstrap' | awk '{ print $1 }')
74         docker exec -it "$CID" ./teardown.sh
75         for i in {1..10}
76         do
77             if ! is_running; then
78                 break
79             fi
80
81             echo -n "."
82             sleep 1
83         done
84         echo
85
86         if is_running; then
87             echo "Not stopped; may still be shutting down or shutdown may have failed"
88             exit 1
89         else
90             echo "Stopped"
91             if [ -f "$pid_file" ]; then
92                 rm "$pid_file"
93             fi
94         fi
95     else
96         echo "Not running"
97     fi
98     ;;
99     restart)
100     $0 stop
101     if is_running; then
102         echo "Unable to stop, will not attempt to start"
103         exit 1
104     fi
105     $0 start
106     ;;
107     status)
108     if is_running; then
109         echo "Running"
110     else
111         echo "Stopped"
112         exit 1
113     fi
114     ;;
115     *)
116     echo "Usage: $0 {start|stop|restart|status}"
117     exit 1
118     ;;
119 esac
120
121 exit 0