Class: Vidar::Config

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

Overview

Loads and provides access to the vidar.yml manifest configuration. Values fall back to DEFAULT_OPTIONS when not defined in the manifest.

Constant Summary collapse

DEFAULT_MANIFEST_FILE =
"vidar.yml".freeze
DEFAULT_BRANCHES =
%w[main master].freeze
REQUIRED_KEYS =
%w[image namespace github].freeze
DEFAULT_OPTIONS =
{
  compose_file: -> { "docker-compose.ci.yml" },
  compose_cmd: -> { "docker compose" },
  default_branch: -> { (DEFAULT_BRANCHES & branches).first || DEFAULT_BRANCHES.first },
  current_branch: -> { (ENV["SEMAPHORE_GIT_WORKING_BRANCH"] || shell_capture("git rev-parse --abbrev-ref HEAD")).tr("/", "-") },
  revision: -> { shell_capture("git rev-parse HEAD") },
  revision_name: -> { shell_capture('git show --pretty=format:"%s (%h)" -s HEAD') },
  kubectl_context: -> { shell_capture("kubectl config current-context") },
  shell_command: -> { "/bin/sh" },
  console_command: -> { "bin/console" },
  base_stage_name: -> { "base" },
  release_stage_name: -> { "release" },
  honeycomb_api_key: -> { ENV["HONEYCOMB_API_KEY"] },
  sidecar_container_names: -> { ["istio-proxy"] }
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/vidar/config.rb', line 26

def data
  @data
end

.manifest_fileString

Returns path to the manifest file.

Returns:

  • (String)

    path to the manifest file



42
43
44
# File 'lib/vidar/config.rb', line 42

def manifest_file
  @manifest_file || DEFAULT_MANIFEST_FILE
end

Class Method Details

.branchesArray<String>

Returns local git branch names.

Returns:

  • (Array<String>)

    local git branch names



112
113
114
115
# File 'lib/vidar/config.rb', line 112

def branches
  stdout, _stderr, _status = Open3.capture3("git for-each-ref --format='%(refname:short)' refs/heads/*")
  stdout.split("\n")
end

.build_deploy_config(kubectl_context) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vidar/config.rb', line 91

def build_deploy_config(kubectl_context)
  deployments = get(:deployments)
  deployments = {} unless deployments.is_a?(Hash)
  deployment = deployments[kubectl_context]

  if deployment.nil?
    Log.error "ERROR: could not find deployment config for #{get!(:kubectl_context)} context"
    return nil
  end

  deployment.transform_keys!(&:to_sym)
  deployment.transform_values! { |value| Vidar::Interpolation.call(value, self) }

  DeployConfig.new(deployment)
end

.build_urlString?

Returns CI build URL resolved from env or manifest.

Returns:

  • (String, nil)

    CI build URL resolved from env or manifest



75
76
77
78
# File 'lib/vidar/config.rb', line 75

def build_url
  value = ENV[get(:build_env).to_s] || get(:build_url)
  value&.empty? ? nil : value
end

.default_branch?Boolean

Returns true if current branch is the default branch.

Returns:

  • (Boolean)

    true if current branch is the default branch



118
119
120
# File 'lib/vidar/config.rb', line 118

def default_branch?
  get!(:current_branch) == get!(:default_branch)
end

.deploy_configDeployConfig

Returns deployment config for the current kubectl context.

Returns:

  • (DeployConfig)

    deployment config for the current kubectl context



87
88
89
# File 'lib/vidar/config.rb', line 87

def deploy_config
  deploy_configs[get!(:kubectl_context)] ||= build_deploy_config(get!(:kubectl_context))
end

.deploy_configsObject



107
108
109
# File 'lib/vidar/config.rb', line 107

def deploy_configs
  @deploy_configs ||= {}
end

.ensure_file_exist!(file_path) ⇒ Object



46
47
48
# File 'lib/vidar/config.rb', line 46

def ensure_file_exist!(file_path)
  fail(MissingManifestFileError, file_path) unless File.exist?(file_path)
end

.get(key) ⇒ Object?

Returns value from manifest or default.

Parameters:

  • key (Symbol, String)

    config key

Returns:

  • (Object, nil)

    value from manifest or default



57
58
59
60
61
62
63
64
65
# File 'lib/vidar/config.rb', line 57

def get(key)
  load unless loaded?

  value = @data[key.to_s] || DEFAULT_OPTIONS[key.to_sym]&.call

  return value unless value.is_a?(String)

  Vidar::Interpolation.call(value, self)
end

.get!(key) ⇒ Object

Returns value from manifest or default.

Parameters:

  • key (Symbol, String)

    config key

Returns:

  • (Object)

    value from manifest or default

Raises:



70
71
72
# File 'lib/vidar/config.rb', line 70

def get!(key)
  get(key) || fail(MissingConfigError, key)
end

.honeycomb_env_api_key(env) ⇒ String?

Returns Honeycomb API key for the given environment.

Parameters:

  • env (String)

    environment name (maps to HONEYCOMB_API_KEY_<ENV>)

Returns:

  • (String, nil)

    Honeycomb API key for the given environment



82
83
84
# File 'lib/vidar/config.rb', line 82

def honeycomb_env_api_key(env)
  ENV["HONEYCOMB_API_KEY_#{env.upcase}"]
end

.load(file_path = manifest_file) ⇒ Object

Loads the manifest file and validates required keys.

Parameters:

  • file_path (String) (defaults to: manifest_file)

    path to vidar.yml

Raises:



33
34
35
36
37
38
39
# File 'lib/vidar/config.rb', line 33

def load(file_path = manifest_file)
  ensure_file_exist!(file_path)

  @data = YAML.load_file(file_path)
  validate_schema!
  @loaded = true
end

.loaded?Boolean

Returns true if the manifest has been loaded.

Returns:

  • (Boolean)

    true if the manifest has been loaded



51
52
53
# File 'lib/vidar/config.rb', line 51

def loaded?
  @loaded
end