Class: VagrantPlugins::TerraformProvider::Action::SetupTerraform

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-terraform/action/setup_terraform.rb

Overview

This action reads the state of the machine and puts it in the ‘:machine_state_id` key in the environment.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SetupTerraform

Returns a new instance of SetupTerraform.



10
11
12
13
# File 'lib/vagrant-terraform/action/setup_terraform.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_terraform::action::setup_terraform")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-terraform/action/setup_terraform.rb', line 15

def call(env)
  raise "env[:machine_tf_dir] not set" if env[:machine_tf_dir].nil?

  terraform_dir = env[:machine_tf_dir]
  if File.exist?(terraform_dir) && File.exist?("#{terraform_dir}/terraform.tfstate")
    env[:ui].info("Already initialized.")
  else
    # dir_name = env[:root_path].basename.to_s.dup.gsub(/[^-a-z0-9_]/i, "")
    begin
      Dir.mkdir(File.dirname(terraform_dir)) unless File.exist?(File.dirname(terraform_dir))
      FileUtils.touch("#{File.dirname(terraform_dir)}/lock")
    rescue => e
      retry if e.message =~ /File exists/
      env[:ui].error("terraform init failed: #{e.message}")
    end

    begin
      Dir.mkdir(terraform_dir) unless File.exist?(terraform_dir)
    rescue => e
      retry if e.message =~ /File exists/
      env[:ui].error("terraform init failed: #{e.message}")
    end
  end
  @app.call(env)
end