From 2b2039759fa46f7a03c6e89e1161e8fde1d304b0 Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Wed, 25 Jul 2012 17:12:57 +0100 Subject: [PATCH 1/5] Use named param for cloud:ssh to allow task to work with multistage --- lib/capify-cloud/capistrano.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/capify-cloud/capistrano.rb b/lib/capify-cloud/capistrano.rb index ce38878..94a712e 100644 --- a/lib/capify-cloud/capistrano.rb +++ b/lib/capify-cloud/capistrano.rb @@ -22,14 +22,19 @@ def capify_cloud puts capify_cloud.server_names.sort end - desc "Allows ssh to instance by id. cap ssh " + desc "Allows ssh to instance by id. cap ssh -s cloud_instance=" task :ssh do - server = variables[:logger].instance_variable_get("@options")[:actions][1] + server = fetch(:cloud_instance, false) instance = numeric?(server) ? capify_cloud.desired_instances[server.to_i] : capify_cloud.get_instance_by_name(server) - port = ssh_options[:port] || 22 - command = "ssh -p #{port} #{user}@#{instance.contact_point}" - puts "Running `#{command}`" - exec(command) + if !instance.nil? + port = ssh_options[:port] || 22 + command = "ssh -p #{port} #{user}@#{instance.contact_point}" + puts "Running `#{command}`" + exec(command) + else + puts "Unable to find instance" + puts "Usage:\n cap cloud:ssh -s cloud_instance=" + end end end From 7eafccd5b0139b4bc287cfb8ccca472ce2a6612f Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Wed, 25 Jul 2012 17:16:01 +0100 Subject: [PATCH 2/5] Update doc to match cloud:ssh task argument format --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 7ad6c74..173a9cc 100644 --- a/readme.md +++ b/readme.md @@ -82,11 +82,11 @@ will list the currently running servers and their associated details Running ```ruby -cap cloud:ssh # +cap cloud:ssh -s cloud_instance= ``` will launch ssh using the user and port specified in your configuration. -The # argument is the index of the server to ssh into. Use the 'cloud:status' +The cloud_instance argument is the index of the server to ssh into. Use the 'cloud:status' command to see the list of servers with their indices. More options From 274d6f1b4581e0faf580b2b79919cf30225765c7 Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Wed, 25 Jul 2012 17:17:29 +0100 Subject: [PATCH 3/5] Fix typo in doc --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 173a9cc..1e85981 100644 --- a/readme.md +++ b/readme.md @@ -82,7 +82,7 @@ will list the currently running servers and their associated details Running ```ruby -cap cloud:ssh -s cloud_instance= +cap cloud:ssh -s cloud_instance= ``` will launch ssh using the user and port specified in your configuration. From 48934d6a191770ec73159d61388a732fee70a37e Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Wed, 25 Jul 2012 17:33:46 +0100 Subject: [PATCH 4/5] Tidy formatting Replace tabs with spaces --- lib/capify-cloud/capistrano.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/capify-cloud/capistrano.rb b/lib/capify-cloud/capistrano.rb index 94a712e..fff4154 100644 --- a/lib/capify-cloud/capistrano.rb +++ b/lib/capify-cloud/capistrano.rb @@ -26,15 +26,15 @@ def capify_cloud task :ssh do server = fetch(:cloud_instance, false) instance = numeric?(server) ? capify_cloud.desired_instances[server.to_i] : capify_cloud.get_instance_by_name(server) - if !instance.nil? - port = ssh_options[:port] || 22 - command = "ssh -p #{port} #{user}@#{instance.contact_point}" - puts "Running `#{command}`" - exec(command) - else - puts "Unable to find instance" - puts "Usage:\n cap cloud:ssh -s cloud_instance=" - end + if !instance.nil? + port = ssh_options[:port] || 22 + command = "ssh -p #{port} #{user}@#{instance.contact_point}" + puts "Running `#{command}`" + exec(command) + else + puts "Unable to find instance" + puts "Usage:\n cap cloud:ssh -s cloud_instance=" + end end end From 10f874303d71b21d157dd9c217ad455e884749b7 Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Wed, 3 Oct 2012 11:54:16 +0100 Subject: [PATCH 5/5] Restore original functionality as fallback Refactor to use guard clauses rather than nested conditionals --- lib/capify-cloud/capistrano.rb | 68 +++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/lib/capify-cloud/capistrano.rb b/lib/capify-cloud/capistrano.rb index fff4154..837a5cd 100644 --- a/lib/capify-cloud/capistrano.rb +++ b/lib/capify-cloud/capistrano.rb @@ -1,13 +1,13 @@ require File.join(File.dirname(__FILE__), '../capify-cloud') require 'colored' -Capistrano::Configuration.instance(:must_exist).load do +Capistrano::Configuration.instance(:must_exist).load do def capify_cloud @capify_cloud ||= CapifyCloud.new(fetch(:cloud_config, 'config/cloud.yml')) end namespace :cloud do - + desc "Prints out all cloud instances. index, name, instance_id, size, DNS/IP, region, tags" task :status do capify_cloud.display_instances @@ -21,29 +21,39 @@ def capify_cloud task :server_names do puts capify_cloud.server_names.sort end - + desc "Allows ssh to instance by id. cap ssh -s cloud_instance=" task :ssh do - server = fetch(:cloud_instance, false) - instance = numeric?(server) ? capify_cloud.desired_instances[server.to_i] : capify_cloud.get_instance_by_name(server) - if !instance.nil? - port = ssh_options[:port] || 22 - command = "ssh -p #{port} #{user}@#{instance.contact_point}" - puts "Running `#{command}`" - exec(command) - else - puts "Unable to find instance" - puts "Usage:\n cap cloud:ssh -s cloud_instance=" - end + instance = get_instance_from_named_arg + instance = get_instance_from_args if instance.nil? + raise "Unable to find instance" if instance.nil? + port = ssh_options[:port] || 22 + command = "ssh -p #{port} #{user}@#{instance.contact_point}" + puts "Running `#{command}`" + exec(command) end end - + + def get_instance_from_named_arg + server = fetch(:cloud_instance, false) + server_to_instance(server) + end + + def get_instance_from_args + server = variables[:logger].instance_variable_get("@options")[:actions][1] + server_to_instance(server) + end + + def server_to_instance(server) + numeric?(server) ? capify_cloud.desired_instances[server.to_i] : capify_cloud.get_instance_by_name(server) + end + def cloud_roles(*roles) server_name = variables[:logger].instance_variable_get("@options")[:actions].first unless variables[:logger].instance_variable_get("@options")[:actions][1].nil? - + if !server_name.nil? named_instance = capify_cloud.get_instance_by_name(server_name) - + task named_instance.name.to_sym do remove_default_roles server_address = named_instance.contact_point @@ -54,26 +64,26 @@ def cloud_roles(*roles) end roles.each {|role| cloud_role(role)} end - + def cloud_role(role_name_or_hash) role = role_name_or_hash.is_a?(Hash) ? role_name_or_hash : {:name => role_name_or_hash,:options => {}} @roles[role[:name]] - + instances = capify_cloud.get_instances_by_role(role[:name]) if role[:options].delete(:default) instances.each do |instance| define_role(role, instance) end - end + end regions = capify_cloud.determine_regions regions.each do |region| define_regions(region, role) end unless regions.nil? define_role_roles(role, instances) - define_instance_roles(role, instances) + define_instance_roles(role, instances) - end + end def define_regions(region, role) instances = [] @@ -104,7 +114,7 @@ def define_role_roles(role, instances) instances.each do |instance| define_role(role, instance) end - end + end end def define_role(role, instance) @@ -112,21 +122,21 @@ def define_role(role, instance) new_options = {} options.each {|key, value| new_options[key] = true if value.to_s == instance.name} instance.tags["Options"].split(%r{,\s*}).each { |option| new_options[option.to_sym] = true} rescue false - + if new_options - role role[:name].to_sym, instance.contact_point, new_options + role role[:name].to_sym, instance.contact_point, new_options else role role[:name].to_sym, instance.contact_point end end - + def numeric?(object) true if Float(object) rescue false end - - def remove_default_roles + + def remove_default_roles roles.reject! { true } end - + end