Commit 29840f404cbb15f78ea1a5aa86e439d72c1aaffb
1 parent
7e5efbca
Exists in
master
and in
39 other branches
Adding supervisor module
Showing
7 changed files
with
465 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,24 @@ |
1 | +Puppet Supervisor | |
2 | +================= | |
3 | + | |
4 | +Install and manage apps in supervisord | |
5 | + | |
6 | + | |
7 | +Usage | |
8 | +================= | |
9 | + | |
10 | +Include supervisor class | |
11 | +```puppet | |
12 | +include supervisor | |
13 | +``` | |
14 | + | |
15 | +Install your app using defined type supervisor::app | |
16 | + | |
17 | +```puppet | |
18 | +supervisor::app { 'your-app-title': | |
19 | + app_name => 'your-app-name' # Default to 'your-app-title' | |
20 | + command => 'The command that will be run this app', # required | |
21 | + directory => 'Path where your command will be run' # required | |
22 | + user => 'User to execute this app' # Default to ubuntu | |
23 | +} | |
24 | +``` | |
0 | 25 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,217 @@ |
1 | +#! /bin/sh | |
2 | +# | |
3 | +# supervisor built from skeleton /etc/init.d/ script. | |
4 | +# | |
5 | +# skeleton by Miquel van Smoorenburg <miquels@cistron.nl>. | |
6 | +# Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>. | |
7 | +# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>. | |
8 | +# More changes by Konstantin Martini <isnok@tuxcode.org>. | |
9 | +# | |
10 | +# Version: @(#)supervisor 0.8 25-Jun-2013 isnok@tuxcode.org | |
11 | +# | |
12 | +### BEGIN INIT INFO | |
13 | +# Provides: supervisor | |
14 | +# Required-Start: $remote_fs $network $named | |
15 | +# Required-Stop: $remote_fs $network $named | |
16 | +# Default-Start: 2 3 4 5 | |
17 | +# Default-Stop: 0 1 6 | |
18 | +# Short-Description: Start/stop supervisord | |
19 | +# Description: Start/stop supervisor daemon and its configured | |
20 | +# subprocesses. | |
21 | +### END INIT INFO | |
22 | + | |
23 | + | |
24 | +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
25 | +#DAEMON=/usr/bin/supervisord # Debian package version (presently 3.0a8-1) | |
26 | +DAEMON=/usr/local/bin/supervisord # Python package index (pypi) version (3.0b1) | |
27 | +NAME=supervisord | |
28 | +DESC="supervisord" | |
29 | + | |
30 | +CONFIGFILE=/etc/supervisord.conf # the supervisord config file | |
31 | +SUPERVISORCTL=/usr/local/bin/supervisorctl # used for reload command(s) | |
32 | +#SUPERVISORCTL=/usr/bin/supervisorctl # again: official deb version | |
33 | + | |
34 | +if [ ! -x $DAEMON ]; then | |
35 | + echo "ERROR: Not executable: $DAEMON" | |
36 | + exit 0 | |
37 | +fi | |
38 | + | |
39 | +LOGDIR=/var/log/supervisor | |
40 | +PIDFILE=/var/run/$NAME.pid | |
41 | +DODTIME=5 # Time to wait for the server to die, in seconds | |
42 | + # If this value is set too low you might not | |
43 | + # let some servers to die gracefully and | |
44 | + # 'restart' will not work. | |
45 | + | |
46 | +# fix some args for certain commands | |
47 | +DAEMON_ARGS="-c$CONFIGFILE" | |
48 | +CTL_ARGS="-c$CONFIGFILE" | |
49 | +STARTSTOP_ARGS="--quiet --pidfile $PIDFILE --exec $DAEMON" | |
50 | + | |
51 | +# Include supervisor defaults if available | |
52 | +if [ -r /etc/default/supervisor ]; then | |
53 | + . /etc/default/supervisor | |
54 | +fi | |
55 | + | |
56 | +set -e | |
57 | + | |
58 | +dod_sleep () { | |
59 | + if [ -n "$DODTIME" ]; then | |
60 | + sleep "$DODTIME"s | |
61 | + fi | |
62 | +} | |
63 | + | |
64 | +running_pid () { | |
65 | + # Check if a pid's cmdline contains a string (name). | |
66 | + # This should work for all users. | |
67 | + pid="$1" | |
68 | + name="$2" | |
69 | + if [ -z "$pid" ]; then | |
70 | + return 1 # no pid given | |
71 | + fi | |
72 | + if [ ! -d /proc/"$pid" ]; then | |
73 | + return 1 # no /proc/$pid directory | |
74 | + fi | |
75 | + if cat /proc/"$pid"/cmdline | tr "\000" "\n"| grep -q "$name"; then | |
76 | + return 0 | |
77 | + else | |
78 | + return 1 # $pid does not match $name | |
79 | + fi | |
80 | +} | |
81 | + | |
82 | +running () { | |
83 | + # Check if DAEMON is running by examining $PIDFILE. | |
84 | + # If this succeeds, it sets $pid (a side effect being used). | |
85 | + | |
86 | + if [ ! -f "$PIDFILE" ]; then | |
87 | + return 1 # No pidfile, probably no daemon present. | |
88 | + fi | |
89 | + # Now, obtain the pid and check it's /proc cmdline: | |
90 | + pid="$(cat $PIDFILE)" | |
91 | + if running_pid "$pid" "$DAEMON"; then | |
92 | + return 0 | |
93 | + else | |
94 | + return 1 | |
95 | + fi | |
96 | +} | |
97 | + | |
98 | +normal_start () { | |
99 | + start-stop-daemon $STARTSTOP_ARGS --start \ | |
100 | + -- "$DAEMON_ARGS" $DAEMON_OPTS | |
101 | + if [ ! -f "$PIDFILE" ]; then | |
102 | + sleep 1 # grace time to create PIDFILE | |
103 | + fi | |
104 | +} | |
105 | + | |
106 | +normal_stop () { | |
107 | + #start-stop-daemon $STARTSTOP_ARGS --stop --oknodo | |
108 | + "$SUPERVISORCTL" $CTL_ARGS shutdown | |
109 | +} | |
110 | + | |
111 | +force_stop () { | |
112 | + # Forcefully stop a running DAEMON. | |
113 | + if running; then | |
114 | + kill -15 "$pid" | |
115 | + dod_sleep | |
116 | + # Check again, try harder if needed. | |
117 | + if running; then | |
118 | + kill -9 "$pid" | |
119 | + dod_sleep | |
120 | + if running; then | |
121 | + echo "Unable to kill running $NAME process (pid=$pid)!" | |
122 | + exit 1 | |
123 | + fi | |
124 | + fi | |
125 | + fi | |
126 | + rm -f "$PIDFILE" | |
127 | + return 0 | |
128 | +} | |
129 | + | |
130 | +ctl_reload () { | |
131 | + # make supervisord reload it's config | |
132 | + if [ -x "$SUPERVISORCTL" ]; then | |
133 | + "$SUPERVISORCTL" $CTL_ARGS reload | |
134 | + else | |
135 | + return 1 | |
136 | + fi | |
137 | +} | |
138 | + | |
139 | +ctl_status () { | |
140 | + # show stati of supervised processes. | |
141 | + # do not mind if this fails. | |
142 | + "$SUPERVISORCTL" $CTL_ARGS status | |
143 | +} | |
144 | + | |
145 | +case "$1" in | |
146 | + start) | |
147 | + echo -n "Starting $DESC: " | |
148 | + normal_start | |
149 | + if running; then | |
150 | + echo "$NAME." | |
151 | + else | |
152 | + echo "ERROR." | |
153 | + fi | |
154 | + ;; | |
155 | + stop) | |
156 | + echo -n "Stopping $DESC: " | |
157 | + if normal_stop; then | |
158 | + echo "$NAME." | |
159 | + else | |
160 | + echo "ERROR." | |
161 | + fi | |
162 | + ;; | |
163 | + restart) | |
164 | + echo "Restarting $DESC..." | |
165 | + "$0" stop && "$0" start | |
166 | + ;; | |
167 | + force-stop) | |
168 | + echo -n "Forcefully stopping $DESC: " | |
169 | + force_stop | |
170 | + if running; then | |
171 | + echo "$NAME." | |
172 | + else | |
173 | + echo "ERROR." | |
174 | + fi | |
175 | + ;; | |
176 | + reload|force-reload) | |
177 | + # | |
178 | + # If the daemon can reload its config files on the fly | |
179 | + # for example by sending it SIGHUP, do it here. | |
180 | + # | |
181 | + # If the daemon responds to changes in its config file | |
182 | + # directly anyway, make this a do-nothing entry. | |
183 | + # | |
184 | + echo "Reloading $DESC: " | |
185 | + if ctl_reload; then | |
186 | + echo "$NAME." | |
187 | + else | |
188 | + echo "ERROR." | |
189 | + fi | |
190 | + ;; | |
191 | + #force-reload) | |
192 | + # | |
193 | + # If the "reload" option is implemented, move the "force-reload" | |
194 | + # option to the "reload" entry above. If not, "force-reload" is | |
195 | + # just the same as "restart" except that it does nothing if the | |
196 | + # daemon isn't already running. | |
197 | + # Check wether $DAEMON is running. If so, restart. | |
198 | + #echo "Not implemented." | |
199 | + #;; | |
200 | + status) | |
201 | + echo -n "$NAME is " | |
202 | + if running; then | |
203 | + echo "running" | |
204 | +# ctl_status | |
205 | + else | |
206 | + echo "not running." | |
207 | + exit 1 | |
208 | + fi | |
209 | + ;; | |
210 | + *) | |
211 | + N=/etc/init.d/"$NAME" | |
212 | + echo "Usage: $N {start|stop|restart|reload|status|force-stop}" >&2 | |
213 | + exit 1 | |
214 | + ;; | |
215 | +esac | |
216 | + | |
217 | +exit 0 | ... | ... |
... | ... | @@ -0,0 +1,139 @@ |
1 | +; Sample supervisor config file. | |
2 | +; | |
3 | +; For more information on the config file, please see: | |
4 | +; http://supervisord.org/configuration.html | |
5 | +; | |
6 | +; Note: shell expansion ("~" or "$HOME") is not supported. Environment | |
7 | +; variables can be expanded using this syntax: "%(ENV_HOME)s". | |
8 | + | |
9 | +[unix_http_server] | |
10 | +file=/tmp/supervisor.sock ; (the path to the socket file) | |
11 | +;chmod=0700 ; socket file mode (default 0700) | |
12 | +;chown=nobody:nogroup ; socket file uid:gid owner | |
13 | +;username=user ; (default is no username (open server)) | |
14 | +;password=123 ; (default is no password (open server)) | |
15 | + | |
16 | +;[inet_http_server] ; inet (TCP) server disabled by default | |
17 | +;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface) | |
18 | +;username=user ; (default is no username (open server)) | |
19 | +;password=123 ; (default is no password (open server)) | |
20 | + | |
21 | +[supervisord] | |
22 | +logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log) | |
23 | +logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) | |
24 | +logfile_backups=10 ; (num of main logfile rotation backups;default 10) | |
25 | +loglevel=info ; (log level;default info; others: debug,warn,trace) | |
26 | +pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) | |
27 | +nodaemon=false ; (start in foreground if true;default false) | |
28 | +minfds=1024 ; (min. avail startup file descriptors;default 1024) | |
29 | +minprocs=200 ; (min. avail process descriptors;default 200) | |
30 | +;umask=022 ; (process file creation umask;default 022) | |
31 | +;user=chrism ; (default is current user, required if root) | |
32 | +;identifier=supervisor ; (supervisord identifier, default is 'supervisor') | |
33 | +;directory=/tmp ; (default is not to cd during start) | |
34 | +;nocleanup=true ; (don't clean up tempfiles at start;default false) | |
35 | +;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) | |
36 | +;environment=KEY="value" ; (key value pairs to add to environment) | |
37 | +;strip_ansi=false ; (strip ansi escape codes in logs; def. false) | |
38 | + | |
39 | +; the below section must remain in the config file for RPC | |
40 | +; (supervisorctl/web interface) to work, additional interfaces may be | |
41 | +; added by defining them in separate rpcinterface: sections | |
42 | +[rpcinterface:supervisor] | |
43 | +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface | |
44 | + | |
45 | +[supervisorctl] | |
46 | +serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket | |
47 | +;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket | |
48 | +;username=chris ; should be same as http_username if set | |
49 | +;password=123 ; should be same as http_password if set | |
50 | +;prompt=mysupervisor ; cmd line prompt (default "supervisor") | |
51 | +;history_file=~/.sc_history ; use readline history if available | |
52 | + | |
53 | +; The below sample program section shows all possible program subsection values, | |
54 | +; create one or more 'real' program: sections to be able to control them under | |
55 | +; supervisor. | |
56 | + | |
57 | +;[program:theprogramname] | |
58 | +;command=/bin/cat ; the program (relative uses PATH, can take args) | |
59 | +;process_name=%(program_name)s ; process_name expr (default %(program_name)s) | |
60 | +;numprocs=1 ; number of processes copies to start (def 1) | |
61 | +;directory=/tmp ; directory to cwd to before exec (def no cwd) | |
62 | +;umask=022 ; umask for process (default None) | |
63 | +;priority=999 ; the relative start priority (default 999) | |
64 | +;autostart=true ; start at supervisord start (default: true) | |
65 | +;autorestart=unexpected ; whether/when to restart (default: unexpected) | |
66 | +;startsecs=1 ; number of secs prog must stay running (def. 1) | |
67 | +;startretries=3 ; max # of serial start failures (default 3) | |
68 | +;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) | |
69 | +;stopsignal=QUIT ; signal used to kill process (default TERM) | |
70 | +;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) | |
71 | +;stopasgroup=false ; send stop signal to the UNIX process group (default false) | |
72 | +;killasgroup=false ; SIGKILL the UNIX process group (def false) | |
73 | +;user=chrism ; setuid to this UNIX account to run the program | |
74 | +;redirect_stderr=true ; redirect proc stderr to stdout (default false) | |
75 | +;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO | |
76 | +;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) | |
77 | +;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) | |
78 | +;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) | |
79 | +;stdout_events_enabled=false ; emit events on stdout writes (default false) | |
80 | +;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO | |
81 | +;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) | |
82 | +;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) | |
83 | +;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) | |
84 | +;stderr_events_enabled=false ; emit events on stderr writes (default false) | |
85 | +;environment=A="1",B="2" ; process environment additions (def no adds) | |
86 | +;serverurl=AUTO ; override serverurl computation (childutils) | |
87 | + | |
88 | +; The below sample eventlistener section shows all possible | |
89 | +; eventlistener subsection values, create one or more 'real' | |
90 | +; eventlistener: sections to be able to handle event notifications | |
91 | +; sent by supervisor. | |
92 | + | |
93 | +;[eventlistener:theeventlistenername] | |
94 | +;command=/bin/eventlistener ; the program (relative uses PATH, can take args) | |
95 | +;process_name=%(program_name)s ; process_name expr (default %(program_name)s) | |
96 | +;numprocs=1 ; number of processes copies to start (def 1) | |
97 | +;events=EVENT ; event notif. types to subscribe to (req'd) | |
98 | +;buffer_size=10 ; event buffer queue size (default 10) | |
99 | +;directory=/tmp ; directory to cwd to before exec (def no cwd) | |
100 | +;umask=022 ; umask for process (default None) | |
101 | +;priority=-1 ; the relative start priority (default -1) | |
102 | +;autostart=true ; start at supervisord start (default: true) | |
103 | +;autorestart=unexpected ; whether/when to restart (default: unexpected) | |
104 | +;startsecs=1 ; number of secs prog must stay running (def. 1) | |
105 | +;startretries=3 ; max # of serial start failures (default 3) | |
106 | +;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) | |
107 | +;stopsignal=QUIT ; signal used to kill process (default TERM) | |
108 | +;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) | |
109 | +;stopasgroup=false ; send stop signal to the UNIX process group (default false) | |
110 | +;killasgroup=false ; SIGKILL the UNIX process group (def false) | |
111 | +;user=chrism ; setuid to this UNIX account to run the program | |
112 | +;redirect_stderr=true ; redirect proc stderr to stdout (default false) | |
113 | +;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO | |
114 | +;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) | |
115 | +;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) | |
116 | +;stdout_events_enabled=false ; emit events on stdout writes (default false) | |
117 | +;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO | |
118 | +;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) | |
119 | +;stderr_logfile_backups ; # of stderr logfile backups (default 10) | |
120 | +;stderr_events_enabled=false ; emit events on stderr writes (default false) | |
121 | +;environment=A="1",B="2" ; process environment additions | |
122 | +;serverurl=AUTO ; override serverurl computation (childutils) | |
123 | + | |
124 | +; The below sample group section shows all possible group values, | |
125 | +; create one or more 'real' group: sections to create "heterogeneous" | |
126 | +; process groups. | |
127 | + | |
128 | +;[group:thegroupname] | |
129 | +;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions | |
130 | +;priority=999 ; the relative start priority (default 999) | |
131 | + | |
132 | +; The [include] section can just contain the "files" setting. This | |
133 | +; setting can list multiple files (separated by whitespace or | |
134 | +; newlines). It can also contain wildcards. The filenames are | |
135 | +; interpreted as relative to this file. Included files *cannot* | |
136 | +; include files themselves. | |
137 | + | |
138 | +[include] | |
139 | +files = /etc/supervisor/conf.d/*.conf | ... | ... |
... | ... | @@ -0,0 +1,31 @@ |
1 | +define supervisor::app ( | |
2 | + $app_name = $title, | |
3 | + $command, | |
4 | + $directory, | |
5 | + $user = 'ubuntu', | |
6 | +) { | |
7 | + | |
8 | + $conf_file = "supervisor_${app_name}" | |
9 | + $service_name = $conf_file | |
10 | + | |
11 | + file { $conf_file: | |
12 | + path => "/etc/supervisor/conf.d/${app_name}.conf", | |
13 | + ensure => present, | |
14 | + content => template('supervisor/supervisor.conf.erb'), | |
15 | + require => Package['supervisor'], | |
16 | + notify => Service['supervisord'], | |
17 | + } | |
18 | + | |
19 | + service { $service_name: | |
20 | + ensure => running, | |
21 | + path => ['/usr/bin'], | |
22 | + start => "supervisorctl start $app_name", | |
23 | + restart => "supervisorctl restart $app_name", | |
24 | + stop => "supervisorctl stop $app_name", | |
25 | + status => "supervisorctl status | awk '/^${name}[: ]/{print \$2}' | grep '^RUNNING$'", | |
26 | + subscribe => File[$conf_file], | |
27 | + hasrestart => false, | |
28 | + hasstatus => false, | |
29 | + provider => base | |
30 | + } | |
31 | +} | ... | ... |
... | ... | @@ -0,0 +1,40 @@ |
1 | +class supervisor { | |
2 | + | |
3 | + package { "supervisor": | |
4 | + ensure => installed, | |
5 | + provider => pip, | |
6 | + } | |
7 | + | |
8 | + service { "supervisord": | |
9 | + ensure => running, | |
10 | + enable => true, | |
11 | + require => [Package['supervisor'], | |
12 | + File['/etc/init.d/supervisord']], | |
13 | + stop => '/etc/init.d/supervisord stop', | |
14 | + start => '/etc/init.d/supervisord start', | |
15 | + restart => '/etc/init.d/supervisord restart', | |
16 | + subscribe => File['/etc/supervisord.conf'], | |
17 | + } | |
18 | + | |
19 | + file { '/etc/init.d/supervisord': | |
20 | + source => 'puppet:///modules/supervisor/debian-isnok', | |
21 | + mode => '0755', | |
22 | + } | |
23 | + | |
24 | + file { '/etc/supervisor': | |
25 | + ensure => directory, | |
26 | + } | |
27 | + | |
28 | + file { '/etc/supervisord.conf': | |
29 | + source => 'puppet:///modules/supervisor/supervisord.conf', | |
30 | + require => File['/etc/supervisor'], | |
31 | + } | |
32 | + | |
33 | + file { '/etc/supervisor/conf.d/': | |
34 | + ensure => directory, | |
35 | + recurse => true, | |
36 | + purge => true, | |
37 | + notify => Service['supervisord'], | |
38 | + require => File['/etc/supervisor'], | |
39 | + } | |
40 | +} | ... | ... |