kaqing 2.0.172__py3-none-any.whl → 2.0.186__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kaqing might be problematic. Click here for more details.

Files changed (140) hide show
  1. adam/app_session.py +2 -2
  2. adam/apps.py +18 -4
  3. adam/batch.py +1 -1
  4. adam/checks/check_utils.py +3 -1
  5. adam/commands/__init__.py +8 -2
  6. adam/commands/alter_tables.py +24 -35
  7. adam/commands/app/__init__.py +0 -0
  8. adam/commands/app/app.py +38 -0
  9. adam/commands/app/app_ping.py +38 -0
  10. adam/commands/app/show_app_actions.py +49 -0
  11. adam/commands/app/show_app_id.py +44 -0
  12. adam/commands/app/show_app_queues.py +38 -0
  13. adam/commands/app/utils_app.py +106 -0
  14. adam/commands/audit/audit.py +9 -27
  15. adam/commands/audit/audit_repair_tables.py +5 -7
  16. adam/commands/audit/audit_run.py +1 -1
  17. adam/commands/audit/completions_l.py +15 -0
  18. adam/commands/audit/show_last10.py +2 -14
  19. adam/commands/audit/show_slow10.py +2 -13
  20. adam/commands/audit/show_top10.py +2 -11
  21. adam/commands/audit/utils_show_top10.py +14 -1
  22. adam/commands/bash/bash.py +1 -1
  23. adam/commands/cat.py +5 -19
  24. adam/commands/cd.py +6 -8
  25. adam/commands/check.py +10 -18
  26. adam/commands/cli_commands.py +6 -1
  27. adam/commands/{cp.py → clipboard_copy.py} +34 -36
  28. adam/commands/code.py +2 -2
  29. adam/commands/command.py +94 -10
  30. adam/commands/commands_utils.py +19 -12
  31. adam/commands/cql/completions_c.py +28 -0
  32. adam/commands/cql/cqlsh.py +3 -7
  33. adam/commands/cql/utils_cql.py +22 -60
  34. adam/commands/deploy/deploy_pg_agent.py +2 -2
  35. adam/commands/deploy/undeploy_pg_agent.py +2 -2
  36. adam/commands/devices/device.py +39 -8
  37. adam/commands/devices/device_app.py +19 -29
  38. adam/commands/devices/device_auit_log.py +3 -3
  39. adam/commands/devices/device_cass.py +17 -23
  40. adam/commands/devices/device_export.py +12 -11
  41. adam/commands/devices/device_postgres.py +79 -63
  42. adam/commands/download_file.py +47 -0
  43. adam/commands/export/clean_up_all_export_sessions.py +3 -3
  44. adam/commands/export/clean_up_export_sessions.py +7 -19
  45. adam/commands/export/completions_x.py +11 -0
  46. adam/commands/export/download_export_session.py +40 -0
  47. adam/commands/export/drop_export_database.py +6 -22
  48. adam/commands/export/drop_export_databases.py +3 -9
  49. adam/commands/export/export.py +1 -17
  50. adam/commands/export/export_databases.py +93 -21
  51. adam/commands/export/export_select.py +8 -68
  52. adam/commands/export/export_sessions.py +209 -0
  53. adam/commands/export/export_use.py +13 -16
  54. adam/commands/export/export_x_select.py +48 -0
  55. adam/commands/export/exporter.py +108 -129
  56. adam/commands/export/import_files.py +44 -0
  57. adam/commands/export/import_session.py +10 -6
  58. adam/commands/export/importer.py +19 -5
  59. adam/commands/export/importer_athena.py +112 -41
  60. adam/commands/export/importer_sqlite.py +47 -19
  61. adam/commands/export/show_column_counts.py +11 -20
  62. adam/commands/export/show_export_databases.py +5 -2
  63. adam/commands/export/show_export_session.py +6 -15
  64. adam/commands/export/show_export_sessions.py +4 -11
  65. adam/commands/export/utils_export.py +46 -16
  66. adam/commands/find_files.py +51 -0
  67. adam/commands/find_processes.py +76 -0
  68. adam/commands/head.py +36 -0
  69. adam/commands/help.py +2 -2
  70. adam/commands/intermediate_command.py +6 -3
  71. adam/commands/ls.py +1 -1
  72. adam/commands/medusa/medusa_backup.py +13 -16
  73. adam/commands/medusa/medusa_restore.py +39 -32
  74. adam/commands/medusa/medusa_show_backupjobs.py +6 -4
  75. adam/commands/medusa/medusa_show_restorejobs.py +5 -3
  76. adam/commands/medusa/utils_medusa.py +15 -0
  77. adam/commands/nodetool.py +3 -8
  78. adam/commands/param_get.py +10 -12
  79. adam/commands/param_set.py +7 -10
  80. adam/commands/postgres/completions_p.py +22 -0
  81. adam/commands/postgres/postgres.py +25 -40
  82. adam/commands/postgres/postgres_databases.py +270 -0
  83. adam/commands/postgres/utils_postgres.py +33 -20
  84. adam/commands/preview_table.py +4 -2
  85. adam/commands/pwd.py +3 -3
  86. adam/commands/reaper/reaper_forward.py +2 -2
  87. adam/commands/reaper/reaper_run_abort.py +4 -10
  88. adam/commands/reaper/reaper_runs.py +3 -3
  89. adam/commands/reaper/reaper_schedule_activate.py +12 -12
  90. adam/commands/reaper/reaper_schedule_start.py +7 -12
  91. adam/commands/reaper/reaper_schedule_stop.py +7 -12
  92. adam/commands/reaper/utils_reaper.py +13 -6
  93. adam/commands/repair/repair_scan.py +0 -2
  94. adam/commands/repair/repair_stop.py +0 -1
  95. adam/commands/shell.py +7 -5
  96. adam/commands/show/show.py +1 -1
  97. adam/commands/show/show_adam.py +3 -3
  98. adam/commands/show/show_cassandra_repairs.py +5 -3
  99. adam/commands/show/show_cassandra_status.py +27 -20
  100. adam/commands/show/{show_commands.py → show_cli_commands.py} +2 -2
  101. adam/commands/show/show_login.py +2 -2
  102. adam/commands/show/show_params.py +2 -5
  103. adam/commands/show/show_processes.py +15 -14
  104. adam/commands/show/show_storage.py +9 -8
  105. adam/config.py +1 -0
  106. adam/embedded_params.py +1 -1
  107. adam/repl.py +20 -11
  108. adam/repl_commands.py +16 -9
  109. adam/repl_session.py +8 -1
  110. adam/repl_state.py +33 -10
  111. adam/sql/lark_completer.py +280 -0
  112. adam/sql/lark_parser.py +604 -0
  113. adam/sql/sql_state_machine.py +8 -2
  114. adam/utils.py +116 -29
  115. adam/utils_athena.py +7 -8
  116. adam/utils_issues.py +2 -2
  117. adam/utils_k8s/app_clusters.py +2 -2
  118. adam/utils_k8s/app_pods.py +5 -2
  119. adam/utils_k8s/cassandra_clusters.py +11 -3
  120. adam/utils_k8s/cassandra_nodes.py +2 -2
  121. adam/utils_k8s/k8s.py +14 -5
  122. adam/utils_k8s/kube_context.py +2 -2
  123. adam/utils_k8s/pods.py +23 -5
  124. adam/utils_k8s/statefulsets.py +5 -2
  125. adam/utils_local.py +4 -0
  126. adam/utils_repl/appendable_completer.py +6 -0
  127. adam/utils_repl/repl_completer.py +128 -2
  128. adam/utils_sqlite.py +14 -14
  129. adam/version.py +1 -1
  130. {kaqing-2.0.172.dist-info → kaqing-2.0.186.dist-info}/METADATA +1 -1
  131. kaqing-2.0.186.dist-info/RECORD +250 -0
  132. adam/commands/cql/cql_completions.py +0 -33
  133. adam/commands/export/export_handlers.py +0 -71
  134. adam/commands/export/export_select_x.py +0 -54
  135. adam/commands/postgres/postgres_context.py +0 -272
  136. adam/commands/postgres/psql_completions.py +0 -10
  137. kaqing-2.0.172.dist-info/RECORD +0 -230
  138. {kaqing-2.0.172.dist-info → kaqing-2.0.186.dist-info}/WHEEL +0 -0
  139. {kaqing-2.0.172.dist-info → kaqing-2.0.186.dist-info}/entry_points.txt +0 -0
  140. {kaqing-2.0.172.dist-info → kaqing-2.0.186.dist-info}/top_level.txt +0 -0
