kaqing 2.0.98__py3-none-any.whl → 2.0.171__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 (194) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +9 -7
  3. adam/batch.py +4 -18
  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/columns/columns.py +3 -1
  8. adam/columns/cpu.py +3 -1
  9. adam/columns/cpu_metrics.py +22 -0
  10. adam/commands/__init__.py +15 -0
  11. adam/commands/alter_tables.py +50 -61
  12. adam/commands/app_cmd.py +38 -0
  13. adam/commands/app_ping.py +8 -14
  14. adam/commands/audit/audit.py +43 -30
  15. adam/commands/audit/audit_repair_tables.py +26 -46
  16. adam/commands/audit/audit_run.py +50 -0
  17. adam/commands/audit/show_last10.py +48 -0
  18. adam/commands/audit/show_slow10.py +47 -0
  19. adam/commands/audit/show_top10.py +45 -0
  20. adam/commands/audit/utils_show_top10.py +59 -0
  21. adam/commands/bash/__init__.py +5 -0
  22. adam/commands/bash/bash.py +36 -0
  23. adam/commands/bash/bash_completer.py +93 -0
  24. adam/commands/bash/utils_bash.py +16 -0
  25. adam/commands/cat.py +50 -0
  26. adam/commands/cd.py +15 -91
  27. adam/commands/check.py +23 -18
  28. adam/commands/cli_commands.py +2 -3
  29. adam/commands/code.py +57 -0
  30. adam/commands/command.py +96 -40
  31. adam/commands/commands_utils.py +9 -19
  32. adam/commands/cp.py +33 -39
  33. adam/commands/cql/cql_completions.py +30 -8
  34. adam/commands/cql/cqlsh.py +12 -27
  35. adam/commands/cql/utils_cql.py +343 -0
  36. adam/commands/deploy/code_start.py +7 -10
  37. adam/commands/deploy/code_stop.py +4 -21
  38. adam/commands/deploy/code_utils.py +3 -3
  39. adam/commands/deploy/deploy.py +4 -21
  40. adam/commands/deploy/deploy_frontend.py +14 -17
  41. adam/commands/deploy/deploy_pg_agent.py +3 -6
  42. adam/commands/deploy/deploy_pod.py +67 -73
  43. adam/commands/deploy/deploy_utils.py +14 -24
  44. adam/commands/deploy/undeploy.py +4 -21
  45. adam/commands/deploy/undeploy_frontend.py +4 -7
  46. adam/commands/deploy/undeploy_pg_agent.py +6 -8
  47. adam/commands/deploy/undeploy_pod.py +11 -12
  48. adam/commands/devices/device.py +118 -0
  49. adam/commands/devices/device_app.py +173 -0
  50. adam/commands/devices/device_auit_log.py +49 -0
  51. adam/commands/devices/device_cass.py +185 -0
  52. adam/commands/devices/device_export.py +86 -0
  53. adam/commands/devices/device_postgres.py +144 -0
  54. adam/commands/devices/devices.py +25 -0
  55. adam/commands/exit.py +1 -4
  56. adam/commands/export/__init__.py +0 -0
  57. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  58. adam/commands/export/clean_up_export_sessions.py +51 -0
  59. adam/commands/export/drop_export_database.py +55 -0
  60. adam/commands/export/drop_export_databases.py +43 -0
  61. adam/commands/export/export.py +53 -0
  62. adam/commands/export/export_databases.py +170 -0
  63. adam/commands/export/export_handlers.py +71 -0
  64. adam/commands/export/export_select.py +81 -0
  65. adam/commands/export/export_select_x.py +54 -0
  66. adam/commands/export/export_use.py +52 -0
  67. adam/commands/export/exporter.py +352 -0
  68. adam/commands/export/import_session.py +40 -0
  69. adam/commands/export/importer.py +67 -0
  70. adam/commands/export/importer_athena.py +80 -0
  71. adam/commands/export/importer_sqlite.py +47 -0
  72. adam/commands/export/show_column_counts.py +54 -0
  73. adam/commands/export/show_export_databases.py +36 -0
  74. adam/commands/export/show_export_session.py +48 -0
  75. adam/commands/export/show_export_sessions.py +44 -0
  76. adam/commands/export/utils_export.py +314 -0
  77. adam/commands/help.py +10 -6
  78. adam/commands/intermediate_command.py +49 -0
  79. adam/commands/issues.py +14 -40
  80. adam/commands/kubectl.py +38 -0
  81. adam/commands/login.py +28 -24
  82. adam/commands/logs.py +4 -6
  83. adam/commands/ls.py +11 -116
  84. adam/commands/medusa/medusa.py +4 -22
  85. adam/commands/medusa/medusa_backup.py +20 -24
  86. adam/commands/medusa/medusa_restore.py +30 -32
  87. adam/commands/medusa/medusa_show_backupjobs.py +16 -17
  88. adam/commands/medusa/medusa_show_restorejobs.py +12 -17
  89. adam/commands/nodetool.py +11 -17
  90. adam/commands/param_get.py +11 -12
  91. adam/commands/param_set.py +9 -10
  92. adam/commands/postgres/postgres.py +43 -36
  93. adam/commands/postgres/{postgres_session.py → postgres_context.py} +80 -46
  94. adam/commands/postgres/postgres_ls.py +4 -8
  95. adam/commands/postgres/postgres_preview.py +5 -9
  96. adam/commands/postgres/psql_completions.py +2 -2
  97. adam/commands/postgres/utils_postgres.py +66 -0
  98. adam/commands/preview_table.py +8 -61
  99. adam/commands/pwd.py +14 -44
  100. adam/commands/reaper/reaper.py +4 -24
  101. adam/commands/reaper/reaper_forward.py +48 -55
  102. adam/commands/reaper/reaper_forward_session.py +6 -0
  103. adam/commands/reaper/reaper_forward_stop.py +10 -16
  104. adam/commands/reaper/reaper_restart.py +7 -14
  105. adam/commands/reaper/reaper_run_abort.py +11 -30
  106. adam/commands/reaper/reaper_runs.py +42 -57
  107. adam/commands/reaper/reaper_runs_abort.py +29 -49
  108. adam/commands/reaper/reaper_schedule_activate.py +11 -30
  109. adam/commands/reaper/reaper_schedule_start.py +10 -29
  110. adam/commands/reaper/reaper_schedule_stop.py +10 -29
  111. adam/commands/reaper/reaper_schedules.py +4 -14
  112. adam/commands/reaper/reaper_status.py +8 -16
  113. adam/commands/reaper/utils_reaper.py +196 -0
  114. adam/commands/repair/repair.py +4 -22
  115. adam/commands/repair/repair_log.py +4 -7
  116. adam/commands/repair/repair_run.py +27 -29
  117. adam/commands/repair/repair_scan.py +31 -34
  118. adam/commands/repair/repair_stop.py +4 -7
  119. adam/commands/report.py +25 -21
  120. adam/commands/restart.py +25 -26
  121. adam/commands/rollout.py +19 -24
  122. adam/commands/shell.py +5 -4
  123. adam/commands/show/show.py +6 -19
  124. adam/commands/show/show_app_actions.py +26 -22
  125. adam/commands/show/show_app_id.py +8 -11
  126. adam/commands/show/show_app_queues.py +7 -10
  127. adam/commands/show/{show_repairs.py → show_cassandra_repairs.py} +8 -17
  128. adam/commands/show/show_cassandra_status.py +29 -33
  129. adam/commands/show/show_cassandra_version.py +4 -14
  130. adam/commands/show/show_commands.py +19 -21
  131. adam/commands/show/show_host.py +1 -1
  132. adam/commands/show/show_login.py +26 -24
  133. adam/commands/show/show_processes.py +16 -18
  134. adam/commands/show/show_storage.py +10 -20
  135. adam/commands/watch.py +26 -29
  136. adam/config.py +5 -14
  137. adam/embedded_params.py +1 -1
  138. adam/pod_exec_result.py +7 -1
  139. adam/repl.py +95 -131
  140. adam/repl_commands.py +48 -20
  141. adam/repl_state.py +270 -61
  142. adam/sql/sql_completer.py +105 -63
  143. adam/sql/sql_state_machine.py +618 -0
  144. adam/sql/term_completer.py +3 -0
  145. adam/sso/authn_ad.py +6 -5
  146. adam/sso/authn_okta.py +3 -3
  147. adam/sso/cred_cache.py +3 -2
  148. adam/sso/idp.py +3 -3
  149. adam/utils.py +439 -3
  150. adam/utils_app.py +98 -0
  151. adam/utils_athena.py +140 -87
  152. adam/utils_audits.py +106 -0
  153. adam/utils_issues.py +32 -0
  154. adam/utils_k8s/app_clusters.py +28 -0
  155. adam/utils_k8s/app_pods.py +33 -0
  156. adam/utils_k8s/cassandra_clusters.py +22 -20
  157. adam/utils_k8s/cassandra_nodes.py +4 -4
  158. adam/utils_k8s/custom_resources.py +5 -0
  159. adam/utils_k8s/ingresses.py +2 -2
  160. adam/utils_k8s/k8s.py +87 -0
  161. adam/utils_k8s/pods.py +77 -68
  162. adam/utils_k8s/secrets.py +4 -4
  163. adam/utils_k8s/service_accounts.py +5 -4
  164. adam/utils_k8s/services.py +2 -2
  165. adam/utils_k8s/statefulsets.py +1 -12
  166. adam/utils_net.py +4 -4
  167. adam/utils_repl/__init__.py +0 -0
  168. adam/utils_repl/automata_completer.py +48 -0
  169. adam/utils_repl/repl_completer.py +46 -0
  170. adam/utils_repl/state_machine.py +173 -0
  171. adam/utils_sqlite.py +109 -0
  172. adam/version.py +1 -1
  173. {kaqing-2.0.98.dist-info → kaqing-2.0.171.dist-info}/METADATA +1 -1
  174. kaqing-2.0.171.dist-info/RECORD +236 -0
  175. adam/commands/app.py +0 -67
  176. adam/commands/bash.py +0 -92
  177. adam/commands/cql/cql_table_completer.py +0 -8
  178. adam/commands/cql/cql_utils.py +0 -115
  179. adam/commands/describe/describe.py +0 -47
  180. adam/commands/describe/describe_keyspace.py +0 -60
  181. adam/commands/describe/describe_keyspaces.py +0 -49
  182. adam/commands/describe/describe_schema.py +0 -49
  183. adam/commands/describe/describe_table.py +0 -60
  184. adam/commands/describe/describe_tables.py +0 -49
  185. adam/commands/devices.py +0 -118
  186. adam/commands/postgres/postgres_utils.py +0 -31
  187. adam/commands/postgres/psql_table_completer.py +0 -11
  188. adam/commands/reaper/reaper_session.py +0 -159
  189. adam/sql/state_machine.py +0 -460
  190. kaqing-2.0.98.dist-info/RECORD +0 -191
  191. /adam/commands/{describe → devices}/__init__.py +0 -0
  192. {kaqing-2.0.98.dist-info → kaqing-2.0.171.dist-info}/WHEEL +0 -0
  193. {kaqing-2.0.98.dist-info → kaqing-2.0.171.dist-info}/entry_points.txt +0 -0
  194. {kaqing-2.0.98.dist-info → kaqing-2.0.171.dist-info}/top_level.txt +0 -0
