Class: KubernetesHelper::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/kubernetes_helper/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_env_name) ⇒ Core

Returns a new instance of Core.

Parameters:

  • _env_name (String)


24
25
26
# File 'lib/kubernetes_helper/core.rb', line 24

def initialize(_env_name)
  @config_values = KubernetesHelper.load_settings
end

Instance Attribute Details

#config_valuesHash

Returns:

  • (Hash)


21
22
23
# File 'lib/kubernetes_helper/core.rb', line 21

def config_values
  @config_values
end

Instance Method Details

#parse_yml_file(file_path, output_path) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/kubernetes_helper/core.rb', line 28

def parse_yml_file(file_path, output_path)
  parsed_content = replace_config_variables(File.read(file_path))
  File.open(output_path, 'w+') { |f| f << parsed_content } # save as draft to be reviewed if failed
  old_yaml = YAML.load_stream(parsed_content)
  json_data = old_yaml.to_json # fix to skip anchors
  yml_data = JSON.parse(json_data)
  export_documents(yml_data, output_path)
end

#replace_config_variables(text, locals = {}) ⇒ Object

Sample: replicas: ‘#KubernetesHelper::Core.deploymentdeployment.replicas’

Parameters:

  • text (String)


39
40
41
42
43
44
45
46
47
# File 'lib/kubernetes_helper/core.rb', line 39

def replace_config_variables(text, locals = {})
  values = config_values.merge(locals: locals).map do |key, value| # rubocop:disable Style/HashTransformValues
    [key, value.is_a?(Hash) ? OpenStruct.new(value) : value]
  end.to_h
  values[:render_template] = method(:render_template)
  bind = ErbBinding.new(values).get_binding
  template = ERB.new(text)
  template.result(bind)
end

#run_command(command) ⇒ Object



49
50
51
52
# File 'lib/kubernetes_helper/core.rb', line 49

def run_command(command)
  command = replace_config_variables(command)
  KubernetesHelper.run_cmd(command)
end

#run_script(script_path) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/kubernetes_helper/core.rb', line 54

def run_script(script_path)
  content = replace_config_variables(File.read(script_path))
  tmp_file = KubernetesHelper.settings_path('tmp_script.sh')
  File.write(tmp_file, content)
  KubernetesHelper.run_cmd("chmod +x #{tmp_file}")
  KubernetesHelper.run_cmd(tmp_file)
  # File.delete(tmp_file) # keep tmp script for analysis purpose
end