forked from oceanbase/obdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_types.py
432 lines (329 loc) · 14.4 KB
/
_types.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# coding: utf-8
# OceanBase Deploy.
# Copyright (C) 2021 OceanBase
#
# This file is part of OceanBase Deploy.
#
# OceanBase Deploy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OceanBase Deploy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OceanBase Deploy. If not, see <https://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
import os
import re
import uuid
import traceback
__all__ = ("Moment", "Time", "Capacity", "CapacityWithB", "CapacityMB", "StringList", "Dict", "List", "StringOrKvList", "Double", "Boolean", "Integer", "String", "Path", "SafeString", "PathList", "SafeStringList", "DBUrl", "WebUrl", "OBUser")
class Null(object):
def __init__(self):
pass
class ConfigItemType(object):
TYPE_STR = None
NULL = Null()
def __init__(self, s):
try:
self._origin = s
self._value = 0
self.value = self.NULL
self._format()
if self.value == self.NULL:
self.value = self._origin
except Exception as e:
if str(e):
raise Exception("%s" % str(e))
raise Exception("'%s' is not %s" % (self._origin, self._type_str))
@property
def _type_str(self):
if self.TYPE_STR is None:
self.TYPE_STR = str(self.__class__.__name__).split('.')[-1]
return self.TYPE_STR
def _format(self):
raise NotImplementedError
def __str__(self):
return str(self._origin)
def __hash__(self):
return self._origin.__hash__()
@property
def __cmp_value__(self):
return self._value
def __eq__(self, value):
if value is None:
return False
return self.__cmp_value__ == value.__cmp_value__
def __gt__(self, value):
if value is None:
return True
return self.__cmp_value__ > value.__cmp_value__
def __ge__(self, value):
if value is None:
return True
return self.__eq__(value) or self.__gt__(value)
def __lt__(self, value):
if value is None:
return False
return self.__cmp_value__ < value.__cmp_value__
def __le__(self, value):
if value is None:
return False
return self.__eq__(value) or self.__lt__(value)
class Moment(ConfigItemType):
def _format(self):
if self._origin:
if self._origin.upper() == 'DISABLE':
self._value = 0
else:
r = re.match('^(\d{1,2}):(\d{1,2})$', self._origin)
h, m = r.groups()
h, m = int(h), int(m)
if 0 <= h <= 23 and 0 <= m <= 60:
self._value = h * 60 + m
else:
raise Exception('Invalid Value(Please use the format like 20:00)')
else:
self._value = 0
class Time(ConfigItemType):
UNITS = {
'ns': 0.000000001,
'us': 0.000001,
'ms': 0.001,
's': 1,
'm': 60,
'h': 3600,
'd': 86400
}
def _format(self):
if self._origin:
self._origin = str(self._origin).strip()
if self._origin.isdigit():
n = self._origin
unit = self.UNITS['s']
else:
r = re.match('^(\d+)(\w+)$', self._origin.lower())
n, u = r.groups()
unit = self.UNITS.get(u.lower())
if unit:
self._value = int(n) * unit
else:
raise Exception('%s is Invalid Value(Please use the format like 20m、20h or 20s)'.format(self._origin))
else:
self._value = 0
class DecimalValue:
def __init__(self, value, precision=None):
if isinstance(value, str):
self.value = float(value)
else:
self.value = value
self.precision = precision
def __repr__(self):
if self.precision is not None:
return "%.*f" % (self.precision, self.value)
return str(self.value)
def __add__(self, other):
if isinstance(other, DecimalValue):
return DecimalValue(self.value + other.value, self.precision if self.precision is not None else other.precision)
return DecimalValue(self.value + other, self.precision)
def __sub__(self, other):
if isinstance(other, DecimalValue):
return DecimalValue(self.value - other.value, self.precision if self.precision is not None else other.precision)
return DecimalValue(self.value - other, self.precision)
def __mul__(self, other):
if isinstance(other, DecimalValue):
return DecimalValue(self.value * other.value, self.precision if self.precision is not None else other.precision)
return DecimalValue(self.value * other, self.precision)
def __truediv__(self, other):
if isinstance(other, DecimalValue):
return DecimalValue(self.value / other.value, self.precision if self.precision is not None else other.precision)
return DecimalValue(self.value / other, self.precision)
class Capacity(ConfigItemType):
UNITS = {"B": 1, "K": 1 << 10, "M": 1 << 20, "G": 1 << 30, "T": 1 << 40, "P": 1 << 50}
def __init__(self, s, precision = 0):
self.precision = precision
super(Capacity, self).__init__(s)
def __str__(self):
return str(self.value)
@property
def bytes(self):
return self._value
def _format(self):
if self._origin:
if not isinstance(self._origin, str) or self._origin.strip().isdigit():
self._origin = int(float(self._origin))
n = self._origin
unit = self.UNITS['B']
for u, v in sorted(self.UNITS.items(), key=lambda item: item[1], reverse=True):
if n >= v:
n /= v
break
n = self._origin
else:
groups = re.match("^(\d+)\s*([BKMGTP])((IB)|B)?\s*$", self._origin.upper())
if not groups:
raise ValueError("Invalid capacity string: %s(Please use the format like 20G/20GB/20GIB)" % self._origin)
n, u, _, _ = groups.groups()
unit = self.UNITS.get(u.upper())
if unit:
self._value = int(n) * unit
self.value = str(DecimalValue(self._value, self.precision) / self.UNITS[u]) + u
else:
raise Exception('Invalid Value')
else:
self._value = 0
self.value = str(DecimalValue(0, self.precision))
class CapacityWithB(Capacity):
def __init__(self, s):
super(CapacityWithB, self).__init__(s, precision=0)
def _format(self):
super(CapacityWithB, self)._format()
self.value = self.value + 'B'
class CapacityMB(Capacity):
def _format(self):
super(CapacityMB, self)._format()
if isinstance(self._origin, str) and self._origin.isdigit():
self.value = self._origin + 'M'
self._value *= self.UNITS['M']
if not self._origin:
self.value = '0M'
class StringList(ConfigItemType):
def _format(self):
if self._origin:
self._origin = str(self._origin).strip()
self._value = self._origin.split(';')
else:
self._value = []
class Dict(ConfigItemType):
def _format(self):
if self._origin:
if not isinstance(self._origin, dict):
raise Exception("Invalid Value: {} is not a dict.".format(self._origin))
self._value = self._origin
else:
self._value = self.value = {}
class List(ConfigItemType):
def _format(self):
if self._origin:
if not isinstance(self._origin, list):
raise Exception("Invalid value: {} is not a list.".format(self._origin))
self._value = self._origin
else:
self._value = self.value = []
class StringOrKvList(ConfigItemType):
def _format(self):
if self._origin:
if not isinstance(self._origin, list):
raise Exception("Invalid value: {} is not a list.".format(self._origin))
for item in self._origin:
if not item:
continue
if not isinstance(item, (str, dict)):
raise Exception("Invalid value: {} should be string or key-value format.".format(item))
if isinstance(item, dict):
if len(item.keys()) != 1:
raise Exception("Invalid value: {} should be single key-value format".format(item))
self._value = self._origin
else:
self._value = self.value = []
class Double(ConfigItemType):
def _format(self):
self.value = self._value = float(self._origin) if self._origin else 0
class Boolean(ConfigItemType):
def _format(self):
if isinstance(self._origin, bool):
self._value = self._origin
else:
_origin = str(self._origin).lower()
if _origin == 'true':
self._value = True
elif _origin == 'false':
self._value = False
elif _origin.isdigit():
self._value = bool(self._origin)
else:
raise Exception('%s is not Boolean' % _origin)
self.value = self._value
class Integer(ConfigItemType):
def _format(self):
if self._origin is None:
self._value = 0
self._origin = 0
else:
_origin = str(self._origin)
try:
self.value = self._value = int(_origin)
except:
raise Exception('%s is not Integer' % _origin)
class String(ConfigItemType):
def _format(self):
self.value = self._value = str(self._origin) if self._origin else ''
# this type is used to ensure the parameter is a valid oceanbase user
class OBUser(ConfigItemType):
OB_USER_PATTERN = re.compile("^[a-zA-Z0-9_.-]+(@[a-zA-Z0-9_.-]+)?(#[a-zA-Z0-9_.-]+)?$")
def _format(self):
if not self.OB_USER_PATTERN.match(str(self._origin)):
raise Exception("%s is not a valid config(Please use the format like root@sys#obcluster" % self._origin)
self.value = self._value = str(self._origin) if self._origin else ''
# this type is used to ensure the parameter not containing special characters to inject command
class SafeString(ConfigItemType):
SAFE_STRING_PATTERN = re.compile("^[a-zA-Z0-9\u4e00-\u9fa5\-_:@/\.]*$")
def _format(self):
if not self.SAFE_STRING_PATTERN.match(str(self._origin)):
raise Exception("%s is not a valid string(Support: a-z、A-Z、0-9、chinese characters、- _ : @ / .)" % self._origin)
self.value = self._value = str(self._origin) if self._origin else ''
# this type is used to ensure the parameter not containing special characters to inject command
class SafeStringList(ConfigItemType):
SAFE_STRING_PATTERN = re.compile("^[a-zA-Z0-9\u4e00-\u9fa5\-_:@/\.]*$")
def _format(self):
if self._origin:
self._origin = str(self._origin).strip()
self._value = self._origin.split(';')
for v in self._value:
if not self.SAFE_STRING_PATTERN.match(v):
raise Exception("%s is not a valid string(Support: a-z、A-Z、0-9、chinese characters、- _ : @ / .)" % v)
else:
self._value = []
# this type is used to ensure the parameter is a valid path by checking it's only certaining certain characters and not crossing path
class Path(ConfigItemType):
PATH_PATTERN = re.compile("^[a-zA-Z0-9\u4e00-\u9fa5\-_:@/\.]*$")
def _format(self):
parent_path = "/{0}".format(uuid.uuid4().hex)
absolute_path = "/".join([parent_path, str(self._origin)])
normalized_path = os.path.normpath(absolute_path)
if not (self.PATH_PATTERN.match(str(self._origin)) and normalized_path.startswith(parent_path)):
raise Exception("%s is not a valid path(Support: a-z、A-Z、0-9、chinese characters、- _ : @ / .)" % self._origin)
self.value = self._value = str(self._origin) if self._origin else ''
# this type is used to ensure the parameter is a valid path by checking it's only certaining certain characters and not crossing path
class PathList(ConfigItemType):
PATH_PATTERN = re.compile("^[a-zA-Z0-9\u4e00-\u9fa5\-_:@/\.]*$")
def _format(self):
parent_path = "/{0}".format(uuid.uuid4().hex)
if self._origin:
self._origin = str(self._origin).strip()
self._value = self._origin.split(';')
for v in self._value:
absolute_path = "/".join([parent_path, v])
normalized_path = os.path.normpath(absolute_path)
if not (self.PATH_PATTERN.match(v) and normalized_path.startswith(parent_path)):
raise Exception("%s is not a valid path(Support: a-z、A-Z、0-9、chinese characters、- _ : @ / .)" % v)
else:
self._value = []
# this type is used to ensure the parameter is a valid database connection url
class DBUrl(ConfigItemType):
DBURL_PATTERN = re.compile("^jdbc:(mysql|oceanbase):(\/\/)([a-zA-Z0-9_.-]+)(:[0-9]{1,5})?\/([a-zA-Z0-9_\-]+)(\?[a-zA-Z0-9_&;=.-]*)?$")
def _format(self):
if not self.DBURL_PATTERN.match(str(self._origin)):
raise Exception("%s is not a valid config(Please use the format like jdbc:mysql://root:[email protected]:2883/test)" % self._origin)
self.value = self._value = str(self._origin) if self._origin else ''
# this type is used to ensure the parameter is a valid web url
class WebUrl(ConfigItemType):
WEBURL_PATTERN = re.compile("^(https?:\/\/)?([\da-z_.-]+)(:[0-9]{1,5})?([\/\w \.-]*)*\/?(?:\?[\w&=_.-]*)?$")
def _format(self):
if not self.WEBURL_PATTERN.match(str(self._origin)):
raise Exception("%s is not a valid config(Please use the format like https://127.0.0.1:8680/test)" % self._origin)
self.value = self._value = str(self._origin) if self._origin else ''