Class: CemAcpt::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/config.rb

Overview

Holds the configuration for cem_acpt

Constant Summary collapse

KEYS =
%i[
  actions
  ci_mode
  config_file
  image_name_builder
  log_level
  log_file
  log_format
  module_dir
  node_data
  no_ephemeral_ssh_key
  platform
  provisioner
  quiet
  terraform
  test_data
  tests
  user_config
  verbose
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts: {}, config_file: nil, load_user_config: true) ⇒ Config

Returns a new instance of Config.



36
37
38
39
# File 'lib/cem_acpt/config.rb', line 36

def initialize(opts: {}, config_file: nil, load_user_config: true)
  @load_user_config = load_user_config
  load(opts: opts, config_file: config_file)
end

Instance Attribute Details

#configObject (readonly) Also known as: to_h

Returns the value of attribute config.



34
35
36
# File 'lib/cem_acpt/config.rb', line 34

def config
  @config
end

#env_varsObject (readonly)

Returns the value of attribute env_vars.



34
35
36
# File 'lib/cem_acpt/config.rb', line 34

def env_vars
  @env_vars
end

Instance Method Details

#[](key) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/cem_acpt/config.rb', line 140

def [](key)
  if key.is_a?(Symbol)
    @config[key].dup
  elsif key.is_a?(String)
    @config.dget(key).dup
  else
    raise ArgumentError, "Invalid key type '#{key.class}'"
  end
end

#ci_mode?Boolean Also known as: ci?

Returns:

  • (Boolean)


163
164
165
# File 'lib/cem_acpt/config.rb', line 163

def ci_mode?
  !!get('ci_mode') || !!(ENV['GITHUB_ACTIONS'] || ENV['CI'])
end

#debug_mode?Boolean Also known as: debug?

Returns:

  • (Boolean)


168
169
170
# File 'lib/cem_acpt/config.rb', line 168

def debug_mode?
  get('log_level') == 'debug'
end

#defaultsObject

The default configuration



42
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cem_acpt/config.rb', line 42

def defaults
  {
    actions: {},
    ci_mode: false,
    config_file: nil,
    image_name_builder: {
      character_substitutions: ['_', '-'],
      parts: ['cem-acpt', '$image_fam', '$collection', '$firewall'],
      join_with: '-',
    },
    log_level: 'info',
    log_file: nil,
    log_format: 'text',
    module_dir: Dir.pwd,
    node_data: {},
    no_ephemeral_ssh_key: false,
    platform: {
      name: 'gcp',
    },
    quiet: false,
    test_data: {
      for_each: {
        collection: %w[puppet7],
      },
      vars: {},
      name_pattern_vars: %r{^(?<framework>[a-z]+)_(?<image_fam>[a-z0-9-]+)_(?<firewall>[a-z]+)_(?<framework_vars>[-_a-z0-9]+)$},
      vars_post_processing: {
        new_vars: [
          {
            name: 'profile',
            string_split: {
              from: 'framework_vars',
              using: '_',
              part: 0,
            },
          },
          {
            name: 'level',
            string_split: {
              from: 'framework_vars',
              using: '_',
              part: 1,
            },
          },
        ],
        delete_vars: %w[framework_vars],
      },
    },
    tests: [],
    verbose: false,
  }
end

#empty?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/cem_acpt/config.rb', line 159

def empty?
  @config.empty?
end

#explainObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cem_acpt/config.rb', line 122

def explain
  explanation = {}
  %i[defaults env_vars user_config config_from_file options].each do |source|
    source_vals = send(source).dup
    next if source_vals.nil? || source_vals.empty?

    # The loop below will overwrite the value of explanation[key] if the same key is found in multiple sources
    # This is intentional, as the last source to set the value is the one that should be used
    source_vals.each do |key, value|
      explanation[key] = source if @config.dget(key.to_s) == value
    end
  end
  explained = explanation.each_with_object([]) do |(key, value), ary|
    ary << "Key '#{key}' from source '#{value}'"
  end
  explained.join("\n")
end

#get(dot_key) ⇒ Object Also known as: dget



150
151
152
# File 'lib/cem_acpt/config.rb', line 150

def get(dot_key)
  @config.dget(dot_key).dup
end

#has?(dot_key) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/cem_acpt/config.rb', line 155

def has?(dot_key)
  !!get(dot_key)
end

#load(opts: {}, config_file: nil) ⇒ self

Load the configuration from the environment variables, config file, and opts The order of precedence is:

1. environment variables
2. user config file (config.yaml in user_config_dir)
3. specified config file (if it exists)
4. opts
5. static options (set in this class)

Parameters:

  • opts (Hash) (defaults to: {})

    The options to load

  • config_file (String) (defaults to: nil)

    The config file to load

Returns:

  • (self)

    This object with the config loaded



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cem_acpt/config.rb', line 105

def load(opts: {}, config_file: nil)
  create_config_dirs!
  init_config!(opts: opts, config_file: config_file)
  add_env_vars!(@config)
  @config.merge!(user_config) if user_config && @load_user_config
  @config.merge!(config_from_file) if config_from_file
  @config.merge!(@options) if @options
  add_static_options!(@config)
  @config.format! # Symbolize keys of all hashes
  validate_config!
  # Freeze the config so it can't be modified
  # This helps with thread safety and deterministic behavior
  @config.freeze
  self
end

#quiet_mode?Boolean Also known as: quiet?

Returns:

  • (Boolean)


178
179
180
# File 'lib/cem_acpt/config.rb', line 178

def quiet_mode?
  !!get('quiet')
end

#to_json(*args) ⇒ Object



187
188
189
# File 'lib/cem_acpt/config.rb', line 187

def to_json(*args)
  @config.to_json(*args)
end

#to_yamlObject



183
184
185
# File 'lib/cem_acpt/config.rb', line 183

def to_yaml
  @config.to_yaml
end

#verbose_mode?Boolean Also known as: verbose?

Returns:

  • (Boolean)


173
174
175
# File 'lib/cem_acpt/config.rb', line 173

def verbose_mode?
  !!get('verbose')
end