| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  | require "db/mongo/models/model_with_provider" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "db/mongo/models/stack_template/stack_template_factory" | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  | require "providers/exceptions/name_conflict" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "db/validators/run_list_array" | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     class StackBase | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       include ::Mongoid::Document | 
					
						
							|  |  |  |       include ::Mongoid::Timestamps::Created | 
					
						
							|  |  |  |       include ::ActiveModel::Validations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       store_in collection: "stacks" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       include ModelWithProviderAccount | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       field :_id, as: :id, overwrite: true, type: String | 
					
						
							|  |  |  |       field :name, type: String | 
					
						
							|  |  |  |       field :project, type: String | 
					
						
							|  |  |  |       field :environment, type: String | 
					
						
							|  |  |  |       field :category, type: String | 
					
						
							|  |  |  |       field :stack_template, type: String | 
					
						
							|  |  |  |       field :parameters, type: Hash, default: {} | 
					
						
							|  |  |  |       field :owner, type: String | 
					
						
							|  |  |  |       field :run_list, type: Array, default: [] | 
					
						
							|  |  |  |       field :stack_status, type: String | 
					
						
							|  |  |  |       field :persisting_is_locked, type: Boolean | 
					
						
							|  |  |  |       field :tags, type: Hash, default: {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_accessor :events | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       validates :id, :project, :environment, :category, :stack_template, :owner, presence: true | 
					
						
							|  |  |  |       validates :id, uniqueness: true, on: :create | 
					
						
							|  |  |  |       validates :name, presence: true, allow_nil: true | 
					
						
							|  |  |  |       validates :run_list, runListArray: {allow_nil: true} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def to_hash | 
					
						
							|  |  |  |         attributes.tap { |hash| hash['id'] = hash.delete('_id') } | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:48:07 +03:00
										 |  |  |       def create_stack_in_cloud! out | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |         begin | 
					
						
							| 
									
										
										
										
											2015-08-14 17:48:07 +03:00
										 |  |  |           provider_instance.create_stack(self, out) | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |         rescue ProviderErrors::NameConflict | 
					
						
							|  |  |  |           raise InvalidRecord.new "Duplicate key error: stack with name '#{id}' already exists in cloud" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |       def delete_stack_in_cloud! | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |         provider_instance.delete_stack(self) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 13:17:07 +03:00
										 |  |  |       def sync_status_and_events! | 
					
						
							| 
									
										
										
										
											2016-02-24 21:26:44 +03:00
										 |  |  |         self.stack_status = provider_instance.stack_details(self)[:stack_status] | 
					
						
							| 
									
										
										
										
											2015-11-17 13:23:13 +03:00
										 |  |  |         self.events = provider_instance.stack_events(self) | 
					
						
							| 
									
										
										
										
											2016-03-23 18:35:05 +03:00
										 |  |  |       rescue Fog::AWS::CloudFormation::NotFound | 
					
						
							|  |  |  |         self.stack_status = 'NOT_FOUND' | 
					
						
							| 
									
										
										
										
											2015-07-15 18:37:27 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def resources | 
					
						
							|  |  |  |         provider_instance.stack_resources(self) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # resource_id is logical | 
					
						
							|  |  |  |       def resource(resource_id) | 
					
						
							|  |  |  |         provider_instance.stack_resource(self, resource_id) | 
					
						
							| 
									
										
										
										
											2015-12-15 18:56:14 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def stack_template_model | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         Devops::Model::StackTemplateFactory.get_class(provider).find(stack_template) | 
					
						
							| 
									
										
										
										
											2015-07-30 02:14:45 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 13:17:07 +03:00
										 |  |  |       def lock_persisting! | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         update_attribute :persisting_is_locked, true | 
					
						
							| 
									
										
										
										
											2016-04-04 13:17:07 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def unlock_persisting! | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         update_attribute :persisting_is_locked, false | 
					
						
							| 
									
										
										
										
											2016-04-04 13:17:07 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def account_instance | 
					
						
							|  |  |  |         @account_instance ||= Devops::Model::AwsProviderAccount.find(provider_account) | 
					
						
							| 
									
										
										
										
											2016-04-20 14:48:33 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |       class << self | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # attrs should include: | 
					
						
							|  |  |  |         # - id (String) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         # - environment (String) | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |         # - stack_template (String) | 
					
						
							| 
									
										
										
										
											2015-10-27 12:01:21 +03:00
										 |  |  |         # - provider (String) | 
					
						
							|  |  |  |         # - provider_account (String) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:48:07 +03:00
										 |  |  |         def create(attrs, out) | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |           model = new(attrs) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:48:07 +03:00
										 |  |  |           model.create_stack_in_cloud!(out) | 
					
						
							| 
									
										
										
										
											2016-04-04 13:17:07 +03:00
										 |  |  |           model.sync_status_and_events! | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |           model | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |