lak / puppet
- Source
- Commits
- Network (26)
- Issues (0)
- Downloads (62)
- Wiki (1)
- Graphs
-
Tree:
79a4339
Thomas Bellman (author)
Tue Aug 04 05:47:12 -0700 2009
| 3ef46639 » | luke | 2006-07-27 | 1 | # Just quick mess-around to see what a DSL would look like. | |
| 4f2c066a » | ibtaylor | 2009-06-05 | 2 | # | |
| 3ef46639 » | luke | 2006-07-27 | 3 | # This is what the executable could look like: | |
| 9d30b260 » | plathrop | 2008-03-25 | 4 | ##!/usr/bin/env ruby | |
| 3ef46639 » | luke | 2006-07-27 | 5 | # | |
| 6 | #require 'puppet' | ||||
| 7 | #require 'puppet/dsl' | ||||
| 8 | # | ||||
| 9 | #Puppet::DSL.import(ARGV[0]) | ||||
| 10 | # | ||||
| 11 | #bucket = Puppet::TransBucket.new | ||||
| 12 | #bucket.type = "top" | ||||
| 13 | #bucket.keyword = "class" | ||||
| 14 | # | ||||
| 15 | #Puppet::DSL.find_all do |name, sub| | ||||
| 16 | # sub.included | ||||
| 17 | #end.each do |name, sub| | ||||
| 18 | # bucket.push sub.export | ||||
| 19 | #end | ||||
| 20 | # | ||||
| 21 | #puts bucket.to_manifest | ||||
| 22 | # | ||||
| 23 | # And here's what an example config could look like: | ||||
| 24 | # | ||||
| 25 | |||||
| 9d30b260 » | plathrop | 2008-03-25 | 26 | ##!/usr/bin/env ruby | |
| 3ef46639 » | luke | 2006-07-27 | 27 | # | |
| 28 | # | ||||
| ed38ba44 » | luke | 2006-10-18 | 29 | # require 'puppet' | |
| 30 | # require 'puppet/dsl' | ||||
| 4f2c066a » | ibtaylor | 2009-06-05 | 31 | # | |
| ed38ba44 » | luke | 2006-10-18 | 32 | # include Puppet::DSL | |
| 33 | # init() | ||||
| 4f2c066a » | ibtaylor | 2009-06-05 | 34 | # | |
| ed38ba44 » | luke | 2006-10-18 | 35 | # aspect :webserver do | |
| 36 | # file "/tmp/testone", :content => "yaytest" | ||||
| 4f2c066a » | ibtaylor | 2009-06-05 | 37 | # | |
| ed38ba44 » | luke | 2006-10-18 | 38 | # exec "testing", :command => "/bin/echo this is a test" | |
| 39 | # end | ||||
| 4f2c066a » | ibtaylor | 2009-06-05 | 40 | # | |
| ed38ba44 » | luke | 2006-10-18 | 41 | # aspect :other, :inherits => :webserver do | |
| 42 | # file "/tmp/testone", :mode => "755" | ||||
| 43 | # end | ||||
| 4f2c066a » | ibtaylor | 2009-06-05 | 44 | # | |
| ed38ba44 » | luke | 2006-10-18 | 45 | # acquire :other | |
| 4f2c066a » | ibtaylor | 2009-06-05 | 46 | # | |
| ed38ba44 » | luke | 2006-10-18 | 47 | # apply | |
| 48 | |||||
| ff970591 » | lak | 2008-02-23 | 49 | require 'puppet' | |
| ed38ba44 » | luke | 2006-10-18 | 50 | ||
| ff970591 » | lak | 2008-02-23 | 51 | # Provide the actual commands for acting like a language. | |
| 52 | module Puppet::DSL | ||||
| 53 | def aspect(name, options = {}, &block) | ||||
| 54 | Puppet::DSL::Aspect.new(name, options, &block) | ||||
| 55 | end | ||||
| 56 | |||||
| 57 | def acquire(*names) | ||||
| 58 | names.each do |name| | ||||
| 59 | if aspect = Puppet::DSL::Aspect[name] | ||||
| 60 | unless aspect.evaluated? | ||||
| 61 | aspect.evaluate | ||||
| ed38ba44 » | luke | 2006-10-18 | 62 | end | |
| ff970591 » | lak | 2008-02-23 | 63 | else | |
| 64 | raise "Could not find aspect %s" % name | ||||
| ed38ba44 » | luke | 2006-10-18 | 65 | end | |
| 66 | end | ||||
| ff970591 » | lak | 2008-02-23 | 67 | end | |
| ed38ba44 » | luke | 2006-10-18 | 68 | ||
| ff970591 » | lak | 2008-02-23 | 69 | def apply | |
| 70 | bucket = export() | ||||
| 71 | catalog = bucket.to_catalog | ||||
| 72 | catalog.apply | ||||
| 73 | end | ||||
| ed38ba44 » | luke | 2006-10-18 | 74 | ||
| ff970591 » | lak | 2008-02-23 | 75 | def export | |
| 76 | objects = Puppet::DSL::Aspect.collect do |name, aspect| | ||||
| 77 | if aspect.evaluated? | ||||
| 78 | aspect.export | ||||
| ed38ba44 » | luke | 2006-10-18 | 79 | end | |
| ff970591 » | lak | 2008-02-23 | 80 | end.reject { |a| a.nil? }.flatten.collect do |obj| | |
| 81 | obj.to_trans | ||||
| ed38ba44 » | luke | 2006-10-18 | 82 | end | |
| ff970591 » | lak | 2008-02-23 | 83 | bucket = Puppet::TransBucket.new(objects) | |
| 84 | bucket.name = "top" | ||||
| 85 | bucket.type = "class" | ||||
| ed38ba44 » | luke | 2006-10-18 | 86 | ||
| ff970591 » | lak | 2008-02-23 | 87 | return bucket | |
| 88 | end | ||||
| ed38ba44 » | luke | 2006-10-18 | 89 | ||
| ff970591 » | lak | 2008-02-23 | 90 | def init | |
| 91 | unless Process.uid == 0 | ||||
| 92 | Puppet[:confdir] = File.expand_path("~/.puppet") | ||||
| 93 | Puppet[:vardir] = File.expand_path("~/.puppet/var") | ||||
| 94 | end | ||||
| 95 | Puppet[:user] = Process.uid | ||||
| 96 | Puppet[:group] = Process.gid | ||||
| 97 | Puppet::Util::Log.newdestination(:console) | ||||
| 98 | Puppet::Util::Log.level = :info | ||||
| ed38ba44 » | luke | 2006-10-18 | 99 | end | |
| 100 | |||||
| 101 | class Aspect | ||||
| 102 | Resource = Puppet::Parser::Resource | ||||
| 103 | |||||
| 104 | include Puppet::Util | ||||
| 105 | include Puppet::DSL | ||||
| 106 | extend Puppet::Util | ||||
| 107 | extend Enumerable | ||||
| 108 | attr_accessor :parent, :name, :evaluated | ||||
| 109 | |||||
| 110 | @aspects = {} | ||||
| 111 | |||||
| 112 | @@objects = Hash.new do |hash, key| | ||||
| 113 | hash[key] = {} | ||||
| 114 | end | ||||
| 115 | |||||
| 116 | # Create an instance method for every type | ||||
| 117 | Puppet::Type.loadall | ||||
| 118 | Puppet::Type.eachtype do |type| | ||||
| 119 | define_method(type.name) do |*args| | ||||
| 120 | newresource(type, *args) | ||||
| 121 | end | ||||
| 122 | end | ||||
| 123 | |||||
| 124 | def self.[]=(name, aspect) | ||||
| 125 | name = symbolize(name) | ||||
| 126 | @aspects[name] = aspect | ||||
| 127 | end | ||||
| 128 | |||||
| 129 | def self.[](name) | ||||
| 130 | name = symbolize(name) | ||||
| 131 | |||||
| 132 | # Make sure there's always a main. This can get deleted in testing. | ||||
| 133 | if name == :main and ! @aspects[name] | ||||
| 134 | new(:main) {} | ||||
| 135 | end | ||||
| 136 | @aspects[name] | ||||
| 137 | end | ||||
| 138 | |||||
| 139 | def self.clear | ||||
| 140 | @aspects.clear | ||||
| 141 | @@objects.clear | ||||
| 142 | end | ||||
| 143 | |||||
| 144 | def self.delete(name) | ||||
| 145 | name = symbolize(name) | ||||
| 146 | if @aspects.has_key?(name) | ||||
| 147 | @aspects.delete(name) | ||||
| 148 | end | ||||
| 149 | end | ||||
| 150 | |||||
| 151 | def self.each | ||||
| 152 | @aspects.each do |name, a| | ||||
| 153 | yield name, a | ||||
| 154 | end | ||||
| 155 | end | ||||
| 156 | |||||
| 157 | def child_of?(aspect) | ||||
| 158 | unless aspect.is_a?(self.class) | ||||
| 159 | obj = self.class[aspect] | ||||
| 160 | unless obj | ||||
| 161 | raise "Could not find aspect %s" % aspect | ||||
| 162 | end | ||||
| 163 | aspect = obj | ||||
| 164 | end | ||||
| 165 | if self.parent | ||||
| 166 | if self.parent == aspect | ||||
| 167 | return true | ||||
| 168 | elsif self.parent.child_of?(aspect) | ||||
| 169 | return true | ||||
| 170 | else | ||||
| 171 | return false | ||||
| 172 | end | ||||
| 173 | else | ||||
| 174 | return false | ||||
| 175 | end | ||||
| 176 | end | ||||
| 177 | |||||
| 178 | def evaluate | ||||
| 179 | if self.parent and ! self.parent.evaluated? | ||||
| 180 | self.parent.evaluate | ||||
| 181 | end | ||||
| 182 | |||||
| 183 | unless evaluated? | ||||
| 184 | if defined? @block | ||||
| 185 | instance_eval(&@block) | ||||
| 186 | end | ||||
| 187 | @evaluated = true | ||||
| 188 | end | ||||
| 189 | end | ||||
| 190 | |||||
| 191 | def evaluated? | ||||
| 192 | if self.evaluated | ||||
| 193 | true | ||||
| 194 | else | ||||
| 195 | false | ||||
| 196 | end | ||||
| 197 | end | ||||
| 198 | |||||
| 199 | def export | ||||
| 200 | @resources.dup | ||||
| 201 | end | ||||
| 202 | |||||
| 203 | def initialize(name, options = {}, &block) | ||||
| 204 | name = symbolize(name) | ||||
| 205 | @name = name | ||||
| 206 | if block | ||||
| 207 | @block = block | ||||
| 208 | end | ||||
| 209 | if pname = options[:inherits] | ||||
| 210 | if pname.is_a?(self.class) | ||||
| 211 | @parent = pname | ||||
| 212 | elsif parent = self.class[pname] | ||||
| 213 | @parent = parent | ||||
| 214 | else | ||||
| 215 | raise "Could not find parent aspect %s" % pname | ||||
| 216 | end | ||||
| 217 | end | ||||
| 218 | |||||
| 219 | @resources = [] | ||||
| 220 | |||||
| 221 | self.class[name] = self | ||||
| 222 | end | ||||
| 223 | |||||
| 224 | def newresource(type, name, params = {}) | ||||
| ff970591 » | lak | 2008-02-23 | 225 | if self.is_a?(Puppet::DSL::Aspect) | |
| ed38ba44 » | luke | 2006-10-18 | 226 | source = self | |
| 227 | else | ||||
| ff970591 » | lak | 2008-02-23 | 228 | source = Puppet::DSL::Aspect[:main] | |
| ed38ba44 » | luke | 2006-10-18 | 229 | end | |
| 230 | unless obj = @@objects[type][name] | ||||
| 231 | obj = Resource.new :title => name, :type => type.name, | ||||
| 4eb87ed7 » | lak | 2007-08-20 | 232 | :source => source, :scope => scope | |
| ed38ba44 » | luke | 2006-10-18 | 233 | @@objects[type][name] = obj | |
| 234 | |||||
| 235 | @resources << obj | ||||
| 236 | end | ||||
| 237 | |||||
| 238 | params.each do |name, value| | ||||
| 239 | param = Resource::Param.new( | ||||
| 240 | :name => name, | ||||
| 241 | :value => value, | ||||
| 242 | :source => source | ||||
| 243 | ) | ||||
| 244 | |||||
| 4eb87ed7 » | lak | 2007-08-20 | 245 | obj.send(:set_parameter, param) | |
| ed38ba44 » | luke | 2006-10-18 | 246 | end | |
| 247 | |||||
| 248 | obj | ||||
| 249 | end | ||||
| 4eb87ed7 » | lak | 2007-08-20 | 250 | ||
| 251 | def scope | ||||
| 252 | unless defined?(@scope) | ||||
| f084d83d » | lak | 2007-10-05 | 253 | # Set the code to something innocuous; we just need the | |
| 254 | # scopes, not the interpreter. Hackish, but true. | ||||
| 255 | Puppet[:code] = " " | ||||
| 256 | @interp = Puppet::Parser::Interpreter.new | ||||
| 43f22a24 » | lak | 2007-09-12 | 257 | require 'puppet/node' | |
| 8b3361af » | lak | 2007-08-22 | 258 | @node = Puppet::Node.new(Facter.value(:hostname)) | |
| f084d83d » | lak | 2007-10-05 | 259 | if env = Puppet[:environment] and env == "" | |
| 260 | env = nil | ||||
| 261 | end | ||||
| 4eb87ed7 » | lak | 2007-08-20 | 262 | @node.parameters = Facter.to_hash | |
| ff970591 » | lak | 2008-02-23 | 263 | @compile = Puppet::Parser::Compiler.new(@node, @interp.send(:parser, env)) | |
| 2625eb1d » | lak | 2007-08-27 | 264 | @scope = @compile.topscope | |
| 4eb87ed7 » | lak | 2007-08-20 | 265 | end | |
| 266 | @scope | ||||
| 267 | end | ||||
| ed38ba44 » | luke | 2006-10-18 | 268 | ||
| 269 | def type | ||||
| 270 | self.name | ||||
| 271 | end | ||||
| 272 | end | ||||
| 273 | end | ||||
| 274 | |||||
| 275 | @aspects = {} | ||||

