Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions lib/capify-cloud/capistrano.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,24 +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 <INSTANCE NAME>"

desc "Allows ssh to instance by id. cap ssh -s cloud_instance=<INSTANCE NAME>"
task :ssh do
server = variables[:logger].instance_variable_get("@options")[:actions][1]
instance = numeric?(server) ? capify_cloud.desired_instances[server.to_i] : capify_cloud.get_instance_by_name(server)
port = ssh_options[:port] || 22
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
Expand All @@ -49,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 = []
Expand Down Expand Up @@ -99,29 +114,29 @@ def define_role_roles(role, instances)
instances.each do |instance|
define_role(role, instance)
end
end
end
end

def define_role(role, instance)
options = role[:options]
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
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<instance name/id from cloud:status>
```

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
Expand Down