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
@@ -1,10 +1,11 @@
1
- from adam.commands.bash.bash import Bash
2
- from adam.commands.command import Command
1
+ from adam.commands.bash.bash_completer import BashCompleter
2
+ from adam.commands.command import Command, InvalidStateException
3
3
  from adam.commands.commands_utils import show_pods, show_rollout
4
+ from adam.commands.cql.utils_cql import cassandra, cassandra_table_names
4
5
  from adam.commands.devices.device import Device
5
6
  from adam.config import Config
6
7
  from adam.repl_state import ReplState
7
- from adam.utils import lines_to_tabular, log, log2
8
+ from adam.utils import lines_to_tabular, log, log2, wait_log
8
9
  from adam.utils_k8s.cassandra_clusters import CassandraClusters
9
10
  from adam.utils_k8s.custom_resources import CustomResources
10
11
  from adam.utils_k8s.kube_context import KubeContext
@@ -41,7 +42,7 @@ class DeviceCass(Command, Device):
41
42
 
42
43
  def ls(self, cmd: str, state: ReplState):
43
44
  if state.pod:
44
- return Bash().run('bash ' + cmd, state)
45
+ return self.bash(state, state, cmd.split(' '))
45
46
  elif state.sts and state.namespace:
46
47
  show_pods(StatefulSets.pods(state.sts, state.namespace), state.namespace, show_namespace=not KubeContext.in_cluster_namespace())
47
48
  show_rollout(state.sts, state.namespace)
@@ -57,6 +58,18 @@ class DeviceCass(Command, Device):
57
58
  else:
58
59
  return {cmd: {n: None for n in StatefulSets.list_sts_names()}}
59
60
 
61
+ def cat(self, cmd: str, state: ReplState):
62
+ if state.pod:
63
+ return self.bash(state, state, cmd.split(' '))
64
+
65
+ raise InvalidStateException()
66
+
67
+ def cat_completion(self, cmd: str, state: ReplState, default: dict = {}):
68
+ if state.sts:
69
+ return {f'@{p}': {cmd: None} for p in StatefulSets.pod_names(state.sts, state.namespace)}
70
+
71
+ return default
72
+
60
73
  def show_statefulsets(self):
61
74
  ss = StatefulSets.list_sts_names()
62
75
  if len(ss) == 0:
@@ -101,7 +114,7 @@ class DeviceCass(Command, Device):
101
114
  if dir in names:
102
115
  state.pod = names[dir]
103
116
  else:
104
- log2('Not a valid pod name or host id.')
117
+ state.pod = dir
105
118
 
106
119
  def cd_completion(self, cmd: str, state: ReplState, default: dict = {}):
107
120
  if state.pod:
@@ -140,6 +153,33 @@ class DeviceCass(Command, Device):
140
153
  if auto_enter == 'first-pod':
141
154
  state.pod = f'{state.sts}-0'
142
155
  if KubeContext().in_cluster_namespace:
143
- Config().wait_log(f'Moving to the only Cassandra cluster: {state.sts}...')
156
+ wait_log(f'Moving to the only Cassandra cluster: {state.sts}...')
144
157
  else:
