storing templates in s3
This commit is contained in:
parent
c016d777e2
commit
b46dffe3fc
@ -14,7 +14,6 @@ class StackTemplate < Handler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle
|
def handle
|
||||||
current_command = ARGV[1].to_sym
|
|
||||||
@options, @args = @options_parser.parse_options_for!(current_command)
|
@options, @args = @options_parser.parse_options_for!(current_command)
|
||||||
case current_command
|
case current_command
|
||||||
when :list
|
when :list
|
||||||
@ -27,6 +26,8 @@ class StackTemplate < Handler
|
|||||||
create_handler
|
create_handler
|
||||||
when :delete
|
when :delete
|
||||||
delete_handler
|
delete_handler
|
||||||
|
when :update_url
|
||||||
|
update_url_handler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,6 +77,16 @@ class StackTemplate < Handler
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_url_handler
|
||||||
|
r = inspect_parameters @options_parser.update_url_params, @args[2]
|
||||||
|
unless r.nil?
|
||||||
|
@options_parser.invalid_update_url_command
|
||||||
|
abort(r)
|
||||||
|
end
|
||||||
|
stack_template = post "/stack_template/#{@args[2]}/update_template_url"
|
||||||
|
puts stack_template['template_url']
|
||||||
|
end
|
||||||
|
|
||||||
def provider_stack_templates(provider)
|
def provider_stack_templates(provider)
|
||||||
if Providers.has_functionality?(provider, :stack_templates)
|
if Providers.has_functionality?(provider, :stack_templates)
|
||||||
@provider = true
|
@provider = true
|
||||||
|
|||||||
@ -2,7 +2,7 @@ require "devops-client/options/common_options"
|
|||||||
|
|
||||||
class StackTemplateOptions < CommonOptions
|
class StackTemplateOptions < CommonOptions
|
||||||
|
|
||||||
commands :create, :delete, :list, :show
|
commands :create, :delete, :list, :show, :update_url
|
||||||
|
|
||||||
def initialize args, def_options
|
def initialize args, def_options
|
||||||
super(args, def_options)
|
super(args, def_options)
|
||||||
@ -11,6 +11,7 @@ class StackTemplateOptions < CommonOptions
|
|||||||
self.list_params = ["[provider]", "[ec2|openstack]"]
|
self.list_params = ["[provider]", "[ec2|openstack]"]
|
||||||
self.show_params = ["STACK_TEMPLATE"]
|
self.show_params = ["STACK_TEMPLATE"]
|
||||||
self.delete_params = ["STACK_TEMPLATE"]
|
self.delete_params = ["STACK_TEMPLATE"]
|
||||||
|
self.update_url_params = ["STACK_TEMPLATE"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_options
|
def create_options
|
||||||
|
|||||||
@ -39,6 +39,14 @@ module Devops
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# temp solution to update url on existing stacks
|
||||||
|
def update_template_url(id)
|
||||||
|
template = Devops::Db.connector.stack_template(id)
|
||||||
|
template.update_template_url
|
||||||
|
Devops::Db.connector.stack_template_update(template)
|
||||||
|
template
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# returns:
|
# returns:
|
||||||
|
|||||||
@ -22,6 +22,11 @@ module Devops
|
|||||||
create_response 'Created', model.to_hash, 201
|
create_response 'Created', model.to_hash, 201
|
||||||
end
|
end
|
||||||
|
|
||||||
|
app.post_with_headers "/stack_template/:id/update_template_url", :headers => [:accept] do |template_id|
|
||||||
|
check_privileges('stack_template', 'w')
|
||||||
|
json Devops::API2_0::Handler::StackTemplate.new(request).update_template_url(template_id).to_hash
|
||||||
|
end
|
||||||
|
|
||||||
hash = {}
|
hash = {}
|
||||||
|
|
||||||
hash['GET'] = lambda {|stack_template_id|
|
hash['GET'] = lambda {|stack_template_id|
|
||||||
|
|||||||
@ -37,6 +37,7 @@ config[:aws_certificate] = "/path/to/.ssh/ec2.pem"
|
|||||||
config[:aws_availability_zone] = "aws_zone"
|
config[:aws_availability_zone] = "aws_zone"
|
||||||
config[:aws_proxy] = ""
|
config[:aws_proxy] = ""
|
||||||
config[:aws_no_proxy] = ""
|
config[:aws_no_proxy] = ""
|
||||||
|
config[:aws_stack_templates_bucket] = 'stacktemplates'
|
||||||
|
|
||||||
# static settings
|
# static settings
|
||||||
config[:static_ssh_key] = "ssh_key" # or nil
|
config[:static_ssh_key] = "ssh_key" # or nil
|
||||||
@ -46,4 +47,4 @@ config[:debug] = true
|
|||||||
|
|
||||||
# set it to :all or [:ec2] to stub calls to selected providers
|
# set it to :all or [:ec2] to stub calls to selected providers
|
||||||
# or to false to disable stubbing
|
# or to false to disable stubbing
|
||||||
config[:stub_providers] = false
|
config[:stub_providers] = false
|
||||||
@ -131,7 +131,11 @@ module Devops
|
|||||||
end
|
end
|
||||||
|
|
||||||
def template_body
|
def template_body
|
||||||
Devops::Db.connector.stack_template(stack_template).template_body
|
stack_template_model.template_body
|
||||||
|
end
|
||||||
|
|
||||||
|
def stack_template_model
|
||||||
|
Devops::Db.connector.stack_template(stack_template)
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|||||||
@ -20,34 +20,25 @@ module Devops
|
|||||||
raise 'Implement me'
|
raise 'Implement me'
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
def update_template_url
|
||||||
|
self.template_url = generate_template_file_and_upload_to_storage(id, template_body)
|
||||||
|
end
|
||||||
|
|
||||||
|
class << self
|
||||||
def create(attrs)
|
def create(attrs)
|
||||||
template = attrs['template_body']
|
template = attrs['template_body']
|
||||||
attrs['template_url'] = generate_template_file_and_upload_to_storage(attrs['id'], template)
|
attrs['template_url'] = generate_template_file_and_upload_to_storage(attrs['id'], template)
|
||||||
super(attrs)
|
super(attrs)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def generate_template_file_and_upload_to_storage(id, json)
|
def generate_template_file_and_upload_to_storage(id, json)
|
||||||
begin
|
uniq_filename = "#{id}-#{SecureRandom.hex}.template"
|
||||||
tempfile = Tempfile.new('foo')
|
provider_instance.store_file(uniq_filename, json)['url']
|
||||||
tempfile.write(json)
|
|
||||||
tempfile.close
|
|
||||||
secure_filename = "#{id}-#{SecureRandom.hex}.template"
|
|
||||||
upload_file_to_storage(secure_filename, tempfile.path)
|
|
||||||
ensure
|
|
||||||
tempfile.unlink
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def upload_file_to_storage(filename, file_path)
|
|
||||||
"https://s3.amazonaws.com/#{filename}"
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -222,7 +222,7 @@ module Provider
|
|||||||
out.flush
|
out.flush
|
||||||
response = cloud_formation.create_stack(stack.name,
|
response = cloud_formation.create_stack(stack.name,
|
||||||
{
|
{
|
||||||
'TemplateBody' => stack.template_body,
|
'TemplateURL' => stack.stack_template_model.template_url,
|
||||||
'Parameters' => stack.parameters || {},
|
'Parameters' => stack.parameters || {},
|
||||||
'Capabilities' => ['CAPABILITY_IAM'],
|
'Capabilities' => ['CAPABILITY_IAM'],
|
||||||
'Tags' => stack_tags(stack)
|
'Tags' => stack_tags(stack)
|
||||||
@ -319,6 +319,15 @@ module Provider
|
|||||||
def describe_vpcs
|
def describe_vpcs
|
||||||
self.compute.describe_vpcs.body["vpcSet"].select{|v| v["state"] == "available"}.map{|v| {"vpc_id" => v["vpcId"], "cidr" => v["cidrBlock"] } }
|
self.compute.describe_vpcs.body["vpcSet"].select{|v| v["state"] == "available"}.map{|v| {"vpc_id" => v["vpcId"], "cidr" => v["cidrBlock"] } }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# use public_url for a while because there is no possibility to create
|
||||||
|
# non expiring private links via fog
|
||||||
|
def store_file(filename, json)
|
||||||
|
{
|
||||||
|
'url' => stack_templates_bucket.files.create(key: filename, body: json, public: true).public_url
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def convert_groups list
|
def convert_groups list
|
||||||
@ -375,6 +384,16 @@ module Provider
|
|||||||
@orchestration ||= Fog::AWS::CloudFormation.new(connection_options)
|
@orchestration ||= Fog::AWS::CloudFormation.new(connection_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def storage
|
||||||
|
@storage ||= Fog::Storage.new(connection_options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def stack_templates_bucket
|
||||||
|
bucket_name = DevopsConfig.config[:aws_stack_templates_bucket] || 'stacktemplates'
|
||||||
|
bucket = storage.directories.get(bucket_name)
|
||||||
|
bucket ||= storage.directories.create(key: bucket_name)
|
||||||
|
end
|
||||||
|
|
||||||
def instance_name(instance)
|
def instance_name(instance)
|
||||||
return instance["tagSet"]["Name"] if instance["tagSet"]["Name"]
|
return instance["tagSet"]["Name"] if instance["tagSet"]["Name"]
|
||||||
if instance['tagSet']['aws:autoscaling:groupName']
|
if instance['tagSet']['aws:autoscaling:groupName']
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user