capistrano-monit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-monit.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrew Eberbach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Capistrano::Monit
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'capistrano-monit'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install capistrano-monit
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/capistrano-monit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrew Eberbach"]
6
+ gem.email = ["andrew@ebertech.ca"]
7
+ gem.description = %q{Monit deployment helper}
8
+ gem.summary = %q{Monit deployment helper}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "capistrano-monit"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Capistrano::Monit::VERSION
17
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Monit
3
+ autoload :VERSION, "capistrano-monit/version"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Monit
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ Dir.glob(File.join(File.dirname(__FILE__), "..", "..", "recipes", "*.rb")).each do |file|
3
+ load file
4
+ end
5
+ end
@@ -0,0 +1,179 @@
1
+ require 'erb'
2
+ class Monit
3
+ class MonitoredItem
4
+ attr_accessor :depends_on
5
+ attr_accessor :base_path
6
+ attr_accessor :monit
7
+ attr_accessor :name
8
+
9
+ def initialize(name, monit, &block)
10
+ self.name = name
11
+ self.depends_on = []
12
+ self.monit = monit
13
+ instance_eval(&block)
14
+ end
15
+
16
+ def each_dependency(&block)
17
+ depends_on.each do |dependency_name|
18
+ yield monitored.detect{|m| m.name == dependency_name}.unique_name
19
+ end
20
+ end
21
+
22
+ def conf_name
23
+ "#{unique_name}.conf"
24
+ end
25
+
26
+ def conf_path
27
+ "#{home_dir}/etc/monit.d/#{conf_name}"
28
+ end
29
+
30
+ def env_path
31
+ "#{home_dir}/etc/environments/#{env_name}"
32
+ end
33
+
34
+ def env_name
35
+ "#{unique_name}.env"
36
+ end
37
+
38
+ def to_monit
39
+ ERB.new(File.read(template_path)).result(binding)
40
+ end
41
+
42
+ def unique_name
43
+ "#{application}.#{stage}.#{name}"
44
+ end
45
+
46
+ def to_environment
47
+ ERB.new(File.read(environment_template_path)).result(binding)
48
+ end
49
+
50
+ def method_missing(method, *args)
51
+ monit.send(method, *args)
52
+ end
53
+
54
+ def needs_environment?
55
+ false
56
+ end
57
+ end
58
+
59
+ class MonitoredFile < MonitoredItem
60
+ attr_accessor :path
61
+
62
+ def template_path
63
+ File.expand_path("../../templates/file.conf.erb", __FILE__)
64
+ end
65
+
66
+ def file_path
67
+ File.join(current_path, path)
68
+ end
69
+ end
70
+
71
+ class MonitoredProgram < MonitoredItem
72
+ attr_accessor :command
73
+ attr_accessor :log_file
74
+ attr_accessor :pid_file
75
+ attr_accessor :bundler
76
+
77
+ def initialize(*args)
78
+ self.bundler = true
79
+ super
80
+ end
81
+
82
+ def start_command
83
+ %Q{#{monit_rvm_shell_path} #{env_path} start 'cd #{current_path} && #{bundle} #{command}'}
84
+ end
85
+
86
+ def stop_command
87
+ %Q{#{monit_rvm_shell_path} #{env_path} stop 'cd #{current_path} && #{bundle}'}
88
+ end
89
+
90
+ def bundle
91
+ bundler ? "bundle exec" : ""
92
+ end
93
+
94
+ def pidfile_path
95
+ File.join(current_path, pid_file)
96
+ end
97
+
98
+ def logfile_path
99
+ File.join(current_path, log_file)
100
+ end
101
+
102
+ def gem_base_dir
103
+ capture(%Q{cd #{current_path} && #{bundle} ruby -e 'puts Gem.loaded_specs["capistrano-monit"].gem_dir'}).strip
104
+ end
105
+
106
+ def monit_rvm_shell_path
107
+ File.join(gem_base_dir, "scripts/rvm-monit-shell")
108
+ end
109
+
110
+ def program_search_path
111
+ "/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin"
112
+ end
113
+
114
+ def template_path
115
+ File.expand_path("../../templates/program.conf.erb", __FILE__)
116
+ end
117
+
118
+ def environment_template_path
119
+ File.expand_path("../../templates/environment.erb", __FILE__)
120
+ end
121
+
122
+ def needs_environment?
123
+ true
124
+ end
125
+
126
+ end
127
+
128
+ TYPES = {
129
+ :file => MonitoredFile,
130
+ :program => MonitoredProgram
131
+ }
132
+
133
+ class << self
134
+ def configure(&block)
135
+ new(&block)
136
+ end
137
+
138
+ def load_from(file, context)
139
+ self.instance_eval(File.read(file), file).tap do |monit|
140
+ monit.context = context
141
+ end
142
+ end
143
+ end
144
+
145
+ def template_path
146
+ File.expand_path("../../templates/monit.conf.erb", __FILE__)
147
+ end
148
+
149
+ def to_monit
150
+ ERB.new(File.read(template_path)).result(binding)
151
+ end
152
+
153
+ def monitd_path
154
+ File.join(home_dir, "etc/monit.d")
155
+ end
156
+
157
+ attr_accessor :port
158
+ attr_accessor :monitored
159
+ attr_accessor :context
160
+
161
+ def method_missing(method, *args)
162
+ if context && context.respond_to?(method)
163
+ context.send(method, *args)
164
+ else
165
+ super
166
+ end
167
+ end
168
+
169
+ def initialize(&block)
170
+ self.monitored = []
171
+ self.port = 1192
172
+ instance_eval(&block)
173
+ end
174
+
175
+ def monitors(options, &block)
176
+ type, name = options.first
177
+ self.monitored << TYPES[type].new(name, self, &block)
178
+ end
179
+ end
@@ -0,0 +1,64 @@
1
+ require 'monit'
2
+
3
+ _cset(:home_dir){ capture("echo $HOME").strip }
4
+ _cset(:monit_config_path) { File.join(Dir.pwd, "config", "monit", "#{stage}.rb") }
5
+ namespace :monit do
6
+
7
+ task :make_directories do
8
+ home_dir =
9
+ %W{etc/
10
+ etc/monit.d
11
+ etc/environments
12
+ var/
13
+ var/run
14
+ var/log
15
+ var/monit
16
+ /etc/cron.d/reboot/
17
+ }.map do |dir|
18
+ File.join(fetch(:home_dir), dir)
19
+ end.tap{|dirs| run %Q{mkdir -p #{dirs.join(" ")}}}
20
+ end
21
+
22
+ task :upload_config do
23
+ if File.exists?(monit_config_path)
24
+ ::Monit.load_from(monit_config_path, self).tap do |monit|
25
+ put monit.to_monit, "#{home_dir}/etc/monit.conf"
26
+ run "chmod 600 #{"#{home_dir}/etc/monit.conf"}"
27
+ end
28
+ end
29
+ end
30
+
31
+ task :setup do
32
+ make_directories
33
+ upload_config
34
+ end
35
+
36
+ desc "Tell monit to reread its configuration"
37
+ task :reload do
38
+ run "monit -c #{home_dir}/etc/monit.conf"
39
+ run "monit -c #{home_dir}/etc/monit.conf reload"
40
+ end
41
+
42
+ task :restart do
43
+ run "monit -c #{home_dir}/etc/monit.conf"
44
+ run "monit -c #{home_dir}/etc/monit.conf restart all"
45
+ end
46
+
47
+ task :deploy do
48
+ if File.exists?(monit_config_path)
49
+ ::Monit.load_from(monit_config_path, self).tap do |monit|
50
+ monit.monitored.each do |monitored_item|
51
+ put monitored_item.to_monit, monitored_item.conf_path
52
+ if monitored_item.needs_environment?
53
+ put monitored_item.to_environment, monitored_item.env_path
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ after "deploy:setup", "monit:setup"
62
+ after "deploy:update_code", "monit:deploy"
63
+ after "monit:deploy", "monit:restart"
64
+ after "monit:setup", "monit:reload"
@@ -0,0 +1,114 @@
1
+ #!/bin/bash
2
+ eval HOME=~`whoami`
3
+
4
+ RC_FILE=$1
5
+ shift
6
+
7
+ start() {
8
+ shift
9
+ if ! mkdir -p "$(dirname "$PIDFILE")"
10
+ then
11
+ echo "Failed making $(dirname "$PIDFILE")"
12
+ exit 3
13
+ fi
14
+
15
+ if ! mkdir -p "$(dirname "$LOGFILE")"
16
+ then
17
+ echo "Failed making $(dirname "$LOGFILE")"
18
+ exit 4
19
+ fi
20
+
21
+ echo $$ > "$PIDFILE";
22
+
23
+ if [ -z "$1" ]
24
+ then
25
+ echo "You must pass in a command to execute"
26
+ usage
27
+ fi
28
+ echo "Writing to $LOGFILE"
29
+
30
+ exec bash -c "$@ >> $LOGFILE 2>&1"
31
+ }
32
+
33
+ stop() {
34
+ if [ -f "$PIDFILE" ]
35
+ then
36
+ echo "killing $PIDFILE"
37
+ if pkill -TERM -P $(cat "$PIDFILE")
38
+ then
39
+ rm "$PIDFILE"
40
+ else
41
+ echo "Failed to kill, leaving pidfile."
42
+ exit 2
43
+ fi
44
+ else
45
+ echo "No such pid file $PIDFILE"
46
+ exit 3
47
+ fi
48
+ }
49
+
50
+ usage() {
51
+ echo "usage: $0 [PATH_TO_RC] {start|stop|restart} COMMAND [ARG1, ARG2...]"
52
+ exit 1
53
+ }
54
+
55
+ if [ -z "$RC_FILE" ]
56
+ then
57
+ RC_FILE=$HOME/.rvmmonitrc
58
+ fi
59
+
60
+ if [ -f "$RC_FILE" ]
61
+ then
62
+ source "$RC_FILE"
63
+ else
64
+ echo "The first argument needs to be a path to a file that will be sourced before running since $RC_FILE does not exist"
65
+ usage
66
+ fi
67
+
68
+ # Load RVM into a shell session *as a function*
69
+ if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
70
+ # First try to load from a user install
71
+ source "$HOME/.rvm/scripts/rvm"
72
+ elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
73
+ # Then try to load from a root install
74
+ source "/usr/local/rvm/scripts/rvm"
75
+ else
76
+ printf "ERROR: An RVM installation was not found.\n"
77
+ exit 2
78
+ fi
79
+
80
+ if [ -z "$LOGFILE" ]
81
+ then
82
+ echo "You must specify a LOGFILE environment variable. You can put it in $RC_FILE"
83
+ usage
84
+ fi
85
+
86
+ if [ -z "$PIDFILE" ]
87
+ then
88
+ echo "You must specify a PIDFILE environment variable. You can put it in $RC_FILE"
89
+ usage
90
+ fi
91
+
92
+ if [ -z "$RVM_STRING" ]
93
+ then
94
+ echo "You must specify a RVM_STRING environment variable. You can put it in $RC_FILE"
95
+ usage
96
+ fi
97
+
98
+ rvm use "$RVM_STRING" > /dev/null 2>&1
99
+
100
+ case $1 in
101
+ start)
102
+ start $*
103
+ ;;
104
+ stop)
105
+ stop
106
+ ;;
107
+ restart)
108
+ stop
109
+ start $*
110
+ ;;
111
+ *)
112
+ usage ;;
113
+ esac
114
+ exit 0
@@ -0,0 +1,5 @@
1
+ export PIDFILE=<%= pidfile_path %>
2
+ export LOGFILE=<%= logfile_path %>
3
+ export PATH=<%= program_search_path %>
4
+ export RVM_STRING=<%= rvm_ruby_string %>
5
+ export RAILS_ENV=<%= stage %>
@@ -0,0 +1,2 @@
1
+ check file <%= unique_name%> path <%= file_path%>
2
+ if changed timestamp then restart
@@ -0,0 +1,25 @@
1
+ set daemon 5
2
+ set logfile <%= home_dir %>/var/log/monit.log
3
+ set idfile <%= home_dir %>/var/monit/id
4
+ set statefile <%= home_dir %>/var/monit/state
5
+
6
+ set eventqueue
7
+ basedir <%= home_dir %>/var/monit/eventqueue # set the base directory where events will be stored
8
+ slots 1000 # optionally limit the queue size
9
+
10
+ set mail-format {
11
+ from: monit@ebertech.ca
12
+ subject: [monit] $SERVICE $EVENT
13
+ message: $EVENT Service $SERVICE
14
+ Host: $HOST
15
+ Date: $DATE
16
+ Action: $ACTION
17
+ Description: $DESCRIPTION
18
+ }
19
+ set mmonit https://monit:monit@mmonit.ebertech.ca/collector
20
+
21
+ set httpd port <%= port %> and
22
+ allow localhost
23
+ allow mmonit.ebertech.ca
24
+
25
+ Include <%= monitd_path %>/*.conf
@@ -0,0 +1,7 @@
1
+ check process <%= unique_name %>
2
+ with pidfile <%= pidfile_path %>
3
+ start program = "<%= start_command %>"
4
+ stop program = "<%= stop_command %>"
5
+ <% each_dependency do |dependency| %>
6
+ depends on <%= dependency %>
7
+ <% end %>
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-monit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Eberbach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Monit deployment helper
15
+ email:
16
+ - andrew@ebertech.ca
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - capistrano-monit.gemspec
27
+ - lib/capistrano-monit.rb
28
+ - lib/capistrano-monit/version.rb
29
+ - lib/capistrano/monit.rb
30
+ - lib/monit.rb
31
+ - recipes/monit.rb
32
+ - scripts/rvm-monit-shell
33
+ - templates/environment.erb
34
+ - templates/file.conf.erb
35
+ - templates/monit.conf.erb
36
+ - templates/program.conf.erb
37
+ homepage: ''
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Monit deployment helper
61
+ test_files: []