@@ -1,14 +1,19 @@
1
1
  import click
2
2
 
3
3
  from adam.commands.audit.audit_repair_tables import AuditRepairTables
4
+ from adam.commands.audit.audit_run import AuditRun
5
+ from adam.commands.audit.show_last10 import ShowLast10
6
+ from adam.commands.audit.show_slow10 import ShowSlow10
7
+ from adam.commands.audit.show_top10 import ShowTop10
8
+ from adam.commands.audit.utils_show_top10 import show_top10_completions_for_nesting
4
9
  from adam.commands.command import Command
5
- from adam.config import Config
10
+ from adam.commands.intermediate_command import IntermediateCommand
6
11
  from adam.repl_state import ReplState
7
- from adam.sql.sql_completer import SqlCompleter
8
- from adam.utils import log2
9
- from adam.utils_athena import audit_column_names, audit_table_names, run_audit_query
12
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
13
+ from adam.utils import log2, wait_log
14
+ from adam.utils_athena import Athena
10
15
 
11
- class Audit(Command):
16
+ class Audit(IntermediateCommand):
12
17
  COMMAND = 'audit'
13
18
 
14
19
  # the singleton pattern
@@ -24,48 +29,56 @@ class Audit(Command):
24
29
  def command(self):
25
30
  return Audit.COMMAND
