Skip to content

Commit

Permalink
autoscale and related mods to use aws roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuart Eccles committed Feb 27, 2012
1 parent 48ef60c commit cd8b4fb
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 138 deletions.
2 changes: 2 additions & 0 deletions lib/conan/capistrano.rb
@@ -1,5 +1,7 @@
require "bundler/capistrano"
require "conan/deployment"
require "conan/cloud/aws/provision"
require "conan/cloud/aws/autoscale"

Capistrano::Configuration.instance(:must_exist).load do
Conan::Deployment.define_tasks(self)
Expand Down
106 changes: 106 additions & 0 deletions lib/conan/cloud/aws/autoscale.rb
@@ -0,0 +1,106 @@
require 'fileutils'
require 'fog'
require 'json'

require_relative "./utils"

module AWS
class Autoscale
include Utils

attr_accessor :autoscale_config, :stage, :application

def initialize(stage = 'production', autoscale_config = {}, application = nil)
@autoscale_config = autoscale_config
@stage = stage
@application = application
end

def create_ami_from_server(server_name, region, image_description = nil)
image_name = "#{server_name}-image-#{Time.now.strftime('%Y%m%d%H%M')}" if image_name.nil?
image_description = "Image of #{server_name} created at #{DateTime.now} by conan"
server_to_image = find_server_by_name(server_name, region)

raise "Server #{server_name} in #{region} does not exist" if server_to_image.nil?

compute = Fog::Compute.new(:provider => :aws, :region => region)
puts "Creating image #{image_name} from server #{server_name}"
ami_request = compute.create_image(server_to_image.id, image_name, image_description)
image_id = ami_request.body["imageId"]

pending = true
image = nil

puts "Waiting for #{image_id} to become available"
while pending
sleep 10
image = compute.images.get(image_id)
pending = image.state == "pending"
end

raise "Image creation failed" if image.state == 'failed'
image_id

end

def create_launch_config(name, config = {})
new_conf = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
region = new_conf[:region] || "us-east-1"

compute = Fog::Compute.new(:provider => :aws, :region => region)
default_key_name = compute.key_pairs.all.first.name

default_params = { :instance_monitoring => true,
:instance_type => "m1.small",
:key_name => default_key_name
}

params = default_params.merge(new_conf)

if new_conf[:security_groups] and new_conf[:security_groups].size > 0
params[:security_groups] = new_conf[:security_groups].collect { |g| "#{stage}-#{g}" }
else
params[:security_groups] = ["#{stage}-default"]
end

if params[:server_to_image].nil?
params[:image_id] = default_image_id(region, params[:flavor_id], params[:root_device_type]) if params[:image_id].nil?
else
params[:image_id] = create_ami_from_server(params[:server_to_image], region) end

autoscale = Fog::AWS::AutoScaling.new(:region => region)

#need to create new launchconfiugrations with unique names
#because you can't modify them and you can't delete them if they are attached
#to an autoscale group
params[:id] = "#{ec2_name_tag(name)}-#{Time.now.strftime('%Y%m%d%H%M')}"

puts "Creating Autoscale Launch Configuration #{params[:id]}"
lc = autoscale.configurations.create(params)

params[:autoscale_groups].each do |group_name|
asg = autoscale.groups.get(group_name)
"Setting Autoscale Group #{group_name} to use Launch Configration #{params[:id]}"
asg.connection.update_auto_scaling_group(asg.id, {"LaunchConfigurationName" => params[:id]})
end unless params[:autoscale_groups].nil?

end

def configure_autoscale
end

def update_autoscale
autoscale_config.each do |type, resources|
case type
when "launch-config"
resources.each do |name, conf|
create_launch_config(name, conf)
end
end
end
end

end
end


56 changes: 28 additions & 28 deletions lib/conan/cloud/aws/default_amis.json
Expand Up @@ -2,72 +2,72 @@
"ubuntu 10.04": {
"ap-northeast-1": {
"64-bit": {
"ebs": "ami-36e65037",
"instance-store": "ami-baf94fbb"
"ebs": "ami-942f9995",
"instance-store": "ami-902f9991"
},
"32-bit": {
"ebs": "ami-1ae6501b",
"instance-store": "ami-46f94f47"
"ebs": "ami-922f9993",
"instance-store": "ami-842f9985"
}
},
"ap-southeast-1": {
"64-bit": {
"ebs": "ami-c0c98c92",
"instance-store": "ami-3cc98c6e"
"ebs": "ami-7089cd22",
"instance-store": "ami-4e89cd1c"
},
"32-bit": {
"ebs": "ami-d8c98c8a",
"instance-store": "ami-76c98c24"
"ebs": "ami-7289cd20",
"instance-store": "ami-4489cd16"
}
},
"eu-west-1": {
"64-bit": {
"ebs": "ami-81dde2f5",
"instance-store": "ami-3fdde24b"
"ebs": "ami-fb665f8f",
"instance-store": "ami-1b665f6f"
},
"32-bit": {
"ebs": "ami-95dde2e1",
"instance-store": "ami-fddee189"
"ebs": "ami-f3665f87",
"instance-store": "ami-39665f4d"
}
},
"sa-east-1": {
"64-bit": {
"ebs": "ami-645f8079",
"instance-store": "ami-7a5f8067"
"ebs": "ami-7874ab65",
"instance-store": "ami-7c74ab61"
},
"32-bit": {
"ebs": "ami-6a5f8077",
"instance-store": "ami-4a5f8057"
"ebs": "ami-7e74ab63",
"instance-store": "ami-4674ab5b"
}
},
"us-east-1": {
"64-bit": {
"ebs": "ami-55dc0b3c",
"instance-store": "ami-35de095c"
"ebs": "ami-349b495d",
"instance-store": "ami-5c9b4935"
},
"32-bit": {
"ebs": "ami-71dc0b18",
"instance-store": "ami-4fd00726"
"ebs": "ami-3e9b4957",
"instance-store": "ami-809a48e9"
}
},
"us-west-1": {
"64-bit": {
"ebs": "ami-a191cfe4",
"instance-store": "ami-3991cf7c"
"ebs": "ami-7fb0e93a",
"instance-store": "ami-6bb0e92e"
},
"32-bit": {
"ebs": "ami-9991cfdc",
"instance-store": "ami-b390cef6"
"ebs": "ami-7db0e938",
"instance-store": "ami-63b0e926"
}
},
"us-west-2": {
"64-bit": {
"ebs": "ami-8eb33ebe",
"instance-store": "ami-94b33ea4"
"ebs": "ami-ec0b86dc",
"instance-store": "ami-e60b86d6"
},
"32-bit": {
"ebs": "ami-8cb33ebc",
"instance-store": "ami-bcb33e8c"
"ebs": "ami-ea0b86da",
"instance-store": "ami-e00b86d0"
}
}
}
Expand Down

0 comments on commit cd8b4fb

Please sign in to comment.