@@ -1,9 +1,6 @@
1
- from adam.commands.audit.utils_show_top10 import extract_limit_and_duration
1
+ from adam.commands.audit.utils_show_top10 import run_configured_query
2
2
  from adam.commands.command import Command
3
3
  from adam.repl_state import ReplState
4
- from adam.utils import log2
5
- from adam.utils_athena import Athena
6
- from adam.utils_audits import Audits
7
4
 
8
5
  class ShowSlow10(Command):
9
6
  COMMAND = 'show slow'
@@ -28,15 +25,7 @@ class ShowSlow10(Command):
28
25
  return super().run(cmd, state)
29
26
 
30
27
  with self.validate(args, state) as (args, state):
31
- limit, date_condition = extract_limit_and_duration(args)
32
-
33
- query = '\n '.join([
34
- "SELECT * FROM audit",
35
- f"WHERE drive <> 'z' and ({date_condition})",
36
- f"ORDER BY CAST(duration AS REAL) DESC LIMIT {limit};"])
37
- log2(query)
38
- log2()
39
- Athena.run_query(query)
28
+ run_configured_query('audit.queries.slow10', args)
40
29
 
41
30
  return state
42
31
 
@@ -1,8 +1,6 @@
1
- from adam.commands.audit.utils_show_top10 import extract_limit_and_duration
1
+ from adam.commands.audit.utils_show_top10 import run_configured_query
2
2
  from adam.commands.command import Command
3
3
  from adam.repl_state import ReplState
4
- from adam.utils import log2
5
- from adam.utils_athena import Athena
6
4
 
7
5
  class ShowTop10(Command):
8
6
  COMMAND = 'show top'
@@ -27,14 +25,7 @@ class ShowTop10(Command):
27
25
  return super().run(cmd, state)
28
26
 
29
27
  with self.validate(args, state) as (args, state):
30
- limit, date_condition = extract_limit_and_duration(args)
31
- query = '\n '.join([
32
- "SELECT min(c) AS cluster, line, COUNT(*) AS cnt, avg(CAST(duration AS REAL)) AS duration",
33
- f"FROM audit WHERE drive <> 'z' and ({date_condition})",
34
- f"GROUP BY line ORDER BY cnt DESC LIMIT {limit};"])
35
- log2(query)
36
- log2()
37
- Athena.run_query(query)
28
+ run_configured_query('audit.queries.top10', args)
38
29
 
39
30
  return state
40
31
 
@@ -1,10 +1,23 @@
1
1
  from datetime import datetime, timedelta
2
2
 
3
- from adam.utils import log_exc
3
+ from adam.config import Config
4
+ from adam.utils import log2, log_exc
5
+ from adam.utils_athena import Athena
4
6
  from adam.utils_audits import Audits
5
7
  from adam.utils_repl.automata_completer import AutomataCompleter
6
8
  from adam.utils_repl.state_machine import StateMachine
7
9
 
10
+ def run_configured_query(config_key: str, args: list[str]):
11
+ limit, date_condition = extract_limit_and_duration(args)
12
+ if query := Config().get(config_key, None):
13
+ query = '\n '.join(query.split('\n'))
14
+ query = query.replace('{date_condition}', date_condition)
15
+ query = query.replace('{limit}', str(limit))
16
+
17
+ log2(query)
18
+ log2()
19
+ Athena.run_query(query)
20
+
8
21
  def extract_limit_and_duration(args: list[str]) -> tuple[int, datetime]:
9
22
  limit = 10
10
23
  _from = datetime.now() - timedelta(days=30)
@@ -30,7 +30,7 @@ class Bash(Command):
30
30
  return exec(args)
31
31
 
32
32
  def completion(self, state: ReplState):
33
- return Devices.device(state).bash_completion(Bash.COMMAND, state, default = {})
33
+ return super().completion(state, {c : {'&': None} for c in ['ls', 'cat', 'head']}, pods=Devices.device(state).pods(state, '-'))
34
34
 
35
35
  def help(self, _: ReplState):
36
36
  return f'{Bash.COMMAND} [pod-name] [bash-commands] [&]\t run bash on the Cassandra nodes'
adam/commands/cat.py CHANGED
@@ -1,9 +1,7 @@
1
+ from adam.commands import validate_args
1
2
  from adam.commands.command import Command
2
3
  from adam.commands.devices.devices import Devices
3
4
  from adam.repl_state import ReplState, RequiredState
4
- from adam.utils import log2
5
- from adam.utils_k8s.app_pods import AppPods
6
- from adam.utils_k8s.statefulsets import StatefulSets
7
5
 
8
6
  class Cat(Command):
9
7
  COMMAND = 'cat'
@@ -28,23 +26,11 @@ class Cat(Command):
28
26
  return super().run(cmd, state)
29
27
 
30
28
  with self.validate(args, state) as (args, state):
31
- if len(args) < 1:
32
- if state.in_repl:
33
- log2('File is required.')
34
- log2()
35
- else:
36
- log2('* File is missing.')
37
- Command.display_help()
38
-
39
- return 'command-missing'
40
-
41
- return Devices.device(state).cat(cmd, state)
29
+ with validate_args(args, state, name='file'):
30
+ return Devices.device(state).bash(state, state, cmd.split(' '))
42
31
 
43
32
  def completion(self, state: ReplState):
