Example for type/provider with a program as backend?

31 views
Skip to first unread message

Tim Landscheidt

unread,
Aug 5, 2015, 10:20:17 AM8/5/15
to puppet...@googlegroups.com
Hi,

I want to write a type/provider for Sun Grid Engine/Open
Grid Scheduler/etc. configuration.

The configuration is (for the most part) managed by a pair
of commands, for example "qconf -sc" will show ("s") the
"complex" configuration, "qconf -mc" will invoke an editor
to modify ("m") it, and for some components "qconf -Mc
$file" can be called to replace the current configuration
with $file. The configuration looks somewhat like:

| #name shortcut type relop requestable consumable default urgency
| #----------------------------------------------------------------------------------------
| arch a RESTRING == YES NO NONE 0
| calendar c RESTRING == YES NO NONE 0
| cpu cpu DOUBLE >= YES NO 0 0
| [...]

This is strikingly similar to crontab where "crontab -l"
shows the configuration and "crontab" changes it, so I
looked at type/cron.rb and provider/cron/crontab.rb for in-
spiration, but the code is rather complex due to the subject
matter :-).

I searched the forge for Puppet::Provider::ParsedFile, but
the only match was
https://forge.puppetlabs.com/adrien/filemapper which explic-
itly recommends against using the former and advertises it-
self as a (better) alternative.

Is there a (simpler) example than crontab of how to use
Puppet::Provider::ParsedFile with a program as backend?

Tim

Raphaël Pinson

unread,
Aug 5, 2015, 11:29:18 AM8/5/15
to Puppet Users, t...@tim-landscheidt.de

Tim Landscheidt

unread,
Aug 9, 2015, 12:31:49 AM8/9/15
to puppet...@googlegroups.com
> [...]

Thanks for the example. Some of the other configurations I
need to manage in the future are more complicated and simi-
lar to LDIF, for example with continuation lines & Co., so I
probably take a closer look then.

For my current task Puppet::Provider::ParsedFile was suffi-
cient, though, and the resulting code is so simple that I'm
sure I'm missing something important:

| require 'puppet/provider/parsedfile'
| require 'puppet/util/filetype'

| Puppet::Type.type(:gridengine_resource).provide(:parsed, :parent => Puppet::Provider::ParsedFile, :default_target => 'default') do
| desc 'Provider for gridengine_resource.'

| commands :qconf => 'qconf'

| # Handle gridengine configuration for complex values.
| Puppet::Util::FileType.newfiletype(:gridengine_complex) do
| # TODO: target/default_target should be used to point to a
| # specific gridengine instance and default to, well, the
| # default.

| def read
| %x{qconf -sc}
| end

| def write(text)
| Tempfile.open('gridengine_resource') do |tmpfile|
| tmpfile.write(text)
| tmpfile.close
| Puppet::Util::Execution.execute(['qconf', '-Mc', tmpfile.path])
| end
| end
| end

| def self.filetype
| Puppet::Util::FileType.filetype(:gridengine_complex)
| end

| text_line :comment, :match => /^#/;
| text_line :blank, :match => /^\s*$/;

| record_line :parsed, :fields => %w{name shortcut type relop requestable consumable default urgency}
| end

That's all. Adding, removing and changing resources suc-
cessfully correspond to adding, removing and changing lines
in the configuration file that is fed from and to the pro-
gram backend.

Two issues I encountered worth mentioning:

1. Puppet is very opinionated where your source files should
be. Sources for providers need to be in
lib/puppet/provider/$type/$something.rb. If it doesn't
find this subdirectory or source files in it, it will
complain in obscure terms.

2. While the provider source is reread on every Puppet run
on the agent, the type source is read once and then
"cached" until puppetmaster is restarted. This can be
/very/ confusing if you change something in both the type
and the provider source. I ran into a similar issue some
months ago while writing a report handler, but I had for-
gotten the pain I went through then. Thankfully in Emacs
you can just add a (shell-command "service puppetmaster
restart") to after-save-hook for the affected files.

Tim

Reply all
Reply to author
Forward
0 new messages