Class: VagrantPlugins::Abiquo::Actions::Create

Inherits:
Object
  • Object
show all
Includes:
Helpers::Abiquo
Defined in:
lib/vagrant_abiquo/actions/create.rb

Instance Method Summary collapse

Methods included from Helpers::Abiquo

#apply_state, #attach_net, #create_vapp, #create_vm, #deploy, #get_network, #get_template, #get_vapp, #get_vdc, #get_vm, #poweroff, #poweron, #reset, #update, #vapp_name

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



9
10
11
12
13
14
# File 'lib/vagrant_abiquo/actions/create.rb', line 9

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @env = env
  @logger = Log4r::Logger.new('vagrant::abiquo::create')
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant_abiquo/actions/create.rb', line 16

def call(env)
  client = env[:abiquo_client]
  
  # Find for selected virtual datacenter
  vdc = get_vdc(client, @machine.provider_config.virtualdatacenter)
  raise Abiquo::Errors::VDCNotFound, vdc: @machine.provider_config.virtualdatacenter if vdc.nil?
  
  # Check if we have to use hwprofiles
  lim = vdc.link(:enterprise).get.link(:limits).get.select {|l| l.link(:location).title == vdc.link(:location).title }.first
  if lim.respond_to?(:enabledHardwareProfiles) && lim.enabledHardwareProfiles
    if @machine.provider_config.hwprofile.nil?
      raise Abiquo::Errors::HWprofileEnabled, vdc: @machine.provider_config.virtualdatacenter
    end
  end

  # Find for selected virtual appliance
  vname = vapp_name(@machine)
  vapp = get_vapp(vdc, vname)

  # Find for selected vm template
  template = get_template(vdc, @machine.provider_config.template)
  raise Abiquo::Errors::TemplateNotFound, template: @machine.provider_config.template, vdc: vdc.name if template.nil?
  
  # If everything is OK we can proceed to create the VM
  # VM Template link
  tmpl_link = template.link(:edit).clone.to_hash
  tmpl_link['rel'] = "virtualmachinetemplate"
  
  # VM entity
  vm_definition = {}
  
  # Configured CPU and RAM
  if lim.respond_to?(:enabledHardwareProfiles) && lim.enabledHardwareProfiles
      # lookup the hwprofile link
      hwprofile = vdc.link(:location).get.link(:hardwareprofiles).get
                        .select {|h| h.name == @machine.provider_config.hwprofile }.first
      raise Abiquo::Errors::HWProfileNotFound, hwprofile: @machine.provider_config.hwprofile, vdc: vdc.name if hwprofile.nil?
      hwprofile_lnk = hwprofile.link(:self).clone.to_hash
      hwprofile_lnk['rel'] = 'hardwareprofile'

      vm_definition['links'] = [ tmpl_link, hwprofile_lnk ]
  else
    cpu_cores = @machine.provider_config.cpu_cores
    ram_mb = @machine.provider_config.ram_mb
    vm_definition['cpu'] = cpu_cores || template.cpuRequired
    vm_definition['ram'] = ram_mb || template.ramRequired
    vm_definition['links'] = [ tmpl_link ]
  end

  vm_definition['label'] = @machine.name
  vm_definition['vdrpEnabled'] = true

  # Create VM
  env[:ui].info I18n.t('vagrant_abiquo.info.create')
  vm = create_vm(client, vm_definition, vapp)

  # User Data
  md = vm.link(:metadata).get
  mdhash = JSON.parse(md.to_json)
  if mdhash['metadata'].nil?
    mdhash['metadata'] = { 'startup-script' => @machine.provider_config.user_data }
  else
    mdhash['metadata']['startup-script'] = @machine.provider_config.user_data
  end
  client.put(vm.link(:metadata), mdhash.to_json)

  # Check network
  unless @machine.provider_config.network.nil?
    # Network config is not nil, so we have
    # to attach a specific net.
    attach_net(client, vm, @machine.provider_config.network)
    raise Abiquo::Errors::NetworkError if vm.nil?
  end
  vm = vm.link(:edit).get
  @machine.id = vm.url

  @app.call(env)
end