| 
									
										
										
										
											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-09-21 15:54:33 +03:00
										 |  |  |   def self.result_codes | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       stack_rolled_back: 1, | 
					
						
							|  |  |  |       unkown_status: 2, | 
					
						
							|  |  |  |       timeout: 3, | 
					
						
							|  |  |  |       error: 5
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   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-21 17:13:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 18:59:26 +03:00
										 |  |  |       begin | 
					
						
							| 
									
										
										
										
											2015-07-21 17:13:10 +03:00
										 |  |  |         out << "Syncing stack '#{stack.id}'...\n" | 
					
						
							|  |  |  |         sleep_times.each do |sleep_time| | 
					
						
							|  |  |  |           sleep sleep_time | 
					
						
							| 
									
										
										
										
											2015-07-20 18:59:26 +03:00
										 |  |  |           stack.sync_details! | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |           case stack.stack_status | 
					
						
							|  |  |  |           when 'CREATE_IN_PROGRESS' | 
					
						
							| 
									
										
										
										
											2015-08-20 21:55:44 +03:00
										 |  |  |             out << "." | 
					
						
							| 
									
										
										
										
											2015-09-21 15:54:33 +03:00
										 |  |  |             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 |