kaqing 2.0.110__py3-none-any.whl → 2.0.184__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 (204) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +9 -12
  3. adam/apps.py +18 -4
  4. adam/batch.py +5 -5
  5. adam/checks/check_utils.py +16 -46
  6. adam/checks/cpu.py +7 -1
  7. adam/checks/cpu_metrics.py +52 -0
  8. adam/checks/disk.py +2 -3
  9. adam/columns/columns.py +3 -1
  10. adam/columns/cpu.py +3 -1
  11. adam/columns/cpu_metrics.py +22 -0
  12. adam/columns/memory.py +3 -4
  13. adam/commands/__init__.py +24 -0
  14. adam/commands/alter_tables.py +33 -48
  15. adam/commands/app/__init__.py +0 -0
  16. adam/commands/app/app.py +38 -0
  17. adam/commands/{app_ping.py → app/app_ping.py} +7 -13
  18. adam/commands/app/show_app_actions.py +49 -0
  19. adam/commands/{show → app}/show_app_id.py +8 -11
  20. adam/commands/{show → app}/show_app_queues.py +7 -14
  21. adam/commands/app/utils_app.py +98 -0
  22. adam/commands/audit/audit.py +27 -31
  23. adam/commands/audit/audit_repair_tables.py +14 -18
  24. adam/commands/audit/audit_run.py +16 -23
  25. adam/commands/audit/show_last10.py +4 -17
  26. adam/commands/audit/show_slow10.py +4 -17
  27. adam/commands/audit/show_top10.py +4 -16
  28. adam/commands/audit/utils_show_top10.py +15 -3
  29. adam/commands/bash/__init__.py +5 -0
  30. adam/commands/bash/bash.py +36 -0
  31. adam/commands/bash/bash_completer.py +93 -0
  32. adam/commands/bash/utils_bash.py +16 -0
  33. adam/commands/cat.py +36 -0
  34. adam/commands/cd.py +11 -95
  35. adam/commands/check.py +15 -24
  36. adam/commands/cli_commands.py +2 -3
  37. adam/commands/clipboard_copy.py +86 -0
  38. adam/commands/code.py +57 -0
  39. adam/commands/command.py +198 -40
  40. adam/commands/commands_utils.py +12 -27
  41. adam/commands/cql/cql_completions.py +27 -10
  42. adam/commands/cql/cqlsh.py +12 -30
  43. adam/commands/cql/utils_cql.py +297 -0
  44. adam/commands/deploy/code_start.py +7 -10
  45. adam/commands/deploy/code_stop.py +4 -21
  46. adam/commands/deploy/code_utils.py +3 -3
  47. adam/commands/deploy/deploy.py +4 -27
  48. adam/commands/deploy/deploy_frontend.py +14 -17
  49. adam/commands/deploy/deploy_pg_agent.py +3 -6
  50. adam/commands/deploy/deploy_pod.py +65 -73
  51. adam/commands/deploy/deploy_utils.py +14 -24
  52. adam/commands/deploy/undeploy.py +4 -27
  53. adam/commands/deploy/undeploy_frontend.py +4 -7
  54. adam/commands/deploy/undeploy_pg_agent.py +6 -8
  55. adam/commands/deploy/undeploy_pod.py +11 -12
  56. adam/commands/devices/__init__.py +0 -0
  57. adam/commands/devices/device.py +123 -0
  58. adam/commands/devices/device_app.py +163 -0
  59. adam/commands/devices/device_auit_log.py +49 -0
  60. adam/commands/devices/device_cass.py +179 -0
  61. adam/commands/devices/device_export.py +84 -0
  62. adam/commands/devices/device_postgres.py +150 -0
  63. adam/commands/devices/devices.py +25 -0
  64. adam/commands/download_file.py +47 -0
  65. adam/commands/exit.py +1 -4
  66. adam/commands/export/__init__.py +0 -0
  67. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  68. adam/commands/export/clean_up_export_sessions.py +39 -0
  69. adam/commands/export/download_export_session.py +39 -0
  70. adam/commands/export/drop_export_database.py +39 -0
  71. adam/commands/export/drop_export_databases.py +37 -0
  72. adam/commands/export/export.py +53 -0
  73. adam/commands/export/export_databases.py +245 -0
  74. adam/commands/export/export_select.py +59 -0
  75. adam/commands/export/export_select_x.py +54 -0
  76. adam/commands/export/export_sessions.py +209 -0
  77. adam/commands/export/export_use.py +49 -0
  78. adam/commands/export/exporter.py +332 -0
  79. adam/commands/export/import_files.py +44 -0
  80. adam/commands/export/import_session.py +44 -0
  81. adam/commands/export/importer.py +81 -0
  82. adam/commands/export/importer_athena.py +177 -0
  83. adam/commands/export/importer_sqlite.py +67 -0
  84. adam/commands/export/show_column_counts.py +45 -0
  85. adam/commands/export/show_export_databases.py +38 -0
  86. adam/commands/export/show_export_session.py +39 -0
  87. adam/commands/export/show_export_sessions.py +37 -0
  88. adam/commands/export/utils_export.py +343 -0
  89. adam/commands/find_files.py +51 -0
  90. adam/commands/find_processes.py +76 -0
  91. adam/commands/head.py +36 -0
  92. adam/commands/help.py +5 -3
  93. adam/commands/intermediate_command.py +49 -0
  94. adam/commands/issues.py +11 -43
  95. adam/commands/kubectl.py +38 -0
  96. adam/commands/login.py +22 -24
  97. adam/commands/logs.py +3 -6
  98. adam/commands/ls.py +11 -116
  99. adam/commands/medusa/medusa.py +4 -22
  100. adam/commands/medusa/medusa_backup.py +20 -27
  101. adam/commands/medusa/medusa_restore.py +38 -37
  102. adam/commands/medusa/medusa_show_backupjobs.py +16 -18
  103. adam/commands/medusa/medusa_show_restorejobs.py +13 -18
  104. adam/commands/nodetool.py +11 -17
  105. adam/commands/param_get.py +11 -14
  106. adam/commands/param_set.py +8 -12
  107. adam/commands/postgres/postgres.py +45 -46
  108. adam/commands/postgres/postgres_databases.py +269 -0
  109. adam/commands/postgres/postgres_ls.py +4 -8
  110. adam/commands/postgres/postgres_preview.py +5 -9
  111. adam/commands/postgres/psql_completions.py +4 -3
  112. adam/commands/postgres/utils_postgres.py +70 -0
  113. adam/commands/preview_table.py +8 -44
  114. adam/commands/pwd.py +14 -46
  115. adam/commands/reaper/reaper.py +4 -27
  116. adam/commands/reaper/reaper_forward.py +49 -56
  117. adam/commands/reaper/reaper_forward_session.py +6 -0
  118. adam/commands/reaper/reaper_forward_stop.py +10 -16
  119. adam/commands/reaper/reaper_restart.py +7 -14
  120. adam/commands/reaper/reaper_run_abort.py +8 -33
  121. adam/commands/reaper/reaper_runs.py +43 -58
  122. adam/commands/reaper/reaper_runs_abort.py +29 -49
  123. adam/commands/reaper/reaper_schedule_activate.py +9 -32
  124. adam/commands/reaper/reaper_schedule_start.py +9 -32
  125. adam/commands/reaper/reaper_schedule_stop.py +9 -32
  126. adam/commands/reaper/reaper_schedules.py +4 -14
  127. adam/commands/reaper/reaper_status.py +8 -16
  128. adam/commands/reaper/utils_reaper.py +194 -0
  129. adam/commands/repair/repair.py +4 -22
  130. adam/commands/repair/repair_log.py +5 -11
  131. adam/commands/repair/repair_run.py +27 -34
  132. adam/commands/repair/repair_scan.py +32 -38
  133. adam/commands/repair/repair_stop.py +5 -11
  134. adam/commands/report.py +27 -29
  135. adam/commands/restart.py +25 -26
  136. adam/commands/rollout.py +19 -24
  137. adam/commands/shell.py +12 -4
  138. adam/commands/show/show.py +10 -25
  139. adam/commands/show/show_adam.py +3 -3
  140. adam/commands/show/show_cassandra_repairs.py +35 -0
  141. adam/commands/show/show_cassandra_status.py +33 -51
  142. adam/commands/show/show_cassandra_version.py +5 -18
  143. adam/commands/show/show_commands.py +20 -25
  144. adam/commands/show/show_host.py +1 -1
  145. adam/commands/show/show_login.py +20 -27
  146. adam/commands/show/show_params.py +2 -5
  147. adam/commands/show/show_processes.py +15 -19
  148. adam/commands/show/show_storage.py +10 -20
  149. adam/commands/watch.py +26 -29
  150. adam/config.py +5 -14
  151. adam/embedded_params.py +1 -1
  152. adam/log.py +4 -4
  153. adam/pod_exec_result.py +6 -3
  154. adam/repl.py +69 -115
  155. adam/repl_commands.py +52 -19
  156. adam/repl_state.py +161 -40
  157. adam/sql/sql_completer.py +52 -27
  158. adam/sql/sql_state_machine.py +131 -19
  159. adam/sso/authn_ad.py +6 -8
  160. adam/sso/authn_okta.py +4 -6
  161. adam/sso/cred_cache.py +3 -5
  162. adam/sso/idp.py +9 -12
  163. adam/utils.py +511 -9
  164. adam/utils_athena.py +145 -0
  165. adam/utils_audits.py +12 -103
  166. adam/utils_issues.py +32 -0
  167. adam/utils_k8s/app_clusters.py +28 -0
  168. adam/utils_k8s/app_pods.py +36 -0
  169. adam/utils_k8s/cassandra_clusters.py +30 -19
  170. adam/utils_k8s/cassandra_nodes.py +3 -3
  171. adam/utils_k8s/custom_resources.py +16 -17
  172. adam/utils_k8s/ingresses.py +2 -2
  173. adam/utils_k8s/jobs.py +7 -11
  174. adam/utils_k8s/k8s.py +87 -0
  175. adam/utils_k8s/kube_context.py +2 -2
  176. adam/utils_k8s/pods.py +89 -78
  177. adam/utils_k8s/secrets.py +4 -4
  178. adam/utils_k8s/service_accounts.py +5 -4
  179. adam/utils_k8s/services.py +2 -2
  180. adam/utils_k8s/statefulsets.py +1 -12
  181. adam/utils_local.py +4 -0
  182. adam/utils_net.py +4 -4
  183. adam/utils_repl/__init__.py +0 -0
  184. adam/utils_repl/automata_completer.py +48 -0
  185. adam/utils_repl/repl_completer.py +46 -0
  186. adam/utils_repl/state_machine.py +173 -0
  187. adam/utils_sqlite.py +137 -0
  188. adam/version.py +1 -1
  189. {kaqing-2.0.110.dist-info → kaqing-2.0.184.dist-info}/METADATA +1 -1
  190. kaqing-2.0.184.dist-info/RECORD +244 -0
  191. adam/commands/app.py +0 -67
  192. adam/commands/bash.py +0 -150
  193. adam/commands/cp.py +0 -95
  194. adam/commands/cql/cql_utils.py +0 -112
  195. adam/commands/devices.py +0 -118
  196. adam/commands/postgres/postgres_context.py +0 -239
  197. adam/commands/postgres/postgres_utils.py +0 -31
  198. adam/commands/reaper/reaper_session.py +0 -159
  199. adam/commands/show/show_app_actions.py +0 -56
  200. adam/commands/show/show_repairs.py +0 -47
  201. kaqing-2.0.110.dist-info/RECORD +0 -187
  202. {kaqing-2.0.110.dist-info → kaqing-2.0.184.dist-info}/WHEEL +0 -0
  203. {kaqing-2.0.110.dist-info → kaqing-2.0.184.dist-info}/entry_points.txt +0 -0
  204. {kaqing-2.0.110.dist-info → kaqing-2.0.184.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,49 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.export_databases import ExportDatabases, export_db
4
+ from adam.repl_state import ReplState
5
+ from adam.utils import log2
6
+
7
+ class ExportUse(Command):
8
+ COMMAND = 'use'
9
+
10
+ # the singleton pattern
11
+ def __new__(cls, *args, **kwargs):
12
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportUse, cls).__new__(cls)
13
+
14
+ return cls.instance
15
+
16
+ def __init__(self, successor: Command=None):
17
+ super().__init__(successor)
18
+
19
+ def command(self):
20
+ return ExportUse.COMMAND
21
+
22
+ def required(self):
23
+ return [ReplState.C, ReplState.X]
24
+
25
+ def run(self, cmd: str, state: ReplState):
26
+ if not(args := self.args(cmd)):
27
+ return super().run(cmd, state)
28
+
29
+ with self.validate(args, state) as (args, state):
30
+ with validate_args(args, state, at_least=0) as session:
31
+ if not session:
32
+ state.export_session = None
33
+
34
+ log2('Export database is unset.')
35
+
36
+ return state
37
+
38
+ state.export_session = session
39
+
40
+ with export_db(state) as dbs:
41
+ dbs.show_database()
42
+
43
+ return state
44
+
45
+ def completion(self, state: ReplState):
46
+ return super().completion(state, {n: None for n in ExportDatabases.database_names()})
47
+
48
+ def help(self, _: ReplState):
49
+ return f'{ExportUse.COMMAND} <export-database-name>\t use export database'
@@ -0,0 +1,332 @@
1
+ from datetime import datetime
2
+ import time
3
+
4
+ from adam.commands.command import InvalidArgumentsException
5
+ from adam.commands.cql.utils_cql import cassandra_table_names, run_cql, table_spec
6
+ from adam.commands.export.export_databases import export_db
7
+ from adam.commands.export.export_sessions import ExportSessions
8
+ from adam.commands.export.importer import Importer
9
+ from adam.commands.export.importer_athena import AthenaImporter
10
+ from adam.commands.export.importer_sqlite import SqliteImporter
11
+ from adam.commands.export.utils_export import ExportSpec, ExportTableStatus, ExportTableSpec, ImportSpec, csv_dir, find_files, state_with_pod
12
+ from adam.config import Config
13
+ from adam.pod_exec_result import PodExecResult
14
+ from adam.repl_state import ReplState
15
+ from adam.utils import debug, log, parallelize, log2, ing, log_exc
16
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
17
+ from adam.utils_k8s.pods import log_prefix
18
+
19
+ class Exporter:
20
+ def export_tables(args: list[str], state: ReplState, export_only: bool = False, max_workers = 0) -> tuple[list[str], ExportSpec]:
21
+ if export_only:
22
+ log2('export-only for testing')
23
+
24
+ spec: ExportSpec = None
25
+ with log_exc(True):
26
+ spec = Exporter.export_spec(' '.join(args), state)
27
+
28
+ statuses, spec = Exporter._export_tables(spec, state, max_workers=max_workers, export_state='init')
29
+ if not statuses:
30
+ return statuses, spec
31
+
32
+ return Exporter._export_tables(spec, state, export_only, max_workers, 'pending_export')
33
+
34
+ return [], None
35
+
36
+ def export_spec(spec_str: str, state: ReplState):
37
+ spec: ExportSpec = ExportSpec.parse_specs(spec_str)
38
+
39
+ session = state.export_session
40
+ if session:
41
+ if spec.importer:
42
+ importer_from_session = Importer.importer_from_session(session)
43
+ if spec.importer != importer_from_session:
44
+ if spec.importer == 'csv':
45
+ prefix = Importer.prefix_from_importer(spec.importer)
46
+ session = f'{prefix}{session[1:]}'
47
+ else:
48
+ raise Exception(f"You're currently using {importer_from_session} export database. You cannot export tables with {spec.importer} type database.")
49
+ else:
50
+ spec.importer = Importer.importer_from_session(session)
51
+
52
+ if spec.importer == 'athena' and not AthenaImporter.ping():
53
+ raise Exception('Credentials for Athena is not present.')
54
+ else:
55
+ if not spec.importer:
56
+ spec.importer = Config().get('export.default-importer', 'sqlite')
57
+
58
+ prefix = Importer.prefix_from_importer(spec.importer)
59
+ session = f'{prefix}{datetime.now().strftime("%Y%m%d%H%M%S")[3:]}'
60
+
61
+ if spec.importer == 'athena' and not AthenaImporter.ping():
62
+ raise Exception('Credentials for Athena is not present.')
63
+
64
+ if spec.importer != 'csv':
65
+ state.export_session = session
66
+
67
+ spec.session = session
68
+
69
+ return spec
70
+
71
+ def import_session(spec_str: str, state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
72
+ import_spec: ImportSpec = None
73
+ with log_exc(True):
74
+ import_spec = Exporter.import_spec(spec_str, state)
75
+ tables, status_in_whole = ExportTableStatus.from_session(state.sts, state.pod, state.namespace, import_spec.session)
76
+ if status_in_whole == 'done':
77
+ log2(f'The session has been completely done - no more csv files are found.')
78
+ return [], ExportSpec(None, None, importer=import_spec.importer, tables=[])
79
+
80
+ spec = ExportSpec(None, None, importer=import_spec.importer, tables=[ExportTableSpec.from_status(table) for table in tables], session=import_spec.session)
81
+
82
+ return Exporter._export_tables(spec, state, max_workers=max_workers, export_state = 'import')
83
+
84
+ return [], None
85
+
86
+ def import_local_csv_files(spec_str: str, state: ReplState, max_workers = 0) -> tuple[list[str], ExportSpec]:
87
+ spec: ImportSpec = None
88
+ with log_exc(True):
89
+ spec = Exporter.import_spec(spec_str, state, files=True)
90
+ if not spec.table_name:
91
+ log2(f"Use 'as <database-name>.<table-name>'.")
92
+ raise InvalidArgumentsException()
93
+
94
+ d_t = spec.table_name.split('.')
95
+ if len(d_t) != 2:
96
+ log2(f'Need <database-name>.<table-name> format for target table.')
97
+ raise InvalidArgumentsException()
98
+
99
+ database = d_t[0]
100
+ table = d_t[1]
101
+ im = AthenaImporter() if spec.importer == 'athena' else SqliteImporter()
102
+
103
+ with parallelize(spec.files, max_workers, msg='Importing|Imported {size} csv files') as exec:
104
+ return exec.map(lambda f: im.import_from_local_csv(state, database, table, f, len(spec.files) > 1, True)), spec
105
+
106
+ return [], None
107
+
108
+ def import_spec(spec_str: str, state: ReplState, files = False):
109
+ spec: ImportSpec = ImportSpec.parse_specs(spec_str, files=files)
110
+
111
+ session = state.export_session
112
+ if session:
113
+ if spec.importer:
114
+ importer = Importer.importer_from_session(state.export_session)
115
+ if spec.importer != importer:
116
+ raise Exception(f"You're currently using {importer} export database. You cannot import to {spec.importer} type database.")
117
+ else:
118
+ spec.importer = Importer.importer_from_session(state.export_session)
119
+ if not spec.importer:
120
+ spec.importer = Config().get('export.default-importer', 'sqlite')
121
+
122
+ if spec.importer == 'athena' and not AthenaImporter.ping():
123
+ raise Exception('Credentials for Athena is not present.')
124
+ else:
125
+ if not spec.importer:
126
+ spec.importer = Importer.importer_from_session(spec.session)
127
+
128
+ if not spec.importer or spec.importer == 'csv':
129
+ spec.importer = Config().get('export.default-importer', 'sqlite')
130
+
131
+ if spec.importer == 'athena' and not AthenaImporter.ping():
132
+ raise Exception('Credentials for Athena is not present.')
133
+
134
+ prefix = Importer.prefix_from_importer(spec.importer)
135
+ if spec.session:
136
+ spec.session = f'{prefix}{spec.session[1:]}'
137
+ else:
138
+ spec.session = f'{prefix}{datetime.now().strftime("%Y%m%d%H%M%S")[3:]}'
139
+
140
+ state.export_session = spec.session
141
+
142
+ return spec
143
+
144
+ def _export_tables(spec: ExportSpec, state: ReplState, export_only = False, max_workers = 0, export_state = None) -> tuple[list[str], ExportSpec]:
145
+ if not spec.keyspace:
146
+ spec.keyspace = f'{state.namespace}_db'
147
+
148
+ if not spec.tables:
149
+ spec.tables = [ExportTableSpec.parse(t) for t in cassandra_table_names(state, keyspace=spec.keyspace)]
150
+
151
+ if not max_workers:
152
+ max_workers = Config().action_workers(f'export.{spec.importer}', 8)
153
+
154
+ if export_state == 'init':
155
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -rf {csv_dir()}/{spec.session}_*', show_out=Config().is_debug(), shell='bash')
156
+
157
+ action = f'[{spec.session}] Exporting|Exported'
158
+ if export_state == 'init':
159
+ action = f'[{spec.session}] Preparing|Prepared'
160
+ elif export_state == 'import':
161
+ action = f'[{spec.session}] Importing|Imported'
162
+
163
+ with parallelize(spec.tables, max_workers, msg=action + ' {size} Cassandra tables') as exec:
164
+ return exec.map(lambda table: Exporter.export_table(table, state, spec.session, spec.importer, export_only, len(spec.tables) > 1, consistency=spec.consistency, export_state=export_state)), spec
165
+
166
+ def export_table(spec: ExportTableSpec, state: ReplState, session: str, importer: str, export_only = False, multi_tables = True, consistency: str = None, export_state=None):
167
+ s: str = None
168
+
169
+ table, target_table, columns = Exporter.resove_table_n_columns(spec, state, include_ks_in_target=False, importer=importer)
170
+
171
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
172
+ create_db = not state.export_session
173
+
174
+ if export_state == 'init':
175
+ Exporter.create_table_log(spec, state, session, table, target_table)
176
+ return 'table_log_created'
177
+ else:
178
+ if export_state == 'pending_export':
179
+ Exporter.export_to_csv(spec, state, session, table, target_table, columns, multi_tables=multi_tables, consistency=consistency)
180
+
181
+ log_files: list[str] = find_files(state.pod, state.namespace, f'{log_file}*')
182
+ if not log_files:
183
+ return s
184
+
185
+ log_file = log_files[0]
186
+
187
+ status: ExportTableStatus = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
188
+ while status.status != 'done':
189
+ if status.status == 'export_in_pregress':
190
+ debug('Exporting to CSV is still in progess, sleeping for 1 sec...')
191
+ time.sleep(1)
192
+ elif status.status == 'exported':
193
+ log_file = Exporter.rename_to_pending_import(spec, state, session, target_table)
194
+ if importer == 'csv' or export_only:
195
+ return 'pending_import'
196
+ elif status.status == 'pending_import':
197
+ log_file, session = Exporter.import_from_csv(spec, state, session, importer, table, target_table, columns, multi_tables=multi_tables, create_db=create_db)
198
+
199
+ status = ExportTableStatus.from_log_file(state.pod, state.namespace, session, log_file)
200
+
201
+ return status.status
202
+
203
+ def create_table_log(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str):
204
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
205
+
206
+ CassandraNodes.exec(state.pod, state.namespace, f'rm -f {log_file}* && touch {log_file}', show_out=Config().is_debug(), shell='bash')
207
+
208
+ return table
209
+
210
+ def export_to_csv(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str, columns: str, multi_tables = True, consistency: str = None):
211
+ db = f'{session}_{target_table}'
212
+
213
+ CassandraNodes.exec(state.pod, state.namespace, f'mkdir -p {csv_dir()}/{db}', show_out=Config().is_debug(), shell='bash')
214
+ csv_file = f'{csv_dir()}/{db}/{table}.csv'
215
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
216
+
217
+ suppress_ing_log = Config().is_debug() or multi_tables
218
+ queries = []
219
+ if consistency:
220
+ queries.append(f'CONSISTENCY {consistency}')
221
+ queries.append(f"COPY {spec.keyspace}.{table}({columns}) TO '{csv_file}' WITH HEADER = TRUE")
222
+ r: PodExecResult = ing(
223
+ f'[{session}] Dumping table {spec.keyspace}.{table}{f" with consistency {consistency}" if consistency else ""}',
224
+ lambda: run_cql(state, ';'.join(queries), show_out=Config().is_debug(), backgrounded=True, log_file=log_file),
225
+ suppress_log=suppress_ing_log)
226
+
227
+ return log_file
228
+
229
+ def rename_to_pending_import(spec: ExportTableSpec, state: ReplState, session: str, target_table: str):
230
+ log_file = f'{log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
231
+ to = f'{log_file}.pending_import'
232
+
233
+ CassandraNodes.exec(state.pod, state.namespace, f'mv {log_file} {to}', show_out=Config().is_debug(), shell='bash')
234
+
235
+ return to
236
+
237
+ def import_from_csv(spec: ExportTableSpec, state: ReplState, session: str, importer: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
238
+ im = AthenaImporter() if importer == 'athena' else SqliteImporter()
239
+ return im.import_from_csv(state, session if session else state.export_session, spec.keyspace, table, target_table, columns, multi_tables, create_db)
240
+
241
+ def resove_table_n_columns(spec: ExportTableSpec, state: ReplState, include_ks_in_target = False, importer = 'sqlite'):
242
+ table = spec.table
243
+ columns = spec.columns
244
+ if not columns:
245
+ columns = Config().get(f'export.{importer}.columns', f'<keys>')
246
+
247
+ keyspaced_table = f'{spec.keyspace}.{spec.table}'
248
+ if columns == '<keys>':
249
+ columns = ','.join(table_spec(state, keyspaced_table, on_any=True).keys())
250
+ elif columns == '<row-key>':
251
+ columns = table_spec(state, keyspaced_table, on_any=True).row_key()
252
+ elif columns == '*':
253
+ columns = ','.join([c.name for c in table_spec(state, keyspaced_table, on_any=True).columns])
254
+
255
+ if not columns:
256
+ log2(f'ERROR: Empty columns on {table}.')
257
+ return table, None, None
258
+
259
+ target_table = spec.target_table if spec.target_table else table
260
+ if not include_ks_in_target and '.' in target_table:
261
+ target_table = target_table.split('.')[-1]
262
+
263
+ return table, target_table, columns
264
+
265
+ class ExportService:
266
+ def __init__(self, handler: 'ExporterHandler'):
267
+ self.handler = handler
268
+
269
+ def export(self, args: list[str], export_only=False):
270
+ state = self.handler.state
271
+ export_session = state.export_session
272
+ spec: ExportSpec = None
273
+ try:
274
+ with state_with_pod(state) as state:
275
+ # --export-only for testing only
276
+ statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
277
+ if not statuses:
278
+ return state
279
+
280
+ ExportSessions.clear_export_session_cache()
281
+
282
+ if spec.importer == 'csv' or export_only:
283
+ ExportSessions.show_session(state.sts, state.pod, state.namespace, spec.session)
284
+ else:
285
+ log()
286
+ with export_db(state) as dbs:
287
+ dbs.show_database()
288
+ finally:
289
+ # if exporting to csv, do not bind the new session id to repl state
290
+ if spec and spec.importer == 'csv':
291
+ state.export_session = export_session
292
+
293
+ return state
294
+
295
+ def import_session(self, spec_str: str):
296
+ state = self.handler.state
297
+
298
+ tables, _ = Exporter.import_session(spec_str, state)
299
+ if tables:
300
+ ExportSessions.clear_export_session_cache()
301
+
302
+ log()
303
+ with export_db(state) as dbs:
304
+ dbs.show_database()
305
+
306
+ return state
307
+
308
+ def import_files(self, spec_str: str):
309
+ state = self.handler.state
310
+
311
+ tables, _ = Exporter.import_local_csv_files(spec_str, state)
312
+ if tables:
313
+ ExportSessions.clear_export_session_cache()
314
+
315
+ log()
316
+ with export_db(state) as dbs:
317
+ dbs.show_database()
318
+
319
+ return state
320
+
321
+ class ExporterHandler:
322
+ def __init__(self, state: ReplState):
323
+ self.state = state
324
+
325
+ def __enter__(self):
326
+ return ExportService(self)
327
+
328
+ def __exit__(self, exc_type, exc_val, exc_tb):
329
+ return False
330
+
331
+ def export(state: ReplState):
332
+ return ExporterHandler(state)
@@ -0,0 +1,44 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.export_sessions import ExportSessions
4
+ from adam.commands.export.exporter import export
5
+ from adam.commands.export.utils_export import state_with_pod
6
+ from adam.repl_state import ReplState, RequiredState
7
+
8
+ class ImportCSVFiles(Command):
9
+ COMMAND = 'import files'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ImportCSVFiles, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+
20
+ def command(self):
21
+ return ImportCSVFiles.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.CLUSTER_OR_POD
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ with self.validate(args, state) as (args, state):
31
+ with validate_args(args, state, name='file') as spec:
32
+ with state_with_pod(state) as state:
33
+ with export(state) as exporter:
34
+ return exporter.import_files(spec)
35
+
36
+ def completion(self, state: ReplState):
37
+ # warm up cache
38
+ ExportSessions.export_session_names(state.sts, state.pod, state.namespace)
39
+ ExportSessions.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import')
40
+
41
+ return {}
42
+
43
+ def help(self, _: ReplState):
44
+ return f'{ImportCSVFiles.COMMAND} <file-names,...>\t import files in session to Athena or SQLite'
@@ -0,0 +1,44 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.export_sessions import ExportSessions
4
+ from adam.commands.export.exporter import export
5
+ from adam.commands.export.utils_export import state_with_pod
6
+ from adam.repl_state import ReplState, RequiredState
7
+
8
+ class ImportSession(Command):
9
+ COMMAND = 'import session'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ImportSession, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+
20
+ def command(self):
21
+ return ImportSession.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.CLUSTER_OR_POD
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ with self.validate(args, state) as (args, state):
31
+ with validate_args(args, state, name='export session') as spec:
32
+ with state_with_pod(state) as state:
33
+ with export(state) as exporter:
34
+ return exporter.import_session(spec)
35
+
36
+ def completion(self, state: ReplState):
37
+ # warm up cache
38
+ ExportSessions.export_session_names(state.sts, state.pod, state.namespace)
39
+ ExportSessions.export_session_names(state.sts, state.pod, state.namespace, export_state='pending_import')
40
+
41
+ return {}
42
+
43
+ def help(self, _: ReplState):
44
+ return f'{ImportSession.COMMAND} <export-session-name>\t import files in session to Athena or SQLite'
@@ -0,0 +1,81 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.export.utils_export import csv_dir
4
+ from adam.config import Config
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import ing
7
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
8
+ from adam.utils_k8s.pods import log_prefix
9
+
10
+ class Importer:
11
+ @abstractmethod
12
+ def prefix(self):
13
+ pass
14
+
15
+ @abstractmethod
16
+ def import_from_csv(self, state: ReplState, from_session: str, keyspace: str, table: str, target_table: str, columns: str, multi_tables = True, create_db = False):
17
+ pass
18
+
19
+ @abstractmethod
20
+ def import_from_local_csv(self, state: ReplState,
21
+ keyspace: str, table: str, target_table: str, columns: str,
22
+ csv_file: str,
23
+ multi_tables = True, create_db = False):
24
+ pass
25
+
26
+ def move_to_done(self, state: ReplState, from_session: str, keyspace: str, target_table: str):
27
+ pod = state.pod
28
+ namespace = state.namespace
29
+ to_session = state.export_session
30
+ log_file = f'{log_prefix()}-{from_session}_{keyspace}.{target_table}.log.pending_import'
31
+
32
+ to = f'{log_prefix()}-{to_session}_{keyspace}.{target_table}.log.done'
33
+
34
+ CassandraNodes.exec(pod, namespace, f'mv {log_file} {to}', show_out=Config().is_debug(), shell='bash')
35
+
36
+ return to, to_session
37
+
38
+ def prefix_adjusted_session(self, session: str):
39
+ if not session.startswith(self.prefix()):
40
+ return f'{self.prefix()}{session[1:]}'
41
+
42
+ return session
43
+
44
+ def remove_csv(self, state: ReplState, from_session: str, table: str, target_table: str, multi_tables = True):
45
+ pod = state.pod
46
+ namespace = state.namespace
47
+
48
+ with ing(f'[{from_session}] Cleaning up temporary files', suppress_log=multi_tables):
49
+ CassandraNodes.exec(pod, namespace, f'rm -rf {self.csv_file(from_session, table, target_table)}', show_out=Config().is_debug(), shell='bash')
50
+
51
+ def db(self, session: str, keyspace: str):
52
+ return f'{session}_{keyspace}'
53
+
54
+ def csv_file(self, session: str, table: str, target_table: str):
55
+ return f'{csv_dir()}/{session}_{target_table}/{table}.csv'
56
+
57
+ def prefix_from_importer(importer: str = ''):
58
+ if not importer:
59
+ return ''
60
+
61
+ prefix = 's'
62
+
63
+ if importer == 'athena':
64
+ prefix = 'e'
65
+ elif importer == 'csv':
66
+ prefix = 'c'
67
+
68
+ return prefix
69
+
70
+ def importer_from_session(session: str):
71
+ if not session:
72
+ return None
73
+
74
+ importer = 'csv'
75
+
76
+ if session.startswith('s'):
77
+ importer = 'sqlite'
78
+ elif session.startswith('e'):
79
+ importer = 'athena'
80
+
81
+ return importer