54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| module Devops
 | |
|   module Model
 | |
|     class StackTemplateEc2 < StackTemplateBase
 | |
| 
 | |
|       # In Amazon Cloudformation the template file must be stored on an Amazon S3 bucket.
 | |
|       attr_accessor :template_url
 | |
| 
 | |
|       types template_url: {type: String, empty: false}
 | |
| 
 | |
|       def initialize(attrs)
 | |
|         self.template_url = attrs['template_url']
 | |
|         super(attrs)
 | |
|       end
 | |
| 
 | |
|       def to_hash_without_id
 | |
|         super.merge(template_url: template_url)
 | |
|       end
 | |
| 
 | |
|       def delete_template_file_from_storage
 | |
|         raise 'Implement me'
 | |
|       end
 | |
| 
 | |
|       class << self
 | |
| 
 | |
|         def create(attrs)
 | |
|           template = attrs['template_body']
 | |
|           attrs['template_url'] = generate_template_file_and_upload_to_storage(attrs['id'], template)
 | |
|           super(attrs)
 | |
|         end
 | |
| 
 | |
|         private
 | |
| 
 | |
|         def generate_template_file_and_upload_to_storage(id, json)
 | |
|           begin
 | |
|             tempfile = Tempfile.new('foo')
 | |
|             tempfile.write(json)
 | |
|             tempfile.close
 | |
|             secure_filename = "#{id}-#{SecureRandom.hex}.template"
 | |
|             upload_file_to_storage(secure_filename, tempfile.path)
 | |
|           ensure
 | |
|             tempfile.unlink
 | |
|           end
 | |
|         end
 | |
| 
 | |
|         def upload_file_to_storage(filename, file_path)
 | |
|           "https://s3.amazonaws.com/#{filename}"
 | |
|         end
 | |
| 
 | |
|       end
 | |
| 
 | |
|     end
 | |
|   end
 | |
| end
 | 
