Cross Reference: /pkg/src/depot.py
depot.py revision 119
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
0N/A#!/usr/bin/python
20N/A#
20N/A# CDDL HEADER START
20N/A#
20N/A# The contents of this file are subject to the terms of the
20N/A# Common Development and Distribution License (the "License").
20N/A# You may not use this file except in compliance with the License.
20N/A#
20N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
20N/A# or http://www.opensolaris.org/os/licensing.
20N/A# See the License for the specific language governing permissions
20N/A# and limitations under the License.
20N/A#
20N/A# When distributing Covered Code, include this CDDL HEADER in each
20N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
20N/A# If applicable, add the following below this CDDL HEADER, with the
20N/A# fields enclosed by brackets "[]" replaced with your own identifying
20N/A# information: Portions Copyright [yyyy] [name of copyright owner]
20N/A#
20N/A# CDDL HEADER END
20N/A#
20N/A# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
20N/A# Use is subject to license terms.
20N/A#
20N/A
22N/A# pkg.depotd - package repository daemon
0N/A
50N/A# XXX The prototype pkg.depotd combines both the version management server that
50N/A# answers to pkgsend(1) sessions and the HTTP file server that answers to the
50N/A# various GET operations that a pkg(1) client makes. This split is expected to
50N/A# be made more explicit, by constraining the pkg(1) operations such that they
50N/A# can be served as a typical HTTP/HTTPS session. Thus, pkg.depotd will reduce
50N/A# to a special purpose HTTP/HTTPS server explicitly for the version management
50N/A# operations, and must manipulate the various state files--catalogs, in
50N/A# particular--such that the pkg(1) pull client can operately accurately with
50N/A# only a basic HTTP/HTTPS server in place.
50N/A
50N/A# XXX We should support simple "last-modified" operations via HEAD queries.
50N/A
50N/A# XXX Although we pushed the evaluation of next-version, etc. to the pull
50N/A# client, we should probably provide a query API to do same on the server, for
50N/A# dumb clients (like a notification service).
50N/A
0N/Aimport BaseHTTPServer
55N/Aimport SocketServer
54N/Aimport errno
26N/Aimport getopt
0N/Aimport os
0N/Aimport re
0N/Aimport sha
0N/Aimport shutil
52N/Aimport sys
0N/Aimport time
22N/Aimport urllib
119N/Aimport tarfile
119N/Aimport cgi
0N/A
34N/Aimport pkg.catalog as catalog
22N/Aimport pkg.dependency as dependency
22N/Aimport pkg.fmri as fmri
46N/Aimport pkg.misc as misc
34N/Aimport pkg.package as package
22N/Aimport pkg.version as version
22N/A
114N/Aimport pkg.server.face as face
26N/Aimport pkg.server.config as config
23N/Aimport pkg.server.transaction as trans
23N/A
26N/Adef usage():
26N/A print """\
26N/AUsage: /usr/lib/pkg.depotd [-n]
26N/A"""
14N/A
50N/Adef catalog_get(scfg, request):
30N/A scfg.inc_catalog()
14N/A
0N/A request.send_response(200)
25N/A request.send_header('Content-type', 'text/plain')
0N/A request.end_headers()
26N/A request.wfile.write("%s" % scfg.catalog)
26N/A
50N/Adef manifest_get(scfg, request):
30N/A """The request is an encoded pkg FMRI. If the version is specified
50N/A incompletely, we return an error, as the client is expected to form
50N/A correct requests, based on its interpretation of the catalog and its
50N/A image policies."""
30N/A
30N/A scfg.inc_manifest()
30N/A
30N/A # Parse request into FMRI component and decode.
30N/A m = re.match("^/manifest/(.*)", request.path)
30N/A pfmri = urllib.unquote(m.group(1))
30N/A
50N/A f = fmri.PkgFmri(pfmri, None)
45N/A
30N/A # Open manifest and send.
50N/A file = open("%s/%s" % (scfg.pkg_root, f.get_dir_path()), "r")
45N/A data = file.read()
30N/A
30N/A request.send_response(200)
30N/A request.send_header('Content-type', 'text/plain')
30N/A request.end_headers()
45N/A request.wfile.write(data)
30N/A
119N/Adef file_get_multiple(scfg, request):
119N/A """Request data contains application/x-www-form-urlencoded entries
119N/A with the requested filenames."""
119N/A hdrs = request.headers
119N/A # If the sender doesn't specify the content length, reject this request.
119N/A # Calling read() with no size specified will force the server to block
119N/A # until the client sends EOF, an undesireable situation
119N/A size = int(hdrs.getheader("Content-Length"))
119N/A if size == 0:
119N/A request.send_response(411)
119N/A return
119N/A
119N/A rfile = request.rfile
119N/A data_dict = cgi.parse_qs(rfile.read(size))
119N/A
119N/A scfg.inc_flist()
119N/A
119N/A request.send_response(200)
119N/A request.send_header("Content-type", "application/data")
119N/A request.end_headers()
119N/A
119N/A tar_stream = tarfile.open(mode = "w|", fileobj = request.wfile)
119N/A
119N/A for v in data_dict.values():
119N/A filepath = os.path.normpath(os.path.join(
119N/A scfg.file_root, misc.hash_file_name(v[0])))
119N/A
119N/A tar_stream.add(filepath, v[0], False)
119N/A scfg.inc_flist_files()
119N/A
119N/A tar_stream.close()
119N/A
50N/Adef file_get_single(scfg, request):
30N/A """The request is the SHA-1 hash name for the file."""
30N/A scfg.inc_file()
30N/A
30N/A m = re.match("^/file/(.*)", request.path)
30N/A fhash = m.group(1)
30N/A
54N/A try:
119N/A file = open(os.path.normpath(os.path.join(
119N/A scfg.file_root, misc.hash_file_name(fhash))))
54N/A except IOError, e:
54N/A if e.errno == errno.ENOENT:
54N/A request.send_response(404)
54N/A else:
54N/A request.send_response(500)
54N/A return
54N/A
30N/A data = file.read()
30N/A
30N/A request.send_response(200)
30N/A request.send_header("Content-type", "application/data")
30N/A request.end_headers()
30N/A request.wfile.write(data)
0N/A
0N/Adef trans_open(scfg, request):
22N/A # XXX Authentication will be handled by virtue of possessing a signed
22N/A # certificate (or a more elaborate system).
22N/A t = trans.Transaction()
22N/A
22N/A ret = t.open(scfg, request)
22N/A if ret == 200:
26N/A scfg.in_flight_trans[t.get_basename()] = t
0N/A
22N/A request.send_response(200)
22N/A request.send_header('Content-type', 'text/plain')
22N/A request.send_header('Transaction-ID', t.get_basename())
22N/A request.end_headers()
22N/A elif ret == 400:
22N/A request.send_response(400)
22N/A else:
22N/A request.send_response(500)
0N/A
0N/A
0N/Adef trans_close(scfg, request):
0N/A # Pull transaction ID from headers.
0N/A m = re.match("^/close/(.*)", request.path)
0N/A trans_id = m.group(1)
0N/A
22N/A # XXX KeyError?
26N/A t = scfg.in_flight_trans[trans_id]
22N/A t.close(request)
26N/A del scfg.in_flight_trans[trans_id]
22N/A
22N/Adef trans_abandon(scfg, request):
22N/A # Pull transaction ID from headers.
22N/A m = re.match("^/abandon/(.*)", request.path)
22N/A trans_id = m.group(1)
22N/A
26N/A t = scfg.in_flight_trans[trans_id]
22N/A t.abandon(request)
26N/A del scfg.in_flight_trans[trans_id]
0N/A
0N/Adef trans_add(scfg, request):
0N/A m = re.match("^/add/([^/]*)/(.*)", request.path)
0N/A trans_id = m.group(1)
0N/A type = m.group(2)
0N/A
26N/A t = scfg.in_flight_trans[trans_id]
22N/A t.add_content(request, type)
0N/A
20N/Aif "PKG_REPO" in os.environ:
34N/A scfg = config.SvrConfig(os.environ["PKG_REPO"], "pkg.sun.com")
21N/Aelse:
34N/A scfg = config.SvrConfig("/var/pkg/repo", "pkg.sun.com")
0N/A
0N/Aclass pkgHandler(BaseHTTPServer.BaseHTTPRequestHandler):
0N/A
0N/A def do_GET(self):
26N/A # Client APIs
0N/A if re.match("^/catalog$", self.path):
50N/A catalog_get(scfg, self)
26N/A elif re.match("^/manifest/.*$", self.path):
50N/A manifest_get(scfg, self)
30N/A elif re.match("^/file/.*$", self.path):
50N/A file_get_single(scfg, self)
26N/A
26N/A # Publisher APIs
0N/A elif re.match("^/open/(.*)$", self.path):
0N/A trans_open(scfg, self)
0N/A elif re.match("^/close/(.*)$", self.path):
0N/A trans_close(scfg, self)
22N/A elif re.match("^/abandon/(.*)$", self.path):
22N/A trans_abandon(scfg, self)
0N/A elif re.match("^/add/(.*)$", self.path):
0N/A trans_add(scfg, self)
26N/A
26N/A # Informational APIs
114N/A elif face.match(self):
114N/A face.respond(scfg, self)
0N/A else:
114N/A face.unknown(scfg, self)
0N/A
0N/A def do_PUT(self):
0N/A self.send_response(200)
25N/A self.send_header('Content-type', 'text/plain')
0N/A self.end_headers()
30N/A self.wfile.write('''PUT URI %s ; headers %s''' %
30N/A (self.path, self.headers))
0N/A
0N/A def do_POST(self):
119N/A if re.match("^/filelist/.*$", self.path):
119N/A file_get_multiple(scfg, self)
119N/A elif re.match("^/add/(.*)$", self.path):
0N/A trans_add(scfg, self)
0N/A else:
0N/A self.send_response(404)
0N/A
0N/A def do_DELETE(self):
0N/A self.send_response(200)
25N/A self.send_header('Content-type', 'text/plain')
0N/A self.end_headers()
30N/A self.wfile.write('''URI %s ; headers %s''' %
30N/A (self.path, self.headers))
0N/A
55N/Aclass ThreadingHTTPServer(SocketServer.ThreadingMixIn,
55N/A BaseHTTPServer.HTTPServer):
55N/A pass
55N/A
22N/Aif __name__ == "__main__":
22N/A scfg.init_dirs()
26N/A scfg.acquire_in_flight()
26N/A scfg.acquire_catalog()
26N/A
82N/A port = 10000
82N/A
26N/A try:
82N/A opts, pargs = getopt.getopt(sys.argv[1:], "np:")
26N/A for opt, arg in opts:
26N/A if opt == "-n":
26N/A sys.exit(0)
82N/A elif opt == "-p":
82N/A port = int(arg)
52N/A except getopt.GetoptError, e:
52N/A print "pkg.depotd: unknown option '%s'" % e.opt
26N/A usage()
26N/A
82N/A server = ThreadingHTTPServer(('', port), pkgHandler)
22N/A server.serve_forever()