26
31
 
32
+ def required(self):
33
+ return ReplState.L
34
+
27
35
  def run(self, cmd: str, state: ReplState):
28
36
  if not(args := self.args(cmd)):
29
37
  return super().run(cmd, state)
30
38
 
31
- state, args = self.apply_state(args, state)
32
-
33
- r = None
34
- if len(args) > 0:
35
- r = super().intermediate_run(cmd, state, args, Audit.cmd_list(), display_help=False)
39
+ with self.validate(args, state) as (args, state):
40
+ r = None
41
+ if len(args) > 0:
42
+ r = self.intermediate_run(cmd, state, args, self.cmd_list(), display_help=False)
36
43
 
37
- if not r or isinstance(r, str) and r == 'command-missing':
38
- sql = 'select * from audit order by ts desc limit 10'
39
- if args:
40
- sql = ' '.join(args)
41
- else:
42
- log2(sql)
44
+ if not r or isinstance(r, str) and r == 'command-missing':
45
+ sql = 'select * from audit order by ts desc limit 10'
46
+ if args:
47
+ sql = ' '.join(args)
48
+ else:
49
+ log2(sql)
43
50
 
44
- run_audit_query(sql)
51
+ Athena.run_query(sql)
45
52
 
46
- return state
53
+ return state
47
54
 
