Sha256: aece351641e83056d0f414e30ed7b2f1fa4d51cdf06657cd51515eb7de5d4701

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'hygroscope'
require 'cfoo'

module Hygroscope
  class TemplateYamlParseError < StandardError
  end

  class Template
    attr_reader :path

    def initialize(path)
      @path = path
    end

    # Process a set of files with cfoo and return JSON
    def process
      return @template if @template

      out = StringIO.new
      err = StringIO.new

      files = Dir.glob(File.join(@path, '*.{yml,yaml}'))
      cfoo = Cfoo::Factory.new(out, err).cfoo

      Dir.chdir('/') do
        cfoo.process(*files)
      end

      fail(TemplateYamlParseError, err.string) unless err.string.empty?

      # Substitute
      # APP_CONFIG = YAML.load(ERB.new(File.read("#{Rails.root}/config/app_config.yml")).result)[Rails.env]
      #
      @template = out.string
      @template
    end

    def compress
      JSON.parse(process).to_json
    end

    def parameters
      template = JSON.parse(process)
      template['Parameters'] || []
    end

    # Process a set of files with cfoo and write JSON to a temporary file
    def process_to_file
      file = Tempfile.new(['hygroscope-', '.json'])
      file.write(process)
      file.close

      at_exit { file.unlink }

      file
    end

    def validate
      # Parsing the template to JSON and then re-outputting it is a form of
      # compression (removing all extra spaces) to keep within the 50KB limit
      # for CloudFormation templates.
      template = compress

      begin
        stack = Hygroscope::Stack.new('template-validator')
        stack.client.validate_template(template_body: template)
      rescue => e
        raise e
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hygroscope-1.1.3 lib/hygroscope/template.rb
hygroscope-1.1.2 lib/hygroscope/template.rb
hygroscope-1.1.1 lib/hygroscope/template.rb
hygroscope-1.1.0 lib/hygroscope/template.rb