kaqing 2.0.145__py3-none-any.whl → 2.0.172__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 (172) 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 +18 -0
  13. adam/commands/alter_tables.py +43 -47
  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 +13 -19
  25. adam/commands/cd.py +8 -10
  26. adam/commands/check.py +20 -21
  27. adam/commands/cli_commands.py +2 -3
  28. adam/commands/code.py +20 -23
  29. adam/commands/command.py +120 -39
  30. adam/commands/commands_utils.py +8 -17
  31. adam/commands/cp.py +33 -39
  32. adam/commands/cql/cql_completions.py +9 -4
  33. adam/commands/cql/cqlsh.py +10 -30
  34. adam/commands/cql/{cql_utils.py → utils_cql.py} +149 -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 +2 -2
  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 +18 -7
  55. adam/commands/export/drop_export_database.py +15 -18
  56. adam/commands/export/drop_export_databases.py +6 -9
  57. adam/commands/export/export.py +8 -38
  58. adam/commands/export/export_databases.py +16 -12
  59. adam/commands/export/export_handlers.py +71 -0
  60. adam/commands/export/export_select.py +33 -24
  61. adam/commands/export/export_use.py +12 -15
  62. adam/commands/export/exporter.py +37 -48
  63. adam/commands/export/import_session.py +4 -32
  64. adam/commands/export/importer_athena.py +4 -7
  65. adam/commands/export/importer_sqlite.py +19 -27
  66. adam/commands/export/show_column_counts.py +13 -22
  67. adam/commands/export/show_export_databases.py +3 -6
  68. adam/commands/export/show_export_session.py +10 -13
  69. adam/commands/export/show_export_sessions.py +8 -11
  70. adam/commands/export/utils_export.py +24 -1
  71. adam/commands/intermediate_command.py +49 -0
  72. adam/commands/issues.py +11 -43
  73. adam/commands/kubectl.py +3 -6
  74. adam/commands/login.py +22 -24
  75. adam/commands/logs.py +3 -6
  76. adam/commands/ls.py +8 -9
  77. adam/commands/medusa/medusa.py +4 -22
  78. adam/commands/medusa/medusa_backup.py +20 -24
  79. adam/commands/medusa/medusa_restore.py +29 -33
  80. adam/commands/medusa/medusa_show_backupjobs.py +14 -18
  81. adam/commands/medusa/medusa_show_restorejobs.py +11 -18
  82. adam/commands/nodetool.py +6 -15
  83. adam/commands/param_get.py +11 -12
  84. adam/commands/param_set.py +9 -10
  85. adam/commands/postgres/postgres.py +29 -37
  86. adam/commands/postgres/postgres_context.py +47 -23
  87. adam/commands/postgres/postgres_ls.py +4 -8
  88. adam/commands/postgres/postgres_preview.py +5 -9
  89. adam/commands/postgres/psql_completions.py +1 -1
  90. adam/commands/postgres/utils_postgres.py +66 -0
  91. adam/commands/preview_table.py +5 -44
  92. adam/commands/pwd.py +13 -16
  93. adam/commands/reaper/reaper.py +4 -27
  94. adam/commands/reaper/reaper_forward.py +48 -55
  95. adam/commands/reaper/reaper_forward_session.py +6 -0
  96. adam/commands/reaper/reaper_forward_stop.py +10 -16
  97. adam/commands/reaper/reaper_restart.py +7 -14
  98. adam/commands/reaper/reaper_run_abort.py +11 -30
  99. adam/commands/reaper/reaper_runs.py +42 -57
  100. adam/commands/reaper/reaper_runs_abort.py +29 -49
  101. adam/commands/reaper/reaper_schedule_activate.py +11 -30
  102. adam/commands/reaper/reaper_schedule_start.py +10 -29
  103. adam/commands/reaper/reaper_schedule_stop.py +10 -29
  104. adam/commands/reaper/reaper_schedules.py +4 -14
  105. adam/commands/reaper/reaper_status.py +8 -16
  106. adam/commands/reaper/utils_reaper.py +196 -0
  107. adam/commands/repair/repair.py +4 -22
  108. adam/commands/repair/repair_log.py +5 -11
  109. adam/commands/repair/repair_run.py +27 -34
  110. adam/commands/repair/repair_scan.py +32 -38
  111. adam/commands/repair/repair_stop.py +5 -11
  112. adam/commands/report.py +27 -29
  113. adam/commands/restart.py +25 -26
  114. adam/commands/rollout.py +19 -24
  115. adam/commands/shell.py +10 -4
  116. adam/commands/show/show.py +10 -26
  117. adam/commands/show/show_cassandra_repairs.py +35 -0
  118. adam/commands/show/show_cassandra_status.py +32 -43
  119. adam/commands/show/show_cassandra_version.py +5 -18
  120. adam/commands/show/show_commands.py +19 -24
  121. adam/commands/show/show_host.py +1 -1
  122. adam/commands/show/show_login.py +20 -27
  123. adam/commands/show/show_processes.py +15 -19
  124. adam/commands/show/show_storage.py +10 -20
  125. adam/commands/watch.py +26 -29
  126. adam/config.py +4 -16
  127. adam/embedded_params.py +1 -1
  128. adam/log.py +4 -4
  129. adam/pod_exec_result.py +3 -3
  130. adam/repl.py +29 -32
  131. adam/repl_commands.py +11 -11
  132. adam/repl_state.py +52 -26
  133. adam/sql/sql_completer.py +4 -6
  134. adam/sql/sql_state_machine.py +21 -14
  135. adam/sso/authn_ad.py +6 -8
  136. adam/sso/authn_okta.py +4 -6
  137. adam/sso/cred_cache.py +3 -5
  138. adam/sso/idp.py +9 -12
  139. adam/utils.py +393 -33
  140. adam/utils_athena.py +14 -13
  141. adam/utils_audits.py +12 -12
  142. adam/utils_issues.py +32 -0
  143. adam/utils_k8s/app_clusters.py +13 -18
  144. adam/utils_k8s/app_pods.py +2 -0
  145. adam/utils_k8s/cassandra_clusters.py +21 -18
  146. adam/utils_k8s/custom_resources.py +16 -17
  147. adam/utils_k8s/ingresses.py +2 -2
  148. adam/utils_k8s/jobs.py +7 -11
  149. adam/utils_k8s/k8s.py +87 -0
  150. adam/utils_k8s/pods.py +14 -76
  151. adam/utils_k8s/secrets.py +4 -4
  152. adam/utils_k8s/service_accounts.py +5 -4
  153. adam/utils_k8s/services.py +2 -2
  154. adam/utils_k8s/statefulsets.py +1 -12
  155. adam/utils_repl/state_machine.py +3 -3
  156. adam/utils_sqlite.py +78 -42
  157. adam/version.py +1 -1
  158. {kaqing-2.0.145.dist-info → kaqing-2.0.172.dist-info}/METADATA +1 -1
  159. kaqing-2.0.172.dist-info/RECORD +230 -0
  160. adam/commands/app.py +0 -67
  161. adam/commands/app_ping.py +0 -44
  162. adam/commands/export/clean_up_export_session.py +0 -53
  163. adam/commands/postgres/postgres_utils.py +0 -31
  164. adam/commands/reaper/reaper_session.py +0 -159
  165. adam/commands/show/show_app_actions.py +0 -56
  166. adam/commands/show/show_app_id.py +0 -47
  167. adam/commands/show/show_app_queues.py +0 -45
  168. adam/commands/show/show_repairs.py +0 -47
  169. kaqing-2.0.145.dist-info/RECORD +0 -227
  170. {kaqing-2.0.145.dist-info → kaqing-2.0.172.dist-info}/WHEEL +0 -0
  171. {kaqing-2.0.145.dist-info → kaqing-2.0.172.dist-info}/entry_points.txt +0 -0
  172. {kaqing-2.0.145.dist-info → kaqing-2.0.172.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, InvalidState
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
+ return InvalidState()
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
+
@@ -3,7 +3,7 @@ from adam.commands.devices.device import Device
3
3
  from adam.commands.export.export_databases import ExportDatabases
4
4
  from adam.config import Config
5
5
  from adam.repl_state import ReplState
6
- 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}:'
@@ -78,7 +78,7 @@ class DeviceExport(Command, Device):
78
78
  def enter(self, state: ReplState):
79
79
  if auto_enter := Config().get('repl.x.auto-enter', 'no'):
80
80
  if auto_enter == 'latest':
81
- Config().wait_log(f'Moving to the latest export database...')
81
+ wait_log(f'Moving to the latest export database...')
82
82
  if dbs := ExportDatabases.database_names():
83
83
  state.export_session = sorted(dbs)[-1]
84
84
  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, InvalidState
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
+ return InvalidState()
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.exporter import Exporter
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 Exporter.clean_up_all_sessions(state.sts, state.pod, state.namespace):
29
+ Exporter.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,10 @@
1
1
  from adam.commands.command import Command
2
2
  from adam.commands.export.exporter import Exporter
3
3
  from adam.repl_state import ReplState, RequiredState
4
+ from adam.utils import log, log2
4
5
 
5
6
  class CleanUpExportSessions(Command):
6
- COMMAND = 'clean up all export sessions'
7
+ COMMAND = 'clean up export sessions'
7
8
 
8
9
  # the singleton pattern
9
10
  def __new__(cls, *args, **kwargs):
@@ -24,17 +25,27 @@ class CleanUpExportSessions(Command):
24
25
  if not(args := self.args(cmd)):
25
26
  return super().run(cmd, state)
26
27
 
27
- state, args = self.apply_state(args, state)
28
- if not self.validate_state(state):
29
- return state
28
+ with self.validate(args, state) as (args, state):
29
+ if not args:
30
+ if state.in_repl:
31
+ log2('Specify export session name.')
32
+ else:
33
+ log2('* Session name is missing.')
34
+
35
+ Command.display_help()
36
+
37
+ return 'command-missing'
38
+
39
+ sessions = [arg.strip(' ') for arg in ' '.join(args).split(',')]
40
+ csv_cnt, log_cnt = Exporter.clean_up_sessions(state.sts, state.pod, state.namespace, sessions)
41
+ log(f'Removed {csv_cnt} csv and {log_cnt} log files.')
30
42
 
