Module: ForemanFogProxmox::ProxmoxInterfaces

Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_interfaces.rb

Instance Method Summary collapse

Instance Method Details

#check_cidr(nic_compute_attributes, v6, ip) ⇒ Object

Raises:

  • (::Foreman::Exception)


72
73
74
75
76
77
78
79
80
81
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 72

def check_cidr(nic_compute_attributes, v6, ip)
  valid = Fog::Proxmox::IpHelper.send(cidr_prefix_method(v6), cidr_prefix(nic_compute_attributes, v6))
  ipv = "IPv#{v6 ? '6' : '4'}"
  max = v6 ? 128 : 32
  checked = valid || ForemanFogProxmox::Value.empty?(ip)
  message = format(
    'Invalid Interface Proxmox CIDR %<ip>s. If %<ip>s is not empty, Proxmox CIDR prefix must be an integer between 0 and %<max>i.', ip: ipv, max: max
  )
  raise ::Foreman::Exception, _(message) unless checked
end

#cidr_prefix(nic_compute_attributes, v6 = false) ⇒ Object



63
64
65
66
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 63

def cidr_prefix(nic_compute_attributes, v6 = false)
  attr_name = "cidr#{v6_s(v6)}"
  nic_compute_attributes[attr_name] if nic_compute_attributes.key?(attr_name)
end

#cidr_prefix_method(v6) ⇒ Object



68
69
70
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 68

def cidr_prefix_method(v6)
  "cidr#{v6_s(v6)}_prefix?".to_sym
end

#container?(host) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 46

def container?(host)
  vm_type(host) == 'lxc'
end

#container_nic_name_valid?(nic) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 50

def container_nic_name_valid?(nic)
  /^(eth)(\d+)$/.match?(nic.compute_attributes['name'])
end

#dhcp?(nic_compute_attributes, v6 = false) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 115

def dhcp?(nic_compute_attributes, v6 = false)
  attr_name = "dhcp#{v6_s(v6)}"
  nic_compute_attributes.key?(attr_name) ? to_boolean(nic_compute_attributes[attr_name]) : false
end

#editable_network_interfaces?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 25

def editable_network_interfaces?
  true
end

#host_interfaces_attrs(host) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 126

def host_interfaces_attrs(host)
  host.interfaces.select(&:physical?).each.with_index.reduce({}) do |hash, (nic, index)|
    set_nic_identifier(nic, index)
    set_container_interface_name(host, nic, index) if container?(host)
    ForemanFogProxmox::HashCollection.remove_empty_values(nic.compute_attributes)
    mac = nic.mac
    mac ||= nic.attributes['mac']
    set_mac(nic.compute_attributes, mac, vm_type(host)) if mac.present?
    interface_compute_attributes = if host.compute_attributes['interfaces_attributes']
                                     host.compute_attributes['interfaces_attributes'].select do |_k, v|
                                       v['id'] == nic.compute_attributes[:id]
                                     end
                                   else
                                     {}
                                   end
    unless interface_compute_attributes.empty?
      nic.compute_attributes.store(:_delete,
        interface_compute_attributes[interface_compute_attributes.keys[0]]['_delete'])
    end
    set_ip(host, nic, nic.compute_attributes)
    set_ip(host, nic, nic.compute_attributes, true)
    ForemanFogProxmox::HashCollection.remove_keys(nic.compute_attributes, ['dhcp', 'dhcp6', 'cidr', 'cidr6'])
    hash.merge(index.to_s => nic.compute_attributes)
  end
end

#ip_s(v6) ⇒ Object



87
88
89
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 87

def ip_s(v6)
  "ip#{v6_s(v6)}"
end

#set_container_interface_name(_host, nic, index) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 54

def set_container_interface_name(_host, nic, index)
  nic.compute_attributes['name'] = format('eth%<index>s', index: index) if nic.compute_attributes['name'].empty?
  unless container_nic_name_valid?(nic)
    raise ::Foreman::Exception,
      _(format('Invalid name interface[%<index>s]. Must be eth[n] with n integer >= 0',
        index: index))
  end
end

#set_ip(host, nic, nic_compute_attributes, v6 = false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 95

def set_ip(host, nic, nic_compute_attributes, v6 = false)
  ip = nic.send(ip_s(v6).to_sym)
  if container?(host)
    if dhcp?(nic_compute_attributes, v6)
      ip = 'dhcp'
    elsif !ForemanFogProxmox::Value.empty?(cidr_prefix(nic_compute_attributes, v6))
      check_cidr(nic_compute_attributes, v6, ip)
      if ip
        ip = Fog::Proxmox::IpHelper.send(to_cidr_method(v6), nic.send(ip_s(v6).to_sym),
          cidr_prefix(nic_compute_attributes, v6))
      end
    end
  end
  nic_compute_attributes[ip_s(v6).to_sym] = ip
end

#set_mac(nic_compute_attributes, mac, type) ⇒ Object



120
121
122
123
124
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 120

def set_mac(nic_compute_attributes, mac, type)
  mac_attr_name = { 'qemu' => :macaddr, 'lxc' => :hwaddr }
  mac_key = mac_attr_name[type] || 'mac'
  nic_compute_attributes[mac_key] = Net::Validations.normalize_mac(mac)
end

#set_nic_identifier(nic, index) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 29

def set_nic_identifier(nic, index)
  nic.compute_attributes[:id] = format('net%<index>s', index: index) if nic.compute_attributes[:id].empty?
  unless Fog::Proxmox::NicHelper.nic?(nic.compute_attributes[:id])
    raise ::Foreman::Exception,
      _(format('Invalid proxmox NIC id on interface[%<index>s]. Must be net[n] with n integer >= 0',
        index: index))
  end

  nic.identifier = nic.compute_attributes['id'] if nic.identifier.empty?
end

#to_boolean(value) ⇒ Object



111
112
113
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 111

def to_boolean(value)
  [1, true, '1', 'true'].include?(value)
end

#to_cidr_method(v6) ⇒ Object



91
92
93
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 91

def to_cidr_method(v6)
  "to_cidr#{v6_s(v6)}".to_sym
end

#v6_s(v6) ⇒ Object



83
84
85
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 83

def v6_s(v6)
  v6 ? '6' : ''
end

#vm_type(host) ⇒ Object



40
41
42
43
44
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 40

def vm_type(host)
  type = host.compute_attributes['type']
  type ||= host.compute_attributes[:config_attributes].key?(:arch) ? 'lxc' : 'qemu'
  type
end