bosh-gen 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/ChangeLog.md +13 -0
  2. data/lib/bosh/gen/cli.rb +45 -29
  3. data/lib/bosh/gen/generators/deployment_manifest_generator.rb +7 -3
  4. data/lib/bosh/gen/generators/extract_job_generator.rb +37 -6
  5. data/lib/bosh/gen/generators/extract_package_generator.rb +23 -5
  6. data/lib/bosh/gen/generators/job_generator.rb +34 -16
  7. data/lib/bosh/gen/generators/job_generator/templates/examples/%job_name%_nginx_rack/nginx_puma_migrate.yml +14 -0
  8. data/lib/bosh/gen/generators/job_generator/templates/examples/%job_name%_nginx_rack/nginx_rackup.yml.tt +8 -0
  9. data/lib/bosh/gen/generators/job_generator/templates/examples/%job_name%_simple/default.yml +2 -0
  10. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/monit.tt +13 -0
  11. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/%job_name%_rack_ctl.tt +59 -0
  12. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_db_utils.sh.erb +54 -0
  13. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_nginx.sh.erb +17 -0
  14. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_redis_utils.sh.erb +18 -0
  15. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_setup.sh.erb +48 -0
  16. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_start.sh.erb +59 -0
  17. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/ctl_utils.sh +148 -0
  18. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/monit_debugger +13 -0
  19. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/nginx_ctl.tt +41 -0
  20. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/bin/rails_ctl_setup.sh.erb +40 -0
  21. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/config/database.yml.erb +17 -0
  22. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/config/mime.types +73 -0
  23. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/config/nginx.conf.erb +150 -0
  24. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/config/nginx_proxy.conf +11 -0
  25. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_nginx_rack/templates/config/redis.yml.erb +6 -0
  26. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/monit.tt +5 -0
  27. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/templates/bin/%job_name%_ctl.tt +36 -0
  28. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/templates/bin/monit_debugger +13 -0
  29. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/templates/data/properties.sh.erb +10 -0
  30. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/templates/helpers/ctl_setup.sh +81 -0
  31. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_simple/templates/helpers/ctl_utils.sh +156 -0
  32. data/lib/bosh/gen/generators/package_generator.rb +56 -10
  33. data/lib/bosh/gen/models.rb +1 -0
  34. data/lib/bosh/gen/models/bosh_config.rb +15 -0
  35. data/lib/bosh/gen/models/deployment_manifest.rb +5 -0
  36. data/lib/bosh/gen/version.rb +1 -1
  37. data/spec/fixtures/bosh_config/multiple_boshes.yml +34 -0
  38. data/spec/fixtures/deployment_manifests/2_jobs_1_ip_8196_disk.yml +2 -2
  39. data/spec/fixtures/deployment_manifests/2_jobs_2_ips_no_disk.yml +2 -2
  40. data/spec/generators/generator_spec_helper.rb +102 -0
  41. data/spec/generators/jobs/webapp_job_generator_spec.rb +68 -0
  42. data/spec/models/bosh_config_spec.rb +11 -0
  43. metadata +38 -8
  44. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%/monit.tt +0 -5
  45. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%/templates/%job_name%_ctl.tt +0 -62
  46. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_rubyrack/monit.tt +0 -5
  47. data/lib/bosh/gen/generators/job_generator/templates/jobs/%job_name%_rubyrack/templates/%job_name%_ctl.tt +0 -64
