summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-02-07 03:15:25 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-02-07 03:15:25 +0000
commit39d33ca6987b2b98663edfa061cac7f4f69f9fb3 (patch)
tree3fd8600d6bce4d454fb7b215291c552b847bdeb7
parent4ecfa7bf4a0f88334afdec359f317b3bf7b44c21 (diff)
downloadpuppet-39d33ca6987b2b98663edfa061cac7f4f69f9fb3.tar.gz
puppet-39d33ca6987b2b98663edfa061cac7f4f69f9fb3.tar.xz
puppet-39d33ca6987b2b98663edfa061cac7f4f69f9fb3.zip
Temporary commit; configs now can be converted to manifests
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@869 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/config.rb69
-rwxr-xr-xtest/other/config.rb20
2 files changed, 86 insertions, 3 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb
index 3f9f65c47..22e3ef003 100644
--- a/lib/puppet/config.rb
+++ b/lib/puppet/config.rb
@@ -20,6 +20,9 @@ class Config
unless @config.include?(param)
@config[param] = newelement(param, value)
end
+ unless @order.include?(param)
+ @order << param
+ end
@config[param].value = value
end
@@ -30,8 +33,25 @@ class Config
}
end
+ def each
+ @order.each { |name|
+ if @config.include?(name)
+ yield name, @config[name]
+ else
+ raise Puppet::DevError, "%s is in the order but does not exist" % name
+ end
+ }
+ end
+
+ # Return an object by name.
+ def element(param)
+ param = param.intern unless param.is_a? Symbol
+ @config[param]
+ end
+
# Create a new config object
def initialize
+ @order = []
@config = {}
end
@@ -55,6 +75,8 @@ class Config
}
section = "puppet"
+ metas = %w{user group mode}
+ values = Hash.new { |hash, key| hash[key] = {} }
text.split(/\n/).each { |line|
case line
when /^\[(\w+)\]$/: section = $1 # Section names
@@ -63,10 +85,26 @@ class Config
when /^\s*(\w+)\s*=\s*(.+)$/: # settings
var = $1.intern
value = $2
- Puppet.info "%s: Setting %s to '%s'" % [section, var, value]
+ # Mmm, "special" attributes
+ if metas.include?(var.to_s)
+ unless values.include?(section)
+ values[section] = {}
+ end
+ values[section][var.to_s] = value
+ next
+ end
+ Puppet.info "%s: Setting %s to '%s'" % [section, var, value]
self[var] = value
@config[var].section = section
+
+ metas.each { |meta|
+ if values[section][meta]
+ if @config[var].respond_to?(meta + "=")
+ @config[var].send(meta + "=", values[section][meta])
+ end
+ end
+ }
else
raise Puppet::Error, "Could not match line %s" % line
end
@@ -112,6 +150,23 @@ class Config
}
end
+ def to_manifest
+ fest = ""
+ self.each { |name, obj|
+ [:user, :group].each { |type|
+ if obj.respond_to? type and val = obj.send(type)
+ fest += "#{type.to_s} { \"#{val}\": ensure => exists }\n\n"
+ end
+ }
+
+ if obj.respond_to? :to_manifest
+ fest += obj.to_manifest + "\n"
+ end
+ }
+
+ fest
+ end
+
# The base element type.
class CElement
attr_accessor :name, :section, :default, :parent
@@ -191,6 +246,18 @@ class Config
return value
end
+ def to_manifest
+ hash = {"ensure" => self.type}
+ %w{user group mode}.each { |var|
+ if value = self.send(var)
+ hash[var] = value
+ end
+ }
+ "file { \"#{self.value}\":\n %s\n}" % hash.collect { |p, v|
+ "#{p} => \"#{v}\""
+ }.join(",\n ")
+ end
+
# Make sure any provided variables look up to something.
def validate(value)
value.scan(/\$(\w+)/) { |name|
diff --git a/test/other/config.rb b/test/other/config.rb
index 639fb38f7..849fa1c97 100755
--- a/test/other/config.rb
+++ b/test/other/config.rb
@@ -111,11 +111,17 @@ class TestConfig < Test::Unit::TestCase
end
def test_parse
- text = %{one = this is a test
- two = another test
+ text = %{
+one = this is a test
+two = another test
+user = root
+group = root
+yay = /a/path
[section1]
attr = value
+ user = puppet
+ group = puppet
attr2 = /some/dir
attr3 = $attr2/other
}
@@ -132,6 +138,16 @@ class TestConfig < Test::Unit::TestCase
assert_equal("value", c[:attr])
assert_equal("/some/dir", c[:attr2])
assert_equal("/some/dir/other", c[:attr3])
+
+ elem = nil
+ assert_nothing_raised {
+ elem = c.element(:attr3)
+ }
+
+ assert(elem)
+ assert_equal("puppet", elem.user)
+
+ puts c.to_manifest
end
end