Template: IPv6 understands auto, IPv4/IPv6 has sanity-functions.

DataCenter-description.
This commit is contained in:
Denis Knauf 2021-06-16 14:03:42 +02:00
parent 1ec6b5c6d1
commit 4a80534bfe

View file

@ -27,14 +27,27 @@ module PVE::CTTemplate
File.read( options[:'ssh-public-keys-file'] || '/root/.ssh/authorized_keys') File.read( options[:'ssh-public-keys-file'] || '/root/.ssh/authorized_keys')
end end
def _ipv4 ip, gw
return [ip, nil] if %w[dhcp].include? ip
ip = IPAddress::IPv4.new ip
[ip.to_string, gw || ip.hosts.last.to_s]
end
def _ipv6 ip, gw
return [ip, nil] if %w[dhcp auto].include? ip
ip = IPAddress::IPv6.new ip
[ip.to_string, gw || ip.hosts.last.to_s]
end
def net0() def net0()
if options.ipv4 || options.ipv6 if options.ipv4 || options.ipv6
ipv4 = IPAddress::IPv4.new options.ipv4 ipv4, gw4 = _ipv4( options.ipv4, options.gateway4)
ipv6, gw6 = _ipv6( options.ipv6, options.gateway6)
{ {
name: 'eth0', name: 'eth0',
bridge: 'vmbr1', bridge: 'vmbr1',
ip: ipv4.to_string, ip: ipv4, ip6: ipv6,
gw: options.gateway || ipv4.hosts.last.to_s gw: gw4, gw6: gw6,
} }
end end
end end
@ -44,6 +57,10 @@ module PVE::CTTemplate
end end
class Default < Base class Default < Base
def self.help
nil
end
def self.requirements def self.requirements
{ {
node: [:string, false, "Create CT on this node."], node: [:string, false, "Create CT on this node."],
@ -62,7 +79,7 @@ module PVE::CTTemplate
:'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."], :'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."],
ipv4: [:string, false, "IPv4-Address with net-size."], ipv4: [:string, false, "IPv4-Address with net-size."],
gateway4: [:string, false, "IPv4-Address of gateway."], gateway4: [:string, false, "IPv4-Address of gateway."],
ipv6: [:string, false, "IPv6-Address with net-size."], ipv6: [:string, false, "IPv6-Address with net-size or auto."],
gateway6: [:string, false, "IPv6-Address of gateway."], gateway6: [:string, false, "IPv6-Address of gateway."],
storage: [:string, false, "Device will be create on this Storage (default: local"], storage: [:string, false, "Device will be create on this Storage (default: local"],
} }
@ -70,12 +87,24 @@ module PVE::CTTemplate
end end
class Datacenter < Base class Datacenter < Base
def self.help
<<-EOF.gsub /^ {6}/, ''
Datacenter provides an interface for special network-settings.
Networks in Datacenters are often based on this behaviour:
A Network has an ID like 99.
This defines the VLANs: 2099 for Layer2/3099 for Layer3.
The IPv4-Range would be 10.99.0.0/16, but container will be put static in 10.99.255.0/24.
IPv6 uses RADV, so we do not need to know the IPv6-Range => auto.
VMID can be generated by Network-ID, too: smallest unused VMID in 100*ID.
EOF
end
def self.requirements def self.requirements
{ {
node: [:string, false, "Create CT on this node."], node: [:string, false, "Create CT on this node."],
name: [:string, true, "Set (uniq) name"], name: [:string, true, "Set (uniq) name"],
arch: [:enum, false, "Architecture", %w[amd64 i386 arm64 armhf]], arch: [:enum, false, "Architecture", %w[amd64 i386 arm64 armhf]],
vmid: [:numeric, true, "VM-ID. Proxmox internal number (100...)"], vmid: [:numeric, false, "VM-ID. Proxmox internal number (100...)"],
ostype: [:string, true, "OS-Type (OS or distribution)"], ostype: [:string, true, "OS-Type (OS or distribution)"],
cmode: [:enum, false, "Console-mode", %w[shell console tty]], cmode: [:enum, false, "Console-mode", %w[shell console tty]],
cores: [:numeric, false, "Count of cores"], cores: [:numeric, false, "Count of cores"],
@ -87,12 +116,12 @@ module PVE::CTTemplate
:'ssh-public-keys' => [:string, false, "SSH-Public-Keys, which should be added to root-user in CT."], :'ssh-public-keys' => [:string, false, "SSH-Public-Keys, which should be added to root-user in CT."],
:'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."], :'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."],
:'network-id' => [:numeric, true, "Put Container to this VLAN and use a random IPv4-Address for this CT."], :'network-id' => [:numeric, true, "Put Container to this VLAN and use a random IPv4-Address for this CT."],
ipv4: [:string, false, "IPv4-Address with net-size."], ipv4: [:string, false, "IPv4-Address with net-size or dhcp."],
gateway4: [:string, false, "IPv4-Address of gateway."], gateway4: [:string, false, "IPv4-Address of gateway."],
ipv6: [:string, false, "IPv6-Address with net-size."], ipv6: [:string, false, "IPv6-Address with net-size or auto|dhcp."],
gateway6: [:string, false, "IPv6-Address of gateway."], gateway6: [:string, false, "IPv6-Address of gateway."],
storage: [:string, false, "Device will be create on this Storage (default: root)"], storage: [:string, false, "Device will be create on this Storage (default: root)"],
ostemplate: [:string, false, "OS-Template eg. vztmp/superlinux-1.2-amd64.tar.xz"], ostemplate: [:string, false, "OS-Template eg. local:vztmpl/superlinux-1.2-amd64.tar.xz"],
} }
end end
@ -112,15 +141,27 @@ module PVE::CTTemplate
end end
def net0 def net0
ipv4, gw4 =
if options.ipv4
_ipv4( options.ipv4, options.gateway4)
else
self.ipv4_gw
end
ipv6, gw6 =
if options.ipv6
_ipv6( options.ipv6, options.gateway6)
else
['auto', nil]
end
{ {
name: 'eth0', name: 'eth0',
bridge: 'vmbr1', bridge: 'vmbr1',
tag: 2000+network_id, tag: 2000+network_id,
mtu: 9166, mtu: 9166,
firewall: 1, firewall: 1,
ip: ipv4.to_string, ip: ipv4, gw: gw4,
gw: ipv4.hosts.last.to_s, ip6: ipv6, gw6: gw6,
} }.delete_if {|k,v| v.nil? }
end end
def vmid def vmid
@ -131,14 +172,13 @@ module PVE::CTTemplate
IPAddress::IPv4.new "10.#{network_id}.255.0/24" IPAddress::IPv4.new "10.#{network_id}.255.0/24"
end end
def ipv4 def ipv4_gw
return options.ipv4 if options.ipv4 return @ipv4_gw if @ipv4_gw
return @ipv4 if @ipv4
ipv4s = network.hosts ipv4s = network.hosts
@virts.each do |v| @virts.each do |v|
v.config[:network].each {|n| ipv4s.delete n[:ip] if n[:ip] } v.config[:network].each {|n| ipv4s.delete n[:ip] if n[:ip] }
end end
@ipv4 = ipv4s.first @ipv4_gw = [ipv4s.first.to_string, network.last.to_s]
end end
def ostemplate def ostemplate