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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#!/usr/bin/ruby -w
#--------------------
# accept and serve files
#
# $Id$
require 'webrick'
#require 'webrick/https'
require 'xmlrpc/server'
require 'xmlrpc/client'
#require 'webrick/httpstatus'
require 'facter'
require 'digest/md5'
require 'base64'
module Puppet
class Server
class BucketError < RuntimeError; end
class FileBucket < Handler
DEFAULTPORT = 8139
@interface = XMLRPC::Service::Interface.new("puppetbucket") { |iface|
iface.add_method("string addfile(string, string)")
iface.add_method("string getfile(string)")
}
# this doesn't work for relative paths
def FileBucket.paths(base,md5)
return [
File.join(base, md5),
File.join(base, md5, "contents"),
File.join(base, md5, "paths")
]
end
def initialize(hash)
# build our AST
if hash.include?(:ConflictCheck)
@conflictchk = hash[:ConflictCheck]
hash.delete(:ConflictCheck)
else
@conflictchk = true
end
if hash.include?(:Path)
@bucket = hash[:Path]
hash.delete(:Path)
else
if defined? Puppet
@bucket = Puppet[:bucketdir]
else
@bucket = File.expand_path("~/.filebucket")
end
end
Puppet.recmkdir(@bucket)
end
# accept a file from a client
def addfile(string,path, client = nil, clientip = nil)
#puts "entering addfile"
contents = Base64.decode64(string)
#puts "string is decoded"
md5 = Digest::MD5.hexdigest(contents)
#puts "md5 is made"
bpath, bfile, pathpath = FileBucket.paths(@bucket,md5)
# if it's a new directory...
if Puppet.recmkdir(bpath)
# ...then just create the file
#puts "creating file"
File.open(bfile, File::WRONLY|File::CREAT) { |of|
of.print contents
}
#puts "File is created"
else # if the dir already existed...
# ...we need to verify that the contents match the existing file
if @conflictchk
unless FileTest.exists?(bfile)
raise(BucketError,
"No file at %s for sum %s" % [bfile,md5], caller)
end
curfile = ""
File.open(bfile) { |of|
curfile = of.read
}
# if the contents don't match, then we've found a conflict
# unlikely, but quite bad
if curfile != contents
raise(BucketError,
"Got passed new contents for sum %s" % md5, caller)
end
end
#puts "Conflict check is done"
end
# in either case, add the passed path to the list of paths
paths = nil
addpath = false
if FileTest.exists?(pathpath)
File.open(pathpath) { |of|
paths = of.readlines
}
# unless our path is already there...
unless paths.include?(path)
addpath = true
end
else
addpath = true
end
#puts "Path is checked"
# if it's a new file, or if our path isn't in the file yet, add it
if addpath
File.open(pathpath, File::WRONLY|File::CREAT|File::APPEND) { |of|
of.puts path
}
#puts "Path is added"
end
return md5
end
def getfile(md5, client = nil, clientip = nil)
bpath, bfile, bpaths = FileBucket.paths(@bucket,md5)
unless FileTest.exists?(bfile)
return false
end
contents = nil
File.open(bfile) { |of|
contents = of.read
}
return Base64.encode64(contents)
end
#---------------------------------------------------------------
end
end
end
|