Removing old, never-used DSL code
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
This commit is contained in:
Родитель
df2d39248d
Коммит
6bf1953271
|
@ -1,275 +0,0 @@
|
|||
# Just quick mess-around to see what a DSL would look like.
|
||||
#
|
||||
# This is what the executable could look like:
|
||||
##!/usr/bin/env ruby
|
||||
#
|
||||
#require 'puppet'
|
||||
#require 'puppet/dsl'
|
||||
#
|
||||
#Puppet::DSL.import(ARGV[0])
|
||||
#
|
||||
#bucket = Puppet::TransBucket.new
|
||||
#bucket.type = "top"
|
||||
#bucket.keyword = "class"
|
||||
#
|
||||
#Puppet::DSL.find_all do |name, sub|
|
||||
# sub.included
|
||||
#end.each do |name, sub|
|
||||
# bucket.push sub.export
|
||||
#end
|
||||
#
|
||||
#puts bucket.to_manifest
|
||||
#
|
||||
# And here's what an example config could look like:
|
||||
#
|
||||
|
||||
##!/usr/bin/env ruby
|
||||
#
|
||||
#
|
||||
# require 'puppet'
|
||||
# require 'puppet/dsl'
|
||||
#
|
||||
# include Puppet::DSL
|
||||
# init()
|
||||
#
|
||||
# aspect :webserver do
|
||||
# file "/tmp/testone", :content => "yaytest"
|
||||
#
|
||||
# exec "testing", :command => "/bin/echo this is a test"
|
||||
# end
|
||||
#
|
||||
# aspect :other, :inherits => :webserver do
|
||||
# file "/tmp/testone", :mode => "755"
|
||||
# end
|
||||
#
|
||||
# acquire :other
|
||||
#
|
||||
# apply
|
||||
|
||||
require 'puppet'
|
||||
|
||||
# Provide the actual commands for acting like a language.
|
||||
module Puppet::DSL
|
||||
def aspect(name, options = {}, &block)
|
||||
Puppet::DSL::Aspect.new(name, options, &block)
|
||||
end
|
||||
|
||||
def acquire(*names)
|
||||
names.each do |name|
|
||||
if aspect = Puppet::DSL::Aspect[name]
|
||||
unless aspect.evaluated?
|
||||
aspect.evaluate
|
||||
end
|
||||
else
|
||||
raise "Could not find aspect %s" % name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def apply
|
||||
bucket = export()
|
||||
catalog = bucket.to_catalog
|
||||
catalog.apply
|
||||
end
|
||||
|
||||
def export
|
||||
objects = Puppet::DSL::Aspect.collect do |name, aspect|
|
||||
if aspect.evaluated?
|
||||
aspect.export
|
||||
end
|
||||
end.reject { |a| a.nil? }.flatten.collect do |obj|
|
||||
obj.to_trans
|
||||
end
|
||||
bucket = Puppet::TransBucket.new(objects)
|
||||
bucket.name = "top"
|
||||
bucket.type = "class"
|
||||
|
||||
return bucket
|
||||
end
|
||||
|
||||
def init
|
||||
unless Process.uid == 0
|
||||
Puppet[:confdir] = File.expand_path("~/.puppet")
|
||||
Puppet[:vardir] = File.expand_path("~/.puppet/var")
|
||||
end
|
||||
Puppet[:user] = Process.uid
|
||||
Puppet[:group] = Process.gid
|
||||
Puppet::Util::Log.newdestination(:console)
|
||||
Puppet::Util::Log.level = :info
|
||||
end
|
||||
|
||||
class Aspect
|
||||
Resource = Puppet::Parser::Resource
|
||||
|
||||
include Puppet::Util
|
||||
include Puppet::DSL
|
||||
extend Puppet::Util
|
||||
extend Enumerable
|
||||
attr_accessor :parent, :name, :evaluated
|
||||
|
||||
@aspects = {}
|
||||
|
||||
@@objects = Hash.new do |hash, key|
|
||||
hash[key] = {}
|
||||
end
|
||||
|
||||
# Create an instance method for every type
|
||||
Puppet::Type.loadall
|
||||
Puppet::Type.eachtype do |type|
|
||||
define_method(type.name) do |*args|
|
||||
newresource(type, *args)
|
||||
end
|
||||
end
|
||||
|
||||
def self.[]=(name, aspect)
|
||||
name = symbolize(name)
|
||||
@aspects[name] = aspect
|
||||
end
|
||||
|
||||
def self.[](name)
|
||||
name = symbolize(name)
|
||||
|
||||
# Make sure there's always a main. This can get deleted in testing.
|
||||
if name == :main and ! @aspects[name]
|
||||
new(:main) {}
|
||||
end
|
||||
@aspects[name]
|
||||
end
|
||||
|
||||
def self.clear
|
||||
@aspects.clear
|
||||
@@objects.clear
|
||||
end
|
||||
|
||||
def self.delete(name)
|
||||
name = symbolize(name)
|
||||
if @aspects.has_key?(name)
|
||||
@aspects.delete(name)
|
||||
end
|
||||
end
|
||||
|
||||
def self.each
|
||||
@aspects.each do |name, a|
|
||||
yield name, a
|
||||
end
|
||||
end
|
||||
|
||||
def child_of?(aspect)
|
||||
unless aspect.is_a?(self.class)
|
||||
obj = self.class[aspect]
|
||||
unless obj
|
||||
raise "Could not find aspect %s" % aspect
|
||||
end
|
||||
aspect = obj
|
||||
end
|
||||
if self.parent
|
||||
if self.parent == aspect
|
||||
return true
|
||||
elsif self.parent.child_of?(aspect)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate
|
||||
if self.parent and ! self.parent.evaluated?
|
||||
self.parent.evaluate
|
||||
end
|
||||
|
||||
unless evaluated?
|
||||
if defined? @block
|
||||
instance_eval(&@block)
|
||||
end
|
||||
@evaluated = true
|
||||
end
|
||||
end
|
||||
|
||||
def evaluated?
|
||||
if self.evaluated
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def export
|
||||
@resources.dup
|
||||
end
|
||||
|
||||
def initialize(name, options = {}, &block)
|
||||
name = symbolize(name)
|
||||
@name = name
|
||||
if block
|
||||
@block = block
|
||||
end
|
||||
if pname = options[:inherits]
|
||||
if pname.is_a?(self.class)
|
||||
@parent = pname
|
||||
elsif parent = self.class[pname]
|
||||
@parent = parent
|
||||
else
|
||||
raise "Could not find parent aspect %s" % pname
|
||||
end
|
||||
end
|
||||
|
||||
@resources = []
|
||||
|
||||
self.class[name] = self
|
||||
end
|
||||
|
||||
def newresource(type, name, params = {})
|
||||
if self.is_a?(Puppet::DSL::Aspect)
|
||||
source = self
|
||||
else
|
||||
source = Puppet::DSL::Aspect[:main]
|
||||
end
|
||||
unless obj = @@objects[type][name]
|
||||
obj = Resource.new :title => name, :type => type.name,
|
||||
:source => source, :scope => scope
|
||||
@@objects[type][name] = obj
|
||||
|
||||
@resources << obj
|
||||
end
|
||||
|
||||
params.each do |name, value|
|
||||
param = Resource::Param.new(
|
||||
:name => name,
|
||||
:value => value,
|
||||
:source => source
|
||||
)
|
||||
|
||||
obj.send(:set_parameter, param)
|
||||
end
|
||||
|
||||
obj
|
||||
end
|
||||
|
||||
def scope
|
||||
unless defined?(@scope)
|
||||
# Set the code to something innocuous; we just need the
|
||||
# scopes, not the interpreter. Hackish, but true.
|
||||
Puppet[:code] = " "
|
||||
@interp = Puppet::Parser::Interpreter.new
|
||||
require 'puppet/node'
|
||||
@node = Puppet::Node.new(Facter.value(:hostname))
|
||||
if env = Puppet[:environment] and env == ""
|
||||
env = nil
|
||||
end
|
||||
@node.parameters = Facter.to_hash
|
||||
@compile = Puppet::Parser::Compiler.new(@node, @interp.send(:parser, env))
|
||||
@scope = @compile.topscope
|
||||
end
|
||||
@scope
|
||||
end
|
||||
|
||||
def type
|
||||
self.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@aspects = {}
|
|
@ -1,215 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require File.dirname(__FILE__) + '/../lib/puppettest'
|
||||
|
||||
require 'puppet'
|
||||
require 'puppet/dsl'
|
||||
require 'puppettest'
|
||||
|
||||
class TestDSL < Test::Unit::TestCase
|
||||
include PuppetTest
|
||||
include Puppet::DSL
|
||||
|
||||
def teardown
|
||||
super
|
||||
Puppet::DSL::Aspect.clear
|
||||
end
|
||||
|
||||
def test_aspect
|
||||
a = nil
|
||||
assert_nothing_raised do
|
||||
a = aspect :yaytest do
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal(a, Puppet::DSL::Aspect[:yaytest])
|
||||
|
||||
# Now make a child aspect
|
||||
b = nil
|
||||
assert_nothing_raised do
|
||||
b = aspect :child, :inherits => :yaytest do
|
||||
end
|
||||
end
|
||||
|
||||
assert(b.child_of?(a), "Parentage not set up correctly")
|
||||
assert(b.child_of?(:yaytest), "Parentage not set up for symbols")
|
||||
|
||||
# Now make another subclass
|
||||
c = nil
|
||||
assert_nothing_raised do
|
||||
c = aspect :kid, :inherits => :child do
|
||||
end
|
||||
end
|
||||
|
||||
assert(c.child_of?(b), "Parentage not set up correctly")
|
||||
assert(c.child_of?(a), "Parentage is not inherited")
|
||||
|
||||
# Lastly, make a separate aspect
|
||||
x = nil
|
||||
assert_nothing_raised do
|
||||
x = aspect :other do
|
||||
end
|
||||
end
|
||||
|
||||
assert(! x.child_of?(a), "Parentage came from nowhere")
|
||||
assert(! x.child_of?(b), "Parentage came from nowhere")
|
||||
assert(! x.child_of?(c), "Parentage came from nowhere")
|
||||
|
||||
# Make sure we can specify the name or the aspect
|
||||
y = nil
|
||||
assert_nothing_raised do
|
||||
x = aspect :naming, :inherits => a do
|
||||
end
|
||||
end
|
||||
assert(x.child_of?(a), "Parentage not set up correctly")
|
||||
|
||||
# And make sure the parent must exist
|
||||
z = nil
|
||||
assert_raise(RuntimeError) do
|
||||
z = aspect :noparent, :inherits => :nosuchaspect do
|
||||
end
|
||||
end
|
||||
assert(x.child_of?(a), "Parentage not set up correctly")
|
||||
end
|
||||
|
||||
def test_evaluate
|
||||
parent = child = nil
|
||||
parenteval = childeval = nil
|
||||
|
||||
assert_nothing_raised do
|
||||
parent = aspect :parent do
|
||||
if parenteval
|
||||
raise "parent already evaluated"
|
||||
end
|
||||
parenteval = true
|
||||
end
|
||||
|
||||
child = aspect :child, :inherits => parent do
|
||||
if childeval
|
||||
raise "child already evaluated"
|
||||
end
|
||||
childeval = true
|
||||
end
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
parent.evaluate()
|
||||
end
|
||||
|
||||
assert(parenteval, "Parent was not evaluated")
|
||||
assert(parent.evaluated?, "parent was not considered evaluated")
|
||||
|
||||
# Make sure evaluating twice silently does nothing
|
||||
assert_nothing_raised do
|
||||
parent.evaluate()
|
||||
end
|
||||
|
||||
# Now evaluate the child
|
||||
assert_nothing_raised do
|
||||
child.evaluate
|
||||
end
|
||||
|
||||
assert(childeval, "child was not evaluated")
|
||||
assert(child.evaluated?, "child was not considered evaluated")
|
||||
|
||||
# Now reset them both
|
||||
parenteval = childeval = nil
|
||||
parent.evaluated = false
|
||||
child.evaluated = false
|
||||
|
||||
# evaluate the child
|
||||
assert_nothing_raised do
|
||||
child.evaluate
|
||||
end
|
||||
|
||||
# and make sure both get evaluated
|
||||
assert(parenteval, "Parent was not evaluated")
|
||||
assert(parent.evaluated?, "parent was not considered evaluated")
|
||||
assert(childeval, "child was not evaluated")
|
||||
assert(child.evaluated?, "child was not considered evaluated")
|
||||
end
|
||||
|
||||
def test_acquire
|
||||
evalled = false
|
||||
a = aspect :test do
|
||||
evalled = true
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
acquire :test
|
||||
end
|
||||
|
||||
assert(evalled, "Did not evaluate aspect")
|
||||
|
||||
assert_nothing_raised do
|
||||
acquire :test
|
||||
end
|
||||
end
|
||||
|
||||
def test_newresource
|
||||
filetype = Puppet::Type.type(:file)
|
||||
path = tempfile()
|
||||
|
||||
a = aspect :testing
|
||||
|
||||
resource = nil
|
||||
assert_nothing_raised do
|
||||
resource = a.newresource filetype, path, :content => "yay", :mode => "640"
|
||||
end
|
||||
|
||||
assert_instance_of(Puppet::Parser::Resource, resource)
|
||||
|
||||
assert_equal("yay", resource[:content])
|
||||
assert_equal("640", resource[:mode])
|
||||
assert_equal(:testing, resource.source.name)
|
||||
|
||||
# Now try exporting our aspect
|
||||
assert_nothing_raised do
|
||||
a.evaluate
|
||||
end
|
||||
|
||||
result = nil
|
||||
assert_nothing_raised do
|
||||
result = a.export
|
||||
end
|
||||
|
||||
assert_equal([resource], result)
|
||||
|
||||
# Then try the DSL export
|
||||
assert_nothing_raised do
|
||||
result = export
|
||||
end
|
||||
|
||||
assert_instance_of(Puppet::TransBucket, result)
|
||||
|
||||
# And just for kicks, test applying everything
|
||||
assert_nothing_raised do
|
||||
apply()
|
||||
end
|
||||
|
||||
assert(FileTest.exists?(path), "File did not get created")
|
||||
assert_equal("yay", File.read(path))
|
||||
end
|
||||
|
||||
def test_typemethods
|
||||
Puppet::Type.loadall
|
||||
filetype = Puppet::Type.type(:file)
|
||||
path = tempfile()
|
||||
|
||||
a = aspect :testing
|
||||
|
||||
Puppet::Type.eachtype do |type|
|
||||
next if type.name.to_s =~ /test/
|
||||
assert(a.respond_to?(type.name),
|
||||
"Aspects do not have a %s method" % type.name)
|
||||
end
|
||||
|
||||
file = nil
|
||||
assert_nothing_raised do
|
||||
file = a.file path,
|
||||
:content => "yay", :mode => "640"
|
||||
end
|
||||
|
||||
assert_instance_of(Puppet::Parser::Resource, file)
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче