Class: TerraformDevKit::TerraformConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/TerraformDevKit/terraform_config_manager.rb

Class Method Summary collapse

Class Method Details

.create_environment_directory(env) ⇒ Object



40
41
42
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 40

def self.create_environment_directory(env)
  FileUtils.makedirs(env.working_dir)
end

.fix_configuration(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 25

def self.fix_configuration(env)
  raise 'No AWS section in the config file' if Configuration.get('aws').nil?
  if Environment.running_on_jenkins?
    Configuration.get('aws').delete('profile')
  elsif Configuration.get('aws').key?('profile')
    unless env.local_backend?
      raise "AWS credentials for environment #{env.name} must not be stored!"
    end
  else
    profile = request_profile(env)
    Configuration.get('aws')['profile'] = profile
  end
end

.register_extra_vars_proc(p) ⇒ Object



9
10
11
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 9

def self.register_extra_vars_proc(p)
  @extra_vars_proc = p
end

.render_template_config_files(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 45

def self.render_template_config_files(env)
  aws_config = Configuration.get('aws')
  file_list = Dir['*.tf.mustache'] + Dir['*.tfvars.mustache']
  file_list.each do |fname|
    template_file = TerraformTemplateConfigFile.new(
      File.read(fname),
      env,
      aws_config,
      extra_vars: @extra_vars_proc.call(env)
    )
    config_fname = File.basename(fname, File.extname(fname))
    Dir.chdir(env.working_dir) do
      File.open(config_fname, 'w') { |f| f.write(template_file.render) }
    end
  end
end

.request_profile(env) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 63

def self.request_profile(env)
  puts "Environment #{env.name} requires manual input of AWS credentials"
  print 'Enter the profile to use: '
  profile = $stdin.gets.tr("\r\n", '')
  /^\w+$/ =~ profile || (raise 'Invalid profile name')
  profile
end

.setup(env) ⇒ Object



13
14
15
16
17
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 13

def self.setup(env)
  fix_configuration(env)
  create_environment_directory(env)
  render_template_config_files(env)
end

.update_modules?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/TerraformDevKit/terraform_config_manager.rb', line 19

def self.update_modules?
  var = ENV.fetch('TF_DEVKIT_UPDATE_MODULES', 'false')
  var.strip.casecmp('true').zero?
end