Class: LXC::Configuration

Inherits:
Object
  • Object
show all
Includes:
ConfigurationOptions
Defined in:
lib/lxc/configuration.rb

Constant Summary

Constants included from ConfigurationOptions

LXC::ConfigurationOptions::VALID_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Configuration

Initialize a new LXC::Configuration instance

Parameters:

  • string (data)

    or hash data (optional)



9
10
11
12
13
# File 'lib/lxc/configuration.rb', line 9

def initialize(data=nil)
  if data.kind_of?(String)
    @content = parse(data)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object



41
42
43
# File 'lib/lxc/configuration.rb', line 41

def method_missing(key)
  @content[key.to_s]
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/lxc/configuration.rb', line 5

def content
  @content
end

Class Method Details

.load_file(path) ⇒ LXC::Configuration

Load an existing LXC container configuration from file

Parameters:

  • path (path)

    to configuration

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/lxc/configuration.rb', line 18

def self.load_file(path)
  fullpath = File.expand_path(path)

  if !File.exists?(fullpath)
    raise ArgumentError, "File '#{path}' does not exist."
  end

  LXC::Configuration.new(File.read(fullpath))
end

Instance Method Details

#[](key) ⇒ String

Get attribute value

Parameters:

  • attribute (key)

    name

Returns:

  • (String)

    configuration attribute value



37
38
39
# File 'lib/lxc/configuration.rb', line 37

def [](key)
  @content[key.to_s]
end

#attributesArray

Get all configuration attributes

Returns:

  • (Array)


30
31
32
# File 'lib/lxc/configuration.rb', line 30

def attributes
  @content.keys
end

#save_to_file(path) ⇒ Object

Save configuration into file

Parameters:

  • path (path)

    to output file



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lxc/configuration.rb', line 47

def save_to_file(path)
  fullpath = File.expand_path(path)
  lines = []

  @content.each_pair do |key,value|
    k = "lxc.#{key.gsub('_', '.')}"

    if value.kind_of?(Array)
      lines << value.map { |v| "#{k} = #{v}" }
    else
      lines << "#{k} = #{value}"
    end
  end

  File.open(path, 'w') do |f|
    f.write(lines.flatten.join("\n"))
  end
end