kaqing 2.0.145__py3-none-any.whl → 2.0.174__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 (174) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +8 -11
  3. adam/batch.py +3 -3
  4. adam/checks/check_utils.py +14 -46
  5. adam/checks/cpu.py +7 -1
  6. adam/checks/cpu_metrics.py +52 -0
  7. adam/checks/disk.py +2 -3
  8. adam/columns/columns.py +3 -1
  9. adam/columns/cpu.py +3 -1
  10. adam/columns/cpu_metrics.py +22 -0
  11. adam/columns/memory.py +3 -4
  12. adam/commands/__init__.py +22 -0
  13. adam/commands/alter_tables.py +33 -48
  14. adam/commands/audit/audit.py +22 -23
  15. adam/commands/audit/audit_repair_tables.py +14 -17
  16. adam/commands/audit/audit_run.py +15 -23
  17. adam/commands/audit/show_last10.py +10 -13
  18. adam/commands/audit/show_slow10.py +10 -13
  19. adam/commands/audit/show_top10.py +10 -13
  20. adam/commands/audit/utils_show_top10.py +2 -3
  21. adam/commands/bash/__init__.py +5 -0
  22. adam/commands/bash/bash.py +7 -104
  23. adam/commands/bash/utils_bash.py +16 -0
  24. adam/commands/cat.py +7 -23
  25. adam/commands/cd.py +7 -11
  26. adam/commands/check.py +14 -23
  27. adam/commands/cli_commands.py +2 -3
  28. adam/commands/code.py +20 -23
  29. adam/commands/command.py +152 -37
  30. adam/commands/commands_utils.py +8 -17
  31. adam/commands/cp.py +18 -32
  32. adam/commands/cql/cql_completions.py +11 -7
  33. adam/commands/cql/cqlsh.py +10 -30
  34. adam/commands/cql/{cql_utils.py → utils_cql.py} +147 -15
  35. adam/commands/deploy/code_start.py +7 -10
  36. adam/commands/deploy/code_stop.py +4 -21
  37. adam/commands/deploy/code_utils.py +3 -3
  38. adam/commands/deploy/deploy.py +4 -27
  39. adam/commands/deploy/deploy_frontend.py +14 -17
  40. adam/commands/deploy/deploy_pg_agent.py +2 -5
  41. adam/commands/deploy/deploy_pod.py +64 -68
  42. adam/commands/deploy/undeploy.py +4 -27
  43. adam/commands/deploy/undeploy_frontend.py +4 -7
  44. adam/commands/deploy/undeploy_pg_agent.py +4 -7
  45. adam/commands/deploy/undeploy_pod.py +9 -12
  46. adam/commands/devices/device.py +93 -2
  47. adam/commands/devices/device_app.py +37 -10
  48. adam/commands/devices/device_auit_log.py +8 -2
  49. adam/commands/devices/device_cass.py +47 -7
  50. adam/commands/devices/device_export.py +9 -11
  51. adam/commands/devices/device_postgres.py +41 -6
  52. adam/commands/exit.py +1 -4
  53. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  54. adam/commands/export/clean_up_export_sessions.py +12 -8
  55. adam/commands/export/drop_export_database.py +7 -26
  56. adam/commands/export/drop_export_databases.py +5 -14
  57. adam/commands/export/export.py +8 -38
  58. adam/commands/export/export_databases.py +86 -27
  59. adam/commands/export/export_select.py +25 -27
  60. adam/commands/export/export_select_x.py +3 -3
  61. adam/commands/export/export_sessions.py +124 -0
  62. adam/commands/export/export_use.py +8 -17
  63. adam/commands/export/exporter.py +88 -158
  64. adam/commands/export/import_session.py +7 -35
  65. adam/commands/export/importer.py +12 -5
  66. adam/commands/export/importer_athena.py +21 -20
  67. adam/commands/export/importer_sqlite.py +16 -21
  68. adam/commands/export/show_column_counts.py +7 -25
  69. adam/commands/export/show_export_databases.py +4 -6
  70. adam/commands/export/show_export_session.py +7 -18
  71. adam/commands/export/show_export_sessions.py +9 -12
  72. adam/commands/export/utils_export.py +26 -1
  73. adam/commands/intermediate_command.py +49 -0
  74. adam/commands/issues.py +11 -43
  75. adam/commands/kubectl.py +3 -6
  76. adam/commands/login.py +22 -24
  77. adam/commands/logs.py +3 -6
  78. adam/commands/ls.py +8 -9
  79. adam/commands/medusa/medusa.py +4 -22
  80. adam/commands/medusa/medusa_backup.py +20 -25
  81. adam/commands/medusa/medusa_restore.py +34 -36
  82. adam/commands/medusa/medusa_show_backupjobs.py +14 -18
  83. adam/commands/medusa/medusa_show_restorejobs.py +11 -18
  84. adam/commands/nodetool.py +6 -15
  85. adam/commands/param_get.py +11 -13
  86. adam/commands/param_set.py +8 -12
  87. adam/commands/postgres/postgres.py +22 -38
  88. adam/commands/postgres/postgres_context.py +47 -23
  89. adam/commands/postgres/postgres_ls.py +4 -8
  90. adam/commands/postgres/postgres_preview.py +5 -9
  91. adam/commands/postgres/psql_completions.py +1 -1
  92. adam/commands/postgres/utils_postgres.py +70 -0
  93. adam/commands/preview_table.py +6 -45
  94. adam/commands/pwd.py +13 -16
  95. adam/commands/reaper/reaper.py +4 -27
  96. adam/commands/reaper/reaper_forward.py +48 -55
  97. adam/commands/reaper/reaper_forward_session.py +6 -0
  98. adam/commands/reaper/reaper_forward_stop.py +10 -16
  99. adam/commands/reaper/reaper_restart.py +7 -14
  100. adam/commands/reaper/reaper_run_abort.py +8 -33
  101. adam/commands/reaper/reaper_runs.py +42 -57
  102. adam/commands/reaper/reaper_runs_abort.py +29 -49
  103. adam/commands/reaper/reaper_schedule_activate.py +9 -32
  104. adam/commands/reaper/reaper_schedule_start.py +9 -32
  105. adam/commands/reaper/reaper_schedule_stop.py +9 -32
  106. adam/commands/reaper/reaper_schedules.py +4 -14
  107. adam/commands/reaper/reaper_status.py +8 -16
  108. adam/commands/reaper/utils_reaper.py +196 -0
  109. adam/commands/repair/repair.py +4 -22
  110. adam/commands/repair/repair_log.py +5 -11
  111. adam/commands/repair/repair_run.py +27 -34
  112. adam/commands/repair/repair_scan.py +32 -38
  113. adam/commands/repair/repair_stop.py +5 -11
  114. adam/commands/report.py +27 -29
  115. adam/commands/restart.py +25 -26
  116. adam/commands/rollout.py +19 -24
  117. adam/commands/shell.py +10 -4
  118. adam/commands/show/show.py +10 -26
  119. adam/commands/show/show_cassandra_repairs.py +35 -0
  120. adam/commands/show/show_cassandra_status.py +32 -43
  121. adam/commands/show/show_cassandra_version.py +5 -18
  122. adam/commands/show/show_commands.py +19 -24
  123. adam/commands/show/show_host.py +1 -1
  124. adam/commands/show/show_login.py +20 -27
  125. adam/commands/show/show_processes.py +15 -19
  126. adam/commands/show/show_storage.py +10 -20
  127. adam/commands/watch.py +26 -29
  128. adam/config.py +4 -16
  129. adam/embedded_params.py +1 -1
  130. adam/log.py +4 -4
  131. adam/pod_exec_result.py +3 -3
  132. adam/repl.py +31 -32
  133. adam/repl_commands.py +11 -11
  134. adam/repl_state.py +52 -26
  135. adam/sql/sql_completer.py +4 -6
  136. adam/sql/sql_state_machine.py +21 -14
  137. adam/sso/authn_ad.py +6 -8
  138. adam/sso/authn_okta.py +4 -6
  139. adam/sso/cred_cache.py +3 -5
  140. adam/sso/idp.py +9 -12
  141. adam/utils.py +393 -33
  142. adam/utils_athena.py +14 -13
  143. adam/utils_audits.py +12 -12
  144. adam/utils_issues.py +32 -0
  145. adam/utils_k8s/app_clusters.py +13 -18
  146. adam/utils_k8s/app_pods.py +2 -0
  147. adam/utils_k8s/cassandra_clusters.py +21 -18
  148. adam/utils_k8s/custom_resources.py +16 -17
  149. adam/utils_k8s/ingresses.py +2 -2
  150. adam/utils_k8s/jobs.py +7 -11
  151. adam/utils_k8s/k8s.py +87 -0
  152. adam/utils_k8s/pods.py +14 -76
  153. adam/utils_k8s/secrets.py +4 -4
  154. adam/utils_k8s/service_accounts.py +5 -4
  155. adam/utils_k8s/services.py +2 -2
  156. adam/utils_k8s/statefulsets.py +1 -12
  157. adam/utils_repl/state_machine.py +3 -3
  158. adam/utils_sqlite.py +78 -42
  159. adam/version.py +1 -1
  160. {kaqing-2.0.145.dist-info → kaqing-2.0.174.dist-info}/METADATA +1 -1
  161. kaqing-2.0.174.dist-info/RECORD +230 -0
  162. adam/commands/app.py +0 -67
  163. adam/commands/app_ping.py +0 -44
  164. adam/commands/export/clean_up_export_session.py +0 -53
  165. adam/commands/postgres/postgres_utils.py +0 -31
  166. adam/commands/reaper/reaper_session.py +0 -159
  167. adam/commands/show/show_app_actions.py +0 -56
  168. adam/commands/show/show_app_id.py +0 -47
  169. adam/commands/show/show_app_queues.py +0 -45
  170. adam/commands/show/show_repairs.py +0 -47
  171. kaqing-2.0.145.dist-info/RECORD +0 -227
  172. {kaqing-2.0.145.dist-info → kaqing-2.0.174.dist-info}/WHEEL +0 -0
  173. {kaqing-2.0.145.dist-info → kaqing-2.0.174.dist-info}/entry_points.txt +0 -0
  174. {kaqing-2.0.145.dist-info → kaqing-2.0.174.dist-info}/top_level.txt +0 -0
