Skip to content

Instantly share code, notes, and snippets.

@johntdyer
Created December 5, 2011 21:23
Show Gist options
  • Select an option

  • Save johntdyer/1435417 to your computer and use it in GitHub Desktop.

Select an option

Save johntdyer/1435417 to your computer and use it in GitHub Desktop.
module Rayo
module Aws
AWS = Fog::Compute.new({
:provider => 'AWS',
:aws_access_key_id => CONFIG.aws.access_key_id,
:aws_secret_access_key => CONFIG.aws.secret_access_key
})
def create_server(opts={})
hostname=opts[:hostname]
domain=opts[:domain]
fqdn="#{hostname}.#{domain}"
instance=AWS.servers.create({
:image_id => CONFIG.aws.ami_id,
:key_name => CONFIG.aws.key_name,
:flavor_id => CONFIG.aws.instance_size,
:groups => ["rayo_workshop"],
:availability_zone => CONFIG.aws.availability_zone,
:tags=>[]
})
instance.wait_for{ ready? }
if opts[:tags]
opts[:tags].each_pair do |key,val|
AWS.tags.create :key => key, :value => val, :resource_id => instance.id
end
end
print "Launching #{fqdn}: "
instance.wait_for{ ready? }
puts "---------------------------------------------"
puts "Chef client instance id: " + instance.id
puts "Chef client IP: #{instance.public_ip_address}"
puts "Chef client ec2 DNS: #{instance.dns_name}"
sleep 10
eruby = Erubis::Eruby.new(File.read("#{File.join(File.dirname(__FILE__),'..','bootstraps')}/centos_ec2_new.erb"))
userdata = eruby.result(
:validation_key => CONFIG.chef.validation_key,
:target_server => CONFIG.chef.server,
:client_name => CONFIG.chef.client_name,
:hostname=> hostname,
:domain=>domain
)
ssh_session = AWS.servers.get(instance.id)
ssh_session.username = CONFIG.aws.username
ssh_session.private_key_path = CONFIG.aws.key_path
begin
puts "Sending userdata to #{hostname} - Key: #{CONFIG.aws.key_path}, User: #{CONFIG.aws.username}"
ssh_session.ssh(userdata)
rescue Errno::ECONNREFUSED
puts "Host: #{hostname} not ready, sleeping 10 seconds"
sleep 10
retry
end
puts "Finished Bootstrap - {:id=>#{instance.id},:hostname=>#{hostname}}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment