kaqing 2.0.93__py3-none-any.whl → 2.0.115__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 (134) hide show
  1. adam/apps.py +2 -2
  2. adam/batch.py +2 -16
  3. adam/checks/check_utils.py +4 -4
  4. adam/checks/compactionstats.py +1 -1
  5. adam/checks/cpu.py +2 -2
  6. adam/checks/disk.py +1 -1
  7. adam/checks/gossip.py +1 -1
  8. adam/checks/memory.py +3 -3
  9. adam/checks/status.py +1 -1
  10. adam/commands/alter_tables.py +3 -14
  11. adam/commands/app.py +3 -3
  12. adam/commands/app_ping.py +2 -2
  13. adam/commands/audit/audit.py +26 -11
  14. adam/commands/audit/audit_repair_tables.py +39 -4
  15. adam/commands/audit/audit_run.py +58 -0
  16. adam/commands/audit/show_last10.py +51 -0
  17. adam/commands/audit/show_slow10.py +50 -0
  18. adam/commands/audit/show_top10.py +49 -0
  19. adam/commands/audit/utils_show_top10.py +59 -0
  20. adam/commands/bash/bash.py +124 -0
  21. adam/commands/bash/bash_completer.py +93 -0
  22. adam/commands/cat.py +55 -0
  23. adam/commands/cd.py +26 -14
  24. adam/commands/check.py +6 -0
  25. adam/commands/cli_commands.py +3 -3
  26. adam/commands/code.py +60 -0
  27. adam/commands/command.py +9 -4
  28. adam/commands/commands_utils.py +4 -5
  29. adam/commands/cql/cql_completions.py +7 -3
  30. adam/commands/cql/cql_utils.py +103 -11
  31. adam/commands/cql/cqlsh.py +10 -5
  32. adam/commands/deploy/code_utils.py +2 -2
  33. adam/commands/deploy/deploy.py +7 -1
  34. adam/commands/deploy/deploy_pg_agent.py +2 -2
  35. adam/commands/deploy/deploy_pod.py +6 -6
  36. adam/commands/deploy/deploy_utils.py +2 -2
  37. adam/commands/deploy/undeploy.py +7 -1
  38. adam/commands/deploy/undeploy_pg_agent.py +2 -2
  39. adam/commands/deploy/undeploy_pod.py +4 -4
  40. adam/commands/devices.py +29 -0
  41. adam/commands/export/export.py +60 -0
  42. adam/commands/export/export_on_x.py +76 -0
  43. adam/commands/export/export_rmdbs.py +65 -0
  44. adam/commands/export/export_select.py +68 -0
  45. adam/commands/export/export_use.py +56 -0
  46. adam/commands/export/utils_export.py +253 -0
  47. adam/commands/help.py +9 -5
  48. adam/commands/issues.py +6 -0
  49. adam/commands/kubectl.py +41 -0
  50. adam/commands/login.py +6 -3
  51. adam/commands/logs.py +2 -1
  52. adam/commands/ls.py +43 -31
  53. adam/commands/medusa/medusa_backup.py +2 -2
  54. adam/commands/medusa/medusa_restore.py +2 -2
  55. adam/commands/medusa/medusa_show_backupjobs.py +3 -2
  56. adam/commands/medusa/medusa_show_restorejobs.py +2 -2
  57. adam/commands/nodetool.py +11 -16
  58. adam/commands/postgres/postgres.py +4 -4
  59. adam/commands/postgres/{postgres_session.py → postgres_context.py} +29 -30
  60. adam/commands/postgres/postgres_utils.py +5 -5
  61. adam/commands/postgres/psql_completions.py +1 -1
  62. adam/commands/preview_table.py +18 -32
  63. adam/commands/pwd.py +4 -3
  64. adam/commands/reaper/reaper.py +3 -0
  65. adam/commands/reaper/reaper_restart.py +1 -1
  66. adam/commands/reaper/reaper_session.py +1 -1
  67. adam/commands/repair/repair.py +3 -3
  68. adam/commands/repair/repair_log.py +1 -1
  69. adam/commands/repair/repair_run.py +2 -2
  70. adam/commands/repair/repair_scan.py +1 -1
  71. adam/commands/repair/repair_stop.py +1 -1
  72. adam/commands/report.py +6 -0
  73. adam/commands/restart.py +2 -2
  74. adam/commands/rollout.py +1 -1
  75. adam/commands/show/show.py +3 -1
  76. adam/commands/show/show_app_actions.py +3 -0
  77. adam/commands/show/show_app_id.py +1 -1
  78. adam/commands/show/show_app_queues.py +3 -2
  79. adam/commands/show/show_cassandra_status.py +3 -3
  80. adam/commands/show/show_cassandra_version.py +3 -3
  81. adam/commands/show/show_login.py +3 -0
  82. adam/commands/show/show_processes.py +1 -1
  83. adam/commands/show/show_repairs.py +2 -2
  84. adam/commands/show/show_storage.py +1 -1
  85. adam/commands/watch.py +1 -1
  86. adam/config.py +2 -1
  87. adam/embedded_params.py +1 -1
  88. adam/pod_exec_result.py +7 -1
  89. adam/repl.py +125 -99
  90. adam/repl_commands.py +29 -17
  91. adam/repl_state.py +229 -49
  92. adam/sql/sql_completer.py +86 -62
  93. adam/sql/sql_state_machine.py +563 -0
  94. adam/sql/term_completer.py +3 -0
  95. adam/sso/cred_cache.py +1 -1
  96. adam/sso/idp.py +1 -1
  97. adam/utils_athena.py +108 -74
  98. adam/utils_audits.py +104 -0
  99. adam/utils_export.py +42 -0
  100. adam/utils_k8s/__init__.py +0 -0
  101. adam/utils_k8s/app_clusters.py +33 -0
  102. adam/utils_k8s/app_pods.py +31 -0
  103. adam/{k8s_utils → utils_k8s}/cassandra_clusters.py +5 -6
  104. adam/{k8s_utils → utils_k8s}/cassandra_nodes.py +11 -4
  105. adam/{k8s_utils → utils_k8s}/deployment.py +2 -2
  106. adam/{k8s_utils → utils_k8s}/pods.py +54 -11
  107. adam/{k8s_utils → utils_k8s}/statefulsets.py +2 -2
  108. adam/version.py +1 -1
  109. {kaqing-2.0.93.dist-info → kaqing-2.0.115.dist-info}/METADATA +1 -1
  110. kaqing-2.0.115.dist-info/RECORD +203 -0
  111. adam/commands/bash.py +0 -91
  112. adam/commands/cql/cql_table_completer.py +0 -8
  113. adam/commands/describe/describe.py +0 -46
  114. adam/commands/describe/describe_keyspace.py +0 -60
  115. adam/commands/describe/describe_keyspaces.py +0 -50
  116. adam/commands/describe/describe_table.py +0 -60
  117. adam/commands/describe/describe_tables.py +0 -50
  118. adam/commands/postgres/psql_table_completer.py +0 -11
  119. adam/sql/state_machine.py +0 -460
  120. kaqing-2.0.93.dist-info/RECORD +0 -190
  121. /adam/commands/{describe → bash}/__init__.py +0 -0
  122. /adam/{k8s_utils → commands/export}/__init__.py +0 -0
  123. /adam/{k8s_utils → utils_k8s}/config_maps.py +0 -0
  124. /adam/{k8s_utils → utils_k8s}/custom_resources.py +0 -0
  125. /adam/{k8s_utils → utils_k8s}/ingresses.py +0 -0
  126. /adam/{k8s_utils → utils_k8s}/jobs.py +0 -0
  127. /adam/{k8s_utils → utils_k8s}/kube_context.py +0 -0
  128. /adam/{k8s_utils → utils_k8s}/secrets.py +0 -0
  129. /adam/{k8s_utils → utils_k8s}/service_accounts.py +0 -0
  130. /adam/{k8s_utils → utils_k8s}/services.py +0 -0
  131. /adam/{k8s_utils → utils_k8s}/volumes.py +0 -0
  132. {kaqing-2.0.93.dist-info → kaqing-2.0.115.dist-info}/WHEEL +0 -0
  133. {kaqing-2.0.93.dist-info → kaqing-2.0.115.dist-info}/entry_points.txt +0 -0
  134. {kaqing-2.0.93.dist-info → kaqing-2.0.115.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,124 @@
1
+ from typing import Callable
2
+
3
+ from adam.commands.bash.bash_completer import BashCompleter
4
+ from adam.commands.command import Command
5
+ from adam.utils_k8s.app_clusters import AppClusters
6
+ from adam.utils_k8s.app_pods import AppPods
7
+ from adam.utils_k8s.cassandra_clusters import CassandraClusters
8
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
9
+ from adam.pod_exec_result import PodExecResult
10
+ from adam.repl_state import BashSession, ReplState, RequiredState
11
+ from adam.utils_k8s.statefulsets import StatefulSets
12
+ from build.lib.adam.utils_k8s.cassandra_nodes import CassandraNodes
13
+
14
+ class Bash(Command):
15
+ COMMAND = 'bash'
16
+
17
+ # the singleton pattern
18
+ def __new__(cls, *args, **kwargs):
19
+ if not hasattr(cls, 'instance'): cls.instance = super(Bash, cls).__new__(cls)
20
+
21
+ return cls.instance
22
+
23
+ def __init__(self, successor: Command=None):
24
+ super().__init__(successor)
25
+
26
+ def command(self):
27
+ return Bash.COMMAND
28
+
29
+ def required(self):
30
+ return [RequiredState.CLUSTER_OR_POD, RequiredState.APP_APP]
31
+
32
+ def run(self, cmd: str, s0: ReplState):
33
+ if not(args := self.args(cmd)):
34
+ return super().run(cmd, s0)
35
+
36
+ state, args = self.apply_state(args, s0, args_to_check=2)
37
+ if not self.validate_state(state):
38
+ return state
39
+
40
+ def _run(state_changed: bool, cli: Callable[[str], None]):
41
+ if state.in_repl:
42
+ if state_changed:
43
+ r = self.exec_with_dir(state, args)
44
+ else:
45
+ r = self.exec_with_dir(s0, args)
46
+
47
+ if not r:
48
+ state.exit_bash()
49
+
50
+ return 'inconsistent pwd'
51
+
52
+ return r
53
+ else:
54
+ cli(' '.join(args))
55
+
56
+ return state
57
+
58
+ if state.device == ReplState.A:
59
+ def cli(command: str):
60
+ if state.app_pod:
61
+ AppPods.exec(state.app_pod, state.namespace, command, show_out=True)
62
+ elif state.app_app:
63
+ AppClusters.exec(AppPods.pod_names(state.namespace, state.app_env, state.app_app), state.namespace, command, action='bash', show_out=True)
64
+
65
+ return _run(s0.app_env != state.app_env or s0.app_app != state.app_app or s0.app_pod != state.app_pod, cli)
66
+
67
+ def cli(command: str):
68
+ if state.pod:
69
+ CassandraNodes.exec(state.pod, state.namespace, command, show_out=True)
70
+ elif state.sts:
71
+ CassandraClusters.exec(state.sts, state.namespace, command, action='bash', show_out=True)
72
+
73
+ return _run(s0.sts != state.sts or s0.pod != state.pod, cli)
74
+
75
+ def exec_with_dir(self, state: ReplState, args: list[str]) -> list[PodExecResult]:
76
+ session_just_created = False
77
+ if not args:
78
+ session_just_created = True
79
+ session = BashSession(state.device)
80
+ state.enter_bash(session)
81
+
82
+ if state.bash_session:
83
+ if args != ['pwd']:
84
+ if args:
85
+ args.append('&&')
86
+ args.extend(['pwd', '>', f'/tmp/.qing-{state.bash_session.session_id}'])
87
+
88
+ if not session_just_created:
89
+ if pwd := state.bash_session.pwd(state):
90
+ args = ['cd', pwd, '&&'] + args
91
+
92
+ command = ' '.join(args)
93
+
94
+ rs = []
95
+
96
+ if state.device == ReplState.A:
97
+ if state.app_pod:
98
+ rs = [AppPods.exec(state.app_pod, state.namespace, command,
99
+ show_out=not session_just_created, shell='bash')]
100
+ elif state.app_app:
101
+ rs = AppClusters.exec(AppPods.pod_names(state.namespace, state.app_env, state.app_app), state.namespace, command, action='bash',
102
+ show_out=not session_just_created, shell='bash')
103
+ else:
104
+ if state.pod:
105
+ rs = [CassandraNodes.exec(state.pod, state.namespace, command,
106
+ show_out=not session_just_created, shell='bash')]
107
+ elif state.sts:
108
+ rs = CassandraClusters.exec(state.sts, state.namespace, command, action='bash',
109
+ show_out=not session_just_created, shell='bash')
110
+
111
+ return rs
112
+
113
+ def completion(self, state: ReplState):
114
+ if state.device == ReplState.A and state.app_app:
115
+ return { Bash.COMMAND: BashCompleter(lambda: []) } | \
116
+ {f'@{p}': {Bash.COMMAND: BashCompleter(lambda: [])} for p in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}
117
+ elif state.sts:
118
+ return { Bash.COMMAND: BashCompleter(lambda: []) } | \
119
+ {f'@{p}': {Bash.COMMAND: BashCompleter(lambda: [])} for p in StatefulSets.pod_names(state.sts, state.namespace)}
120
+
121
+ return {}
122
+
123
+ def help(self, _: ReplState):
124
+ 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
adam/commands/cat.py ADDED
@@ -0,0 +1,55 @@
1
+ from adam.commands.bash.bash import Bash
2
+ from adam.commands.command import Command
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]
25
+
26
+ def run(self, cmd: str, state: ReplState):
27
+ if not(args := self.args(cmd)):
28
+ return super().run(cmd, state)
29
+
30
+ state, args = self.apply_state(args, state)
31
+
32
+ if len(args) < 1:
33
+ if state.in_repl:
34
+ log2('File is required.')
35
+ log2()
36
+ else:
37
+ log2('* File is missing.')
38
+ Command.display_help()
39
+
40
+ return 'command-missing'
41
+
42
+ return Bash().run('bash ' + cmd, state)
43
+
44
+ def completion(self, state: ReplState):
45
+ if state.device == ReplState.A and state.app_app:
46
+ return super().completion(state) | \
47
+ {f'@{p}': {Cat.COMMAND: None} for p in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}
48
+ elif state.device == ReplState.C and state.sts:
49
+ return super().completion(state) | \
50
+ {f'@{p}': {Cat.COMMAND: None} for p in StatefulSets.pod_names(state.sts, state.namespace)}
51
+
52
+ return {}
53
+
54
+ def help(self, _: ReplState):
55
+ return f'{Cat.COMMAND} file [&]\t run cat command on the Cassandra nodes'
adam/commands/cd.py CHANGED
@@ -1,9 +1,10 @@
1
1
  from adam.commands.command import Command
2
2
  from adam.commands.postgres.postgres_utils import pg_database_names
3
- from adam.commands.postgres.postgres_session import PostgresSession
4
- from adam.k8s_utils.cassandra_clusters import CassandraClusters
5
- from adam.k8s_utils.kube_context import KubeContext
6
- from adam.k8s_utils.statefulsets import StatefulSets
3
+ from adam.commands.postgres.postgres_context import PostgresContext
4
+ from adam.utils_k8s.app_pods import AppPods
5
+ from adam.utils_k8s.cassandra_clusters import CassandraClusters
6
+ from adam.utils_k8s.kube_context import KubeContext
7
+ from adam.utils_k8s.statefulsets import StatefulSets
7
8
  from adam.repl_state import ReplState
8
9
  from adam.utils import log2
9
10
  from adam.apps import Apps
@@ -23,10 +24,16 @@ class Cd(Command):
23
24
  def command(self):
24
25
  return Cd.COMMAND
25
26
 
27
+ def required(self):
28
+ return ReplState.NON_L
29
+
26
30
  def run(self, cmd: str, state: ReplState):
27
31
  if not(args := self.args(cmd)):
28
32
  return super().run(cmd, state)
29
33
 
34
+ if not self.validate_state(state):
35
+ return state
36
+
30
37
  if len(args) < 2:
31
38
  return state
32
39
 
@@ -36,22 +43,27 @@ class Cd(Command):
36
43
  if dir == '':
37
44
  state.pg_path = None
38
45
  else:
39
- session = PostgresSession(state.namespace, state.pg_path)
46
+ context: PostgresContext = PostgresContext.apply(state.namespace, state.pg_path, arg=dir)
40
47
  # patch up state.namespace from pg cd
41
- if not state.namespace and (ns := session.find_namespace(arg)):
42
- state.namespace = ns
43
- state.pg_path = session.directory(arg)
48
+ if not state.namespace and context.namespace:
49
+ state.namespace = context.namespace
50
+ state.pg_path = context.path()
44
51
  elif state.device == ReplState.A:
45
52
  if dir == '':
46
53
  state.app_env = None
47
54
  state.app_app = None
55
+ state.app_pod = None
48
56
  elif dir == '..':
49
- if state.app_app:
57
+ if state.app_pod:
58
+ state.app_pod = None
59
+ elif state.app_app:
50
60
  state.app_app = None
51
61
  else:
52
62
  state.app_env = None
53
63
  else:
54
- if not state.app_env:
64
+ if state.app_app:
65
+ state.app_pod = dir
66
+ elif not state.app_env:
55
67
  tks = dir.split('@')