adam/commands/command.py CHANGED
@@ -1,11 +1,12 @@
1
1
  from abc import abstractmethod
2
+ from collections.abc import Callable
2
3
  import copy
3
4
  import subprocess
4
5
  import sys
6
+ from typing import Union
5
7
 
6
- from adam.commands.command_helpers import ClusterCommandHelper
7
8
  from adam.repl_state import ReplState, RequiredState
8
- from adam.utils import is_lambda, lines_to_tabular, log, log2
9
+ from adam.utils import is_lambda, log2
9
10
 
10
11
  repl_cmds: list['Command'] = []
11
12
 
@@ -44,7 +45,10 @@ class Command:
44
45
  def required(self) -> RequiredState:
45
46
  return None
46
47
 
47
- def validate_state(self, state: ReplState, show_err=True):
48
+ def validate(self, args: list[str], state: ReplState, apply = True):
49
+ return ValidateStateHandler(self, args, state, apply = apply)
50
+
51
+ def validate_state(self, state: ReplState, show_err = True):
48
52
  return state.validate(self.required(), show_err=show_err)
49
53
 
50
54
  def help(self, _: ReplState) -> str:
@@ -93,17 +97,50 @@ class Command:
93
97
  args.extend(['--help'])
94
98
  subprocess.run(args)
