fluke/devops-service/workers/stack_bootstrap_worker.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

2016-03-30 12:05:59 +03:00
require 'lib/executors/stack_executor'
2015-09-21 15:54:33 +03:00
class StackBootstrapWorker < Worker
2015-07-20 18:59:26 +03:00
2016-04-04 13:17:07 +03:00
# @options:
# 'stack_attributes', required
2015-07-23 12:54:36 +03:00
def perform(options)
call do
2016-04-04 13:17:07 +03:00
puts_and_flush JSON.pretty_generate(options)
stack_attrs = options.fetch('stack_attributes')
save_report(stack_attrs)
2015-09-01 16:31:31 +03:00
@stack = executor.create_stack(stack_attrs)
without_bootstrap = @stack.without_bootstrap?
skip_rollback = @stack.skip_rollback?
2016-03-30 12:05:59 +03:00
if !executor.wait_till_stack_is_created
puts_and_flush "Stack creating error"
2016-03-25 14:50:22 +03:00
return 1
end
begin
2016-04-04 13:17:07 +03:00
executor.persist_new_servers
@stack.unlock_persisting!
2016-03-30 12:05:59 +03:00
if without_bootstrap
puts_and_flush "\n\nBootstrap has been skipped. Stack has been successfully created."
2016-03-30 12:05:59 +03:00
0
else
bootstrap_or_rollback_if_failed(skip_rollback: skip_rollback)
end
2016-03-25 14:50:22 +03:00
rescue StandardError => e
puts_and_flush "\nAn error occured."
2016-03-30 12:05:59 +03:00
rollback_stack! unless skip_rollback
2016-03-25 14:50:22 +03:00
raise e
2015-08-12 12:24:48 +03:00
end
2015-07-20 18:59:26 +03:00
end
end
2015-07-30 02:14:45 +03:00
private
2016-03-30 12:05:59 +03:00
def executor
@executor ||= Devops::Executor::StackExecutor.new(out: out)
end
def bootstrap_or_rollback_if_failed(options)
2016-04-04 13:17:07 +03:00
bootstrap_result = executor.bootstrap_just_persisted(jid)
2016-03-30 12:05:59 +03:00
puts_and_flush Devops::Messages.t("worker.stack_bootstrap.bootstrap_result.#{bootstrap_result.reason}")
# move try_rollback to stack executor
if bootstrap_result.bootstrap_error?
if options[:skip_rollback]
puts_and_flush "\nSkip rollback because skip_rollback option set to true."
else
rollback_stack!
end
2016-03-30 12:05:59 +03:00
end
bootstrap_result.code
end
2015-09-21 15:54:33 +03:00
2016-03-30 12:05:59 +03:00
def rollback_stack!
puts_and_flush "\nStart rollback of a stack"
executor.delete_stack
puts_and_flush "Stack rollback has been completed"
2015-09-01 16:31:31 +03:00
end
def save_report(stack_attrs)
update_report(
2015-09-01 16:31:31 +03:00
"created_by" => stack_attrs['owner'],
"project" => stack_attrs["project"],
"deploy_env" => stack_attrs["deploy_env"],
2015-11-05 16:47:12 +03:00
"type" => ::Devops::Model::Report::STACK_TYPE,
"subreports" => [],
"stack" => stack_attrs['name']
2015-09-01 16:31:31 +03:00
)
end
2015-07-20 18:59:26 +03:00
end