44
- if super().completion(state):
45
- return Devices.device(state).cat_completion(Cat.COMMAND, state, default={})
46
-
47
- return {}
33
+ return super().completion(state, lambda: {f: None for f in Devices.device(state).files(state)}, pods=Devices.device(state).pods(state, '-'), auto='jit')
48
34
 
49
35
  def help(self, _: ReplState):
50
- return f'{Cat.COMMAND} file [&]\t run cat command on the Cassandra nodes'
36
+ return f'{Cat.COMMAND} file [&]\t run cat command on the pod'
adam/commands/cd.py CHANGED
@@ -1,3 +1,4 @@
1
+ from adam.commands import validate_args
1
2
  from adam.commands.command import Command
2
3
  from adam.commands.devices.device import Device
3
4
  from adam.commands.devices.devices import Devices
@@ -26,15 +27,12 @@ class Cd(Command):
26
27
  return super().run(cmd, state)
27
28
 
28
29
  with self.validate(args, state, apply=False) as (args, state):
29
- if len(args) < 2:
30
- return state
31
-
32
- device: Device = Devices.device(state)
33
- arg = args[1]
34
- for dir in arg.split('/'):
35
- device.cd(dir, state)
30
+ with validate_args(args, state, name='directory') as arg_str:
31
+ device: Device = Devices.device(state)
32
+ for dir in arg_str.split('/'):
33
+ device.cd(dir, state)
36
34
 
37
- return state
35
+ return state
38
36
 
39
37
  def completion(self, state: ReplState):
40
38
  return Devices.device(state).cd_completion(Cd.COMMAND, state, default = {})
adam/commands/check.py CHANGED
@@ -2,12 +2,12 @@ import click
2
2
 
3
3
  from adam.checks.check_result import CheckResult
4
4
  from adam.checks.check_utils import all_checks, checks_from_csv, run_checks
5
- from adam.commands import extract_options
5
+ from adam.commands import extract_options, validate_args
6
6
  from adam.commands.command import Command
7
7
  from adam.commands.command_helpers import ClusterOrPodCommandHelper
8
8
  from adam.commands.issues import Issues
9
9
  from adam.repl_state import ReplState
10
- from adam.utils import lines_to_tabular, log
10
+ from adam.utils import tabulize, log
11
11
  from adam.utils_issues import IssuesUtils
12
12
 
13
13
  class Check(Issues):
@@ -34,25 +34,17 @@ class Check(Issues):
34
34
 
35
35
  with self.validate(args, state) as (args, state):
36
36
  with extract_options(args, ['-s', '--show']) as (args, show_out):
37
- if not args:
38
- if state.in_repl:
39
- log(lines_to_tabular([check.help() for check in all_checks()], separator=':'))
40
- else:
41
- log('* Check name is missing.')
42
- Command.display_help()
37
+ with validate_args(args, state, name='check name', msg=lambda: tabulize([check.help() for check in all_checks()], separator=':')) as arg:
38
+ checks = checks_from_csv(args[0])
39
+ if not checks:
40
+ return 'invalid check name'
43
41
 
44
- return 'arg missing'
42
+ results = run_checks(state.sts, state.namespace, state.pod, checks=checks, show_out=show_out)
45
43
 
46
- checks = checks_from_csv(args[0])
47
- if not checks:
48
- return 'invalid check name'
44
+ issues = CheckResult.collect_issues(results)
45
+ IssuesUtils.show_issues(issues, in_repl=state.in_repl)
49
46
 
50
- results = run_checks(state.sts, state.namespace, state.pod, checks=checks, show_out=show_out)
51
-
52
- issues = CheckResult.collect_issues(results)
53
- IssuesUtils.show_issues(issues, in_repl=state.in_repl)
54
-
55
- return issues if issues else 'no issues found'
47
+ return issues if issues else 'no issues found'
56
48
 
57
49
  def completion(self, _: ReplState):
58
50
  return {Check.COMMAND: {check.name(): {'-s': None} for check in all_checks()}}
@@ -3,14 +3,19 @@ import re
3
3
 
4
4
  from adam.commands.reaper.utils_reaper import Reapers
5
5
  from adam.config import Config
6
+ from adam.utils import log_timing
6
7
  from adam.utils_k8s.kube_context import KubeContext
7
8
  from adam.utils_k8s.secrets import Secrets
8
9
  from adam.utils_k8s.statefulsets import StatefulSets
9
10
  from adam.repl_state import ReplState
10
11
 
11
12
  class CliCommands:
12
- @functools.lru_cache()
13
13
  def values(state: ReplState, collapse = False):
