2015-07-20 18:59:26 +03:00
|
|
|
require "db/mongo/models/stack/stack_factory"
|
2015-07-30 02:14:45 +03:00
|
|
|
require "db/mongo/models/project"
|
2015-07-20 18:59:26 +03:00
|
|
|
require "db/mongo/models/report"
|
2016-01-31 17:52:11 +03:00
|
|
|
require "workers/stack_bootstrap/stack_synchronizer"
|
|
|
|
|
require "workers/stack_bootstrap/stack_servers_bootstrapper"
|
|
|
|
|
require "workers/stack_bootstrap/stack_servers_persister"
|
|
|
|
|
require "workers/stack_bootstrap/errors"
|
2015-07-20 18:59:26 +03:00
|
|
|
|
2015-09-21 15:54:33 +03:00
|
|
|
|
2015-07-30 13:18:55 +03:00
|
|
|
class StackBootstrapWorker < Worker
|
2015-07-20 18:59:26 +03:00
|
|
|
|
2015-07-23 12:54:36 +03:00
|
|
|
def perform(options)
|
2015-09-01 16:31:31 +03:00
|
|
|
stack_attrs = options.fetch('stack_attributes')
|
2015-08-14 17:48:07 +03:00
|
|
|
|
2015-11-03 12:05:07 +03:00
|
|
|
call() do |out, file|
|
2015-09-21 15:54:33 +03:00
|
|
|
@out = out
|
2015-09-01 16:31:31 +03:00
|
|
|
without_bootstrap = stack_attrs.delete('without_bootstrap')
|
2015-09-24 15:06:02 +03:00
|
|
|
@out.puts "Received 'without_bootstrap' option" if without_bootstrap
|
|
|
|
|
|
2016-01-31 17:52:11 +03:00
|
|
|
save_report(file, stack_attrs)
|
2015-09-01 16:31:31 +03:00
|
|
|
|
2015-09-21 15:54:33 +03:00
|
|
|
begin
|
2016-01-31 17:52:11 +03:00
|
|
|
@stack = create_stack(stack_attrs)
|
2015-07-20 18:59:26 +03:00
|
|
|
|
2015-11-17 16:21:28 +03:00
|
|
|
begin
|
2016-01-31 17:52:11 +03:00
|
|
|
@servers_with_priority = stack_servers_persister.persist
|
|
|
|
|
bootstrap_in_priority_order unless without_bootstrap
|
2015-11-17 16:21:28 +03:00
|
|
|
0
|
2016-01-31 17:52:11 +03:00
|
|
|
rescue StackServerBootstrapError
|
|
|
|
|
puts_and_flush "\nAn error occured during bootstraping stack servers. Initiating stack rollback."
|
|
|
|
|
rollback_stack!(@stack)
|
2015-11-17 16:21:28 +03:00
|
|
|
2
|
2016-01-31 17:52:11 +03:00
|
|
|
rescue StackServerDeployError => e
|
|
|
|
|
out.puts "\nStack was launched, but an error occured during deploying stack servers."
|
|
|
|
|
puts_and_flush "You can redeploy stack after fixing the error."
|
2015-11-17 16:21:28 +03:00
|
|
|
3
|
2016-01-31 17:52:11 +03:00
|
|
|
rescue StackServerBootstrapDeployTimeout
|
|
|
|
|
puts_and_flush "\nBootstrap or deploy wasn't completed due to timeout."
|
|
|
|
|
4
|
2015-11-17 16:21:28 +03:00
|
|
|
rescue StandardError => e
|
2016-01-31 17:52:11 +03:00
|
|
|
puts_and_flush "\nAn error occured. Initiating stack rollback."
|
|
|
|
|
rollback_stack!(@stack)
|
2015-11-17 16:21:28 +03:00
|
|
|
raise e
|
2015-11-03 13:08:31 +03:00
|
|
|
end
|
2015-11-17 16:21:28 +03:00
|
|
|
rescue StackCreatingError
|
2016-01-31 17:52:11 +03:00
|
|
|
puts_and_flush "Stack creating error"
|
2015-09-21 15:54:33 +03:00
|
|
|
1
|
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-01-31 17:52:11 +03:00
|
|
|
def stack_synchronizer(stack)
|
|
|
|
|
StackSynchronizer.new(stack, out)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def stack_servers_persister
|
|
|
|
|
@stack_servers_persister ||= StackServersPersister.new(@stack, out)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def stack_servers_bootstrapper
|
|
|
|
|
@stack_servers_bootstrapper ||= StackServersBootstrapper.new(out, jid)
|
2015-09-21 15:54:33 +03:00
|
|
|
end
|
|
|
|
|
|
2016-01-31 17:52:11 +03:00
|
|
|
# builds and persist stack model, initiate stack creating in cloud
|
2015-11-03 12:05:07 +03:00
|
|
|
def create_stack(stack_attrs)
|
|
|
|
|
stack = Devops::Model::StackFactory.create(stack_attrs["provider"], stack_attrs, @out)
|
2015-09-21 15:54:33 +03:00
|
|
|
mongo.stack_insert(stack)
|
2016-01-31 17:52:11 +03:00
|
|
|
synchronizer = stack_synchronizer(stack)
|
|
|
|
|
operation_result = synchronizer.sync
|
2015-09-21 15:54:33 +03:00
|
|
|
|
|
|
|
|
if operation_result == 0
|
2016-01-31 17:52:11 +03:00
|
|
|
puts_and_flush "\nStack '#{stack.name}' has been created"
|
2015-09-21 15:54:33 +03:00
|
|
|
stack
|
|
|
|
|
else
|
2016-01-31 17:52:11 +03:00
|
|
|
human_readable_code = synchronizer.reason_from_error_code(operation_result)
|
|
|
|
|
out.puts "An error ocurred during stack creating"
|
|
|
|
|
puts_and_flush "Stack creating operation result was #{human_readable_code}"
|
2015-09-21 15:54:33 +03:00
|
|
|
raise StackCreatingError
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-31 17:52:11 +03:00
|
|
|
# Bootstrap servers with high priorities first
|
|
|
|
|
def bootstrap_in_priority_order
|
|
|
|
|
sorted_priorities = @servers_with_priority.keys.sort.reverse
|
|
|
|
|
sorted_priorities.each do |priority|
|
|
|
|
|
@out.puts "Servers with priority '#{priority}':"
|
|
|
|
|
stack_servers_bootstrapper.bootstrap(@servers_with_priority[priority])
|
2015-09-21 15:54:33 +03:00
|
|
|
end
|
2016-01-31 17:52:11 +03:00
|
|
|
puts_and_flush "Done."
|
2015-09-21 15:54:33 +03:00
|
|
|
end
|
|
|
|
|
|
2016-01-31 17:52:11 +03:00
|
|
|
def rollback_stack!(stack)
|
|
|
|
|
puts_and_flush "\nStart rollback of a stack"
|
|
|
|
|
stack.delete_stack_in_cloud!
|
|
|
|
|
Devops::Db.connector.stack_servers_delete(stack.name)
|
|
|
|
|
Devops::Db.connector.stack_delete(stack.id)
|
|
|
|
|
puts_and_flush "Rollback has been completed"
|
2015-09-01 16:31:31 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def save_report(file, stack_attrs)
|
|
|
|
|
report = ::Devops::Model::Report.new(
|
|
|
|
|
"file" => file,
|
|
|
|
|
"_id" => jid,
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
mongo.save_report(report)
|
2015-09-17 11:50:35 +03:00
|
|
|
report
|
2015-09-01 16:31:31 +03:00
|
|
|
end
|
2015-07-20 18:59:26 +03:00
|
|
|
end
|