fluke/devops-service/workers/stack_bootstrap_worker.rb

74 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
2018-04-04 22:44:39 +03:00
# 'without_bootstrap', optional. false by default
# 'skip_rollback', optional. false by default
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')
2018-04-04 22:44:39 +03:00
without_bootstrap = options['without_bootstrap'] || false
skip_rollback = options['skip_rollback'] || false
@current_user = stack_attrs['owner']
2015-09-01 16:31:31 +03:00
2018-04-04 22:44:39 +03:00
set_task_data({
"created_by" => stack_attrs['owner'],
"project" => stack_attrs["project"],
"environment" => stack_attrs["environment"],
"type" => ::Devops::Model::JobTask::STACK_TYPE,
"subtasks" => [],
"stack" => stack_attrs['name']
})
2018-04-04 22:44:39 +03:00
executor.create_stack(stack_attrs)
2016-03-30 12:05:59 +03:00
if !executor.wait_till_stack_is_created
puts_and_flush "Stack creating error"
2018-04-04 22:44:39 +03:00
return 1
end
begin
executor.persist_new_servers
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
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
2018-04-04 22:44:39 +03:00
@executor ||= Devops::Executor::StackExecutor.new(out: out, current_user: @current_user)
end
2016-03-30 12:05:59 +03:00
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}")
2018-04-04 22:44:39 +03:00
if bootstrap_result.bootstrap_error? && !options[:skip_rollback]
rollback_stack!
2016-03-30 12:05:59 +03:00
end
bootstrap_result.code
end
2016-03-30 12:05:59 +03:00
def rollback_stack!
puts_and_flush "\nStart rollback of a stack"
2018-04-04 22:44:39 +03:00
begin
executor.delete_stack
puts_and_flush "Stack rollback has been completed"
rescue StandardError
puts_and_flush "Stack rollback failed"
end
2015-09-01 16:31:31 +03:00
end
2015-07-20 18:59:26 +03:00
end