~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to lib/puppet/file_serving/mount/file.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/util/cacher'
 
2
 
 
3
require 'puppet/file_serving/mount'
 
4
 
 
5
class Puppet::FileServing::Mount::File < Puppet::FileServing::Mount
 
6
    class << self
 
7
        include Puppet::Util::Cacher
 
8
 
 
9
        cached_attr(:localmap) do
 
10
            {   "h" =>  Facter.value("hostname"),
 
11
                "H" => [Facter.value("hostname"),
 
12
                        Facter.value("domain")].join("."),
 
13
                "d" =>  Facter.value("domain")
 
14
            }
 
15
        end
 
16
    end
 
17
 
 
18
    def complete_path(relative_path, node)
 
19
        full_path = path(node)
 
20
 
 
21
        raise ArgumentError.new("Mounts without paths are not usable") unless full_path
 
22
 
 
23
        # If there's no relative path name, then we're serving the mount itself.
 
24
        return full_path unless relative_path
 
25
 
 
26
        file = ::File.join(full_path, relative_path)
 
27
 
 
28
        return nil unless FileTest.exist?(file)
 
29
 
 
30
        return file
 
31
    end
 
32
 
 
33
    # Return an instance of the appropriate class.
 
34
    def find(short_file, request)
 
35
        complete_path(short_file, request.node)
 
36
    end
 
37
 
 
38
    # Return the path as appropriate, expanding as necessary.
 
39
    def path(node = nil)
 
40
        if expandable?
 
41
            return expand(@path, node)
 
42
        else
 
43
            return @path
 
44
        end
 
45
    end
 
46
 
 
47
    # Set the path.
 
48
    def path=(path)
 
49
        # FIXME: For now, just don't validate paths with replacement
 
50
        # patterns in them.
 
51
        if path =~ /%./
 
52
            # Mark that we're expandable.
 
53
            @expandable = true
 
54
        else
 
55
            unless FileTest.directory?(path)
 
56
                raise ArgumentError, "%s does not exist or is not a directory" % path
 
57
            end
 
58
            unless FileTest.readable?(path)
 
59
                raise ArgumentError, "%s is not readable" % path
 
60
            end
 
61
            @expandable = false
 
62
        end
 
63
        @path = path
 
64
    end
 
65
 
 
66
    def search(path, request)
 
67
        return nil unless path = complete_path(path, request.node)
 
68
        return [path]
 
69
    end
 
70
 
 
71
    # Verify our configuration is valid.  This should really check to
 
72
    # make sure at least someone will be allowed, but, eh.
 
73
    def validate
 
74
        raise ArgumentError.new("Mounts without paths are not usable") if @path.nil?
 
75
    end
 
76
 
 
77
    private
 
78
 
 
79
    # Create a map for a specific node.
 
80
    def clientmap(node)
 
81
        {
 
82
            "h" => node.sub(/\..*$/, ""),
 
83
            "H" => node,
 
84
            "d" => node.sub(/[^.]+\./, "") # domain name
 
85
        }
 
86
    end
 
87
 
 
88
    # Replace % patterns as appropriate.
 
89
    def expand(path, node = nil)
 
90
        # This map should probably be moved into a method.
 
91
        map = nil
 
92
 
 
93
        if node
 
94
            map = clientmap(node)
 
95
        else
 
96
            Puppet.notice "No client; expanding '%s' with local host" %
 
97
                path
 
98
            # Else, use the local information
 
99
            map = localmap()
 
100
        end
 
101
 
 
102
        path.gsub(/%(.)/) do |v|
 
103
            key = $1
 
104
            if key == "%"
 
105
                "%"
 
106
            else
 
107
                map[key] || v
 
108
            end
 
109
        end
 
110
    end
 
111
 
 
112
    # Do we have any patterns in our path, yo?
 
113
    def expandable?
 
114
        if defined? @expandable
 
115
            @expandable
 
116
        else
 
117
            false
 
118
        end
 
119
    end
 
120
 
 
121
    # Cache this manufactured map, since if it's used it's likely
 
122
    # to get used a lot.
 
123
    def localmap
 
124
        self.class.localmap
 
125
    end
 
126
end