| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  | require "db/mongo/models/model_with_provider" | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  | require "providers/exceptions/name_conflict" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							|  |  |  |     class StackBase < MongoModel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |       include ModelWithProvider | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 13:32:26 +03:00
										 |  |  |       attr_accessor :id, :name, :project, :deploy_env, :stack_template, :parameters, :details, :owner, :run_list | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       types id: {type: String, empty: false}, | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  |             provider: {type: String, empty: false}, | 
					
						
							| 
									
										
										
										
											2015-07-16 16:01:53 +03:00
										 |  |  |             project: {type: String}, | 
					
						
							|  |  |  |             deploy_env: {type: String}, | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |             stack_template: {type: String, empty: false}, | 
					
						
							| 
									
										
										
										
											2015-08-12 13:32:26 +03:00
										 |  |  |             name: {type: String, nil: true}, | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |             owner: {type: String}, | 
					
						
							| 
									
										
										
										
											2015-08-05 16:06:29 +03:00
										 |  |  |             run_list: {type: Array, value_type: String, empty: true, nil: true} | 
					
						
							| 
									
										
										
										
											2015-07-17 15:15:52 +03:00
										 |  |  |             # details: {type: Hash, nil: true} # Hash type isn't supported yet | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def initialize attrs={} | 
					
						
							| 
									
										
										
										
											2015-08-18 15:55:58 +03:00
										 |  |  | #        self.provider = self.class.provider | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         self.id = attrs['id'] | 
					
						
							|  |  |  |         self.project = attrs['project'] | 
					
						
							|  |  |  |         self.deploy_env = attrs['deploy_env'] | 
					
						
							|  |  |  |         self.stack_template = attrs['stack_template'] | 
					
						
							| 
									
										
										
										
											2015-08-12 13:32:26 +03:00
										 |  |  |         self.name = attrs['name'] | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  |         self.parameters = attrs['parameters'] | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |         self.details = attrs['details'] | 
					
						
							| 
									
										
										
										
											2015-07-30 02:14:45 +03:00
										 |  |  |         self.owner = attrs['owner'] | 
					
						
							| 
									
										
										
										
											2015-08-14 17:48:07 +03:00
										 |  |  |         self.run_list = attrs['run_list'] || [] | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         self | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def to_hash_without_id | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           provider: provider, | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  |           project: project, | 
					
						
							|  |  |  |           deploy_env: deploy_env, | 
					
						
							|  |  |  |           stack_template: stack_template, | 
					
						
							| 
									
										
										
										
											2015-08-12 13:32:26 +03:00
										 |  |  |           name: name, | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |           parameters: parameters, | 
					
						
							| 
									
										
										
										
											2015-08-28 14:44:28 +03:00
										 |  |  |           # details are required to proper status handling | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  |           details: bson_safe_details, | 
					
						
							| 
									
										
										
										
											2015-07-30 02:14:45 +03:00
										 |  |  |           stack_status: stack_status, | 
					
						
							|  |  |  |           owner: owner | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  |       # overrided in ec2 | 
					
						
							|  |  |  |       def bson_safe_details | 
					
						
							|  |  |  |         details | 
					
						
							|  |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |       def sync_details! | 
					
						
							| 
									
										
										
										
											2015-08-28 17:00:48 +03:00
										 |  |  |         self.details = provider_instance.stack_details(self) | 
					
						
							| 
									
										
										
										
											2015-07-15 18:37:27 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def resources | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |         provider_instance.stack_resources(self) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-23 12:54:36 +03:00
										 |  |  |       # resource_id is logical | 
					
						
							| 
									
										
										
										
											2015-07-16 16:01:53 +03:00
										 |  |  |       def resource(resource_id) | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |         provider_instance.stack_resource(self, resource_id) | 
					
						
							| 
									
										
										
										
											2015-07-16 16:01:53 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 14:55:33 +03:00
										 |  |  |       # there is only one way to update stack status right now: | 
					
						
							|  |  |  |       #     stack.sync_details! | 
					
						
							|  |  |  |       #     mongo.stack_update(stack) | 
					
						
							|  |  |  |       # It's because syncing updates @details instance variable, but doesn'not persist it. | 
					
						
							|  |  |  |       # Also that's why we need to have "details" key in #to_mongo_hash | 
					
						
							| 
									
										
										
										
											2015-07-20 18:59:26 +03:00
										 |  |  |       def stack_status | 
					
						
							|  |  |  |         raise 'override me' | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def stack_statuses | 
					
						
							|  |  |  |         # maybe they differ in different providers, so use method instead of hardcoding | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           in_progress: 'CREATE_IN_PROGRESS', | 
					
						
							|  |  |  |           complete: 'CREATE_COMPLETE' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 02:14:45 +03:00
										 |  |  |       def template_body | 
					
						
							|  |  |  |         Devops::Db.connector.stack_template(stack_template).template_body | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |       class << self | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # attrs should include: | 
					
						
							|  |  |  |         # - id (String) | 
					
						
							|  |  |  |         # - deploy_env (String) | 
					
						
							|  |  |  |         # - stack_template (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) | 
					
						
							| 
									
										
										
										
											2015-07-20 18:59:26 +03:00
										 |  |  |           model.sync_details! | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |           model | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def build_from_bson(attrs) | 
					
						
							|  |  |  |           attrs['id'] = attrs["_id"] | 
					
						
							|  |  |  |           self.new(attrs) | 
					
						
							|  |  |  |         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 |