145
- Config().wait_log(f'Moving to the only Cassandra cluster: {state.sts}@{state.namespace}...')
158
+ wait_log(f'Moving to the only Cassandra cluster: {state.sts}@{state.namespace}...')
159
+
160
+ def show_tables(self, state: ReplState):
161
+ log(lines_to_tabular(cassandra_table_names(state), separator=','))
162
+
163
+ def show_table_preview(self, state: ReplState, table: str, rows: int):
164
+ with cassandra(state) as pods:
165
+ pods.cql(f'select * from {table} limit {rows}', show_out=True, use_single_quotes=True, on_any=True)
166
+
167
+ def bash_target_changed(self, s0: ReplState, s1: ReplState):
168
+ return s0.sts != s1.sts or s0.pod != s1.pod
169
+
170
+ def exec_no_dir(self, command: str, state: ReplState):
171
+ with cassandra(state) as pods:
172
+ return pods.exec(command, action='bash', show_out=True, shell='bash')
173
+
174
+ def exec_with_dir(self, command: str, session_just_created: bool, state: ReplState):
175
+ with cassandra(state) as pods:
176
+ return pods.exec(command, action='bash', show_out=not session_just_created, shell='bash')
177
+
178
+ def bash_completion(self, cmd: str, state: ReplState, default: dict = {}):
179
+ completions = {cmd: BashCompleter(lambda: [])}
180
+
181
+ if state.sts and state.namespace:
182
+ completions |= {f'@{p}': {cmd: BashCompleter(lambda: [])} for p in StatefulSets.pod_names(state.sts, state.namespace)}
183
+
184
+ return completions
185
+
@@ -1,9 +1,9 @@
1
1
  from adam.commands.command import Command
2
2
  from adam.commands.devices.device import Device
3
- from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.commands.export.export_databases import ExportDatabases, export_db
4
4
  from adam.config import Config
5
5
  from adam.repl_state import ReplState
6
- from adam.utils import lines_to_tabular, log, log2
6
+ from adam.utils import lines_to_tabular, log, log2, wait_log
7
7
 
8
8
  class DeviceExport(Command, Device):
9
9
  COMMAND = f'{ReplState.X}:'
@@ -36,16 +36,14 @@ class DeviceExport(Command, Device):
36
36
 
37
37
  def ls(self, cmd: str, state: ReplState):
38
38
  if state.export_session:
39
- self.show_export_tables(state.export_session)
39
+ log(lines_to_tabular(ExportDatabases.table_names(state.export_session), 'NAME', separator=','))
40
40
  else:
41
- self.show_export_databases()
41
+ ExportDatabases.show_databases()
42
42
 
43
- def show_export_databases(self, importer: str = None):
44
- lines = [f'{k}\t{v}' for k, v in ExportDatabases.database_names_with_keyspace_cnt(importer).items()]
45
- log(lines_to_tabular(lines, 'NAME\tKEYSPACES', separator='\t'))
46
-
47
- def show_export_tables(self, export_session: str):
48
- log(lines_to_tabular(ExportDatabases.table_names(export_session), 'NAME', separator=','))
43
+ def show_table_preview(self, state: ReplState, table: str, rows: int):
44
+ if state.export_session:
45
+ with export_db(state) as dbs:
46
+ dbs.sql(f'select * from {table} limit {rows}')
49
47
 
50
48
  def cd(self, dir: str, state: ReplState):
51
49
  if dir in ['', '..']:
@@ -78,7 +76,7 @@ class DeviceExport(Command, Device):
78
76
  def enter(self, state: ReplState):
79
77
  if auto_enter := Config().get('repl.x.auto-enter', 'no'):
80
78
  if auto_enter == 'latest':
81
- Config().wait_log(f'Moving to the latest export database...')
79
+ wait_log(f'Moving to the latest export database...')
82
80
  if dbs := ExportDatabases.database_names():
83
81
  state.export_session = sorted(dbs)[-1]
84
82
  else:
@@ -1,10 +1,11 @@
1
- from adam.commands.command import Command
1
+ from adam.commands.bash.bash_completer import BashCompleter
2
+ from adam.commands.command import Command, InvalidStateException
2
3
  from adam.commands.devices.device import Device
3
4
  from adam.commands.postgres.postgres_context import PostgresContext
4
- from adam.commands.postgres.postgres_utils import pg_database_names, pg_table_names
5
- from adam.config import Config
5
+ from adam.commands.postgres.utils_postgres import pg_database_names, pg_table_names, postgres
6
6
  from adam.repl_state import ReplState
7
- from adam.utils import lines_to_tabular, log
7
+ from adam.utils import lines_to_tabular, log, log2, wait_log
8
+ from adam.utils_k8s.pods import Pods
8
9
 
