root / snf-pithos-backend / pithos / backends / lib / hashfiler / store_helpers.py @ 401777e9
History | View | Annotate | Download (4.4 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import os |
35 |
import rados |
36 |
from hashlib import new as newhasher |
37 |
from pithos.api.settings import RADOS_CEPH_CONF |
38 |
|
39 |
|
40 |
def bootstrap_storage(storageType, **params): |
41 |
umask = params['umask']
|
42 |
path = params['path']
|
43 |
if umask is not None: |
44 |
os.umask(umask) |
45 |
if storageType.lower() == 'nfs': |
46 |
if path and not os.path.exists(path): |
47 |
os.makedirs(path) |
48 |
if not os.path.isdir(path): |
49 |
raise RuntimeError("Cannot open path '%s'" % (path,)) |
50 |
elif storageType.lower() == 'rados': |
51 |
cluster = rados.Rados(conffile=RADOS_CEPH_CONF) |
52 |
cluster.connect() |
53 |
if not cluster.pool_exists(params['blockpool']): |
54 |
try:
|
55 |
cluster.create_pool(params['blockpool'])
|
56 |
except Exception as err: |
57 |
err_msg = "Cannot create % RADOS Pool"
|
58 |
raise RuntimeError(err_msg % params["blockpool"]) |
59 |
if not cluster.pool_exists(params['mappool']): |
60 |
try:
|
61 |
cluster.create_pool(params['mappool'])
|
62 |
except Exception as err: |
63 |
err_msg = "Cannot create % RADOS Pool"
|
64 |
raise RuntimeError(err_msg % params["mappool"]) |
65 |
cluster.shutdown() |
66 |
hashtype = params['hash_algorithm']
|
67 |
try:
|
68 |
hasher = newhasher(hashtype) |
69 |
except ValueError: |
70 |
msg = "Variable hashtype '%s' is not available from hashlib"
|
71 |
raise ValueError(msg % (hashtype,)) |
72 |
|
73 |
hasher.update("")
|
74 |
emptyhash = hasher.digest() |
75 |
|
76 |
pb = {'blocksize': params['block_size'], |
77 |
'blockpath': os.path.join(path + '/blocks'), |
78 |
'hashtype': params['hash_algorithm'], |
79 |
'blockpool': params['blockpool']} |
80 |
pm = {'mappath': os.path.join(path + '/maps'), |
81 |
'namelen': len(emptyhash), |
82 |
'mappool': params['mappool']} |
83 |
|
84 |
return (pb, pm)
|
85 |
|
86 |
|
87 |
def get_blocker(storageType, **params): |
88 |
rblocker = None
|
89 |
fblocker = None
|
90 |
hashlen = None
|
91 |
blocksize = None
|
92 |
if storageType.lower() == 'rados': |
93 |
if params['blockpool']: |
94 |
from radosblocker import RadosBlocker |
95 |
rblocker = RadosBlocker(**params) |
96 |
hashlen = rblocker.hashlen |
97 |
blocksize = params['blocksize']
|
98 |
elif storageType.lower() == 'nfs': |
99 |
from fileblocker import FileBlocker |
100 |
fblocker = FileBlocker(**params) |
101 |
hashlen = fblocker.hashlen |
102 |
blocksize = params['blocksize']
|
103 |
else:
|
104 |
raise RuntimeError("Cannot find suitable pithos backend storage") |
105 |
return (fblocker, rblocker, hashlen, blocksize)
|
106 |
|
107 |
|
108 |
def get_mapper(storageType, **params): |
109 |
rmap = None
|
110 |
fmap = None
|
111 |
if storageType.lower() == 'rados': |
112 |
if params['mappool']: |
113 |
from radosmapper import RadosMapper |
114 |
rmap = RadosMapper(**params) |
115 |
elif storageType.lower() == 'nfs': |
116 |
from filemapper import FileMapper |
117 |
fmap = FileMapper(**params) |
118 |
else:
|
119 |
raise RuntimeError("Cannot find suitable pithos backend storage") |
120 |
return (fmap, rmap)
|