2016-01-31 17:52:11 +03:00
|
|
|
class StackSynchronizer
|
|
|
|
|
include PutsAndFlush
|
|
|
|
|
attr_reader :out, :stack
|
|
|
|
|
|
|
|
|
|
def initialize(stack, out)
|
|
|
|
|
@stack, @out = stack, out
|
|
|
|
|
@printed_events = []
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sync
|
|
|
|
|
puts_and_flush "Syncing stack '#{stack.id}'..."
|
|
|
|
|
|
|
|
|
|
# 5 tries each 5 seconds, then 200 tries each 10 seconds
|
|
|
|
|
sleep_times = [5]*5 + [10]*400
|
|
|
|
|
|
|
|
|
|
sleep_times.each do |sleep_time|
|
|
|
|
|
sleep sleep_time
|
2016-02-24 21:26:44 +03:00
|
|
|
stack.sync!
|
2016-01-31 17:52:11 +03:00
|
|
|
print_new_events
|
|
|
|
|
case stack.stack_status
|
2016-03-02 16:00:20 +03:00
|
|
|
when 'CREATE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS', 'DELETE_IN_PROGRESS'
|
2016-01-31 17:52:11 +03:00
|
|
|
when 'CREATE_COMPLETE'
|
|
|
|
|
::Devops::Db.connector.stack_update(stack)
|
|
|
|
|
puts_and_flush "Stack '#{stack.id}' status is now #{stack.stack_status}"
|
|
|
|
|
return 0
|
|
|
|
|
when 'ROLLBACK_COMPLETE'
|
|
|
|
|
puts_and_flush "Stack '#{stack.id}' status is rolled back"
|
|
|
|
|
return error_code(:stack_rolled_back)
|
2016-03-02 16:00:20 +03:00
|
|
|
when 'DELETE_COMPLETE'
|
|
|
|
|
puts_and_flush "Stack '#{stack.id}' status is deleted"
|
|
|
|
|
return error_code(:stack_deleted)
|
2016-01-31 17:52:11 +03:00
|
|
|
else
|
|
|
|
|
puts_and_flush "Unknown stack status: '#{stack.stack_status}'"
|
|
|
|
|
return error_code(:unkown_status)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
puts_and_flush "Stack hasn't been synced in #{sleep_times.inject(&:+)} seconds."
|
|
|
|
|
error_code(:timeout)
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
DevopsLogger.logger.error e.message
|
|
|
|
|
puts_and_flush "Error: #{e.message}\n#{e.backtrace.join("\n")}"
|
|
|
|
|
error_code(:error)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reason_from_error_code(code)
|
|
|
|
|
error_codes.key(code)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def error_code(reason)
|
|
|
|
|
error_codes.fetch(reason)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def error_codes
|
|
|
|
|
{
|
|
|
|
|
stack_rolled_back: 1,
|
|
|
|
|
unkown_status: 2,
|
|
|
|
|
timeout: 3,
|
2016-03-02 16:00:20 +03:00
|
|
|
error: 5,
|
|
|
|
|
stack_deleted: 6
|
2016-01-31 17:52:11 +03:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_new_events
|
|
|
|
|
stack.events.each do |event|
|
|
|
|
|
unless @printed_events.include?(event["event_id"])
|
|
|
|
|
@printed_events << event["event_id"]
|
|
|
|
|
out.puts "#{event["timestamp"]} - #{event["status"]}: #{event["reason"]}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
out.flush
|
|
|
|
|
end
|
|
|
|
|
end
|