fluke/devops-service/db/mongo/models/stack_template/stack_template_ec2.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

2015-03-06 12:20:30 +03:00
module Devops
module Model
class StackTemplateEc2 < StackTemplateBase
2015-02-12 13:01:05 +03:00
# 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
2015-03-06 12:20:30 +03:00
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
2015-03-06 12:20:30 +03:00
private
2015-02-12 13:01:05 +03:00
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
2015-03-06 12:20:30 +03:00
end
def upload_file_to_storage(filename, file_path)
"https://s3.amazonaws.com/#{filename}"
end
2015-03-06 12:20:30 +03:00
end
2015-02-12 13:01:05 +03:00
end
end
end