56
68
  if len(tks) > 1:
57
69
  state.namespace = tks[1]
@@ -91,16 +103,16 @@ class Cd(Command):
91
103
 
92
104
  def completion(self, state: ReplState):
93
105
  if state.device == ReplState.P:
94
- pg = PostgresSession(state.namespace, state.pg_path) if state.pg_path else None
106
+ pg: PostgresContext = PostgresContext.apply(state.namespace, state.pg_path) if state.pg_path else None
95
107
  if pg and pg.db:
96
108
  return {Cd.COMMAND: {'..': None}}
97
109
  elif pg and pg.host:
98
- return {Cd.COMMAND: {'..': None} | {p: None for p in pg_database_names(state.namespace, pg.directory())}}
110
+ return {Cd.COMMAND: {'..': None} | {p: None for p in pg_database_names(state.namespace, pg.path())}}
99
111
  else:
100
- return {Cd.COMMAND: {p: None for p in PostgresSession.hosts(state.namespace)}}
112
+ return {Cd.COMMAND: {p: None for p in PostgresContext.hosts(state.namespace)}}
101
113
  elif state.device == ReplState.A:
102
114
  if state.app_app:
103
- return {Cd.COMMAND: {'..': None}}
115
+ return {Cd.COMMAND: {'..': None} | {pod: None for pod in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}}
104
116
  elif state.app_env:
105
117
  return {Cd.COMMAND: {'..': None} | {app[0].split('-')[1]: None for app in Apps.apps(state.app_env)}}
106
118
  else:
adam/commands/check.py CHANGED
@@ -23,11 +23,17 @@ class Check(Issues):
23
23
  def command(self):
24
24
  return Check.COMMAND
25
25
 
26
+ def required(self):
27
+ return ReplState.NON_L
28
+
26
29
  def run(self, cmd: str, state: ReplState):
27
30
  if not(args := self.args(cmd)):
28
31
  return super().run(cmd, state)
29
32
 
30
33
  state, args = self.apply_state(args, state)
34
+ if not self.validate_state(state):
35
+ return state
36
+
31
37
  args, show = Command.extract_options(args, ['-s', '--show'])
32
38
 
33
39
  if not args:
@@ -3,9 +3,9 @@ import re
3
3
 
4
4
  from adam.commands.reaper.reaper_session import ReaperSession
5
5
  from adam.config import Config
6
- from adam.k8s_utils.kube_context import KubeContext
7
- from adam.k8s_utils.secrets import Secrets
8
- from adam.k8s_utils.statefulsets import StatefulSets
6
+ from adam.utils_k8s.kube_context import KubeContext
7
+ from adam.utils_k8s.secrets import Secrets
8
+ from adam.utils_k8s.statefulsets import StatefulSets
9
9
  from adam.repl_state import ReplState
10
10
 
11
11
  class CliCommands:
