/
pool.py
167 lines (137 loc) · 6.54 KB
/
pool.py
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import cherrypy
from . import ApiController, RESTController, Endpoint, ReadPermission, Task
from .. import mgr
from ..security import Scope
from ..services.ceph_service import CephService
from ..services.exception import handle_send_command_error
from ..tools import str_to_bool
def pool_task(name, metadata, wait_for=2.0):
return Task("pool/{}".format(name), metadata, wait_for)
@ApiController('/pool', Scope.POOL)
class Pool(RESTController):
@staticmethod
def _serialize_pool(pool, attrs):
if not attrs or not isinstance(attrs, list):
attrs = pool.keys()
crush_rules = {r['rule_id']: r["rule_name"] for r in mgr.get('osd_map_crush')['rules']}
res = {}
for attr in attrs:
if attr not in pool:
continue
if attr == 'type':
res[attr] = {1: 'replicated', 3: 'erasure'}[pool[attr]]
elif attr == 'crush_rule':
res[attr] = crush_rules[pool[attr]]
elif attr == 'application_metadata':
res[attr] = list(pool[attr].keys())
else:
res[attr] = pool[attr]
# pool_name is mandatory
res['pool_name'] = pool['pool_name']
return res
def _pool_list(self, attrs=None, stats=False):
if attrs:
attrs = attrs.split(',')
if str_to_bool(stats):
pools = CephService.get_pool_list_with_stats()
else:
pools = CephService.get_pool_list()
return [self._serialize_pool(pool, attrs) for pool in pools]
def list(self, attrs=None, stats=True):
return self._pool_list(attrs, stats)
def _get(self, pool_name, attrs=None, stats=False):
# type: (str, str, bool) -> dict
pools = self._pool_list(attrs, stats)
pool = [pool for pool in pools if pool['pool_name'] == pool_name]
if not pool:
raise cherrypy.NotFound('No such pool')
return pool[0]
def get(self, pool_name, attrs=None, stats=False):
# type: (str, str, bool) -> dict
return self._get(pool_name, attrs, stats)
@pool_task('delete', ['{pool_name}'])
@handle_send_command_error('pool')
def delete(self, pool_name):
return CephService.send_command('mon', 'osd pool delete', pool=pool_name, pool2=pool_name,
yes_i_really_really_mean_it=True)
@pool_task('edit', ['{pool_name}'])
def set(self, pool_name, flags=None, application_metadata=None, **kwargs):
self._set_pool_values(pool_name, application_metadata, flags, True, kwargs)
@pool_task('create', {'pool_name': '{pool}'})
@handle_send_command_error('pool')
def create(self, pool, pg_num, pool_type, erasure_code_profile=None, flags=None,
application_metadata=None, rule_name=None, **kwargs):
ecp = erasure_code_profile if erasure_code_profile else None
CephService.send_command('mon', 'osd pool create', pool=pool, pg_num=int(pg_num),
pgp_num=int(pg_num), pool_type=pool_type, erasure_code_profile=ecp,
rule=rule_name)
self._set_pool_values(pool, application_metadata, flags, False, kwargs)
def _set_pool_values(self, pool, application_metadata, flags, update_existing, kwargs):
update_name = False
if update_existing:
current_pool = self._get(pool)
self._handle_update_compression_args(current_pool.get('options'), kwargs)
if flags and 'ec_overwrites' in flags:
CephService.send_command('mon', 'osd pool set', pool=pool, var='allow_ec_overwrites',
val='true')
if application_metadata is not None:
def set_app(what, app):
CephService.send_command('mon', 'osd pool application ' + what, pool=pool, app=app,
yes_i_really_mean_it=True)
if update_existing:
original_app_metadata = set(
current_pool.get('application_metadata'))
else:
original_app_metadata = set()
for app in original_app_metadata - set(application_metadata):
set_app('disable', app)
for app in set(application_metadata) - original_app_metadata:
set_app('enable', app)
def set_key(key, value):
CephService.send_command('mon', 'osd pool set', pool=pool, var=key, val=str(value))
for key, value in kwargs.items():
if key == 'pool':
update_name = True
destpool = value
else:
set_key(key, value)
if key == 'pg_num':
set_key('pgp_num', value)
if update_name:
CephService.send_command('mon', 'osd pool rename', srcpool=pool, destpool=destpool)
def _handle_update_compression_args(self, options, kwargs):
if kwargs.get('compression_mode') == 'unset' and options is not None:
def reset_arg(arg, value):
if options.get(arg):
kwargs[arg] = value
for arg in ['compression_min_blob_size', 'compression_max_blob_size',
'compression_required_ratio']:
reset_arg(arg, '0')
reset_arg('compression_algorithm', 'unset')
@Endpoint()
@ReadPermission
def _info(self):
"""Used by the create-pool dialog"""
def rules(pool_type):
return [r
for r in mgr.get('osd_map_crush')['rules']
if r['type'] == pool_type]
def all_bluestore():
return all(o['osd_objectstore'] == 'bluestore'
for o in mgr.get('osd_metadata').values())
def compression_enum(conf_name):
return [[v for v in o['enum_values'] if len(v) > 0]
for o in mgr.get('config_options')['options']
if o['name'] == conf_name][0]
return {
"pool_names": [p['pool_name'] for p in self._pool_list()],
"crush_rules_replicated": rules(1),
"crush_rules_erasure": rules(3),
"is_all_bluestore": all_bluestore(),
"osd_count": len(mgr.get('osd_map')['osds']),
"bluestore_compression_algorithm": mgr.get('config')['bluestore_compression_algorithm'],
"compression_algorithms": compression_enum('bluestore_compression_algorithm'),
"compression_modes": compression_enum('bluestore_compression_mode'),
}