Class: AMI::InstanceProvisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/ami/instance_provisioner.rb

Overview

Provisions an EC2 instance and returns the resulting server object

Class Method Summary collapse

Class Method Details

.provision(server_name, server_template, config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ami/instance_provisioner.rb', line 6

def self.provision(server_name, server_template, config)
  options = {
    :image_id => server_template.image_id,
    :instance_type => server_template.instance_type,
    :security_groups => server_template.security_groups,
    :key_name => config.key_name
  }

  ec2 = AWS::EC2.new(
    :access_key_id => config.access_key,
    :secret_access_key => config.secret_key
  )

  server = ec2.instances.create(options)

  print 'creating server'
  wait_for { server.status != :pending }
  puts 'done'

  server.tags.Name = server_name
  puts "tagged as #{server_name}"
  server
end

.wait_for(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ami/instance_provisioner.rb', line 30

def self.wait_for(&block)
  output = ['.', '.', '.', "\b\b\b   \b\b\b"]

  output.cycle do |o|
    break if block.call
    print o
    sleep 1
  end
end