Class: VagrantPlugins::TerraformProvider::Action::StartVM

Inherits:
Object
  • Object
show all
Includes:
Util::TerraformExecute, Util::UpdateVmState
Defined in:
lib/vagrant-terraform/action/start_vm.rb

Overview

Just start the VM.

Instance Method Summary collapse

Methods included from Util::UpdateVmState

update_vm_state

Methods included from Util::TerraformExecute

terraform_execute

Constructor Details

#initialize(app, env) ⇒ StartVM

Returns a new instance of StartVM.



15
16
17
18
# File 'lib/vagrant-terraform/action/start_vm.rb', line 15

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

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-terraform/action/start_vm.rb', line 20

def call(env)
  config = env[:machine].provider_config

  env[:ui].info(I18n.t("vagrant_terraform.starting_vm"))

  begin
    terraform_dir = env[:machine_tf_dir]
    terraform_main_file = "#{terraform_dir}/main.tf"
    update_vm_state(terraform_main_file, "running")
    terraform_execute(env, 'terraform apply -auto-approve')
  rescue Exception => e
    fault_message = /Error was \"\[?(.+?)\]?\".*/.match(e.message)[1] rescue e.message
    # TODO: retry in some case?
    # retry if e.message =~ /something/

    if e.message !~ /VM is running/
      if config.debug
        raise e
      else
        raise Errors::StartVMError,
          :error_message => fault_message
      end
    end

  end

  @app.call(env)
end