Class: VagrantPlugins::Google::Action::StartInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-google/action/start_instance.rb

Overview

This starts a stopped instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ StartInstance

Returns a new instance of StartInstance.



25
26
27
28
# File 'lib/vagrant-google/action/start_instance.rb', line 25

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

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-google/action/start_instance.rb', line 30

def call(env)
  # Initialize metrics if they haven't been
  env[:metrics] ||= {}

  server = env[:google_compute].servers.get(env[:machine].id, env[:machine].provider_config.zone)

  env[:ui].info(I18n.t("vagrant_google.starting"))

  begin
    server.start

    # Wait for the instance to be ready first
    env[:metrics]["instance_ready_time"] = Util::Timer.time do

      tries = env[:machine].provider_config.instance_ready_timeout / 2

      env[:ui].info(I18n.t("vagrant_google.waiting_for_ready"))
      begin
        retryable(:on => Fog::Errors::TimeoutError, :tries => tries) do
          # If we're interrupted don't worry about waiting
          next if env[:interrupted]

          # Wait for the server to be ready
          server.wait_for(2) { ready? }
        end
      rescue Fog::Errors::TimeoutError
        # Notify the user
        raise Errors::InstanceReadyTimeout,
              timeout: env[:machine].provider_config.instance_ready_timeout
      end
    end
  rescue Fog::Compute::Google::Error => e
    raise Errors::FogError, :message => e.message
  end

  @logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")

  unless env[:interrupted]
    env[:metrics]["instance_comm_time"] = Util::Timer.time do
      # Wait for Comms to be ready.
      env[:ui].info(I18n.t("vagrant_google.waiting_for_comm"))
      while true
        # If we're interrupted then just back out
        break if env[:interrupted]
        break if env[:machine].communicate.ready?
        sleep 2
      end
    end

    @logger.info("Time for Comms ready: #{env[:metrics]["instance_comm_time"]}")

    # Ready and booted!
    env[:ui].info(I18n.t("vagrant_google.ready"))
  end

  @app.call(env)
end