9
10
  class DevicePostgres(Command, Device):
10
11
  COMMAND = f'{ReplState.P}:'
@@ -45,6 +46,12 @@ class DevicePostgres(Command, Device):
45
46
  else:
46
47
  self.show_pg_hosts(state)
47
48
 
49
+ def cat(self, cmd: str, state: ReplState):
50
+ if state.pod:
51
+ return self.bash(state, state, cmd.split(' '))
52
+
53
+ raise InvalidStateException()
54
+
48
55
  def show_pg_hosts(self, state: ReplState):
49
56
  if state.namespace:
50
57
  def line(pg: PostgresContext):
@@ -105,5 +112,33 @@ class DevicePostgres(Command, Device):
105
112
 
106
113
  return False, None
107
114
 
108
- def enter(self, state: ReplState):
109
- Config().wait_log('Inspecting postgres database instances...')
115
+ def enter(self, _: ReplState):
116
+ wait_log('Inspecting postgres database instances...')
117
+
118
+ def show_tables(self, state: ReplState):
119
+ pg = PostgresContext.apply(state.namespace, state.pg_path)
120
+ lines = [db["name"] for db in pg.tables() if db["schema"] == PostgresContext.default_schema()]
121
+ log(lines_to_tabular(lines, separator=','))
122
+
123
+ def show_table_preview(self, state: ReplState, table: str, rows: int):
124
+ PostgresContext.apply(state.namespace, state.pg_path).run_sql(f'select * from {table} limit {rows}')
125
+
126
+ def bash(self, s0: ReplState, s1: ReplState, args: list[str]):
127
+ pod, container = PostgresContext.pod_and_container(s1.namespace)
128
+ log2(f'Running on {pod}(container:{container})...')
129
+
130
+ return super().bash(s0, s1, args)
131
+
132
+ def bash_target_changed(self, s0: ReplState, s1: ReplState):
133
+ return s0.pg_path != s1.pg_path
134
+
135
+ def exec_no_dir(self, command: str, state: ReplState):
136
+ with postgres(state) as pod:
137
+ return pod.exec(command, show_out=True)
138
+
139
+ def exec_with_dir(self, command: str, session_just_created: bool, state: ReplState):
140
+ with postgres(state) as pod:
141
+ return pod.exec(command, show_out=not session_just_created)
142
+
143
+ def bash_completion(self, cmd: str, state: ReplState, default: dict = {}):
144
+ return {cmd: BashCompleter(lambda: [])}
adam/commands/exit.py CHANGED
@@ -23,10 +23,7 @@ class Exit(Command):
23
23
  exit()
24
24
 
25
25
  def completion(self, state: ReplState):
26
- if state.pod:
27
- return {Exit.COMMAND: None}
28
-
29
- return {}
26
+ return {Exit.COMMAND: None}
30
27
 
31
28
  def help(self, _: ReplState):
32
29
  return f'{Exit.COMMAND}\t exit kaqing <Ctrl-D>'
@@ -0,0 +1,37 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_sessions import ExportSessions
3
+ from adam.repl_state import ReplState, RequiredState
4
+
5
+ class CleanUpAllExportSessions(Command):
6
+ COMMAND = 'clean up all export sessions'
7
+
8
+ # the singleton pattern
9
+ def __new__(cls, *args, **kwargs):
10
+ if not hasattr(cls, 'instance'): cls.instance = super(CleanUpAllExportSessions, cls).__new__(cls)
11
+
12
+ return cls.instance
13
+
14
+ def __init__(self, successor: Command=None):
15
+ super().__init__(successor)
16
+
17
+ def command(self):
18
+ return CleanUpAllExportSessions.COMMAND
19
+
20
+ def required(self):
21
+ return RequiredState.CLUSTER_OR_POD
22
+
23
+ def run(self, cmd: str, state: ReplState):
24
+ if not(args := self.args(cmd)):
25
+ return super().run(cmd, state)
26
+
27
+ with self.validate(args, state) as (args, state):
28
+ if ExportSessions.clean_up_all_sessions(state.sts, state.pod, state.namespace):
29
+ ExportSessions.clear_export_session_cache()
30
+
31
+ return state
32
+
33
+ def completion(self, _: ReplState):
34
+ return {}
35
+
36
+ def help(self, _: ReplState):
37
+ return f'{CleanUpAllExportSessions.COMMAND}\t clean up all export sessions'
@@ -1,9 +1,12 @@
1
+ from adam.commands import validate_args
1
2
  from adam.commands.command import Command