14
+ with log_timing('CliCommands.values'):
15
+ return CliCommands._values(state, collapse)
16
+
17
+ @functools.lru_cache()
18
+ def _values(state: ReplState, collapse = False):
14
19
  # node-exec-?, nodetool-?, cql-?, reaper-exec, reaper-forward, reaper-ui, reaper-usernae, reaper-password
15
20
  d = {}
16
21
 
@@ -1,14 +1,17 @@
1
+ from functools import partial
1
2
  import click
2
3
  import pyperclip
3
4
 
4
- from adam.commands.command import Command
5
+ from adam.commands import validate_args
6
+ from adam.commands.command import Command, InvalidArgumentsException
5
7
  from adam.commands.command_helpers import ClusterOrPodCommandHelper
6
8
  from adam.commands.cli_commands import CliCommands
9
+ from adam.config import Config
7
10
  from adam.repl_state import ReplState, RequiredState
8
- from adam.utils import lines_to_tabular, log, log2
11
+ from adam.utils import tabulize, log, log2
9
12
 
10
13
  class ClipboardCopy(Command):
11
- COMMAND = 'cp'
14
+ COMMAND = 'cli cp'
12
15
 
13
16
  # the singleton pattern
14
17
  def __new__(cls, *args, **kwargs):
@@ -30,40 +33,37 @@ class ClipboardCopy(Command):
30
33
  return super().run(cmd, state)
31
34
 
32
35
  with self.validate(args, state) as (args, state):
33
- if len(args) < 1:
34
- if state.in_repl:
36
+ def display_keys(missing = False):
37
+ if missing:
35
38
  log2('Key is required.')
36
- log2()
37
- log2('Keys:')
38
- log2(lines_to_tabular([f'{k},{v}' for k, v in CliCommands.values(state, collapse=True).items()], separator=','))
39
39
  else:
40
- log2('* Key is missing.')
41
- Command.display_help()
42
-
43
- return 'command-missing'
44
-
45
- key = args[0]
46
- if not key in CliCommands.values(state):
47
- if state.in_repl:
48
- log2('Key is required.')
49
- log2()
50
- log2('Keys:')
51
- log2(lines_to_tabular([f'{k},{v}' for k, v in CliCommands.values(state, collapse=True).items()], separator=','))
52
- else:
53
- log2('* Invalid key')
54
- Command.display_help()
55
-
56
- return 'command-invalid'
57
-
58
- value = CliCommands.values(state)[key]
59
- pyperclip.copy(value)
60
- log2('The following line has been copied to clipboard. Use <Ctrl-V> to use it.')
61
- log2(f' {value}')
62
-
63
- return 'value-copied'
40
+ log2('Key is invalid.')
41
+ log2()
42
+ tabulize(CliCommands.values(state, collapse=True).items(),
43
+ lambda a: f'{a[0]},{a[1]}',
44
+ header='KEY,VALUE',
45
+ separator=',',
46
+ to=2)
47
+
48
+ with validate_args(args, state, name='key', msg=partial(display_keys, True)) as key:
49
+ if not key in CliCommands.values(state):
50
+ if state.in_repl:
51
+ display_keys(False)
52
+ else:
53
+ log2('* Invalid key')
54
+ Command.display_help()
55
+
56
+ raise InvalidArgumentsException()
57
+
58
+ value = CliCommands.values(state)[key]
59
+ pyperclip.copy(value)
60
+ log2('The following line has been copied to clipboard. Use <Ctrl-V> to use it.')
61
+ log2(f' {value}')
62
+
63
+ return state
64
64
 
65
65
  def completion(self, state: ReplState):
66
- return super().completion(state, lambda: {key: None for key in CliCommands.values(state).keys()})
66
+ return super().completion(state, lambda: {key: None for key in CliCommands.values(state).keys()}, auto_key='cli.cp-auto-complete')
67
67
 
68
68
  def help(self, _: ReplState):
69
69
  return f"{ClipboardCopy.COMMAND} <key>\t copy a value to clipboard for conveninence"
@@ -82,8 +82,6 @@ class CopyCommandHelper(click.Command):
82
82
  def get_help(self, ctx: click.Context):
83
83
  log(super().get_help(ctx))
84
84
  log()
85
- log('Keys:')
86
-
87
- log(lines_to_tabular(self.lines(), separator=':'))
85
+ tabulize(self.lines(), header='KEY:DESC', separator=':')
88
86
  log()
89
87
  ClusterOrPodCommandHelper.cluter_or_pod_help()
adam/commands/code.py CHANGED
@@ -4,7 +4,7 @@ from functools import partial
4
4
  from adam.commands.command import Command
5
5
  from adam.commands.cql.utils_cql import cassandra_table_names, run_cql
6
6
  from adam.repl_state import ReplState, RequiredState
