| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | require "db/mongo/models/deploy_env/deploy_env_factory" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "db/mongo/models/user" | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | require "db/mongo/models/deploy_env/deploy_env_multi" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "db/mongo/models/mongo_model" | 
					
						
							|  |  |  | require "json" | 
					
						
							| 
									
										
										
										
											2015-07-27 15:56:30 +03:00
										 |  |  | require "hooks" | 
					
						
							| 
									
										
										
										
											2015-08-03 11:05:18 +03:00
										 |  |  | require "lib/project/type/types_factory" | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  | require "db/validators/deploy_env/deploy_envs" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							|  |  |  |     class Project < MongoModel | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  |       include Hooks | 
					
						
							|  |  |  |       include Hooks::InstanceHooks | 
					
						
							|  |  |  |       define_hook :before_create | 
					
						
							|  |  |  |       define_hook :after_create | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-01 17:22:38 +03:00
										 |  |  |       define_hook :before_delete | 
					
						
							|  |  |  |       define_hook :after_delete | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  |       #define_hook :after_add_deploy_env | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |       attr_accessor :id, :deploy_envs, :type, :archived, :description, :run_list | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       types :id => {:type => String, :empty => false}, | 
					
						
							|  |  |  |             :deploy_envs => {:type => Array, :value_type => false, :empty => false}, | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |             :description => {:type => String, :empty => true, :nil => true}, | 
					
						
							|  |  |  |             :run_list => {:type => Array, :value_type => String, :empty => true, :nil => false} | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       MULTI_TYPE = "multi" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  |       set_validators ::Validators::DeployEnv::RunList, | 
					
						
							|  |  |  |                      ::Validators::DeployEnv::DeployEnvs | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def self.fields | 
					
						
							|  |  |  |         ["deploy_envs", "type", "description"] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-08-26 10:45:47 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def initialize p={} | 
					
						
							|  |  |  |         self.id = p["name"] | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |         #raise InvalidRecord.new "No deploy environments for project #{self.id}" if p["deploy_envs"].nil? or p["deploy_envs"].empty? | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  |         self.type = p["type"] || Devops::GenericType.name | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         self.description = p["description"] | 
					
						
							|  |  |  |         self.archived = p["archived"] || false | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |         self.run_list = p["run_list"] || [] | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 14:05:14 +03:00
										 |  |  |         @handler = Devops::TypesFactory.type self.type | 
					
						
							|  |  |  |         @handler.prepare(self) | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         env_class = ( self.multi? ? DeployEnvMulti : DeployEnvFactory ) | 
					
						
							|  |  |  |         unless p["deploy_envs"].nil? | 
					
						
							|  |  |  |           self.deploy_envs = [] | 
					
						
							|  |  |  |           p["deploy_envs"].each do |e| | 
					
						
							|  |  |  |             env = env_class.create(e) | 
					
						
							|  |  |  |             self.deploy_envs.push env | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-08-26 10:45:47 +04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def deploy_env env | 
					
						
							|  |  |  |         de = self.deploy_envs.detect {|e| e.identifier == env} | 
					
						
							|  |  |  |         raise RecordNotFound.new("Project '#{self.id}' does not have deploy environment '#{env}'") if de.nil? | 
					
						
							|  |  |  |         de | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:47:18 +03:00
										 |  |  |       def add_deploy_env deploy_env | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  |         res = deploy_env.create_role(self.id) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:47:18 +03:00
										 |  |  |         Devops::Db.connector.add_deploy_env_to_project self.id, deploy_env | 
					
						
							| 
									
										
										
										
											2015-10-06 13:50:26 +03:00
										 |  |  |         Project.create_roles_response(res) | 
					
						
							| 
									
										
										
										
											2015-08-14 17:47:18 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def add_authorized_user user, env=nil | 
					
						
							|  |  |  |         return if user.nil? | 
					
						
							|  |  |  |         new_users = ( user.is_a?(Array) ? user : [ user ] ) | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |         environments = env.nil? ? self.deploy_envs : [ self.deploy_env(env) ] | 
					
						
							|  |  |  |         environments .each do |e| | 
					
						
							| 
									
										
										
										
											2015-09-22 14:30:52 +03:00
										 |  |  |           e.add_users new_users | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |           Devops::Db.connector.set_project_deploy_env_field(self.id, e.identifier, {users: e.users}) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def remove_authorized_user user, env=nil | 
					
						
							|  |  |  |         return if user.nil? | 
					
						
							|  |  |  |         users = ( user.is_a?(Array) ? user : [ user ] ) | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |         environments = env.nil? ? self.deploy_envs : [ self.deploy_env(env) ] | 
					
						
							|  |  |  |         environments.each do |e| | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |           e.users = e.users - users | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |           Devops::Db.connector.set_project_deploy_env_field(self.id, e.identifier, {users: e.users}) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def check_authorization user_id, env | 
					
						
							|  |  |  |         e = self.deploy_env(env) | 
					
						
							|  |  |  |         return true if user_id == User::ROOT_USER_NAME | 
					
						
							|  |  |  |         return e.users.include? user_id | 
					
						
							|  |  |  |       rescue RecordNotFound => e | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def validate! | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |         check_name_value(self.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       rescue InvalidRecord, ArgumentError => e | 
					
						
							|  |  |  |         raise InvalidRecord.new "Project '#{self.id}'. #{e.message}" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:20 +03:00
										 |  |  |       def delete_deploy_env env | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         self.deploy_envs.delete_if {|e| e.identifier == env} | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:20 +03:00
										 |  |  |         Devops::Db.connector.remove_deploy_env_from_project self.id, env | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:20 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def add_env env | 
					
						
							|  |  |  |         raise InvalidRecord.new "Deploy environment '#{env.identifier}' for project '#{self.id}' already exist" unless self.deploy_env(env.identifier).nil? | 
					
						
							|  |  |  |         self.deploy_envs.push env | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:20 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def to_hash | 
					
						
							|  |  |  |         h = self.to_hash_without_id | 
					
						
							|  |  |  |         h["name"] = self.id | 
					
						
							|  |  |  |         h | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  |       def to_hash_list | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: self.id, | 
					
						
							|  |  |  |           description: self.description | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 14:16:56 +03:00
										 |  |  |       def deploy_info deploy_env, build_number=nil | 
					
						
							| 
									
										
										
										
											2015-08-14 13:28:03 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2015-08-21 15:17:42 +03:00
										 |  |  |           "use_json_file" => true, | 
					
						
							|  |  |  | #          "run_list" => Set.new.merge(self.run_list).merge(deploy_env.run_list).to_a, | 
					
						
							| 
									
										
										
										
											2015-08-14 13:28:03 +03:00
										 |  |  |           "project" => self.id, | 
					
						
							|  |  |  |           "project_info" => { | 
					
						
							|  |  |  |             "project" => self.id, | 
					
						
							|  |  |  |             "deploy_env" => deploy_env.identifier, | 
					
						
							|  |  |  |             "deployers" => deploy_env.users | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def to_hash_without_id | 
					
						
							|  |  |  |         h = {} | 
					
						
							|  |  |  |         h["deploy_envs"] = self.deploy_envs.map {|e| e.to_hash} unless self.deploy_envs.nil? | 
					
						
							| 
									
										
										
										
											2015-07-16 16:01:53 +03:00
										 |  |  |         h["archived"] = self.archived if self.archived | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         h["description"] = self.description | 
					
						
							| 
									
										
										
										
											2015-08-10 11:35:03 +03:00
										 |  |  |         h["run_list"] = self.run_list | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         if self.multi? | 
					
						
							|  |  |  |           h["type"] = MULTI_TYPE | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         h | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def multi? | 
					
						
							|  |  |  |         self.type == MULTI_TYPE | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-01 17:22:38 +03:00
										 |  |  |       def create | 
					
						
							|  |  |  |         res = {} | 
					
						
							|  |  |  |         res[:before] = self.run_hook :before_create | 
					
						
							|  |  |  |         Devops::Db.connector.project_insert self | 
					
						
							|  |  |  |         res[:after] = self.run_hook :after_create | 
					
						
							|  |  |  |         res | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def delete | 
					
						
							|  |  |  |         res = {} | 
					
						
							|  |  |  |         res[:before] = self.run_hook :before_delete | 
					
						
							|  |  |  |         Devops::Db.connector.project_delete self.id | 
					
						
							|  |  |  |         res[:after] = self.run_hook :after_delete | 
					
						
							|  |  |  |         res | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 14:05:14 +03:00
										 |  |  |       def extract_servers env, params | 
					
						
							|  |  |  |         @handler.extract_servers(self, env, params) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def self.build_from_bson p | 
					
						
							|  |  |  |         p["name"] = p["_id"] | 
					
						
							|  |  |  |         Project.new p | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:47:11 +03:00
										 |  |  |       #TODO: create validator | 
					
						
							|  |  |  |       def validate_components | 
					
						
							|  |  |  |         raise InvalidRecord.new "Components is not a hash" unless self.components.is_a?(Hash) | 
					
						
							|  |  |  |         self.components.each do |id, c| | 
					
						
							|  |  |  |           raise InvalidRecord.new "Component '#{id}' has no attribute 'filename'" unless c.key?("filename") | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 13:50:26 +03:00
										 |  |  |       def self.create_roles_response roles | 
					
						
							| 
									
										
										
										
											2015-11-25 18:07:06 +03:00
										 |  |  |         if !roles || roles.is_a?(String) | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  |           roles | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           info = "" | 
					
						
							| 
									
										
										
										
											2015-10-09 14:09:37 +03:00
										 |  |  |           info += " Project roles '#{roles[:new].join("', '")}' have been automaticaly created" unless roles[:new].nil? | 
					
						
							|  |  |  |           info += " Project roles '#{roles[:exist].join("', '")}' weren't created because they exist" unless roles[:exist].nil? | 
					
						
							|  |  |  |           info += " Project roles '#{roles[:error].join("', '")}' weren't created because of internal error" unless roles[:error].nil? | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  |           info | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | end |