adam/commands/code.py ADDED
@@ -0,0 +1,60 @@
1
+ import code
2
+ from functools import partial
3
+
4
+ from adam.commands.command import Command
5
+ from adam.commands.cql.cql_utils import cassandra_table_names, run_cql
6
+ from adam.repl_state import ReplState, RequiredState
7
+ from adam.utils import lines_to_tabular
8
+ from adam.utils_k8s.statefulsets import StatefulSets
9
+
10
+ class Code(Command):
11
+ COMMAND = 'python'
12
+
13
+ # the singleton pattern
14
+ def __new__(cls, *args, **kwargs):
15
+ if not hasattr(cls, 'instance'): cls.instance = super(Code, cls).__new__(cls)
16
+
17
+ return cls.instance
18
+
19
+ def __init__(self, successor: Command=None):
20
+ super().__init__(successor)
21
+
22
+ def command(self):
23
+ return Code.COMMAND
24
+
25
+ def required(self):
26
+ return RequiredState.NAMESPACE
27
+
28
+ def run(self, cmd: str, state: ReplState):
29
+ if not(args := self.args(cmd)):
30
+ return super().run(cmd, state)
31
+
32
+ state, args = self.apply_state(args, state)
33
+ if not self.validate_state(state):
34
+ return state
35
+
36
+ sts = state.sts
37
+ pod = state.pod
38
+ pods = StatefulSets.pod_names(state.sts, state.namespace)
39
+ tables = cassandra_table_names(state)
40
+ cql = partial(run_cql, state, show_out=True)
41
+
42
+ my_local = globals() | locals()
43
+ # my_local = globals() | {'StatefulSets': StatefulSets} | locals()
44
+ lines = [
45
+ 'sts: StatefulSet name',
46
+ 'pod: Pod name',
47
+ 'pods: Pod names in the current StatefulSet',
48
+ 'tables: Cassandra tables names in the current StatefulSet',
49
+ "cql('select...'): run cql statement"
50
+ ]
51
+
52
+ code.interact(local=my_local, banner=lines_to_tabular(lines, header='Variables', separator=':'))
53
+
54
+ return state
55
+
56
+ def completion(self, state: ReplState):
57
+ return super().completion(state)
58
+
59
+ def help(self, _: ReplState):
60
+ return f'{Code.COMMAND} \t invoke interactive Python shell'
adam/commands/command.py CHANGED
@@ -28,8 +28,10 @@ class Command:
28
28
 
29
29
  return None
30
30
 
31
- def completion(self, _: ReplState, leaf: dict[str, any] = None) -> dict[str, any]:
32
- # COMMAND = 'reaper activate schedule'
31
+ def completion(self, state: ReplState, leaf: dict[str, any] = None) -> dict[str, any]:
32
+ if not self.validate_state(state, show_err=False):
33
+ return {}
34
+
33
35
  d = leaf
34
36
  for t in reversed(self.command().split(' ')):
35
37
  d = {t: d}
@@ -39,8 +41,8 @@ class Command:
39
41
  def required(self) -> RequiredState:
40
42
  return None
41
43
 
42
- def validate_state(self, state: ReplState, pg_required: RequiredState = None, app_required: RequiredState = None):
43
- return state.validate(self.required(), pg_required=pg_required, app_required=app_required)
44
+ def validate_state(self, state: ReplState, show_err=True):
45
+ return state.validate(self.required(), show_err=show_err)
44
46
 
45
47
  def help(self, _: ReplState) -> str:
46
48
  return None
@@ -54,6 +56,9 @@ class Command:
54
56
  return a
55
57
 
56
58
  def apply_state(self, args: list[str], state: ReplState, resolve_pg = True, args_to_check = 6) -> tuple[ReplState, list[str]]:
59
+ """
60
+ Applies any contextual arguments such as namespace or statefulset to the ReplState and returns any non-contextual arguments.
61
+ """
57
62
  return state.apply_args(args, cmd=self.command_tokens(), resolve_pg=resolve_pg, args_to_check=args_to_check)
58
63
 
59
64
  def command_tokens(self):
@@ -1,16 +1,15 @@
1
1
  from concurrent.futures import ThreadPoolExecutor
2
- import time
3
2
  from kubernetes import client
4
3
  from typing import List
5
4
 
6
5
  from adam.checks.check_utils import run_checks
7
6
  from adam.columns.columns import Columns, collect_checks
8
7
  from adam.commands.issues import Issues
9
- from adam.k8s_utils.cassandra_nodes import CassandraNodes
10
- from adam.k8s_utils.pods import Pods
11
- from adam.k8s_utils.statefulsets import StatefulSets
8
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
9
+ from adam.utils_k8s.pods import Pods
10
+ from adam.utils_k8s.statefulsets import StatefulSets
12
11
  from adam.repl_state import ReplState
13
- from adam.utils import convert_seconds, duration, lines_to_tabular, log, log2
12
+ from adam.utils import duration, lines_to_tabular, log, log2
14
13
 
15
14
  def show_pods(pods: List[client.V1Pod], ns: str, show_namespace = True, show_host_id = True):
16
15
  if len(pods) == 0:
@@ -1,11 +1,15 @@
1
- from adam.commands.cql.cql_utils import table_names
1
+ from adam.commands.cql.cql_utils import cassandra_table_names
2
+ from adam.config import Config
2
3
  from adam.repl_state import ReplState
3
4
  from adam.sql.sql_completer import SqlCompleter
4
5
 
5
6
  def cql_completions(state: ReplState) -> dict[str, any]:
7
+ ps = Config().get('cql.alter-tables.gc-grace-periods', '3600,86400,864000,7776000').split(',')
6
8
  return {
7
9
  'describe': {
8
10
  'keyspaces': None,
9
- 'table': {t: None for t in table_names(state)},
11
+ 'table': {t: None for t in cassandra_table_names(state)},
10
12
  'tables': None},
11
- } | SqlCompleter.completions(lambda: table_names(state))
13
+ } | SqlCompleter(lambda: cassandra_table_names(state), table_props=lambda: {
14
+ 'GC_GRACE_SECONDS': ps
15
+ }, variant='cql').completions_for_nesting()
@@ -2,9 +2,9 @@ import functools
2
2
  import re
3
3
 
4
4
  from adam.config import Config
5
- from adam.k8s_utils.cassandra_clusters import CassandraClusters
6
- from adam.k8s_utils.cassandra_nodes import CassandraNodes
7
- from adam.k8s_utils.secrets import Secrets
5
+ from adam.utils_k8s.cassandra_clusters import CassandraClusters
6
+ from adam.utils_k8s.cassandra_nodes import CassandraNodes
7
+ from adam.utils_k8s.secrets import Secrets
8
8
  from adam.pod_exec_result import PodExecResult
9
9
  from adam.repl_state import ReplState
10
10
  from adam.utils import log2
@@ -23,19 +23,28 @@ def keyspaces(state: ReplState, on_any=False):
23
23
 
24
24
  return parse_cql_desc_keyspaces(r.stdout if state.pod else r[0].stdout)
25
25
 
26
- def table_names(state: ReplState):
27
- return [f'{k}.{t}' for k, ts in tables(state, on_any=True).items() for t in ts]
26
+ def cassandra_table_names(state: ReplState, keyspace = None):
27
+ return [f'{k}.{t}' for k, ts in tables(state, on_any=True).items() for t in ts if not keyspace or k == keyspace]
28
28
 
29
29
  @functools.lru_cache()
30
- def tables(state: ReplState, on_any=False):
30
+ def tables(state: ReplState, on_any=False) -> dict[str, list[str]]:
31
31
  r: list[PodExecResult] = run_cql(state, 'describe tables', show_out=False, on_any=on_any)
32
32
  if not r:
33
33
  log2('No pod is available')
34
- return []
34
+ return {}
35
35
 
36
36
  return parse_cql_desc_tables(r.stdout if state.pod else r[0].stdout)
37
37
 
38
- def run_cql(state: ReplState, cql: str, opts: list = [], show_out = False, use_single_quotes = False, on_any = False):
38
+ @functools.lru_cache()
39
+ def table_spec(state: ReplState, table: str, on_any=False) -> 'TableSpec':
40
+ r: list[PodExecResult] = run_cql(state, f'describe table {table}', show_out=False, on_any=on_any)
41
+ if not r:
42
+ log2('No pod is available')
43
+ return None
44
+
45
+ return parse_cql_desc_table(r.stdout if state.pod else r[0].stdout)
46
+
47
+ def run_cql(state: ReplState, cql: str, opts: list = [], show_out = False, use_single_quotes = False, on_any = False, background=False) -> list[PodExecResult]:
39
48
  user, pw = Secrets.get_user_pass(state.sts if state.sts else state.pod, state.namespace, secret_path='cql.secret')
40
49
  if use_single_quotes:
41
50
  command = f"cqlsh -u {user} -p {pw} {' '.join(opts)} -e '{cql}'"
@@ -43,9 +52,9 @@ def run_cql(state: ReplState, cql: str, opts: list = [], show_out = False, use_s
43
52
  command = f'cqlsh -u {user} -p {pw} {" ".join(opts)} -e "{cql}"'
44
53
 
45
54
  if state.pod:
46
- return CassandraNodes.exec(state.pod, state.namespace, command, show_out=show_out)
55
+ return CassandraNodes.exec(state.pod, state.namespace, command, show_out=show_out, background=background)
47
56
  else:
48
- return CassandraClusters.exec(state.sts, state.namespace, command, show_out=show_out, action='cql', on_any=on_any)
57
+ return CassandraClusters.exec(state.sts, state.namespace, command, show_out=show_out, action='cql', on_any=on_any, background=background)
49
58
 
50
59
  def parse_cql_desc_tables(out: str):
51
60
  # Keyspace data_endpoint_auth
@@ -109,4 +118,87 @@ def parse_cql_desc_keyspaces(out: str) -> list[str]:
109
118
  if s := ks.strip(' \r\t'):
110
119
  kses.append(s)
111
120
 
112
- return kses
121
+ return kses
122
+
123
+ class ColumnSpec:
124
+ def __init__(self, name: str, type: str, key_index = -1):
125
+ self.name = name
126
+ self.type = type
127
+ self.key_index = key_index
128
+
129
+ def __eq__(self, other):
130
+ if not isinstance(other, ColumnSpec):
131
+ return NotImplemented # Or raise TypeError, depending on desired strictness
132
+
133
+ # Then, compare the relevant attributes for equality
134
+ return self.name == other.name and self.type == other.type and self.key_index == other.key_index
135
+
136
+ class TableSpec:
137
+ def __init__(self, columns: list[ColumnSpec]):
138
+ self.columns = columns
139
+
140
+ def row_key(self):
141
+ for c in self.columns:
142
+ if c.key_index == 0:
143
+ return c.name
144
+
145
+ def keys(self):
146
+ return [c.name for c in self.columns if c.key_index > -1]
147
+
148
+ def parse_cql_desc_table(out: str) -> TableSpec:
149
+ # CREATE TABLE azops88_db.analyticscontainer_dfeevalhistory (
150
+ # id text,
151
+ # columnname text,
152
+ # version bigint static,
153
+ # contentb blob,
154
+ # contentbool boolean,
155
+ # contentn double,
156
+ # contents text,
157
+ # PRIMARY KEY (id, columnname)
158
+ # ) WITH CLUSTERING ORDER BY (columnname ASC)
159
+ # AND additional_write_policy = '99p'
160
+ # AND bloom_filter_fp_chance = 0.1
161
+ # AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
162
+ # AND cdc = false
163
+ # AND comment = ''
164
+ # AND compaction = {'class': 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
165
+ # AND compression = {'chunk_length_in_kb': '16', 'class': 'org.apache.cassandra.io.compress.SnappyCompressor'}
166
+ # AND memtable = 'default'
167
+ # AND crc_check_chance = 1.0
168
+ # AND default_time_to_live = 0
169
+ # AND extensions = {}
170
+ # AND gc_grace_seconds = 3600
171
+ # AND max_index_interval = 2048
172
+ # AND memtable_flush_period_in_ms = 0
173
+ # AND min_index_interval = 128
174
+ # AND read_repair = 'BLOCKING'
175
+ # AND speculative_retry = '99p';
176
+ pkeys = {}
177
+ columns: list[ColumnSpec] = []
178
+
179
+ state = 's0'
180
+ for line in out.split('\n'):
181
+ if state == 's0':
182
+ if line.startswith('CREATE TABLE'):
183
+ state = 's1'
184
+ elif state == 's1':
185
+ if line.startswith(')'):
186
+ state = 's2'
187
+ continue
188
+
189
+ groups = re.match(r'^\s*PRIMARY KEY\s*\((.*)\).*$', line)
190
+ if groups:
191
+ pkeys = {n.strip(' '): i for i, n in enumerate(groups[1].strip(' \r').split(','))}
192
+ continue
193
+
194
+ groups = re.match(r'^\s*(\S*?)\s*(\S*?),.*$', line)
195
+ if groups:
196
+ columns.append(ColumnSpec(groups[1], groups[2]))
197
+ elif state == 's2':
198
+ pass
199
+
200
+ for column in columns:
201
+ if column.name in pkeys.keys():
202
+ column.key_index = pkeys[column.name]
203
+
204
+ return TableSpec(columns)