91 lines
2.5 KiB
Ruby
91 lines
2.5 KiB
Ruby
require 'lib/executors/stack_executor'
|
|
|
|
class StackBootstrapWorker < Worker
|
|
|
|
# options must contain 'stack_attributes'
|
|
def perform(options)
|
|
call do
|
|
stack_attrs = options.fetch('stack_attributes')
|
|
|
|
without_bootstrap = stack_attrs.delete('without_bootstrap')
|
|
skip_rollback = false # take it from options in future
|
|
@out.puts "Received 'without_bootstrap' option" if without_bootstrap
|
|
|
|
save_report(stack_attrs)
|
|
|
|
create_stack(stack_attrs)
|
|
if !executor.wait_till_stack_is_created
|
|
puts_and_flush "Stack creating error"
|
|
return 1
|
|
end
|
|
|
|
begin
|
|
persist_stack_servers
|
|
if without_bootstrap
|
|
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 create_stack(stack_attrs)
|
|
@stack = executor.create_stack(stack_attrs)
|
|
end
|
|
|
|
def persist_stack_servers
|
|
@servers_by_priorities = executor.persist_stack_servers[:just_persisted_by_priority]
|
|
end
|
|
|
|
# options should contain :skip_rollback
|
|
def bootstrap_or_rollback_if_failed(options)
|
|
bootstrap_result = bootstrap_servers_by_priority
|
|
puts_and_flush Devops::Messages.t("worker.stack_bootstrap.bootstrap_result.#{bootstrap_result.reason}")
|
|
if bootstrap_result.bootstrap_error? && !options[:skip_rollback]
|
|
rollback_stack!
|
|
end
|
|
bootstrap_result.code
|
|
end
|
|
|
|
def bootstrap_servers_by_priority
|
|
out.puts "Bootstrapping just persisted servers"
|
|
@servers_by_priorities.each do |priority, servers|
|
|
out.puts "Servers with priority '#{priority}': #{servers.map(&:id).join(", ")}"
|
|
end
|
|
out.flush
|
|
executor.bootstrap_servers_by_priority(@servers_by_priorities, jid)
|
|
end
|
|
|
|
def rollback_stack!
|
|
puts_and_flush "\nStart rollback of a stack"
|
|
begin
|
|
executor.delete_stack
|
|
puts_and_flush "Stack rollback has been completed"
|
|
rescue StandardError
|
|
puts_and_flush "Stack rollback failed"
|
|
end
|
|
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
|