Class: PVE::CTTemplate::Datacenter

Inherits:
Base
  • Object
show all
Defined in:
lib/pve/templates.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_ipv4, #_ipv6, #arch, #cmode, #cores, #description, #hostname, #initialize, #name, #net1, #net2, #net3, #ssh_public_keys, #swap, #unprivileged

Constructor Details

This class inherits a constructor from PVE::CTTemplate::Base

Class Method Details

.helpObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pve/templates.rb', line 90

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

.requirementsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pve/templates.rb', line 102

def self.requirements
  {
    node:                      [:string,  false, "Create CT on this node."],
    name:                      [:string,  true,  "Set (uniq) name"],
    arch:                      [:enum,    false, "Architecture", %w[amd64 i386 arm64 armhf]],
    vmid:                      [:numeric, false, "VM-ID.  Proxmox internal number (100...)"],
    ostype:                    [:string,  true,  "OS-Type (OS or distribution)"],
    cmode:                     [:enum,    false, "Console-mode", %w[shell console tty]],
    cores:                     [:numeric, false, "Count of cores"],
    description:               [:string,  false, "Description.  Eg. What should this CT do?"],
    hostname:                  [:string,  false, "Hostname"],
    memory:                    [:numeric, false, "How much memory CT could use?"],
    swap:                      [:numeric, false, "How much CT can swap?"],
    unprivileged:              [:boolean, false, "Unprivileged are restricted to own UID/GID-space."],
    :'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."],
    :'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 or dhcp."],
    gateway4:                  [:string,  false, "IPv4-Address of gateway."],
    ipv6:                      [:string,  false, "IPv6-Address with net-size or auto|dhcp."],
    gateway6:                  [:string,  false, "IPv6-Address of gateway."],
    storage:                   [:string,  false, "Device will be create on this Storage (default: root)"],
    ostemplate:                [:string,  false, "OS-Template eg. local:vztmpl/superlinux-1.2-amd64.tar.xz"],
  }
end

Instance Method Details

#ipv4_gwObject



175
176
177
178
179
180
181
182
# File 'lib/pve/templates.rb', line 175

def ipv4_gw
  return @ipv4_gw  if @ipv4_gw
  ipv4s = network.hosts
  @virts.each do |v|
    v.config[:network].each {|n| ipv4s.delete n[:ip]  if n[:ip] }
  end
  @ipv4_gw = [ipv4s.first.to_string, network.last.to_s]
end

#memoryObject



130
# File 'lib/pve/templates.rb', line 130

def memory() options.memory || 2048 end

#net0Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pve/templates.rb', line 143

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',
    bridge: 'vmbr1',
    tag: 2000+network_id,
    mtu: 9166,
    firewall: 1,
    ip: ipv4, gw: gw4,
    ip6: ipv6, gw6: gw6,
  }.delete_if {|k,v| v.nil? }
end

#networkObject



171
172
173
# File 'lib/pve/templates.rb', line 171

def network
  IPAddress::IPv4.new "10.#{network_id}.255.0/24"
end

#network_idObject



133
134
135
136
137
138
139
140
141
# File 'lib/pve/templates.rb', line 133

def network_id
  return @network_id  if @network_id
  expect = 0..12
  nid = options[:'network-id']
  unless nid == nid.to_i.to_s && expect.include?( nid.to_i)
    raise ArgumentError, "Network ID must be #{expect.inspect}.  Given: #{nid.inspect}"
  end
  @network_id = nid.to_i
end

#nodeObject



128
# File 'lib/pve/templates.rb', line 128

def node() options.node || 'svc1' end

#ostemplateObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pve/templates.rb', line 184

def ostemplate
  @ostemplate ||=
    options.ostemplate ||
      case ostype
      when 'debian'
        # TODO: how to determine which template?
        'local:vztmpl/debian-11-standard_11.0-1_amd64.tar.gz'
      else
        raise ArgumentError, "OS-Template for ostype #{ostype} not found or ostemplate not provided."
      end
end

#ostypeObject



129
# File 'lib/pve/templates.rb', line 129

def ostype() options.ostype || 'debian' end

#storageObject



131
# File 'lib/pve/templates.rb', line 131

def storage() options.storage || 'root' end

#vmidObject



167
168
169
# File 'lib/pve/templates.rb', line 167

def vmid
  super || ((0...100).map {|i| "#{100*network_id+i}" } - @virts.map( &:vmid)).first
end