summaryrefslogtreecommitdiffstats
path: root/lib/puppet/configurer/downloader.rb
blob: f1c4c03b1f4b67fdb4703d0a75e5b494f0de30d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'puppet/agent'
require 'puppet/resource/catalog'

class Puppet::Configurer::Downloader
    attr_reader :name, :path, :source, :ignore

    # Determine the timeout value to use.
    def self.timeout
        timeout = Puppet[:configtimeout]
        case timeout
        when String:
            if timeout =~ /^\d+$/
                timeout = Integer(timeout)
            else
                raise ArgumentError, "Configuration timeout must be an integer"
            end
        when Integer: # nothing
        else
            raise ArgumentError, "Configuration timeout must be an integer"
        end

        return timeout
    end

    # Evaluate our download, returning the list of changed values.
    def evaluate
        Puppet.info "Retrieving #{name}"

        files = []
        begin
            Timeout.timeout(self.class.timeout) do
                catalog.apply do |trans|
                    trans.changed?.find_all do |resource|
                        yield resource if block_given?
                        files << resource[:path]
                    end
                end
            end
        rescue Puppet::Error, Timeout::Error => detail
            puts detail.backtrace if Puppet[:debug]
            Puppet.err "Could not retrieve #{name}: %s" % detail
        end

        return files
    end

    def initialize(name, path, source, ignore = nil)
        @name, @path, @source, @ignore = name, path, source, ignore
    end

    def catalog
        catalog = Puppet::Resource::Catalog.new
        catalog.add_resource(file)
        catalog
    end

    def file
        args = default_arguments.merge(:path => path, :source => source)
        args[:ignore] = ignore if ignore
        Puppet::Type.type(:file).create(args)
    end

    private

    def default_arguments
        {
            :path => path,
            :recurse => true,
            :source => source,
            :tag => name,
            :owner => Process.uid,
            :group => Process.gid,
            :purge => true,
            :force => true,
            :backup => false,
            :noop => false
        }
    end
end