Class: AWS::Autoscale

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/conan/cloud/aws/autoscale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#all_availability_zones, #all_regions, #default_availability_zone, #default_image_id, #ec2_name_tag, #elasticache_name_tag, #find_server_by_name, #list_stage_servers, #rds_name_tag

Constructor Details

#initialize(stage = 'production', autoscale_config = {}, application = nil) ⇒ Autoscale

Returns a new instance of Autoscale.



13
14
15
16
17
# File 'lib/conan/cloud/aws/autoscale.rb', line 13

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

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



11
12
13
# File 'lib/conan/cloud/aws/autoscale.rb', line 11

def application
  @application
end

#autoscale_configObject

Returns the value of attribute autoscale_config.



11
12
13
# File 'lib/conan/cloud/aws/autoscale.rb', line 11

def autoscale_config
  @autoscale_config
end

#stageObject

Returns the value of attribute stage.



11
12
13
# File 'lib/conan/cloud/aws/autoscale.rb', line 11

def stage
  @stage
end

Instance Method Details

#configure_autoscaleObject



91
92
# File 'lib/conan/cloud/aws/autoscale.rb', line 91

def configure_autoscale
end

#create_ami_from_server(server_name, region, image_description = nil) ⇒ Object



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
# File 'lib/conan/cloud/aws/autoscale.rb', line 19

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

#create_launch_config(name, config = {}) ⇒ Object



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
# File 'lib/conan/cloud/aws/autoscale.rb', line 46

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)

  params = {}
  params['KeyName'] = new_conf[:key_name] || compute.key_pairs.all.first.name

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

  instance_type = new_conf.delete(:instance_type) || "m1.small"

  if new_conf[:server_to_image].nil?
    if new_conf[:image_id].nil?
      image_id = default_image_id(region, instance_type, new_conf[:root_device_type]) 
    else
      image_id = new_conf[:image_id]
    end
  else
    image_id = create_ami_from_server(new_conf[: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
  id = "#{ec2_name_tag(name)}-#{Time.now.strftime('%Y%m%d%H%M')}"

  puts "Creating Autoscale Launch Configuration #{id}"
  lc = autoscale.configurations.connection.create_launch_configuration(image_id, instance_type, id, params)

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

end

#update_autoscaleObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/conan/cloud/aws/autoscale.rb', line 94

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