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