Class: VagrantPlugins::Vrealize::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-vrealize/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-vrealize/action/run_instance.rb', line 15

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

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagrant-vrealize/action/run_instance.rb', line 77

def call(env)
  vra = env[:vra] or fail "No VRA client instance!"

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

  created_resource = create_resource(env)
  env[:machine].id = created_resource.id
  save_ip_address(env, created_resource)
  wait_for_ssh(env)
  terminate(env) if env[:interrupted]

  @app.call(env)
end

#create_resource(env) ⇒ Object

There are more literal types than this, but we don’t support them yet



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-vrealize/action/run_instance.rb', line 22

def create_resource(env)
  env[:ui].info("Creating machine")

  config = env[:machine].provider_config
  vra = env[:vra]

  EntitledItemsCollection.fetch(env[:vra])
    .find_by_id(config.catalog_item_id)
    .request(cpus:          config.cpus,
             memory:        config.memory,
             requested_for: config.requested_for,
             subtenant_id:  config.subtenant_id,
             lease_days:    config.lease_days) { |req|
      config.extra_entries.types.each do |type|
        config.extra_entries.of_type(type).each do |k,v|
          req.set_parameter(k, type, v)
        end
      end
    }
    .join
    .machine
end

#recover(env) ⇒ Object



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

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

#save_ip_address(env, resource) ⇒ Object



71
72
73
74
75
# File 'lib/vagrant-vrealize/action/run_instance.rb', line 71

def save_ip_address(env, resource)
  env[:machine].data_dir.join('elastic_ip').open("w+") do |f|
    f.write({:public_ip => resource.ip_addresses.first}.to_json)
  end
end

#terminate(env) ⇒ Object



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

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

#wait_for_ssh(env) ⇒ Object



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
# File 'lib/vagrant-vrealize/action/run_instance.rb', line 45

def wait_for_ssh(env)
  if !env[:interrupted]
    env[:metrics]["instance_ssh_time"] = Util::Timer.time do
      # Wait for SSH to be ready.
      env[:ui].info(I18n.t("vagrant_vrealize.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]
        begin
          break if env[:machine].communicate.ready?
        rescue => e
          if network_ready_retries < network_ready_retries_max then
            network_ready_retries += 1
            @logger.warn(I18n.t("vagrant_vrealize.waiting_for_ssh, retrying"))
          else
            raise e
          end
        end
        sleep 2
      end
    end
  end
end