summaryrefslogtreecommitdiffstats
path: root/nova/objectstore/s3server.py
blob: 1b0a7c794f558589e49d65aaead3376241862497 (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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2010 OpenStack Foundation
# Copyright 2009 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Implementation of an S3-like storage server based on local files.

Useful to test features that will eventually run on S3, or if you want to
run something locally that was once running on S3.

We don't support all the features of S3, but it does work with the
standard S3 client for the most basic semantics. To use the standard
S3 client with this module::

    c = S3.AWSAuthConnection("", "", server="localhost", port=8888,
                             is_secure=False)
    c.create_bucket("mybucket")
    c.put("mybucket", "mykey", "a value")
    print c.get("mybucket", "mykey").body

"""

import bisect
import datetime
import hashlib
import os
import os.path
import urllib

from oslo.config import cfg
import routes
import webob

from nova.openstack.common import fileutils
from nova import paths
from nova import utils
from nova import wsgi


s3_opts = [
    cfg.StrOpt('buckets_path',
               default=paths.state_path_def('buckets'),
               help='path to s3 buckets'),
    cfg.StrOpt('s3_listen',
               default="0.0.0.0",
               help='IP address for S3 API to listen'),
    cfg.IntOpt('s3_listen_port',
               default=3333,
               help='port for s3 api to listen'),
]

CONF = cfg.CONF
CONF.register_opts(s3_opts)


def get_wsgi_server():
    return wsgi.Server("S3 Objectstore",
                       S3Application(CONF.buckets_path),
                       port=CONF.s3_listen_port,
                       host=CONF.s3_listen)


class S3Application(wsgi.Router):
    """Implementation of an S3-like storage server based on local files.

    If bucket depth is given, we break files up into multiple directories
    to prevent hitting file system limits for number of files in each
    directories. 1 means one level of directories, 2 means 2, etc.

    """

    def __init__(self, root_directory, bucket_depth=0, mapper=None):
        if mapper is None:
            mapper = routes.Mapper()

        mapper.connect('/',
                controller=lambda *a, **kw: RootHandler(self)(*a, **kw))
        mapper.connect('/{bucket}/{object_name}',
                controller=lambda *a, **kw: ObjectHandler(self)(*a, **kw))
        mapper.connect('/{bucket_name}/',
                controller=lambda *a, **kw: BucketHandler(self)(*a, **kw))
        self.directory = os.path.abspath(root_directory)
        fileutils.ensure_tree(self.directory)
        self.bucket_depth = bucket_depth
        super(S3Application, self).__init__(mapper)


class BaseRequestHandler(object):
    """Base class emulating Tornado's web framework pattern in WSGI.

    This is a direct port of Tornado's implementation, so some key decisions
    about how the code interacts have already been chosen.

    The two most common ways of designing web frameworks can be
    classified as async object-oriented and sync functional.

    Tornado's is on the OO side because a response is built up in and using
    the shared state of an object and one of the object's methods will
    eventually trigger the "finishing" of the response asynchronously.

    Most WSGI stuff is in the functional side, we pass a request object to
    every call down a chain and the eventual return value will be a response.

    Part of the function of the routing code in S3Application as well as the
    code in BaseRequestHandler's __call__ method is to merge those two styles
    together enough that the Tornado code can work without extensive
    modifications.

    To do that it needs to give the Tornado-style code clean objects that it
    can modify the state of for each request that is processed, so we use a
    very simple factory lambda to create new state for each request, that's
    the stuff in the router, and when we let the Tornado code modify that
    object to handle the request, then we return the response it generated.
    This wouldn't work the same if Tornado was being more async'y and doing
    other callbacks throughout the process, but since Tornado is being
    relatively simple here we can be satisfied that the response will be
    complete by the end of the get/post method.

    """

    def __init__(self, application):
        self.application = application

    @webob.dec.wsgify
    def __call__(self, request):
        method = request.method.lower()
        f = getattr(self, method, self.invalid)
        self.request = request
        self.response = webob.Response()
        params = request.environ['wsgiorg.routing_args'][1]
        del params['controller']
        f(**params)
        return self.response

    def get_argument(self, arg, default):
        return self.request.params.get(arg, default)

    def set_header(self, header, value):
        self.response.headers[header] = value

    def set_status(self, status_code):
        self.response.status = status_code

    def set_404(self):
        self.render_xml({"Error": {
            "Code": "NoSuchKey",
            "Message": "The resource you requested does not exist"
            }})
        self.set_status(404)

    def finish(self, body=''):
        self.response.body = utils.utf8(body)

    def invalid(self, **kwargs):
        pass

    def render_xml(self, value):
        assert isinstance(value, dict) and len(value) == 1
        self.set_header("Content-Type", "application/xml; charset=UTF-8")
        name = value.keys()[0]
        parts = []
        parts.append('<' + utils.utf8(name) +
                     ' xmlns="http://doc.s3.amazonaws.com/2006-03-01">')
        self._render_parts(value.values()[0], parts)
        parts.append('</' + utils.utf8(name) + '>')
        self.finish('<?xml version="1.0" encoding="UTF-8"?>\n' +
                    ''.join(parts))

    def _render_parts(self, value, parts=None):
        if not parts:
            parts = []

        if isinstance(value, basestring):
            parts.append(utils.xhtml_escape(value))
        elif isinstance(value, int) or isinstance(value, long):
            parts.append(str(value))
        elif isinstance(value, datetime.datetime):
            parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
        elif isinstance(value, dict):
            for name, subvalue in value.iteritems():
                if not isinstance(subvalue, list):
                    subvalue = [subvalue]
                for subsubvalue in subvalue:
                    parts.append('<' + utils.utf8(name) + '>')
                    self._render_parts(subsubvalue, parts)
                    parts.append('</' + utils.utf8(name) + '>')
        else:
            raise Exception("Unknown S3 value type %r", value)

    def _object_path(self, bucket, object_name):
        if self.application.bucket_depth < 1:
            return os.path.abspath(os.path.join(
                self.application.directory, bucket, object_name))
        hash = hashlib.md5(object_name).hexdigest()
        path = os.path.abspath(os.path.join(
            self.application.directory, bucket))
        for i in range(self.application.bucket_depth):
            path = os.path.join(path, hash[:2 * (i + 1)])
        return os.path.join(path, object_name)


class RootHandler(BaseRequestHandler):
    def get(self):
        names = os.listdir(self.application.directory)
        buckets = []
        for name in names:
            path = os.path.join(self.application.directory, name)
            info = os.stat(path)
            buckets.append({
                "Name": name,
                "CreationDate": datetime.datetime.utcfromtimestamp(
                    info.st_ctime),
            })
        self.render_xml({"ListAllMyBucketsResult": {
            "Buckets": {"Bucket": buckets},
        }})


class BucketHandler(BaseRequestHandler):
    def get(self, bucket_name):
        prefix = self.get_argument("prefix", u"")
        marker = self.get_argument("marker", u"")
        max_keys = int(self.get_argument("max-keys", 50000))
        path = os.path.abspath(os.path.join(self.application.directory,
                                            bucket_name))
        terse = int(self.get_argument("terse", 0))
        if (not path.startswith(self.application.directory) or
            not os.path.isdir(path)):
            self.set_404()
            return
        object_names = []
        for root, dirs, files in os.walk(path):
            for file_name in files:
                object_names.append(os.path.join(root, file_name))
        skip = len(path) + 1
        for i in range(self.application.bucket_depth):
            skip += 2 * (i + 1) + 1
        object_names = [n[skip:] for n in object_names]
        object_names.sort()
        contents = []

        start_pos = 0
        if marker:
            start_pos = bisect.bisect_right(object_names, marker, start_pos)
        if prefix:
            start_pos = bisect.bisect_left(object_names, prefix, start_pos)

        truncated = False
        for object_name in object_names[start_pos:]:
            if not object_name.startswith(prefix):
                break
            if len(contents) >= max_keys:
                truncated = True
                break
            object_path = self._object_path(bucket_name, object_name)
            c = {"Key": object_name}
            if not terse:
                info = os.stat(object_path)
                c.update({
                    "LastModified": datetime.datetime.utcfromtimestamp(
                        info.st_mtime),
                    "Size": info.st_size,
                })
            contents.append(c)
            marker = object_name
        self.render_xml({"ListBucketResult": {
            "Name": bucket_name,
            "Prefix": prefix,
            "Marker": marker,
            "MaxKeys": max_keys,
            "IsTruncated": truncated,
            "Contents": contents,
        }})

    def put(self, bucket_name):
        path = os.path.abspath(os.path.join(
            self.application.directory, bucket_name))
        if (not path.startswith(self.application.directory) or
            os.path.exists(path)):
            self.set_status(403)
            return
        fileutils.ensure_tree(path)
        self.finish()

    def delete(self, bucket_name):
        path = os.path.abspath(os.path.join(
            self.application.directory, bucket_name))
        if (not path.startswith(self.application.directory) or
            not os.path.isdir(path)):
            self.set_404()
            return
        if len(os.listdir(path)) > 0:
            self.set_status(403)
            return
        os.rmdir(path)
        self.set_status(204)
        self.finish()


class ObjectHandler(BaseRequestHandler):
    def get(self, bucket, object_name):
        object_name = urllib.unquote(object_name)
        path = self._object_path(bucket, object_name)
        if (not path.startswith(self.application.directory) or
            not os.path.isfile(path)):
            self.set_404()
            return
        info = os.stat(path)
        self.set_header("Content-Type", "application/unknown")
        self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp(
            info.st_mtime))
        object_file = open(path, "r")
        try:
            self.finish(object_file.read())
        finally:
            object_file.close()

    def put(self, bucket, object_name):
        object_name = urllib.unquote(object_name)
        bucket_dir = os.path.abspath(os.path.join(
            self.application.directory, bucket))
        if (not bucket_dir.startswith(self.application.directory) or
            not os.path.isdir(bucket_dir)):
            self.set_404()
            return
        path = self._object_path(bucket, object_name)
        if not path.startswith(bucket_dir) or os.path.isdir(path):
            self.set_status(403)
            return
        directory = os.path.dirname(path)
        fileutils.ensure_tree(directory)
        object_file = open(path, "w")
        object_file.write(self.request.body)
        object_file.close()
        self.set_header('ETag',
                        '"%s"' % hashlib.md5(self.request.body).hexdigest())
        self.finish()

    def delete(self, bucket, object_name):
        object_name = urllib.unquote(object_name)
        path = self._object_path(bucket, object_name)
        if (not path.startswith(self.application.directory) or
            not os.path.isfile(path)):
            self.set_404()
            return
        os.unlink(path)
        self.set_status(204)
        self.finish()