Module: Std
- Defined in:
- lib/vmbuilder_plugins/std.rb
Overview
Purpose
Std is a Capistrano plugin that provides a set of standard methods refactored out of several Capistrano task libraries.
Installs within Capistrano as the plugin std
Usage
require 'vmbuilder_plugins/std'
Prefix all calls to the library with std.
Instance Method Summary collapse
-
#connect_as_root(&block) ⇒ Object
Wrap this around your task to force a connection as root.
-
#fput(file_pattern, destination, options = {}) ⇒ Object
Copies the files specified by
file_pattern
todestination
. -
#ignore_no_servers_error(&block) ⇒ Object
Wrap this around your task calls to catch the no servers error and ignore it std.ignore_no_servers_error do activate_mysql end.
-
#patch(patchfile, level = '0', where = '/') ⇒ Object
Run a patchfile on the servers Ignores reverses and rejects.
-
#put_template(template, destination, options = {}) ⇒ Object
Render a template file and upload it to the servers.
-
#random_string(size = 10) ⇒ Object
Returns a random string of alphanumeric characters of size
size
Useful for passwords, usernames and the like. -
#relative_path(from_str, to_str) ⇒ Object
Return a relative path from the destination directory
from_str
to the target file/directoryto_str
. -
#ruby(cmd, options = {}, &block) ⇒ Object
Run a ruby command file on the servers.
-
#su_delete(path, options = {}) ⇒ Object
Deletes the given file(s) from all servers targetted by the current task, but runs the
delete
command according to the current setting of:use_sudo
. -
#su_put(data, destination, temporary_area = '/tmp', options = {}) ⇒ Object
Upload
data
totemporary_area
before installing it indestination
using sudo. -
#unzip(file_pattern, destination, options = {}) ⇒ Object
Copies the
file_pattern
, which is assumed to be a tar file of some description (gzipped or plain), and unpacks it intodestination
.
Instance Method Details
#connect_as_root(&block) ⇒ Object
Wrap this around your task to force a connection as root. Flushes the session cache before and after the connection.
std.connect_as_root do
install_sudo
end
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/vmbuilder_plugins/std.rb', line 120 def connect_as_root (&block) begin tempuser = user set :user, "root" actor.sessions.delete_if { true } yield tempuser ensure set :user, tempuser if tempuser actor.sessions.delete_if { true } end end |
#fput(file_pattern, destination, options = {}) ⇒ Object
Copies the files specified by file_pattern
to destination
Error checking is minimal - a pattern onto a single file will result in destination
containing the data from the last file only.
Installs via sudo, options
are as for put.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vmbuilder_plugins/std.rb', line 40 def fput(file_pattern, destination, ={}) logger.info file_pattern Dir.glob(file_pattern) do |fname| if File.readable?(fname) then if MMAP logger.debug "Using Memory Mapped File Upload" fdata=Mmap.new(fname,"r", Mmap::MAP_SHARED, :advice => Mmap::MADV_SEQUENTIAL) else fdata=File.open(fname).read end su_put(fdata, destination, File.join('/tmp',File.basename(fname)), ) else logger.error "Unable to read file #{fname}" end end end |
#ignore_no_servers_error(&block) ⇒ Object
Wrap this around your task calls to catch the no servers error and ignore it
std.ignore_no_servers_error do
activate_mysql
end
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vmbuilder_plugins/std.rb', line 101 def ignore_no_servers_error (&block) begin yield rescue RuntimeError => failure if failure. =~ /no servers matched/ logger.debug "Ignoring 'no servers matched' error in task #{current_task.name}" else raise end end end |
#patch(patchfile, level = '0', where = '/') ⇒ Object
Run a patchfile on the servers Ignores reverses and rejects.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/vmbuilder_plugins/std.rb', line 164 def patch(patchfile, level = '0', where = '/') temp_name = random_string begin fput(patchfile, temp_name, :mode => 0600) send(run_method, %{ patch -p#{level} -tNd #{where} -r /dev/null < #{temp_name} || true }) ensure delete temp_name end end |
#put_template(template, destination, options = {}) ⇒ Object
Render a template file and upload it to the servers
189 190 191 192 193 194 195 196 197 |
# File 'lib/vmbuilder_plugins/std.rb', line 189 def put_template(template, destination, ={}) if MMAP logger.debug "Using Memory Mapped File Upload" fdata=Mmap.new(template,"r", Mmap::MAP_SHARED, :advice => Mmap::MADV_SEQUENTIAL) else fdata=File.read(template) end put(render(:template => fdata), destination, ) end |
#random_string(size = 10) ⇒ Object
Returns a random string of alphanumeric characters of size size
Useful for passwords, usernames and the like.
134 135 136 137 138 |
# File 'lib/vmbuilder_plugins/std.rb', line 134 def random_string(size=10) s = "" size.times { s << (i = rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr } s end |
#relative_path(from_str, to_str) ⇒ Object
Return a relative path from the destination directory from_str
to the target file/directory to_str
. Used to create relative symbolic link paths.
144 145 146 147 |
# File 'lib/vmbuilder_plugins/std.rb', line 144 def relative_path (from_str, to_str) require 'pathname' Pathname.new(to_str).relative_path_from(Pathname.new(from_str)).to_s end |
#ruby(cmd, options = {}, &block) ⇒ Object
Run a ruby command file on the servers
151 152 153 154 155 156 157 158 159 |
# File 'lib/vmbuilder_plugins/std.rb', line 151 def ruby(cmd, ={}, &block) temp_name = random_string + ".rb" begin put(cmd, temp_name, :mode => 0700) send(run_method, "ruby #{temp_name}", , &block) ensure delete temp_name end end |
#su_delete(path, options = {}) ⇒ Object
Deletes the given file(s) from all servers targetted by the current task, but runs the delete
command according to the current setting of :use_sudo
.
If :recursive => true
is specified, it may be used to remove directories.
182 183 184 185 |
# File 'lib/vmbuilder_plugins/std.rb', line 182 def su_delete(path, ={}) cmd = "rm -%sf #{path}" % ([:recursive] ? "r" : "") send(run_method, cmd, ) end |
#su_put(data, destination, temporary_area = '/tmp', options = {}) ⇒ Object
Upload data
to temporary_area
before installing it in destination
using sudo.
options
are as for put
62 63 64 65 66 67 68 69 |
# File 'lib/vmbuilder_plugins/std.rb', line 62 def su_put(data, destination, temporary_area='/tmp', ={}) temporary_area = File.join(temporary_area,File.basename(destination)) put(data, temporary_area, ) send run_method, <<-CMD sh -c "install -m#{sprintf("%3o",[:mode]||0755)} #{temporary_area} #{destination} && rm -f #{temporary_area}" CMD end |
#unzip(file_pattern, destination, options = {}) ⇒ Object
Copies the file_pattern
, which is assumed to be a tar file of some description (gzipped or plain), and unpacks it into destination
.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vmbuilder_plugins/std.rb', line 74 def unzip(file_pattern, destination, ={}) Dir.glob(file_pattern) do |fname| if File.readable?(fname) then target="/tmp/#{File.basename(fname)}" if MMAP logger.debug "Using Memory Mapped File Upload" fdata=Mmap.new(fname,"r", Mmap::MAP_SHARED, :advice => Mmap::MADV_SEQUENTIAL) else fdata=File.open(fname).read end put(fdata, target, ) send run_method, <<-CMD sh -c "cd #{destination} && zcat -f #{target} | tar xvf - && rm -f #{target}" CMD end end end |