lib/elecksee/lxc_file_config.rb in elecksee-1.0.22 vs lib/elecksee/lxc_file_config.rb in elecksee-1.1.0

- old
+ new

@@ -1,13 +1,23 @@ +require 'elecksee' + class Lxc + # Configuration file interface class FileConfig + # @return [Array] attr_reader :network + # @return [String] path to configuration file attr_reader :base class << self + # Convert object to Hash if possible + # + # @param thing [Object] + # @return [Hash] + # @note used mostly for the lxc resource within chef def convert_to_hash(thing) if(defined?(Chef) && thing.is_a?(Chef::Resource)) result = Hash[*( (thing.methods - Chef::Resource.instance_methods).map{ |key| unless(key.to_s.start_with?('_') || thing.send(key).nil?) @@ -24,10 +34,14 @@ end end result || thing end + # Symbolize keys within hash + # + # @param thing [Hashish] + # @return [Hash] def symbolize_hash(thing) if(defined?(Mash)) Mash.new(thing) else result = {} @@ -36,10 +50,14 @@ end result end end + # Generate configuration file contents + # + # @param resource [Hashish] + # @return [String] def generate_config(resource) resource = symbolize_hash(convert_to_hash(resource)) config = [] config << "lxc.utsname = #{resource[:utsname]}" if(resource[:aa_profile]) @@ -77,20 +95,26 @@ config.join("\n") + "\n" end end + # Create new instance + # + # @param path [String] def initialize(path) raise 'LXC config file not found' unless File.exists?(path) @path = path @network = [] @base = defined?(Mash) ? Mash.new : {} parse! end private + # Parse the configuration file + # + # @return [TrueClass] def parse! cur_net = nil File.readlines(@path).each do |line| if(line.start_with?('lxc.network')) parts = line.split('=') @@ -113,8 +137,10 @@ @base[name] = parts.last end end end @network << cur_net if cur_net + true end + end end