kaqing 2.0.98__py3-none-any.whl → 2.0.203__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.
Files changed (254) 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 +11 -25
  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 +37 -63
  15. adam/commands/app/app.py +38 -0
  16. adam/commands/{app_ping.py → app/app_ping.py} +8 -14
  17. adam/commands/app/show_app_actions.py +49 -0
  18. adam/commands/{show → app}/show_app_id.py +8 -11
  19. adam/commands/{show → app}/show_app_queues.py +8 -14
  20. adam/commands/app/utils_app.py +106 -0
  21. adam/commands/audit/audit.py +31 -35
  22. adam/commands/audit/audit_repair_tables.py +26 -48
  23. adam/commands/audit/audit_run.py +50 -0
  24. adam/commands/audit/completions_l.py +15 -0
  25. adam/commands/audit/show_last10.py +36 -0
  26. adam/commands/audit/show_slow10.py +36 -0
  27. adam/commands/audit/show_top10.py +36 -0
  28. adam/commands/audit/utils_show_top10.py +71 -0
  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/cassandra/__init__.py +0 -0
  34. adam/commands/cassandra/download_cassandra_log.py +45 -0
  35. adam/commands/cassandra/nodetool.py +64 -0
  36. adam/commands/cassandra/nodetool_commands.py +120 -0
  37. adam/commands/{restart.py → cassandra/restart_cluster.py} +12 -26
  38. adam/commands/cassandra/restart_node.py +51 -0
  39. adam/commands/cassandra/restart_nodes.py +47 -0
  40. adam/commands/cassandra/rollout.py +88 -0
  41. adam/commands/cat.py +36 -0
  42. adam/commands/cd.py +14 -92
  43. adam/commands/check.py +18 -21
  44. adam/commands/cli_commands.py +8 -4
  45. adam/commands/clipboard_copy.py +87 -0
  46. adam/commands/code.py +57 -0
  47. adam/commands/command.py +212 -39
  48. adam/commands/commands_utils.py +20 -28
  49. adam/commands/cql/alter_tables.py +66 -0
  50. adam/commands/cql/completions_c.py +29 -0
  51. adam/commands/cql/cqlsh.py +10 -29
  52. adam/commands/cql/utils_cql.py +305 -0
  53. adam/commands/debug/__init__.py +0 -0
  54. adam/commands/debug/debug.py +22 -0
  55. adam/commands/debug/debug_completes.py +35 -0
  56. adam/commands/debug/debug_timings.py +35 -0
  57. adam/commands/deploy/code_start.py +7 -10
  58. adam/commands/deploy/code_stop.py +4 -21
  59. adam/commands/deploy/code_utils.py +3 -3
  60. adam/commands/deploy/deploy.py +4 -21
  61. adam/commands/deploy/deploy_frontend.py +14 -17
  62. adam/commands/deploy/deploy_pg_agent.py +3 -6
  63. adam/commands/deploy/deploy_pod.py +65 -73
  64. adam/commands/deploy/deploy_utils.py +14 -24
  65. adam/commands/deploy/undeploy.py +4 -21
  66. adam/commands/deploy/undeploy_frontend.py +4 -7
  67. adam/commands/deploy/undeploy_pg_agent.py +6 -8
  68. adam/commands/deploy/undeploy_pod.py +11 -12
  69. adam/commands/devices/__init__.py +0 -0
  70. adam/commands/devices/device.py +149 -0
  71. adam/commands/devices/device_app.py +163 -0
  72. adam/commands/devices/device_auit_log.py +49 -0
  73. adam/commands/devices/device_cass.py +179 -0
  74. adam/commands/devices/device_export.py +87 -0
  75. adam/commands/devices/device_postgres.py +160 -0
  76. adam/commands/devices/devices.py +25 -0
  77. adam/commands/download_cassandra_log.py +45 -0
  78. adam/commands/download_file.py +47 -0
  79. adam/commands/exit.py +1 -4
  80. adam/commands/export/__init__.py +0 -0
  81. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  82. adam/commands/export/clean_up_export_sessions.py +39 -0
  83. adam/commands/export/completions_x.py +11 -0
  84. adam/commands/export/download_export_session.py +40 -0
  85. adam/commands/export/drop_export_database.py +39 -0
  86. adam/commands/export/drop_export_databases.py +37 -0
  87. adam/commands/export/export.py +37 -0
  88. adam/commands/export/export_databases.py +247 -0
  89. adam/commands/export/export_select.py +34 -0
  90. adam/commands/export/export_sessions.py +211 -0
  91. adam/commands/export/export_use.py +49 -0
  92. adam/commands/export/export_x_select.py +48 -0
  93. adam/commands/export/exporter.py +361 -0
  94. adam/commands/export/import_files.py +44 -0
  95. adam/commands/export/import_session.py +44 -0
  96. adam/commands/export/importer.py +82 -0
  97. adam/commands/export/importer_athena.py +150 -0
  98. adam/commands/export/importer_sqlite.py +69 -0
  99. adam/commands/export/show_column_counts.py +45 -0
  100. adam/commands/export/show_export_databases.py +39 -0
  101. adam/commands/export/show_export_session.py +39 -0
  102. adam/commands/export/show_export_sessions.py +37 -0
  103. adam/commands/export/utils_export.py +366 -0
  104. adam/commands/find_files.py +51 -0
  105. adam/commands/find_processes.py +76 -0
  106. adam/commands/generate_report.py +52 -0
  107. adam/commands/head.py +36 -0
  108. adam/commands/help.py +12 -8
  109. adam/commands/intermediate_command.py +52 -0
  110. adam/commands/issues.py +14 -40
  111. adam/commands/kubectl.py +38 -0
  112. adam/commands/login.py +26 -25
  113. adam/commands/ls.py +11 -116
  114. adam/commands/medusa/medusa.py +4 -22
  115. adam/commands/medusa/medusa_backup.py +20 -27
  116. adam/commands/medusa/medusa_restore.py +35 -48
  117. adam/commands/medusa/medusa_show_backupjobs.py +17 -18
  118. adam/commands/medusa/medusa_show_restorejobs.py +13 -18
  119. adam/commands/medusa/utils_medusa.py +15 -0
  120. adam/commands/nodetool.py +8 -19
  121. adam/commands/os/__init__.py +0 -0
  122. adam/commands/os/cat.py +36 -0
  123. adam/commands/os/download_file.py +47 -0
  124. adam/commands/os/find_files.py +51 -0
  125. adam/commands/os/find_processes.py +76 -0
  126. adam/commands/os/head.py +36 -0
  127. adam/commands/os/shell.py +41 -0
  128. adam/commands/param_get.py +11 -14
  129. adam/commands/param_set.py +8 -12
  130. adam/commands/postgres/completions_p.py +22 -0
  131. adam/commands/postgres/postgres.py +47 -55
  132. adam/commands/postgres/postgres_databases.py +269 -0
  133. adam/commands/postgres/postgres_ls.py +4 -8
  134. adam/commands/postgres/postgres_preview.py +5 -9
  135. adam/commands/postgres/utils_postgres.py +79 -0
  136. adam/commands/preview_table.py +10 -61
  137. adam/commands/pwd.py +14 -46
  138. adam/commands/reaper/reaper.py +4 -24
  139. adam/commands/reaper/reaper_forward.py +49 -56
  140. adam/commands/reaper/reaper_forward_session.py +6 -0
  141. adam/commands/reaper/reaper_forward_stop.py +10 -16
  142. adam/commands/reaper/reaper_restart.py +7 -14
  143. adam/commands/reaper/reaper_run_abort.py +8 -33
  144. adam/commands/reaper/reaper_runs.py +43 -58
  145. adam/commands/reaper/reaper_runs_abort.py +29 -49
  146. adam/commands/reaper/reaper_schedule_activate.py +14 -33
  147. adam/commands/reaper/reaper_schedule_start.py +9 -33
  148. adam/commands/reaper/reaper_schedule_stop.py +9 -33
  149. adam/commands/reaper/reaper_schedules.py +4 -14
  150. adam/commands/reaper/reaper_status.py +8 -16
  151. adam/commands/reaper/utils_reaper.py +203 -0
  152. adam/commands/repair/repair.py +4 -22
  153. adam/commands/repair/repair_log.py +5 -11
  154. adam/commands/repair/repair_run.py +27 -34
  155. adam/commands/repair/repair_scan.py +32 -40
  156. adam/commands/repair/repair_stop.py +5 -12
  157. adam/commands/restart_cluster.py +47 -0
  158. adam/commands/restart_node.py +51 -0
  159. adam/commands/restart_nodes.py +47 -0
  160. adam/commands/rollout.py +19 -24
  161. adam/commands/shell.py +12 -4
  162. adam/commands/show/show.py +10 -23
  163. adam/commands/show/show_adam.py +3 -3
  164. adam/commands/show/show_cassandra_repairs.py +37 -0
  165. adam/commands/show/show_cassandra_status.py +47 -51
  166. adam/commands/show/show_cassandra_version.py +5 -18
  167. adam/commands/show/show_cli_commands.py +56 -0
  168. adam/commands/show/show_host.py +1 -1
  169. adam/commands/show/show_login.py +23 -27
  170. adam/commands/show/show_params.py +2 -5
  171. adam/commands/show/show_processes.py +18 -21
  172. adam/commands/show/show_storage.py +11 -20
  173. adam/commands/watch.py +26 -29
  174. adam/config.py +5 -15
  175. adam/embedded_params.py +1 -1
  176. adam/log.py +4 -4
  177. adam/repl.py +105 -133
  178. adam/repl_commands.py +68 -28
  179. adam/repl_session.py +9 -1
  180. adam/repl_state.py +300 -62
  181. adam/sql/async_executor.py +44 -0
  182. adam/sql/lark_completer.py +286 -0
  183. adam/sql/lark_parser.py +604 -0
  184. adam/sql/qingl.lark +1076 -0
  185. adam/sql/sql_completer.py +104 -64
  186. adam/sql/sql_state_machine.py +630 -0
  187. adam/sql/term_completer.py +3 -0
  188. adam/sso/authn_ad.py +6 -8
  189. adam/sso/authn_okta.py +4 -6
  190. adam/sso/cred_cache.py +3 -5
  191. adam/sso/idp.py +9 -12
  192. adam/utils.py +640 -10
  193. adam/utils_athena.py +140 -87
  194. adam/utils_audits.py +102 -0
  195. adam/utils_issues.py +32 -0
  196. adam/utils_k8s/app_clusters.py +28 -0
  197. adam/utils_k8s/app_pods.py +35 -0
  198. adam/utils_k8s/cassandra_clusters.py +34 -21
  199. adam/utils_k8s/cassandra_nodes.py +9 -6
  200. adam/utils_k8s/custom_resources.py +16 -17
  201. adam/utils_k8s/ingresses.py +2 -2
  202. adam/utils_k8s/jobs.py +7 -11
  203. adam/utils_k8s/k8s.py +96 -0
  204. adam/utils_k8s/kube_context.py +3 -6
  205. adam/{pod_exec_result.py → utils_k8s/pod_exec_result.py} +11 -5
  206. adam/utils_k8s/pods.py +146 -75
  207. adam/utils_k8s/secrets.py +4 -4
  208. adam/utils_k8s/service_accounts.py +5 -4
  209. adam/utils_k8s/services.py +2 -2
  210. adam/utils_k8s/statefulsets.py +6 -14
  211. adam/utils_local.py +42 -0
  212. adam/utils_net.py +4 -4
  213. adam/utils_repl/__init__.py +0 -0
  214. adam/utils_repl/appendable_completer.py +6 -0
  215. adam/utils_repl/automata_completer.py +48 -0
  216. adam/utils_repl/repl_completer.py +89 -0
  217. adam/utils_repl/state_machine.py +173 -0
  218. adam/utils_sqlite.py +137 -0
  219. adam/version.py +1 -1
  220. {kaqing-2.0.98.dist-info → kaqing-2.0.203.dist-info}/METADATA +1 -1
  221. kaqing-2.0.203.dist-info/RECORD +277 -0
  222. kaqing-2.0.203.dist-info/top_level.txt +2 -0
  223. teddy/__init__.py +0 -0
  224. teddy/lark_parser.py +436 -0
  225. teddy/lark_parser2.py +618 -0
  226. adam/commands/app.py +0 -67
  227. adam/commands/bash.py +0 -92
  228. adam/commands/cp.py +0 -95
  229. adam/commands/cql/cql_completions.py +0 -11
  230. adam/commands/cql/cql_table_completer.py +0 -8
  231. adam/commands/cql/cql_utils.py +0 -115
  232. adam/commands/describe/describe.py +0 -47
  233. adam/commands/describe/describe_keyspace.py +0 -60
  234. adam/commands/describe/describe_keyspaces.py +0 -49
  235. adam/commands/describe/describe_schema.py +0 -49
  236. adam/commands/describe/describe_table.py +0 -60
  237. adam/commands/describe/describe_tables.py +0 -49
  238. adam/commands/devices.py +0 -118
  239. adam/commands/logs.py +0 -39
  240. adam/commands/postgres/postgres_session.py +0 -240
  241. adam/commands/postgres/postgres_utils.py +0 -31
  242. adam/commands/postgres/psql_completions.py +0 -10
  243. adam/commands/postgres/psql_table_completer.py +0 -11
  244. adam/commands/reaper/reaper_session.py +0 -159
  245. adam/commands/report.py +0 -57
  246. adam/commands/show/show_app_actions.py +0 -53
  247. adam/commands/show/show_commands.py +0 -61
  248. adam/commands/show/show_repairs.py +0 -47
  249. adam/sql/state_machine.py +0 -460
  250. kaqing-2.0.98.dist-info/RECORD +0 -191
  251. kaqing-2.0.98.dist-info/top_level.txt +0 -1
  252. /adam/commands/{describe → app}/__init__.py +0 -0
  253. {kaqing-2.0.98.dist-info → kaqing-2.0.203.dist-info}/WHEEL +0 -0
  254. {kaqing-2.0.98.dist-info → kaqing-2.0.203.dist-info}/entry_points.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 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]
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 {}
47
+
48
+ def help(self, _: ReplState):
49
+ return f'{ExportUse.COMMAND} <export-database-name>\t use export database'
@@ -0,0 +1,48 @@
1
+ from adam.commands import extract_trailing_options, validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.export.completions_x import completions_x
4
+ from adam.commands.export.export_databases import export_db
5
+ from adam.repl_state import ReplState, RequiredState
6
+
7
+ class ExportXSelect(Command):
8
+ COMMAND = 'xelect'
9
+
10
+ # the singleton pattern
11
+ def __new__(cls, *args, **kwargs):
12
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportXSelect, 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 ExportXSelect.COMMAND
21
+
22
+ def required(self):
23
+ return RequiredState.EXPORT_DB
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 extract_trailing_options(args, '&') as (args, backgrounded):
31
+ with validate_args(args, state, name='SQL statement') as query:
32
+ with export_db(state) as dbs:
33
+ dbs.sql(f'select {query}', backgrounded=backgrounded)
34
+
35
+ return state
36
+
37
+ def completion(self, state: ReplState):
38
+ if state.device != ReplState.C:
39
+ return {}
40
+
41
+ if not state.export_session:
42
+ return {}
43
+
44
+ # add only xelect completions to c: drive from lark
45
+ return {ExportXSelect.COMMAND: completions_x(state)[ExportXSelect.COMMAND]}
46
+
47
+ def help(self, _: ReplState):
48
+ return f'xelect...\t run queries on export database'
@@ -0,0 +1,361 @@
1
+ import copy
2
+ from datetime import datetime
3
+ import time
4
+ import traceback
5
+
6
+ from adam.commands.command import InvalidArgumentsException
7
+ from adam.commands.cql.utils_cql import cassandra_table_names, run_cql, table_spec
8
+ from adam.commands.export.export_databases import export_db
9
+ from adam.commands.export.export_sessions import ExportSessions
10
+ from adam.commands.export.importer import Importer
11
+ from adam.commands.export.importer_athena import AthenaImporter
12
+ from adam.commands.export.importer_sqlite import SqliteImporter
13
+ from adam.commands.export.utils_export import ExportSpec, ExportTableStatus, ExportTableSpec, ImportSpec, csv_dir, export_log_prefix, find_files, os_system_exec, state_with_pod
14
+ from adam.config import Config
15
+ from adam.repl_state import ReplState
16
+ from adam.utils import debug, log, offload, parallelize, log2, ing, log_exc
17
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
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}] Triggering export of'
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', collect=export_state == 'init', name='exporter') as exec:
164
+ return exec.map(lambda table: Exporter.export_table(table, copy.copy(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'{export_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
+
189
+ with offload(name='exporter') as exec:
190
+ exec.submit(lambda: Exporter.export_loop(ExportTableContext(spec, state, session, importer, export_only, multi_tables, table, target_table, columns, create_db, log_file, status)))
191
+ # Exporter.export_loop(ExportTableContext(spec, state, session, importer, export_only, multi_tables, table, target_table, columns, create_db, log_file, status))
192
+
193
+ return status.status
194
+
195
+ def export_loop(ctx: 'ExportTableContext'):
196
+ try:
197
+ while ctx.status.status != 'done':
198
+ if ctx.status.status == 'export_in_pregress':
199
+ debug('Exporting to CSV is still in progess, sleeping for 1 sec...')
200
+ time.sleep(1)
201
+ elif ctx.status.status == 'exported':
202
+ ctx.log_file = Exporter.rename_to_pending_import(ctx.spec, ctx.state, ctx.session, ctx.target_table)
203
+ ExportSessions.clear_export_session_cache()
204
+ if ctx.importer == 'csv' or ctx.export_only:
205
+ return 'pending_import'
206
+ elif ctx.status.status == 'pending_import':
207
+ ctx.log_file, ctx.session = Exporter.import_from_csv(ctx.spec, ctx.state, ctx.session, ctx.importer, ctx.table, ctx.target_table, ctx.columns, multi_tables=ctx.multi_tables, create_db=ctx.create_db)
208
+
209
+ ctx.status = ExportTableStatus.from_log_file(ctx.state.pod, ctx.state.namespace, ctx.session, ctx.log_file)
210
+
211
+ return ctx.status.status
212
+ except:
213
+ traceback.print_exc()
214
+
215
+ def create_table_log(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str):
216
+ log_file = f'{export_log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
217
+
218
+ cmd = f'rm -f {log_file}* && touch {log_file}'
219
+ os_system_exec(cmd, show_out=Config().is_debug())
220
+
221
+ return table
222
+
223
+ def export_to_csv(spec: ExportTableSpec, state: ReplState, session: str, table: str, target_table: str, columns: str, multi_tables = True, consistency: str = None):
224
+ db = f'{session}_{target_table}'
225
+
226
+ CassandraNodes.exec(state.pod, state.namespace, f'mkdir -p {csv_dir()}/{db}', show_out=Config().is_debug(), shell='bash')
227
+ csv_file = f'{csv_dir()}/{db}/{table}.csv'
228
+ log_file = f'{export_log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
229
+
230
+ suppress_ing_log = Config().is_debug() or multi_tables
231
+ queries = []
232
+ if consistency:
233
+ queries.append(f'CONSISTENCY {consistency}')
234
+ queries.append(f"COPY {spec.keyspace}.{table}({columns}) TO '{csv_file}' WITH HEADER = TRUE")
235
+
236
+ with ing(f'[{session}] Triggering dump of table {spec.keyspace}.{table}{f" with consistency {consistency}" if consistency else ""}',
237
+ suppress_log=suppress_ing_log):
238
+ run_cql(state, ';'.join(queries), show_out=Config().is_debug(), backgrounded=True, log_file=log_file)
239
+
240
+ return log_file
241
+
242
+ def rename_to_pending_import(spec: ExportTableSpec, state: ReplState, session: str, target_table: str):
243
+ log_file = f'{export_log_prefix()}-{session}_{spec.keyspace}.{target_table}.log'
244
+ to = f'{log_file}.pending_import'
245
+
246
+ cmd =f'mv {log_file} {to}'
247
+ os_system_exec(cmd, show_out=Config().is_debug())
248
+
249
+ return to
250
+
251
+ 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):
252
+ im = AthenaImporter() if importer == 'athena' else SqliteImporter()
253
+ return im.import_from_csv(state, session if session else state.export_session, spec.keyspace, table, target_table, columns, multi_tables, create_db)
254
+
255
+ def resove_table_n_columns(spec: ExportTableSpec, state: ReplState, include_ks_in_target = False, importer = 'sqlite'):
256
+ table = spec.table
257
+ columns = spec.columns
258
+ if not columns:
259
+ columns = Config().get(f'export.{importer}.columns', f'<keys>')
260
+
261
+ keyspaced_table = f'{spec.keyspace}.{spec.table}'
262
+ if columns == '<keys>':
263
+ columns = ','.join(table_spec(state, keyspaced_table, on_any=True).keys())
264
+ elif columns == '<row-key>':
265
+ columns = table_spec(state, keyspaced_table, on_any=True).row_key()
266
+ elif columns == '*':
267
+ columns = ','.join([c.name for c in table_spec(state, keyspaced_table, on_any=True).columns])
268
+
269
+ if not columns:
270
+ log2(f'ERROR: Empty columns on {table}.')
271
+ return table, None, None
272
+
273
+ target_table = spec.target_table if spec.target_table else table
274
+ if not include_ks_in_target and '.' in target_table:
275
+ target_table = target_table.split('.')[-1]
276
+
277
+ return table, target_table, columns
278
+
279
+ class ExportTableContext:
280
+ def __init__(self, spec: ExportTableSpec, state: ReplState, session: str, importer: str, export_only = False, multi_tables = True, table: str = None, target_table: str = None, columns: str = None, create_db = False, log_file: str = None, status: ExportTableStatus = None):
281
+ self.spec = spec
282
+ self.state = state
283
+ self.session = session
284
+ self.importer = importer
285
+ self.export_only = export_only
286
+ self.multi_tables = multi_tables
287
+ self.table = table
288
+ self.target_table = target_table
289
+ self.columns = columns
290
+ self.create_db = create_db
291
+ self.log_file = log_file
292
+ self.status = status
293
+
294
+ class ExportService:
295
+ def __init__(self, handler: 'ExporterHandler'):
296
+ self.handler = handler
297
+
298
+ def export(self, args: list[str], export_only=False):
299
+ state = self.handler.state
300
+ export_session = state.export_session
301
+ spec: ExportSpec = None
302
+ try:
303
+ with state_with_pod(state) as state:
304
+ # --export-only for testing only
305
+ statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
306
+ if not statuses:
307
+ return state
308
+
309
+ ExportSessions.clear_export_session_cache()
310
+
311
+ if spec.importer == 'csv' or export_only:
312
+ ExportSessions.show_session(state.sts, state.pod, state.namespace, spec.session)
313
+ else:
314
+ log()
315
+ with export_db(state) as dbs:
316
+ dbs.show_database()
317
+ finally:
318
+ # if exporting to csv, do not bind the new session id to repl state
319
+ if spec and spec.importer == 'csv':
320
+ state.export_session = export_session
321
+
322
+ return state
323
+
324
+ def import_session(self, spec_str: str):
325
+ state = self.handler.state
326
+
327
+ tables, _ = Exporter.import_session(spec_str, state)
328
+ if tables:
329
+ ExportSessions.clear_export_session_cache()
330
+
331
+ log()
332
+ with export_db(state) as dbs:
333
+ dbs.show_database()
334
+
335
+ return state
336
+
337
+ def import_files(self, spec_str: str):
338
+ state = self.handler.state
339
+
340
+ tables, _ = Exporter.import_local_csv_files(spec_str, state)
341
+ if tables:
342
+ ExportSessions.clear_export_session_cache()
343
+
344
+ log()
345
+ with export_db(state) as dbs:
346
+ dbs.show_database()
347
+
348
+ return state
349
+
350
+ class ExporterHandler:
351
+ def __init__(self, state: ReplState):
352
+ self.state = state
353
+
354
+ def __enter__(self):
355
+ return ExportService(self)
356
+
357
+ def __exit__(self, exc_type, exc_val, exc_tb):
358
+ return False
359
+
360
+ def export(state: ReplState):
361
+ 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,82 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.export.utils_export import csv_dir, os_system_exec
4
+ from adam.config import Config
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import ing
7
+ from adam.utils_k8s.pods import log_prefix
8
+
9
+ class Importer:
10
+ @abstractmethod
11
+ def prefix(self):
12
+ pass
13
+
14
+ @abstractmethod
15
+ 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):
16
+ pass
17
+
18
+ @abstractmethod
19
+ def import_from_local_csv(self, state: ReplState,
20
+ keyspace: str, table: str, target_table: str, columns: str,
21
+ csv_file: str,
22
+ multi_tables = True, create_db = False):
23
+ pass
24
+
25
+ def move_to_done(self, state: ReplState, from_session: str, keyspace: str, target_table: str):
26
+ pod = state.pod
27
+ namespace = state.namespace
28
+ to_session = state.export_session
29
+ log_file = f'{log_prefix()}-{from_session}_{keyspace}.{target_table}.log.pending_import'
30
+
31
+ to = f'{log_prefix()}-{to_session}_{keyspace}.{target_table}.log.done'
32
+
33
+ cmd = f'mv {log_file} {to}'
34
+ os_system_exec(cmd, show_out=Config().is_debug())
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
+ cmd = f'rm -rf {self.csv_file(from_session, table, target_table)}'
50
+ os_system_exec(cmd, show_out=Config().is_debug())
51
+
52
+ def db(self, session: str, keyspace: str):
53
+ return f'{session}_{keyspace}'
54
+
55
+ def csv_file(self, session: str, table: str, target_table: str):
56
+ return f'{csv_dir()}/{session}_{target_table}/{table}.csv'
57
+
58
+ def prefix_from_importer(importer: str = ''):
59
+ if not importer:
60
+ return ''
61
+
62
+ prefix = 's'
63
+
64
+ if importer == 'athena':
65
+ prefix = 'e'
66
+ elif importer == 'csv':
67
+ prefix = 'c'
68
+
69
+ return prefix
70
+
71
+ def importer_from_session(session: str):
72
+ if not session:
73
+ return None
74
+
75
+ importer = 'csv'
76
+
77
+ if session.startswith('s'):
78
+ importer = 'sqlite'
79
+ elif session.startswith('e'):
80
+ importer = 'athena'
81
+
82
+ return importer