31
- if Exporter.clean_up_all_sessions(state.sts, state.pod, state.namespace):
32
43
  Exporter.clear_export_session_cache()
33
44
 
34
- return state
45
+ return state
35
46
 
36
47
  def completion(self, _: ReplState):
37
48
  return {}
38
49
 
39
50
  def help(self, _: ReplState):
40
- return f'{CleanUpExportSessions.COMMAND}\t clean up all export sessions'
51
+ return f'{CleanUpExportSessions.COMMAND} <export-session-name>\t clean up export session'
@@ -27,29 +27,26 @@ class DropExportDatabase(Command):
27
27
  if not(args := self.args(cmd)):
28
28
  return super().run(cmd, state)
29
29
 
30
- state, args = self.apply_state(args, state)
31
- if not self.validate_state(state):
32
- return state
33
-
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()
30
+ with self.validate(args, state) as (args, state):
31
+ if not len(args):
32
+ if state.in_repl:
33
+ log2('Database name is required.')
34
+ log2()
35
+ else:
36
+ log2('* Database name is missing.')
37
+ Command.display_help()
41
38
 
42
- return 'command-missing'
39
+ return 'command-missing'
43
40
 
44
- Exporter.drop_databases(state.sts, state.pod, state.namespace, args[0])
41
+ Exporter.drop_databases(state.sts, state.pod, state.namespace, args[0])
45
42
 
46
- SQLite.clear_cache()
47
- Athena.clear_cache()
43
+ SQLite.clear_cache()
44
+ Athena.clear_cache()
48
45
 
49
- if state.export_session == args[0]:
50
- state.export_session = None
46
+ if state.export_session == args[0]:
47
+ state.export_session = None
51
48
 
52
- return state
49
+ return state
53
50
 
54
51
  def completion(self, _: ReplState):
55
52
  return {}
@@ -26,18 +26,15 @@ class DropExportDatabases(Command):
26
26
  if not(args := self.args(cmd)):
27
27
  return super().run(cmd, state)
28
28
 
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)
29
+ with self.validate(args, state) as (args, state):
30
+ Exporter.drop_databases(state.sts, state.pod, state.namespace)
34
31
 
35
- SQLite.clear_cache()
36
- Athena.clear_cache()
32
+ SQLite.clear_cache()
33
+ Athena.clear_cache()
37
34
 
38
- state.export_session = None
35
+ state.export_session = None
39
36
 
40
- return state
37
+ return state
41
38
 
