Class: Chapp::EC2Provisioner

Inherits:
Provisioner show all
Defined in:
lib/chapp/provisioner/ec2_provisioner.rb

Constant Summary collapse

ACCESS_KEY =

Knife config

"ec2_access_key_id"
SECRET_KEY =
"ec2_secret_access_key"
KEY_PAIR =
"ec2_key_pair"
IMAGE_ID =

App config

"image_id"
INSTANCE_TYPE =
"instance_type"

Instance Method Summary collapse

Instance Method Details

#build_server(app_config, properties) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chapp/provisioner/ec2_provisioner.rb', line 16

def build_server app_config, properties
  
  errors = []
  
  access_key = Chef::Config[ACCESS_KEY]
  secret_key = Chef::Config[SECRET_KEY]
  key_pair = Chef::Config[KEY_PAIR]
  
  if access_key.nil?
    errors << "#{ACCESS_KEY} must be specified in your knife config"
  end
  
  if secret_key.nil?
    errors << "#{SECRET_KEY} must be specified in your knife config"
  end
  
  if key_pair.nil?
    errors << "#{KEY_PAIR} must be specified in your knife config"
  end
  
  unless app_config.cloud.has_key? IMAGE_ID
    errors << "#{IMAGE_ID} must be specified in your app's cloud config"
  end
  
  unless app_config.cloud.has_key? INSTANCE_TYPE
    errors << "#{INSTANCE_TYPE} must be specified in your app's cloud config"
  end
  
  image_id = app_config.cloud[IMAGE_ID]
  instance_type = app_config.cloud[INSTANCE_TYPE]
  
  ec2 = AWS::EC2.new(:access_key_id => access_key, :secret_access_key => secret_key)
  
  instance = ec2.instances.create(
    :image_id => image_id,
    :instance_type => instance_type,
    :key_pair => ec2.key_pairs[key_pair]
  )
  
  puts "Waiting for new instance with id #{instance.id} to become available..."
  sleep 1 while instance.status == :pending
  
  puts "#{instance.dns_name} is ready"

  # TODO: Wait for SSH to become available
  sleep 70

  instance.dns_name
end