95
99
 
96
- def extract_options(args: list[str], names: list[str]):
97
- found: list[str] = []
100
+ def extract_options(args: list[str], trailing: Union[str, list[str]] = [], sequence: list[str] = [], options: list[str] = []):
101
+ found_options: list[str] = []
102
+ found_trailing = None
103
+ found_sequence = None
104
+
105
+ if trailing is None:
106
+ trailing = []
107
+ elif isinstance(trailing, str):
108
+ trailing = [trailing]
109
+
110
+ if options is None:
111
+ options = []
112
+ elif isinstance(options, str):
113
+ options = [options]
114
+
115
+ if args and trailing:
116
+ while args[-1] in trailing:
117
+ found_trailing = True
118
+ args = args[:-1]
119
+
120
+ if args and sequence:
121
+ args, found_sequence = Command.extract_option_sequence(args, sequence)
98
122
 
99
123
  new_args: list[str] = []
100
- for arg in args:
101
- if arg in names:
102
- found.append(arg)
103
- else:
104
- new_args.append(arg)
124
+ if args:
125
+ for arg in args:
126
+ if arg in options:
127
+ found_options.append(arg)
128
+ else:
129
+ new_args.append(arg)
130
+
131
+ return new_args, found_trailing, found_sequence, found_options
132
+
133
+ def extract_option_sequence(args, sequence):
134
+ len_sub = len(sequence)
135
+ len_main = len(args)
136
+ for i in range(len_main - len_sub + 1):
137
+ if args[i:i+len_sub] == sequence:
138
+ new_args = copy.copy(args)
139
+ del new_args[i:i+len_sub]
140
+
141
+ return new_args, True
105
142
 
106
- return new_args, found
143
+ return new_args, False
107
144
 
108
145
  def print_chain(cmd: 'Command'):
109
146
  print(f'{cmd.command()}', end = '')
@@ -112,31 +149,109 @@ class Command:
112
149
  cmd = s
113
150
  print()
114
151
 
115
- def intermediate_run(self, cmd: str, state: ReplState, args: list[str], cmds: list['Command'], separator='\t', display_help=True):
116
- state, _ = self.apply_state(args, state)
152
+ class InvalidStateException(Exception):
153
+ def __init__(self, state: ReplState):
154
+ super().__init__(f'Invalid state')
155
+
156
+ class ValidateStateHandler:
157
+ def __init__(self, cmd: Command, args: list[str], state: ReplState, apply = True):
158
+ self.cmd = cmd
159
+ self.args = args
160
+ self.state = state
161
+ self.apply = apply
162
+
163
+ def __enter__(self) -> tuple[list[str], ReplState]:
164
+ state = self.state
165
+ args = self.args
166
+ if self.apply:
167
+ state, args = self.cmd.apply_state(args, state)
168
+ else:
169
+ args = args[len(self.cmd.command_tokens()):]
117
170
 
118
- if state.in_repl:
119
- if display_help:
120
- log(lines_to_tabular([c.help(state) for c in cmds], separator=separator))
171
+ if not self.cmd.validate_state(state):
172
+ raise InvalidStateException(state)
121
173
 
