lib/autocanary24/client.rb in autocanary24-0.1.0.pre.alpha2 vs lib/autocanary24/client.rb in autocanary24-0.1.0
- old
+ new
@@ -10,11 +10,11 @@
class Client
def initialize(**params)
@configuration = Configuration.new(params) #params.fetch(:configuration, Configuration::new(params))
end
- def deploy_stack(parent_stack_name, template, parameters, tags = nil, deployment_check = lambda { |stacks, elb, instances_to_create| true })
+ def deploy_stack(parent_stack_name, template, parameters, tags = nil, deployment_check = lambda { |stacks, elb, new_attached_instances| true })
begin
write_log(parent_stack_name, "Starting the deployment")
write_log(parent_stack_name, "Using the following configuration #{@configuration.inspect}")
elb = get_elb(parent_stack_name)
@@ -31,12 +31,19 @@
after_switch(stacks, failed || @configuration.keep_inactive_stack)
rescue Exception => e
write_log(parent_stack_name, "Unexpected exception #{e}")
+ raise "Deployment failed"
end
- write_log(parent_stack_name, "Deployment finished")
+
+ if failed
+ raise "Deployment failed because of rollback"
+ else
+ write_log(parent_stack_name, "Deployment finished")
+ end
+
end
private
def get_stacks_to_create_and_to_delete_for(blue_cs, green_cs, elb)
@@ -84,34 +91,38 @@
desired = stacks[:stack_to_create].get_desired_capacity
instances_to_toggle = (desired / 100.0 * @configuration.scaling_instance_percent).round
instances_to_toggle = 1 if (instances_to_toggle < 1)
- instances_to_create = stacks[:stack_to_create].get_instance_ids
- instances_to_delete = stacks[:stack_to_delete].nil? ? [] : stacks[:stack_to_delete].get_instance_ids
+ instances_to_attach = stacks[:stack_to_create].get_instance_ids
+ instances_to_detach = stacks[:stack_to_delete].nil? ? [] : stacks[:stack_to_delete].get_instance_ids
missing = desired
while missing > 0
write_log(stacks[:stack_to_create].stack_name, "Adding #{instances_to_toggle} instances (#{desired-missing+instances_to_toggle}/#{desired})")
+ already_attached_instances = instances_to_attach[0, desired-missing+instances_to_toggle]
+ already_detached_instances = instances_to_detach[0, desired-missing]
+
begin
- stacks[:stack_to_create].attach_instances_to_elb_and_wait(elb, instances_to_create[desired-missing, instances_to_toggle])
- rescue
- rollback(stacks, elb, instances_to_create, instances_to_delete)
+ stacks[:stack_to_create].attach_instances_to_elb_and_wait(elb, instances_to_attach[desired-missing, instances_to_toggle])
+ rescue Exception => e
+ write_log(stacks[:stack_to_create].stack_name, "Unexpected exception: #{e}")
+ rollback(stacks, elb, already_attached_instances, already_detached_instances)
return true
end
- unless deployment_check.call(stacks, elb, instances_to_create)
- rollback(stacks, elb, instances_to_create, instances_to_delete)
+ unless deployment_check.call(stacks, elb, already_attached_instances)
+ rollback(stacks, elb, already_attached_instances, already_detached_instances)
return true
end
if @configuration.keep_instances_balanced && !stacks[:stack_to_delete].nil?
begin
- write_log(stacks[:stack_to_delete].stack_name, "Removing #{instances_to_toggle} instances (#{instances_to_delete[desired-missing, instances_to_toggle]})")
- stacks[:stack_to_delete].detach_instances_from_elb(elb, instances_to_delete[desired-missing, instances_to_toggle])
+ write_log(stacks[:stack_to_delete].stack_name, "Removing #{instances_to_toggle} instances (#{instances_to_detach[desired-missing, instances_to_toggle]})")
+ stacks[:stack_to_delete].detach_instances_from_elb(elb, instances_to_detach[desired-missing, instances_to_toggle])
rescue Exception => e
write_log(stacks[:stack_to_delete].stack_name, "WARNING: #{e}")
end
end
@@ -128,24 +139,28 @@
write_log(stacks[:stack_to_delete].stack_name, "Detach from ELB #{elb}")
stacks[:stack_to_delete].detach_asg_from_elb_and_wait(elb)
end
end
- def rollback(stacks, elb, instances_to_create, instances_to_delete)
+ def rollback(stacks, elb, already_attached_instances, already_detached_instances)
+ write_log("", "Rollback triggered")
begin
- stacks[:stack_to_create].detach_instances_from_elb(elb, instances_to_create)
- stacks[:stack_to_delete].attach_instances_to_elb_and_wait(elb, instances_to_delete)
+ stacks[:stack_to_create].detach_instances_from_elb(elb, already_attached_instances)
+ stacks[:stack_to_delete].attach_instances_to_elb_and_wait(elb, already_detached_instances)
rescue Exception => e
write_log("", "ROLLBACK FAILED: #{e}")
end
end
def after_switch(stacks, keep_inactive_stack)
stacks[:stack_to_create].resume_asg_processes
- stacks[:stack_to_delete].resume_asg_processes unless stacks[:stack_to_delete].nil?
- if keep_inactive_stack == false
- delete_stack(stacks[:stack_to_delete])
+ unless stacks[:stack_to_delete].nil?
+ stacks[:stack_to_delete].resume_asg_processes
+
+ unless keep_inactive_stack
+ delete_stack(stacks[:stack_to_delete].stack_name)
+ end
end
end
def get_elb(stack_name)
get_first_resource_id(stack_name, 'AWS::ElasticLoadBalancing::LoadBalancer')