Class: Vidar::Config
- Inherits:
-
Object
- Object
- Vidar::Config
- 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
- 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
-
.data ⇒ Object
readonly
Returns the value of attribute data.
-
.manifest_file ⇒ String
Path to the manifest file.
Class Method Summary collapse
-
.branches ⇒ Array<String>
Local git branch names.
- .build_deploy_config(kubectl_context) ⇒ Object
-
.build_url ⇒ String?
CI build URL resolved from env or manifest.
-
.default_branch? ⇒ Boolean
True if current branch is the default branch.
-
.deploy_config ⇒ DeployConfig
Deployment config for the current kubectl context.
- .deploy_configs ⇒ Object
- .ensure_file_exist!(file_path) ⇒ Object
-
.get(key) ⇒ Object?
Value from manifest or default.
-
.get!(key) ⇒ Object
Value from manifest or default.
-
.honeycomb_env_api_key(env) ⇒ String?
Honeycomb API key for the given environment.
- .load(file_path = manifest_file) ⇒ Object
-
.loaded? ⇒ Boolean
True if the manifest has been loaded.
Class Attribute Details
.data ⇒ Object (readonly)
Returns the value of attribute data.
24 25 26 |
# File 'lib/vidar/config.rb', line 24 def data @data end |
.manifest_file ⇒ String
Returns path to the manifest file.
38 39 40 |
# File 'lib/vidar/config.rb', line 38 def manifest_file @manifest_file || DEFAULT_MANIFEST_FILE end |
Class Method Details
.branches ⇒ Array<String>
Returns local git branch names.
108 109 110 111 |
# File 'lib/vidar/config.rb', line 108 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vidar/config.rb', line 87 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_url ⇒ String?
Returns CI build URL resolved from env or manifest.
71 72 73 74 |
# File 'lib/vidar/config.rb', line 71 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.
114 115 116 |
# File 'lib/vidar/config.rb', line 114 def default_branch? get!(:current_branch) == get!(:default_branch) end |
.deploy_config ⇒ DeployConfig
Returns deployment config for the current kubectl context.
83 84 85 |
# File 'lib/vidar/config.rb', line 83 def deploy_config deploy_configs[get!(:kubectl_context)] ||= build_deploy_config(get!(:kubectl_context)) end |
.deploy_configs ⇒ Object
103 104 105 |
# File 'lib/vidar/config.rb', line 103 def deploy_configs @deploy_configs ||= {} end |
.ensure_file_exist!(file_path) ⇒ Object
42 43 44 |
# File 'lib/vidar/config.rb', line 42 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.
53 54 55 56 57 58 59 60 61 |
# File 'lib/vidar/config.rb', line 53 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.
66 67 68 |
# File 'lib/vidar/config.rb', line 66 def get!(key) get(key) || fail(MissingConfigError, key) end |
.honeycomb_env_api_key(env) ⇒ String?
Returns Honeycomb API key for the given environment.
78 79 80 |
# File 'lib/vidar/config.rb', line 78 def honeycomb_env_api_key(env) ENV["HONEYCOMB_API_KEY_#{env.upcase}"] end |
.load(file_path = manifest_file) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/vidar/config.rb', line 29 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.
47 48 49 |
# File 'lib/vidar/config.rb', line 47 def loaded? @loaded end |