Sha256: 834fa0c58b2ea37217b7cdbd1b6b4c6087c07470100d459ffbe4e922bdf77d6d

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

require 'json'
require 'open-uri'

module Lono::Cfn
  class Download < Base
    def run
      pretty_path = download_path.sub("#{Lono.root}/", '')
      logger.info "Downloading template to: #{pretty_path}"
      download_template
    end

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

    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

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

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

    def download_path
      "#{Lono.root}/output/#{@blueprint.name}/templates/#{@blueprint.name}.yml"
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lono-8.0.0.pre.rc6 lib/lono/cfn/download.rb
lono-8.0.0.pre.rc5 lib/lono/cfn/download.rb
lono-8.0.0.pre.rc4 lib/lono/cfn/download.rb
lono-8.0.0.pre.rc3 lib/lono/cfn/download.rb