Class: VagrantPlugins::Dustcloud::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-dustcloud/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.



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

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

Instance Method Details

#call(env) ⇒ Object



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
92
93
94
95
96
97
98
# File 'lib/vagrant-dustcloud/action/run_instance.rb', line 19

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

  flavor = env[:machine].provider_config.flavor.to_s
  image = env[:machine].provider_config.image.to_s

  env[:ui].info(I18n.t("vagrant_dustcloud.launching_instance"))
  env[:ui].info(" -- Image: #{image}")
  env[:ui].info(" -- Flavor: #{flavor}")

  begin
    server = env[:dustcloud].create_vm(image, flavor)
  end

  env[:machine].id = server["uuid"]
  env[:machine_ssh_username] = server["username"]
  env[:machine_ssh_password] = server["password"]
  pw_file = env[:machine].data_dir.join('ssh_password')
  pw_file.open('w+') do |f|
    f.write(server["password"])
  end



  env[:metrics]["instance_ready_time"] = Util::Timer.time do
    env[:ui].info(I18n.t("vagrant_dustcloud.waiting_for_ready"))
    total_waited = 0
    while true
    # If we're interrupted don't worry about waiting
      break if env[:interrupted]

      # Wait for the server to be ready
      vm = env[:dustcloud].get_vm(server["uuid"])
      if vm["status"] == "ACTIVE"
        break
      end
      sleep(2)
      total_waited += 2
      if total_waited > 180
        raise Errors::InstanceReadyTimeout, timeout: 180
      end
    end
  end

  env[:ui].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_dustcloud.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 ec2 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("SSH not yet up, retrying...")
          else
            raise e
          end
        end
        sleep 2
      end
    end
    env[:ui].info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")

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

  terminate(env) if env[:interrupted]

  @app.call(env)
end

#terminate(env) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/vagrant-dustcloud/action/run_instance.rb', line 100

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