7
- from adam.utils import lines_to_tabular
7
+ from adam.utils import tabulize
8
8
  from adam.utils_k8s.statefulsets import StatefulSets
9
9
 
10
10
  class Code(Command):
@@ -46,7 +46,7 @@ class Code(Command):
46
46
  "cql('select...'): run cql statement"
47
47
  ]
48
48
 
49
- code.interact(local=my_local, banner=lines_to_tabular(lines, header='Variables', separator=':'))
49
+ code.interact(local=my_local, banner=tabulize(lines, header='Variables', separator=':'))
50
50
 
51
51
  return state
52
52
 
adam/commands/command.py CHANGED
@@ -1,11 +1,14 @@
1
1
  from abc import abstractmethod
2
+ from collections.abc import Callable
2
3
  import copy
3
4
  import subprocess
4
5
  import sys
5
6
  from typing import Union
6
7
 
8
+ from adam.config import Config
7
9
  from adam.repl_state import ReplState, RequiredState
8
- from adam.utils import is_lambda
10
+ from adam.sql.lark_completer import LarkCompleter
11
+ from adam.utils import log2
9
12
 
10
13
  repl_cmds: list['Command'] = []
11
14
 
@@ -28,12 +31,38 @@ class Command:
28
31
 
29
32
  return None
30
33
 
31
- def completion(self, state: ReplState, leaf: dict[str, any] = None) -> dict[str, any]:
32
- if not self.validate_state(state, show_err=False):
34
+ def completion(self, state: ReplState, leaf: dict[str, any] = None, pods: tuple[list[str], str] = None, auto: str = 'on', auto_key: str = None) -> dict[str, any]:
35
+ # pods is a tuple of list of pod names and the current pod repl is on
36
+ if not pods:
37
+ return self._completion(state, leaf, auto=auto, auto_key=auto_key)
38
+
39
+ c = {}
40
+
41
+ pod_names = pods[0]
42
+ pod = pods[1]
43
+
44
+ if pod:
45
+ c |= self._completion(state, leaf, auto=auto, auto_key=auto_key)
46
+
47
+ c |= {f'@{p}': self._completion(state, leaf, to_validate=False, auto=auto, auto_key=auto_key) for p in pod_names if p != pod}
48
+
49
+ return c
50
+
51
+ def _completion(self, state: ReplState, leaf: dict[str, any] = None, to_validate = True, auto: str = 'on', auto_key: str = None) -> dict[str, any]:
52
+ if to_validate and not self.validate_state(state, show_err=False):
33
53
  return {}
34
54
 
35
- if is_lambda(leaf):
36
- leaf = leaf()
55
+ if callable(leaf):
56
+ if auto_key:
57
+ auto_key = f'auto-complete.{auto_key}'
58
+ auto = Config().get(auto_key, 'off')
59
+
60
+ if auto == 'on':
61
+ leaf = leaf()
62
+ elif auto in ['lazy', 'jit']:
63
+ leaf = LarkCompleter.from_lambda(auto_key, leaf, auto=auto)
64
+ else:
65
+ leaf = None
37
66
 
38
67
  d = leaf
39
68
  for t in reversed(self.command().split(' ')):
@@ -45,7 +74,7 @@ class Command:
45
74
  return None
46
75
 
47
76
  def validate(self, args: list[str], state: ReplState, apply = True):
48
- return ValidateHandler(self, args, state, apply = apply)
77
+ return ValidateStateHandler(self, args, state, apply = apply)
49
78
 
50
79
  def validate_state(self, state: ReplState, show_err = True):
51
80
  return state.validate(self.required(), show_err=show_err)
@@ -112,7 +141,7 @@ class Command:
112
141
  options = [options]
113
142
 
114
143
  if args and trailing:
115
- while args[-1] in trailing:
144
+ while args and args[-1] in trailing:
116
145
  found_trailing = True
117
146
  args = args[:-1]
118
147
 
@@ -130,6 +159,8 @@ class Command:
130
159
  return new_args, found_trailing, found_sequence, found_options
131
160
 
132
161
  def extract_option_sequence(args, sequence):
162
+ new_args = args
163
+
133
164
  len_sub = len(sequence)
134
165
  len_main = len(args)
135
166
  for i in range(len_main - len_sub + 1):
@@ -148,11 +179,11 @@ class Command:
148
179
  cmd = s
149
180
  print()
150
181
 
151
- class InvalidState(Exception):
182
+ class InvalidStateException(Exception):
152
183
  def __init__(self, state: ReplState):
153
184
  super().__init__(f'Invalid state')
154
185
 
155
- class ValidateHandler:
186
+ class ValidateStateHandler:
156
187
  def __init__(self, cmd: Command, args: list[str], state: ReplState, apply = True):
