Class: Terrenv::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/terrenv/cli.rb

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



11
12
13
# File 'lib/terrenv/cli.rb', line 11

def __print_version
  puts Terrenv::VERSION
end

#applyObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/terrenv/cli.rb', line 16

def apply
  puts 'Creating environments'
  settings = YAML.load(File.read('TerraformFile'))
  # TODO Delete environments not specified
  settings['environments'].each do |env|
    create(env)
    use(env)
    remote_setup
  end
end

#initObject



52
53
54
55
56
57
58
59
# File 'lib/terrenv/cli.rb', line 52

def init
  settings = Hash.new
  settings['project'] = ask('Project name', 'empty')
  settings['bucket'] = ask('s3 bucket', 'telusdigital-terraform-states')
  settings['region'] = ask('bucket region', 'us-west-2')
  settings['environments'] = ['staging']
  File.open('TerraformFile', 'w') { |file| file.write(settings.to_yaml) }
end

#remote_setupObject



41
42
43
44
45
46
47
48
49
# File 'lib/terrenv/cli.rb', line 41

def remote_setup
  settings = YAML.load(File.read('TerraformFile'))
  puts "#{settings['project']}-#{current_env} remote being used"
  puts system("terraform remote config \
         -backend=s3 \
         -backend-config=\"bucket=#{settings['bucket']}\" \
         -backend-config=\"key=#{settings['project']}-#{current_env}.tfstate\" \
         -backend-config=\"region=#{settings['region']}\"")
end

#use(environment) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/terrenv/cli.rb', line 28

def use(environment)
  state_dir = state_dir_format(environment)
  puts "Using environment #{ state_dir }"
  if Dir.exists?(state_dir)
    FileUtils.rm('.terraform', force: true)
    # TODO: if .terraform exists and is not a soft link, this silently fails
    FileUtils.ln_s(state_dir, '.terraform', force: true)
    FileUtils.rm('terraform.tfvars', force: true)
    FileUtils.ln_s("#{state_dir}/terraform.tfvars", 'terraform.tfvars', force: true)
  end
end