-
Notifications
You must be signed in to change notification settings - Fork 133
/
_cmd.py
2288 lines (1800 loc) · 110 KB
/
_cmd.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 sys
import time
import textwrap
import json
from uuid import uuid1 as uuid, UUID
from optparse import OptionParser, BadOptionError, Option, IndentedHelpFormatter
from core import ObdHome
from _stdio import IO, FormatText
from _lock import LockMode
from _types import Capacity
from tool import DirectoryUtil, FileUtil, NetUtil, COMMAND_ENV
from _errno import DOC_LINK_MSG, LockError
import _environ as ENV
from ssh import LocalClient
from const import (
CONST_OBD_HOME,
VERSION, REVISION, BUILD_BRANCH, BUILD_TIME, FORBIDDEN_VARS,
COMP_OCEANBASE_DIAGNOSTIC_TOOL, PKG_RPM_FILE,PKG_REPO_FILE
)
ROOT_IO = IO(1)
OBD_HOME_PATH = os.path.join(os.environ.get(CONST_OBD_HOME, os.getenv('HOME')), '.obd')
OBDIAG_HOME_PATH = os.path.join(os.environ.get(CONST_OBD_HOME, os.getenv('HOME')), COMP_OCEANBASE_DIAGNOSTIC_TOOL)
COMMAND_ENV.load(os.path.join(OBD_HOME_PATH, '.obd_environ'), ROOT_IO)
ROOT_IO.default_confirm = COMMAND_ENV.get(ENV.ENV_DEFAULT_CONFIRM, '0') == '1'
class OptionHelpFormatter(IndentedHelpFormatter):
def format_option(self, option):
result = []
opts = self.option_strings[option]
opt_width = self.help_position - self.current_indent - 2
if len(opts) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts)
indent_first = self.help_position
else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
indent_first = 0
result.append(opts)
if option.help:
help_text = self.expand_default(option)
help_lines = help_text.split('\n')
if len(help_lines) == 1:
help_lines = textwrap.wrap(help_text, self.help_width)
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
for line in help_lines[1:]])
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)
class AllowUndefinedOptionParser(OptionParser):
IS_TTY = sys.stdin.isatty()
def __init__(self,
usage=None,
option_list=None,
option_class=Option,
version=None,
conflict_handler="error",
description=None,
formatter=None,
add_help_option=True,
prog=None,
epilog=None,
allow_undefine=True,
undefine_warn=True
):
OptionParser.__init__(
self, usage, option_list, option_class, version, conflict_handler,
description, formatter, add_help_option, prog, epilog
)
self.allow_undefine = allow_undefine
self.undefine_warn = undefine_warn
def warn(self, msg, file=None):
if self.IS_TTY:
print("%s %s" % (IO.WARNING_PREV, msg))
else:
print('warn: %s' % msg)
def _process_long_opt(self, rargs, values):
try:
value = rargs[0]
OptionParser._process_long_opt(self, rargs, values)
except BadOptionError as e:
if self.allow_undefine:
key = e.opt_str
value = value[len(key)+1:]
setattr(values, key.strip('-').replace('-', '_'), value if value != '' else True)
self.undefine_warn and self.warn(e)
else:
raise e
def _process_short_opts(self, rargs, values):
try:
value = rargs[0]
OptionParser._process_short_opts(self, rargs, values)
except BadOptionError as e:
if self.allow_undefine:
key = e.opt_str
value = value[len(key)+1:]
setattr(values, key.strip('-').replace('-', '_'), value if value != '' else True)
self.undefine_warn and self.warn(e)
else:
raise e
def print_usage(self, file=None):
print(self.format_help(OptionHelpFormatter()), file=file)
class BaseCommand(object):
def __init__(self, name, summary):
self.name = name
self.summary = summary
self.args = []
self.cmds = []
self.opts = {}
self.prev_cmd = ''
self.is_init = False
self.hidden = False
self.has_trace = True
self.parser = AllowUndefinedOptionParser(add_help_option=False)
self.parser.add_option('-h', '--help', action='callback', callback=self._show_help, help='Show help and exit.')
self.parser.add_option('-v', '--verbose', action='callback', callback=self._set_verbose, help='Activate verbose output.')
def _set_verbose(self, *args, **kwargs):
ROOT_IO.set_verbose_level(0xfffffff)
def init(self, cmd, args):
if self.is_init is False:
self.prev_cmd = cmd
self.args = args
self.is_init = True
self.parser.prog = self.prev_cmd
option_list = self.parser.option_list[2:]
option_list.append(self.parser.option_list[0])
option_list.append(self.parser.option_list[1])
self.parser.option_list = option_list
return self
def parse_command(self):
self.opts, self.cmds = self.parser.parse_args(self.args)
return self.opts
def do_command(self):
raise NotImplementedError
def _show_help(self, *args, **kwargs):
ROOT_IO.print(self._mk_usage())
self.parser.exit(1)
def _mk_usage(self):
return self.parser.format_help(OptionHelpFormatter())
class ObdCommand(BaseCommand):
OBD_PATH = OBD_HOME_PATH
OBD_INSTALL_PRE = COMMAND_ENV.get(ENV.ENV_OBD_INSTALL_PRE, '/')
OBD_INSTALL_PATH = COMMAND_ENV.get(ENV.ENV_OBD_INSTALL_PATH, os.path.join(OBD_INSTALL_PRE, 'usr/obd/'))
def init_home(self):
version_path = os.path.join(self.OBD_PATH, 'version')
version_fobj = FileUtil.open(version_path, 'a+', stdio=ROOT_IO)
version_fobj.seek(0)
version = version_fobj.read()
if not COMMAND_ENV.get(ENV.ENV_OBD_ID):
COMMAND_ENV.set(ENV.ENV_OBD_ID, uuid())
if VERSION != version:
for part in ['plugins', 'config_parser', 'optimize', 'mirror/remote']:
obd_part_dir = os.path.join(self.OBD_PATH, part)
if DirectoryUtil.mkdir(self.OBD_PATH):
root_part_path = os.path.join(self.OBD_INSTALL_PATH, part)
if os.path.exists(root_part_path):
DirectoryUtil.copy(root_part_path, obd_part_dir, ROOT_IO)
version_fobj.seek(0)
version_fobj.truncate()
version_fobj.write(VERSION)
version_fobj.flush()
version_fobj.close()
@property
def dev_mode(self):
return COMMAND_ENV.get(ENV.ENV_DEV_MODE) == "1"
@property
def lock_mode(self):
return COMMAND_ENV.get(ENV.ENV_LOCK_MODE)
@property
def enable_log(self):
return True
def parse_command(self):
if self.parser.allow_undefine != True:
self.parser.allow_undefine = self.dev_mode
return super(ObdCommand, self).parse_command()
def _init_log(self):
trace_id = uuid()
log_dir = os.path.join(self.OBD_PATH, 'log')
DirectoryUtil.mkdir(log_dir)
log_path = os.path.join(log_dir, 'obd')
ROOT_IO.init_trace_logger(log_path, 'obd', trace_id)
ROOT_IO.exit_msg = '''Trace ID: {trace_id}
If you want to view detailed obd logs, please run: obd display-trace {trace_id}'''.format(trace_id=trace_id)
def do_command(self):
self.parse_command()
self.init_home()
ret = False
try:
if self.has_trace and self.enable_log:
self._init_log()
ROOT_IO.track_limit += 1
ROOT_IO.verbose('cmd: %s' % self.cmds)
ROOT_IO.verbose('opts: %s' % self.opts)
obd = ObdHome(home_path=self.OBD_PATH, dev_mode=self.dev_mode, lock_mode=self.lock_mode, stdio=ROOT_IO)
obd.set_options(self.opts)
obd.set_cmds(self.cmds)
ret = self._do_command(obd)
if not ret:
ROOT_IO.exit_msg = DOC_LINK_MSG + "\n" + ROOT_IO.exit_msg
except NotImplementedError:
ROOT_IO.exception('command \'%s\' is not implemented' % self.prev_cmd)
except LockError:
ROOT_IO.exception('Another app is currently holding the obd lock.')
except SystemExit:
pass
except KeyboardInterrupt:
ROOT_IO.exception('Keyboard Interrupt')
except:
e = sys.exc_info()[1]
ROOT_IO.exception('Running Error: %s' % e)
return ret
def _do_command(self, obd):
raise NotImplementedError
def get_white_ip_list(self):
if self.opts.white:
return self.opts.white.split(',')
ROOT_IO.warn("Security Risk: the whitelist is empty and anyone can request this program!")
if ROOT_IO.confirm("Do you want to continue?"):
return []
wthite_ip_list = ROOT_IO.read("Please enter the whitelist, eq: '192.168.1.1'")
raise wthite_ip_list.split(',')
class MajorCommand(BaseCommand):
def __init__(self, name, summary):
super(MajorCommand, self).__init__(name, summary)
self.commands = {}
def _mk_usage(self):
if self.commands:
usage = ['%s <command> [options]\n\nAvailable commands:\n' % self.prev_cmd]
commands = [x for x in self.commands.values() if not (hasattr(x, 'hidden') and x.hidden)]
commands.sort(key=lambda x: x.name)
for command in commands:
if command.hidden is False:
usage.append("%-14s %s\n" % (command.name, command.summary))
self.parser.set_usage('\n'.join(usage))
return super(MajorCommand, self)._mk_usage()
def do_command(self):
if not self.is_init:
ROOT_IO.error('%s command not init' % self.prev_cmd)
raise SystemExit('command not init')
if len(self.args) < 1:
ROOT_IO.print('You need to give some commands.\n\nTry `obd --help` for more information.')
self._show_help()
return False
base, args = self.args[0], self.args[1:]
if base not in self.commands:
self.parse_command()
self._show_help()
return False
cmd = '%s %s' % (self.prev_cmd, base)
ROOT_IO.track_limit += 1
return self.commands[base].init(cmd, args).do_command()
def register_command(self, command):
self.commands[command.name] = command
class HiddenObdCommand(ObdCommand):
def __init__(self, name, summary):
super(HiddenObdCommand, self).__init__(name, summary)
self.hidden = self.dev_mode is False
class HiddenMajorCommand(MajorCommand, HiddenObdCommand):
pass
class DevCommand(HiddenObdCommand):
def do_command(self):
if self.hidden:
ROOT_IO.error('`%s` is a developer command. Please start the developer mode first.\nUse `obd devmode enable` to start the developer mode' % self.prev_cmd)
return False
return super(DevCommand, self).do_command()
class DevModeEnableCommand(HiddenObdCommand):
def __init__(self):
super(DevModeEnableCommand, self).__init__('enable', 'Enable Dev Mode')
def _do_command(self, obd):
if COMMAND_ENV.set(ENV.ENV_DEV_MODE, "1", save=True, stdio=obd.stdio):
obd.stdio.print("Dev Mode: ON")
return True
return False
class DevModeDisableCommand(HiddenObdCommand):
def __init__(self):
super(DevModeDisableCommand, self).__init__('disable', 'Disable Dev Mode')
def _do_command(self, obd):
if COMMAND_ENV.set(ENV.ENV_DEV_MODE, "0", save=True, stdio=obd.stdio):
obd.stdio.print("Dev Mode: OFF")
return True
return False
class DevModeMajorCommand(HiddenMajorCommand):
def __init__(self):
super(DevModeMajorCommand, self).__init__('devmode', 'Developer mode switch')
self.register_command(DevModeEnableCommand())
self.register_command(DevModeDisableCommand())
class EnvironmentSetCommand(HiddenObdCommand):
def __init__(self):
super(EnvironmentSetCommand, self).__init__("set", "Set obd environment variable")
def init(self, cmd, args):
super(EnvironmentSetCommand, self).init(cmd, args)
self.parser.set_usage('%s [key] [value]' % self.prev_cmd)
return self
def _do_command(self, obd):
if len(self.cmds) == 2:
key = self.cmds[0]
if key in FORBIDDEN_VARS:
obd.stdio.error("Set the environment variable {} is not allowed.".format(key))
return False
return COMMAND_ENV.set(key, self.cmds[1], save=True, stdio=obd.stdio)
else:
return self._show_help()
class EnvironmentUnsetCommand(HiddenObdCommand):
def __init__(self):
super(EnvironmentUnsetCommand, self).__init__("unset", "Unset obd environment variable")
def init(self, cmd, args):
super(EnvironmentUnsetCommand, self).init(cmd, args)
self.parser.set_usage('%s [key] [value]' % self.prev_cmd)
return self
def _do_command(self, obd):
if len(self.cmds) == 1:
return COMMAND_ENV.delete(self.cmds[0], save=True, stdio=obd.stdio)
else:
return self._show_help()
class EnvironmentShowCommand(HiddenObdCommand):
def __init__(self):
super(EnvironmentShowCommand, self).__init__("show", "Show obd environment variables")
self.parser.add_option('-A', '--all', action="store_true", help="Show all environment variables including system variables")
def _do_command(self, obd):
if self.opts.all:
envs = COMMAND_ENV.copy().items()
else:
envs = COMMAND_ENV.show_env().items()
obd.stdio.print_list(envs, ["Key", "Value"], title="Environ")
return True
class EnvironmentClearCommand(HiddenObdCommand):
def __init__(self):
super(EnvironmentClearCommand, self).__init__("clear", "Clear obd environment variables")
def _do_command(self, obd):
return COMMAND_ENV.clear(stdio=obd.stdio)
class EnvironmentMajorCommand(HiddenMajorCommand):
def __init__(self):
super(EnvironmentMajorCommand, self).__init__('env', 'Environment variables for OBD')
self.register_command(EnvironmentSetCommand())
self.register_command(EnvironmentUnsetCommand())
self.register_command(EnvironmentShowCommand())
self.register_command(EnvironmentClearCommand())
class TelemetryPostCommand(HiddenObdCommand):
def __init__(self):
super(TelemetryPostCommand, self).__init__('post', "Post telemetry data to OceanBase.By default, OBD telemetry is enabled. To disable OBD telemetry, run the `obd env set TELEMETRY_MODE 0` command. To enable OBD telemetry data printing, run `obd env set TELEMETRY_LOG_MODE 1`.")
self.parser.add_option('-d', '--data', type='string', help="post obd data")
@property
def lock_mode(self):
return LockMode.NO_LOCK
@property
def enable_log(self):
return COMMAND_ENV.get(ENV.ENV_TELEMETRY_LOG_MODE, default='0') == '1'
def init(self, cmd, args):
super(TelemetryPostCommand, self).init(cmd, args)
self.parser.set_usage('%s <deploy name> [options]' % self.prev_cmd)
return self
def _do_command(self, obd):
return obd.telemetry_post(self.cmds[0])
class TelemetryMajorCommand(HiddenMajorCommand):
def __init__(self):
super(TelemetryMajorCommand, self).__init__('telemetry', "Telemetry for OB-Deploy.By default, OBD telemetry is enabled. To disable OBD telemetry, run the `obd env set TELEMETRY_MODE 0` command. To enable OBD telemetry data printing, run `obd env set TELEMETRY_LOG_MODE 1`.")
self.register_command(TelemetryPostCommand())
def do_command(self):
if COMMAND_ENV.get(ENV.ENV_TELEMETRY_MODE, default='1') == '1':
return super(TelemetryMajorCommand, self).do_command()
else:
ROOT_IO.critical('Telemetry is disabled. To enable OBD telemetry, run the `obd env set TELEMETRY_MODE 1` command.')
return False
class MirrorCloneCommand(ObdCommand):
def __init__(self):
super(MirrorCloneCommand, self).__init__('clone', 'Clone an RPM package to the local mirror repository.')
self.parser.add_option('-f', '--force', action='store_true', help="Force clone, overwrite the mirror.")
def init(self, cmd, args):
super(MirrorCloneCommand, self).init(cmd, args)
self.parser.set_usage('%s [mirror path] [options]' % self.prev_cmd)
return self
def _do_command(self, obd):
if self.cmds:
for src in self.cmds:
if not obd.add_mirror(src):
return False
return True
else:
return self._show_help()
class MirrorCreateCommand(ObdCommand):
def __init__(self):
super(MirrorCreateCommand, self).__init__('create', 'Create a local mirror by using the local binary file.')
self.parser.conflict_handler = 'resolve'
self.parser.add_option('-n', '--name', type='string', help="Mirror name.")
self.parser.add_option('-t', '--tag', type='string', help="Mirror tags. Multiple tags are separated with commas.")
self.parser.add_option('-V', '--version', type='string', help="Mirror version.")
self.parser.add_option('-p','--path', type='string', help="Mirror path. [./]", default='./')
self.parser.add_option('-f', '--force', action='store_true', help="Force create, overwrite the mirror.")
self.parser.conflict_handler = 'error'
def _do_command(self, obd):
return obd.create_repository()
class MirrorListCommand(ObdCommand):
def __init__(self):
super(MirrorListCommand, self).__init__('list', 'List mirrors.')
def init(self, cmd, args):
super(MirrorListCommand, self).init(cmd, args)
self.parser.set_usage('%s [section name] [options]\n\nExample: %s local' % (self.prev_cmd, self.prev_cmd))
return self
def show_pkg(self, name, pkgs):
ROOT_IO.print_list(
pkgs,
['name', 'version', 'release', 'arch', 'md5'],
lambda x: [x.name, x.version, x.release, x.arch, x.md5],
title='%s Package List' % name
)
def _do_command(self, obd):
if self.cmds:
name = self.cmds[0]
if name == 'local':
pkgs = obd.mirror_manager.local_mirror.get_all_pkg_info()
self.show_pkg(name, pkgs)
return True
else:
repos = obd.mirror_manager.get_mirrors(is_enabled=None)
for repo in repos:
if repo.section_name == name:
if not repo.enabled:
ROOT_IO.error('Mirror repository %s is disabled.' % name)
return False
pkgs = repo.get_all_pkg_info()
self.show_pkg(name, pkgs)
return True
ROOT_IO.error('No such mirror repository: %s' % name)
return False
else:
repos = obd.mirror_manager.get_mirrors(is_enabled=None)
ROOT_IO.print_list(
repos,
['SectionName', 'Type', 'Enabled', 'Avaiable' , 'Update Time'],
lambda x: [x.section_name, x.mirror_type.value, x.enabled, x.available, time.strftime("%Y-%m-%d %H:%M", time.localtime(x.repo_age))],
title='Mirror Repository List'
)
ROOT_IO.print("Use `obd mirror list <section name>` for more details")
return True
class MirrorUpdateCommand(ObdCommand):
def __init__(self):
super(MirrorUpdateCommand, self).__init__('update', 'Update remote mirror information.')
def _do_command(self, obd):
success = True
current = int(time.time())
mirrors = obd.mirror_manager.get_remote_mirrors()
for mirror in mirrors:
try:
if mirror.enabled and mirror.repo_age < current:
success = mirror.update_mirror() and success
except:
success = False
ROOT_IO.stop_loading('fail')
ROOT_IO.exception('Fail to synchronize mirorr (%s)' % mirror.name)
return success
class MirrorEnableCommand(ObdCommand):
def __init__(self):
super(MirrorEnableCommand, self).__init__('enable', 'Enable remote mirror repository.')
def _do_command(self, obd):
ret = True
for name in self.cmds:
ret = obd.mirror_manager.set_remote_mirror_enabled(name, True) and ret
return ret
class MirrorDisableCommand(ObdCommand):
def __init__(self):
super(MirrorDisableCommand, self).__init__('disable', 'Disable remote mirror repository.')
def _do_command(self, obd):
ret = True
for name in self.cmds:
ret = obd.mirror_manager.set_remote_mirror_enabled(name, False) and ret
return ret
class MirrorAddRepoCommand(ObdCommand):
def __init__(self):
super(MirrorAddRepoCommand, self).__init__('add-repo', 'Add remote mirror repository file.')
def _do_command(self, obd):
url = self.cmds[0]
return obd.mirror_manager.add_repo(url)
class MirrorCleanPkgCommand(ObdCommand):
def __init__(self):
super(MirrorCleanPkgCommand, self).__init__('clean', 'After the list of files to be deleted is displayed, double confirm and then clean up them.')
self.parser.add_option('-y', '--confirm', action='store_true', help="confirm to clean up.")
self.parser.add_option('-c', '--components', type='string', help="Clean up specified components. Separate multiple components with `,`.")
self.parser.add_option('-t', '--type', type='string', help="Specify the file types to be deleted as '%s or %s'." % (PKG_RPM_FILE, PKG_REPO_FILE))
self.parser.add_option('--hash', type='string', help="Repository's md5")
def _do_command(self, obd):
if self.opts.type and self.opts.type not in [PKG_RPM_FILE, PKG_REPO_FILE]:
ROOT_IO.error("Invalid type specified. Please specify '%s' or '%s'." % (PKG_RPM_FILE, PKG_REPO_FILE))
return False
return obd.clean_pkg(self.opts)
class MirrorMajorCommand(MajorCommand):
def __init__(self):
super(MirrorMajorCommand, self).__init__('mirror', 'Manage a component repository for OBD.')
self.register_command(MirrorListCommand())
self.register_command(MirrorCloneCommand())
self.register_command(MirrorCreateCommand())
self.register_command(MirrorUpdateCommand())
self.register_command(MirrorEnableCommand())
self.register_command(MirrorDisableCommand())
self.register_command(MirrorAddRepoCommand())
self.register_command(MirrorCleanPkgCommand())
class RepositoryListCommand(ObdCommand):
def __init__(self):
super(RepositoryListCommand, self).__init__('list', 'List local repository.')
@property
def lock_mode(self):
return LockMode.NO_LOCK
def show_repo(self, repos, name=None):
ROOT_IO.print_list(
repos,
['name', 'version', 'release', 'arch', 'md5', 'tags', 'size'],
lambda x: [x.name, x.version, x.release, x.arch, x.md5, ', '.join(x.tags), Capacity(x.size, 2).value],
title='%s Local Repository List' % name if name else 'Local Repository List'
)
def _do_command(self, obd):
name = self.cmds[0] if self.cmds else None
repos = obd.repository_manager.get_repositories_view(name)
self.show_repo(repos, name)
return True
class RepositoryMajorCommand(MajorCommand):
def __init__(self):
super(RepositoryMajorCommand, self).__init__('repo', 'Manage local repository for OBD.')
self.register_command(RepositoryListCommand())
class ClusterMirrorCommand(ObdCommand):
def init(self, cmd, args):
super(ClusterMirrorCommand, self).init(cmd, args)
self.parser.set_usage('%s <deploy name> [options]' % self.prev_cmd)
return self
def get_obd_namespaces_data(self, obd):
data = {}
for component, _ in obd.namespaces.items():
data[component] = _.get_variable('run_result')
return data
def background_telemetry_task(self, obd, demploy_name=None):
if demploy_name is None:
demploy_name = self.cmds[0]
data = json.dumps(self.get_obd_namespaces_data(obd))
LocalClient.execute_command_background("nohup obd telemetry post %s --data='%s' >/dev/null 2>&1 &" % (demploy_name, data))
class ClusterConfigStyleChange(ClusterMirrorCommand):
def __init__(self):
super(ClusterConfigStyleChange, self).__init__('chst', 'Change Deployment Configuration Style')
self.parser.add_option('-c', '--components', type='string', help="List the components. Multiple components are separated with commas.")
self.parser.add_option('--style', type='string', help="Preferred Style")
def _do_command(self, obd):
if self.cmds:
return obd.change_deploy_config_style(self.cmds[0])
else:
return self._show_help()
class ClusterCheckForOCPChange(ClusterMirrorCommand):
def __init__(self):
super(ClusterCheckForOCPChange, self).__init__('check4ocp', 'Check Whether OCP Can Take Over Configurations in Use')
self.parser.add_option('-c', '--components', type='string', help="List the components. Multiple components are separated with commas.")
self.parser.add_option('-V', '--version', type='string', help="OCP Version", default='3.1.1')
def _do_command(self, obd):
if self.cmds:
return obd.check_for_ocp(self.cmds[0])
else:
return self._show_help()
class ClusterExportToOCPCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterExportToOCPCommand, self).__init__('export-to-ocp', 'Export obcluster to OCP')
self.parser.add_option('-a', '--address', type='string', help="OCP address, example http://127.0.0.1:8080, you can find it in OCP system parameters with Key='ocp.site.url'")
self.parser.add_option('-u', '--user', type='string', help="OCP user, this user should have create cluster privilege.")
self.parser.add_option('-p', '--password', type='string', help="OCP user password.")
self.parser.add_option('--host_type', type='string', help="Host type of observer, a host type will be created when there's no host type exists in ocp, the first host type will be used if this parameter is empty.", default="")
self.parser.add_option('--credential_name', type='string', help="Credential used to connect hosts, a credential will be created if credential_name is empty or no credential with this name exists in ocp.", default="")
def _do_command(self, obd):
if self.cmds:
return obd.export_to_ocp(self.cmds[0])
else:
return self._show_help()
class ClusterTakeoverCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterTakeoverCommand, self).__init__('takeover', 'Takeover oceanbase cluster')
self.parser.remove_option('-h')
self.parser.add_option('--help', action='callback', callback=self._show_help, help='Show help and exit.')
self.parser.add_option('-h', '--host', type='string', help="db connection host, default: 127.0.0.1", default='127.0.0.1')
self.parser.add_option('-P', '--mysql-port', type='int', help="mysql port, default: 2881", default=2881)
self.parser.add_option('-p', '--root-password', type='string', help="password of root@sys user, default: ''", default='')
self.parser.add_option('--ssh-user', type='string', help="ssh user, default: current user")
self.parser.add_option('--ssh-password', type='string', help="ssh password, default: ''", default='')
self.parser.add_option('--ssh-port', type='int', help="ssh port, default: 22")
self.parser.add_option('-t', '--ssh-timeout', type='int', help="ssh connection timeout (second), default: 30")
self.parser.add_option('--ssh-key-file', type='string', help="ssh key file")
def _do_command(self, obd):
if self.cmds:
return obd.takeover(self.cmds[0])
else:
return self._show_help()
class DemoCommand(ClusterMirrorCommand):
def __init__(self):
super(DemoCommand, self).__init__('demo', 'Quickly start')
self.parser.add_option('-c', '--components', type='string', help="List the components. Multiple components are separated with commas. [oceanbase-ce,obproxy-ce,obagent,prometheus,grafana,ob-configserver]\nExample: \nstart oceanbase-ce: obd demo -c oceanbase-ce\n"
+ "start -c oceanbase-ce V3.2.3: obd demo -c oceanbase-ce --oceanbase-ce.version=3.2.3\n"
+ "start oceanbase-ce and obproxy-ce: obd demo -c oceanbase-ce,obproxy-ce", default='oceanbase-ce,obproxy-ce,obagent,prometheus,grafana')
self.parser.allow_undefine = True
self.parser.undefine_warn = False
def _do_command(self, obd):
setattr(self.opts, 'force', True)
setattr(self.opts, 'clean', True)
setattr(self.opts, 'force', True)
setattr(self.opts, 'force_delete', True)
obd.set_options(self.opts)
res = obd.demo()
self.background_telemetry_task(obd, 'demo')
return res
class WebCommand(ObdCommand):
def __init__(self):
super(WebCommand, self).__init__('web', 'Start obd deploy application as web.')
self.parser.add_option('-p', '--port', type='int', help="web server listen port", default=8680)
self.parser.add_option('-w', '--white', type='str', help="ip white list, eq: '127.0.0.1, 192.168.1.1'.", default='')
def _do_command(self, obd):
from service.app import OBDWeb
# white_ip_list = self.get_white_ip_list()
url = '/#/updateWelcome' if self.cmds and self.cmds[0] in ('upgrade', 'update') else ''
ROOT_IO.print('start OBD WEB in 0.0.0.0:%s' % self.opts.port)
ROOT_IO.print('please open http://{0}:{1}{2}'.format(NetUtil.get_host_ip(), self.opts.port, url))
try:
COMMAND_ENV.set(ENV.ENV_DISABLE_PARALLER_EXTRACT, True, stdio=obd.stdio)
OBDWeb(obd, None, self.OBD_INSTALL_PATH).start(self.opts.port)
except KeyboardInterrupt:
ROOT_IO.print('Keyboard Interrupt')
except BaseException as e:
ROOT_IO.exception('Runtime Error %s' % e)
finally:
ROOT_IO.print('stop OBD WEB')
return True
class ClusterAutoDeployCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterAutoDeployCommand, self).__init__('autodeploy', 'Deploy a cluster automatically by using a simple configuration file.')
self.parser.add_option('-c', '--config', type='string', help="Path to the configuration file.")
self.parser.add_option('-f', '--force', action='store_true', help="Force autodeploy, overwrite the home_path.")
self.parser.add_option('-C', '--clean', action='store_true', help="Clean the home_path if the directory belong to you.", default=False)
self.parser.add_option('--generate-consistent-config', '--gcc', action='store_true', help="Generate consistent config")
self.parser.add_option('-U', '--unuselibrepo', '--ulp', action='store_true', help="Disable OBD from installing the libs mirror automatically.")
self.parser.add_option('-A', '--auto-create-tenant', '--act', action='store_true', help="Automatically create a tenant named `test` by using all the available resource of the cluster.")
self.parser.add_option('--force-delete', action='store_true', help="Force delete, delete the registered cluster.")
self.parser.add_option('-s', '--strict-check', action='store_true', help="Throw errors instead of warnings when check fails.")
def _do_command(self, obd):
if self.cmds:
if getattr(self.opts, 'force', False) or getattr(self.opts, 'clean', False):
setattr(self.opts, 'skip_cluster_status_check', True)
obd.set_options(self.opts)
name = self.cmds[0]
if obd.genconfig(name):
self.opts.config = ''
obd.set_cmds(self.cmds[1:])
res = obd.deploy_cluster(name) and obd.start_cluster(name)
self.background_telemetry_task(obd)
return res
return False
else:
return self._show_help()
class ClusterDeployCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterDeployCommand, self).__init__('deploy', 'Deploy a cluster by using the current deploy configuration or a deploy yaml file.')
self.parser.add_option('-c', '--config', type='string', help="Path to the configuration yaml file.")
self.parser.add_option('-f', '--force', action='store_true', help="Force deploy, overwrite the home_path.", default=False)
self.parser.add_option('-C', '--clean', action='store_true', help="Clean the home path if the directory belong to you.", default=False)
self.parser.add_option('-U', '--unuselibrepo', '--ulp', action='store_true', help="Disable OBD from installing the libs mirror automatically.")
self.parser.add_option('-A', '--auto-create-tenant', '--act', action='store_true', help="Automatically create a tenant named `test` by using all the available resource of the cluster.")
# self.parser.add_option('-F', '--fuzzymatch', action='store_true', help="enable fuzzy match when search package")
def _do_command(self, obd):
if self.cmds:
if getattr(self.opts, 'force', False) or getattr(self.opts, 'clean', False):
setattr(self.opts, 'skip_cluster_status_check', True)
obd.set_options(self.opts)
res = obd.deploy_cluster(self.cmds[0])
self.background_telemetry_task(obd)
if res:
obd.stdio.print(FormatText.success('Please execute ` obd cluster start %s ` to start' % self.cmds[0]))
return res
else:
return self._show_help()
class ClusterScaleoutCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterScaleoutCommand, self).__init__('scale_out', 'Scale out cluster with an additional deploy yaml file.')
self.parser.add_option('-c', '--config', type='string', help="Path to the configuration yaml file.")
self.parser.add_option('-f', '--force', action='store_true', help="Force deploy, overwrite the home_path.", default=False)
self.parser.add_option('-C', '--clean', action='store_true', help="Clean the home path if the directory belong to you.", default=False)
self.parser.add_option('-t', '--scale_out_timeout', type='int', help="Scale out timeout in seconds.", default=3600)
def _do_command(self, obd):
if self.cmds:
return obd.scale_out(self.cmds[0])
else:
return self._show_help()
class ClusterComponentAddCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterComponentAddCommand, self).__init__('add', 'Add components for cluster')
self.parser.add_option('-c', '--config', type='string', help="Path to the configuration yaml file.")
self.parser.add_option('-f', '--force', action='store_true', help="Force deploy, overwrite the home_path.", default=False)
self.parser.add_option('-C', '--clean', action='store_true', help="Clean the home path if the directory belong to you.", default=False)
def _do_command(self, obd):
if self.cmds:
return obd.add_components(self.cmds[0])
else:
return self._show_help()
class ClusterComponentDeleteCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterComponentDeleteCommand, self).__init__('del', 'Add components for cluster')
self.parser.add_option('-f', '--force', action='store_true', help="Force delete components.", default=False)
self.parser.add_option('--ignore-standby', '--igs', action='store_true', help="Force kill the observer while standby tenant in others cluster exists.")
def init(self, cmd, args):
super(ClusterComponentDeleteCommand, self).init(cmd, args)
self.parser.set_usage('%s <deploy name> <component> ... [component]' % self.prev_cmd)
return self
def _do_command(self, obd):
if self.cmds and len(self.cmds) >= 2:
return obd.delete_components(self.cmds[0], self.cmds[1:])
else:
return self._show_help()
class ClusterComponentMajorCommand(MajorCommand):
def __init__(self):
super(ClusterComponentMajorCommand, self).__init__('component', 'Add or delete component for cluster')
self.register_command(ClusterComponentAddCommand())
self.register_command(ClusterComponentDeleteCommand())
class ClusterStartCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterStartCommand, self).__init__('start', 'Start a deployed cluster.')
self.parser.add_option('-s', '--servers', type='string', help="List of servers to be started. Multiple servers are separated with commas.")
self.parser.add_option('-c', '--components', type='string', help="List of components to be started. Multiple components are separated with commas.")
self.parser.add_option('-f', '--force-delete', action='store_true', help="Force delete, delete the registered cluster.")
self.parser.add_option('-S', '--strict-check', action='store_true', help="Throw errors instead of warnings when check fails.")
self.parser.add_option('--without-parameter', '--wop', action='store_true', help='Start without parameters.')
def _do_command(self, obd):
if self.cmds:
obd.set_cmds(self.cmds[1:])
res = obd.start_cluster(self.cmds[0])
self.background_telemetry_task(obd)
return res
else:
return self._show_help()
class ClusterStopCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterStopCommand, self).__init__('stop', 'Stop a started cluster.')
self.parser.add_option('-s', '--servers', type='string', help="List of servers to be stoped. Multiple servers are separated with commas.")
self.parser.add_option('-c', '--components', type='string', help="List of components to be stoped. Multiple components are separated with commas.")
def _do_command(self, obd):
if self.cmds:
res = obd.stop_cluster(self.cmds[0])
self.background_telemetry_task(obd)
return res
else:
return self._show_help()
class ClusterDestroyCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterDestroyCommand, self).__init__('destroy', 'Destroy a deployed cluster.')
self.parser.add_option('-f', '--force-kill', action='store_true', help="Force kill the running observer process in the working directory.")
self.parser.add_option('--confirm', action='store_true', help='Confirm to destroy.')
self.parser.add_option('--ignore-standby', '--igs', action='store_true', help="Force kill the observer while standby tenant in others cluster exists.")
def _do_command(self, obd):
if self.cmds:
res = obd.destroy_cluster(self.cmds[0], need_confirm=not getattr(self.opts, 'confirm', False))
return res
else:
return self._show_help()
class ClusterDisplayCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterDisplayCommand, self).__init__('display', 'Display the information for a cluster.')
def _do_command(self, obd):
if self.cmds:
return obd.display_cluster(self.cmds[0])
else:
return self._show_help()
class ClusterRestartCommand(ClusterMirrorCommand):
def __init__(self):
super(ClusterRestartCommand, self).__init__('restart', 'Restart a started cluster.')
self.parser.add_option('-s', '--servers', type='string', help="List of servers to be restarted. Multiple servers are separated with commas.")
self.parser.add_option('-c', '--components', type='string', help="List of components to be restarted. Multiple components are separated with commas.")
self.parser.add_option('--with-parameter', '--wp', action='store_true', help='Restart with parameters.')
def _do_command(self, obd):