Class: VagrantPlugins::Joyent::Action::RunInstance

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

Overview

This runs the configured instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



13
14
15
16
# File 'lib/vagrant-joyent/action/run_instance.rb', line 13

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

Instance Method Details

#call(env) ⇒ Object



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
87
88
89
90
91
# File 'lib/vagrant-joyent/action/run_instance.rb', line 18

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

  # Get the configs
  dataset = env[:machine].provider_config.dataset
  flavor = env[:machine].provider_config.flavor
  node_name = env[:machine].provider_config.node_name || env[:machine].name
  keyname = env[:machine].provider_config.joyent_keyname

  # Launch!
  env[:ui].info(I18n.t("vagrant_joyent.launching_instance"))
  env[:ui].info(" -- Flavor: #{flavor}")
  env[:ui].info(" -- Dataset: #{dataset}")
  env[:ui].info(" -- Node name: #{node_name}")
  env[:ui].info(" -- Key name: #{keyname}")

  begin
    options = {
      :name             => node_name,
      :dataset          => dataset,
      :package          => flavor
    }

    server = env[:joyent_compute].servers.create(options)

  rescue Fog::Compute::Joyent::NotFound => e
    raise Errors::FogError, :message => e.message
  rescue Fog::Compute::Joyent::Error => e
    raise Errors::FogError, :message => e.message
  end

  # Immediately save the ID since it is created at this point.
  env[:machine].id = server.id

  # Wait for the instance to be ready first
  env[:metrics]["instance_ready_time"] = Util::Timer.time do
    env[:ui].info(I18n.t("vagrant_joyent.waiting_for_ready"))
    retryable(:on => Fog::Errors::TimeoutError, :tries => 30) 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
  end

  @logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")
 
  if !env[:interrupted]
    env[:metrics]["instance_ssh_time"] = Util::Timer.time do
      # Wait for SSH to be ready.
      env[:ui].info(I18n.t("vagrant_joyent.waiting_for_ssh"))
      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 SSH ready: #{env[:metrics]["instance_ssh_time"]}")

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

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

  @app.call(env)
end

#recover(env) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/vagrant-joyent/action/run_instance.rb', line 93

def recover(env)
  return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)

  if env[:machine].provider.state.id != :not_created
    # Undo the import
    terminate(env)
  end
end

#terminate(env) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/vagrant-joyent/action/run_instance.rb', line 102

def terminate(env)
  destroy_env = env.dup
  destroy_env.delete(:interrupted)
  destroy_env[:config_validate] = false
  destroy_env[:force_confirm_destroy] = true
  env[:action_runner].run(Action.action_destroy, destroy_env)
end