157
188
  self.cmd = cmd
158
189
  self.args = args
@@ -164,15 +195,68 @@ class ValidateHandler:
164
195
  args = self.args
165
196
  if self.apply:
166
197
  state, args = self.cmd.apply_state(args, state)
198
+ else:
199
+ args = args[len(self.cmd.command_tokens()):]
167
200
 
168
201
  if not self.cmd.validate_state(state):
169
- raise InvalidState(state)
202
+ raise InvalidStateException(state)
170
203
 
171
204
  return args, state
172
205
 
173
206
  def __exit__(self, exc_type, exc_val, exc_tb):
174
207
  return False
175
208
 
209
+ class InvalidArgumentsException(Exception):
210
+ def __init__(self):
211
+ super().__init__(f'Invalid arguments')
212
+
213
+ class ValidateArgCountHandler:
214
+ def __init__(self, args: list[str], state: ReplState, at_least: int = 1, exactly: int = 1,
215
+ name: str = None, msg: Callable[[], None] = None, default: Union[str, list[str]] = None,
216
+ separator: str = None):
217
+ self.args = args
218
+ self.state = state
219
+ self.at_least = at_least
220
+ self.exactly = exactly
221
+ self.name = name
222
+ self.msg = msg
223
+ self.default = default
224
+ self.separator = separator
225
+
226
+ def __enter__(self) -> Union[tuple[list[str], ReplState], tuple[str, ReplState]]:
227
+ if self.exactly > 0 and len(self.args) != self.exactly or len(self.args) < self.at_least:
228
+ if self.default:
229
+ v = self.default
230
+ if isinstance(v, list):
231
+ v = ' '.join(v)
232
+
233
+ return v
234
+
235
+ if self.state.in_repl:
236
+ if self.msg:
237
+ self.msg()
238
+ elif self.name:
239
+ log2(f'{self.name} is required.')
240
+ elif self.name:
241
+ log2(f'* {self.name} is missing.')
242
+
243
+ Command.display_help()
244
+
245
+ raise InvalidArgumentsException()
246
+
247
+ if self.separator == ' ':
248
+ return self.args
249
+
250
+ # join and re-split with separator
251
+ args_to_return = ' '.join(self.args)
252
+ if self.separator:
253
+ args_to_return = [arg.strip(' ') for arg in args_to_return.split(self.separator)]
254
+
255
+ return args_to_return
256
+
257
+ def __exit__(self, exc_type, exc_val, exc_tb):
258
+ return False
259
+
176
260
  class ExtractOptionsHandler:
177
261
  def __init__(self, args: list[str], options: list[str] = None):
178
262
  self.args = args
@@ -1,14 +1,17 @@
1
+ from datetime import datetime
1
2
  from kubernetes import client
2
3
  from typing import List
3
4
 
4
5
  from adam.checks.check_utils import run_checks
5
6
  from adam.columns.columns import Columns, collect_checks
7
+ from adam.config import Config
8
+ from adam.repl_session import ReplSession
6
9
  from adam.utils_issues import IssuesUtils
7
10
  from adam.utils_k8s.cassandra_nodes import CassandraNodes
8
11
  from adam.utils_k8s.pods import Pods
9
12
  from adam.utils_k8s.statefulsets import StatefulSets
10
13
  from adam.repl_state import ReplState
11
- from adam.utils import duration, lines_to_tabular, log, log2
14
+ from adam.utils import SORT, duration, tabulize, log, log2
12
15
 
13
16
  def show_pods(pods: List[client.V1Pod], ns: str, show_namespace = True, show_host_id = True):
14
17
  if len(pods) == 0:
