2016-04-04 13:17:07 +03:00
|
|
|
require 'lib/executors/stack_executor'
|
|
|
|
|
|
|
|
|
|
class StackSyncWorker < Worker
|
|
|
|
|
|
2016-04-15 15:51:52 +03:00
|
|
|
MAX_LOCK_WAITING_TIME = 6000
|
2016-04-04 13:17:07 +03:00
|
|
|
|
|
|
|
|
# @options:
|
|
|
|
|
# 'stack_name' required
|
|
|
|
|
# 'created_by' optional
|
|
|
|
|
def perform(options)
|
|
|
|
|
call do
|
|
|
|
|
stack_name = options.fetch('stack_name')
|
|
|
|
|
created_by = options['created_by'] || ::Devops::Model::Report::SYSTEM_OWNER
|
|
|
|
|
@stack = mongo.stack(stack_name)
|
|
|
|
|
save_report(created_by)
|
|
|
|
|
|
2016-04-15 15:51:52 +03:00
|
|
|
wait_until_stack_is_unlocked
|
|
|
|
|
begin
|
|
|
|
|
@stack.lock_persisting!
|
|
|
|
|
executor.delete_stale_servers
|
|
|
|
|
executor.persist_new_servers
|
|
|
|
|
ensure
|
|
|
|
|
@stack.unlock_persisting!
|
|
|
|
|
end
|
2016-04-04 13:17:07 +03:00
|
|
|
|
|
|
|
|
puts_and_flush "\n\nBootstrapping just persisted servers."
|
|
|
|
|
bootstrap_result = executor.bootstrap_just_persisted(jid)
|
|
|
|
|
puts_and_flush Devops::Messages.t("worker.stack_sync.bootstrap_result.#{bootstrap_result.reason}")
|
|
|
|
|
bootstrap_result.code
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def executor
|
|
|
|
|
@executor ||= Devops::Executor::StackExecutor.new(out: out, stack: @stack)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def wait_until_stack_is_unlocked
|
|
|
|
|
return unless @stack.persisting_is_locked
|
|
|
|
|
puts_and_flush 'Stack is locked, waiting...'
|
|
|
|
|
waiting_time = 0
|
|
|
|
|
sleep_time = 10
|
|
|
|
|
loop do
|
|
|
|
|
sleep(sleep_time)
|
|
|
|
|
@stack = mongo.stack(@stack.name)
|
|
|
|
|
return unless @stack.persisting_is_locked
|
|
|
|
|
|
|
|
|
|
waiting_time += sleep_time
|
|
|
|
|
raise "Stack has been locked for too long" if waiting_time > MAX_LOCK_WAITING_TIME
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def save_report(created_by)
|
|
|
|
|
update_report(
|
|
|
|
|
"created_by" => created_by,
|
|
|
|
|
"project" => @stack.project,
|
|
|
|
|
"deploy_env" => @stack.deploy_env,
|
|
|
|
|
"type" => ::Devops::Model::Report::SYNC_STACK_TYPE,
|
|
|
|
|
"subreports" => [],
|
|
|
|
|
"stack" => @stack.name
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|