Class: Pvectl::Parsers::LxcNetConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/parsers/lxc_net_config.rb

Overview

Parses and formats LXC network configurations for Proxmox containers.

LXC network format differs from QEMU: uses name/type instead of model, and supports IP configuration directly in the network spec.

Examples:

Parsing a net config string

config = LxcNetConfig.parse("bridge=vmbr0,name=eth0,ip=dhcp")
config[:bridge] #=> "vmbr0"
config[:name]   #=> "eth0"
config[:ip]     #=> "dhcp"

Converting to Proxmox API format

LxcNetConfig.to_proxmox({ bridge: "vmbr0", ip: "dhcp" })
#=> "name=eth0,bridge=vmbr0,ip=dhcp,type=veth"

Constant Summary collapse

VALID_KEYS =

All recognized LXC network configuration keys.

%w[bridge name ip gw ip6 gw6 tag firewall mtu rate type].freeze
REQUIRED_KEYS =

Keys that must be present in every network configuration.

%w[bridge].freeze
OPTIONAL_FLAGS =

Optional flags appended to the Proxmox API string.

%w[ip gw ip6 gw6 tag firewall mtu rate].freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Hash<Symbol, String>

Parses a comma-separated key=value LXC net config string into a Hash.

Examples:

LxcNetConfig.parse("bridge=vmbr0,name=eth0,ip=dhcp")
#=> { bridge: "vmbr0", name: "eth0", ip: "dhcp" }

Parameters:

  • string (String)

    net config in “key=value,key=value” format

Returns:

  • (Hash<Symbol, String>)

    parsed configuration

Raises:

  • (ArgumentError)

    if unknown keys are present or required keys are missing



39
40
41
42
43
44
45
# File 'lib/pvectl/parsers/lxc_net_config.rb', line 39

def self.parse(string)
  pairs = string.split(",").map { |pair| pair.strip.split("=", 2).map(&:strip) }
  config = pairs.to_h { |k, v| [k.to_sym, v] }

  validate!(config)
  config
end

.to_proxmox(config) ⇒ String

Converts a parsed LXC net config Hash to a Proxmox API string.

The Proxmox API expects LXC network specifications in the format “name=eth0,bridge=vmbr0,,type=veth”. Name defaults to “eth0” and type defaults to “veth” when not specified.

Examples:

Minimal config

LxcNetConfig.to_proxmox({ bridge: "vmbr0" })
#=> "name=eth0,bridge=vmbr0,type=veth"

With IP and gateway

LxcNetConfig.to_proxmox({ bridge: "vmbr0", ip: "10.0.0.5/24", gw: "10.0.0.1" })
#=> "name=eth0,bridge=vmbr0,ip=10.0.0.5/24,gw=10.0.0.1,type=veth"

Parameters:

  • config (Hash<Symbol, String>)

    parsed network configuration

Returns:

  • (String)

    Proxmox API-compatible network string



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pvectl/parsers/lxc_net_config.rb', line 63

def self.to_proxmox(config)
  name = config[:name] || "eth0"
  type = config[:type] || "veth"
  parts = ["name=#{name}", "bridge=#{config[:bridge]}"]

  OPTIONAL_FLAGS.each do |flag|
    parts << "#{flag}=#{config[flag.to_sym]}" if config[flag.to_sym]
  end

  parts << "type=#{type}"
  parts.join(",")
end