Class: Opsicle::SSH
- Inherits:
-
Object
- Object
- Opsicle::SSH
- Defined in:
- lib/opsicle/commands/ssh.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #execute(options = {}) ⇒ Object
-
#initialize(environment) ⇒ SSH
constructor
A new instance of SSH.
- #instance_info(instance) ⇒ Object
- #instances ⇒ Object
- #public_ips ⇒ Object
- #ssh_command(instance, options = {}) ⇒ Object
- #ssh_username ⇒ Object
Constructor Details
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/opsicle/commands/ssh.rb', line 5 def client @client end |
Instance Method Details
#execute(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/opsicle/commands/ssh.rb', line 13 def execute(={}) if instances.length == 1 choice = 1 else Output.say "Choose an Opsworks instance:" instances.each_with_index do |instance, index| Output.say "#{index+1}) #{instance[:hostname]} #{instance_info(instance)}" end choice = Output.ask("? ", Integer) { |q| q.in = 1..instances.length } end command = ssh_command(instances[choice-1], ) Output.say_verbose "Executing shell command: #{command}" system(command) end |
#instance_info(instance) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/opsicle/commands/ssh.rb', line 59 def instance_info(instance) infos = [] infos << instance[:layer_ids].map{ |layer_id| @stack.layer_name(layer_id) } if instance[:layer_ids] infos << "EIP" if instance[:elastic_ip] "(#{infos.join(', ')})" end |
#instances ⇒ Object
31 32 33 34 35 36 |
# File 'lib/opsicle/commands/ssh.rb', line 31 def instances @instances ||= client.api_call(:describe_instances, { stack_id: client.config.opsworks_config[:stack_id] }) .data[:instances] .select { |instance| instance[:status].to_s == 'online'} .sort { |a,b| a[:hostname] <=> b[:hostname] } end |
#public_ips ⇒ Object
38 39 40 |
# File 'lib/opsicle/commands/ssh.rb', line 38 def public_ips instances.map{|instance| instance[:elastic_ip] || instance[:public_ip] }.compact end |
#ssh_command(instance, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/opsicle/commands/ssh.rb', line 46 def ssh_command(instance, ={}) ssh_command = " \"#{[:"ssh-cmd"].gsub(/'/){ %q(\') }}\"" if [:"ssh-cmd"] #escape single quotes = [:"ssh-opts"] ? "#{[:"ssh-opts"]} " : "" if instance_ip = instance[:elastic_ip] || instance[:public_ip] ssh_string = "#{ssh_username}@#{instance_ip}" else ssh_string = "#{ssh_username}@#{public_ips.sample} ssh #{instance[:private_ip]}" .concat('-A -t ') end "ssh #{}#{ssh_string}#{ssh_command}" end |
#ssh_username ⇒ Object
42 43 44 |
# File 'lib/opsicle/commands/ssh.rb', line 42 def ssh_username @user_profile.ssh_username end |