1+ #! /bin/sh
2+ # ## BEGIN INIT INFO
3+ # Provides: oauth2-proxy
4+ # Required-Start: $remote_fs $syslog
5+ # Required-Stop: $remote_fs $syslog
6+ # Default-Start: 2 3 4 5
7+ # Default-Stop: 0 1 6
8+ # Short-Description: Start oauth2-proxy at boot time.
9+ # Description: A reverse proxy that provides authentication with Google, Github or other provider.
10+ # ## END INIT INFO
11+
12+ # Author: Yves H. <[email protected] >13+
14+ # Do NOT "set -e"
15+
16+ # PATH should only include /usr/* if it runs after the mountnfs.sh script
17+ PATH=/sbin:/usr/sbin:/bin:/usr/bin
18+ DESC=" A reverse proxy that provides authentication with Google, Github or other provider"
19+ NAME=` basename $0 `
20+ DAEMON={{ oauth2_dir }}/current/oauth2_proxy
21+ DAEMON_ARGS=" -config={{ oauth2_config_path }} {{ oauth2_config_cmdline_args }}"
22+ PIDFILE=/var/run/$NAME .pid
23+ SCRIPTNAME=/etc/init.d/oauth2-proxy
24+ USER={{ oauth2_user }}
25+ GROUP=$USER
26+
27+ STDOUT_LOG=" /var/log/oauth2-proxy/$NAME .log"
28+ STDERR_LOG=" /var/log/oauth2-proxy/$NAME .error.log"
29+
30+ # Exit if the package is not installed
31+ [ -x " $DAEMON " ] || exit 0
32+
33+ # Read configuration variable file if it is present
34+ [ -r /etc/default/$NAME ] && . /etc/default/$NAME
35+
36+ # Load the VERBOSE setting and other rcS variables
37+ . /lib/init/vars.sh
38+
39+ # Define LSB log_* functions.
40+ # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
41+ # and status_of_proc is working.
42+ . /lib/lsb/init-functions
43+
44+ VERBOSE=true
45+
46+ get_pid () {
47+ cat " $PIDFILE "
48+ }
49+
50+ is_running () {
51+ [ -f " $PIDFILE " ] && ps ` get_pid` > /dev/null 2>&1
52+ }
53+
54+ #
55+ # Function that starts the daemon/service
56+ #
57+ do_start ()
58+ {
59+ # Return
60+ # 0 if daemon has been started
61+ # 1 if daemon was already running
62+ # 2 if daemon could not be started
63+ start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
64+ || return 1
65+
66+ start-stop-daemon --make-pidfile --pidfile $PIDFILE --chuid $USER :$GROUP \
67+ --background --no-close --exec $DAEMON --start -- $DAEMON_ARGS \
68+ >> $STDOUT_LOG 2>> $STDERR_LOG
69+
70+ sleep 2
71+ if ! is_running; then
72+ return 2
73+ fi
74+ }
75+
76+ #
77+ # Function that stops the daemon/service
78+ #
79+ do_stop ()
80+ {
81+ # Return
82+ # 0 if daemon has been stopped
83+ # 1 if daemon was already stopped
84+ # 2 if daemon could not be stopped
85+ # other if a failure occurred
86+ start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
87+ RETVAL=" $? "
88+ [ " $RETVAL " = 2 ] && return 2
89+ # Wait for children to finish too if this is a daemon that forks
90+ # and if the daemon is only ever run from this initscript.
91+ # If the above conditions are not satisfied then add some other code
92+ # that waits for the process to drop all resources that could be
93+ # needed by services started subsequently. A last resort is to
94+ # sleep for some time.
95+ start-stop-daemon --stop --quiet --oknodo --retry=0/1/KILL/5 --exec $DAEMON
96+ [ " $? " = 2 ] && return 2
97+ # Many daemons don't delete their pidfiles when they exit.
98+ rm -f $PIDFILE
99+ return " $RETVAL "
100+ }
101+
102+ #
103+ # Function that sends a SIGHUP to the daemon/service
104+ #
105+ do_reload () {
106+ #
107+ # If the daemon can reload its configuration without
108+ # restarting (for example, when it is sent a SIGHUP),
109+ # then implement that here.
110+ #
111+ start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
112+ return 0
113+ }
114+
115+ case " $1 " in
116+ start)
117+ [ " $VERBOSE " != no ] && log_daemon_msg " Starting $DESC " " $NAME "
118+ do_start
119+ case " $? " in
120+ 0|1) [ " $VERBOSE " != no ] && log_end_msg 0 ;;
121+ 2) [ " $VERBOSE " != no ] && log_end_msg 1 ;;
122+ esac
123+ ;;
124+ stop)
125+ [ " $VERBOSE " != no ] && log_daemon_msg " Stopping $DESC " " $NAME "
126+ do_stop
127+ case " $? " in
128+ 0|1) [ " $VERBOSE " != no ] && log_end_msg 0 ;;
129+ 2) [ " $VERBOSE " != no ] && log_end_msg 1 ;;
130+ esac
131+ ;;
132+ status)
133+ status_of_proc " $DAEMON " " $NAME " && exit 0 || exit $?
134+ ;;
135+ restart|force-reload)
136+ #
137+ # If the "reload" option is implemented then remove the
138+ # 'force-reload' alias
139+ #
140+ log_daemon_msg " Restarting $DESC " " $NAME "
141+ do_stop
142+ case " $? " in
143+ 0|1)
144+ do_start
145+ case " $? " in
146+ 0) log_end_msg 0 ;;
147+ 1) log_end_msg 1 ;; # Old process is still running
148+ * ) log_end_msg 1 ;; # Failed to start
149+ esac
150+ ;;
151+ * )
152+ # Failed to stop
153+ log_end_msg 1
154+ ;;
155+ esac
156+ ;;
157+ * )
158+ echo " Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
159+ exit 3
160+ ;;
161+ esac
162+
163+ :
0 commit comments