Skip to content

Instantly share code, notes, and snippets.

@optiz0r
Last active October 13, 2016 14:37
Show Gist options
  • Save optiz0r/ce33557375b9bd15ccde6df8aed4ae02 to your computer and use it in GitHub Desktop.
Save optiz0r/ce33557375b9bd15ccde6df8aed4ae02 to your computer and use it in GitHub Desktop.
Puppet::Type.newtype(:entropy_mask) do
@desc = "Mask packages in Entropy"
ensurable
newparam(:name) do
desc "Unique name for this mask"
end
newproperty(:operator) do
desc "Operator that applies to the version. If not specified, defaults to '=' if a version is provided, not used if no version is provided"
end
newproperty(:package) do
desc "Name of the package being masked"
newvalues(%r{^(?:[A-Za-z0-9+_.-]+\/)?[a-zA-Z0-9+_-]+$})
end
newproperty(:version) do
desc "Version of the package"
newvalues(%r{^(\d*(?:\.\d+[a-zA-Z]*)*)(?:_((?:alpha|beta|pre|rc)\d*))?(-r\d+)?$})
end
newproperty(:slot) do
desc "Slot the package is in"
end
newproperty(:use) do
desc "Useflags for the package"
end
newproperty(:tag) do
desc "Tag for the package"
end
newproperty(:repo) do
desc "Repo for the package"
end
newproperty(:target) do
desc "Location of the package.mask file being managed"
defaultto {
if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile)
@resource.class.defaultprovider.default_target
else
nil
end
}
end
validate do
Puppet.warning("validate: name=#{self[:name]}, package=#{self[:package]}, tag=#{self[:tag]}, repo=#{self[:repo]}")
raise(ArgumentError, "At least one of package, tag or repo is required") if self[:package].nil? && self[:tag].nil? && self[:repo].nil?
raise(ArgumentError, "Package is required when a version is specified") if self[:package].nil? && !self[:version].nil?
raise(ArgumentError, "Version is required when an operator is specified") if self[:version].nil? && !self[:operator].nil?
end
autobefore(:package) do
[self[:package]]
end
end
require 'puppet/provider/parsedfile'
masks = "/etc/entropy/packages/package.mask"
Puppet::Type.type(:entropy_mask).provide(:parsed,
:parent => Puppet::Provider::ParsedFile,
:default_target => masks,
:filetype => :flat
) do
desc "File mask provider for entropy packages"
defaultfor :operatingsystem => :sabayon
text_line :blank,
:match => /^\s*$/
text_line :comment,
:match => /^\s*#/
text_line :unmanaged,
:match => %r{^([<>]?=)?((?:[A-Za-z0-9+_.-]+/)?[a-zA-Z0-9+_-]+)?(?:-(\d+(?:\.\d+)*[a-z]*(?:_(?:alpha|beta|pre|p|rc)\d*)?(?:-r\d+)?))?(?::([a-zA-Z0-9._-]+))?(?:\[([^\]]*)\])?(?:#([a-zA-Z0-9._-]+))?(?:::([a-zA-Z0-9\._-]+))?\s*$}
record_line :parsed,
:fields => %w{operator package version slot use tag repo name},
:match => %r{^([<>]?=)?((?:[A-Za-z0-9+_.-]+/)?[a-zA-Z0-9+_-]+)?(?:-(\d+(?:\.\d+)*[a-z]*(?:_(?:alpha|beta|pre|p|rc)\d*)?(?:-r\d+)?))?(?::([a-zA-Z0-9._-]+))?(?:\[([^\]]*)\])?(?:#([a-zA-Z0-9._-]+))?(?:::([a-zA-Z0-9\._-]+))?\s+#+ Puppet Name: (.*)\s*$},
:post_parse => proc { |record|
Puppet.warning("post-parse: package=#{record[:package]}, tag=#{record[:tag]}, repo=#{record[:repo]}")
},
:to_line => proc { |record|
line = ""
line += record[:operator] if record[:operator]
line += record[:package] if record[:package]
line += "-" + record[:version] if record[:version]
line += ":" + record[:slot] if record[:slot]
line += "[" + record[:use] + "]" if record[:use]
line += "#" + record[:tag] if record[:tag]
line += "::" + record[:repo] if record[:repo]
line += " ## Puppet Name: " + record[:name]
line
}
def self.instances
instances = super()
Puppet.warning("instances: name=#{instances[0].name}, package=#{instances[0].package}")
instances
end
end
# env RUBYLIB=/home/ben/git/puppet/modules/sabayon/lib /opt/puppetlabs/puppet/bin/ruby /opt/puppetlabs/puppet/bin/puppet resource entropy_mask
Warning: post-parse: name=openssh-without-ldap, package=openssh, tag=, repo
Warning: instances: name=openssh-without-ldap, package=openssh
Warning: validate: name=openssh-without-ldap, package=, tag=, repo=
Error: Could not run: Validation of Entropy_mask[openssh-without-ldap] failed: At least one of package, tag or repo is required
openssh[-ldap] # Puppet Name: openssh-without-ldap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment