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



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



39
40
41
# File 'lib/lxc/configuration.rb', line 39

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



18
19
20
21
22
23
24
# 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



35
36
37
# File 'lib/lxc/configuration.rb', line 35

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

#attributesArray

Get all configuration attributes



28
29
30
# File 'lib/lxc/configuration.rb', line 28

def attributes
  @content.keys
end

#save_to_file(path) ⇒ Object

Save configuration into file



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lxc/configuration.rb', line 45

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') { |f| f.write(lines.flatten.join("\n")) }
end