lib/elecksee/lxc_file_config.rb in elecksee-1.0.10 vs lib/elecksee/lxc_file_config.rb in elecksee-1.0.12
- old
+ new
@@ -3,17 +3,41 @@
attr_reader :network
attr_reader :base
class << self
+
+ def convert_to_hash(thing)
+ unless(thing.is_a?(Hash))
+ result = defined?(Mash) ? Mash.new : {}
+ thing.each do |k,v|
+ result[k] = v.respond_to?(:keys) && v.respond_to?(:values) ? convert_to_hash(v) : v
+ end
+ end
+ result || thing
+ end
+
+ def symbolize_hash(thing)
+ if(defined?(Mash))
+ Mash.new(thing)
+ else
+ result = {}
+ thing.each do |k,v|
+ result[k.to_sym] = v.is_a?(Hash) ? symbolize_hash(v) : v
+ end
+ result
+ end
+ end
+
def generate_config(resource)
+ resource = symbolize_hash(convert_to_hash(resource))
config = []
- config << "lxc.utsname = #{resource.utsname}"
- if(resource.aa_profile)
- config << "lxc.aa_profile = #{resource.aa_profile}"
+ config << "lxc.utsname = #{resource[:utsname]}"
+ if(resource[:aa_profile])
+ config << "lxc.aa_profile = #{resource[:aa_profile]}"
end
- [resource.network].flatten.each do |net_hash|
+ [resource.[:network]].flatten.each do |net_hash|
nhsh = Mash.new(net_hash)
flags = nhsh.delete(:flags)
%w(type link).each do |k|
config << "lxc.network.#{k} = #{nhsh.delete(k)}" if nhsh[k]
end
@@ -22,18 +46,18 @@
end
if(flags)
config << "lxc.network.flags = #{flags}"
end
end
- if(resource.cap_drop)
- config << "lxc.cap.drop = #{Array(resource.cap_drop).join(' ')}"
+ if(resource[:cap_drop])
+ config << "lxc.cap.drop = #{Array(resource[:cap_drop]).join(' ')}"
end
%w(pts tty arch devttydir mount mount_entry rootfs rootfs_mount pivotdir).each do |k|
- config << "lxc.#{k.sub('_', '.')} = #{resource.send(k)}" if resource.send(k)
+ config << "lxc.#{k.sub('_', '.')} = #{resource[k.to_sym]}" if resource[k.to_sym]
end
prefix = 'lxc.cgroup'
- resource.cgroup.each_pair do |key, value|
+ resource[:cgroup].each_pair do |key, value|
if(value.is_a?(Array))
value.each do |val|
config << "#{prefix}.#{key} = #{val}"
end
else
@@ -47,10 +71,10 @@
def initialize(path)
raise 'LXC config file not found' unless File.exists?(path)
@path = path
@network = []
- @base = Mash.new
+ @base = defined?(Mash) ? Mash.new : {}
parse!
end
private