Top Level Namespace
Defined Under Namespace
Modules: GeorgiaRecipes
Instance Method Summary collapse
- #are_you_sure? ⇒ Boolean
- #ask(question) ⇒ Object
-
#close_sessions ⇒ Object
disconnect all sessions.
- #git_pull_or_clone(repo_name, repo_url) ⇒ Object
-
#handle_command_with_input(local_run_method, shell_command, input_query, response = nil) ⇒ Object
Does the actual capturing of the input and streaming of the output.
-
#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command and ask for input when input_query is seen.
- #set_default(name, *args, &block) ⇒ Object
-
#set_user(user, pass) ⇒ Object
change the capistrano user.
- #template(from, to) ⇒ Object
- #timestamp ⇒ Object
-
#with_user(new_user, new_pass, &block) ⇒ Object
run a command on the server with a different user.
Instance Method Details
#are_you_sure? ⇒ Boolean
14 15 16 |
# File 'lib/georgia_recipes/helper_methods.rb', line 14 def are_you_sure? ask("Holy Moly! Are you sure you want to do that? (type 'yes' if so)") == 'yes' end |
#ask(question) ⇒ Object
18 19 20 |
# File 'lib/georgia_recipes/helper_methods.rb', line 18 def ask question Capistrano::CLI.ui.ask question end |
#close_sessions ⇒ Object
disconnect all sessions
83 84 85 86 |
# File 'lib/georgia_recipes/helper_methods.rb', line 83 def close_sessions sessions.values.each { |session| session.close } sessions.clear end |
#git_pull_or_clone(repo_name, repo_url) ⇒ Object
58 59 60 |
# File 'lib/georgia_recipes/helper_methods.rb', line 58 def git_pull_or_clone repo_name, repo_url run "bash -c 'if cd #{repo_name}; then git pull origin master; else git clone #{repo_url} #{repo_name}; fi'" end |
#handle_command_with_input(local_run_method, shell_command, input_query, response = nil) ⇒ Object
Does the actual capturing of the input and streaming of the output.
local_run_method: run or sudo shell_command: The command to run input_query: A regular expression matching a request for input: /^Please enter your password/
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/georgia_recipes/helper_methods.rb', line 41 def handle_command_with_input(local_run_method, shell_command, input_query, response=nil) send(local_run_method, shell_command, {:pty => true}) do |channel, stream, data| if data =~ input_query if response logger.info "#{data} #{"*"*(rand(10)+5)}", channel[:host] channel.send_data "#{response}\n" else logger.info data, channel[:host] response = ::Capistrano::CLI.password_prompt "#{data}" channel.send_data "#{response}\n" end else logger.info data, channel[:host] end end 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?/
31 32 33 |
# File 'lib/georgia_recipes/helper_methods.rb', line 31 def run_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:run, shell_command, input_query, response) end |
#set_default(name, *args, &block) ⇒ Object
10 11 12 |
# File 'lib/georgia_recipes/helper_methods.rb', line 10 def set_default(name, *args, &block) set(name, *args, &block) unless exists?(name) end |
#set_user(user, pass) ⇒ Object
change the capistrano user
76 77 78 79 80 |
# File 'lib/georgia_recipes/helper_methods.rb', line 76 def set_user(user, pass) set :user, user set :password, pass close_sessions end |
#template(from, to) ⇒ Object
5 6 7 8 |
# File 'lib/georgia_recipes/helper_methods.rb', line 5 def template(from, to) erb = File.read(File.("../templates/#{from}", __FILE__)) put ERB.new(erb).result(binding), to end |
#timestamp ⇒ Object
1 2 3 |
# File 'lib/georgia_recipes/helper_methods.rb', line 1 def @timestamp ||= Time.now.strftime('%Y%m%d%H%M%S') end |
#with_user(new_user, new_pass, &block) ⇒ Object
run a command on the server with a different user
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/georgia_recipes/helper_methods.rb', line 63 def with_user(new_user, new_pass, &block) old_user, old_pass = user, password set_user(new_user, new_pass) begin yield rescue Exception => e set_user(old_user, old_pass) raise e end set_user(old_user, old_pass) end |