kaqing 2.0.14__py3-none-any.whl → 2.0.189__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 (228) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +9 -12
  3. adam/apps.py +20 -6
  4. adam/batch.py +16 -6
  5. adam/checks/check_utils.py +19 -49
  6. adam/checks/compactionstats.py +1 -1
  7. adam/checks/cpu.py +9 -3
  8. adam/checks/cpu_metrics.py +52 -0
  9. adam/checks/disk.py +3 -4
  10. adam/checks/gossip.py +1 -1
  11. adam/checks/memory.py +3 -3
  12. adam/checks/status.py +1 -1
  13. adam/columns/columns.py +3 -1
  14. adam/columns/cpu.py +3 -1
  15. adam/columns/cpu_metrics.py +22 -0
  16. adam/columns/memory.py +3 -4
  17. adam/commands/__init__.py +24 -0
  18. adam/commands/alter_tables.py +66 -0
  19. adam/commands/app/app.py +38 -0
  20. adam/commands/{app_ping.py → app/app_ping.py} +8 -14
  21. adam/commands/app/show_app_actions.py +49 -0
  22. adam/commands/{show → app}/show_app_id.py +9 -12
  23. adam/commands/{show → app}/show_app_queues.py +8 -14
  24. adam/commands/app/utils_app.py +106 -0
  25. adam/commands/audit/__init__.py +0 -0
  26. adam/commands/audit/audit.py +67 -0
  27. adam/commands/audit/audit_repair_tables.py +72 -0
  28. adam/commands/audit/audit_run.py +50 -0
  29. adam/commands/audit/completions_l.py +15 -0
  30. adam/commands/audit/show_last10.py +36 -0
  31. adam/commands/audit/show_slow10.py +36 -0
  32. adam/commands/audit/show_top10.py +36 -0
  33. adam/commands/audit/utils_show_top10.py +71 -0
  34. adam/commands/bash/__init__.py +5 -0
  35. adam/commands/bash/bash.py +36 -0
  36. adam/commands/bash/bash_completer.py +93 -0
  37. adam/commands/bash/utils_bash.py +16 -0
  38. adam/commands/cat.py +36 -0
  39. adam/commands/cd.py +14 -88
  40. adam/commands/check.py +18 -21
  41. adam/commands/cli_commands.py +11 -7
  42. adam/commands/clipboard_copy.py +87 -0
  43. adam/commands/code.py +57 -0
  44. adam/commands/command.py +220 -19
  45. adam/commands/commands_utils.py +28 -31
  46. adam/commands/cql/__init__.py +0 -0
  47. adam/commands/cql/completions_c.py +28 -0
  48. adam/commands/{cqlsh.py → cql/cqlsh.py} +13 -32
  49. adam/commands/cql/utils_cql.py +305 -0
  50. adam/commands/deploy/code_start.py +7 -10
  51. adam/commands/deploy/code_stop.py +4 -21
  52. adam/commands/deploy/code_utils.py +5 -5
  53. adam/commands/deploy/deploy.py +4 -40
  54. adam/commands/deploy/deploy_frontend.py +15 -18
  55. adam/commands/deploy/deploy_pg_agent.py +4 -7
  56. adam/commands/deploy/deploy_pod.py +74 -77
  57. adam/commands/deploy/deploy_utils.py +16 -26
  58. adam/commands/deploy/undeploy.py +4 -40
  59. adam/commands/deploy/undeploy_frontend.py +5 -8
  60. adam/commands/deploy/undeploy_pg_agent.py +7 -8
  61. adam/commands/deploy/undeploy_pod.py +16 -17
  62. adam/commands/devices/__init__.py +0 -0
  63. adam/commands/devices/device.py +149 -0
  64. adam/commands/devices/device_app.py +163 -0
  65. adam/commands/devices/device_auit_log.py +49 -0
  66. adam/commands/devices/device_cass.py +179 -0
  67. adam/commands/devices/device_export.py +87 -0
  68. adam/commands/devices/device_postgres.py +160 -0
  69. adam/commands/devices/devices.py +25 -0
  70. adam/commands/download_file.py +47 -0
  71. adam/commands/exit.py +1 -4
  72. adam/commands/export/__init__.py +0 -0
  73. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  74. adam/commands/export/clean_up_export_sessions.py +39 -0
  75. adam/commands/export/completions_x.py +11 -0
  76. adam/commands/export/download_export_session.py +40 -0
  77. adam/commands/export/drop_export_database.py +39 -0
  78. adam/commands/export/drop_export_databases.py +37 -0
  79. adam/commands/export/export.py +37 -0
  80. adam/commands/export/export_databases.py +246 -0
  81. adam/commands/export/export_select.py +34 -0
  82. adam/commands/export/export_sessions.py +209 -0
  83. adam/commands/export/export_use.py +49 -0
  84. adam/commands/export/export_x_select.py +48 -0
  85. adam/commands/export/exporter.py +332 -0
  86. adam/commands/export/import_files.py +44 -0
  87. adam/commands/export/import_session.py +44 -0
  88. adam/commands/export/importer.py +81 -0
  89. adam/commands/export/importer_athena.py +148 -0
  90. adam/commands/export/importer_sqlite.py +67 -0
  91. adam/commands/export/show_column_counts.py +45 -0
  92. adam/commands/export/show_export_databases.py +39 -0
  93. adam/commands/export/show_export_session.py +39 -0
  94. adam/commands/export/show_export_sessions.py +37 -0
  95. adam/commands/export/utils_export.py +344 -0
  96. adam/commands/find_files.py +51 -0
  97. adam/commands/find_processes.py +76 -0
  98. adam/commands/head.py +36 -0
  99. adam/commands/help.py +14 -9
  100. adam/commands/intermediate_command.py +52 -0
  101. adam/commands/issues.py +14 -40
  102. adam/commands/kubectl.py +38 -0
  103. adam/commands/login.py +26 -25
  104. adam/commands/logs.py +5 -7
  105. adam/commands/ls.py +11 -115
  106. adam/commands/medusa/medusa.py +4 -46
  107. adam/commands/medusa/medusa_backup.py +22 -29
  108. adam/commands/medusa/medusa_restore.py +51 -49
  109. adam/commands/medusa/medusa_show_backupjobs.py +20 -21
  110. adam/commands/medusa/medusa_show_restorejobs.py +16 -21
  111. adam/commands/medusa/utils_medusa.py +15 -0
  112. adam/commands/nodetool.py +8 -17
  113. adam/commands/param_get.py +11 -14
  114. adam/commands/param_set.py +9 -13
  115. adam/commands/postgres/completions_p.py +22 -0
  116. adam/commands/postgres/postgres.py +49 -73
  117. adam/commands/postgres/postgres_databases.py +270 -0
  118. adam/commands/postgres/postgres_ls.py +4 -8
  119. adam/commands/postgres/postgres_preview.py +5 -9
  120. adam/commands/postgres/utils_postgres.py +79 -0
  121. adam/commands/preview_table.py +10 -69
  122. adam/commands/pwd.py +14 -43
  123. adam/commands/reaper/reaper.py +6 -49
  124. adam/commands/reaper/reaper_forward.py +49 -56
  125. adam/commands/reaper/reaper_forward_session.py +6 -0
  126. adam/commands/reaper/reaper_forward_stop.py +10 -16
  127. adam/commands/reaper/reaper_restart.py +8 -15
  128. adam/commands/reaper/reaper_run_abort.py +8 -33
  129. adam/commands/reaper/reaper_runs.py +43 -58
  130. adam/commands/reaper/reaper_runs_abort.py +29 -49
  131. adam/commands/reaper/reaper_schedule_activate.py +14 -33
  132. adam/commands/reaper/reaper_schedule_start.py +9 -33
  133. adam/commands/reaper/reaper_schedule_stop.py +9 -33
  134. adam/commands/reaper/reaper_schedules.py +4 -14
  135. adam/commands/reaper/reaper_status.py +8 -16
  136. adam/commands/reaper/utils_reaper.py +203 -0
  137. adam/commands/repair/repair.py +4 -46
  138. adam/commands/repair/repair_log.py +6 -12
  139. adam/commands/repair/repair_run.py +29 -36
  140. adam/commands/repair/repair_scan.py +33 -41
  141. adam/commands/repair/repair_stop.py +6 -13
  142. adam/commands/report.py +25 -21
  143. adam/commands/restart.py +27 -28
  144. adam/commands/rollout.py +20 -25
  145. adam/commands/shell.py +12 -4
  146. adam/commands/show/show.py +15 -46
  147. adam/commands/show/show_adam.py +3 -3
  148. adam/commands/show/show_cassandra_repairs.py +37 -0
  149. adam/commands/show/show_cassandra_status.py +48 -52
  150. adam/commands/show/show_cassandra_version.py +5 -18
  151. adam/commands/show/show_cli_commands.py +56 -0
  152. adam/commands/show/show_host.py +33 -0
  153. adam/commands/show/show_login.py +23 -27
  154. adam/commands/show/show_params.py +2 -5
  155. adam/commands/show/show_processes.py +18 -21
  156. adam/commands/show/show_storage.py +11 -20
  157. adam/commands/watch.py +27 -30
  158. adam/config.py +8 -6
  159. adam/embedded_params.py +1 -1
  160. adam/log.py +4 -4
  161. adam/pod_exec_result.py +13 -5
  162. adam/repl.py +136 -120
  163. adam/repl_commands.py +66 -24
  164. adam/repl_session.py +8 -1
  165. adam/repl_state.py +343 -73
  166. adam/sql/__init__.py +0 -0
  167. adam/sql/lark_completer.py +284 -0
  168. adam/sql/lark_parser.py +604 -0
  169. adam/sql/sql_completer.py +118 -0
  170. adam/sql/sql_state_machine.py +630 -0
  171. adam/sql/term_completer.py +76 -0
  172. adam/sso/authn_ad.py +7 -9
  173. adam/sso/authn_okta.py +4 -6
  174. adam/sso/cred_cache.py +4 -6
  175. adam/sso/idp.py +10 -13
  176. adam/utils.py +539 -11
  177. adam/utils_athena.py +145 -0
  178. adam/utils_audits.py +102 -0
  179. adam/utils_issues.py +32 -0
  180. adam/utils_k8s/__init__.py +0 -0
  181. adam/utils_k8s/app_clusters.py +28 -0
  182. adam/utils_k8s/app_pods.py +36 -0
  183. adam/utils_k8s/cassandra_clusters.py +44 -0
  184. adam/{k8s_utils → utils_k8s}/cassandra_nodes.py +12 -5
  185. adam/{k8s_utils → utils_k8s}/custom_resources.py +16 -17
  186. adam/{k8s_utils → utils_k8s}/deployment.py +2 -2
  187. adam/{k8s_utils → utils_k8s}/ingresses.py +2 -2
  188. adam/{k8s_utils → utils_k8s}/jobs.py +7 -11
  189. adam/utils_k8s/k8s.py +96 -0
  190. adam/{k8s_utils → utils_k8s}/kube_context.py +3 -3
  191. adam/{k8s_utils → utils_k8s}/pods.py +132 -83
  192. adam/{k8s_utils → utils_k8s}/secrets.py +7 -3
  193. adam/{k8s_utils → utils_k8s}/service_accounts.py +5 -4
  194. adam/{k8s_utils → utils_k8s}/services.py +2 -2
  195. adam/{k8s_utils → utils_k8s}/statefulsets.py +9 -16
  196. adam/utils_local.py +4 -0
  197. adam/utils_net.py +24 -0
  198. adam/utils_repl/__init__.py +0 -0
  199. adam/utils_repl/appendable_completer.py +6 -0
  200. adam/utils_repl/automata_completer.py +48 -0
  201. adam/utils_repl/repl_completer.py +172 -0
  202. adam/utils_repl/state_machine.py +173 -0
  203. adam/utils_sqlite.py +137 -0
  204. adam/version.py +1 -1
  205. {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/METADATA +1 -1
  206. kaqing-2.0.189.dist-info/RECORD +253 -0
  207. kaqing-2.0.189.dist-info/top_level.txt +2 -0
  208. teddy/__init__.py +0 -0
  209. teddy/lark_parser.py +436 -0
  210. teddy/lark_parser2.py +618 -0
  211. adam/commands/app.py +0 -67
  212. adam/commands/bash.py +0 -87
  213. adam/commands/cp.py +0 -95
  214. adam/commands/cql_utils.py +0 -53
  215. adam/commands/devices.py +0 -89
  216. adam/commands/postgres/postgres_session.py +0 -247
  217. adam/commands/reaper/reaper_session.py +0 -159
  218. adam/commands/show/show_app_actions.py +0 -53
  219. adam/commands/show/show_commands.py +0 -61
  220. adam/commands/show/show_repairs.py +0 -47
  221. adam/k8s_utils/cassandra_clusters.py +0 -48
  222. kaqing-2.0.14.dist-info/RECORD +0 -167
  223. kaqing-2.0.14.dist-info/top_level.txt +0 -1
  224. /adam/{k8s_utils → commands/app}/__init__.py +0 -0
  225. /adam/{k8s_utils → utils_k8s}/config_maps.py +0 -0
  226. /adam/{k8s_utils → utils_k8s}/volumes.py +0 -0
  227. {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/WHEEL +0 -0
  228. {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,344 @@
1
+ import io
2
+ import re
3
+
4
+ from adam.config import Config
5
+ from adam.pod_exec_result import PodExecResult
6
+ from adam.repl_state import ReplState
7
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
8
+ from adam.utils_k8s.pods import log_prefix
9
+ from adam.utils_k8s.statefulsets import StatefulSets
10
+
11
+ class ImportSpec:
12
+ def __init__(self, table_name: str, session: str = None, files: list[str] = None, importer: str = None):
13
+ self.table_name = table_name
14
+ self.session = session
15
+ self.files = files
16
+ self.importer = importer
17
+
18
+ def parse_specs(specs_str: str, files = False):
19
+ session_or_files: str = None
20
+ importer: str = None
21
+ table_name: str = None
22
+
23
+ if specs_str:
24
+ importer, rest = ImportSpec._extract_importer(specs_str.strip(' '))
25
+
26
+ if rest:
27
+ table_name, session_or_files = ImportSpec._extract_table_name(rest)
28
+
29
+ if not files:
30
+ return ImportSpec(table_name, session=session_or_files, importer=importer)
31
+
32
+ return ImportSpec(table_name, files=[f.strip(' ') for f in session_or_files.split(',')], importer=importer)
33
+
34
+ def _extract_importer(spec_str: str) -> tuple[str, str]:
35
+ importer = None
36
+ rest = spec_str
37
+
38
+ p = re.compile(r"(.*?)to\s+(.*)", re.IGNORECASE)
39
+ match = p.match(spec_str)
40
+ if match:
41
+ rest = match.group(1).strip(' ')
42
+ importer = match.group(2).strip(' ')
43
+
44
+ return importer, rest
45
+
46
+ def _extract_table_name(spec_str: str) -> tuple[str, str]:
47
+ table_name = None
48
+ rest = spec_str
49
+
50
+ p = re.compile(r"(.*?)as\s+(.*)", re.IGNORECASE)
51
+ match = p.match(spec_str)
52
+ if match:
53
+ rest = match.group(1).strip(' ')
54
+ table_name = match.group(2).strip(' ')
55
+
56
+ return table_name, rest
57
+
58
+ class ExportSpec(ImportSpec):
59
+ def __init__(self, keyspace: str, consistency: str, importer: str, tables: list['ExportTableSpec'], session: str = None):
60
+ super().__init__(None, importer)
61
+
62
+ self.keyspace = keyspace
63
+ self.consistency = consistency
64
+ self.importer = importer
65
+ self.tables = tables
66
+ self.session = session
67
+
68
+ def __str__(self):
69
+ return f'keyspace: {self.keyspace}, consistency: {self.consistency}, importer: {self.importer}, tables: {",".join([t.table for t in self.tables])}, session: {self.session}'
70
+
71
+ def __eq__(self, other):
72
+ if not isinstance(other, ExportSpec):
73
+ return NotImplemented
74
+
75
+ return self.keyspace == other.keyspace and self.tables == other.tables and self.consistency == other.consistency and self.importer == other.importer and self.session == other.session
76
+
77
+ def parse_specs(specs_str: str):
78
+ keyspace: str = None
79
+ consistency: str = None
80
+ importer: str = None
81
+ specs: list[ExportTableSpec] = None
82
+
83
+ if specs_str:
84
+ importer, specs_str = ExportSpec._extract_importer(specs_str.strip(' '))
85
+ keyspace, specs_str = ExportSpec._extract_keyspace(specs_str)
86
+ consistency, specs = ExportSpec._extract_consisteny(specs_str)
87
+
88
+ return ExportSpec(keyspace, consistency, importer, specs)
89
+
90
+ def _extract_keyspace(spec_str: str) -> tuple[str, str]:
91
+ keyspace = None
92
+ rest = spec_str
93
+
94
+ p = re.compile(r"\s*\*\s+in\s+(\S+)(.*)", re.IGNORECASE)
95
+ match = p.match(spec_str)
96
+ if match:
97
+ keyspace = match.group(1).strip(' ')
98
+ rest = match.group(2).strip(' ')
99
+ elif spec_str.startswith('*'):
100
+ keyspace = '*'
101
+ rest = spec_str[1:].strip(' ')
102
+
103
+ return keyspace, rest
104
+
105
+ def _extract_consisteny(spec_str: str) -> tuple[str, list['ExportTableSpec']]:
106
+ consistency = None
107
+
108
+ p = re.compile(r"(.*?)with\s+consistency\s+(.*)", re.IGNORECASE)
109
+ match = p.match(spec_str)
110
+ if match:
111
+ spec_str = match.group(1).strip(' ')
112
+ consistency = match.group(2)
113
+
114
+ if spec_str:
115
+ p = r",\s*(?![^()]*\))"
116
+ specs = re.split(p, spec_str)
117
+
118
+ return consistency, [ExportTableSpec.parse(spec) for spec in specs]
119
+
120
+ return consistency, None
121
+
122
+ class ExportTableSpec:
123
+ def __init__(self, keyspace: str, table: str, columns: str = None, target_table: str = None):
124
+ self.keyspace = keyspace
125
+ self.table = table
126
+ self.columns = columns
127
+ self.target_table = target_table
128
+
129
+ def __str__(self):
130
+ return f'{self.keyspace}.{self.table}({self.columns}) AS {self.target_table}'
131
+
132
+ def __eq__(self, other):
133
+ if not isinstance(other, ExportTableSpec):
134
+ return NotImplemented
135
+
136
+ return self.keyspace == other.keyspace and self.table == other.table and self.columns == other.columns and self.target_table == other.target_table
137
+
138
+ def from_status(status: 'ExportTableStatus'):
139
+ return ExportTableSpec(status.keyspace, status.table, target_table=status.target_table)
140
+
141
+ def parse(spec_str: str) -> 'ExportTableSpec':
142
+ target = None
143
+
144
+ p = re.compile(r"(.*?)\s+as\s+(.*)", re.IGNORECASE)
145
+ match = p.match(spec_str)
146
+ if match:
147
+ spec_str = match.group(1)
148
+ target = match.group(2)
149
+
150
+ keyspace = None
151
+ table = spec_str
152
+ columns = None
153
+
154
+ p = re.compile('(.*?)\.(.*?)\((.*)\)')
155
+ match = p.match(spec_str)
156
+ if match:
157
+ keyspace = match.group(1)
158
+ table = match.group(2)
159
+ columns = match.group(3)
160
+ else:
161
+ p = re.compile('(.*?)\.(.*)')
162
+ match = p.match(spec_str)
163
+ if match:
164
+ keyspace = match.group(1)
165
+ table = match.group(2)
166
+
167
+ return ExportTableSpec(keyspace, table, columns, target)
168
+
169
+ def __eq__(self, other):
170
+ if isinstance(other, ExportTableSpec):
171
+ return self.keyspace == other.keyspace and self.table == other.table and self.columns == other.columns and self.target_table == other.target_table
172
+
173
+ return False
174
+
175
+ def __str__(self):
176
+ return f'{self.keyspace}.{self.table}({self.columns}) as {self.target_table}'
177
+
178
+ class ExportTableStatus:
179
+ def __init__(self, keyspace: str, target_table: str, status: str, table: str = None, csv_file: str = ''):
180
+ self.keyspace = keyspace
181
+ self.target_table = target_table
182
+ self.status = status
183
+ self.table = table
184
+ self.csv_file = csv_file
185
+
186
+ def __str__(self):
187
+ return f'{self.keyspace}.{self.table} as {self.target_table} = {self.status}'
188
+
189
+ def __eq__(self, other):
190
+ if isinstance(other, ExportTableStatus):
191
+ return self.keyspace == other.keyspace and self.table == other.table and self.status == other.status and self.target_table == other.target_table
192
+
193
+ return False
194
+
195
+ def from_session(sts: str, pod: str, namespace: str, export_session: str) -> tuple['ExportTableStatus', str]:
196
+ statuses: list[ExportTableStatus] = []
197
+
198
+ status_in_whole = 'done'
199
+ log_files: list[str] = find_files(pod, namespace, f'{log_prefix()}-{export_session}_*.log*')
200
+
201
+ for log_file in log_files:
202
+ status: ExportTableStatus = ExportTableStatus.from_log_file(pod, namespace, export_session, log_file)
203
+ statuses.append(status)
204
+
205
+ if status.status != 'done':
206
+ status_in_whole = status.status
207
+
208
+ return statuses, status_in_whole
209
+
210
+ def from_log_file(pod: str, namespace: str, copy_session: str, log_file: str):
211
+ def get_csv_files_n_table(target_table: str):
212
+ db = f'{copy_session}_{target_table}'
213
+ csv_file = f'{csv_dir()}/{db}/*.csv'
214
+ csv_files: list[str] = find_files(pod, namespace, csv_file)
215
+ if csv_files:
216
+ table = target_table
217
+ m = re.match(f'{csv_dir()}/{db}/(.*).csv', csv_files[0])
218
+ if m:
219
+ table = m.group(1)
220
+ return csv_files, table
221
+
222
+ return csv_files, target_table
223
+
224
+ m = re.match(f'{log_prefix()}-{copy_session}_(.*?)\.(.*?)\.log(.*)', log_file)
225
+ if m:
226
+ keyspace = m.group(1)
227
+ target_table = m.group(2)
228
+ state = m.group(3)
229
+ if state == '.pending_import':
230
+ csv_files, table = get_csv_files_n_table(target_table)
231
+ return ExportTableStatus(keyspace, target_table, 'pending_import', table, csv_files[0] if csv_files else '')
232
+ elif state == '.done':
233
+ return ExportTableStatus(keyspace, target_table, 'done', target_table)
234
+
235
+ # 4 rows exported to 1 files in 0 day, 0 hour, 0 minute, and 1.335 seconds.
236
+ pattern = 'rows exported to'
237
+ r: PodExecResult = CassandraNodes.exec(pod, namespace, f"grep '{pattern}' {log_file}", show_out=Config().is_debug(), shell='bash')
238
+ if r.exit_code() == 0:
239
+ csv_files, table = get_csv_files_n_table(target_table)
240
+ if csv_files:
241
+ return ExportTableStatus(keyspace, target_table, 'exported', table, csv_files[0])
242
+ else:
243
+ return ExportTableStatus(keyspace, target_table, 'imported', target_table)
244
+ else:
245
+ return ExportTableStatus(keyspace, target_table, 'export_in_pregress')
246
+
247
+ return ExportTableStatus(None, None, 'unknown')
248
+
249
+ def csv_dir():
250
+ return Config().get('export.csv_dir', '/c3/cassandra/tmp')
251
+
252
+ def find_files(pod: str, namespace: str, pattern: str, mmin: int = 0):
253
+ if mmin:
254
+ r = CassandraNodes.exec(pod, namespace, f'find {pattern} -mmin -{mmin}', show_out=Config().is_debug(), shell='bash')
255
+ else:
256
+ r = CassandraNodes.exec(pod, namespace, f'find {pattern}', show_out=Config().is_debug(), shell='bash')
257
+
258
+ log_files = []
259
+ for line in r.stdout.split('\n'):
260
+ line = line.strip(' \r')
261
+ if line:
262
+ log_files.append(line)
263
+
264
+ return log_files
265
+
266
+ class GeneratorStream(io.RawIOBase):
267
+ def __init__(self, generator):
268
+ self._generator = generator
269
+ self._buffer = b'' # Buffer to store leftover bytes from generator yields
270
+
271
+ def readable(self):
272
+ return True
273
+
274
+ def _read_from_generator(self):
275
+ try:
276
+ chunk = next(self._generator)
277
+ if isinstance(chunk, str):
278
+ chunk = chunk.encode('utf-8') # Encode if generator yields strings
279
+ self._buffer += chunk
280
+ except StopIteration:
281
+ pass # Generator exhausted
282
+
283
+ def readinto(self, b):
284
+ # Fill the buffer if necessary
285
+ while len(self._buffer) < len(b):
286
+ old_buffer_len = len(self._buffer)
287
+ self._read_from_generator()
288
+ if len(self._buffer) == old_buffer_len: # Generator exhausted and buffer empty
289
+ break
290
+
291
+ bytes_to_read = min(len(b), len(self._buffer))
292
+ b[:bytes_to_read] = self._buffer[:bytes_to_read]
293
+ self._buffer = self._buffer[bytes_to_read:]
294
+ return bytes_to_read
295
+
296
+ def read(self, size=-1):
297
+ if size == -1: # Read all remaining data
298
+ while True:
299
+ old_buffer_len = len(self._buffer)
300
+ self._read_from_generator()
301
+ if len(self._buffer) == old_buffer_len:
302
+ break
303
+ data = self._buffer
304
+ self._buffer = b''
305
+ return data
306
+ else:
307
+ # Ensure enough data in buffer
308
+ while len(self._buffer) < size:
309
+ old_buffer_len = len(self._buffer)
310
+ self._read_from_generator()
311
+ if len(self._buffer) == old_buffer_len:
312
+ break
313
+
314
+ data = self._buffer[:size]
315
+ self._buffer = self._buffer[size:]
316
+ return data
317
+
318
+ class PodPushHandler:
319
+ def __init__(self, state: ReplState, pod: str = None):
320
+ self.state = state
321
+ self.pushed = False
322
+ self.pod = pod
323
+
324
+ def __enter__(self):
325
+ state = self.state
326
+
327
+ if not state.pod:
328
+ self.pushed = True
329
+ state.push()
330
+
331
+ if not self.pod:
332
+ self.pod = StatefulSets.pod_names(state.sts, state.namespace)[0]
333
+ state.pod = self.pod
334
+
335
+ return state
336
+
337
+ def __exit__(self, exc_type, exc_val, exc_tb):
338
+ if self.pushed:
339
+ self.state.pop()
340
+
341
+ return False
342
+
343
+ def state_with_pod(state: ReplState, pod: str = None):
344
+ return PodPushHandler(state, pod=pod)
@@ -0,0 +1,51 @@
1
+ import os
2
+
3
+ from adam.commands.command import Command
4
+ from adam.repl_state import ReplState
5
+ from adam.utils import log2
6
+ from adam.utils_local import local_tmp_dir
7
+
8
+ class FindLocalFiles(Command):
9
+ COMMAND = 'find local'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(FindLocalFiles, 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 FindLocalFiles.COMMAND
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
+ cmd = 'find'
29
+
30
+ if not args:
31
+ cmd = f'find {local_tmp_dir()}'
32
+ elif len(args) == 1:
33
+ cmd = f"find {local_tmp_dir()} -name '{args[0]}'"
34
+ else:
35
+ new_args = [f"'{arg}'" if '*' in arg else arg for arg in args]
36
+ cmd = 'find ' + ' '.join(new_args)
37
+
38
+ log2(cmd)
39
+ os.system(cmd)
40
+
41
+ return state
42
+
43
+ def completion(self, state: ReplState):
44
+ return super().completion(state, {
45
+ '*.csv': None,
46
+ '*.db': None,
47
+ '*': None
48
+ })
49
+
50
+ def help(self, _: ReplState):
51
+ return f'{FindLocalFiles.COMMAND} [linux-find-arguments]\t find files from local machine'
@@ -0,0 +1,76 @@
1
+ from adam.commands import extract_options, validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.devices.devices import Devices
4
+ from adam.commands.export.utils_export import state_with_pod
5
+ from adam.repl_state import ReplState, RequiredState
6
+ from adam.utils import log2, tabulize
7
+
8
+ class FindProcesses(Command):
9
+ COMMAND = 'find processes'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(FindProcesses, 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 FindProcesses.COMMAND
22
+
23
+ def required(self):
24
+ return [RequiredState.CLUSTER_OR_POD, RequiredState.APP_APP, ReplState.P]
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 extract_options(args, '-kill') as (args, kill):
32
+ with validate_args(args, state, name='words to look for'):
33
+ arg = ' | '.join([f'grep {a}' for a in args])
34
+ awk = "awk '{ print $1, $2, $8, $NF }'"
35
+ rs = Devices.device(state).bash(state, state, f"ps -ef | grep -v grep | {arg} | {awk}".split(' '))
36
+
37
+ lines: list[list[str]] = []
38
+ for r in rs:
39
+ for l in r.stdout.split('\n'):
40
+ l = l.strip(' \t\r\n')
41
+ if not l:
42
+ continue
43
+
44
+ tokens = [r.pod] + l.split(' ')
45
+ lines.append(tokens)
46
+
47
+ pids = []
48
+ for l in lines:
49
+ pids.append(f'{l[2]}@{l[0]}')
50
+
51
+ tabulize(lines, lambda l: '\t'.join(l), header = 'POD\tUSER\tPID\tCMD\tLAST_ARG', separator='\t')
52
+ log2()
53
+ log2(f'PIDS with {",".join(args)}: {",".join(pids)}')
54
+
55
+ if kill:
56
+ log2()
57
+ for pidp in pids:
58
+ pid_n_pod = pidp.split('@')
59
+ pid = pid_n_pod[0]
60
+ if len(pid_n_pod) < 2:
61
+ continue
62
+
63
+ pod = pid_n_pod[1]
64
+
65
+ log2(f'@{pod} bash kill -9 {pid}')
66
+
67
+ with state_with_pod(state, pod) as state1:
68
+ Devices.device(state).bash(state, state1, ['kill', '-9', pid])
69
+
70
+ return rs
71
+
72
+ def completion(self, state: ReplState):
73
+ return super().completion(state)
74
+
75
+ def help(self, _: ReplState):
76
+ return f'{FindProcesses.COMMAND} word... [-kill]\t find processes with words'
adam/commands/head.py ADDED
@@ -0,0 +1,36 @@
1
+ from adam.commands import validate_args
2
+ from adam.commands.command import Command
3
+ from adam.commands.devices.devices import Devices
4
+ from adam.repl_state import ReplState, RequiredState
5
+
6
+ class Head(Command):
7
+ COMMAND = 'head'
8
+
9
+ # the singleton pattern
10
+ def __new__(cls, *args, **kwargs):
11
+ if not hasattr(cls, 'instance'): cls.instance = super(Head, cls).__new__(cls)
12
+
13
+ return cls.instance
14
+
15
+ def __init__(self, successor: Command=None):
16
+ super().__init__(successor)
17
+
18
+ def command(self):
19
+ return Head.COMMAND
20
+
21
+ def required(self):
22
+ return [RequiredState.CLUSTER_OR_POD, RequiredState.APP_APP, ReplState.P]
23
+
24
+ def run(self, cmd: str, state: ReplState):
25
+ if not(args := self.args(cmd)):
26
+ return super().run(cmd, state)
27
+
28
+ with self.validate(args, state) as (args, state):
29
+ with validate_args(args, state, name='file'):
30
+ return Devices.device(state).bash(state, state, cmd.split(' '))
31
+
32
+ def completion(self, state: ReplState):
33
+ return super().completion(state, lambda: {f: None for f in Devices.device(state).files(state)}, pods=Devices.device(state).pods(state, '-'), auto='jit')
34
+
35
+ def help(self, _: ReplState):
36
+ return f'{Head.COMMAND} file [&]\t run head command on the pod'
adam/commands/help.py CHANGED
@@ -1,7 +1,7 @@
1
1
  from adam.commands.command import Command
2
2
  from adam.repl_commands import ReplCommands
3
3
  from adam.repl_state import ReplState
4
- from adam.utils import lines_to_tabular, log
4
+ from adam.utils import tabulize, log
5
5
 
6
6
  class Help(Command):
7
7
  COMMAND = 'help'
@@ -23,24 +23,29 @@ class Help(Command):
23
23
  return super().run(cmd, state)
24
24
 
25
25
  def section(cmds : list[ReplCommands]):
26
- return [f' {c.help(state)}' for c in cmds if c.help(state)]
26
+ sorted_cmds = sorted(cmds, key=lambda cmd: cmd.command())
27
+ return [f' {c.help(state)}' for c in sorted_cmds if c.help(state)]
27
28
 
28
29
  lines = []
29
30
  lines.append('NAVIGATION')
30
- lines.append(' a: | c: | p:\t switch to another operational device: App, Cassandra or Postgres')
31
+ lines.append(' a: | c: | l: | p: | x:\t switch to another operational device: App, Cassandra, Audit, Postgres or Export')
31
32
  lines.extend(section(ReplCommands.navigation()))
32
- lines.append('CHECK CASSANDRA')
33
- lines.extend(section(ReplCommands.cassandra_check()))
34
- lines.append('CASSANDRA OPERATIONS')
33
+ lines.append('CASSANDRA')
35
34
  lines.extend(section(ReplCommands.cassandra_ops()))
35
+ lines.append('POSTGRES')
36
+ lines.extend(section(ReplCommands.postgres_ops()))
37
+ lines.append('APP')
38
+ lines.extend(section(ReplCommands.app_ops()))
39
+ lines.append('EXPORT DB')
40
+ lines.extend(section(ReplCommands.export_ops()))
41
+ lines.append('AUDIT')
42
+ lines.extend(section(ReplCommands.audit_ops()))
36
43
  lines.append('TOOLS')
37
44
  lines.extend(section(ReplCommands.tools()))
38
- lines.append('APP')
39
- lines.extend(section(ReplCommands.app()))
40
45
  lines.append('')
41
46
  lines.extend(section(ReplCommands.exit()))
42
47
 
43
- log(lines_to_tabular(lines, separator='\t'))
48
+ tabulize(lines, separator='\t')
44
49
 
45
50
  return lines
46
51
 
@@ -0,0 +1,52 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.command import Command
4
+ from adam.commands.command_helpers import ClusterCommandHelper
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import tabulize, log, log2
7
+
8
+ class IntermediateCommand(Command):
9
+ def run(self, cmd: str, state: ReplState):
10
+ if not(args := self.args(cmd)):
11
+ return super().run(cmd, state)
12
+
13
+ return self.intermediate_run(cmd, state, args, self.cmd_list())
14
+
15
+ def completion(self, state: ReplState):
16
+ return {}
17
+
18
+ @abstractmethod
19
+ def cmd_list(self):
20
+ pass
21
+
22
+ def intermediate_run(self, cmd: str, state: ReplState, args: list[str], cmds: list['Command'], separator='\t', display_help=True):
23
+ state, _ = self.apply_state(args, state)
24
+
25
+ if state.in_repl:
26
+ if display_help:
27
+ tabulize(cmds, lambda c: c.help(state), separator=separator)
28
+
29
+ return 'command-missing'
30
+ else:
31
+ # head with the Chain of Responsibility pattern
32
+ if not self.run_subcommand(cmd, state):
33
+ if display_help:
34
+ log2('* Command is missing.')
35
+ Command.display_help()
36
+ return 'command-missing'
37
+
38
+ return state
39
+
40
+ def run_subcommand(self, cmd: str, state: ReplState):
41
+ cmds = Command.chain(self.cmd_list())
42
+ return cmds.run(cmd, state)
43
+
44
+ def intermediate_help(super_help: str, cmd: str, cmd_list: list['Command'], separator='\t', show_cluster_help=False):
45
+ log(super_help)
46
+ log()
47
+ log('Sub-Commands:')
48
+
49
+ tabulize(cmd_list, lambda c: c.help(ReplState()).replace(f'{cmd} ', ' ', 1), separator=separator)
50
+ if show_cluster_help:
51
+ log()
52
+ ClusterCommandHelper.cluster_help()
adam/commands/issues.py CHANGED
@@ -1,10 +1,9 @@
1
1
  from adam.checks.check_result import CheckResult
2
2
  from adam.checks.check_utils import run_checks
3
- from adam.checks.issue import Issue
3
+ from adam.commands import extract_options
4
4
  from adam.commands.command import Command
5
- from adam.repl_session import ReplSession
6
5
  from adam.repl_state import ReplState
7
- from adam.utils import lines_to_tabular, log, log2
6
+ from adam.utils_issues import IssuesUtils
8
7
 
9
8
  class Issues(Command):
10
9
  COMMAND = 'issues'
@@ -21,49 +20,24 @@ class Issues(Command):
21
20
  def command(self):
22
21
  return Issues.COMMAND
23
22
 
23
+ def required(self):
24
+ return ReplState.NON_L
25
+
24
26
  def run(self, cmd: str, state: ReplState):
25
27
  if not(args := self.args(cmd)):
26
28
  return super().run(cmd, state)
27
29
 
28
- state, args = self.apply_state(args, state)
29
- args, show = Command.extract_options(args, ['-s', '--show'])
30
-
31
- results = run_checks(state.sts, state.namespace, state.pod, show_output=show)
32
-
33
- issues = CheckResult.collect_issues(results)
34
- Issues.show_issues(issues, in_repl=state.in_repl)
35
-
36
- return issues if issues else 'issues'
37
-
38
- def show(check_results: list[CheckResult], in_repl = False):
39
- Issues.show_issues(CheckResult.collect_issues(check_results), in_repl=in_repl)
30
+ with self.validate(args, state) as (args, state):
31
+ with extract_options(args, ['-s', '--show']) as (args, show_out):
32
+ results = run_checks(state.sts, state.namespace, state.pod, show_out=show_out)
40
33
 
41
- def show_issues(issues: list[Issue], in_repl = False):
42
- if not issues:
43
- log2('No issues found.')
44
- else:
45
- suggested = 0
46
- log2(f'* {len(issues)} issues found.')
47
- lines = []
48
- for i, issue in enumerate(issues, start=1):
49
- lines.append(f"{i}||{issue.category}||{issue.desc}")
50
- lines.append(f"||statefulset||{issue.statefulset}@{issue.namespace}")
51
- lines.append(f"||pod||{issue.pod}@{issue.namespace}")
52
- if issue.details:
53
- lines.append(f"||details||{issue.details}")
34
+ issues = CheckResult.collect_issues(results)
35
+ IssuesUtils.show_issues(issues, in_repl=state.in_repl)
54
36
 
55
- if issue.suggestion:
56
- lines.append(f'||suggestion||{issue.suggestion}')
57
- if in_repl:
58
- ReplSession().prompt_session.history.append_string(issue.suggestion)
59
- suggested += 1
60
- log(lines_to_tabular(lines, separator='||'))
61
- if suggested:
62
- log2()
63
- log2(f'* {suggested} suggested commands are added to history. Press <Up> arrow to access them.')
37
+ return issues if issues else 'issues'
64
38
 
65
- def completion(self, _: ReplState):
66
- return {Issues.COMMAND: None}
39
+ def completion(self, state: ReplState):
40
+ return super().completion(state, {'-s': None})
67
41
 
68
42
  def help(self, _: ReplState):
69
- return f'{Issues.COMMAND}\t find all issues'
43
+ return f'{Issues.COMMAND} [-s]\t find all issues'