| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require "db/mongo/models/model_with_provider_account" | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | require "providers/static" | 
					
						
							| 
									
										
										
										
											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 Server | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       include ::Mongoid::Document | 
					
						
							|  |  |  |       include ::Mongoid::Timestamps::CreatedInt | 
					
						
							|  |  |  |       include ::ActiveModel::Validations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       store_in collection: "servers" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       include ModelWithProviderAccount | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-09 18:57:22 +03:00
										 |  |  |       module OperationType | 
					
						
							| 
									
										
										
										
											2015-11-20 15:15:06 +03:00
										 |  |  |         # we store strings in mongo, so it's better not to use symbols | 
					
						
							|  |  |  |         CREATION = 'creation' | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         BOOTSTRAP = 'bootstrap' | 
					
						
							| 
									
										
										
										
											2015-11-20 15:15:06 +03:00
										 |  |  |         DEPLOY = 'deploy' | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         DEPLOY_INFO = 'deploy_info' | 
					
						
							| 
									
										
										
										
											2015-11-20 15:15:06 +03:00
										 |  |  |         RESERVE = 'reserve' | 
					
						
							|  |  |  |         UNRESERVE = 'unreserve' | 
					
						
							|  |  |  |         PAUSE = 'pause' | 
					
						
							|  |  |  |         UNPAUSE = 'unpause' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def self.supported_type?(value) | 
					
						
							|  |  |  |           return false unless value.is_a?(String) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           [CREATION, DEPLOY, RESERVE, UNRESERVE, PAUSE, UNPAUSE, BOOTSTRAP, DEPLOY_INFO].include?(value) | 
					
						
							| 
									
										
										
										
											2015-11-20 15:15:06 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-11-09 18:57:22 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       field :_id, as: :'id', overwrite: true, type: String | 
					
						
							|  |  |  |       field :name, type: String | 
					
						
							|  |  |  |       field :cm_name, type: String | 
					
						
							|  |  |  |       field :remote_user, type: String | 
					
						
							|  |  |  |       field :project, type: String | 
					
						
							|  |  |  |       field :environment, type: String | 
					
						
							|  |  |  |       field :category, type: String | 
					
						
							|  |  |  |       field :private_ip, type: String | 
					
						
							|  |  |  |       field :public_ip, type: String | 
					
						
							|  |  |  |       field :created_by, type: String | 
					
						
							|  |  |  |       field :reserved_by, type: String | 
					
						
							|  |  |  |       field :stack, type: String | 
					
						
							|  |  |  |       field :run_list, type: Array, default: [] | 
					
						
							|  |  |  |       field :ssh_key, type: String | 
					
						
							|  |  |  |       field :last_operation, type: Hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       validates :remote_user, :project, :environment, :category, :private_ip, :ssh_key, :created_by, | 
					
						
							|  |  |  |         :name, presence: true | 
					
						
							|  |  |  |       validates :run_list, runListArray: {allow_nil: true} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =begin | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def self.fields | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         ["name", "project", "environment", "provider", "remote_user", "private_ip", "public_ip", "created_at", "created_by", "key", "reserved_by", "run_list", "stack", | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |           "last_operation"] | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-08-26 10:09:30 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def initialize hash | 
					
						
							|  |  |  | #        raise InvalidRecord.new("Parameter 'id' is not a string") unless hash["id"].is_a?(String) | 
					
						
							|  |  |  |         super(hash) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def to_hash | 
					
						
							|  |  |  |         hash = self.attributes.clone | 
					
						
							|  |  |  |         hash["id"] = hash.delete("_id") | 
					
						
							|  |  |  |         hash | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def info | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         str = "Instance Name: #{self.name}\n" | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         str << "Instance ID: #{self.id}\n" | 
					
						
							|  |  |  |         str << "Private IP: #{self.private_ip}\n" | 
					
						
							|  |  |  |         str << "Public IP: #{self.public_ip}\n" unless self.public_ip.nil? | 
					
						
							|  |  |  |         str << "Remote user: #{self.remote_user}\n" | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         str << "Project: #{self.project}\n" | 
					
						
							|  |  |  |         str << "Environment: #{self.environment}\n" | 
					
						
							|  |  |  |         str << "Category: #{self.category}\n" | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         str << "Created by: #{self.created_by}" | 
					
						
							|  |  |  |         str | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def static? | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         self.provider == ::Provider::Static::PROVIDER | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-10-22 15:01:55 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def set_last_operation(operation_type, user, job_task) | 
					
						
							| 
									
										
										
										
											2015-11-20 15:15:06 +03:00
										 |  |  |         raise ArgumentError unless OperationType.supported_type?(operation_type) | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         self.last_operation = { | 
					
						
							|  |  |  |           'type' => operation_type, | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           'date' => Time.now.to_i, | 
					
						
							|  |  |  |           'user' => user, | 
					
						
							|  |  |  |           'job_task' => job_task | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         self.save | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.stack_servers(stack_id) | 
					
						
							|  |  |  |         where('stack' => stack_id) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.delete_stack_servers(stack_id) | 
					
						
							|  |  |  |         stack_servers(stack_id).delete | 
					
						
							| 
									
										
										
										
											2015-11-09 18:57:22 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | end |