lib/capistrano-mon.rb in capistrano-mon-0.0.3 vs lib/capistrano-mon.rb in capistrano-mon-0.0.4

- old
+ new

@@ -1,8 +1,9 @@ require "capistrano-mon/version" +require "capistrano/configuration/actions/file_transfer_ext" +require "capistrano/configuration/resources/file_resources" require "erb" -require "tempfile" require "uri" module Capistrano module Mon def self.extended(configuration) @@ -63,11 +64,11 @@ } } # Do not run automatically during normal `deploy' to avoid slow down. # If you want to do so, add following line in your ./config/deploy.rb # - # after 'deploy:update', 'mon:update' + # after 'deploy:finalize_update', 'mon:update' task(:_update, :roles => :app, :except => { :no_release => true }) { configure restart } @@ -110,17 +111,10 @@ task(:install_plugins, :roles => :app, :except => { :no_release => true }) { update_plugins } - def tempfile(name) - f = Tempfile.new(name) - path = f.path - f.close(true) # close and remove tempfile immediately - path - end - _cset(:mon_plugins_path, "/usr/local/lib/mon") _cset(:mon_plugins, []) # # Example: # @@ -135,11 +129,11 @@ # "https://gist.github.com/raw/2321002/pyhttp.monitor.py" => "pyhttp.monitor", # }} # 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') } + tmps = mon_plugins.map { |uri, name| capture("t=$(mktemp /tmp/capistrano-mon.XXXXXXXXXX);rm -f $t;echo $t").chomp } 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) @@ -166,38 +160,15 @@ task(:install_service, :roles => :app, :except => { :no_release => true }) { # TODO: setup (sysvinit|daemontools|upstart|runit|systemd) service of mon } - 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? + mon_configure_files.each do |f| + safe_put(template(f, :path => mon_template_path), (File.expand_path(f) == f ? f : File.join(mon_path, f)), + :sudo => true, :place => :if_modified) end } _cset(:mon_service_name, 'mon') desc("Start mon daemon.")