48
55
  def completion(self, state: ReplState):
49
56
  if state.device == ReplState.L:
50
57
  if not self.schema_read:
51
- Config().wait_log(f'Inspecting audit database schema...')
58
+ wait_log(f'Inspecting audit database schema...')
52
59
  self.schema_read = True
53
- audit_column_names()
54
-
55
- def columns(_):
56
- return audit_column_names()
57
-
58
- return super().completion(state) | SqlCompleter.completions(lambda: audit_table_names(), columns=columns) | {
59
- 'desc': {table: None for table in audit_table_names()}}
60
+ # warm up the caches first time when l: drive is accessed
61
+ Athena.table_names()
62
+ Athena.column_names()
63
+ Athena.column_names(partition_cols_only=True)
64
+
65
+ return super().completion(state) | show_top10_completions_for_nesting() | SqlCompleter(
66
+ lambda: Athena.table_names(),
67
+ expandables={
68
+ 'columns': lambda table: Athena.column_names(),
69
+ 'partition_columns': lambda table: Athena.column_names(partition_cols_only=True)
70
+ },
71
+ variant=SqlVariant.ATHENA
72
+ ).completions_for_nesting()
60
73
 
61
74
  return {}
62
75
 
63
- def cmd_list():
64
- return [AuditRepairTables()]
76
+ def cmd_list(self):
77
+ return [AuditRepairTables(), AuditRun(), ShowLast10(), ShowSlow10(), ShowTop10()]
65
78
 
66
79
  def help(self, _: ReplState):
67
- return f'[{Audit.COMMAND}] <sql-statements>\t run SQL queries on Authena audit database'
80
+ return f'[{Audit.COMMAND}] [<sql-statements>]\t run SQL queries on Authena audit database'
68
81
 
69
82
  class AuditCommandHelper(click.Command):
70
83
  def get_help(self, ctx: click.Context):
71
- Command.intermediate_help(super().get_help(ctx), Audit.COMMAND, Audit.cmd_list(), show_cluster_help=False)
84
+ IntermediateCommand.intermediate_help(super().get_help(ctx), Audit.COMMAND, Audit().cmd_list(), show_cluster_help=False)
@@ -1,12 +1,11 @@
1
- import concurrent
2
1
  import time
3
- import requests
4
2
 
5
3
  from adam.commands.command import Command
6
4
  from adam.config import Config
7
5
  from adam.repl_state import ReplState
8
6
  from adam.utils import log, log2
9
- from adam.utils_athena import audit_query, run_audit_query
7
+ from adam.utils_athena import Athena
8
+ from adam.utils_audits import AuditMeta, Audits
10
9
 
11
10
  class AuditRepairTables(Command):
12
11
  COMMAND = 'audit repair'
@@ -24,26 +23,30 @@ class AuditRepairTables(Command):
24
23
  def command(self):
25
24
  return AuditRepairTables.COMMAND
26
25
 
26
+ def required(self):
27
+ return ReplState.L
28
+
27
29
  def run(self, cmd: str, state: ReplState):
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
-
33
- tables = Config().get('audit.tables', 'audit').split(',')
34
- if args:
35
- tables = args
33
+ with self.validate(args, state) as (args, state):
34
+ tables = Config().get('audit.athena.repair-partition-tables', 'audit').split(',')
35
+ if args:
36
+ tables = args
36
37
 
37
- self.repair(tables)
38
+ meta = Audits.get_meta()
39
+ self.repair(tables, meta)
38
40
 
39
- return state
41
+ return state
40
42
 
41
43
  def completion(self, state: ReplState):
44
+ # trigger auto repair if on l: drive
42
45
  if state.device == ReplState.L:
43
46
  if not self.auto_repaired:
44
47
  if hours := Config().get('audit.athena.auto-repair.elapsed_hours', 12):
45
- with concurrent.futures.ThreadPoolExecutor(max_workers=Config().get('audit.workers', 3)) as executor:
46
- executor.submit(self.auto_repair, hours,)
48
+ with Audits.offload() as exec:
49
+ exec.submit(lambda: self.auto_repair(hours))
47
50
 
