Class: VagrantPlugins::Scaleway::Action::StartServer

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

Overview

This starts a stopped server.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ StartServer



11
12
13
14
# File 'lib/vagrant-scaleway/action/start_server.rb', line 11

def initialize(app, _env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_scaleway::action::start_server')
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
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
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-scaleway/action/start_server.rb', line 16

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

  config = env[:machine].provider_config

  server = env[:scaleway_compute].servers.get(env[:machine].id)

  env[:ui].info(I18n.t('vagrant_scaleway.starting'))

  begin
    server.poweron
  rescue Fog::Scaleway::Compute::Error => e
    raise Errors::FogError, message: e.message
  end

  # Wait for the server to be ready first
  env[:metrics]['server_ready_time'] = Util::Timer.time do
    tries = config.server_ready_timeout / 2

    env[:ui].info(I18n.t('vagrant_scaleway.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, config.server_check_interval) { ready? }
      end
    rescue Fog::Errors::TimeoutError
      # Notify the user
      raise Errors::ServerReadyTimeout,
            timeout: config.server_ready_timeout
    end
  end

  @logger.info("Time to server ready: #{env[:metrics]['server_ready_time']}")

  unless env[:interrupted]
    env[:metrics]['server_ssh_time'] = Util::Timer.time do
      # Wait for SSH to be ready.
      env[:ui].info(I18n.t('vagrant_scaleway.waiting_for_ssh'))
      network_ready_retries = 0
      network_ready_retries_max = 10
      loop do
        # If we're interrupted then just back out
        break if env[:interrupted]
        # When a server comes up, it's networking may not be ready by
        # the time we connect.
        begin
          break if env[:machine].communicate.ready?
        rescue
          if network_ready_retries < network_ready_retries_max
            network_ready_retries += 1
            @logger.warn(I18n.t('vagrant_scaleway.waiting_for_ssh, retrying'))
          else
            raise
          end
        end
        sleep 2
      end
    end

    @logger.info("Time for SSH ready: #{env[:metrics]['server_ssh_time']}")

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

  @app.call(env)
end