Module: Nvoi::Utils::Templates

Defined in:
lib/nvoi/utils/templates.rb

Overview

Templates handles K8s manifest template loading and rendering

Defined Under Namespace

Classes: TemplateBinding

Class Method Summary collapse

Class Method Details

.apply_manifest(ssh, template_name, data) ⇒ Object

Render a template and apply it via kubectl



49
50
51
52
53
# File 'lib/nvoi/utils/templates.rb', line 49

def apply_manifest(ssh, template_name, data)
  manifest = render(template_name, data)
  cmd = "cat <<'EOF' | kubectl apply -f -\n#{manifest}\nEOF"
  ssh.execute(cmd)
end

.load_template(name) ⇒ Object



28
29
30
31
32
33
# File 'lib/nvoi/utils/templates.rb', line 28

def load_template(name)
  path = template_path(name)
  raise Errors::TemplateError, "template #{name} not found at #{path}" unless File.exist?(path)

  ERB.new(File.read(path), trim_mode: "-")
end

.render(name, data) ⇒ Object

Render a template with the provided data



42
43
44
45
46
# File 'lib/nvoi/utils/templates.rb', line 42

def render(name, data)
  template = load_template(name)
  binding_obj = TemplateBinding.new(data)
  template.result(binding_obj.get_binding)
end

.template_namesObject



35
36
37
38
39
# File 'lib/nvoi/utils/templates.rb', line 35

def template_names
  Dir.glob(File.join(Nvoi.templates_path, "*.erb")).map do |path|
    File.basename(path, ".erb")
  end
end

.template_path(name) ⇒ Object



24
25
26
# File 'lib/nvoi/utils/templates.rb', line 24

def template_path(name)
  File.join(Nvoi.templates_path, "#{name}.erb")
end

.wait_for_deployment(ssh, name, namespace: "default", timeout: 300) ⇒ Object

Wait for a deployment to be ready



56
57
58
# File 'lib/nvoi/utils/templates.rb', line 56

def wait_for_deployment(ssh, name, namespace: "default", timeout: 300)
  ssh.execute("kubectl rollout status deployment/#{name} -n #{namespace} --timeout=#{timeout}s")
end