Module: Capitate::Plugins::Utils
- Defined in:
- lib/capitate/plugins/utils.rb
Instance Method Summary collapse
-
#append_to(path, data, check = nil, left_strip = true, should_exist = true) ⇒ Object
Append data to a file.
-
#egrep(grep, path) ⇒ Object
Grep file for regex.
-
#exist?(path) ⇒ Boolean
Check if file exists.
-
#hostname ⇒ Object
Get the hostname of the remote server.
-
#install_template(template_path, destination, options = {}) ⇒ Object
Load template and install it.
-
#ln(*args) ⇒ Object
Symlink source to dest.
-
#rm(path) ⇒ Object
Delete file.
-
#rm_rf(path) ⇒ Object
Delete file (recursive/force).
Instance Method Details
#append_to(path, data, check = nil, left_strip = true, should_exist = true) ⇒ Object
Append data to a file. Optionally check that it exists before adding.
Options
pathPath to file to append to
dataString data to append
checkIf not nil, will check to see if egrep matches this regex and will not re-append
left_stripIf true (default), remove whitespace before lines
should_existIf true (default), raise error if file does not exist
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/capitate/plugins/utils.rb', line 136 def append_to(path, data, check = nil, left_strip = true, should_exist = true) # If checking and found expression then abort append return if check and egrep(check, path) # If should exist and doesn't then abort append raise "Can't append to file. File should exist: #{path}" if should_exist and !exist?(path) data.split("\n").each do |line| line = line.gsub(/^\s+/, "") if left_strip run_via "echo '#{line}' >> #{path}" end end |
#egrep(grep, path) ⇒ Object
Grep file for regex. Returns true if found, false otherwise.
Options
grepRegular expression
pathPath to file
Example
utils.egrep("^mail.\*", "/etc/syslog.conf") => true
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/capitate/plugins/utils.rb', line 82 def egrep(grep, path) found = true run_via %{egrep '#{grep}' #{path} || echo $?} do |channel, stream, data| if data =~ /^(\d+)/ if $1.to_i > 0 logger.trace "Not found" found = false end end end found end |
#exist?(path) ⇒ Boolean
Check if file exists.
Options
pathPath to file
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/capitate/plugins/utils.rb', line 100 def exist?(path) found = true run_via "head -1 #{path} >/dev/null 2>&1 || echo $?" do |channel, stream, data| if data =~ /^(\d+)/ if $1.to_i > 0 logger.trace "Not found" found = false end end end found end |
#hostname ⇒ Object
Get the hostname of the remote server
Example
utils.hostname => "localhost.localdomain"
118 119 120 121 122 123 124 |
# File 'lib/capitate/plugins/utils.rb', line 118 def hostname hostname = nil run "hostname" do |channel, stream, data| hostname = data.chomp end hostname end |
#install_template(template_path, destination, options = {}) ⇒ Object
Load template and install it. Removes temporary files during transfer and ensures desination directory is created before install.
See template plugin for where template paths are loaded from.
Options
template_pathPath to template
destinationRemote path to evaluated template
optionsOptions (see Install template options)
Install template options
userUser to install (-o). Defaults to root.
modeMode to install file (-m)
Example
utils.install_template("monit/memcached.monitrc.erb", "/etc/monit/memcached.monitrc", :user => "root", :mode => "600")
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/capitate/plugins/utils.rb', line 57 def install_template(template_path, destination, = {}) # Truncate extension tmp_file_path = template_path.gsub("/", "_").gsub(/.erb$/, "") tmp_path = "/tmp/#{tmp_file_path}" [:user] ||= "root" = [] << "-o #{[:user]}" << "-m #{[:mode]}" if .has_key?(:mode) put template.load(template_path), tmp_path # TOOD: Ensure directory exists? mkdir -p #{File.dirname(destination)} run_via "install #{.join(" ")} #{tmp_path} #{destination} && rm -f #{tmp_path}" end |
#ln(*args) ⇒ Object
Symlink source to dest
Options
argsArguments. If argument is hash, then key is symlinked to value.
Examples
utils.ln("src/foo" => "dest/foo") # Run: ln -nfs src/foo dest/foo utils.ln("src/foo" => "dest/foo", "src/bar" => "dest/bar") # Links both
12 13 14 15 16 17 18 19 20 |
# File 'lib/capitate/plugins/utils.rb', line 12 def ln(*args) args.each do |arg| if arg.is_a?(Hash) arg.each do |src, dest| run_via "ln -nfs #{src} #{dest}" end end end end |
#rm(path) ⇒ Object
Delete file.
Options
pathPath to delete
27 28 29 |
# File 'lib/capitate/plugins/utils.rb', line 27 def rm(path) run_via "rm #{path}" end |
#rm_rf(path) ⇒ Object
Delete file (recursive/force)
Options
pathPath to delete
36 37 38 |
# File 'lib/capitate/plugins/utils.rb', line 36 def rm_rf(path) run_via "rm -rf #{path}" end |