42
39
  def completion(self, _: ReplState):
43
40
  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.export_handlers 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,9 +1,11 @@
1
+ from collections.abc import Callable
1
2
  import os
2
3
  import boto3
3
4
 
4
5
  from adam.commands.export.utils_export import ExportTableStatus
5
6
  from adam.config import Config
6
- from adam.utils import lines_to_tabular, log, log2, ing
7
+ from adam.repl_session import ReplSession
8
+ from adam.utils import debug, lines_to_tabular, log, log2, ing, log_exc
7
9
  from adam.utils_athena import Athena
8
10
  from adam.utils_k8s.statefulsets import StatefulSets
9
11
  from adam.utils_sqlite import SQLite
@@ -11,16 +13,23 @@ from adam.utils_sqlite import SQLite
11
13
  LIKE = 'e%_%'
12
14
 
13
15
  class ExportDatabases:
14
- def run_query(query: str, database: str, show_query=False) -> int:
16
+ def run_query(query: str, database: str, show_query=False, output: Callable[[str], str] = None) -> int:
15
17
  cnt: int = 0
16
18
 
17
19
  if show_query:
18
20
  log2(query)
19
21
 
22
+ log_file = None
20
23
  if database.startswith('s'):
21
- cnt += SQLite.run_query(query, database=database)
24
+ cnt0, log_file = SQLite.run_query(query, database=database, output=output)
25
+ cnt += cnt0
22
26
  else:
23
- cnt += Athena.run_query(query, database=database)
27
+ cnt0, log_file = Athena.run_query(query, database=database, output=output)
28
+ cnt += cnt0
29
+
30
+ if Config().get('repl.history.push-cat-log-file', True):
31
+ if log_file and ReplSession().prompt_session:
32
+ ReplSession().prompt_session.history.append_string(f':sh cat {log_file}')
24
33
 
25
34
  return cnt
26
35
 
@@ -47,15 +56,13 @@ class ExportDatabases:
47
56
  dbs = SQLite.database_names(db)
48
57
  if dbs:
49
58
  with ing(f'Droping {len(dbs)} SQLite databases'):
50
- try:
59
+ with log_exc():
51
60
  for db in dbs:
52
61
  file_path = f'{SQLite.local_db_dir()}/{db}'
53
62
  try:
54
63
  os.remove(file_path)
55
64
  except OSError as e:
56
65
  pass
57
- except:
58
- pass
59
66
 
60
67
  return dbs
61
68
 
@@ -65,20 +72,17 @@ class ExportDatabases:
65
72
  with ing(f'Droping {len(dbs)} Athena databases'):
66
73
  for db in dbs:
67
74
  query = f'DROP DATABASE {db} CASCADE'
68
- if Config().is_debug():
69
- log2(query)
75
+ debug(query)
70
76
  Athena.query(query)
71
77
 
72
78
  with ing(f'Deleting s3 folder: export'):
73
- try:
79
+ with log_exc():
74
80
  if not db:
75
81
  db = ''
76
82
 
77
83
  s3 = boto3.resource('s3')
78
84
  bucket = s3.Bucket(Config().get('export.bucket', 'c3.ops--qing'))
79
85
  bucket.objects.filter(Prefix=f'export/{db}').delete()
80
- except:
81
- pass
82
86
 
83
87
  return dbs
84
88
 
