| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "db/mongo/models/environment/environment" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "db/mongo/models/user" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "db/mongo/models/environment/environments_array" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 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" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "exceptions/invalid_record" | 
					
						
							|  |  |  | require "exceptions/record_not_found" | 
					
						
							|  |  |  | #require "db/validators/environment/environments" | 
					
						
							|  |  |  | require "db/validators/project/named_tasks" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     class Project | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       include ::Mongoid::Document | 
					
						
							|  |  |  |       include ::Mongoid::Timestamps::CreatedInt | 
					
						
							|  |  |  |       include ::ActiveModel::Validations | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       #attr_accessor :components | 
					
						
							|  |  |  |       store_in collection: "projects" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       field :_id, as: :'id', overwrite: true, type: String | 
					
						
							|  |  |  |       field :environments, type: EnvironmentsArray, default: ->{ [] } | 
					
						
							|  |  |  |       field :type, type: String | 
					
						
							|  |  |  |       field :owner, type: String | 
					
						
							|  |  |  |       field :project_users, type: Array, default: ->{ [] } | 
					
						
							|  |  |  |       field :archived, type: Boolean | 
					
						
							|  |  |  |       field :description, type: String | 
					
						
							|  |  |  |       field :run_list, type: Array, default: ->{ [] } | 
					
						
							|  |  |  |       field :named_tasks, type: Array, default: ->{ [] } | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       MAX_ID_LEN = 100
 | 
					
						
							|  |  |  |       ID_REGEX = /\A[\w\-\.]{1,#{MAX_ID_LEN}}\z/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       validates_presence_of :id, message: "'id' is undefined" | 
					
						
							|  |  |  |       validates_length_of :id, minimum: 1, maximum: MAX_ID_LEN | 
					
						
							|  |  |  |       validates_format_of :id, with: ID_REGEX, message: "invalid id, it should contains 'a-zA-Z0-9-.' symbols" | 
					
						
							|  |  |  |       validates_uniqueness_of :id, on: :create | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       validates_length_of :description, maximum: 500
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       validates_presence_of :owner, message: "'owner' is undefined" | 
					
						
							|  |  |  |       validates :named_tasks, proper_named_tasks_format: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =begin | 
					
						
							|  |  |  | TODO: | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |       set_field_validators :named_tasks, [::Validators::FieldValidator::Nil, | 
					
						
							|  |  |  |                                     ::Validators::FieldValidator::FieldType::Array, | 
					
						
							|  |  |  |                                     ::Validators::FieldValidator::NamedTasks] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  |       set_validators ::Validators::DeployEnv::RunList, | 
					
						
							|  |  |  |                      ::Validators::DeployEnv::DeployEnvs | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | =end | 
					
						
							|  |  |  |       validates_length_of :description, maximum: 500
 | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       #TODO:  self.named_tasks = p["named_tasks"] || [] | 
					
						
							|  |  |  |       def initialize hash | 
					
						
							|  |  |  |         raise InvalidRecord.new("Parameter 'id' is not a string") unless hash["id"].is_a?(String) | 
					
						
							|  |  |  |         raise InvalidRecord.new("Parameter 'environments' is not an array") unless hash["environments"].is_a?(Array) or hash["environments"].nil? | 
					
						
							|  |  |  |         raise InvalidRecord.new("Parameter 'run_list' is not an array") unless hash["run_list"].is_a?(Array) or hash["run_list"].nil? | 
					
						
							|  |  |  |         raise InvalidRecord.new("Parameter 'project_users' is not an array") unless hash["project_users"].is_a?(Array) or hash["project_users"].nil? | 
					
						
							|  |  |  |         super(hash) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def self.find_with_environment id, environment | 
					
						
							|  |  |  |         Project.find_by({_id: id, environments:{'$elemMatch' => {id: environment}}}) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-08-26 10:45:47 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def self.find_with_category id, environment, category | 
					
						
							|  |  |  |         Project.find_by( | 
					
						
							|  |  |  |           _id: id, | 
					
						
							|  |  |  |           environments: { | 
					
						
							|  |  |  |             '$elemMatch' => { | 
					
						
							|  |  |  |               id: environment, | 
					
						
							|  |  |  |               categories: {'$elemMatch' => {id: category}} | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       # TODO: extract to handler or parser | 
					
						
							|  |  |  |       def self.list_fields | 
					
						
							|  |  |  |         ['deploy_envs', 'type', 'description', 'named_tasks'] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-07-01 17:14:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def self.check_user_authorization project_id, env, user_id | 
					
						
							|  |  |  | #        find({'_id' => project_id}).projection({environments:{'$elemMatch' => {id:env}}}) | 
					
						
							|  |  |  |         project = find(project_id) | 
					
						
							|  |  |  |         if !project.check_authorization(user_id, env) | 
					
						
							|  |  |  |           raise Devops::Exception::Unauthorized.new("User '#{user_id}' is unauthorized to work with project '#{project_id}' and environment '#{env}'") | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         project | 
					
						
							|  |  |  |       rescue Mongoid::Errors::DocumentNotFound | 
					
						
							|  |  |  |         raise Devops::Exception::RecordNotFound.new("Project with id '#{project_id}' not found") | 
					
						
							| 
									
										
										
										
											2014-08-26 10:45:47 +04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def environment env | 
					
						
							|  |  |  |         de = self.environments.detect {|e| e.id == env} | 
					
						
							|  |  |  |         raise Devops::Exception::RecordNotFound.new("Project '#{self.id}' does not have deploy environment '#{env}'") if de.nil? | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         de | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def add_environment environment | 
					
						
							|  |  |  |         self.push(environments: environment.to_hash) | 
					
						
							|  |  |  |         DevopsLogger.logger.info("Added environment '#{environment.id}' to project '#{self.id}'") | 
					
						
							|  |  |  |         DevopsLogger.logger.debug("Added environment: #{environment.to_hash.inspect}") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def add_category env, category | 
					
						
							|  |  |  |         env.categories.push category | 
					
						
							|  |  |  |         category_hash = category.to_hash | 
					
						
							|  |  |  |         Project.where('_id' => self.id, 'environments.id' => env.id).push('environments.$.categories' => category_hash) | 
					
						
							|  |  |  |         DevopsLogger.logger.debug("Added category '#{category.id}' to project '#{self.id}' and environment '#{env.id}'") | 
					
						
							|  |  |  |         DevopsLogger.logger.debug("Added category: #{category_hash.inspect}") | 
					
						
							|  |  |  |         category | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def delete_category env, category_id | 
					
						
							|  |  |  |         bsize = env.categories.size | 
					
						
							|  |  |  |         env.categories.delete_if{|c| c.id == category_id} | 
					
						
							|  |  |  |         if bsize == env.categories.size | 
					
						
							|  |  |  |           raise Devops::Exception::RecordNotFound.new("Category '#{category_id}' in project '#{id}' and deploy environment '#{env}' does not exist") | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Project.where({'_id' => self.id, 'environments.id' => env.id}).pull('environments.$.categories' => {'id' => category_id}) | 
					
						
							|  |  |  |           DevopsLogger.logger.info("Category '#{category_id}' removed from project '#{self.id}' and environment '#{env.id}'") | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-08-14 17:47:18 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |       # user could be a String or an Array of strings. | 
					
						
							|  |  |  |       # if env is nil, add given user(s) to all project's envs, | 
					
						
							|  |  |  |       # otherwise only to specified one. | 
					
						
							| 
									
										
										
										
											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 ] ) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         environments = env.nil? ? self.environments : [ self.environment(env) ] | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |         environments.each do |e| | 
					
						
							| 
									
										
										
										
											2015-09-22 14:30:52 +03:00
										 |  |  |           e.add_users new_users | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           Devops::Db.connector.set_project_environment_field(self.id, e.id, {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 ] ) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         environments = env.nil? ? self.environments : [ self.environment(env) ] | 
					
						
							| 
									
										
										
										
											2015-11-25 12:39:01 +03:00
										 |  |  |         environments.each do |e| | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |           e.users = e.users - users | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           Devops::Db.connector.set_project_environment_field(self.id, e.id, {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 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         return true if user_id == self.owner or user_id == User::ROOT_USER_NAME | 
					
						
							| 
									
										
										
										
											2015-12-03 10:31:36 +03:00
										 |  |  |         return true if is_sandbox? | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         e = self.environment(env) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         return e.users.include? user_id | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       rescue Devops::Exception::RecordNotFound => e | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         return false | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 10:31:36 +03:00
										 |  |  |       def is_sandbox? | 
					
						
							|  |  |  |         id.start_with?('sandbox-') | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def delete_environment env | 
					
						
							|  |  |  |         self.environments.delete_if {|e| e.id == env} | 
					
						
							|  |  |  |         Project.where('_id' => self.id).pull('environments' => {'id' => env}) | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def to_hash | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         hash = self.attributes.clone | 
					
						
							|  |  |  |         hash["id"] = hash.delete("_id") | 
					
						
							|  |  |  |         hash.delete('archived') unless hash['archived'] | 
					
						
							|  |  |  |         hash | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 13:04:35 +03:00
										 |  |  |       # TODO: why symbols here? | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  |       def to_hash_list | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           id: self.id, | 
					
						
							|  |  |  |           description: self.description, | 
					
						
							|  |  |  |           owner: self.owner, | 
					
						
							|  |  |  |           run_list: self.run_list, | 
					
						
							|  |  |  |           project_users: self.project_users, | 
					
						
							|  |  |  |           created_at: self.created_at | 
					
						
							| 
									
										
										
										
											2015-08-13 15:49:37 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def deploy_info environment, build_number=nil | 
					
						
							| 
									
										
										
										
											2015-08-14 13:28:03 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           "use_json_file" => false, | 
					
						
							|  |  |  | #          "run_list" => Set.new.merge(self.run_list).merge(environment.run_list).to_a, | 
					
						
							| 
									
										
										
										
											2015-08-14 13:28:03 +03:00
										 |  |  |           "project" => self.id, | 
					
						
							|  |  |  |           "project_info" => { | 
					
						
							|  |  |  |             "project" => self.id, | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |             "environment" => environment.id, | 
					
						
							|  |  |  |             "deployers" => environment.users | 
					
						
							| 
									
										
										
										
											2015-08-14 13:28:03 +03:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-08-05 14:05:14 +03:00
										 |  |  |       def extract_servers env, params | 
					
						
							|  |  |  |         @handler.extract_servers(self, env, params) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 16:35:53 +03:00
										 |  |  |       # maybe it worth to move components functionality to devops-nibr? | 
					
						
							| 
									
										
										
										
											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| | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           raise InvalidRecord.new "Component '#{id}' has no attribute 'fileid'" unless c.key?("fileid") | 
					
						
							| 
									
										
										
										
											2015-09-14 17:47:11 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2015-09-14 17:47:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       # def self.create_roles_response roles | 
					
						
							|  |  |  |       #   return "error in creating roles" unless roles | 
					
						
							|  |  |  |       #   info = '' | 
					
						
							|  |  |  |       #   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? | 
					
						
							|  |  |  |       #   info | 
					
						
							|  |  |  |       # end | 
					
						
							| 
									
										
										
										
											2015-10-05 14:54:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | end |