Class: Vidar::Config

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

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'] || `git rev-parse --abbrev-ref HEAD`.strip).tr("/", "-") },
  revision:           -> { `git rev-parse HEAD`.strip },
  revision_name:      -> { `git show --pretty=format:"%s (%h)" -s HEAD`.strip },
  kubectl_context:    -> { `kubectl config current-context`.strip },
  shell_command:      -> { "/bin/sh" },
  console_command:    -> { "bin/console" },
  base_stage_name:    -> { "base" },
  release_stage_name: -> { "release" },
  honeycomb_api_key:  -> { ENV['HONEYCOMB_API_KEY'] },
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/vidar/config.rb', line 22

def data
  @data
end

.manifest_fileObject



32
33
34
# File 'lib/vidar/config.rb', line 32

def manifest_file
  @manifest_file || DEFAULT_MANIFEST_FILE
end

Class Method Details

.branchesObject



91
92
93
# File 'lib/vidar/config.rb', line 91

def branches
  `git for-each-ref --format='%(refname:short)' refs/heads/*`.split("\n")
end

.build_deploy_config(kubectl_context) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vidar/config.rb', line 71

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_urlObject



58
59
60
61
# File 'lib/vidar/config.rb', line 58

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

.default_branch?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/vidar/config.rb', line 95

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

.deploy_configObject



67
68
69
# File 'lib/vidar/config.rb', line 67

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

.deploy_configsObject



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

def deploy_configs
  @deploy_configs ||= {}
end

.ensure_file_exist!(file_path) ⇒ Object



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

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

.get(key) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/vidar/config.rb', line 44

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



54
55
56
# File 'lib/vidar/config.rb', line 54

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

.honeycomb_env_api_key(env) ⇒ Object



63
64
65
# File 'lib/vidar/config.rb', line 63

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

.load(file_path = manifest_file) ⇒ Object



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

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

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

.loaded?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/vidar/config.rb', line 40

def loaded?
  @loaded
end