122
- return 'command-missing'
123
- else:
124
- # head with the Chain of Responsibility pattern
125
- cmds = Command.chain(cmds)
126
- if not cmds.run(cmd, state):
127
- if display_help:
128
- log2('* Command is missing.')
129
- Command.display_help()
130
- return 'command-missing'
131
-
132
- return state
133
-
134
- def intermediate_help(super_help: str, cmd: str, cmd_list: list['Command'], separator='\t', show_cluster_help=False):
135
- log(super_help)
136
- log()
137
- log('Sub-Commands:')
138
-
139
- log(lines_to_tabular([c.help(ReplState()).replace(f'{cmd} ', ' ', 1) for c in cmd_list], separator=separator))
140
- if show_cluster_help:
141
- log()
142
- ClusterCommandHelper.cluster_help()
174
+ return args, state
175
+
176
+ def __exit__(self, exc_type, exc_val, exc_tb):
177
+ return False
178
+
179
+ class InvalidArgumentsException(Exception):
180
+ def __init__(self):
181
+ super().__init__(f'Invalid arguments')
182
+
183
+ class ValidateArgCountHandler:
184
+ def __init__(self, args: list[str], state: ReplState, count_at_least: int = 1, name: str = None, msg: Callable[[], None] = None):
185
+ self.args = args
186
+ self.state = state
187
+ self.count_at_least = count_at_least
188
+ self.name = name
189
+ self.msg = msg
190
+
191
+ def __enter__(self) -> tuple[list[str], ReplState]:
192
+ if len(self.args) < self.count_at_least:
193
+ if self.state.in_repl:
194
+ if self.msg:
195
+ self.msg()
196
+ elif self.name:
197
+ log2(f'{self.name} is required.')
198
+ elif self.name:
199
+ log2(f'* {self.name} is missing.')
200
+
201
+ Command.display_help()
202
+
203
+ raise InvalidArgumentsException()
204
+
205
+ return ' '.join(self.args)
206
+
207
+ def __exit__(self, exc_type, exc_val, exc_tb):
208
+ return False
209
+
210
+ class ExtractOptionsHandler:
211
+ def __init__(self, args: list[str], options: list[str] = None):
212
+ self.args = args
213
+ self.options = options
214
+
215
+ def __enter__(self) -> tuple[list[str], list[str]]:
216
+ args, _, _, options = Command.extract_options(self.args, options=self.options)
217
+ return args, options
218
+
219
+ def __exit__(self, exc_type, exc_val, exc_tb):
220
+ return False
221
+
222
+ class ExtractTrailingOptionsHandler:
223
+ def __init__(self, args: list[str], trailing: list[str] = None):
224
+ self.args = args
225
+ self.trailing = trailing
226
+
227
+ def __enter__(self) -> tuple[list[str], list[str]]:
228
+ args, trailing, _, _ = Command.extract_options(self.args, trailing=self.trailing)
229
+ return args, trailing
230
+
231
+ def __exit__(self, exc_type, exc_val, exc_tb):
232
+ return False
233
+
234
+ class ExtractAllOptionsHandler:
235
+ def __init__(self, args: list[str], trailing: list[str] = None, sequence: list[str] = None, options: list[str] = None):
236
+ self.args = args
237
+ self.trailing = trailing
238
+ self.sequence = sequence
239
+ self.options = options
240
+
241
+ def __enter__(self) -> tuple[list[str], list[str]]:
242
+ return Command.extract_options(self.args, trailing=self.trailing, sequence=self.sequence, options=self.options)
243
+
244
+ def __exit__(self, exc_type, exc_val, exc_tb):
245
+ return False
246
+
247
+ class ExtractSequenceOptionsHandler:
248
+ def __init__(self, args: list[str], sequence: list[str]):
249
+ self.args = args
250
+ self.sequence = sequence
251
+
252
+ def __enter__(self) -> tuple[list[str], list[str]]:
253
+ args, _, sequence, _ = Command.extract_options(self.args, sequence=self.sequence)
254
+ return args, sequence
255
+
256
+ def __exit__(self, exc_type, exc_val, exc_tb):
257
+ return False
@@ -1,10 +1,9 @@
1
- from concurrent.futures import ThreadPoolExecutor
2
1
  from kubernetes import client
3
2
  from typing import List
4
3
 
5
4
  from adam.checks.check_utils import run_checks
6
5
  from adam.columns.columns import Columns, collect_checks
7
- from adam.commands.issues import Issues
6
+ from adam.utils_issues import IssuesUtils
8
7
  from adam.utils_k8s.cassandra_nodes import CassandraNodes
9
8
  from adam.utils_k8s.pods import Pods
10
9
  from adam.utils_k8s.statefulsets import StatefulSets
