Class: VagrantPlugins::OVirtProvider::Action::CreateVM

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateVM

Returns a new instance of CreateVM.



10
11
12
13
# File 'lib/vagrant-ovirt/action/create_vm.rb', line 10

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

Instance Method Details

#call(env) ⇒ Object

Raises:

  • (Error::NoClusterError)


15
16
17
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
92
93
94
95
96
97
98
# File 'lib/vagrant-ovirt/action/create_vm.rb', line 15

def call(env)
  # Get config.
  config = env[:machine].provider_config

  # Gather some info about domain
  name = env[:domain_name]
  cpus = config.cpus
  memory_size = config.memory*1024

  # Get cluster
  if config.cluster == nil
    cluster = env[:ovirt_compute].clusters.first
  else
    cluster = OVirtProvider::Util::Collection.find_matching(
      env[:ovirt_compute].clusters.all, config.cluster)
  end
  raise Error::NoClusterError if cluster == nil
  # TODO fill env also with other ovirtoptions.
  env[:ovirt_cluster] = cluster

  # Get template
  template = OVirtProvider::Util::Collection.find_matching(
    env[:ovirt_compute].templates.all, config.template)
  if template == nil
    raise Error::NoTemplateError,
      :template_name => config.template
  end

  # Get quota
  if config.quota == nil
    quota = env[:ovirt_client].quotas.first
  else
    quota = OVirtProvider::Util::Collection.find_matching(
      env[:ovirt_client].quotas.all, config.quota)
  end
  raise Error::NoQuotaError if quota == nil

  # Output the settings we're going to use to the user
  env[:ui].info(I18n.t("vagrant_ovirt.creating_vm"))
  env[:ui].info(" -- Name:          #{name}")
  env[:ui].info(" -- Cpus:          #{cpus}")
  env[:ui].info(" -- Memory:        #{memory_size/1024}M")
  env[:ui].info(" -- Base box:      #{env[:machine].box.name}")
  env[:ui].info(" -- Template:      #{template.name}")
  env[:ui].info(" -- Quota:         #{quota.name}")
  env[:ui].info(" -- Datacenter:    #{config.datacenter}")
  env[:ui].info(" -- Cluster:       #{cluster.name}")

  # Create oVirt VM.
  attr = {
      :name     => name,
      :cores    => cpus,
      :memory   => memory_size*1024,
      :cluster  => cluster.id,
      :template => template.id,
      :quota    => quota.id,
  }

  begin
    server = env[:ovirt_compute].servers.create(attr)
  rescue OVIRT::OvirtException => e
    raise Errors::FogCreateServerError,
      :error_message => e.message
  end

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

  # Wait till all volumes are ready.
  env[:ui].info(I18n.t("vagrant_ovirt.wait_for_ready_vm"))
  for i in 0..5
    ready = true
    server.volumes.each do |volume|
      if volume.status != 'ok'
        ready = false
        break
      end
    end
    break if ready
    sleep 2
  end

  @app.call(env)
end

#recover(env) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-ovirt/action/create_vm.rb', line 100

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

  # Undo the import
  env[:ui].info(I18n.t("vagrant_ovirt.error_recovering"))
  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