Class: LXC::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc/config.rb

Overview

Main Config Class

Author:

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Config) initialize(lxc, filename)

Returns a new instance of Config

Raises:



13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

- (Object) filename

Returns the value of attribute filename



11
12
13
# File 'lib/lxc/config.rb', line 11

def filename
  @filename
end

- (Object) networks

Returns the value of attribute networks



11
12
13
# File 'lib/lxc/config.rb', line 11

def networks
  @networks
end

Instance Method Details

- (Object) [](key)

Configuration Key/Value Query

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



91
92
93
# File 'lib/lxc/config.rb', line 91

def [](key)
  @config[key]
end

- (Object) []=(key, value)

Configuration Key/Value Assignment

Allows setting the internal hash values.



84
85
86
# File 'lib/lxc/config.rb', line 84

def []=(key, value)
  @config.merge!(key => [value]) { |k,o,n| k = (o + n) }
end

- (Hash) clear

Clear configuration

Clears out the current configuration, leaving an empty configuration behind

Returns:

  • (Hash)

    LXC configuration hash.



56
57
58
59
60
61
# File 'lib/lxc/config.rb', line 56

def clear
  @config   = Hash.new
  @networks = Array.new

  true
end

- (String) inspect

Provides a concise string representation of the class

Returns:

  • (String)


97
98
99
# File 'lib/lxc/config.rb', line 97

def inspect
  @config.inspect
end

- (Array) keys

Configuration keys

Returns all of the configuration keys

Returns:

  • (Array)

    An array of the current configurations keys.



68
69
70
# File 'lib/lxc/config.rb', line 68

def keys
  @config.keys
end

- (Hash) load

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.

Returns:

  • (Hash)

    LXC configuration hash.



29
30
31
# File 'lib/lxc/config.rb', line 29

def load
  parse_config(@lxc.exec("cat #{@filename} 2>/dev/null"))
end

- (Hash) save

Saves the specified LXC configuration

Saves the internal hash out to an LXC configuration file.

Returns:

  • (Hash)

    LXC configuration hash.



38
39
40
41
42
43
44
45
46
47
48
# 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

- (Array) values

Configuration Values

Returns all of the configuration values

Returns:

  • (Array)

    An array of the current configurations values.



77
78
79
# File 'lib/lxc/config.rb', line 77

def values
  @config.values
end