Sha256: ad7c38f8ddce919e3ace1d234a71f323d6348d1be577743c0b844277a08a0903

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

require 'puppet/indirector/terminus'
require 'puppet/util/yaml'

# The base class for YAML indirection termini.
class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus
  # Read a given name's file in and convert it from YAML.
  def find(request)
    file = path(request.key)
    return nil unless FileTest.exist?(file)

    begin
      return Puppet::Util::Yaml.load_file(file)
    rescue Puppet::Util::Yaml::YamlLoadError => detail
      raise Puppet::Error, "Could not parse YAML data for #{indirection.name} #{request.key}: #{detail}"
    end
  end

  # Convert our object to YAML and store it to the disk.
  def save(request)
    raise ArgumentError.new("You can only save objects that respond to :name") unless request.instance.respond_to?(:name)

    file = path(request.key)

    basedir = File.dirname(file)

    # This is quite likely a bad idea, since we're not managing ownership or modes.
    Dir.mkdir(basedir) unless FileTest.exist?(basedir)

    begin
      Puppet::Util::Yaml.dump(request.instance, file)
    rescue TypeError => detail
      Puppet.err "Could not save #{self.name} #{request.key}: #{detail}"
    end
  end

  # Return the path to a given node's file.
  def path(name,ext='.yaml')
    if name =~ Puppet::Indirector::BadNameRegexp then
      Puppet.crit("directory traversal detected in #{self.class}: #{name.inspect}")
      raise ArgumentError, "invalid key"
    end

    base = Puppet.run_mode.master? ? Puppet[:yamldir] : Puppet[:clientyamldir]
    File.join(base, self.class.indirection_name.to_s, name.to_s + ext)
  end

  def destroy(request)
    file_path = path(request.key)
    File.unlink(file_path) if File.exists?(file_path)
  end

  def search(request)
    Dir.glob(path(request.key,'')).collect do |file|
      YAML.load_file(file)
    end
  end

  private

  def from_yaml(text)
    YAML.load(text)
  end

  def to_yaml(object)
    YAML.dump(object)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
puppet-3.3.2 lib/puppet/indirector/yaml.rb
puppet-3.3.1 lib/puppet/indirector/yaml.rb
puppet-3.3.1.rc3 lib/puppet/indirector/yaml.rb
puppet-3.3.1.rc2 lib/puppet/indirector/yaml.rb
puppet-3.3.1.rc1 lib/puppet/indirector/yaml.rb