fluke/devops-service/workers/stack_bootstrap_worker.rb
2016-04-22 15:43:59 +03:00

77 lines
2.1 KiB
Ruby

require 'lib/executors/stack_executor'
class StackBootstrapWorker < Worker
# @options:
# 'stack_attributes', required
def perform(options)
call do
puts_and_flush JSON.pretty_generate(options)
stack_attrs = options.fetch('stack_attributes')
save_report(stack_attrs)
@stack = executor.create_stack(stack_attrs)
without_bootstrap = @stack.without_bootstrap?
skip_rollback = @stack.skip_rollback?
if !executor.wait_till_stack_is_created
puts_and_flush "Stack creating error"
return 1
end
begin
executor.persist_new_servers
@stack.unlock_persisting!
if without_bootstrap
puts_and_flush "\n\nBootstrap has been skipped. Stack has been successfully created."
0
else
bootstrap_or_rollback_if_failed(skip_rollback: skip_rollback)
end
rescue StandardError => e
puts_and_flush "\nAn error occured."
rollback_stack! unless skip_rollback
raise e
end
end
end
private
def executor
@executor ||= Devops::Executor::StackExecutor.new(out: out)
end
def bootstrap_or_rollback_if_failed(options)
bootstrap_result = executor.bootstrap_just_persisted(jid)
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
end
bootstrap_result.code
end
def rollback_stack!
puts_and_flush "\nStart rollback of a stack"
executor.delete_stack
puts_and_flush "Stack rollback has been completed"
end
def save_report(stack_attrs)
update_report(
"created_by" => stack_attrs['owner'],
"project" => stack_attrs["project"],
"deploy_env" => stack_attrs["deploy_env"],
"type" => ::Devops::Model::Report::STACK_TYPE,
"subreports" => [],
"stack" => stack_attrs['name']
)
end
end