Sha256: ec985ee3fc205fe8b5ff0993725d72668374b687dcb4ddac42efa3369d69f222

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Seira
  module Util
    class ResourceRenderer
      include ERB::Util

      def initialize(template:, context:, locals:)
        @template = template
        @context = context
        @locals = locals
        @summary = {}
      end

      # "binding" is a special method every ruby object has to expose its
      # instance variables
      # https://ruby-doc.org/core-2.2.0/Binding.html
      def render
        result = ERB.new(@template).result(binding)

        puts "Rendered with following ERB variables:"
        @summary.each do |key, value|
          puts "#{key}: #{value}"
        end

        result
      end

      # BEGIN ERB templating methods and variables
      def current_replica_count(deployment)
        count = Seira::Helpers.get_current_replicas(deployment: deployment, context: @context)
        @summary["#{deployment}-replicas"] = count

        # Validate a sane count so that we don't accidentally deploy 0 replicas
        unless count && count.is_a?(Integer)
          fail "Received invalid value for replica count for Deployment #{deployment} '#{count}'"
        end

        count
      end

      def target_revision
        rv = @locals['REVISION']
        @summary["revision"] = rv
        rv
      end

      def restarted_at_value
        rv = @locals['RESTARTED_AT_VALUE']
        @summary["restarted_at_value"] = rv
        rv
      end
      # END ERB templating methods and variables
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
seira-0.4.8 lib/seira/util/resource_renderer.rb
seira-0.4.7 lib/seira/util/resource_renderer.rb
seira-0.4.5 lib/seira/util/resource_renderer.rb