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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
require 'puppet'
require 'cgi'
module Puppet
class Server
class FileServerError < Puppet::Error; end
class FileServer
attr_accessor :local
#CHECKPARAMS = %w{checksum type mode owner group}
CHECKPARAMS = [:mode, :type, :owner, :group, :checksum]
def self.interface
XMLRPC::Service::Interface.new("fileserver") { |iface|
iface.add_method("string describe(string)")
iface.add_method("string list(string, boolean)")
iface.add_method("string retrieve(string)")
}
end
Puppet::Server.addhandler(:FileServer, self)
def check(dir)
unless FileTest.exists?(dir)
Puppet.notice "File source %s does not exist" % dir
return nil
end
obj = nil
unless obj = Puppet::Type::PFile[dir]
obj = Puppet::Type::PFile.new(
:name => dir,
:check => CHECKPARAMS
)
end
# we should really have a timeout here -- we don't
# want to actually check on every connection, maybe no more
# than every 60 seconds or something
#@files[mount].evaluate
obj.evaluate
return obj
end
def describe(file)
mount, path = splitpath(file)
subdir = nil
unless subdir = subdir(mount, path)
Puppet.notice "Could not find subdirectory %s" %
"//%s/%s" % [mount, path]
return ""
end
obj = nil
unless obj = self.check(subdir)
return ""
end
desc = []
CHECKPARAMS.each { |check|
if state = obj.state(check)
unless state.is
Puppet.notice "Manually retrieving info for %s" % check
state.retrieve
end
desc << state.is
else
if check == "checksum" and obj.state(:type).is == "file"
Puppet.notice "File %s does not have data for %s" %
[obj.name, check]
end
desc << nil
end
}
return desc.join("\t")
end
def initialize(hash = {})
@mounts = {}
@files = {}
if hash[:Local]
@local = hash[:Local]
else
@local = false
end
end
def list(dir, recurse = false, sum = "md5")
mount, path = splitpath(dir)
subdir = nil
unless subdir = subdir(mount, path)
Puppet.notice "Could not find subdirectory %s" %
"//%s/%s" % [mount, path]
return ""
end
obj = nil
unless FileTest.exists?(subdir)
return ""
end
#rmdir = File.dirname(File.join(@mounts[mount], path))
rmdir = nameswap(dir, mount)
desc = self.reclist(rmdir, subdir, recurse)
if desc.length == 0
Puppet.notice "Got no information on //%s/%s" %
[mount, path]
return ""
end
desc.collect { |sub|
sub.join("\t")
}.join("\n")
end
def mount(dir, name)
if @mounts.include?(name)
if @mounts[name] != dir
raise FileServerError, "%s is already mounted at %s" %
[@mounts[name], name]
else
# it's already mounted; no problem
return
end
end
unless name =~ %r{^\w+$}
raise FileServerError, "Invalid name format '%s'" % name
end
unless FileTest.exists?(dir)
raise FileServerError, "%s does not exist" % dir
end
if FileTest.directory?(dir)
if FileTest.readable?(dir)
Puppet.info "Mounting %s at %s" % [dir, name]
@mounts[name] = dir
else
raise FileServerError, "%s is not readable" % dir
end
else
raise FileServerError, "%s is not a directory" % dir
end
end
# recursive listing function
def reclist(root, path, recurse)
#desc = [obj.name.sub(%r{#{root}/?}, '')]
name = path.sub(root, '')
if name == ""
name = "/"
end
if name == path
raise Puppet::FileServerError, "Could not match %s in %s" %
[root, path]
end
desc = [name]
ftype = File.stat(path).ftype
desc << ftype
if recurse.is_a?(Integer)
recurse -= 1
end
ary = [desc]
if recurse == true or (recurse.is_a?(Integer) and recurse > -1)
if ftype == "directory"
Dir.entries(path).each { |child|
next if child =~ /^\.\.?$/
self.reclist(root, File.join(path, child), recurse).each { |cobj|
ary << cobj
}
}
end
end
return ary.reject { |c| c.nil? }
end
def retrieve(file)
mount, path = splitpath(file)
unless (@mounts.include?(mount))
# FIXME I really need some better way to pass and handle xmlrpc errors
raise FileServerError, "%s not mounted" % mount
end
fpath = nil
if path
fpath = File.join(@mounts[mount], path)
else
fpath = @mounts[mount]
end
unless FileTest.exists?(fpath)
return ""
end
str = File.read(fpath)
if @local
return str
else
return CGI.escape(str)
end
end
private
def nameswap(name, mount)
name.sub(/\/#{mount}/, @mounts[mount]).gsub(%r{//}, '/').sub(
%r{/$}, ''
)
#Puppet.info "Swapped %s to %s" % [name, newname]
#newname
end
def splitpath(dir)
# the dir is based on one of the mounts
# so first retrieve the mount path
mount = nil
path = nil
if dir =~ %r{/(\w+)/?}
mount = $1
path = dir.sub(%r{/#{mount}/?}, '')
unless @mounts.include?(mount)
raise FileServerError, "%s not mounted" % mount
end
else
raise FileServerError, "Invalid path '%s'" % dir
end
if path == ""
path = nil
end
return mount, path
end
def subdir(mount, dir)
basedir = @mounts[mount]
dirname = nil
if dir
dirname = File.join(basedir, dir.split("/").join(File::SEPARATOR))
else
dirname = basedir
end
return dirname
end
end
end
end
# $Id$
|