Class: Ec2ctl::CLI
- Inherits:
-
Thor
- Object
- Thor
- Ec2ctl::CLI
- Defined in:
- lib/ec2ctl.rb
Instance Method Summary collapse
- #add(name, elb_name) ⇒ Object
- #lb(elb_name) ⇒ Object
- #list(pattern = "") ⇒ Object
- #remove(name, elb_name) ⇒ Object
- #ssh(name, command) ⇒ Object
- #start(name) ⇒ Object
- #status(name) ⇒ Object
- #stop(name) ⇒ Object
Instance Method Details
#add(name, elb_name) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/ec2ctl.rb', line 118 def add name, elb_name instance = find_instance_by_name name load_balancer = find_load_balancer_by_name elb_name abort "Instance is already registered to the load balancer!" if load_balancer.instances.include? instance puts "Adding instance to the load balancer..." load_balancer.instances.add instance end |
#lb(elb_name) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ec2ctl.rb', line 140 def lb elb_name load_balancer = find_load_balancer_by_name elb_name instances = load_balancer.instances rows = [] load_balancer.instances.health.each do |instance_health| next unless instances.any? {|instance| instance.id == instance_health[:instance].id} rows << [ instance_health[:instance].["Name"], instance_health[:instance].id, instance_health[:description], instance_health[:state], ] end puts Terminal::Table.new headings: ["Name", "Instance ID", "Description", "State"], rows: rows end |
#list(pattern = "") ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ec2ctl.rb', line 31 def list pattern = "" rows = [] ec2.instances.each do |instance| name = instance.["Name"].to_s next if !pattern.empty? and !name.match pattern rows << [ instance.id, name, instance.instance_type, instance.private_ip_address, instance.public_ip_address, status_colorize(instance.status), ] end col = case [:sort] when /ID/i then 0 when /Name/i then 1 when /Type/i then 2 when /private/i then 3 when /public/i then 4 when /status/i then 5 end rows.sort_by! {|row| row[col].to_s} if col rows = rows[0, [:limit]] if [:limit] puts Terminal::Table.new headings: ["ID", "Name", "Type", "Private IP", "Public IP", "Status"], rows: rows end |
#remove(name, elb_name) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/ec2ctl.rb', line 129 def remove name, elb_name instance = find_instance_by_name name load_balancer = find_load_balancer_by_name elb_name abort "Instance is not registered to the load balancer!" unless load_balancer.instances.include? instance puts "Removing instance from the load balancer..." load_balancer.instances.remove instance end |
#ssh(name, command) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ec2ctl.rb', line 103 def ssh name, command instance = find_instance_by_name name ip_address = [:via_public_ip] ? instance.public_ip_address : instance.private_ip_address = { keys: [[:identity_file]], passphrase: [:passphrase], } Net::SSH.start ip_address, [:user], do |ssh| puts ssh.exec!(command) end end |
#start(name) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/ec2ctl.rb', line 80 def start name instance = find_instance_by_name name abort "Instance is not stopped!" unless instance.status == :stopped instance.start sleep 2 puts "Instance is now #{status_colorize instance.status}." end |
#status(name) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ec2ctl.rb', line 64 def status name instance = find_instance_by_name name = instance..to_h puts <<-EOS.unindent * Name: #{["Name"]} * Status: #{status_colorize instance.status} * Instance Type: #{instance.instance_type} * Private IP: #{instance.private_ip_address} * Public IP: #{instance.public_ip_address} * Availability Zone: #{instance.availability_zone} * Other Tags: #{.reject {|k, v| k == "Name"}.inspect} EOS end |
#stop(name) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/ec2ctl.rb', line 89 def stop name instance = find_instance_by_name name abort "Instance is not running!" unless instance.status == :running instance.stop sleep 2 puts "Instance is now #{status_colorize instance.status}." end |