Sha256: 77bc7c48773657bd166af4f74db6ba8fe03322f72707a490ce9034112fb38854

Contents?: true

Size: 1.9 KB

Versions: 15

Compression:

Stored size: 1.9 KB

Contents

module Jets::Cfn
  class Template
    include Jets::AwsServices

    attr_reader :path

    def initialize(path, options={})
      @path = path
      @options = options
    end

    def body
      @body ||= IO.read(path)
    end

    def url
      @url ||= upload_file_to_s3
    end

    def stack_option
      if upload_to_s3?
        from_s3
      else
        from_path
      end
    end

  private
    def upload_to_s3?
      return false if @options[:stack_type] == :minimal # bucket not yet available
      bucket_name.present?
    end

    def bucket_name
      Jets.s3_bucket
    end

    def from_s3
      {
        template_url: url
      }
    end

    def from_path
      {
        template_body: body
      }
    end

    def upload_file_to_s3
      obj = s3_resource.bucket(bucket_name).object(s3_key)
      obj.upload_file(path)

      "https://s3.amazonaws.com/#{bucket_name}/#{s3_key}"
    end

    def s3_key
      @s3_key ||= "jets/cfn-templates/#{File.basename(path)}"
    end

    class << self
      # Caches reduce filesystem IO calls
      @@cache = {}
      def load_file(path)
        if @@cache[path]
          @@cache[path]
        else
          @@cache[path] = Jets::Util::Yamler.load_file(path).deep_symbolize_keys
        end
      end

      # Jets::Cfn::Template.lookup_logical_id(template_name, key)
      # Jets::Cfn::Template.lookup_logical_id("api-resources", "UpApiResource")
      def lookup_logical_id(template_name, key)
        expr = "#{Jets::Names.templates_folder}/#{template_name}-*"
        template_paths = Dir.glob(expr).sort.to_a
        found_template = template_paths.detect do |path|
          next unless File.file?(path)

          template = Jets::Cfn::Template.load_file(path)
          template[:Outputs].keys.include?(key.to_sym)
        end

        name = File.basename(found_template).sub(/\.yml$/,'')
        name.underscore.camelize # IE: ApiResources1
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jets-5.0.13 lib/jets/cfn/template.rb
jets-5.0.12 lib/jets/cfn/template.rb
jets-5.0.11 lib/jets/cfn/template.rb
jets-5.0.10 lib/jets/cfn/template.rb
jets-5.0.9 lib/jets/cfn/template.rb
jets-5.0.8 lib/jets/cfn/template.rb
jets-5.0.7 lib/jets/cfn/template.rb
jets-5.0.6 lib/jets/cfn/template.rb
jets-5.0.5 lib/jets/cfn/template.rb
jets-5.0.4 lib/jets/cfn/template.rb
jets-5.0.3 lib/jets/cfn/template.rb
jets-5.0.2 lib/jets/cfn/template.rb
jets-5.0.1 lib/jets/cfn/template.rb
jets-5.0.0 lib/jets/cfn/template.rb
jets-5.0.0.beta1 lib/jets/cfn/template.rb