@@ -48,9 +51,7 @@ def show_pods(pods: List[client.V1Pod], ns: str, show_namespace = True, show_hos
48
51
  line += f"@{ns}"
49
52
  return line + f" {ready}/{pod_cnt} {status}"
50
53
 
51
- pod_names = [line(pod) for pod in pods]
52
-
53
- log(lines_to_tabular(pod_names, 'HOST_ID POD_NAME READY POD_STATUS' if show_host_id else 'POD_NAME READY POD_STATUS'))
54
+ tabulize(pods, line, header='HOST_ID POD_NAME READY POD_STATUS' if show_host_id else 'POD_NAME READY POD_STATUS')
54
55
 
55
56
  def show_rollout(sts: str, ns: str):
56
57
  restarted, rollingout = StatefulSets.restarted_at(sts, ns)
@@ -61,18 +62,24 @@ def show_rollout(sts: str, ns: str):
61
62
  else:
62
63
  log2(f'Cluster has completed rollout {d} ago.')
63
64
 
64
- def show_table(state: ReplState, pods: list[str], cols: str, header: str, show_out=False):
65
+ def show_table(state: ReplState, pods: list[str], cols: str, header: str, show_out=False, backgrounded = False):
65
66
  columns = Columns.create_columns(cols)
66
67
 
67
68
  results = run_checks(cluster=state.sts, pod=state.pod, namespace=state.namespace, checks=collect_checks(columns), show_out=show_out)
68
69
 
69
- def line(pod_name: str):
70
- cells = [c.pod_value(results, pod_name) for c in columns]
71
- return ','.join(cells)
70
+ r = tabulize(pods, lambda p: ','.join([c.pod_value(results, p) for c in columns]), header=header, separator=',', sorted=SORT, to = 0 if backgrounded else 1)
71
+
72
+ if backgrounded:
73
+ log_prefix = Config().get('log-prefix', '/tmp/qing')
74
+ log_file = f'{log_prefix}-{datetime.now().strftime("%d%H%M%S")}.log'
75
+
76
+ with open(log_file, 'w') as f:
77
+ f.write(r)
78
+
79
+ ReplSession().append_history(f':sh cat {log_file}')
72
80
 
73
- lines = [line(pod) for pod in pods]
74
- lines.sort()
81
+ r = log_file
75
82
 
76
- log(lines_to_tabular(lines, header, separator=','))
83
+ IssuesUtils.show(results, state.in_repl)
77
84
 
78
- IssuesUtils.show(results, state.in_repl)
85
+ return r
@@ -0,0 +1,28 @@
1
+ from adam.commands.cql.utils_cql import cassandra_keyspaces, cassandra_table_names
2
+ from adam.commands.export.export_sessions import ExportSessions
3
+ from adam.commands.export.export_databases import ExportDatabases
4
+ from adam.config import Config
5
+ from adam.repl_state import ReplState
6
+ from adam.sql.lark_completer import LarkCompleter
7
+ from adam.utils import log_timing
8
+ from adam.utils_k8s.statefulsets import StatefulSets
9
+
10
+ def completions_c(state: ReplState) -> dict[str, any]:
11
+ ps = Config().get('cql.alter-tables.gc-grace-periods', '3600,86400,864000,7776000').split(',')
12
+
13
+ with log_timing('lark.completions'):
14
+ return LarkCompleter(
15
+ expandables = {
16
+ 'tables': lambda x: cassandra_table_names(state),
17
+ 'keyspaces': lambda x: cassandra_keyspaces(state.with_no_pod()),
18
+ 'table-props': lambda x: {
19
+ 'GC_GRACE_SECONDS': ps
20
+ },
21
+ 'export-database-types': lambda x: ['athena', 'sqlite', 'csv'],
22
+ 'export-databases': lambda x: ExportDatabases.database_names(),
23
+ 'export-sessions': lambda x: ExportSessions.export_session_names(state.sts, state.pod, state.namespace),
24
+ 'export-sessions-incomplete': lambda x: ExportSessions.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import'),
25
+ 'hosts': lambda x: [f'@{p}' for p in StatefulSets.pod_names(state.sts, state.namespace)],
26
+ },
27
+ variant=ReplState.C
28
+ ).completions_for_nesting()
@@ -3,9 +3,8 @@ import click
3
3
  from adam.commands import extract_trailing_options
4
4
  from adam.commands.command import Command
5
5
  from adam.commands.command_helpers import ClusterOrPodCommandHelper
6
- from adam.commands.cql.cql_completions import cql_completions
6
+ from adam.commands.cql.completions_c import completions_c
7
7
  from adam.commands.cql.utils_cql import cassandra
8
- from adam.utils_k8s.statefulsets import StatefulSets
9
8
  from adam.repl_state import ReplState, RequiredState
10
9
  from adam.utils import log
11
10
 
@@ -34,17 +33,14 @@ class Cqlsh(Command):
34
33
  with self.validate(args, state) as (args, state):
35
34
  with extract_trailing_options(args, '&') as (args, backgrounded):
36
35
  with cassandra(state) as pods:
37
- pods.cql(args, background=backgrounded)
36
+ pods.cql(args, backgrounded=backgrounded)
38
37
 
39
38
  def completion(self, state: ReplState) -> dict[str, any]:
40
39
  if state.device != state.C:
41
- # conflicts with psql completions
42
40
  return {}
43
41
 
44
42
  if state.sts or state.pod:
45
- c = cql_completions(state)
46
- return c | \
47
- {f'@{p}': c for p in StatefulSets.pod_names(state.sts, state.namespace)}
43
+ return completions_c(state)
48
44
 
49
45
  return {}
50
46