class LXC::Config

Main Config Class

@author Zachary Patten <zachary AT jovelabs DOT com>

Constants

NETWORK_KEY_ORDER

Attributes

filename[RW]
networks[RW]

Public Class Methods

new(lxc, filename) click to toggle source
# File lib/lxc/config.rb, line 13
def initialize(lxc, filename)
  raise ConfigError, "You must supply a LXC object!" if lxc.nil?
  raise ConfigError, "You must supply a configuration filename!" if filename.nil?

  @lxc      = lxc
  @filename = filename

  self.clear
end

Public Instance Methods

[](key) click to toggle source

Configuration Key/Value Query

Allows getting the internal hash value for an internal hash key.

# File lib/lxc/config.rb, line 91
def [](key)
  @config[key]
end
[]=(key, value) click to toggle source

Configuration Key/Value Assignment

Allows setting the internal hash values.

# File lib/lxc/config.rb, line 84
def []=(key, value)
  @config.merge!(key => [value]) { |k,o,n| k = (o + n) }
end
clear() click to toggle source

Clear configuration

Clears out the current configuration, leaving an empty configuration behind

@return [Hash] LXC configuration hash.

# File lib/lxc/config.rb, line 56
def clear
  @config   = Hash.new
  @networks = Array.new

  true
end
inspect() click to toggle source

Provides a concise string representation of the class @return [String]

# File lib/lxc/config.rb, line 97
def inspect
  @config.inspect
end
keys() click to toggle source

Configuration keys

Returns all of the configuration keys

@return [Array] An array of the current configurations keys.

# File lib/lxc/config.rb, line 68
def keys
  @config.keys
end
load() click to toggle source

Loads the specified LXC configuration

Loads the filename specified at instantiation and converts it to an internal hash so that we can manipulate it easier.

@return [Hash] LXC configuration hash.

# File lib/lxc/config.rb, line 29
def load
  parse_config(@lxc.exec("cat #{@filename} 2>/dev/null"))
end
save() click to toggle source

Saves the specified LXC configuration

Saves the internal hash out to an LXC configuration file.

@return [Hash] LXC configuration hash.

# File lib/lxc/config.rb, line 38
def save
  use_sudo = (@lxc.runner.use_sudo ? 'sudo ' : nil)

  script = Array.new
  script << "cat <<EOF | #{use_sudo}tee #{@filename}"
  script << build_config
  script << "EOF"
  script = script.join("\n")

  @lxc.exec(script)
end
values() click to toggle source

Configuration Values

Returns all of the configuration values

@return [Array] An array of the current configurations values.

# File lib/lxc/config.rb, line 77
def values
  @config.values
end

Private Instance Methods

build_config() click to toggle source
# File lib/lxc/config.rb, line 119
def build_config
  content = Array.new
  @config.each do |key, value|
    content << build_values(key, value)
  end
  @networks.each do |network|
    network_keys = (network.keys - NETWORK_KEY_ORDER)
    NETWORK_KEY_ORDER.each do |key|
      network.has_key?(key) and (content << build_values(key, network[key]))
    end
    network_keys.each do |key|
      content << build_values(key, network[key])
    end
  end
  content.join("\n")
end
build_values(key, value) click to toggle source
# File lib/lxc/config.rb, line 105
def build_values(key, value)
  content = Array.new

  if value.is_a?(Array)
    value.each do |v|
      content << "#{key} = #{v}"
    end
  else
    content << "#{key} = #{value}"
  end

  content
end
parse_config(content) click to toggle source
# File lib/lxc/config.rb, line 136
def parse_config(content)
  @config   = Hash.new
  @networks = Array.new
  network   = nil

  content.split("\n").map(&:strip).each do |line|
    key, value = line.split('=').map(&:strip)
    if key =~ /lxc\.network/
      if key =~ /lxc\.network\.type/
        # this is a new network object
        @networks << network
        network = Hash.new
      else
        # add to previous network object
      end
      network.merge!(key => [value]) { |k,o,n| k = (o + n) }
    else
      @config.merge!(key => [value]) { |k,o,n| k = (o + n) }
    end
  end
  @networks << network
  @networks.compact!

  true
end