Class: Nexoform::Config

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

Defined Under Namespace

Classes: ConfigError

Class Method Summary collapse

Class Method Details

.bucket(environment) ⇒ Object



106
107
108
# File 'lib/nexoform/config.rb', line 106

def self.bucket(environment)
  find_value(I[nexoform environments #{environment} state bucket])
end

.debug?Boolean



90
91
92
# File 'lib/nexoform/config.rb', line 90

def self.debug?
  settings[:nexoform][:debug]
end

.default_envObject



102
103
104
# File 'lib/nexoform/config.rb', line 102

def self.default_env
  settings[:nexoform][:environments][:default]
end

.default_filenameObject



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

def self.default_filename
  './nexoform.yml'
end

.default_settings(project_name = nil) ⇒ Object



72
73
74
# 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) ⇒ Object



34
35
36
37
38
39
40
41
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
# 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
            bucket: #{project_name}-terraform-state
            key: dev.tfstate
            region: us-east-1
        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
            bucket: #{project_name}-terraform-state
            key: staging.tfstate
            region: us-east-1
        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
            bucket: #{project_name}-terraform-state
            key: prod.tfstate
            region: us-east-1
  ).split("\n").map { |s| s.sub(' ' * 8, '') }.join("\n")
end

.envsObject



94
95
96
# File 'lib/nexoform/config.rb', line 94

def self.envs
  settings[:nexoform][:environments].keys.reject { |k| k == 'default' }
end

.filenameObject



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

def self.filename
  find_config_file(Dir.pwd)
end

.find_config_file(starting_dir) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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) ⇒ Boolean



8
9
10
# File 'lib/nexoform/config.rb', line 8

def self.has_config_file?(dir)
  File.exist?("#{dir}/nexoform.yml")
end

.has_plan_file?(environment) ⇒ Boolean



132
133
134
135
136
137
138
# 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) ⇒ Boolean



144
145
146
147
148
149
# 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) ⇒ Object



110
111
112
# File 'lib/nexoform/config.rb', line 110

def self.key(environment)
  find_value(I[nexoform environments #{environment} state key])
end

.plan_disabled?(environment) ⇒ Boolean



122
123
124
125
126
# File 'lib/nexoform/config.rb', line 122

def self.plan_disabled?(environment)
  !plan_enabled(environment)
rescue ConfigError => e
  false
end

.plan_enabled(environment) ⇒ Object



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

def self.plan_enabled(environment)
  find_value(I[nexoform environments #{environment} plan enabled])
end

.plan_file(environment) ⇒ Object



128
129
130
# 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) ⇒ Object



140
141
142
# 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) ⇒ Object



30
31
32
# File 'lib/nexoform/config.rb', line 30

def self.proj_name(project_name)
  project_name && !project_name.empty? ? project_name : '<companyname>'
end

.region(environment) ⇒ Object



114
115
116
# File 'lib/nexoform/config.rb', line 114

def self.region(environment)
  find_value(I[nexoform environments #{environment} state region])
end

.settings(filename = self.filename) ⇒ Object



76
77
78
# 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) ⇒ Object



98
99
100
# 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) ⇒ Object



86
87
88
# 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) ⇒ Object



80
81
82
83
84
# 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