fluke/devops-service/db/mongo/models/stack_template/stack_template_ec2.rb
2016-01-20 14:49:30 +03:00

50 lines
1.2 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
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