2011-12-07 22:59:34 +00:00
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
Puppet::Type.type(:god_init).provide(:install) do
|
|
|
|
desc "Install a God script"
|
|
|
|
|
|
|
|
def create
|
2012-06-01 18:24:53 +00:00
|
|
|
FileUtils.mkdir_p File.dirname(file)
|
|
|
|
|
2012-02-28 20:07:55 +00:00
|
|
|
File.open(file, 'wb') { |fh| fh.print processed_config }
|
2011-12-07 22:59:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
File.unlink file
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
2012-02-28 20:07:55 +00:00
|
|
|
File.file?(file) && File.read(file) == processed_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def processed_config
|
|
|
|
ERB.new(config).result(binding)
|
2011-12-07 22:59:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def file
|
2012-06-01 18:24:53 +00:00
|
|
|
File.join(@resource[:dir], "#{@resource[:name]}.god")
|
2011-12-07 22:59:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
@resource[:start] || ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@resource[:stop] || ''
|
|
|
|
end
|
|
|
|
|
2012-02-28 20:07:55 +00:00
|
|
|
def restart
|
|
|
|
@resource[:restart] || ''
|
|
|
|
end
|
|
|
|
|
2011-12-07 22:59:34 +00:00
|
|
|
def name
|
|
|
|
@resource[:name] || ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def pid_file
|
|
|
|
@resource[:pid_file] || ''
|
|
|
|
end
|
|
|
|
|
2012-04-30 16:02:19 +00:00
|
|
|
def interval
|
|
|
|
@resource[:interval] || 5
|
|
|
|
end
|
|
|
|
|
2011-12-07 22:59:34 +00:00
|
|
|
def config
|
|
|
|
<<-GOD
|
|
|
|
God.watch do |w|
|
|
|
|
w.name = "<%= name %>"
|
2012-04-30 16:02:19 +00:00
|
|
|
w.interval = <%= interval %>.seconds
|
2011-12-07 22:59:34 +00:00
|
|
|
|
|
|
|
w.start = lambda { system("<%= start %>") }
|
2012-06-04 20:30:49 +00:00
|
|
|
w.start_grace = <%= interval %>.seconds
|
2011-12-07 22:59:34 +00:00
|
|
|
|
|
|
|
<% if !stop.empty? %>
|
2012-04-25 22:14:41 +00:00
|
|
|
w.stop = lambda { system("<%= stop %>") ; system("killall -9 <%= name %>") }
|
2012-02-28 20:07:55 +00:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<% if !restart.empty? %>
|
|
|
|
w.restart = lambda { system("<%= restart %>") }
|
2011-12-07 22:59:34 +00:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<% if pid_file %>
|
|
|
|
w.pid_file = "<%= pid_file %>";
|
|
|
|
<% else %>
|
|
|
|
w.behavior(:clean_pid_file)
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
w.start_if do |start|
|
|
|
|
start.condition(:process_running) do |c|
|
2012-04-30 16:02:19 +00:00
|
|
|
c.interval = <%= interval %>.seconds
|
2011-12-07 22:59:34 +00:00
|
|
|
c.running = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
GOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|