~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/indirector/yaml.rb

  • Committer: Benjamin Kerensa
  • Date: 2012-11-21 23:50:52 UTC
  • mfrom: (1.1.30)
  • Revision ID: bkerensa@ubuntu.com-20121121235052-ah7nzabp77sh69gb
New Upstream Release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'puppet/indirector/terminus'
2
 
require 'puppet/util/file_locking'
3
2
 
4
3
# The base class for YAML indirection termini.
5
4
class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus
6
 
  include Puppet::Util::FileLocking
 
5
  if defined?(::Psych::SyntaxError)
 
6
    YamlLoadExceptions = [::StandardError, ::ArgumentError, ::Psych::SyntaxError]
 
7
  else
 
8
    YamlLoadExceptions = [::StandardError, ::ArgumentError]
 
9
  end
7
10
 
8
11
  # Read a given name's file in and convert it from YAML.
9
12
  def find(request)
12
15
 
13
16
    yaml = nil
14
17
    begin
15
 
      readlock(file) { |fh| yaml = fh.read }
 
18
      yaml = ::File.read(file)
16
19
    rescue => detail
17
20
      raise Puppet::Error, "Could not read YAML data for #{indirection.name} #{request.key}: #{detail}"
18
21
    end
 
22
 
19
23
    begin
20
24
      return from_yaml(yaml)
21
 
    rescue => detail
 
25
    rescue *YamlLoadExceptions => detail
22
26
      raise Puppet::Error, "Could not parse YAML data for #{indirection.name} #{request.key}: #{detail}"
23
27
    end
24
28
  end
35
39
    Dir.mkdir(basedir) unless FileTest.exist?(basedir)
36
40
 
37
41
    begin
38
 
      writelock(file, 0660) { |f| f.print to_yaml(request.instance) }
 
42
      Puppet::Util.replace_file(file, 0660) do |f|
 
43
        f.print to_yaml(request.instance)
 
44
      end
39
45
    rescue TypeError => detail
40
46
      Puppet.err "Could not save #{self.name} #{request.key}: #{detail}"
41
47
    end