module Dumpr::Util
Public Class Methods
cat_file(h, fn)
click to toggle source
return contents of a file
# File lib/dumpr/util.rb, line 35 def self.cat_file(h, fn) cmd = "cat #{fn}" if h == "localhost" `#{cmd}`.strip else `ssh #{h} #{cmd}`.strip end end
dir_exists?(h, fn)
click to toggle source
# File lib/dumpr/util.rb, line 15 def self.dir_exists?(h, fn) if h == "localhost" File.exists?(fn) else `ssh #{h} test -d '#{fn}' &> /dev/null` $?.success? end end
file_exists?(h, fn)
click to toggle source
# File lib/dumpr/util.rb, line 6 def self.file_exists?(h, fn) if h == "localhost" File.exists?(fn) else `ssh #{h} test -f '#{fn}' &> /dev/null` $?.success? end end
human_file_size(h, fn)
click to toggle source
return the human readable size of a file like 10MB
# File lib/dumpr/util.rb, line 54 def self.human_file_size(h, fn) cmd = "du -h #{fn} | cut -f 1" if h == "localhost" `#{cmd}`.strip else `ssh #{h} #{cmd}`.strip end end
process_running?(h, pid)
click to toggle source
# File lib/dumpr/util.rb, line 63 def self.process_running?(h, pid) cmd = "ps -p #{pid}" if h == "localhost" system(cmd) else system("ssh #{h} #{cmd}") end end
remove_file(h, fn)
click to toggle source
# File lib/dumpr/util.rb, line 44 def self.remove_file(h, fn) cmd = "rm #{fn}" if h == "localhost" system(cmd) else system("ssh #{h} #{cmd}") end end
touch_file(h, fn, msg=nil)
click to toggle source
touch a file and optionally overwrite it's content with msg
# File lib/dumpr/util.rb, line 25 def self.touch_file(h, fn, msg=nil) cmd = "touch #{fn}" + (msg ? " && echo '#{msg}' > #{fn}" : '') if h == "localhost" system(cmd) else system("ssh #{h} '#{cmd}'") end end
with_lockfile(h, fn, remove_dead_locks=false) { || ... }
click to toggle source
# File lib/dumpr/util.rb, line 72 def self.with_lockfile(h, fn, remove_dead_locks=false) fn = fn.chomp('.dumpr.lock') + '.dumpr.lock' mylock = nil if file_exists?(h, fn) pid = cat_file(h, fn) mylock = Process.pid == pid if mylock # my own lock.. proceed elsif process_running?(h, pid) raise BusyDumping.new "Lockfile '#{fn}' exists for another process (#{pid})!" else if remove_dead_locks puts "Removing lockfile '#{fn}' for dead process (#{pid})" remove_file(h, fn) else raise BusyDumping.new "Lockfile '#{fn}' exists for dead process (#{pid}) ! You may want to investigate the reason why, or use --force" end end end begin touch_file(h, fn, Process.pid) yield rescue => e raise e ensure remove_file(h, fn) end end