Class: VagrantPlugins::Cloudstack::Action::RunInstance

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



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

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

Instance Method Details

#call(env) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vagrant-cloudstack/action/run_instance.rb', line 20

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

  # Get the domain we're going to booting up in
  domain = env[:machine].provider_config.domain

  # Get the configs
  domain_config       = env[:machine].provider_config.get_domain_config(domain)
  zone_id             = domain_config.zone_id
  network_id          = domain_config.network_id
  project_id          = domain_config.project_id
  service_offering_id = domain_config.service_offering_id
  template_id         = domain_config.template_id

  # Launch!
  env[:ui].info(I18n.t("vagrant_cloudstack.launching_instance"))
  env[:ui].info(" -- Service offering UUID: #{service_offering_id}")
  env[:ui].info(" -- Template UUID: #{template_id}")
  env[:ui].info(" -- Project UUID: #{project_id}") if project_id != nil
  env[:ui].info(" -- Zone UUID: #{zone_id}")
  env[:ui].info(" -- Network UUID: #{network_id}") if network_id

  local_user = ENV['USER'].dup
  local_user.gsub!(/[^-a-z0-9_]/i, "")
  prefix = env[:root_path].basename.to_s
  prefix.gsub!(/[^-a-z0-9_]/i, "")
  display_name = local_user + "_" + prefix + "_#{Time.now.to_i}"

  begin
    options = {
      :display_name       => display_name,
      :zone_id            => zone_id,
      :flavor_id          => service_offering_id,
      :image_id           => template_id,
      :network_ids        => [network_id]
    }

    options['project_id'] = project_id if project_id != nil

    server = env[:cloudstack_compute].servers.create(options)
  rescue Fog::Compute::Cloudstack::NotFound => e
    # Invalid subnet doesn't have its own error so we catch and
    # check the error message here.
    # XXX FIXME vpc?
    if e.message =~ /subnet ID/
      raise Errors::FogError,
        :message => "Subnet ID not found: #{network_id}"
    end

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

  # Immediately save the ID since it is created at this point.
  # XXX FIXME does cloudstack+fog return the job id rather than
  # server id?
  env[:machine].id = server.id

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

    env[:ui].info(I18n.t("vagrant_cloudstack.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
      # Delete the instance
      terminate(env)

      # Notify the user
      raise Errors::InstanceReadyTimeout,
        timeout: domain_config.instance_ready_timeout
    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_cloudstack.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_cloudstack.ready"))
  end

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

  @app.call(env)
end

#recover(env) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/vagrant-cloudstack/action/run_instance.rb', line 129

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



138
139
140
141
142
143
144
# File 'lib/vagrant-cloudstack/action/run_instance.rb', line 138

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