@@ -0,0 +1,71 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.commands.export.exporter import Exporter
4
+ from adam.commands.export.utils_export import ExportSpec, state_with_pod
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import log, log2
7
+
8
+ class ExportService:
9
+ def __init__(self, handler: 'ExporterHandler'):
10
+ self.handler = handler
11
+
12
+ def export(self, args: list[str], export_only=False):
13
+ state = self.handler.state
14
+ export_session = state.export_session
15
+ spec: ExportSpec = None
16
+ try:
17
+ with state_with_pod(state) as state:
18
+ # --export-only for testing only
19
+ statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
20
+ if not statuses:
21
+ return state
22
+
23
+ Exporter.clear_export_session_cache()
24
+
25
+ if spec.importer == 'csv' or export_only:
26
+ ExportDatabases.disply_export_session(state.sts, state.pod, state.namespace, spec.session)
27
+ else:
28
+ log()
29
+ ExportDatabases.display_export_db(state.export_session)
30
+ finally:
31
+ # if exporting to csv, do not bind the new session id to repl state
32
+ if spec and spec.importer == 'csv':
33
+ state.export_session = export_session
34
+
35
+ return state
36
+
37
+ def import_sesion(self, args: list[str]):
38
+ state = self.handler.state
39
+
40
+ if not args:
41
+ if state.in_repl:
42
+ log2('Specify export session name.')
43
+ else:
44
+ log2('* Export session name is missing.')
45
+
46
+ Command.display_help()
47
+
48
+ return 'command-missing'
49
+
50
+ with state_with_pod(state) as state:
51
+ tables, _ = Exporter.import_session(args, state)
52
+ if tables:
53
+ Exporter.clear_export_session_cache()
54
+
55
+ log()
56
+ ExportDatabases.display_export_db(state.export_session)
57
+
58
+ return state
59
+
60
+ class ExporterHandler:
61
+ def __init__(self, state: ReplState):
62
+ self.state = state
63
+
64
+ def __enter__(self):
65
+ return ExportService(self)
66
+
67
+ def __exit__(self, exc_type, exc_val, exc_tb):
68
+ return False
69
+
70
+ def export(state: ReplState):
71
+ return ExporterHandler(state)
@@ -1,10 +1,12 @@
1
+ from datetime import datetime
2
+ from adam.commands import extract_trailing_options
1
3
  from adam.commands.command import Command
2
4
  from adam.commands.export.export_databases import ExportDatabases
5
+ from adam.config import Config
3
6
  from adam.repl_state import ReplState, RequiredState
4
7
  from adam.sql.sql_completer import SqlCompleter, SqlVariant
5
8
  from adam.utils import log2
6
9
  from adam.utils_athena import Athena
7
- from adam.utils_sqlite import SQLite
8
10
 
9
11
  class ExportSelect(Command):
10
12
  COMMAND = '.select'
@@ -28,38 +30,45 @@ class ExportSelect(Command):
28
30
  if not(args := self.args(cmd)):
29
31
  return super().run(cmd, state)
30
32
 
31
- state, args = self.apply_state(args, state)
32
- if not self.validate_state(state):
33
- return state
33
+ with self.validate(args, state) as (args, state):
34
+ with extract_trailing_options(args, '&') as (args, backgrounded):
35
+ if not state.export_session:
36
+ if state.in_repl:
37
+ if state.device == ReplState.C:
38
+ log2("Select an export database first with 'use' command.")
39
+ else:
40
+ log2('cd to an export database first.')
41
+ else:
42
+ log2('* export database is missing.')
34
43
 
35
- if not state.export_session:
36
- if state.in_repl:
37
- if state.device == ReplState.C:
38
- log2("Select an export database first with 'use' command.")
39
- else:
40
- log2('cd to an export database first.')
41
- else:
42
- log2('* export database is missing.')
44
+ Command.display_help()
45
+
46
+ return 'command-missing'
47
+
48
+ if not args:
49
+ if state.in_repl:
50
+ log2('Use a SQL statement.')
51
+ else:
52
+ log2('* SQL statement is missing.')
43
53
 
44
- Command.display_help()
54
+ Command.display_help()
45
55
 
46
- return 'command-missing'
56
+ return 'command-missing'
47
57
 
48
- if not args:
49
- if state.in_repl:
50
- log2('Use a SQL statement.')
51
- else:
52
- log2('* SQL statement is missing.')
58
+ query = ' '.join(args)
53
59
 
54
- Command.display_help()
60
+ def output(out: str):
61
+ log_prefix = Config().get('export.log-prefix', '/tmp/qing')
62
+ log_file = f'{log_prefix}-{datetime.now().strftime("%d%H%M%S")}-sqlite.log'
55
63
 
56
- return 'command-missing'
64
+ with open(log_file, 'w') as f:
65
+ f.write(out)
57
66
 
58
- query = ' '.join(args)
67
+ return log_file
59
68
 
60
- ExportDatabases.run_query(f'select {query}', database=state.export_session)
69
+ ExportDatabases.run_query(f'select {query}', database=state.export_session, output=output if backgrounded else None)
61
70
 
62
- return state
71
+ return state
63
72
 
64
73
  def completion(self, state: ReplState):
65
74
  if not state.export_session: