| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     class Key | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       include ::Mongoid::Document | 
					
						
							|  |  |  |       include ::Mongoid::Timestamps::CreatedInt | 
					
						
							|  |  |  |       include ::ActiveModel::Validations | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       store_in collection: "keys" | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       field :_id, as: :'id', overwrite: true, type: String | 
					
						
							|  |  |  |       field :path, type: String | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | #      validates_presence_of :id, message: "'id' is undefined" | 
					
						
							|  |  |  |       validates_length_of :id, maximum: 100
 | 
					
						
							|  |  |  |       validates_format_of :id, with: /\A[a-z_][a-z0-9_-]{0,99}\z/, message: "invalid id" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       before_destroy :delete_file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def filename | 
					
						
							|  |  |  |         File.basename(self.path) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       def to_list_hash | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           "id" => self.id, | 
					
						
							|  |  |  |           "filename" => self.filename, | 
					
						
							|  |  |  |           "created_at" => self.created_at | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       private | 
					
						
							|  |  |  |       def delete_file | 
					
						
							|  |  |  |         FileUtils.rm(self.path) | 
					
						
							|  |  |  |         DevopsLogger.logger.info("File '#{self.path}' has been removed") | 
					
						
							|  |  |  |       rescue | 
					
						
							|  |  |  |         DevopsLogger.logger.error "Missing key file for #{key_id} - #{k.filename}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | end |