Class: EC2Ctl::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/ec2ctl/cli.rb

Constant Summary collapse

OptionError =
Class.new RuntimeError
OPTION_FILE_NAME =
".ec2ctlrc".freeze
DEFAULT_OPTIONS =
{
  :"ec2 list" => {
    attributes: %w(instance_id tag:Name instance_type public_dns_name state.name ssm:ping_status),
  },
}

Instance Method Summary collapse

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ec2ctl/cli.rb', line 20

def run
  program :name,        self.class.to_s
  program :version,     VERSION
  program :description, "A small command line tool for managing EC2/ELB."

  global_option "-V", "--verbose",                                 "Debug output."
  global_option "-P", "--pretty",                                  "Pretty JSON output."
  global_option "-o", "--output #{EC2Ctl::Logger::VALID_FORMATS}", "Log output format."
  global_option "-p", "--profile PROFILE_NAME",                    "AWS profile name."
  global_option "-r", "--region REGION_NAME",                      "AWS region name."

  default_command :"ec2 list"

  command :"ec2 list" do |c|
    c.syntax      = "ec2ctl ec2 list"
    c.description = "List EC2 instances."

    ec2_options c

    c.option "-a", "--attributes KEY1,KEY2...",     Array,  "(Optional) The instance attribute keys to display."
    c.option "-C", "--count",                               "(Optional) Display instance attribute stats."
    c.option       "--platform-type Linux|Windows", String, "(Optional) Platform type: `Linux` or `Windows`. Default is `Linux`."
    c.option "-S", "--sort KEY",                    String, "(Optional) Sort instances by this attribute."
    c.option       "--private-key-file PATH",       String, "(Optional) Path to private key file."

    c.action do |args, options|
      invoke c, options do
        @client.ec2_list
      end
    end
  end

  command :"ec2 execute" do |c|
    c.syntax      = "ec2ctl ec2 execute"
    c.description = "Execute commands in the EC2 instances."

    ec2_options c
    ssm_options c

    c.action do |args, options|
      invoke c, options do
        mandatory options, :commands
        @client.ec2_execute
      end
    end
  end

  command :"elb list" do |c|
    c.syntax      = "ec2ctl elb list"
    c.description = "List load balancers."

    c.action do |args, options|
      invoke c, options do
        @client.elb_list
      end
    end
  end

  command :"elb status" do |c|
    c.syntax      = "ec2ctl elb status"
    c.description = "Show the load balancer's status."

    c.option "-b", "--load-balancer-name VALUE", String, "The name of the load balancer."

    c.action do |args, options|
      invoke c, options do
        mandatory options, :load_balancer_name
        @client.elb_status
      end
    end
  end

  command :"elb attach" do |c|
    c.syntax      = "ec2ctl elb attach"
    c.description = "Attach the instances to the load balancer."

    c.option "-b", "--load-balancer-name VALUE",     String, "The name of the load balancer."
    c.option "-i", "--instance-ids STRING1,STRING2", Array,   "(Optional) The IDs of the instances to attach."

    c.action do |args, options|
      invoke c, options do
        mandatory options, :load_balancer_name, :instance_ids
        @client.elb_attach
      end
    end
  end

  command :"elb detach" do |c|
    c.syntax      = "ec2ctl elb detach"
    c.description = "Detach the instances from the load balancer."

    c.option "-b", "--load-balancer-name VALUE",     String, "The name of the load balancer."
    c.option "-i", "--instance-ids STRING1,STRING2", Array,   "(Optional) The IDs of the instances to detach."

    c.action do |args, options|
      invoke c, options do
        mandatory options, :load_balancer_name, :instance_ids
        @client.elb_detach
      end
    end
  end

  command :"elb execute" do |c|
    c.syntax      = "ec2ctl elb execute"
    c.description = "Execute commands on instance(s) registered to a load balancer."

    elb_execute_options c

    c.action do |args, options|
      invoke c, options do
        mandatory options, :load_balancer_name, :commands
        @client.elb_execute
      end
    end
  end

  command :"elb graceful" do |c|
    c.syntax      = "ec2ctl elb graceful"
    c.description = "Sequencially deregister instance(s) from load balancer, execute commands, register it back to load balancer and wait until it's in `InService` state."

    elb_execute_options c

    c.action do |args, options|
      invoke c, options do
        mandatory options, :load_balancer_name, :commands
        @client.elb_graceful
      end
    end
  end

  run!
end