class LXC::Config
Main Config
Class
@author Zachary Patten <zachary AT jovelabs DOT com>
Constants
- NETWORK_KEY_ORDER
Attributes
Public Class Methods
# 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
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
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 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
Provides a concise string representation of the class @return [String]
# File lib/lxc/config.rb, line 97 def inspect @config.inspect end
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
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
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
# 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
# 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
# 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