48
51
  return super().completion(state)
49
52
 
@@ -52,43 +55,20 @@ class AuditRepairTables(Command):
52
55
  def auto_repair(self, hours: int):
53
56
  self.auto_repaired = True
54
57
 
55
- state, _, rs = audit_query(f'select * from meta')
56
- if state == 'SUCCEEDED':
57
- do_repair = True
58
- if len(rs) > 1:
59
- try:
60
- checked_in = float(rs[1]['Data'][0]['VarCharValue'])
61
- do_repair = checked_in + hours * 60 * 60 < time.time()
62
- except:
63
- pass
64
-
65
- if do_repair:
66
- tables = Config().get('audit.athena.tables', 'audit').split(',')
67
- self.repair(tables, show_sql=True)
68
- log2(f'Audit tables have been auto-repaired.')
69
-
70
- def repair(self, tables: list[str], show_sql = False):
71
- with concurrent.futures.ThreadPoolExecutor(max_workers=Config().get('audit.workers', 3)) as executor:
58
+ meta: AuditMeta = Audits.get_meta()
59
+ if meta.partitions_last_checked + hours * 60 * 60 < time.time():
60
+ tables = Config().get('audit.athena.repair-partition-tables', 'audit').split(',')
61
+ self.repair(tables, meta, show_sql=True)
62
+ log2(f'Audit tables have been auto-repaired.')
63
+
64
+ def repair(self, tables: list[str], meta: AuditMeta, show_sql = False):
65
+ with Audits.offload() as exec:
72
66
  for table in tables:
73
67
  if show_sql:
74
68
  log(f'MSCK REPAIR TABLE {table}')
75
69
 
76
- executor.submit(run_audit_query, f'MSCK REPAIR TABLE {table}', None,)
77
- executor.submit(self.check_in,)
70
+ exec.submit(Athena.query, f'MSCK REPAIR TABLE {table}', None,)
71
+ exec.submit(Audits.put_meta, Audits.PARTITIONS_ADDED, meta,)
78
72
 
79
73
  def help(self, _: ReplState):
80
- return f"{AuditRepairTables.COMMAND} \t run MSCK REPAIR command for new partition discovery"
81
-
82
- def check_in(self):
83
- payload = {
84
- 'action': 'check-in'
85
- }
86
- audit_endpoint = Config().get("audit.endpoint", "https://4psvtaxlcb.execute-api.us-west-2.amazonaws.com/prod/")
87
- try:
88
- response = requests.post(audit_endpoint, json=payload, timeout=Config().get("audit.timeout", 10))
89
- if response.status_code in [200, 201]:
90
- Config().debug(response.text)
91
- else:
92
- log2(f"Error: {response.status_code} {response.text}")
93
- except requests.exceptions.Timeout as e:
94
- log2(f"Timeout occurred: {e}")
74
+ return f"{AuditRepairTables.COMMAND}\t run MSCK REPAIR command for new partition discovery"
@@ -0,0 +1,50 @@
1
+ from adam.commands.command import Command
2
+ from adam.config import Config
3
+ from adam.repl_state import ReplState
4
+ from adam.utils import log2
5
+ from adam.utils_athena import Athena
6
+ from adam.utils_audits import AuditMeta, Audits
7
+
8
+ class AuditRun(Command):
9
+ COMMAND = 'audit run'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(AuditRun, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+ self.auto_repaired = False
20
+
21
+ def command(self):
22
+ return AuditRun.COMMAND
23
+
24
+ def required(self):
25
+ return ReplState.L
26
+
27
+ def run(self, cmd: str, state: ReplState):
28
+ if not(args := self.args(cmd)):
29
+ return super().run(cmd, state)
30
+
31
+ with self.validate(args, state) as (args, state):
32
+ meta: AuditMeta = Audits.get_meta()
33
+ clusters = Audits.find_new_clusters(meta.cluster_last_checked)
34
+ Audits.put_meta(Audits.ADD_CLUSTERS, meta, clusters=clusters)
35
+ if clusters:
36
+ log2(f'Added {len(clusters)} new clusters.')
37
+ tables = Config().get('audit.athena.repair-cluster-tables', 'cluster').split(',')
38
+ with Audits.offload() as exec:
39
+ for table in tables:
40
+ exec.submit(Athena.query, f'MSCK REPAIR TABLE {table}', None,)
41
+ else:
42
+ log2(f'No new clusters were found.')
43
+
44
+ return state
45
+
46
+ def completion(self, state: ReplState):
47
+ return super().completion(state)
48
+
49
+ def help(self, _: ReplState):
50
+ return f"{AuditRun.COMMAND}\t run"
@@ -0,0 +1,48 @@
1
+ from adam.commands.audit.utils_show_top10 import extract_limit_and_duration
2
+ from adam.commands.command import Command
3
+ from adam.repl_state import ReplState
4
+ from adam.utils import log2
5
+ from adam.utils_athena import Athena
6
+ from adam.utils_audits import Audits
7
+
8
+ class ShowLast10(Command):
9
+ COMMAND = 'show last'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ShowLast10, 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 ShowLast10.COMMAND
22
+
23
+ def required(self):
24
+ return ReplState.L
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
+ limit, date_condition = extract_limit_and_duration(args)
32
+
33
+ query = '\n '.join([
34
+ "SELECT * FROM audit",
35
+ f"WHERE drive <> 'z' and ({date_condition})",
36
+ f"ORDER BY ts DESC LIMIT {limit};"])
37
+ log2(query)
38
+ log2()
39
+ Athena.run_query(query)
40
+
41
+ return state
42
+
43
+ def completion(self, _: ReplState):
44
+
45
+ return {}
46
+
47
+ def help(self, _: ReplState):
48
+ return f'{ShowLast10.COMMAND} [limit]\t show last <limit> audit lines'
@@ -0,0 +1,47 @@
1
+ from adam.commands.audit.utils_show_top10 import extract_limit_and_duration
2
+ from adam.commands.command import Command
3
+ from adam.repl_state import ReplState
4
+ from adam.utils import log2
5
+ from adam.utils_athena import Athena
6
+ from adam.utils_audits import Audits
7
+
8
+ class ShowSlow10(Command):
9
+ COMMAND = 'show slow'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ShowSlow10, 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 ShowSlow10.COMMAND
22
+
23
+ def required(self):
24
+ return ReplState.L
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
+ limit, date_condition = extract_limit_and_duration(args)
32
+
33
+ query = '\n '.join([
34
+ "SELECT * FROM audit",
35
+ f"WHERE drive <> 'z' and ({date_condition})",
36
+ f"ORDER BY CAST(duration AS REAL) DESC LIMIT {limit};"])
37
+ log2(query)
38
+ log2()
39
+ Athena.run_query(query)
40
+
41
+ return state
42
+
43
+ def completion(self, _: ReplState):
44
+ return {}
45
+
46
+ def help(self, _: ReplState):
47
+ return f'{ShowSlow10.COMMAND} [limit]\t show slow <limit> audit lines'
@@ -0,0 +1,45 @@
1
+ from adam.commands.audit.utils_show_top10 import extract_limit_and_duration
2
+ from adam.commands.command import Command
3
+ from adam.repl_state import ReplState
4
+ from adam.utils import log2
5
+ from adam.utils_athena import Athena
6
+
7
+ class ShowTop10(Command):
8
+ COMMAND = 'show top'
9
+
10
+ # the singleton pattern
11
+ def __new__(cls, *args, **kwargs):
12
+ if not hasattr(cls, 'instance'): cls.instance = super(ShowTop10, 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 ShowTop10.COMMAND
21
+
22
+ def required(self):
23
+ return ReplState.L
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
+ limit, date_condition = extract_limit_and_duration(args)
31
+ query = '\n '.join([
32
+ "SELECT min(c) AS cluster, line, COUNT(*) AS cnt, avg(CAST(duration AS REAL)) AS duration",
33
+ f"FROM audit WHERE drive <> 'z' and ({date_condition})",
34
+ f"GROUP BY line ORDER BY cnt DESC LIMIT {limit};"])
35
+ log2(query)
36
+ log2()
37
+ Athena.run_query(query)
38
+
39
+ return state
40
+
41
+ def completion(self, _: ReplState):
42
+ return {}
43
+
44
+ def help(self, _: ReplState):
45
+ return f'{ShowTop10.COMMAND} [limit]\t show top <limit> audit lines'
@@ -0,0 +1,59 @@
1
+ from datetime import datetime, timedelta
2
+
3
+ from adam.utils_audits import Audits
4
+ from adam.utils_repl.automata_completer import AutomataCompleter
5
+ from adam.utils_repl.state_machine import StateMachine
6
+
7
+ def extract_limit_and_duration(args: list[str]) -> tuple[int, datetime]:
8
+ limit = 10
9
+ _from = datetime.now() - timedelta(days=30)
10
+ if args:
11
+ try:
12
+ limit = int(args[0])
13
+ except:
14
+ pass
15
+
16
+ if len(args) > 2 and args[1] == 'over':
17
+ if args[2] == 'day':
18
+ _from = datetime.now() - timedelta(days=1)
19
+
20
+ return (limit, Audits.date_from(_from))
21
+
22
+ def limit_and_duration_completion():
23
+ return {'10': {'over': {
24
+ 'day': None,
25
+ 'month': None
26
+ }}}
27
+
28
+ SHOW_TOP10_SPEC = [
29
+ ' > show > show',
30
+ 'show > last|slow|top > show_top ^ last,slow,top',
31
+ 'show_top > word > show_top_n ^ 10',
32
+ 'show_top_n > over > show_top_n_over ^ over',
33
+ 'show_top_n_over > day|month > show_top_n_over$ ^ day,month',
34
+ ]
35
+
36
+ SHOW_TOP10_KEYWORDS = [
37
+ 'show',
38
+ 'top',
39
+ 'last',
40
+ 'slow',
41
+ 'over',
42
+ 'day',
43
+ 'month'
44
+ ]
45
+
46
+ class ShowTop10StateMachine(StateMachine[str]):
47
+ def spec(self) -> str:
48
+ return SHOW_TOP10_SPEC
49
+
50
+ def keywords(self) -> list[str]:
51
+ return SHOW_TOP10_KEYWORDS
52
+
53
+ def show_top10_completions_for_nesting():
54
+ return {
55
+ 'show': {
56
+ 'last': AutomataCompleter(ShowTop10StateMachine(), first_term='show last'),
57
+ 'slow': AutomataCompleter(ShowTop10StateMachine(), first_term='show slow'),
58
+ 'top': AutomataCompleter(ShowTop10StateMachine(), first_term='show top'),
59
+ }}
@@ -0,0 +1,5 @@
1
+ from adam.commands.bash.utils_bash import BashHandler
2
+ from adam.repl_state import ReplState
3
+
4
+ def bash(s0: ReplState, s1: ReplState):
5
+ return BashHandler(s0, s1)
@@ -0,0 +1,36 @@
1
+ from adam.commands.bash import bash
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 Bash(Command):
7
+ COMMAND = 'bash'
8
+
9
+ # the singleton pattern
10
+ def __new__(cls, *args, **kwargs):
11
+ if not hasattr(cls, 'instance'): cls.instance = super(Bash, 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 Bash.COMMAND
20
+
21
+ def required(self):
22
+ return [RequiredState.CLUSTER_OR_POD, RequiredState.APP_APP, ReplState.P]
23
+
24
+ def run(self, cmd: str, s0: ReplState):
25
+ if not(args := self.args(cmd)):
26
+ return super().run(cmd, s0)
27
+
28
+ with self.validate(args, s0) as (args, s1):
29
+ with bash(s0, s1) as exec:
30
+ return exec(args)
31
+
32
+ def completion(self, state: ReplState):
33
+ return Devices.device(state).bash_completion(Bash.COMMAND, state, default = {})
34
+
35
+ def help(self, _: ReplState):
36
+ return f'{Bash.COMMAND} [pod-name] [bash-commands] [&]\t run bash on the Cassandra nodes'
@@ -0,0 +1,93 @@
1
+ from typing import Callable
2
+ from prompt_toolkit.completion import WordCompleter
3
+
4
+ from adam.utils_repl.automata_completer import AutomataCompleter
5
+ from adam.utils_repl.state_machine import State, StateMachine
6
+
7
+ BASH_SPEC = [
8
+ # <command> ::= <simple_command> | <pipeline> | <conditional_command>
9
+ # <simple_command> ::= <word> <argument>* <redirection>*
10
+ # <pipeline> ::= <command> '|' <command>
11
+ # <conditional_command> ::= <command> '&&' <command> | <command> '||' <command>
12
+ # <word> ::= <letter> <letter_or_digit>*
13
+ # <argument> ::= <word>
14
+ # <redirection> ::= '>' <filename> | '<' <filename>
15
+ # <filename> ::= <word>
16
+ # <letter> ::= 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... | 'Z'
17
+ # <digit> ::= '0' | '1' | ... | '9'
18
+ # <letter_or_digit> ::= <letter> | <digit>
19
+
20
+ ' > word > cmd ^ hosts',
21
+ 'cmd > word > cmd ^ |,>,2>,<,&,&&,||',
22
+ '- > pipe > cmd_pipe',
23
+ '- > _rdr0_ > cmd_rdr0',
24
+ '- > _rdr1_ > cmd_rdr1',
25
+ '- > _rdr2_ > cmd_rdr2',
26
+ '- > & > cmd_bg ^ |,>,2>,<,&,&&,||',
27
+ '- > &&|_or_ > nocmd',
28
+ 'cmd_a > word > cmd',
29
+ 'cmd_pipe > word > cmd',
30
+ 'cmd_rdr0 > word > cmd_rdr0_f',
31
+ 'cmd_rdr1 > word > cmd_rdr1_f',
32
+ 'cmd_rdr2 > word > cmd_rdr2_f',
33
+ 'cmd_rdr1_f > pipe > cmd_pipe ^ |,2>,<,&,&&,||',
34
+ '- > _rdr2_ > cmd_rdr2',
35
+ '- > _rdr0_ > cmd_rdr0',
36
+ 'cmd_rdr2_f > pipe > cmd_pipe ^ |,<,&,&&,||',
37
+ '- > _rdr0_ > cmd_rdr0',
38
+ '- > & > cmd_bg ^ |,>,2>,<,&,&&,||',
39
+ '- > &&|_or_ > nocmd',
40
+ 'cmd_rdr0_f > pipe > cmd_pipe ^ |,&,&&,||',
41
+ '- > & > cmd_bg ^ |,>,2>,<,&,&&,||',
42
+ '- > &&|_or_ > cmd',
43
+ 'cmd_bg > &&|_or_ > nocmd ^ &&,||',
44
+ 'nocmd > word > cmd',
45
+ ]
46
+
47
+ BASH_KEYWORDS = [
48
+ '&',
49
+ '&&',
50
+ '|',
51
+ '||',
52
+ '>',
53
+ '2>',
54
+ '>>',
55
+ '<',
56
+ 'hosts'
57
+ ]
58
+
59
+ class BashStateMachine(StateMachine[str]):
60
+ def spec(self) -> str:
61
+ return BASH_SPEC
62
+
63
+ def keywords(self) -> list[str]:
64
+ return BASH_KEYWORDS
65
+
66
+ class BashCompleter(AutomataCompleter[str]):
67
+ def __init__(self,
68
+ hosts: Callable[[], list[str]],
69
+ debug = False):
70
+ super().__init__(BashStateMachine(debug=debug), '', debug=debug)
71
+
72
+ self.hosts = hosts
73
+ self.debug = debug
74
+
75
+ def suggestions_completer(self, state: State, suggestions: str) -> list[str]:
76
+ if not suggestions:
77
+ return None
78
+
79
+ terms = []
80
+ for suggestion in suggestions.split(','):
81
+ terms.extend(self._terms(state, suggestion))
82
+
83
+ return WordCompleter(terms)
84
+
85
+ def _terms(self, _: State, word: str) -> list[str]:
86
+ terms = []
87
+
88
+ if word == 'hosts':
89
+ terms.extend(self.hosts())
90
+ else:
91
+ terms.append(word)
92
+
93
+ return terms
@@ -0,0 +1,16 @@
1
+ from adam.commands.devices.devices import Devices
2
+ from adam.repl_state import ReplState
3
+
4
+ class BashHandler:
5
+ def __init__(self, s0: ReplState, s1: ReplState):
6
+ self.s0 = s0
7
+ self.s1 = s1
8
+
9
+ def __enter__(self):
10
+ return self.exec
11
+
12
+ def __exit__(self, exc_type, exc_val, exc_tb):
13
+ return False
14
+
15
+ def exec(self, args: list[str]):
16
+ return Devices.device(self.s1).bash(self.s0, self.s1, args)
adam/commands/cat.py ADDED
@@ -0,0 +1,50 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.devices.devices import Devices
3
+ from adam.repl_state import ReplState, RequiredState
4
+ from adam.utils import log2
5
+ from adam.utils_k8s.app_pods import AppPods
6
+ from adam.utils_k8s.statefulsets import StatefulSets
7
+
8
+ class Cat(Command):
9
+ COMMAND = 'cat'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(Cat, 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 Cat.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
+ if len(args) < 1:
32
+ if state.in_repl:
33
+ log2('File is required.')
34
+ log2()
35
+ else:
36
+ log2('* File is missing.')
37
+ Command.display_help()
38
+
39
+ return 'command-missing'
40
+
41
+ return Devices.device(state).cat(cmd, state)
42
+
43
+ def completion(self, state: ReplState):
44
+ if super().completion(state):
45
+ return Devices.device(state).cat_completion(Cat.COMMAND, state, default={})
46
+
47
+ return {}
48
+
49
+ def help(self, _: ReplState):
50
+ return f'{Cat.COMMAND} file [&]\t run cat command on the Cassandra nodes'