8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/capistrano-mon.rb', line 8
def self.extended(configuration)
configuration.load {
namespace(:mon) {
_cset(:mon_path, "/etc/mon")
_cset(:mon_lib_path, "/var/lib/mon")
_cset(:mon_log_path, "/var/log/mon")
desc("Setup mon.")
task(:setup, :roles => :app, :except => { :no_release => true }) {
transaction {
install
_update
}
}
after 'deploy:setup', 'mon:setup'
desc("Update mon configuration.")
task(:update, :roles => :app, :except => { :no_release => true }) {
transaction {
_update
}
}
after 'deploy:update', 'mon:update'
task(:_update, :roles => :app, :except => { :no_release => true }) {
configure
restart
}
_cset(:mon_use_plugins, true)
task(:install, :roles => :app, :except => { :no_release => true }) {
install_dependencies
install_plugins if mon_use_plugins
install_service
}
_cset(:mon_platform) {
capture((<<-EOS).gsub(/\s+/, ' ')).strip
if test -f /etc/debian_version; then
if test -f /etc/lsb-release && grep -i -q DISTRIB_ID=Ubuntu /etc/lsb-release; then
echo ubuntu;
else
echo debian;
fi;
elif test -f /etc/redhat-release; then
echo redhat;
else
echo unknown;
fi;
EOS
}
_cset(:mon_dependencies, %w(mon))
task(:install_dependencies, :roles => :app, :except => { :no_release => true }) {
unless mon_dependencies.empty?
case mon_platform
when /(debian|ubuntu)/i
run("#{sudo} apt-get install -q -y #{mon_dependencies.join(' ')}")
when /redhat/i
run("#{sudo} yum install -q -y #{mon_dependencies.join(' ')}")
else
end
end
}
task(:install_plugins, :roles => :app, :except => { :no_release => true }) {
update_plugins
}
def tempfile(name)
f = Tempfile.new(name)
path = f.path
f.close(true) path
end
_cset(:mon_plugins_path, "/usr/local/lib/mon")
_cset(:mon_plugins, [])
task(:update_plugins, :roles => :app, :except => { :no_release => true }) {
srcs = mon_plugins.map { |uri, name| uri }
tmps = mon_plugins.map { |uri, name| tempfile('capistrano-mon') }
dsts = mon_plugins.map { |uri, name|
basename = File.basename(name || URI.parse(uri).path)
case basename
when /\.alert$/
File.join(mon_plugins_path, 'alert.d', basename)
when /\.monitor$/
File.join(mon_plugins_path, 'mon.d', basename)
else
abort("Unknown plugin type: #{basename}")
end
}
begin
execute = []
dirs = dsts.map { |path| File.dirname(path) }.uniq
execute << "#{sudo} mkdir -p #{dirs.join(' ')}" unless dirs.empty?
srcs.zip(tmps, dsts) do |src, tmp, dst|
execute << "wget --no-verbose -O #{tmp.dump} #{src.dump}"
execute << "( diff -u #{dst.dump} #{tmp.dump} || #{sudo} mv -f #{tmp.dump} #{dst.dump} )"
execute << "( test -x #{dst.dump} || #{sudo} chmod a+rx #{dst.dump} )"
end
run(execute.join(' && ')) unless execute.empty?
ensure
run("rm -f #{tmps.map { |t| t.dump }.join(' ')}") unless tmps.empty?
end
}
task(:install_service, :roles => :app, :except => { :no_release => true }) {
}
def template(file)
if File.file?(file)
File.read(file)
elsif File.file?("#{file}.erb")
ERB.new(File.read("#{file}.erb")).result(binding)
else
abort("No such template: #{file} or #{file}.erb")
end
end
_cset(:mon_template_path, File.join(File.dirname(__FILE__), 'capistrano-mon', 'templates'))
_cset(:mon_configure_files, %w(/etc/default/mon mon.cf))
task(:configure, :roles => :app, :except => { :no_release => true }) {
srcs = mon_configure_files.map { |file| File.join(mon_template_path, file) }
tmps = mon_configure_files.map { |file| tempfile('capistrano-mon') }
dsts = mon_configure_files.map { |file| File.expand_path(file) == file ? file : File.join(mon_path, file) }
begin
srcs.zip(tmps) do |src, tmp|
put(template(src), tmp)
end
execute = []
dirs = dsts.map { |path| File.dirname(path) }.uniq
execute << "#{sudo} mkdir -p #{dirs.map { |dir| dir.dump }.join(' ')}" unless dirs.empty?
tmps.zip(dsts) do |tmp, dst|
execute << "( diff -u #{dst.dump} #{tmp.dump} || #{sudo} mv -f #{tmp.dump} #{dst.dump} )"
end
run(execute.join(' && ')) unless execute.empty?
ensure
run("rm -f #{tmps.map { |t| t.dump }.join(' ')}") unless tmps.empty?
end
}
_cset(:mon_service_name, 'mon')
desc("Start mon daemon.")
task(:start, :roles => :app, :except => { :no_release => true }) {
run("#{sudo} service #{mon_service_name} start")
}
desc("Stop mon daemon.")
task(:stop, :roles => :app, :except => { :no_release => true }) {
run("#{sudo} service #{mon_service_name} stop")
}
desc("Restart mon daemon.")
task(:restart, :roles => :app, :except => { :no_release => true }) {
run("#{sudo} service #{mon_service_name} restart || #{sudo} service #{mon_service_name} start")
}
desc("Show mon daemon status.")
task(:status, :roles => :app, :except => { :no_release => true }) {
run("#{sudo} service #{mon_service_name} status")
}
}
}
end
|