63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| module StackCommands
 | |
|   extend self
 | |
| 
 | |
|   RESULT_CODES = {
 | |
|     stack_rolled_back: 1,
 | |
|     unkown_status: 2,
 | |
|     timeout: 3,
 | |
|     error: 5
 | |
|   }
 | |
| 
 | |
|   def self.result_codes
 | |
|     RESULT_CODES
 | |
|   end
 | |
| 
 | |
|   def self.result_code(code)
 | |
|     result_codes.fetch(code)
 | |
|   end
 | |
| 
 | |
|   def sync_stack_proc
 | |
|     lambda do |out, stack, mongo|
 | |
|       # 5 tries each 5 seconds, then 200 tries each 10 seconds
 | |
|       sleep_times = [5]*5 + [10]*200
 | |
| 
 | |
|       begin
 | |
|         out << "Syncing stack '#{stack.id}'...\n"
 | |
|         events_keys = []
 | |
|         sleep_times.each do |sleep_time|
 | |
|           sleep sleep_time
 | |
|           stack.sync_details!
 | |
|           stack.events.each do |event|
 | |
|             unless events_keys.include?(event["event_id"])
 | |
|               events_keys << event["event_id"]
 | |
|               out.puts "#{event["timestamp"]} - #{event["status"]}: #{event["reason"]}"
 | |
|             end
 | |
|           end
 | |
|           case stack.stack_status
 | |
|           when 'CREATE_IN_PROGRESS'
 | |
|             out.flush
 | |
|           when 'CREATE_COMPLETE'
 | |
|             mongo.stack_update(stack)
 | |
|             out << "\nStack '#{stack.id}' status is now #{stack.stack_status}\n"
 | |
|             out.flush
 | |
|             return 0
 | |
|           when 'ROLLBACK_COMPLETE'
 | |
|             out << "\nStack '#{stack.id}' status is rolled back\n"
 | |
|             return StackCommands.result_code(:stack_rolled_back)
 | |
|           else
 | |
|             out.puts "\nUnknown stack status: '#{stack.stack_status}'"
 | |
|             return StackCommands.result_code(:unkown_status)
 | |
|           end
 | |
|         end
 | |
|         out.puts "Stack hasn't synced in #{sleep_times.inject(&:+)} seconds."
 | |
|         return StackCommands.result_code(:timeout)
 | |
|       rescue StandardError => e
 | |
|         logger.error e.message
 | |
|         out << "Error: #{e.message}\n"
 | |
|         return StackCommands.result_code(:error)
 | |
|       end
 | |
|     end
 | |
|   end
 | |
| 
 | |
| end
 | 
