Skip to content

Instantly share code, notes, and snippets.

@dayglojesus
Created May 1, 2012 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayglojesus/2569267 to your computer and use it in GitHub Desktop.
Save dayglojesus/2569267 to your computer and use it in GitHub Desktop.
Puppet Type/Provider: Simple
# Simple Provider
require 'fileutils'
Puppet::Type.type(:simple).provide(:simple) do
desc "Provides simple useless file creation"
def create
notice("Creating: #{resource[:name]}")
system('/usr/bin/touch', "#{resource[:path]}/#{resource[:name]}")
end
def destroy
notice("Destroying: #{resource[:name]}")
system("/bin/rm", "#{resource[:path]}/#{resource[:name]}")
end
def exists?
notice("Checking for existence of: #{resource[:name]}")
File.exists?("#{resource[:path]}/#{resource[:name]}")
end
end
# simple.pp
simple { 'some_useless_file':
path => '/tmp',
ensure => 'present',
}
# Simple Type
Puppet::Type.newtype(:simple) do
@doc = "Create useless files for demonstration"
ensurable
newparam(:name) do
desc "Name of the file to create"
isnamevar
end
newparam(:path) do
desc "Path at which to create the file"
end
end
@dayglojesus
Copy link
Author

NOTE: The file names listed here have their respective directory appended to the original name as "directory_" to avoid Gist conflict.

Master: Puppet 2.7.11 on Ubuntu 11.04
Client: Puppet 2.7.11 on Mac OS X 10.7.2

@dayglojesus
Copy link
Author

Running a puppet agent -t the first time produces the expected result. However, subsequent execs of puppet agent -t produce...

info: Caching catalog for some.host.com info: Applying configuration version '1335888340' err: /Stage[main]//Node[van-daphne.ucs.sfu.ca]/Simple[a_useless_file]: Could not evaluate: No ability to determine if simple exists notice: Finished catalog run in 0.08 seconds
Everytime. No matter how times I reboot the server or client, I get the same result -- apply once, fail thereafter.

If I purge /var/lib/puppet, puppet agent will again run again without error, but (again) ONLY ONCE!

@kwilczynski
Copy link

My 2p: Try not to use system() from a provider unless you absolutely have to (each new fork() == a pain). In other words, provided that you have already required FileUtils, I do recommend you use FileUtils#touch and FileUtils#remove (or File#unlink) :)

@dayglojesus
Copy link
Author

Noted. Thanks.

@lak
Copy link

lak commented May 3, 2012

You can also use Puppet::Util::Execute rather than system directly and get exception handling and a lot more, although it does look correct to use touch/unlink in this case.

@dayglojesus
Copy link
Author

dayglojesus commented May 3, 2012 via email

@lak
Copy link

lak commented May 3, 2012 via email

@dayglojesus
Copy link
Author

Thanks for the tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment