fluke/devops-service/commands/stack.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

2015-07-20 18:59:26 +03:00
module StackCommands
2015-07-23 12:54:36 +03:00
extend self
2015-07-20 18:59:26 +03:00
2015-11-17 14:43:23 +03:00
RESULT_CODES = {
stack_rolled_back: 1,
unkown_status: 2,
timeout: 3,
error: 5
}
2015-09-21 15:54:33 +03:00
def self.result_codes
2015-11-17 14:43:23 +03:00
RESULT_CODES
2015-09-21 15:54:33 +03:00
end
def self.result_code(code)
result_codes.fetch(code)
end
2015-09-01 16:31:31 +03:00
def sync_stack_proc
2015-07-20 18:59:26 +03:00
lambda do |out, stack, mongo|
2015-09-21 15:54:33 +03:00
# 5 tries each 5 seconds, then 200 tries each 10 seconds
sleep_times = [5]*5 + [10]*200
2015-07-20 18:59:26 +03:00
begin
out << "Syncing stack '#{stack.id}'...\n"
2015-11-17 14:24:55 +03:00
events_keys = []
sleep_times.each do |sleep_time|
sleep sleep_time
2015-07-20 18:59:26 +03:00
stack.sync_details!
2015-11-17 14:24:55 +03:00
stack.events.each do |event|
unless events_keys.include?(event["event_id"])
events_keys << event["event_id"]
2015-11-17 16:46:30 +03:00
out.puts "#{event["timestamp"]} - #{event["status"]}: #{event["reason"]}"
2015-11-17 14:24:55 +03:00
end
end
2015-09-21 15:54:33 +03:00
case stack.stack_status
when 'CREATE_IN_PROGRESS'
out.flush
when 'CREATE_COMPLETE'
2015-07-20 18:59:26 +03:00
mongo.stack_update(stack)
2015-08-19 19:34:46 +03:00
out << "\nStack '#{stack.id}' status is now #{stack.stack_status}\n"
2015-08-18 17:53:39 +03:00
out.flush
2015-08-20 21:55:44 +03:00
return 0
2015-09-21 15:54:33 +03:00
when 'ROLLBACK_COMPLETE'
2015-08-20 21:55:44 +03:00
out << "\nStack '#{stack.id}' status is rolled back\n"
2015-09-21 15:54:33 +03:00
return StackCommands.result_code(:stack_rolled_back)
else
out.puts "\nUnknown stack status: '#{stack.stack_status}'"
return StackCommands.result_code(:unkown_status)
end
2015-07-20 18:59:26 +03:00
end
2015-09-21 15:54:33 +03:00
out.puts "Stack hasn't synced in #{sleep_times.inject(&:+)} seconds."
return StackCommands.result_code(:timeout)
2015-07-20 18:59:26 +03:00
rescue StandardError => e
logger.error e.message
2015-08-18 17:53:39 +03:00
out << "Error: #{e.message}\n"
2015-09-21 15:54:33 +03:00
return StackCommands.result_code(:error)
2015-07-20 18:59:26 +03:00
end
end
end
end