Module: Deprec2
- Defined in:
- lib/deprec/capistrano_extensions.rb
Constant Summary collapse
- DEPREC_TEMPLATES_BASE =
File.join(File.dirname(__FILE__), 'templates')
Instance Method Summary collapse
-
#add_user_to_group(user, group) ⇒ Object
add group to the list of groups this user belongs to.
- #append_to_file_if_missing(filename, value, options = {}) ⇒ Object
- #create_src_dir ⇒ Object
-
#download_src(src_package, src_dir) ⇒ Object
download source package if we don’t already have it.
-
#for_roles(roles) ⇒ Object
Temporarily modify ROLES if HOSTS not set Capistrano’s default behaviour is for HOSTS to override ROLES.
-
#groupadd(group, options = {}) ⇒ Object
create a new group on target system.
-
#ignoring_roles_and_hosts ⇒ Object
Temporarily ignore ROLES and HOSTS.
-
#install_from_src(src_package, src_dir) ⇒ Object
install package from source.
- #invoke_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
-
#mkdir(path, options = {}) ⇒ Object
create directory if it doesn’t already exist set permissions and ownership XXX move mode, path and.
- #overwrite?(full_path, rendered_template) ⇒ Boolean
-
#push_configs(app, files) ⇒ Object
Copy configs to server(s).
-
#render_template(app, options = {}) ⇒ Object
Render template (usually a config file) .
- #render_template_to_file(template_name, destination_file_name, templates_dir = DEPREC_TEMPLATES_BASE) ⇒ Object
-
#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command and ask for input when input_query is seen.
- #set_package_defaults(pkg) ⇒ Object
-
#sudo_stream(command) ⇒ Object
Run a command using sudo and continuously pipe the results back to the console.
-
#sudo_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command using sudo and ask for input when a regular expression is seen.
- #teardown_connections ⇒ Object
-
#unpack_src(src_package, src_dir) ⇒ Object
unpack src and make it writable by the group.
-
#useradd(user, options = {}) ⇒ Object
create new user account on target system.
Instance Method Details
#add_user_to_group(user, group) ⇒ Object
add group to the list of groups this user belongs to
225 226 227 228 |
# File 'lib/deprec/capistrano_extensions.rb', line 225 def add_user_to_group(user, group) invoke_command "groups #{user} | grep ' #{group} ' || sudo /usr/sbin/usermod -G #{group} -a #{user}", :via => run_method end |
#append_to_file_if_missing(filename, value, options = {}) ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/deprec/capistrano_extensions.rb', line 195 def append_to_file_if_missing(filename, value, ={}) # XXX sort out single quotes in 'value' - they'l break command! # XXX if options[:requires_sudo] and :use_sudo then use sudo sudo <<-END sh -c ' grep -F "#{value}" #{filename} > /dev/null 2>&1 || echo "#{value}" >> #{filename} ' END end |
#create_src_dir ⇒ Object
244 245 246 |
# File 'lib/deprec/capistrano_extensions.rb', line 244 def create_src_dir mkdir(src_dir, :mode => 0775, :group => group_src, :via => :sudo) end |
#download_src(src_package, src_dir) ⇒ Object
download source package if we don’t already have it
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/deprec/capistrano_extensions.rb', line 249 def download_src(src_package, src_dir) set_package_defaults(src_package) create_src_dir # check if file exists and if we have an MD5 hash or bytecount to compare # against if so, compare and decide if we need to download again if defined?(src_package[:md5sum]) md5_clause = " && echo '#{src_package[:md5sum]}' | md5sum -c - " end case src_package[:download_method] # when getting source with git when :git # ensure git is installed apt.install( {:base => %w(git)}, :stable) #TODO fix this to test ubuntu version <hardy might need specific git version for full git submodules support package_dir = File.join(src_dir, src_package[:dir]) run "if [ -d #{package_dir} ]; then cd #{package_dir} && #{sudo} git checkout master && #{sudo} git pull && #{sudo} git submodule init && #{sudo} git submodule update; else #{sudo} git clone #{src_package[:url]} #{package_dir} && cd #{package_dir} && #{sudo} git submodule init && #{sudo} git submodule update ; fi" # Checkout the revision wanted if defined if src_package[:version] run "cd #{package_dir} && git branch | grep '#{src_package[:version]}$' && #{sudo} git branch -D '#{src_package[:version]}'; exit 0" run "cd #{package_dir} && #{sudo} git checkout -b #{src_package[:version]} #{src_package[:version]}" end # when getting source with wget when :http # ensure wget is installed apt.install( {:base => %w(wget)}, :stable ) # XXX replace with invoke_command run "cd #{src_dir} && test -f #{src_package[:filename]} #{md5_clause} || #{sudo} wget --quiet --timestamping #{src_package[:url]}" else puts "DOWNLOAD SRC: Download method not recognised. src_package[:download_method]: #{src_package[:download_method]}" end end |
#for_roles(roles) ⇒ Object
Temporarily modify ROLES if HOSTS not set Capistrano’s default behaviour is for HOSTS to override ROLES
9 10 11 12 13 14 |
# File 'lib/deprec/capistrano_extensions.rb', line 9 def for_roles(roles) old_roles = ENV['ROLES'] ENV['ROLES'] = roles.to_s unless ENV['HOSTS'] yield ENV['ROLES'] = old_roles.to_s unless ENV['HOSTS'] end |
#groupadd(group, options = {}) ⇒ Object
create a new group on target system
218 219 220 221 222 |
# File 'lib/deprec/capistrano_extensions.rb', line 218 def groupadd(group, ={}) via = .delete(:via) || run_method # XXX I don't like specifying the path to groupadd - need to sort out paths before long invoke_command "grep '#{group}:' /etc/group || sudo /usr/sbin/groupadd #{group}", :via => via end |
#ignoring_roles_and_hosts ⇒ Object
Temporarily ignore ROLES and HOSTS
17 18 19 20 21 22 23 24 25 |
# File 'lib/deprec/capistrano_extensions.rb', line 17 def ignoring_roles_and_hosts old_roles = ENV['ROLES'] old_hosts = ENV['HOSTS'] ENV['ROLES'] = nil ENV['HOSTS'] = nil yield ENV['ROLES'] = old_roles ENV['HOSTS'] = old_hosts end |
#install_from_src(src_package, src_dir) ⇒ Object
install package from source
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/deprec/capistrano_extensions.rb', line 321 def install_from_src(src_package, src_dir) package_dir = File.join(src_dir, src_package[:dir]) unpack_src(src_package, src_dir) apt.install( {:base => %w(gcc gcc-c++ make)}, :stable ) # XXX replace with invoke_command sudo <<-SUDO sh -c ' cd #{package_dir}; #{src_package[:configure]} #{src_package[:make]} #{src_package[:install]} #{src_package[:post_install]} ' SUDO end |
#invoke_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
364 365 366 |
# File 'lib/deprec/capistrano_extensions.rb', line 364 def invoke_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(run_method, shell_command, input_query, response) end |
#mkdir(path, options = {}) ⇒ Object
create directory if it doesn’t already exist set permissions and ownership XXX move mode, path and
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/deprec/capistrano_extensions.rb', line 233 def mkdir(path, ={}) via = .delete(:via) || :run # XXX need to make sudo commands wrap the whole command (sh -c ?) # XXX removed the extra 'sudo' from after the '||' - need something else invoke_command "test -d #{path} || #{sudo if via == :sudo} mkdir -p #{path}" invoke_command "chmod #{sprintf("%3o",[:mode]||0755)} #{path}", :via => via if [:mode] invoke_command "chown -R #{[:owner]} #{path}", :via => via if [:owner] groupadd([:group], :via => via) if [:group] invoke_command "chgrp -R #{[:group]} #{path}", :via => via if [:group] end |
#overwrite?(full_path, rendered_template) ⇒ Boolean
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/deprec/capistrano_extensions.rb', line 103 def overwrite?(full_path, rendered_template) if defined?(overwrite_all) if overwrite_all == true return true else return false end end # XXX add :always and :never later - not sure how to set persistent value from here # response = Capistrano::CLI.ui.ask "File exists. Overwrite? ([y]es, [n]o, [a]lways, n[e]ver)" do |q| puts response = Capistrano::CLI.ui.ask "File exists (#{full_path}). Overwrite? ([y]es, [n]o, [d]iff)" do |q| q.default = 'n' end case response when 'y' return true when 'n' return false when 'd' require 'tempfile' tf = Tempfile.new("deprec_diff") tf.puts(rendered_template) tf.close puts puts "Running diff -u current_file new_file_if_you_overwrite" puts system "diff -u #{full_path} #{tf.path} | less" puts overwrite?(full_path, rendered_template) # XXX add :always and :never later - not sure how to set persistent value from here # when 'a' # set :overwrite_all, true # when 'e' # set :overwrite_all, false end end |
#push_configs(app, files) ⇒ Object
Copy configs to server(s). Note there is no :pull task. No changes should be made to configs on the servers so why would you need to pull them back?
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/deprec/capistrano_extensions.rb', line 159 def push_configs(app, files) app = app.to_s files.each do |file| # If the file path is relative we will prepend a path to this projects # own config directory for this service. if file[:path][0,1] != '/' full_remote_path = File.join(deploy_to, app, file[:path]) else full_remote_path = file[:path] end full_local_path = File.join('config', app, file[:path]) sudo "test -d #{File.dirname(full_remote_path)} || sudo mkdir -p #{File.dirname(full_remote_path)}" # # XXX work this in to check for per-host variants of config files # # if any variants of this file exist for this host (they have -hostname at end) # servers = find_servers_for_task(current_task) # servers.each do |server| # puts server(..., :hosts => server) # XXX find a way to restrict su_put to one host # end # else # # just send them the normal way, it's quicker in parallel std.su_put File.read(full_local_path), full_remote_path, '/tmp/', :mode=>file[:mode] # end # sudo "chown #{file[:owner]} #{full_remote_path}" end end |
#render_template(app, options = {}) ⇒ Object
Render template (usually a config file)
Usually we render it to a file on the local filesystem. This way, we keep a copy of the config file under source control. We can make manual changes if required and push to new hosts.
If the options hash contains :path then it’s written to that path. If it contains :remote => true, the file will instead be written to remote targets If options and options are missing, it just returns the rendered template as a string (good for debugging).
XXX I would like to get rid of :render_template_to_file
XXX Perhaps pass an option to this function to write to remote
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/deprec/capistrano_extensions.rb', line 43 def render_template(app, ={}) template = [:template] path = [:path] || nil remote = [:remote] || false mode = [:mode] || 0755 owner = [:owner] || nil # replace this with a check for the file if ! template puts "render_template() requires a value for the template!" return false end # If local copies of deprec templates exist they will be used # If you don't specify the location with the local_template_dir option # it defaults to config/templates. # e.g. config/templates/nginx/nginx.conf.erb local_template = File.join(local_template_dir, app.to_s, template) if File.exists?(local_template) puts puts "Using local template (#{local_template})" template = ERB.new(IO.read(local_template), nil, '-') else template = ERB.new(IO.read(File.join(DEPREC_TEMPLATES_BASE, app.to_s, template)), nil, '-') end rendered_template = template.result(binding) if remote # render to remote machine puts 'You need to specify a path to render the template to!' unless path exit unless path sudo "test -d #{File.dirname(path)} || sudo mkdir -p #{File.dirname(path)}" std.su_put rendered_template, path, '/tmp/', :mode => mode sudo "chown #{owner} #{path}" if defined?(owner) elsif path # render to local file full_path = File.join('config', app.to_s, path) path_dir = File.dirname(full_path) if File.exists?(full_path) if IO.read(full_path) == rendered_template puts "[skip] File exists and is identical (#{full_path})." return false elsif overwrite?(full_path, rendered_template) File.delete(full_path) else puts "[skip] Not overwriting #{full_path}" return false end end FileUtils.mkdir_p "#{path_dir}" if ! File.directory?(path_dir) # added line above to make windows compatible # system "mkdir -p #{path_dir}" if ! File.directory?(path_dir) File.open(full_path, 'w'){|f| f.write rendered_template } puts "[done] #{full_path} written" else # render to string return rendered_template end end |
#render_template_to_file(template_name, destination_file_name, templates_dir = DEPREC_TEMPLATES_BASE) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/deprec/capistrano_extensions.rb', line 145 def render_template_to_file(template_name, destination_file_name, templates_dir = DEPREC_TEMPLATES_BASE) template_name += '.conf' if File.extname(template_name) == '' # XXX this to be removed file = File.join(templates_dir, template_name) buffer = render :template => File.read(file) temporary_location = "/tmp/#{template_name}" put buffer, temporary_location sudo "cp #{temporary_location} #{destination_file_name}" delete temporary_location end |
#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command and ask for input when input_query is seen. Sends the response back to the server.
input_query
is a regular expression that defaults to /^Password/.
Can be used where run
would otherwise be used.
run_with_input 'ssh-keygen ...', /^Are you sure you want to overwrite\?/
348 349 350 |
# File 'lib/deprec/capistrano_extensions.rb', line 348 def run_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:run, shell_command, input_query, response) end |
#set_package_defaults(pkg) ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/deprec/capistrano_extensions.rb', line 310 def set_package_defaults(pkg) pkg[:filename] ||= File.basename(pkg[:url]) pkg[:dir] ||= pkg[:filename].sub(/(\.tgz|\.tar\.gz)/,'') pkg[:download_method] ||= :http pkg[:unpack] ||= "tar zxf #{pkg[:filename]};" pkg[:configure] ||= './configure ;' pkg[:make] ||= 'make;' pkg[:install] ||= 'make install;' end |
#sudo_stream(command) ⇒ Object
Run a command using sudo and continuously pipe the results back to the console.
Similar to the built-in stream
, but for privileged users.
373 374 375 376 377 378 379 380 381 |
# File 'lib/deprec/capistrano_extensions.rb', line 373 def sudo_stream(command) sudo(command) do |ch, stream, out| puts out if stream == :out if stream == :err puts "[err : #{ch[:host]}] #{out}" break end end end |
#sudo_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command using sudo and ask for input when a regular expression is seen. Sends the response back to the server.
See also run_with_input
input_query
is a regular expression
360 361 362 |
# File 'lib/deprec/capistrano_extensions.rb', line 360 def sudo_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:sudo, shell_command, input_query, response) end |
#teardown_connections ⇒ Object
188 189 190 191 192 193 |
# File 'lib/deprec/capistrano_extensions.rb', line 188 def teardown_connections sessions.keys.each do |server| sessions[server].close sessions.delete(server) end end |
#unpack_src(src_package, src_dir) ⇒ Object
unpack src and make it writable by the group
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/deprec/capistrano_extensions.rb', line 282 def unpack_src(src_package, src_dir) set_package_defaults(src_package) package_dir = File.join(src_dir, src_package[:dir]) case src_package[:download_method] # when unpacking git sources - nothing to do when :git puts "UNPACK SRC: nothing to do for git installs" when :http sudo <<-EOF bash -c ' cd #{src_dir}; test -d #{package_dir}.old && rm -fr #{package_dir}.old; test -d #{package_dir} && mv #{package_dir} #{package_dir}.old; #{src_package[:unpack]} ' EOF else puts "UNPACK SRC: Download method not recognised. src_package[:download_method]: #{src_package[:download_method]} " end sudo <<-EOF bash -c ' cd #{src_dir}; chgrp -R #{group} #{package_dir}; chmod -R g+w #{package_dir}; ' EOF end |
#useradd(user, options = {}) ⇒ Object
create new user account on target system
207 208 209 210 211 212 213 214 215 |
# File 'lib/deprec/capistrano_extensions.rb', line 207 def useradd(user, ={}) [:shell] ||= '/bin/bash' # new accounts on ubuntu 6.06.1 have been getting /bin/sh switches = '' switches += " -s #{[:shell]} " if [:shell] switches += ' -M ' if [:homedir] == false switches += " -g #{[:group]} " unless [:group].nil? invoke_command "grep '^#{user}:' /etc/passwd || sudo /usr/sbin/useradd #{switches} #{user}", :via => run_method end |