Class: Krane::TemplateSets

Inherits:
Object
  • Object
show all
Includes:
DelayedExceptions
Defined in:
lib/krane/template_sets.rb

Constant Summary collapse

VALID_TEMPLATES =
%w(.yml.erb .yml .yaml .yaml.erb)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DelayedExceptions

#with_delayed_exceptions

Class Method Details

.from_dirs_and_files(paths:, logger:, render_erb: true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/krane/template_sets.rb', line 99

def from_dirs_and_files(paths:, logger:, render_erb: true)
  resource_templates = {}
  dir_paths, file_paths = paths.partition { |path| File.directory?(path) }

  # Directory paths
  dir_paths.each do |template_dir|
    resource_templates[template_dir] = Dir.foreach(template_dir).select do |filename|
      filename.end_with?(*VALID_TEMPLATES) || filename == EjsonSecretProvisioner::EJSON_SECRETS_FILE
    end
  end

  # Filename paths
  file_paths.each do |filename|
    dir_name = File.dirname(filename)
    resource_templates[dir_name] ||= []
    resource_templates[dir_name] << File.basename(filename) unless resource_templates[dir_name].include?(filename)
  end

  template_sets = []
  resource_templates.each do |template_dir, files|
    template_sets << TemplateSet.new(template_dir: template_dir, file_whitelist: files, logger: logger,
      render_erb: render_erb)
  end
  TemplateSets.new(template_sets: template_sets)
end

Instance Method Details

#ejson_secrets_filesObject



144
145
146
# File 'lib/krane/template_sets.rb', line 144

def ejson_secrets_files
  @template_sets.map(&:ejson_secrets_file).compact
end

#validateObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/krane/template_sets.rb', line 148

def validate
  errors = @template_sets.flat_map(&:validate)

  if rendering_erb_disabled? && deploying_with_erb_files?
    errors << "ERB template discovered with rendering disabled. If you were trying to render ERB and " \
      "deploy the result, try piping the output of `krane render` to `krane-deploy -f -`"
  end

  errors
end

#with_resource_definitions(current_sha: nil, bindings: nil, raw: false) ⇒ Object



138
139
140
141
142
# File 'lib/krane/template_sets.rb', line 138

def with_resource_definitions(current_sha: nil, bindings: nil, raw: false)
  with_resource_definitions_and_filename(current_sha: current_sha, bindings: bindings, raw: raw) do |r_def, _|
    yield r_def
  end
end

#with_resource_definitions_and_filename(current_sha: nil, bindings: nil, raw: false) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/krane/template_sets.rb', line 126

def with_resource_definitions_and_filename(current_sha: nil, bindings: nil, raw: false)
  with_delayed_exceptions(@template_sets, Krane::InvalidTemplateError) do |template_set|
    template_set.with_resource_definitions_and_filename(
      current_sha: current_sha,
      bindings: bindings,
      raw: raw
    ) do |r_def, filename|
      yield r_def, filename
    end
  end
end