@@ -20,18 +19,10 @@ def show_pods(pods: List[client.V1Pod], ns: str, show_namespace = True, show_hos
20
19
  if show_host_id:
21
20
  names = [pod.metadata.name for pod in pods]
22
21
 
23
- def get_host_id_with_pod(pod, ns):
24
- return (CassandraNodes.get_host_id(pod, ns), pod)
25
-
26
- def body(executor: ThreadPoolExecutor, pod, ns, show_out):
27
- if executor:
28
- return executor.submit(get_host_id_with_pod, pod, ns)
29
-
30
- id = CassandraNodes.get_host_id(pod, ns)
31
-
32
- return (id, pod)
33
-
34
- host_ids_by_pod = {pod: id for id, pod in Pods.on_pods(names, ns, body, action='get-host-id', show_out=False)}
22
+ msg = 'd`Retrieving|Retrived {size} host ids'
23
+ with Pods.parallelize(names, msg=msg, action = 'get-host-id') as exec:
24
+ host_pods = exec.map(lambda pod: (CassandraNodes.get_host_id(pod, ns), pod))
25
+ host_ids_by_pod = {pod: id for id, pod in host_pods}
35
26
 
36
27
  def line(pod: client.V1Pod):
37
28
  pod_cnt = len(pod.status.container_statuses)
@@ -70,10 +61,10 @@ def show_rollout(sts: str, ns: str):
70
61
  else:
71
62
  log2(f'Cluster has completed rollout {d} ago.')
72
63
 
73
- def show_table(state: ReplState, pods: list[str], cols: str, header: str, show_output=False):
64
+ def show_table(state: ReplState, pods: list[str], cols: str, header: str, show_out=False):
74
65
  columns = Columns.create_columns(cols)
75
66
 
76
- results = run_checks(cluster=state.sts, pod=state.pod, namespace=state.namespace, checks=collect_checks(columns), show_output=show_output)
67
+ results = run_checks(cluster=state.sts, pod=state.pod, namespace=state.namespace, checks=collect_checks(columns), show_out=show_out)
77
68
 
78
69
  def line(pod_name: str):
79
70
  cells = [c.pod_value(results, pod_name) for c in columns]
@@ -84,4 +75,4 @@ def show_table(state: ReplState, pods: list[str], cols: str, header: str, show_o
84
75
 
85
76
  log(lines_to_tabular(lines, header, separator=','))
86
77
 
87
- Issues.show(results, state.in_repl)
78
+ IssuesUtils.show(results, state.in_repl)
adam/commands/cp.py CHANGED
@@ -1,7 +1,8 @@
1
1
  import click
2
2
  import pyperclip
3
3
 
4
- from adam.commands.command import Command
4
+ from adam.commands import validate_args
5
+ from adam.commands.command import Command, InvalidArgumentsException
5
6
  from adam.commands.command_helpers import ClusterOrPodCommandHelper
6
7
  from adam.commands.cli_commands import CliCommands
7
8
  from adam.repl_state import ReplState, RequiredState
@@ -29,47 +30,32 @@ class ClipboardCopy(Command):
29
30
  if not(args := self.args(cmd)):
30
31
  return super().run(cmd, state)
31
32
 
32
- state, args = self.apply_state(args, state)
33
- if not self.validate_state(state):
34
- return state
35
-
36
- if len(args) < 1:
37
- if state.in_repl:
33
+ with self.validate(args, state) as (args, state):
34
+ def display_keys():
38
35
  log2('Key is required.')
39
36
  log2()
40
37
  log2('Keys:')
41
38
  log2(lines_to_tabular([f'{k},{v}' for k, v in CliCommands.values(state, collapse=True).items()], separator=','))
42
- else:
43
- log2('* Key is missing.')
44
- Command.display_help()
45
39
 
46
- return 'command-missing'
40
+ with validate_args(args, state, name='key', msg=display_keys) as key:
41
+ if not key in CliCommands.values(state):
42
+ if state.in_repl:
43
+ display_keys()
44
+ else:
45
+ log2('* Invalid key')
46
+ Command.display_help()
47
47
 
48
- key = args[0]
49
- if not key in CliCommands.values(state):
50
- if state.in_repl:
51
- log2('Key is required.')
52
- log2()
53
- log2('Keys:')
54
- log2(lines_to_tabular([f'{k},{v}' for k, v in CliCommands.values(state, collapse=True).items()], separator=','))
55
- else:
56
- log2('* Invalid key')
57
- Command.display_help()
58
-
59
- return 'command-invalid'
48
+ raise InvalidArgumentsException()
60
49
 
61
- value = CliCommands.values(state)[key]
62
- pyperclip.copy(value)
63
- log2('The following line has been copied to clipboard. Use <Ctrl-V> to use it.')
64
- log2(f' {value}')
50
+ value = CliCommands.values(state)[key]
51
+ pyperclip.copy(value)
52
+ log2('The following line has been copied to clipboard. Use <Ctrl-V> to use it.')
53
+ log2(f' {value}')
65
54
 
66
- return 'value-copied'
55
+ return state
67
56
 
68
57
  def completion(self, state: ReplState):
69
- if state.sts:
70
- return {ClipboardCopy.COMMAND: {key: None for key in CliCommands.values(state).keys()}}
71
-
72
- return {}
58
+ return super().completion(state, lambda: {key: None for key in CliCommands.values(state).keys()})
73
59
 
74
60
  def help(self, _: ReplState):
75
61
  return f"{ClipboardCopy.COMMAND} <key>\t copy a value to clipboard for conveninence"
@@ -1,16 +1,20 @@
1
- from adam.commands.cql.cql_utils import cassandra_keyspaces, cassandra_table_names
2
- from adam.commands.export.exporter import Exporter
1
+ from adam.commands.cql.utils_cql import cassandra_keyspaces, cassandra_table_names
2
+ from adam.commands.export.export_sessions import ExportSessions
3
3
  from adam.commands.export.export_databases import ExportDatabases
4
4
  from adam.config import Config
5
5
  from adam.repl_state import ReplState
6
6
  from adam.sql.sql_completer import SqlCompleter, SqlVariant
7
+ from adam.utils import log_timing
7
8
 
8
9
  def cql_completions(state: ReplState) -> dict[str, any]:
9
10
  ps = Config().get('cql.alter-tables.gc-grace-periods', '3600,86400,864000,7776000').split(',')
10
11
  # warm up caches
11
- cassandra_keyspaces(state)
12
- cassandra_table_names(state)
13
- ExportDatabases.database_names()
12
+ with log_timing('cassandra_keyspaces'):
13
+ cassandra_keyspaces(state)
14
+ with log_timing('cassandra_table_names'):
15
+ cassandra_table_names(state)
16
+ with log_timing('ExportDatabases.database_names'):
17
+ ExportDatabases.database_names()
14
18
 
15
19
  expandables = {
16
20
  'keyspaces': lambda: cassandra_keyspaces(state),
@@ -18,8 +22,8 @@ def cql_completions(state: ReplState) -> dict[str, any]:
18
22
  'GC_GRACE_SECONDS': ps
19
23
  },
20
24
  'export-dbs': lambda: ExportDatabases.database_names(),
21
- 'export-sessions': lambda: Exporter.export_session_names(state.sts, state.pod, state.namespace),
22
- 'export-sessions-incomplete': lambda: Exporter.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import'),
25
+ 'export-sessions': lambda: ExportSessions.export_session_names(state.sts, state.pod, state.namespace),
26
+ 'export-sessions-incomplete': lambda: ExportSessions.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import'),
23
27
  }
24
28
 
25
29
  return SqlCompleter(
@@ -1,12 +1,13 @@
1
1
  import click
2
2
 
3
+ from adam.commands import extract_trailing_options
3
4
  from adam.commands.command import Command
4
5
  from adam.commands.command_helpers import ClusterOrPodCommandHelper
5
6
  from adam.commands.cql.cql_completions import cql_completions
7
+ from adam.commands.cql.utils_cql import cassandra
6
8
  from adam.utils_k8s.statefulsets import StatefulSets
7
- from .cql_utils import run_cql
8
9
  from adam.repl_state import ReplState, RequiredState
9
- from adam.utils import log, log2
10
+ from adam.utils import log
10
11
 
11
12
  class Cqlsh(Command):
12
13
  COMMAND = 'cql'
@@ -30,32 +31,10 @@ class Cqlsh(Command):
30
31
  if not(args := self.args(cmd)):
31
32
  return super().run(cmd, state)
32
33
 
33
- state, args = self.apply_state(args, state)
34
- if not self.validate_state(state):
35
- return state
36
-
37
- background = False
38
- opts = []
39
- cqls = []
40
- for index, arg in enumerate(args):
41
- if arg.startswith('--'):
42
- opts.append(arg)
43
- elif index == len(args) -1 and arg == '&':
44
- background = True
45
- elif arg != '-e':
46
- cqls.append(arg)
47
- if not cqls:
48
- if state.in_repl:
49
- log2('Please enter cql statement. e.g. select host_id from system.local')
50
- else:
51
- log2('* CQL statement is missing.')
52
- log2()
53
- Command.display_help()
54
-
55
- return 'no-cql'
56
-
57
- cql = ' '.join(cqls)
58
- return run_cql(state, cql, opts, show_out=True, background=background)
34
+ with self.validate(args, state) as (args, state):
35
+ with extract_trailing_options(args, '&') as (args, backgrounded):
36
+ with cassandra(state) as pods:
37
+ pods.cql(args, background=backgrounded)
59
38
 
60
39
  def completion(self, state: ReplState) -> dict[str, any]:
61
40
  if state.device != state.C:
@@ -63,8 +42,9 @@ class Cqlsh(Command):
63
42
  return {}
64
43
 
65
44
  if state.sts or state.pod:
66
- return cql_completions(state) | \
67
- {f'@{p}': cql_completions(state) for p in StatefulSets.pod_names(state.sts, state.namespace)}
45
+ c = cql_completions(state)
46
+ return c | \
47
+ {f'@{p}': c for p in StatefulSets.pod_names(state.sts, state.namespace)}
68
48
 
69
49
  return {}
70
50
 
@@ -1,20 +1,23 @@
1
1
  import functools
2
2
  import re
3
+ from typing import Union
3
4
 
4
- from adam.config import Config
5
+ from adam.commands.command import Command
6
+ from adam.commands.commands_utils import show_table
5
7
  from adam.utils_k8s.cassandra_clusters import CassandraClusters
6
8
  from adam.utils_k8s.cassandra_nodes import CassandraNodes
7
9
  from adam.utils_k8s.secrets import Secrets
8
10
  from adam.pod_exec_result import PodExecResult
9
11
  from adam.repl_state import ReplState
10
- from adam.utils import log2
12
+ from adam.utils import log2, log_timing, wait_log
13
+ from adam.utils_k8s.statefulsets import StatefulSets
11
14
 
12
15
  @functools.lru_cache()
13
16
  def cassandra_keyspaces(state: ReplState, on_any=True):
14
17
  if state.pod:
15
- Config().wait_log(f'Inspecting Cassandra Keyspaces on {state.pod}...')
18
+ wait_log(f'Inspecting Cassandra Keyspaces on {state.pod}...')
16
19
  else:
17
- Config().wait_log(f'Inspecting Cassandra Keyspaces...')
20
+ wait_log(f'Inspecting Cassandra Keyspaces...')
18
21
 
19
22
  r: list[PodExecResult] = run_cql(state, 'describe keyspaces', show_out=False, on_any=on_any)
20
23
  if not r:
@@ -44,17 +47,21 @@ def table_spec(state: ReplState, table: str, on_any=False) -> 'TableSpec':
44
47
 
45
48
  return parse_cql_desc_table(r.stdout if state.pod else r[0].stdout)
46
49
 
47
- def run_cql(state: ReplState, cql: str, opts: list = [], show_out = False, use_single_quotes = False, on_any = False, background=False, log_file=None) -> list[PodExecResult]:
48
- user, pw = Secrets.get_user_pass(state.sts if state.sts else state.pod, state.namespace, secret_path='cql.secret')
49
- if use_single_quotes:
50
- command = f"cqlsh -u {user} -p {pw} {' '.join(opts)} -e '{cql}'"
51
- else:
52
- command = f'cqlsh -u {user} -p {pw} {" ".join(opts)} -e "{cql}"'
50
+ def run_cql(state: ReplState, cql: str, opts: list = [], show_out = False, show_query = False, use_single_quotes = False, on_any = False, background=False, log_file=None) -> list[PodExecResult]:
51
+ if show_query:
52
+ log2(cql)
53
53
 
54
- if state.pod:
55
- return CassandraNodes.exec(state.pod, state.namespace, command, show_out=show_out, background=background, log_file=log_file)
56
- else:
57
- return CassandraClusters.exec(state.sts, state.namespace, command, show_out=show_out, action='cql', on_any=on_any, background=background, log_file=log_file)
54
+ command = None
55
+ with log_timing('Secrets.get_user_pass'):
56
+ user, pw = Secrets.get_user_pass(state.sts if state.sts else state.pod, state.namespace, secret_path='cql.secret')
57
+ if use_single_quotes:
58
+ command = f"cqlsh -u {user} -p {pw} {' '.join(opts)} -e '{cql}'"
59
+ else:
60
+ command = f'cqlsh -u {user} -p {pw} {" ".join(opts)} -e "{cql}"'
61
+
62
+ with log_timing(cql):
63
+ with cassandra(state) as pods:
64
+ return pods.exec(command, action='cql', show_out=show_out, on_any=on_any, background=background, log_file=log_file)
58
65
 
59
66
  def parse_cql_desc_tables(out: str):
60
67
  # Keyspace data_endpoint_auth
@@ -206,4 +213,129 @@ def parse_cql_desc_table(out: str) -> TableSpec:
206
213
  if column.name in pkeys.keys():
207
214
  column.key_index = pkeys[column.name]
208
215
 
209
- return TableSpec(columns)
216
+ return TableSpec(columns)
217
+
218
+ class CqlShHandler:
219
+ def __init__(self, state: ReplState, opts: list = [], show_out = False, show_query = False, use_single_quotes = False, on_any = False, background=False):
220
+ self.state = state
221
+ self.opts = opts
222
+ self.show_out = show_out
223
+ self.show_query = show_query
224
+ self.use_single_quotes = use_single_quotes
225
+ self.on_any = on_any
226
+ self.backgroud = background
227
+
228
+ def __enter__(self):
229
+ return self.query
230
+
231
+ def __exit__(self, exc_type, exc_val, exc_tb):
232
+ return False
233
+
234
+ def query(self, args: list[str], log_file: str = None):
235
+ query: str = args
236
+ background = self.backgroud
237
+ show_out = self.show_out
238
+
239
+ if isinstance(query, list):
240
+ opts = []
241
+ cqls = []
242
+ for index, arg in enumerate(args):
243
+ if arg.startswith('--'):
244
+ opts.append(arg)
245
+ elif arg != '-e':
246
+ cqls.append(arg)
247
+ if not cqls:
248
+ if self.state.in_repl:
249
+ log2('Please enter cql statement. e.g. select host_id from system.local')
250
+ else:
251
+ log2('* CQL statement is missing.')
252
+ log2()
253
+ Command.display_help()
254
+
255
+ return 'no-cql'
256
+
257
+ query = ' '.join(cqls)
258
+ show_out = True
259
+
260
+ return run_cql(self.state, query, opts=self.opts, show_out=show_out, show_query=self.show_query, use_single_quotes=self.use_single_quotes, on_any=self.on_any, background=background, log_file=log_file)
261
+
262
+ class CassandraPodService:
263
+ def __init__(self, handler: 'CassandraExecHandler'):
264
+ self.handler = handler
265
+
266
+ def exec(self, command: str, action='bash', show_out = True, on_any = False, throw_err = False, shell = '/bin/sh', background = False, log_file = None) -> Union[PodExecResult, list[PodExecResult]]:
267
+ state = self.handler.state
268
+ pod = self.handler.pod
269
+
270
+ if pod:
271
+ return CassandraNodes.exec(pod, state.namespace, command,
272
+ show_out=show_out, throw_err=throw_err, shell=shell, background=background, log_file=log_file)
273
+ elif state.sts:
274
+ return CassandraClusters.exec(state.sts, state.namespace, command, action=action,
275
+ show_out=show_out, on_any=on_any, shell=shell, background=background, log_file=log_file)
276
+
277
+ return []
278
+
279
+ def cql(self, args: list[str], opts: list = [], show_out = False, show_query = False, use_single_quotes = False, on_any = False, background=False, log_file: str = None):
280
+ state = self.handler.state
281
+ query: str = args
282
+
283
+ if isinstance(query, list):
284
+ opts = []
285
+ cqls = []
286
+ for arg in args:
287
+ if arg.startswith('--'):
288
+ opts.append(arg)
289
+ elif arg != '-e':
290
+ cqls.append(arg)
291
+ if not cqls:
292
+ if self.state.in_repl:
293
+ log2('Please enter cql statement. e.g. select host_id from system.local')
294
+ else:
295
+ log2('* CQL statement is missing.')
296
+ log2()
297
+ Command.display_help()
298
+
299
+ return 'no-cql'
300
+
301
+ query = ' '.join(cqls)
302
+ show_out = True
303
+
304
+ return run_cql(state, query, opts=opts, show_out=show_out, show_query=show_query, use_single_quotes=use_single_quotes, on_any=on_any, background=background, log_file=log_file)
305
+
306
+ def display_table(self, cols: str, header: str, show_out = True):
307
+ state = self.handler.state
308
+
309
+ if state.pod:
310
+ show_table(state, [state.pod], cols, header, show_out=show_out)
311
+ elif state.sts:
312
+ pod_names = [pod.metadata.name for pod in StatefulSets.pods(state.sts, state.namespace)]
313
+ show_table(state, pod_names, cols, header, show_out=show_out)
314
+
315
+ def nodetool(self, args: str, show_out = True) -> Union[PodExecResult, list[PodExecResult]]:
316
+ state = self.handler.state
317
+ pod = self.handler.pod
318
+
319
+ user, pw = state.user_pass()
320
+ command = f"nodetool -u {user} -pw {pw} {args}"
321
+
322
+ if pod:
323
+ return CassandraNodes.exec(pod, state.namespace, command, show_out=show_out)
324
+ else:
325
+ return CassandraClusters.exec(state.sts, state.namespace, command, action='nodetool', show_out=show_out)
326
+
327
+ class CassandraExecHandler:
328
+ def __init__(self, state: ReplState, pod: str = None):
329
+ self.state = state
330
+ self.pod = pod
331
+ if not pod and state.pod:
332
+ self.pod = state.pod
333
+
334
+ def __enter__(self):
335
+ return CassandraPodService(self)
336
+
337
+ def __exit__(self, exc_type, exc_val, exc_tb):
338
+ return False
339
+
340
+ def cassandra(state: ReplState, pod: str=None):
341
+ return CassandraExecHandler(state, pod=pod)
@@ -25,19 +25,16 @@ class CodeStart(Command):
25
25
  if not(args := self.args(cmd)):
26
26
  return super().run(cmd, state)
27
27
 
28
- state, args = self.apply_state(args, state)
29
- if not self.validate_state(state):
30
- return state
31
-
32
- log2('This will support c3/c3 only for demo.')
28
+ with self.validate(args, state) as (args, state):
29
+ log2('This will support c3/c3 only for demo.')
33
30
 
34
- stop_user_codes(state.namespace)
35
- try:
36
- start_user_code(state.namespace)
37
- finally:
38
31
  stop_user_codes(state.namespace)
32
+ try:
33
+ start_user_code(state.namespace)
34
+ finally:
35
+ stop_user_codes(state.namespace)
39
36
 
40
- return state
37
+ return state
41
38
 
42
39
  def completion(self, state: ReplState):
43
40
  if state.namespace: