Class: VagrantPlugins::XenServer::Action::CreateVIFs

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-xenserver/action/create_vifs.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateVIFs

Returns a new instance of CreateVIFs.



8
9
10
11
# File 'lib/vagrant-xenserver/action/create_vifs.rb', line 8

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("vagrant::xenserver::actions::create_vifs")
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-xenserver/action/create_vifs.rb', line 35

def call(env)
  vm_ref = env[:machine].id

  networks = env[:xc].network.get_all_records

  # Remove all current VIFs
  current_vifs = env[:xc].VM.get_VIFs(vm_ref)
  current_vifs.each { |vif| env[:xc].VIF.destroy(vif) }

  # If a HIMN VIF has been asked for, create one
  if env[:machine].provider_config.use_himn
    himn = networks.find { |ref,net| net['other_config']['is_host_internal_management_network'] }
    (himn_ref,himn_rec) = himn

    @logger.debug("himn="+himn.to_s)

    create_vif(env, vm_ref, himn_ref, '')
  end


  env[:machine].config.vm.networks.each do |type, options|
    @logger.info "got an interface: #{type} #{options}"

    if type == :public_network then
      bridge = options[:bridge]
      mac = options[:mac] || ''

      netrefrec = networks.find { |ref,net| net['bridge']==bridge }
      (net_ref,net_rec) = netrefrec
      if net_ref.nil? then
          @logger.error("Error finding bridge #{bridge} on host")
          raise Errors::NoHostsAvailable
      end

      vif_res = create_vif(env, vm_ref, net_ref, mac)

      @logger.info("vif_res=" + vif_res.to_s)
    end
  end

  @app.call env
end

#create_vif(env, vm, network, mac) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-xenserver/action/create_vifs.rb', line 13

def create_vif(env, vm, network, mac)
  vif_devices = env[:xc].VM.get_allowed_VIF_devices(vm)

  vif_record = {
    'VM' => vm,
    'network' => network,
    'device' => vif_devices[0],
    'MAC' => mac,
    'MTU' => '1500',
    'other_config' => {},
    'qos_algorithm_type' => '',
    'qos_algorithm_params' => {},
    'locking_mode' => 'network_default',
    'ipv4_allowed' => [],
    'ipv6_allowed' => []
  }

  vif_res = env[:xc].VIF.create(vif_record)

  return vif_res
end