Skip to content

Instantly share code, notes, and snippets.

@raphink
Created July 9, 2012 18:13
Show Gist options
  • Save raphink/3077988 to your computer and use it in GitHub Desktop.
Save raphink/3077988 to your computer and use it in GitHub Desktop.
# Alternative Augeas-based providers for Puppet
#
# Copyright (c) 2012 Raphaël Pinson
# Licensed under the Apache License, Version 2.0
require 'augeas' if Puppet.features.augeas?
Puppet::Type.type(:sshd_config).provide(:augeas) do
desc "Uses Augeas API to update an sshd_config parameter"
def self.file(resource = nil)
file = "/etc/ssh/sshd_config"
file = resource[:target] if resource and resource[:target]
file.chomp("/")
end
confine :true => Puppet.features.augeas?
confine :exists => file
def self.augopen(resource = nil)
aug = nil
file = file(resource)
begin
aug = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
aug.transform(
:lens => "Sshd.lns",
:name => "Sshd",
:incl => file
)
aug.load!
if aug.match("/files#{file}").empty?
message = aug.get("/augeas/files#{file}/error/message")
fail("Augeas didn't load #{file}: #{message}")
end
rescue
aug.close if aug
raise
end
aug
end
def self.instances
aug = nil
path = "/files#{file}"
begin
resources = []
aug = augopen
aug.match("#{path}/#{name}").each do |hpath|
sshd_config = {}
sshd_config[:name] = name
sshd_config[:value] = aug.get(hpath)
resources << new(sshd_config)
end
resources
ensure
aug.close if aug
end
end
def exists?
aug = nil
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
not aug.match("#{path}/#{name}").empty?
ensure
aug.close if aug
end
end
def create
aug = nil
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
aug.set("#{path}/#{name}", resource[:value])
aug.save!
ensure
aug.close if aug
end
end
def destroy
aug = nil
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
aug.rm("#{path}/#{name}")
aug.save!
ensure
aug.close if aug
end
end
def target
self.class.file(resource)
end
def value
aug = nil
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
aug.get("#{path}/#{name}")
ensure
aug.close if aug
end
end
def value=(value)
aug = nil
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
aug.set("#{path}/#{name}", value)
aug.save!
ensure
aug.close if aug
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment