| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | require 'tempfile' | 
					
						
							|  |  |  | require 'securerandom' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							|  |  |  |     class StackTemplateBase < MongoModel | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       attr_accessor :id, :template_url, :template_json, :provider | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       # Few words about template_url: | 
					
						
							|  |  |  |       # In Amazon Cloudformation the template file must be stored on an Amazon S3 bucket, | 
					
						
							|  |  |  |       # but for Openstack stacks it isn't neccessary (your may use local file). | 
					
						
							|  |  |  |       # I decided to enforce template_url strategy using in openstack to reach more common | 
					
						
							|  |  |  |       # interface between different providers' stack templates. | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       types id: {type: String, empty: false}, | 
					
						
							|  |  |  |         provider: {type: String, empty: false}, | 
					
						
							|  |  |  |         template_json: {type: String, empty: false}, | 
					
						
							|  |  |  |         template_url: {type: String, empty: false} | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def initialize(attrs) | 
					
						
							|  |  |  |         self.id = attrs['id'] | 
					
						
							|  |  |  |         self.template_json = attrs['template_json'].to_s | 
					
						
							|  |  |  |         self.template_url = attrs['template_url'] | 
					
						
							|  |  |  |         self.provider = attrs['provider'] | 
					
						
							|  |  |  |         self | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def to_hash_without_id | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           provider: provider, | 
					
						
							|  |  |  |           template_json: template_json, | 
					
						
							|  |  |  |           template_url: template_url | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       # do not forget to destroy template files on template destroying | 
					
						
							|  |  |  |       def delete_template_file_from_storage | 
					
						
							|  |  |  |         raise 'Override me' | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       # attrs should include: | 
					
						
							|  |  |  |       # - id (String) | 
					
						
							|  |  |  |       # - provider (String) | 
					
						
							|  |  |  |       # - template_json (String) | 
					
						
							|  |  |  |       def self.create(attrs) | 
					
						
							|  |  |  |         json = attrs['template_json'] | 
					
						
							|  |  |  |         attrs['template_url'] = generate_template_file_and_upload_to_storage(attrs['id'], json) | 
					
						
							|  |  |  |         new(attrs) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       def self.build_from_bson(attrs) | 
					
						
							|  |  |  |         attrs['id'] = attrs["_id"] | 
					
						
							|  |  |  |         self.new(attrs) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       class << self | 
					
						
							|  |  |  |         private | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         def generate_template_file_and_upload_to_storage(id, json) | 
					
						
							|  |  |  |           tempfile = Tempfile.new('foo') | 
					
						
							|  |  |  |           tempfile.write(json) | 
					
						
							|  |  |  |           secure_filename = "#{id}-#{SecureRandom.hex}.template" | 
					
						
							|  |  |  |           upload_file_to_storage(secure_filename, tempfile.path) | 
					
						
							|  |  |  |         ensure | 
					
						
							|  |  |  |           tempfile.close | 
					
						
							|  |  |  |           tempfile.unlink | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |         def upload_file_to_storage(filename, file_path) | 
					
						
							|  |  |  |           raise 'Override me' | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |