~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to nova/objectstore/bucket.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright [2010] [Anso Labs, LLC]
 
3
 
4
#    Licensed under the Apache License, Version 2.0 (the "License");
 
5
#    you may not use this file except in compliance with the License.
 
6
#    You may obtain a copy of the License at
 
7
 
8
#        http://www.apache.org/licenses/LICENSE-2.0
 
9
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS,
 
12
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#    See the License for the specific language governing permissions and
 
14
#    limitations under the License.
 
15
 
 
16
"""
 
17
Simple object store using Blobs and JSON files on disk.
 
18
"""
 
19
 
 
20
import datetime
 
21
import glob
 
22
import json
 
23
import os
 
24
import bisect
 
25
 
 
26
from nova import exception
 
27
from nova import flags
 
28
from nova import utils
 
29
from nova.objectstore import stored
 
30
 
 
31
 
 
32
FLAGS = flags.FLAGS
 
33
flags.DEFINE_string('buckets_path', utils.abspath('../buckets'),
 
34
                    'path to s3 buckets')
 
35
 
 
36
 
 
37
class Bucket(object):
 
38
    def __init__(self, name):
 
39
        self.name = name
 
40
        self.path = os.path.abspath(os.path.join(FLAGS.buckets_path, name))
 
41
        if not self.path.startswith(os.path.abspath(FLAGS.buckets_path)) or \
 
42
           not os.path.isdir(self.path):
 
43
            raise exception.NotFound()
 
44
 
 
45
        self.ctime = os.path.getctime(self.path)
 
46
 
 
47
    def __repr__(self):
 
48
        return "<Bucket: %s>" % self.name
 
49
 
 
50
    @staticmethod
 
51
    def all():
 
52
        """ list of all buckets """
 
53
        buckets = []
 
54
        for fn in glob.glob("%s/*.json" % FLAGS.buckets_path):
 
55
            try:
 
56
                json.load(open(fn))
 
57
                name = os.path.split(fn)[-1][:-5]
 
58
                buckets.append(Bucket(name))
 
59
            except:
 
60
                pass
 
61
 
 
62
        return buckets
 
63
 
 
64
    @staticmethod
 
65
    def create(bucket_name, user):
 
66
        """Create a new bucket owned by a user.
 
67
 
 
68
        @bucket_name: a string representing the name of the bucket to create
 
69
        @user: a nova.auth.user who should own the bucket.
 
70
 
 
71
        Raises:
 
72
            NotAuthorized: if the bucket is already exists or has invalid name
 
73
        """
 
74
        path = os.path.abspath(os.path.join(
 
75
            FLAGS.buckets_path, bucket_name))
 
76
        if not path.startswith(os.path.abspath(FLAGS.buckets_path)) or \
 
77
           os.path.exists(path):
 
78
               raise exception.NotAuthorized()
 
79
 
 
80
        os.makedirs(path)
 
81
 
 
82
        with open(path+'.json', 'w') as f:
 
83
            json.dump({'ownerId': user.id}, f)
 
84
 
 
85
    @property
 
86
    def metadata(self):
 
87
        """ dictionary of metadata around bucket,
 
88
        keys are 'Name' and 'CreationDate'
 
89
        """
 
90
 
 
91
        return {
 
92
            "Name": self.name,
 
93
            "CreationDate": datetime.datetime.utcfromtimestamp(self.ctime),
 
94
        }
 
95
 
 
96
    @property
 
97
    def owner_id(self):
 
98
        try:
 
99
            with open(self.path+'.json') as f:
 
100
                return json.load(f)['ownerId']
 
101
        except:
 
102
            return None
 
103
 
 
104
    def is_authorized(self, user):
 
105
        try:
 
106
            return user.is_admin() or self.owner_id == user.id
 
107
        except Exception, e:
 
108
            pass
 
109
 
 
110
    def list_keys(self, prefix='', marker=None, max_keys=1000, terse=False):
 
111
        object_names = []
 
112
        for root, dirs, files in os.walk(self.path):
 
113
            for file_name in files:
 
114
                object_names.append(os.path.join(root, file_name)[len(self.path)+1:])
 
115
        object_names.sort()
 
116
        contents = []
 
117
 
 
118
        start_pos = 0
 
119
        if marker:
 
120
            start_pos = bisect.bisect_right(object_names, marker, start_pos)
 
121
        if prefix:
 
122
            start_pos = bisect.bisect_left(object_names, prefix, start_pos)
 
123
 
 
124
        truncated = False
 
125
        for object_name in object_names[start_pos:]:
 
126
            if not object_name.startswith(prefix):
 
127
                break
 
128
            if len(contents) >= max_keys:
 
129
                truncated = True
 
130
                break
 
131
            object_path = self._object_path(object_name)
 
132
            c = {"Key": object_name}
 
133
            if not terse:
 
134
                info = os.stat(object_path)
 
135
                c.update({
 
136
                    "LastModified": datetime.datetime.utcfromtimestamp(
 
137
                        info.st_mtime),
 
138
                    "Size": info.st_size,
 
139
                })
 
140
            contents.append(c)
 
141
            marker = object_name
 
142
 
 
143
        return {
 
144
            "Name": self.name,
 
145
            "Prefix": prefix,
 
146
            "Marker": marker,
 
147
            "MaxKeys": max_keys,
 
148
            "IsTruncated": truncated,
 
149
            "Contents": contents,
 
150
        }
 
151
 
 
152
    def _object_path(self, object_name):
 
153
        fn = os.path.join(self.path, object_name)
 
154
 
 
155
        if not fn.startswith(self.path):
 
156
            raise exception.NotAuthorized()
 
157
 
 
158
        return fn
 
159
 
 
160
    def delete(self):
 
161
        if len(os.listdir(self.path)) > 0:
 
162
            raise exception.NotAuthorized()
 
163
        os.rmdir(self.path)
 
164
        os.remove(self.path+'.json')
 
165
 
 
166
    def __getitem__(self, key):
 
167
        return stored.Object(self, key)
 
168
 
 
169
    def __setitem__(self, key, value):
 
170
        with open(self._object_path(key), 'wb') as f:
 
171
            f.write(value)
 
172
 
 
173
    def __delitem__(self, key):
 
174
        stored.Object(self, key).delete()