Class: Ec2InstanceManager

Inherits:
Object
  • Object
show all
Includes:
Launch, Output, Status
Defined in:
lib/ec2-instance-manager/ec2_instance_manager.rb

Constant Summary collapse

VERSION =
'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Status

#display_addresses, #display_ami_ids, #display_instances, #display_volumes, #get_instance_state, #status_info

Methods included from Launch

#launch, #launch_ami, #start_launch_plan, #terminate

Methods included from Output

#cancel_message, #green, #output_running_state, #red, #white, #yellow

Constructor Details

#initialize(arguments, stdin) ⇒ Ec2InstanceManager

Returns a new instance of Ec2InstanceManager.



11
12
13
14
15
16
17
18
19
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
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 11

def initialize(arguments, stdin)
  @arguments = arguments
  @stdin = stdin
  options = {}
  
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: ec2-instance-manager #{VERSION} [options]"

    options[:status] = false
    opts.on( '-s', '--status', 'Status only' ) do
      options[:status] = true
    end
    
    options[:terminate] = false
    opts.on( '-t', '--terminate-all', 'Terminates all instances running under a customer config key' ) do
      options[:terminate] = true
    end

    options[:start_launch_plan] = false
    opts.on( '-l', '--start-launch-plan', 'Starts a launch plan under a customer config key' ) do
      options[:start_launch_plan] = true
    end
    
    options[:group] = nil
    opts.on( '-g', '--group LAUNCH_GROUP_NAME', 'Starts a launch plan group under a customer config key' ) do |group|
      options[:group] = group
    end

    options[:config] = nil
    opts.on( '-c', '--config CONFIG_KEY', 'Sets the customer config key' ) do |key|
      options[:config] = key
    end

    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end

  optparse.parse!
  @options = options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 9

def config
  @config
end

#customer_keyObject (readonly)

Returns the value of attribute customer_key.



9
10
11
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 9

def customer_key
  @customer_key
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 9

def options
  @options
end

Instance Method Details

#ec2Object



69
70
71
72
73
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 69

def ec2
  @ec2 = AWS::EC2::Base.new(:access_key_id => config[@customer_key]['amazon_access_key_id'],
    :secret_access_key => config[@customer_key]['amazon_secret_access_key'],
    :server => config[@customer_key]['ec2_server_region'])
end

#read_configObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 55

def read_config
  if File.exists?("config.yml")
    puts "Using config in this directory"
    @config = YAML.load(File.read("config.yml"))
  else
    begin
      puts "Using config in your home directory"
      @config = YAML.load(File.read("#{ENV['HOME']}/.ec2_instance_manager_config.yml"))
    rescue Errno::ENOENT
      raise "config.yml expected in current directory or ~/.ec2_instance_manager_config.yml"
    end
  end
end

#runObject



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
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 75

def run
  puts white("EC2 Instance Manager #{VERSION}")
  puts
  unless options[:config]
    puts "Which customer config do you want to use? (#{config.keys.join(", ")})"
    @customer_key = gets
    @customer_key = @customer_key.rstrip.lstrip
    @customer_key = 'default' if @customer_key.empty?
  else
    @customer_key = options[:config]
  end

  puts "Configuration Key: #{@customer_key}"
  puts "AMAZON_ACCESS_KEY_ID: #{config[@customer_key]['amazon_access_key_id']}"
  puts "KEY: #{config[@customer_key]['key']}"
  puts "ZONE: #{config[@customer_key]['availability_zone']}"
  puts
  
  status_info

  if options[:terminate]
    terminate
  elsif options[:start_launch_plan]
    start_launch_plan
  else
    launch unless options[:status]
  end
end