Class: Ec2InstanceManager

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

Constant Summary collapse

VERSION =
'0.1.2'

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

Constructor Details

#initialize(arguments, stdin) ⇒ Ec2InstanceManager

Returns a new instance of Ec2InstanceManager.



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

def initialize(arguments, stdin)
  @arguments = arguments
  @stdin = stdin
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 7

def config
  @config
end

#customer_keyObject (readonly)

Returns the value of attribute customer_key.



7
8
9
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 7

def customer_key
  @customer_key
end

Instance Method Details

#ec2Object



27
28
29
30
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 27

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'])
end

#read_configObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ec2-instance-manager/ec2_instance_manager.rb', line 15

def read_config
  if File.exists?("config.yml")
    @config = YAML.load(File.read("config.yml"))
  else
    begin
      @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



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

def run
  puts "EC2 Instance Manager"
  puts
  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?

  puts "Configuration: #{@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

  puts
  puts "Which AMI Id do you want to launch?"
  ami_id = gets
  ami_id = ami_id.lstrip.rstrip

  result = ec2.run_instances(
    :image_id => ami_id,
    :instance_type => config[@customer_key]['instance_type'],
    :key_name => config[@customer_key]['key'],
    :availability_zone => config[@customer_key]['availability_zone']
  )

  instance_id = result.instancesSet.item[0].instanceId
  puts "=> Starting Instance Id: #{instance_id}"
  puts

  instance_state = nil
  while(instance_state != 'running')
    instance_state, dns_name = get_instance_state(instance_id)
    puts "=> Checking for running state... #{instance_state}"
    puts "=> Public DNS: #{dns_name}" if instance_state == 'running'
    sleep 10 unless instance_state == 'running'
  end

  puts

  puts "Do you want to associate a public IP? (leave empty if not)"
  public_ip = gets

  if not public_ip.empty? and public_ip.size > 1
    puts "Associating public IP address... #{public_ip}"
    result = ec2.associate_address(:instance_id => instance_id, :public_ip => public_ip.lstrip.rstrip)
    if result["return"] == "true"
      puts "=> Success."
    end
  end

  puts
  puts "Please enter your volume id (leave empty if you don't want to attach a volume):"
  volume_id = gets

  if not volume_id.empty? and volume_id.size > 1
    puts "Attaching volume... #{volume_id}"
    result = ec2.attach_volume(:volume_id => volume_id.lstrip.rstrip, :instance_id => instance_id, :device => '/dev/sdf')
    if result["return"] == "true"
      puts "=> Success."
    end
  end
end