Class: AWS::Autoscale
- Inherits:
-
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
#application ⇒ Object
Returns the value of attribute application.
11
12
13
|
# File 'lib/conan/cloud/aws/autoscale.rb', line 11
def application
@application
end
|
#autoscale_config ⇒ Object
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
|
#stage ⇒ Object
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
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)
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
|
#launch_test_instances ⇒ Object
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/conan/cloud/aws/autoscale.rb', line 105
def launch_test_instances
autoscale_config.each do |type, resources|
case type
when "launch-config"
resources.each do |name, conf|
start_test_server(name, conf)
end
end
end
end
|
#start_test_server(name, config = {}) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/conan/cloud/aws/autoscale.rb', line 116
def start_test_server(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)
autoscale = Fog::AWS::AutoScaling.new(:region => region)
asres = autoscale.describe_auto_scaling_groups({'AutoScalingGroupNames' => new_conf[:autoscale_groups]})
lcname = ""
asres.body["DescribeAutoScalingGroupsResult"]["AutoScalingGroups"].each do |asgroup|
lcname = asgroup["LaunchConfigurationName"]
break if !lcname.nil? && lcname.length > 0
end
lcres = autoscale.describe_launch_configurations({'LaunchConfigurationNames' => [lcname]})
lc = lcres.body["DescribeLaunchConfigurationsResult"]["LaunchConfigurations"][0]
compute = Fog::Compute.new(:provider => :aws, :region => region)
server_params = {}
server_params[:groups] = lc["SecurityGroups"]
server_params[:key_name] = lc["KeyName"]
server_params[:image_id] = lc["ImageId"]
server_params[:monitoring] = false
server_params[:flavor_id] = lc["InstanceType"]
server_params[:root_device_type] = "ebs"
server_params[:availability_zone] = default_availability_zone(region)
puts "Creating test instance from image for launch configuration #{lcname}"
server = compute.servers.create(server_params)
server.wait_for { ready? }
puts "Created server #{server.dns_name} for testing. Please delete manually"
end
|
#update_autoscale ⇒ Object
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
|