puppet-standalone-mashup/shared/lib/puppet/provider/god_init/install.rb

87 lines
1.4 KiB
Ruby
Raw Normal View History

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 }
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)
end
private
def file
2012-06-01 18:24:53 +00:00
File.join(@resource[:dir], "#{@resource[:name]}.god")
end
def start
@resource[:start] || ''
end
def stop
@resource[:stop] || ''
end
2012-02-28 20:07:55 +00:00
def restart
@resource[:restart] || ''
end
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
def config
<<-GOD
God.watch do |w|
w.name = "<%= name %>"
2012-04-30 16:02:19 +00:00
w.interval = <%= interval %>.seconds
w.start = lambda { system("<%= start %>") }
2012-06-04 20:30:49 +00:00
w.start_grace = <%= interval %>.seconds
<% 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 %>") }
<% 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
c.running = false
end
end
end
GOD
end
end