@@ -0,0 +1,17 @@
1
+ <% if properties.postgres %>
2
+ production:
3
+ adapter: postgresql
4
+ encoding: unicode
5
+ database: '<%= properties.postgres.database %>'
6
+ host: '<%= properties.postgres.host %>'
7
+ port: '<%= properties.postgres.port || 5432 %>'
8
+ username: '<%= properties.postgres.user %>'
9
+ password: '<%= properties.postgres.password %>'
10
+ pool: 5
11
+ <% else %>
12
+ production:
13
+ adapter: sqlite3
14
+ database: db/production.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+ <% end %>
@@ -0,0 +1,73 @@
1
+ types {
2
+ text/html html htm shtml;
3
+ text/css css;
4
+ text/xml xml;
5
+ image/gif gif;
6
+ image/jpeg jpeg jpg;
7
+ application/x-javascript js;
8
+ application/atom+xml atom;
9
+ application/rss+xml rss;
10
+
11
+ text/mathml mml;
12
+ text/plain txt;
13
+ text/vnd.sun.j2me.app-descriptor jad;
14
+ text/vnd.wap.wml wml;
15
+ text/x-component htc;
16
+
17
+ image/png png;
18
+ image/tiff tif tiff;
19
+ image/vnd.wap.wbmp wbmp;
20
+ image/x-icon ico;
21
+ image/x-jng jng;
22
+ image/x-ms-bmp bmp;
23
+ image/svg+xml svg;
24
+
25
+ application/java-archive jar war ear;
26
+ application/mac-binhex40 hqx;
27
+ application/msword doc;
28
+ application/pdf pdf;
29
+ application/postscript ps eps ai;
30
+ application/rtf rtf;
31
+ application/vnd.ms-excel xls;
32
+ application/vnd.ms-powerpoint ppt;
33
+ application/vnd.wap.wmlc wmlc;
34
+ application/vnd.wap.xhtml+xml xhtml;
35
+ application/vnd.google-earth.kml+xml kml;
36
+ application/vnd.google-earth.kmz kmz;
37
+ application/x-7z-compressed 7z;
38
+ application/x-cocoa cco;
39
+ application/x-java-archive-diff jardiff;
40
+ application/x-java-jnlp-file jnlp;
41
+ application/x-makeself run;
42
+ application/x-perl pl pm;
43
+ application/x-pilot prc pdb;
44
+ application/x-rar-compressed rar;
45
+ application/x-redhat-package-manager rpm;
46
+ application/x-sea sea;
47
+ application/x-shockwave-flash swf;
48
+ application/x-stuffit sit;
49
+ application/x-tcl tcl tk;
50
+ application/x-x509-ca-cert der pem crt;
51
+ application/x-xpinstall xpi;
52
+ application/zip zip;
53
+
54
+ application/octet-stream bin exe dll;
55
+ application/octet-stream deb;
56
+ application/octet-stream dmg;
57
+ application/octet-stream eot;
58
+ application/octet-stream iso img;
59
+ application/octet-stream msi msp msm;
60
+
61
+ audio/midi mid midi kar;
62
+ audio/mpeg mp3;
63
+ audio/x-realaudio ra;
64
+
65
+ video/3gpp 3gpp 3gp;
66
+ video/mpeg mpeg mpg;
67
+ video/quicktime mov;
68
+ video/x-flv flv;
69
+ video/x-mng mng;
70
+ video/x-ms-asf asx asf;
71
+ video/x-ms-wmv wmv;
72
+ video/x-msvideo avi;
73
+ }
@@ -0,0 +1,150 @@
1
+ #deployment webapp nginx.conf
2
+ <% require 'ostruct' %>
3
+ <% properties.webapp ||= OpenStruct.new %>
4
+ <% properties.webapp.nginx ||= OpenStruct.new %>
5
+ <% package_name = properties.webapp.package || 'rackapp' %>
6
+ <% webapp_port_start = 5000 %>
7
+
8
+ user vcap vcap;
9
+ worker_processes <%= properties.webapp.nginx.worker_processes || 1 %>;
10
+ pid /var/vcap/sys/run/webapp/nginx.pid;
11
+
12
+ events {
13
+ worker_connections 8192;
14
+ use epoll;
15
+ }
16
+
17
+
18
+ http {
19
+ include mime.types;
20
+ default_type text/html;
21
+ server_tokens off;
22
+
23
+ log_format main '$host - [$time_local] '
24
+ '"$request" $status $bytes_sent '
25
+ '"$http_referer" "$http_user_agent" '
26
+ '$remote_addr response_time:$upstream_response_time';
27
+
28
+ sendfile on; #enable use of sendfile()
29
+ tcp_nopush on;
30
+ tcp_nodelay on; #disable nagel's algorithm
31
+
32
+ server_names_hash_bucket_size 128;
33
+ if_modified_since before;
34
+
35
+ gzip on;
36
+ gzip_http_version 1.0;
37
+ gzip_comp_level 2;
38
+ gzip_proxied any;
39
+ gzip_buffers 16 8k;
40
+ gzip_types application/json text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
41
+
42
+ upstream <%= package_name %>_upstream {
43
+ server 127.0.0.1:<%= webapp_port_start %>;
44
+ }
45
+
46
+ server {
47
+ #
48
+ # The default port to listen on.
49
+ #
50
+ # Note that this may be also specified as IP:Port combinations.
51
+ #
52
+ # By default Nginx will listen for traffic on the specified port for all IP
53
+ # addresses assigned to the host.
54
+ #
55
+ listen <%= properties.webapp.nginx.port || 80 %>;
56
+
57
+ #
58
+ # Server Names
59
+ #
60
+ # This is the list of IP's and/or domain names with any wildcards or
61
+ # subdomains. The special value '_' is used to match all domains and IPs and
62
+ # generally denotes the default server for the entire Nginx process.
63
+ #
64
+ # Note that if you specify any domains with wild cards, the very first domain
65
+ # listed must be without a wild card. For example:
66
+ #
67
+ # server_name www.mydomain.org *.mydomain.org;
68
+ #
69
+ # It is good practice to include wildcard subdomains so that Nginx will not
70
+ # require a restart when new services are added for an application.
71
+ #
72
+ server_name <%= properties.webapp.nginx.server_name || '_' %>;
73
+ server_name_in_redirect off;
74
+
75
+ #
76
+ # This assigns the maximum accepted body size of client request. It is placed
77
+ # here as a precaution to limit against certain kinds of attacks. Be aware that
78
+ # it is here in case the application accepts large file uploads. In such a case
79
+ # this value will need to be adjusted.
80
+ #
81
+ client_max_body_size 256M;
82
+
83
+ #
84
+ # The application root which is inherited for all location blocks below.
85
+ #
86
+ root /var/vcap/packages/<%= package_name %>/public;
87
+
88
+ #
89
+ # Logging
90
+ #
91
+ # Log files are stored within the /var/vcap/sys/log/webapp/ directory.
92
+ #
93
+ access_log /var/vcap/sys/log/webapp/<%= package_name %>.access.log main;
94
+ error_log /var/vcap/sys/log/webapp/<%= package_name %>.error.log notice;
95
+
96
+ #
97
+ # Expire header on assets. For more information on the reasoning behind
98
+ # this please browse http://developer.yahoo.com/performance/rules.html#expires
99
+ #
100
+ # set Expire header on assets: see http://developer.yahoo.com/performance/rules.html#expires
101
+ location ~ ^/(images|assets|javascripts|stylesheets)/ {
102
+ try_files $uri $uri/index.html /last_assets/$uri /last_assets/$uri.html @app_<%= package_name %>;
103
+ expires 10y;
104
+ }
105
+
106
+ # For Puma, simply proxy
107
+ location @app_<%= package_name %> {
108
+ include /var/vcap/jobs/webapp/config/nginx_proxy.conf;
109
+ proxy_pass http://<%= package_name %>_upstream;
110
+ }
111
+
112
+ #
113
+ # HTTP Error handling.
114
+ #
115
+ # 404 - Resource Not found.
116
+ error_page 404 /404.html;
117
+ # 50X - Application Error(s)
118
+ error_page 500 502 504 /500.html;
119
+ # 503 - System Maintenance
120
+ error_page 503 @503;
121
+ recursive_error_pages on;
122
+ location @503 {
123
+ error_page 405 = /system/maintenance.html;
124
+
125
+ # Serve static assets if found.
126
+ if (-f $request_filename) {
127
+ break;
128
+ }
129
+
130
+ rewrite ^(.*)$ /system/maintenance.html break;
131
+ }
132
+ error_page 503 /system/maintenance.html;
133
+
134
+ #
135
+ # The default location will process the request as follows:
136
+ #
137
+ # 1. If a system maintenance page is in place, serve it.
138
+ # 2. If the request uri matches a resource on disk, serve it.
139
+ # 3. If the request uri/index.html matches a cache file on disk, serve it.
140
+ # 4. If the request uri.html is cached on disk, serve it.
141
+ # 5. Failing any caching or system maintenance, pass the request to the
142
+ # application.
143
+ #
144
+ location / {
145
+ if (-f $document_root/system/maintenance.html) { return 503; }
146
+ try_files $uri $uri/index.html $uri.html @app_<%= package_name %>;
147
+ }
148
+
149
+ }
150
+ }
@@ -0,0 +1,11 @@
1
+ index index.html index.htm;
2
+
3
+ # needed to forward user's IP address to rails
4
+ proxy_set_header X-Real-IP $remote_addr;
5
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6
+ proxy_set_header Host $http_host;
7
+ # request for $start_time patch to nginx is https://cloudfoundry.atlassian.net/browse/CF-88
8
+ # proxy_set_header X-Queue-Start 't=$start_time';
9
+
10
+ proxy_redirect off;
11
+ proxy_max_temp_file_size 0;
@@ -0,0 +1,6 @@
1
+ ---
2
+ host: '<%= (properties.redis && properties.redis.host) || 'localhost' %>'
3
+ port: <%= (properties.redis && properties.redis.port) || 6379 %>
4
+ <%= (properties.webapp && properties.webapp.framework_env) || 'production' %>:
5
+ host: '<%= (properties.redis && properties.redis.host) || 'localhost' %>'
6
+ port: <%= (properties.redis && properties.redis.port) || 6379 %>
@@ -0,0 +1,5 @@
1
+ check process <%= job_name %>
2
+ with pidfile /var/vcap/sys/run/<%= job_name %>/<%= job_name %>.pid
3
+ start program "/var/vcap/jobs/<%= job_name %>/bin/monit_debugger <%= job_name %>_ctl '/var/vcap/jobs/<%= job_name %>/bin/<%= job_name %>_ctl start'"
4
+ stop program "/var/vcap/jobs/<%= job_name %>/bin/monit_debugger <%= job_name %>_ctl '/var/vcap/jobs/<%= job_name %>/bin/<%= job_name %>_ctl stop'"
5
+ group vcap
@@ -0,0 +1,36 @@
1
+ #!/bin/bash
2
+
3
+ set -e # exit immediately if a simple command exits with a non-zero status
4
+ set -u # report the usage of uninitialized variables
5
+
6
+ # Setup env vars and folders for the webapp_ctl script
7
+ source /var/vcap/jobs/<%= job_name %>/helpers/ctl_setup.sh '<%= job_name %>'
8
+
9
+ export PORT=${PORT:-5000}
10
+ export LANG=en_US.UTF-8
11
+
12
+ case $1 in
13
+
14
+ start)
15
+ pid_guard $PIDFILE $JOB_NAME
16
+
17
+ # TODO: Run some process
18
+ exec TODO
19
+ >>$LOG_DIR/$JOB_NAME.stdout.log \
20
+ 2>>$LOG_DIR/$JOB_NAME.stderr.log
21
+
22
+ # store pid in $PIDFILE
23
+ echo $! > $PIDFILE
24
+ ;;
25
+
26
+ stop)
27
+ kill_and_wait $PIDFILE
28
+
29
+ ;;
30
+ *)
31
+ echo "Usage: <%= job_name %>_ctl {start|stop}"
32
+
33
+ ;;
34
+
35
+ esac
36
+ exit 0
@@ -0,0 +1,13 @@
1
+ #!/bin/sh
2
+ # USAGE monit_debugger <label> command to run
3
+ mkdir -p /var/vcap/sys/log/monit
4
+ {
5
+ echo "MONIT-DEBUG date"
6
+ date
7
+ echo "MONIT-DEBUG env"
8
+ env
9
+ echo "MONIT-DEBUG $@"
10
+ $2 $3 $4 $5 $6 $7
11
+ R=$?
12
+ echo "MONIT-DEBUG exit code $R"
13
+ } >/var/vcap/sys/log/monit/monit_debugger.$1.log 2>&1
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # job template binding variables
4
+
5
+ # job name & index of this VM within cluster
6
+ # e.g. JOB_NAME=redis, JOB_INDEX=0
7
+ export NAME='<%= name %>'
8
+ export JOB_INDEX=<%= index %>
9
+ # full job name, like redis/0 or webapp/3
10
+ export JOB_FULL="$NAME/$JOB_INDEX"
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Setup env vars and folders for the ctl script
4
+ # This helps keep the ctl script as readable
5
+ # as possible
6
+
7
+ # Usage options:
8
+ # source /var/vcap/jobs/foobar/helpers/ctl_setup.sh JOB_NAME OUTPUT_LABEL
9
+ # source /var/vcap/jobs/foobar/helpers/ctl_setup.sh foobar
10
+ # source /var/vcap/jobs/foobar/helpers/ctl_setup.sh foobar foobar
11
+ # source /var/vcap/jobs/foobar/helpers/ctl_setup.sh foobar nginx
12
+
13
+ set -e # exit immediately if a simple command exits with a non-zero status
14
+ set -u # report the usage of uninitialized variables
15
+
16
+ JOB_NAME=$1
17
+ output_label=${1:-JOB_NAME}
18
+
19
+ export JOB_DIR=/var/vcap/jobs/$JOB_NAME
20
+ chmod 755 $JOB_DIR # to access file via symlink
21
+
22
+ # Load some bosh deployment properties into env vars
23
+ # Try to put all ERb into data/properties.sh.erb
24
+ # incl $NAME, $JOB_INDEX, $WEBAPP_DIR
25
+ source $JOB_DIR/data/properties.sh
26
+
27
+ source $JOB_DIR/helpers/ctl_utils.sh
28
+ redirect_output ${output_label}
29
+
30
+ export HOME=${HOME:-/home/vcap}
31
+
32
+ # Add all packages' /bin & /sbin into $PATH
33
+ for package_bin_dir in $(ls -d /var/vcap/packages/*/*bin)
34
+ do
35
+ export PATH=${package_bin_dir}:$PATH
36
+ done
37
+
38
+ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-''} # default to empty
39
+ for package_bin_dir in $(ls -d /var/vcap/packages/*/lib)
40
+ do
41
+ export LD_LIBRARY_PATH=${package_bin_dir}:$LD_LIBRARY_PATH
42
+ done
43
+
44
+ # Setup log, run and tmp folders
45
+
46
+ export RUN_DIR=/var/vcap/sys/run/$JOB_NAME
47
+ export LOG_DIR=/var/vcap/sys/log/$JOB_NAME
48
+ export TMP_DIR=/var/vcap/sys/tmp/$JOB_NAME
49
+ export STORE_DIR=/var/vcap/store/$JOB_NAME
50
+ for dir in $RUN_DIR $LOG_DIR $TMP_DIR $STORE_DIR
51
+ do
52
+ mkdir -p ${dir}
53
+ chown vcap:vcap ${dir}
54
+ chmod 775 ${dir}
55
+ done
56
+ export TMPDIR=$TMP_DIR
57
+
58
+ export C_INCLUDE_PATH=/var/vcap/packages/mysqlclient/include/mysql:/var/vcap/packages/sqlite/include:/var/vcap/packages/libpq/include
59
+ export LIBRARY_PATH=/var/vcap/packages/mysqlclient/lib/mysql:/var/vcap/packages/sqlite/lib:/var/vcap/packages/libpq/lib
60
+
61
+ # consistent place for vendoring python libraries within package
62
+ if [[ -d ${WEBAPP_DIR:-/xxxx} ]]
63
+ then
64
+ export PYTHONPATH=$WEBAPP_DIR/vendor/lib/python
65
+ fi
66
+
67
+ if [[ -d /var/vcap/packages/java7 ]]
68
+ then
69
+ export JAVA_HOME="/var/vcap/packages/java7"
70
+ fi
71
+
72
+ # setup CLASSPATH for all jars/ folders within packages
73
+ export CLASSPATH=${CLASSPATH:-''} # default to empty
74
+ for java_jar in $(ls -d /var/vcap/packages/*/*/*.jar)
75
+ do
76
+ export CLASSPATH=${java_jar}:$CLASSPATH
77
+ done
78
+
79
+ PIDFILE=$RUN_DIR/$JOB_NAME.pid
80
+
81
+ echo '$PATH' $PATH
@@ -0,0 +1,156 @@
1
+ # Helper functions used by ctl scripts
2
+
3
+ # links a job file (probably a config file) into a package
4
+ # Example usage:
5
+ # link_job_file_to_package config/redis.yml [config/redis.yml]
6
+ # link_job_file_to_package config/wp-config.php wp-config.php
7
+ link_job_file_to_package() {
8
+ source_job_file=$1
9
+ target_package_file=${2:-$source_job_file}
10
+ full_package_file=$WEBAPP_DIR/${target_package_file}
11
+
12
+ link_job_file ${source_job_file} ${full_package_file}
13
+ }
14
+
15
+ # links a job file (probably a config file) somewhere
16
+ # Example usage:
17
+ # link_job_file config/bashrc /home/vcap/.bashrc
18
+ link_job_file() {
19
+ source_job_file=$1
20
+ target_file=$2
21
+ full_job_file=$JOB_DIR/${source_job_file}
22
+
23
+ echo link_job_file ${full_job_file} ${target_file}
24
+ if [[ ! -f ${full_job_file} ]]
25
+ then
26
+ echo "file to link ${full_job_file} does not exist"
27
+ else
28
+ # Create/recreate the symlink to current job file
29
+ # If another process is using the file, it won't be
30
+ # deleted, so don't attempt to create the symlink
31
+ mkdir -p $(dirname ${target_file})
32
+ ln -nfs ${full_job_file} ${target_file}
33
+ fi
34
+ }
35
+
36
+ # If loaded within monit ctl scripts then pipe output
37
+ # If loaded from 'source ../utils.sh' then normal STDOUT
38
+ redirect_output() {
39
+ SCRIPT=$1
40
+ mkdir -p /var/vcap/sys/log/monit
41
+ exec 1>> /var/vcap/sys/log/monit/$SCRIPT.log
42
+ exec 2>> /var/vcap/sys/log/monit/$SCRIPT.err.log
43
+ }
44
+
45
+ pid_guard() {
46
+ pidfile=$1
47
+ name=$2
48
+
49
+ if [ -f "$pidfile" ]; then
50
+ pid=$(head -1 "$pidfile")
51
+
52
+ if [ -n "$pid" ] && [ -e /proc/$pid ]; then
53
+ echo "$name is already running, please stop it first"
54
+ exit 1
55
+ fi
56
+
57
+ echo "Removing stale pidfile..."
58
+ rm $pidfile
59
+ fi
60
+ }
61
+
62
+ wait_pid() {
63
+ pid=$1
64
+ try_kill=$2
65
+ timeout=${3:-0}
66
+ force=${4:-0}
67
+ countdown=$(( $timeout * 10 ))
68
+
69
+ echo wait_pid $pid $try_kill $timeout $force $countdown
70
+ if [ -e /proc/$pid ]; then
71
+ if [ "$try_kill" = "1" ]; then
72
+ echo "Killing $pidfile: $pid "
73
+ kill $pid
74
+ fi
75
+ while [ -e /proc/$pid ]; do
76
+ sleep 0.1
77
+ [ "$countdown" != '0' -a $(( $countdown % 10 )) = '0' ] && echo -n .
78
+ if [ $timeout -gt 0 ]; then
79
+ if [ $countdown -eq 0 ]; then
80
+ if [ "$force" = "1" ]; then
81
+ echo -ne "\nKill timed out, using kill -9 on $pid... "
82
+ kill -9 $pid
83
+ sleep 0.5
84
+ fi
85
+ break
86
+ else
87
+ countdown=$(( $countdown - 1 ))
88
+ fi
89
+ fi
90
+ done
91
+ if [ -e /proc/$pid ]; then
92
+ echo "Timed Out"
93
+ else
94
+ echo "Stopped"
95
+ fi
96
+ else
97
+ echo "Process $pid is not running"
98
+ echo "Attempting to kill pid anyway..."
99
+ kill $pid
100
+ fi
101
+ }
102
+
103
+ wait_pidfile() {
104
+ pidfile=$1
105
+ try_kill=$2
106
+ timeout=${3:-0}
107
+ force=${4:-0}
108
+ countdown=$(( $timeout * 10 ))
109
+
110
+ if [ -f "$pidfile" ]; then
111
+ pid=$(head -1 "$pidfile")
112
+ if [ -z "$pid" ]; then
113
+ echo "Unable to get pid from $pidfile"
114
+ exit 1
115
+ fi
116
+
117
+ wait_pid $pid $try_kill $timeout $force
118
+
119
+ rm -f $pidfile
120
+ else
121
+ echo "Pidfile $pidfile doesn't exist"
122
+ fi
123
+ }
124
+
125
+ kill_and_wait() {
126
+ pidfile=$1
127
+ # Monit default timeout for start/stop is 30s
128
+ # Append 'with timeout {n} seconds' to monit start/stop program configs
129
+ timeout=${2:-25}
130
+ force=${3:-1}
131
+ if [[ -f ${pidfile} ]]
132
+ then
133
+ wait_pidfile $pidfile 1 $timeout $force
134
+ else
135
+ # TODO assume $1 is something to grep from 'ps ax'
136
+ pid="$(ps auwwx | grep "$1" | awk '{print $2}')"
137
+ wait_pid $pid 1 $timeout $force
138
+ fi
139
+ }
140
+
141
+ check_nfs_mount() {
142
+ opts=$1
143
+ exports=$2
144
+ mount_point=$3
145
+
146
+ if grep -qs $mount_point /proc/mounts; then
147
+ echo "Found NFS mount $mount_point"
148
+ else
149
+ echo "Mounting NFS..."
150
+ mount $opts $exports $mount_point
151
+ if [ $? != 0 ]; then
152
+ echo "Cannot mount NFS from $exports to $mount_point, exiting..."
153
+ exit 1
154
+ fi
155
+ fi
156
+ }