Class: Lono::Cfn::Download

Inherits:
AbstractBase show all
Includes:
AwsServices
Defined in:
lib/lono/cfn/download.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts

Methods included from AwsServices::Helper

#rollback_complete?, #testing_update?

Methods included from AwsServices::StackSet

#find_stack_set, #stack_set_exists?

Methods included from AwsServices::Stack

#find_stack, #stack_exists?

Methods inherited from AbstractBase

#initialize, #reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::AbstractBase

Instance Method Details

#convert_to_yaml(body) ⇒ Object



35
36
37
# File 'lib/lono/cfn/download.rb', line 35

def convert_to_yaml(body)
  json?(body) ? YAML.dump(JSON.parse(body)) : body
end

#download_pathObject



43
44
45
# File 'lib/lono/cfn/download.rb', line 43

def download_path
  "#{Lono.config.output_path}/#{@blueprint}/templates/#{@blueprint}.yml"
end

#download_stackObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lono/cfn/download.rb', line 22

def download_stack
  source = @options[:source]
  if source
    open(source).read # url or file
  else
    resp = cfn.get_template(
      stack_name: @stack,
      template_stage: "Original"
    )
    resp.template_body
  end
end

#download_templateObject



15
16
17
18
19
20
# File 'lib/lono/cfn/download.rb', line 15

def download_template
  body = download_stack
  body = convert_to_yaml(body)
  FileUtils.mkdir_p(File.dirname(download_path))
  IO.write(download_path, body)
end

#json?(body) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/lono/cfn/download.rb', line 39

def json?(body)
  !!JSON.parse(body) rescue false
end

#nameObject



47
48
49
# File 'lib/lono/cfn/download.rb', line 47

def name
  @options[:name] || @stack
end

#runObject



8
9
10
11
12
13
# File 'lib/lono/cfn/download.rb', line 8

def run
  pretty_path = download_path.sub("#{Lono.root}/", '')
  puts "Downloading template to: #{pretty_path}"
  return if @options[:noop]
  download_template
end