class Terrenv::CLI
Public Instance Methods
__print_version()
click to toggle source
# File lib/terrenv/cli.rb, line 11 def __print_version puts Terrenv::VERSION end
apply()
click to toggle source
# File lib/terrenv/cli.rb, line 16 def apply puts 'Creating environments' begin settings = YAML.load(File.read('TerraformFile')) rescue puts "Couldn't find TerraformFile, did you initialize it?" exit 1 end # TODO Delete environments not specified settings['environments'].each do |env| create(env) use(env) remote_setup end end
init()
click to toggle source
# File lib/terrenv/cli.rb, line 57 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_setup()
click to toggle source
# File lib/terrenv/cli.rb, line 46 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)
click to toggle source
# File lib/terrenv/cli.rb, line 33 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
Private Instance Methods
append_to_gitignore()
click to toggle source
# File lib/terrenv/cli.rb, line 85 def append_to_gitignore end
ask(question, default)
click to toggle source
# File lib/terrenv/cli.rb, line 87 def ask(question, default) print "#{question}(#{default}): " answer = STDIN.gets.chomp answer.empty? ? default : answer end
create(env)
click to toggle source
# File lib/terrenv/cli.rb, line 71 def create(env) state_dir = state_dir_format(env) if not File.exists?(state_dir) Dir.mkdir(state_dir) FileUtils.touch("#{state_dir}/terraform.tfvars") puts "Created directory: #{ state_dir }" end end
current_env()
click to toggle source
# File lib/terrenv/cli.rb, line 96 def current_env File.readlink('.terraform')[10..-1] end
delete(environment)
click to toggle source
# File lib/terrenv/cli.rb, line 66 def delete(environment) # TODO: Deleted environment can still be in use state_dir = state_dir_format(environment) FileUtils.rm_rf(state_dir) end
link_env_variable_file(env)
click to toggle source
# File lib/terrenv/cli.rb, line 79 def link_env_variable_file(env) state_dir = state_dir_format(env) FileUtils.touch("#{state_dir}/#{current_env}.tfvar") FileUtils.ln_s(".terraform-#{current_env}/#{current_env}.tfvars", "terraform.tfvars", :force => true) end
state_dir_format(name)
click to toggle source
# File lib/terrenv/cli.rb, line 92 def state_dir_format(name) "terraform-#{ name }" end