Stronger model typings
[oweals/peertube.git] / support / init.d / peertube
1 #!/bin/sh
2
3 APP_NAME="peertube"
4 USER="peertube"
5 GROUP="peertube"
6 NODE_ENV="production"
7 APP_DIR="/var/www/peertube/peertube-latest"
8 NODE_APP="dist/server"
9 KWARGS=""
10 CONFIG_DIR="/var/www/peertube/config"
11 PID_DIR="$APP_DIR/pid"
12 PID_FILE="$PID_DIR/$APP_NAME.pid"
13 LOG_DIR="/var/log/peertube"
14 LOG_FILE="$LOG_DIR/$APP_NAME.log"
15 NODE_EXEC=$(which node)
16
17 ###############
18
19 # REDHAT chkconfig header
20
21 # chkconfig: - 58 74
22 # description: peertube service script.
23 ### BEGIN INIT INFO
24 # Provides: peertube
25 # Required-Start:    $network $remote_fs $local_fs
26 # Required-Stop:     $network $remote_fs $local_fs
27 # Default-Start:     2 3 4 5
28 # Default-Stop:      0 1 6
29 # Short-Description: start and stop peertube
30 # Description: Node process for peertube
31 ### END INIT INFO
32
33 ###############
34
35 USAGE="Usage: $0 {start|stop|restart|status} [--force]"
36 FORCE_OP=false
37
38 pid_file_exists() {
39     [ -f "$PID_FILE" ]
40 }
41
42 get_pid() {
43     echo "$(cat "$PID_FILE")"
44 }
45
46 is_running() {
47     PID="$(get_pid)"
48     [ -d /proc/$PID ]
49 }
50
51 start_it() {
52     mkdir -p "$PID_DIR"
53     chown $USER:$GROUP "$PID_DIR"
54     mkdir -p "$LOG_DIR"
55     chown $USER:$GROUP "$LOG_DIR"
56
57     echo "Starting $APP_NAME ..."
58     echo "cd $APP_DIR && NODE_ENV=$NODE_ENV NODE_CONFIG_DIR=$CONFIG_DIR $NODE_EXEC $APP_DIR/$NODE_APP $KWARGS 1>$LOG_FILE 2>&1 & echo \$! > $PID_FILE" | sudo -i -u $USER
59     echo "$APP_NAME started with pid $(get_pid)"
60 }
61
62 stop_process() {
63     PID=$(get_pid)
64     echo "Killing process $PID"
65     pkill -P $PID
66 }
67
68 remove_pid_file() {
69     echo "Removing pid file"
70     rm -f "$PID_FILE"
71 }
72
73 start_app() {
74     if pid_file_exists
75     then
76         if is_running
77         then
78             PID=$(get_pid)
79             echo "$APP_NAME already running with pid $PID"
80             exit 1
81         else
82             echo "$APP_NAME stopped, but pid file exists"
83             if [ $FORCE_OP = true ]
84             then
85                 echo "Forcing start anyways"
86                 remove_pid_file
87                 start_it
88             fi
89         fi
90     else
91         start_it
92     fi
93 }
94
95 stop_app() {
96     if pid_file_exists
97     then
98         if is_running
99         then
100             echo "Stopping $APP_NAME ..."
101             stop_process
102             remove_pid_file
103             echo "$APP_NAME stopped"
104         else
105             echo "$APP_NAME already stopped, but pid file exists"
106             if [ $FORCE_OP = true ]
107             then
108                 echo "Forcing stop anyways ..."
109                 remove_pid_file
110                 echo "$APP_NAME stopped"
111             else
112                 exit 1
113             fi
114         fi
115     else
116         echo "$APP_NAME already stopped, pid file does not exist"
117         exit 1
118     fi
119 }
120
121 status_app() {
122     if pid_file_exists
123     then
124         if is_running
125         then
126             PID=$(get_pid)
127             echo "$APP_NAME running with pid $PID"
128         else
129             echo "$APP_NAME stopped, but pid file exists"
130         fi
131     else
132         echo "$APP_NAME stopped"
133     fi
134 }
135
136 case "$2" in
137     --force)
138         FORCE_OP=true
139     ;;
140
141     "")
142     ;;
143
144     *)
145         echo $USAGE
146         exit 1
147     ;;
148 esac
149
150 case "$1" in
151     start)
152         start_app
153     ;;
154
155     stop)
156         stop_app
157     ;;
158
159     restart)
160         stop_app
161         start_app
162     ;;
163
164     status)
165         status_app
166     ;;
167
168     *)
169         echo $USAGE
170         exit 1
171     ;;
172 esac
173