| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "db/exceptions/invalid_record" | 
					
						
							|  |  |  | require "db/mongo/models/mongo_model" | 
					
						
							|  |  |  | require "commands/image" | 
					
						
							| 
									
										
										
										
											2014-07-08 11:48:23 +04:00
										 |  |  | require "commands/bootstrap_templates" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Image < MongoModel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   include ImageCommands | 
					
						
							| 
									
										
										
										
											2014-07-08 11:48:23 +04:00
										 |  |  |   include BootstrapTemplatesCommands | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   attr_accessor :id, :provider, :remote_user, :name, :bootstrap_template | 
					
						
							|  |  |  |   types :id => {:type => String, :empty => false}, | 
					
						
							|  |  |  |         :provider => {:type => String, :empty => false}, | 
					
						
							|  |  |  |         :remote_user => {:type => String, :empty => false}, | 
					
						
							|  |  |  |         :name => {:type => String, :empty => true}, | 
					
						
							|  |  |  |         :bootstrap_template => {:type => String, :empty => false, :nil => true} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def validate! | 
					
						
							|  |  |  |     super | 
					
						
							|  |  |  |     images = get_images(DevopsService.mongo, self.provider) | 
					
						
							|  |  |  |     raise InvalidRecord.new "Invalid image id '#{self.id}' for provider '#{self.provider}', please check image filters" unless images.map{|i| i["id"]}.include?(self.id) | 
					
						
							| 
									
										
										
										
											2014-07-08 11:48:23 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if self.bootstrap_template | 
					
						
							|  |  |  |       templates = get_templates | 
					
						
							|  |  |  |       raise InvalidRecord.new "Invalid bootstrap template '#{self.bootstrap_template}' for image '#{self.id}'" unless templates.include?(self.bootstrap_template) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def initialize p={} | 
					
						
							|  |  |  |     self.id = p["id"] | 
					
						
							|  |  |  |     self.provider = p["provider"] | 
					
						
							|  |  |  |     self.remote_user = p["remote_user"] | 
					
						
							|  |  |  |     self.name = p["name"] || "" | 
					
						
							|  |  |  |     self.bootstrap_template = p["bootstrap_template"] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.create_from_bson args | 
					
						
							|  |  |  |     image = Image.new(args) | 
					
						
							|  |  |  |     image.id = args["_id"] | 
					
						
							|  |  |  |     image | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def to_hash_without_id | 
					
						
							|  |  |  |     o = { | 
					
						
							|  |  |  |       "provider" => self.provider, | 
					
						
							|  |  |  |       "name" => self.name, | 
					
						
							|  |  |  |       "remote_user" => self.remote_user | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     o["bootstrap_template"] = self.bootstrap_template | 
					
						
							|  |  |  |     o | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.create_from_json! json | 
					
						
							|  |  |  |     Image.new( JSON.parse(json) ) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end |