Class: VagrantPlugins::AliyunECS::Action::StartInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-aliyun/action/start_instance.rb

Overview

This runs the configured instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ StartInstance

Returns a new instance of StartInstance.



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

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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
# File 'lib/vagrant-aliyun/action/start_instance.rb', line 16

def call(env)
  start_instance(env[:machine], env)

  @app.call(env)
end

#start_instance(machine, env) ⇒ Object



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
# File 'lib/vagrant-aliyun/action/start_instance.rb', line 22

def start_instance(machine, env)
  return nil if machine.id.nil?

  config = machine.provider_config
  options = {
    :access_key_id => config.access_key_id,
    :access_key_secret => config.access_key_secret
  }
  Aliyun.config options
  ecs = Aliyun::ECS.new

  env[:ui].info(I18n.t("vagrant_aliyun.starting"))
  ecs.start_instance :instance_id=>machine.id

  # Wait for the instance to be ready first
  env[:ui].info(I18n.t("vagrant_aliyun.waiting_for_ready"))
  begin
    instance = ecs.describe_instance_attribute :instance_id=>machine.id
    Timeout.timeout(config.instance_ready_timeout) do
      until instance["Status"] == "Running"
        sleep 2
        instance = ecs.describe_instance_attribute :instance_id=>machine.id
      end
    end
  rescue Timeout::Error
    env[:result] = false # couldn't reach state in time
    # Delete the instance
    terminate(env)

    # Notify the user
    raise Errors::InstanceReadyTimeout,
      timeout: config.instance_ready_timeout
  end

  if !env[:interrupted]
    # Wait for SSH to be ready.
    env[:ui].info(I18n.t("vagrant_aliyun.waiting_for_ssh"))
    network_ready_retries = 0
    network_ready_retries_max = 10
    while true
      # If we're interrupted then just back out
      break if env[:interrupted]
      # When an ECS instance comes up, it's networking may not be ready
      # by the time we connect.
      begin
        break if env[:machine].communicate.ready?
      rescue Exception => e
        if network_ready_retries < network_ready_retries_max then
          network_ready_retries += 1
          @logger.warn(I18n.t("vagrant_aliyun.waiting_for_ssh, retrying"))
        else
          raise e
        end
      end
      sleep 2
    end

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

  # Terminate the instance if we were interrupted
  terminate(env) if env[:interrupted]
end