Class: Mortar::FireCommand

Inherits:
Command
  • Object
show all
Includes:
ClientHelper, ResourceHelper, TTYHelper
Defined in:
lib/mortar/fire_command.rb

Constant Summary

Constants inherited from Command

Command::CHECKSUM_ANNOTATION, Command::LABEL

Instance Method Summary collapse

Methods included from TTYHelper

#pastel

Methods included from ClientHelper

#build_kubeconfig_from_env, #client, #create_client

Methods included from ResourceHelper

#from_file, #from_files, #load_resources, #resources_output, #same_resource?, #stringify_hash

Instance Method Details

#default_configObject



26
27
28
29
30
# File 'lib/mortar/fire_command.rb', line 26

def default_config
  %w{shot.yml shot.yaml}.find { |path|
    File.readable?(path)
  }
end

#dotted_path_to_hash(hash) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mortar/fire_command.rb', line 140

def dotted_path_to_hash(hash)
  h = hash.map do |pkey, pvalue|
    pkey.to_s.split(".").reverse.inject(pvalue) do |value, key|
      { key.to_s => value }
    end
  end
  # Safer to return just empty hash instead of nil
  return {} if h.empty?

  h.inject(&:deep_merge)
end

#executeObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mortar/fire_command.rb', line 43

def execute
  signal_usage_error("#{src} does not exist") unless File.exist?(src)

  load_config

  resources = process_overlays
  resources = inject_extra_labels(resources, process_extra_labels)

  if output?
    puts resources_output(resources)
    exit
  end

  if resources.empty?
    warn 'nothing to do!'
    exit
  end

  K8s::Stack.new(
    name, resources,
    debug: debug?,
    label: LABEL,
    checksum_annotation: CHECKSUM_ANNOTATION
  ).apply(client, prune: prune?)

  puts "shot '#{pastel.cyan(name)}' successfully!" if $stdout.tty?
end

#extra_labelsHash

Returns:

  • (Hash)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mortar/fire_command.rb', line 95

def extra_labels
  return @extra_labels if @extra_labels

  @extra_labels = {}
  label_list.each do |label|
    key, value = label.split('=')
    @extra_labels[key] = value
  end

  @extra_labels
end

#inject_extra_labels(resources, labels) ⇒ Array<K8s::Resource>

Parameters:

  • resources (Array<K8s::Resource>)
  • labels (Hash)

Returns:

  • (Array<K8s::Resource>)


115
116
117
118
119
120
121
122
123
# File 'lib/mortar/fire_command.rb', line 115

def inject_extra_labels(resources, labels)
  resources.map { |resource|
    resource.merge(
      metadata: {
        labels: labels
      }
    )
  }
end

#load_configObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/mortar/fire_command.rb', line 32

def load_config
  if config
    signal_usage_error("Cannot read config file from #{path}") unless File.readable?(config)

    @configuration = Config.load(config)
  else
    # No config provided nor the default config file present
    @configuration = Config.new(variables: {}, overlays: [])
  end
end

#process_extra_labelsHash

Returns:

  • (Hash)


108
109
110
# File 'lib/mortar/fire_command.rb', line 108

def process_extra_labels
  @config.labels(extra_labels)
end

#process_overlaysObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mortar/fire_command.rb', line 71

def process_overlays
  # Reject any resource that do not have kind set
  # Basically means the config or other random yml files found
  resources = load_resources(src).reject { |r| r.kind.nil? }
  @configuration.overlays(overlay_list).each do |overlay|
    overlay_resources = from_files(overlay)
    overlay_resources.each do |overlay_resource|
      match = false
      resources = resources.map { |r|
        if same_resource?(r, overlay_resource)
          match = true
          r.merge(overlay_resource.to_hash)
        else
          r
        end
      }
      resources << overlay_resource unless match
    end
  end

  resources
end

#variables_hashObject



130
131
132
133
134
135
136
137
138
# File 'lib/mortar/fire_command.rb', line 130

def variables_hash
  set_hash = {}
  var_list.each do |var|
    k, v = var.split("=", 2)
    set_hash[k] = v
  end

  dotted_path_to_hash(set_hash)
end

#variables_structRecursiveOpenStruct

Returns:

  • (RecursiveOpenStruct)


126
127
128
# File 'lib/mortar/fire_command.rb', line 126

def variables_struct
  @variables_struct ||= @configuration.variables(variables_hash)
end