summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application/filebucket.rb
blob: 0723054df2e5119acbe6c0dfb23fea5adccec21a (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
80
81
82
83
84
85
86
87
require 'puppet'
require 'puppet/application'
require 'puppet/network/client'

Puppet::Application.new(:filebucket) do

    should_not_parse_config

    option("--bucket BUCKET","-b")
    option("--debug","-d")
    option("--local","-l")
    option("--remote","-r")
    option("--verbose","-v")

    dispatch do
        ARGV.shift
    end

    command(:get) do
        md5 = ARGV.shift
        out = @client.getfile(md5)
        print out
    end

    command(:backup) do
        ARGV.each do |file|
            unless FileTest.exists?(file)
                $stderr.puts "%s: no such file" % file
                next
            end
            unless FileTest.readable?(file)
                $stderr.puts "%s: cannot read file" % file
                next
            end
            md5 = @client.backup(file)
            puts "%s: %s" % [file, md5]
        end
    end

    command(:restore) do
        file = ARGV.shift
        md5 = ARGV.shift
        @client.restore(file, md5)
    end

    setup do
        Puppet::Log.newdestination(:console)

        @client = nil
        @server = nil

        trap(:INT) do
            $stderr.puts "Cancelling"
            exit(1)
        end

        if options[:debug]
            Puppet::Log.level = :debug
        elsif options[:verbose]
            Puppet::Log.level = :info
        end

        # Now parse the config
        Puppet.parse_config

        if Puppet.settings.print_configs?
                exit(Puppet.settings.print_configs ? 0 : 1)
        end

        begin
            if options[:local] or options[:bucket]
                path = options[:bucket] || Puppet[:bucketdir]
                @client = Puppet::Network::Client.dipper.new(:Path => path)
            else
                require 'puppet/network/handler'
                @client = Puppet::Network::Client.dipper.new(:Server => Puppet[:server])
            end
        rescue => detail
            $stderr.puts detail
            if Puppet[:trace]
                puts detail.backtrace
            end
            exit(1)
        end
    end

end