3
+ from adam.commands.export.export_sessions import ExportSessions
2
4
  from adam.commands.export.exporter import Exporter
3
5
  from adam.repl_state import ReplState, RequiredState
6
+ from adam.utils import log, log2
4
7
 
5
8
  class CleanUpExportSessions(Command):
6
- COMMAND = 'clean up all export sessions'
9
+ COMMAND = 'clean up export sessions'
7
10
 
8
11
  # the singleton pattern
9
12
  def __new__(cls, *args, **kwargs):
@@ -24,17 +27,18 @@ class CleanUpExportSessions(Command):
24
27
  if not(args := self.args(cmd)):
25
28
  return super().run(cmd, state)
26
29
 
27
- state, args = self.apply_state(args, state)
28
- if not self.validate_state(state):
29
- return state
30
+ with self.validate(args, state) as (args, state):
31
+ with validate_args(args, state, name='export session') as arg_str:
32
+ sessions = [arg.strip(' ') for arg in arg_str.split(',')]
33
+ csv_cnt, log_cnt = ExportSessions.clean_up_sessions(state.sts, state.pod, state.namespace, sessions)
34
+ log(f'Removed {csv_cnt} csv and {log_cnt} log files.')
30
35
 
31
- if Exporter.clean_up_all_sessions(state.sts, state.pod, state.namespace):
32
- Exporter.clear_export_session_cache()
36
+ ExportSessions.clear_export_session_cache()
33
37
 
34
- return state
38
+ return state
35
39
 
36
40
  def completion(self, _: ReplState):
37
41
  return {}
38
42
 
39
43
  def help(self, _: ReplState):
40
- return f'{CleanUpExportSessions.COMMAND}\t clean up all export sessions'
44
+ return f'{CleanUpExportSessions.COMMAND} <export-session-name>\t clean up export session'
@@ -1,9 +1,7 @@
1
+ from adam.commands import validate_args
1
2
  from adam.commands.command import Command
2
- from adam.commands.export.exporter import Exporter
3
+ from adam.commands.export.export_databases import export_db
3
4
  from adam.repl_state import ReplState
4
- from adam.utils import log2
5
- from adam.utils_athena import Athena
6
- from adam.utils_sqlite import SQLite
7
5
 
8
6
  class DropExportDatabase(Command):
9
7
  COMMAND = 'drop export database'
@@ -27,29 +25,12 @@ class DropExportDatabase(Command):
27
25
  if not(args := self.args(cmd)):
28
26
  return super().run(cmd, state)
29
27
 
30
- state, args = self.apply_state(args, state)
31
- if not self.validate_state(state):
32
- return state
28
+ with self.validate(args, state) as (args, state):
29
+ with validate_args(args, state, name='database name') as db:
30
+ with export_db(state) as dbs:
31
+ dbs.drop(args[0])
33
32
 
34
- if not len(args):
35
- if state.in_repl:
36
- log2('Database name is required.')
37
- log2()
38
- else:
39
- log2('* Database name is missing.')
40
- Command.display_help()
41
-
42
- return 'command-missing'
43
-
44
- Exporter.drop_databases(state.sts, state.pod, state.namespace, args[0])
45
-
46
- SQLite.clear_cache()
47
- Athena.clear_cache()
48
-
49
- if state.export_session == args[0]:
50
- state.export_session = None
51
-
52
- return state
33
+ return state
53
34
 
54
35
  def completion(self, _: ReplState):
55
36
  return {}
@@ -1,8 +1,6 @@
1
1
  from adam.commands.command import Command
2
- from adam.commands.export.exporter import Exporter
2
+ from adam.commands.export.export_databases import export_db
3
3
  from adam.repl_state import ReplState
4
- from adam.utils_athena import Athena
5
- from adam.utils_sqlite import SQLite
6
4
 
7
5
  class DropExportDatabases(Command):
8
6
  COMMAND = 'drop all export databases'
@@ -26,18 +24,11 @@ class DropExportDatabases(Command):
26
24
  if not(args := self.args(cmd)):
27
25
  return super().run(cmd, state)
28
26
 
29
- state, args = self.apply_state(args, state)
30
- if not self.validate_state(state):
31
- return state
32
-
33
- Exporter.drop_databases(state.sts, state.pod, state.namespace)
34
-
35
- SQLite.clear_cache()
36
- Athena.clear_cache()
27
+ with self.validate(args, state) as (args, state):
28
+ with export_db(state) as dbs:
29
+ dbs.drop_all()
37
30
 
38
- state.export_session = None
39
-
40
- return state
31
+ return state
41
32
 
42
33
  def completion(self, _: ReplState):
43
34
  return {}
@@ -1,8 +1,8 @@
1
+ from adam.commands import extract_options
1
2
  from adam.commands.command import Command
2
- from adam.commands.cql.cql_utils import cassandra_keyspaces, cassandra_table_names
3
+ from adam.commands.cql.utils_cql import cassandra_keyspaces, cassandra_table_names
3
4
  from adam.commands.export.export_databases import ExportDatabases
4
- from adam.commands.export.exporter import Exporter
5
- from adam.commands.export.utils_export import ExportSpec
5
+ from adam.commands.export.exporter import export
6
6
  from adam.repl_state import ReplState, RequiredState
7
7
  from adam.sql.sql_completer import SqlCompleter, SqlVariant
8
8
  from adam.utils import log
@@ -30,40 +30,10 @@ class ExportTables(Command):
30
30
  if not(args := self.args(cmd)):
31
31
  return super().run(cmd, state)
32
32
 
33
- state, args = self.apply_state(args, state)
34
- if not self.validate_state(state):
35
- return state
36
-
37
- # --export-only for testing only
38
- args, export_only = Command.extract_options(args, ['--export-only'])
39
-
40
- if not state.pod:
41
- state.push()
42
- state.pod = StatefulSets.pod_names(state.sts, state.namespace)[0]
43
-
44
- export_session = state.export_session
45
- spec: ExportSpec = None
46
- try:
47
- statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
48
- if not statuses:
49
- return state
50
-
51
- Exporter.clear_export_session_cache()
52
-
53
- if spec.importer == 'csv' or export_only:
54
- ExportDatabases.disply_export_session(state.sts, state.pod, state.namespace, spec.session)
55
- else:
56
- log()
57
- ExportDatabases.display_export_db(state.export_session)
58
-
59
- finally:
60
- state.pop()
61
-
62
- # if exporting to csv, do not bind the new session id to repl state
63
- if spec and spec.importer == 'csv':
64
- state.export_session = export_session
65
-
66
- return state
33
+ with self.validate(args, state) as (args, state):
34
+ with extract_options(args, '--export-only') as (args, export_only):
35
+ with export(state) as exporter:
36
+ return exporter.export(args, export_only=export_only)
67
37
 
68
38
  def completion(self, state: ReplState):
69
39
  def sc():
@@ -74,7 +44,7 @@ class ExportTables(Command):
74
44
  'export-dbs': lambda: ExportDatabases.database_names(),
75
45
  }, variant=SqlVariant.CQL)
76
46
 
77
- if state.sts:
47
+ if super().completion(state):
78
48
  return {f'@{p}': {ExportTables.COMMAND: sc()} for p in StatefulSets.pod_names(state.sts, state.namespace)}
79
49
 
80
50
  return {}
@@ -1,26 +1,36 @@
1
+ from collections.abc import Callable
1
2
  import os
2
3
  import boto3
3
4
 
4
- from adam.commands.export.utils_export import ExportTableStatus
5
+ from adam.commands.export.export_sessions import ExportSessions
6
+ from adam.commands.export.importer import Importer
5
7
  from adam.config import Config
6
- from adam.utils import lines_to_tabular, log, log2, ing
8
+ from adam.repl_session import ReplSession
9
+ from adam.repl_state import ReplState
10
+ from adam.utils import debug, lines_to_tabular, log, log2, ing, log_exc
7
11
  from adam.utils_athena import Athena
8
- from adam.utils_k8s.statefulsets import StatefulSets
9
12
  from adam.utils_sqlite import SQLite
10
13
 
11
14
  LIKE = 'e%_%'
12
15
 
13
16
  class ExportDatabases:
14
- def run_query(query: str, database: str, show_query=False) -> int:
17
+ def run_query(query: str, database: str, show_query=False, output: Callable[[str], str] = None) -> int:
15
18
  cnt: int = 0
16
19
 
17
20
  if show_query:
18
21
  log2(query)
19
22
 
23
+ log_file = None
20
24
  if database.startswith('s'):
21
- cnt += SQLite.run_query(query, database=database)
25
+ cnt0, log_file = SQLite.run_query(query, database=database, output=output)
26
+ cnt += cnt0
22
27
  else:
23
- cnt += Athena.run_query(query, database=database)
28
+ cnt0, log_file = Athena.run_query(query, database=database, output=output)
29
+ cnt += cnt0
30
+
31
+ if Config().get('repl.history.push-cat-log-file', True):
32
+ if log_file and ReplSession().prompt_session:
33
+ ReplSession().prompt_session.history.append_string(f':sh cat {log_file}')
24
34
 
25
35
  return cnt
26
36
 
@@ -47,15 +57,13 @@ class ExportDatabases:
47
57
  dbs = SQLite.database_names(db)
48
58
  if dbs:
49
59
  with ing(f'Droping {len(dbs)} SQLite databases'):
50
- try:
60
+ with log_exc():
51
61
  for db in dbs:
52
62
  file_path = f'{SQLite.local_db_dir()}/{db}'
53
63
  try:
54
64
  os.remove(file_path)
55
65
  except OSError as e:
56
66
  pass
57
- except:
58
- pass
59
67
 
60
68
  return dbs
61
69
 
@@ -65,20 +73,17 @@ class ExportDatabases:
65
73
  with ing(f'Droping {len(dbs)} Athena databases'):
66
74
  for db in dbs:
67
75
  query = f'DROP DATABASE {db} CASCADE'
68
- if Config().is_debug():
69
- log2(query)
76
+ debug(query)
70
77
  Athena.query(query)
71
78
 
72
79
  with ing(f'Deleting s3 folder: export'):
73
- try:
80
+ with log_exc():
74
81
  if not db:
75
82
  db = ''
76
83
 
77
84
  s3 = boto3.resource('s3')
78
85
  bucket = s3.Bucket(Config().get('export.bucket', 'c3.ops--qing'))
79
86
  bucket.objects.filter(Prefix=f'export/{db}').delete()
80
- except:
81
- pass
82
87
 
83
88
  return dbs
84
89
 
@@ -86,7 +91,7 @@ class ExportDatabases:
86
91
  if not export_session:
87
92
  return
88
93
 
89
- Athena.clear_cache()
94
+ ExportDatabases.clear_cache()
90
95
 
91
96
  keyspaces = {}
92
97
  for table in ExportDatabases.table_names(export_session):
@@ -98,17 +103,6 @@ class ExportDatabases:
98
103
 
99
104
  log(lines_to_tabular([f'{k},{v}' for k, v in keyspaces.items()], header='SCHEMA,# of TABLES', separator=','))
100
105
 
101
- def disply_export_session(sts: str, pod: str, namespace: str, session: str):
102
- if not pod:
103
- pod = StatefulSets.pod_names(sts, namespace)[0]
104
-
105
- if not pod:
106
- return
107
-
108
- tables, _ = ExportTableStatus.from_session(sts, pod, namespace, session)
109
- log()
110
- log(lines_to_tabular([f'{table.keyspace}\t{table.table}\t{table.target_table}\t{"export_completed_pending_import" if table.status == "pending_import" else table.status}' for table in tables], header='KEYSPACE\tTABLE\tTARGET_TABLE\tSTATUS', separator='\t'))
111
-
112
106
  def database_names():
113
107
  return ExportDatabases.copy_database_names() + ExportDatabases.export_database_names()
114
108
 
@@ -167,4 +161,69 @@ class ExportDatabases:
167
161
  if db.startswith('s'):
168
162
  return SQLite.database_names(prefix=f'{eprefix}_')
169
163
  else:
170
- return Athena.database_names(like=f'{eprefix}_%')
164
+ return Athena.database_names(like=f'{eprefix}_%')
165
+
166
+ def drop_databases(sts: str, pod: str, namespace: str, database: str = None):
167
+ importer = None
168
+ if database:
169
+ importer = Importer.importer_from_session(database)
170
+
171
+ sessions_done = ExportSessions.export_session_names(sts, pod, namespace, importer=importer, export_state='done')
172
+ sessions = ExportDatabases.sessions_from_dbs(ExportDatabases.drop_export_dbs(database))
173
+ if sessions_done and sessions:
174
+ intersects = list(set(sessions_done) & set(sessions))
175
+ with ing(f'Cleaning up {len(intersects)} completed sessions'):
176
+ ExportSessions.clean_up_sessions(sts, pod, namespace, list(intersects))
177
+ ExportSessions.clear_export_session_cache()
178
+
179
+ def clear_cache(database: str = None):
180
+ if not database or database.startswith('s'):
181
+ SQLite.clear_cache()
182
+ if not database or database.startswith('e'):
183
+ Athena.clear_cache()
184
+
185
+ def show_databases(importer: str = None):
186
+ lines = [f'{k}\t{v}' for k, v in ExportDatabases.database_names_with_keyspace_cnt(importer).items()]
187
+ log(lines_to_tabular(lines, 'NAME\tKEYSPACES', separator='\t'))
188
+
189
+ class ExportDatabaseService:
190
+ def __init__(self, handler: 'ExportDatabaseHandler'):
191
+ self.handler = handler
192
+
193
+ def sql(self, query: str, database: str = None):
194
+ if not database:
195
+ database = self.handler.state.export_session
196
+
197
+ ExportDatabases.run_query(query, database, show_query = True)
198
+
199
+ def drop(self, database: str):
200
+ state = self.handler.state
201
+
202
+ ExportDatabases.drop_databases(state.sts, state.pod, state.namespace, database)
203
+ ExportDatabases.clear_cache(database)
204
+ if state.export_session == database:
205
+ state.export_session = None
206
+
207
+ def drop_all(self):
208
+ state = self.handler.state
209
+
210
+ ExportDatabases.drop_databases(state.sts, state.pod, state.namespace)
211
+ ExportDatabases.clear_cache()
212
+
213
+ state.export_session = None
214
+
215
+ def show_databases(self, importer: str = None):
216
+ ExportDatabases.show_databases(importer)
217
+
218
+ class ExportDatabaseHandler:
219
+ def __init__(self, state: ReplState = None):
220
+ self.state = state
221
+
222
+ def __enter__(self):
223
+ return ExportDatabaseService(self)
224
+
225
+ def __exit__(self, exc_type, exc_val, exc_tb):
226
+ return False
227
+
228
+ def export_db(state: ReplState = None):
229
+ return ExportDatabaseHandler(state)