class Nexoform::Config

Public Class Methods

bucket(environment) click to toggle source
# File lib/nexoform/config.rb, line 106
def self.bucket(environment)
  find_value(%I[nexoform environments #{environment} state bucket])
end
debug?() click to toggle source
# File lib/nexoform/config.rb, line 90
def self.debug?
  settings[:nexoform][:debug]
end
default_env() click to toggle source
# File lib/nexoform/config.rb, line 102
def self.default_env
  settings[:nexoform][:environments][:default]
end
default_filename() click to toggle source
# File lib/nexoform/config.rb, line 22
def self.default_filename
  './nexoform.yml'
end
default_settings(project_name = nil) click to toggle source
# File lib/nexoform/config.rb, line 72
def self.default_settings(project_name = nil)
  YAML.safe_load(default_yaml(proj_name(project_name))).with_indifferent_access
end
default_yaml(project_name) click to toggle source
# File lib/nexoform/config.rb, line 34
def self.default_yaml(project_name)
  %(---
    nexoform:
      environments:
        default: dev          # optional default env so you don't have to specify
        dev:                  # name of environment
          varFile: dev.tfvars # terraform var-file to use
          plan:               # optional block. Avoids getting prompted
            enabled: yes      # yes | no.  If no, a plan file is not used
            file: dev.tfplan  # file the plan is saved to automatically
            overwrite: yes    # overwrite existing file. could be: yes | no | ask
          state:              # configuration for state management s3 backend
            region: us-east-1 # Region where the BUCKET specified here lives, not the region you are provisioning to
            bucket: #{project_name}-terraform-state
            key: dev.tfstate
        staging:                  # name of environment
          varFile: staging.tfvars # terraform var-file to use
          plan:                   # optional block. Avoids getting prompted
            enabled: yes          # yes | no.  If no, a plan file is not used
            file: staging.tfplan  # file the plan is saved to automatically
            overwrite: yes        # overwrite existing file. could be: yes | no | ask
          state:                  # configuration for state management s3 backend
            region: us-east-1     # Region where the BUCKET specified here lives, not the region you are provisioning to
            bucket: #{project_name}-terraform-state
            key: staging.tfstate
        prod:                  # name of environment
          varFile: prod.tfvars # terraform var-file to use
          plan:                # optional block. Avoids getting prompted
            enabled: yes       # yes | no.  If no, a plan file is not used
            file: prod.tfplan  # file the plan is saved to automatically
            overwrite: yes     # overwrite existing file. could be: yes | no | ask
          state:               # configuration for state management s3 backend
            region: us-east-1  # Region where the BUCKET specified here lives, not the region you are provisioning to
            bucket: #{project_name}-terraform-state
            key: prod.tfstate
  ).split("\n").map { |s| s.sub(' ' * 8, '') }.join("\n")
end
envs() click to toggle source
# File lib/nexoform/config.rb, line 94
def self.envs
  settings[:nexoform][:environments].keys.reject { |k| k == 'default' }
end
filename() click to toggle source
# File lib/nexoform/config.rb, line 26
def self.filename
  find_config_file(Dir.pwd)
end
find_config_file(starting_dir) click to toggle source
# File lib/nexoform/config.rb, line 12
def self.find_config_file(starting_dir)
  if has_config_file?(starting_dir)
    "#{starting_dir}/nexoform.yml"
  elsif starting_dir == '/'
    default_filename
  else
    find_config_file(File.dirname(starting_dir)) # recurse up to /
  end
end
has_config_file?(dir) click to toggle source
# File lib/nexoform/config.rb, line 8
def self.has_config_file?(dir)
  File.exist?("#{dir}/nexoform.yml")
end
has_plan_file?(environment) click to toggle source
# File lib/nexoform/config.rb, line 132
def self.has_plan_file?(environment)
  return false if plan_disabled?(environment)

  plan_file(environment)
rescue ConfigError => e
  false
end
has_plan_file_overwrite?(environment) click to toggle source
# File lib/nexoform/config.rb, line 144
def self.has_plan_file_overwrite?(environment)
  plan_file_overwrite(environment)
  true
rescue ConfigError => e
  false
end
key(environment) click to toggle source
# File lib/nexoform/config.rb, line 110
def self.key(environment)
  find_value(%I[nexoform environments #{environment} state key])
end
plan_disabled?(environment) click to toggle source
# File lib/nexoform/config.rb, line 122
def self.plan_disabled?(environment)
  !plan_enabled(environment)
rescue ConfigError => e
  false
end
plan_enabled(environment) click to toggle source
# File lib/nexoform/config.rb, line 118
def self.plan_enabled(environment)
  find_value(%I[nexoform environments #{environment} plan enabled])
end
plan_file(environment) click to toggle source
# File lib/nexoform/config.rb, line 128
def self.plan_file(environment)
  find_value(%I[nexoform environments #{environment} plan file])
end
plan_file_overwrite(environment) click to toggle source
# File lib/nexoform/config.rb, line 140
def self.plan_file_overwrite(environment)
  find_value(%I[nexoform environments #{environment} plan overwrite])
end
proj_name(project_name) click to toggle source
# File lib/nexoform/config.rb, line 30
def self.proj_name(project_name)
  project_name && !project_name.empty? ? project_name : '<companyname>'
end
region(environment) click to toggle source
# File lib/nexoform/config.rb, line 114
def self.region(environment)
  find_value(%I[nexoform environments #{environment} state region])
end
settings(filename = self.filename) click to toggle source
# File lib/nexoform/config.rb, line 76
def self.settings(filename = self.filename)
  YAML.load_file(filename).with_indifferent_access if File.exist?(filename)
end
var_file(environment) click to toggle source
# File lib/nexoform/config.rb, line 98
def self.var_file(environment)
  find_value(%I[nexoform environments #{environment} varFile])
end
write_default_settings_file(project_name = nil) click to toggle source
# File lib/nexoform/config.rb, line 86
def self.write_default_settings_file(project_name = nil)
  write_settings(default_yaml(proj_name(project_name)), filename, is_yaml: true)
end
write_settings(settings, filename = self.filename, is_yaml: false) click to toggle source
# File lib/nexoform/config.rb, line 80
def self.write_settings(settings, filename = self.filename, is_yaml: false)
  settings = settings.to_yaml unless is_yaml
  settings.gsub!(/\s*!ruby\/hash:ActiveSupport::HashWithIndifferentAccess/, '')
  File.write(filename, settings)
end

Private Class Methods

find_value(keys) click to toggle source
# File lib/nexoform/config.rb, line 156
def self.find_value(keys)
  keys.reduce(settings) do |last_val, key|
    if last_val[key].nil?
      raise ConfigError, "Key '#{key}' in chain '#{keys.join(' -> ')}' produced a " \
        'nil value.  The expected key is missing in your config file.'
    end
    last_val[key]
  end
end