Class: Puppet::Indirector::Yaml

Inherits:
Terminus show all
Includes:
Util::FileLocking
Defined in:
lib/vendor/puppet/indirector/yaml.rb

Overview

The base class for YAML indirection termini.

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods included from Util::FileLocking

readlock, writelock

Methods inherited from Terminus

abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type

Methods included from Util::InstanceLoader

#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Instance Method Details

#destroy(request) ⇒ Object



55
56
57
58
# File 'lib/vendor/puppet/indirector/yaml.rb', line 55

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

#find(request) ⇒ Object

Read a given name’s file in and convert it from YAML.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vendor/puppet/indirector/yaml.rb', line 9

def find(request)
  file = path(request.key)
  return nil unless FileTest.exist?(file)

  yaml = nil
  begin
    readlock(file) { |fh| yaml = fh.read }
  rescue => detail
    raise Puppet::Error, "Could not read YAML data for #{indirection.name} #{request.key}: #{detail}"
  end
  begin
    return from_yaml(yaml)
  rescue => detail
    raise Puppet::Error, "Could not parse YAML data for #{indirection.name} #{request.key}: #{detail}"
  end
end

#path(name, ext = '.yaml') ⇒ Object

Return the path to a given node’s file.



45
46
47
48
49
50
51
52
53
# File 'lib/vendor/puppet/indirector/yaml.rb', line 45

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

#save(request) ⇒ Object

Convert our object to YAML and store it to the disk.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendor/puppet/indirector/yaml.rb', line 27

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
    writelock(file, 0660) { |f| f.print to_yaml(request.instance) }
  rescue TypeError => detail
    Puppet.err "Could not save #{self.name} #{request.key}: #{detail}"
  end
end

#search(request) ⇒ Object



60
61
62
63
64
# File 'lib/vendor/puppet/indirector/yaml.rb', line 60

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