Class: Orchparty::Services::Chart

Inherits:
Context
  • Object
show all
Defined in:
lib/orchparty/kubernetes_application.rb

Defined Under Namespace

Classes: CleanBinding

Instance Attribute Summary

Attributes inherited from Context

#app_config, #cluster_name, #dir_path, #namespace

Instance Method Summary collapse

Methods inherited from Context

#initialize, #template

Constructor Details

This class inherits a constructor from Orchparty::Services::Context

Instance Method Details

#build_chart(chart) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/orchparty/kubernetes_application.rb', line 172

def build_chart(chart)
  params = chart._services.map {|s| app_config.services[s.to_sym] }.map{|s| [s.name, s]}.to_h
  Dir.mktmpdir do |dir|
    run(templates_path: File.join(self.dir_path, chart.template), params: params, output_chart_path: dir, chart: chart)
    yield dir
  end
end

#generate_chart_yaml(templates_path:, output_chart_path:, chart_name:) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/orchparty/kubernetes_application.rb', line 226

def generate_chart_yaml(templates_path:, output_chart_path:, chart_name: )
  template_path = File.join(templates_path, 'Chart.yaml.erb')
  output_path = File.join(output_chart_path, 'Chart.yaml')

  template = Erubis::Eruby.new(File.read(template_path))
  params = Hashie::Mash.new(chart_name: chart_name)
  document = template.result(CleanBinding.new.get_binding(params))
  File.write(output_path, document)
end

#generate_documents_from_erbs(templates_path:, app_name:, params:, output_chart_path:) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/orchparty/kubernetes_application.rb', line 207

def generate_documents_from_erbs(templates_path:, app_name:, params:, output_chart_path:)
  if params[:kind].nil?
    warn "ERROR: Could not generate service '#{app_name}'. Missing key: 'kind'."
    exit 1
  end

  kind = params.fetch(:kind)

  Dir[File.join(templates_path, kind, '*.erb')].each do |template_path|
    template_name = File.basename(template_path, '.erb')
    output_path = File.join(output_chart_path, 'templates', "#{app_name}-#{template_name}")

    template = Erubis::Eruby.new(File.read(template_path))
    params.app_name = app_name
    document = template.result(CleanBinding.new.get_binding(params))
    File.write(output_path, document)
  end
end

#install(chart) ⇒ Object



248
249
250
251
252
# File 'lib/orchparty/kubernetes_application.rb', line 248

def install(chart)
  build_chart(chart) do |chart_path|
    puts system("helm install --namespace #{namespace} --kube-context #{cluster_name} --name #{chart.name} #{chart_path}")
  end
end


238
239
240
241
242
# File 'lib/orchparty/kubernetes_application.rb', line 238

def print_install(chart)
  build_chart(chart) do |chart_path|
    puts `helm template --namespace #{namespace} --kube-context #{cluster_name} --name #{chart.name} #{chart_path}`
  end
end


244
245
246
# File 'lib/orchparty/kubernetes_application.rb', line 244

def print_upgrade(chart)
  print_install(chart)
end

#run(templates_path:, params:, output_chart_path:, chart:) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/orchparty/kubernetes_application.rb', line 181

def run(templates_path:, params:, output_chart_path:, chart: )

  system("mkdir -p #{output_chart_path}")
  system("mkdir -p #{File.join(output_chart_path, 'templates')}")

  system("cp #{File.join(templates_path, 'values.yaml')} #{File.join(output_chart_path, 'values.yaml')}")
  system("cp #{File.join(templates_path, '.helmignore')} #{File.join(output_chart_path, '.helmignore')}")
  system("cp #{File.join(templates_path, 'templates/_helpers.tpl')} #{File.join(output_chart_path, 'templates/_helpers.tpl')}")

  generate_chart_yaml(
    templates_path: templates_path,
    output_chart_path: output_chart_path,
    chart_name: chart.name,
  )

  params.each do |app_name, subparams|
    subparams[:chart] = chart
    generate_documents_from_erbs(
      templates_path: templates_path,
      app_name: app_name,
      params: subparams,
      output_chart_path: output_chart_path
    )
  end
end

#upgrade(chart) ⇒ Object



254
255
256
257
258
# File 'lib/orchparty/kubernetes_application.rb', line 254

def upgrade(chart)
  build_chart(chart) do |chart_path|
    puts system("helm upgrade --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}")
  end
end