[Puppet-users] Pupppet Ruby DSL

Matthew Palmer
Wed, 26 Jul 2006 16:40:05 -0000

On Wed, Jul 26, 2006 at 02:13:52PM -0700, Luke Kanies
wrote:
> I constantly get told I should just create a DSL in Ruby, instead of
> having a separate, custom language.  This has, unsurprisingly, come
> up multiple times here at OSCON.

DSLs being the hot-topic of the day, it''s unsurprising that everyone
has an
opinion on the topic.  I remember when I first learnt lex/yacc -- suddenly
every problem could be solved with a grammar.  This isn''t much
different,
really.

> So, I decided I''d see if I could hack something up and see how it
> looked.  This is the first thing I could come up with in terms of the
> basic functionality:
>
> #!/usr/bin/ruby
>
> class Base
>      file "/etc/passwd",
>          :owner => "root",
>          :group => "root",
>          :mode => 0644,
>          :source => "puppet://puppet/..."

You might want to "class Base < Puppet::Class" for cleanliness, but
if we
consider these puppet scripts as totally separate, then adding the type
creators under Object might not be considered quite so naughty.  It just
seems... icky.

> class BSD < Base
>      file "/etc/passwd",
>          :group => "wheel"
> end
>
> include :bsd

We''ll need to change the keyword for loading another class --
''include'' is
already taken by Ruby, and overloading that is going to be... Entertaining.

> The implementation I have (which is about 105 lines of code) supports

<goldmember>Toight loik a toiger!</goldmember>

> I have been thinking that maybe Puppet could support loading
''.rb''
> files written in this kind of code, or ''.pp'' files
written in current
> Puppet code, and it should be possible to otherwise treat them the
> same -- puppetmasterd would parse everything and wait for
> connections, while puppet would immediately apply the configuration.
> I wouldn''t expect you to be able to mix files -- all of them would
> be .pp, or all of them would be .rb.

Awwww, that''s no fun!  It also kinda upsets the migration path -- I
know *I*
don''t want to rewrite all of my existing classes in .rb just to be able
to
put ''if'' statements in a couple of them.

> I haven''t thought much about how this would look in real life, but
I
> figured I''d at least look at what a DSL in Ruby would look like
and
> see if it was an incredibly bad idea.
>
> I still don''t know.  It feels like it''s basically as
restricted as
> the language, but certainly it''s a heckuva lot less code than
> Puppet''s parser is.

That''s the tricky problem -- I quite like the idea that the Puppet
manifest
description language is fairly tightly constrained.  It doesn''t give
the
not-quite-so-133t quite the same ability to make a complete mess of things
as what straight Ruby does.  It also feels easier to learn for someone who
is coming from a don''t-know-Puppet-or-Ruby background -- if
you''re going to
have to learn the syntax one of them, I think that Puppet might be a bit
easier.  Conversely, if you already know Ruby, a small Ruby DSL would no
doubt be easier to pick up.

Turning around and speaking from a pro-DSL standpoint, it seems like the
parser is a bit of a bottleneck in evolving the language -- not necessarily
so much from an absolute features point of view (after all, public objects
came on the scene fairly quickly) but having to think about designing the
fundamental details of the language (like boolean operators and if, to take
a recent example) distracts from other, more interesting features.  Also,
you''re the only one who really understands the deep magic in and around
the
parser, I think, so it''s a bit of a single-point-of-failure and general
blocker.  Not that having random dabblers making core language changes is
necessarily something you want to encourage...

I''m terribly conflicted.  I do like writing Rails code specifically
because
I know I''ve got the entire power of the Ruby language behind me, and
there
have been a few times where being able to drop a tiny fragment of Ruby into
a manifest would have been a godsend.  But keeping the language simple,
easily auditable, and above all constrained, is also important.

I think though, at the end of the day, my power-hungry side would win out on
this.  I console myself with the fact that Puppet users have many other ways
to hurt themselves -- locally-written types and shell scripts, so giving
them more power in the manifest description language (which is air-gapped
from the actual end-machine by the manifest transfer from server to client)
probably isn''t the worst thing people are going to be able to do, if
they
want to have an attack of the dumbs.

> Any comments?  Should I commit this to subversion, just to keep it
> around in case people want to mess with it?

I think it should definitely go into svn, it''s worth keeping around.

- Matt

--
I seem to have my life in reverse. When I was a wee''un, it seemed
perfectly
normal that one could pick up the phone and speak to anybody else in the
world who also has a phone. Now I''m older and more experienced,
I''m amazed
that this could possibly work. -- Peter Corlett, in the Monastery