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,8 +1,8 @@
1
1
  from kubernetes import client
2
- import yaml
3
2
 
3
+ from adam.commands import extract_options
4
4
  from adam.commands.command import Command
5
- from adam.commands.deploy.deploy_utils import creating, deploy_frontend, gen_labels
5
+ from adam.commands.deploy.deploy_utils import deploy_frontend, gen_labels
6
6
  from adam.commands.deploy.undeploy_pod import UndeployPod
7
7
  from adam.config import Config
8
8
  from adam.utils_k8s.config_maps import ConfigMaps
@@ -12,7 +12,7 @@ from adam.utils_k8s.pods import Pods
12
12
  from adam.utils_k8s.service_accounts import ServiceAccounts
13
13
  from adam.utils_k8s.volumes import ConfigMapMount
14
14
  from adam.repl_state import ReplState, RequiredState
15
- from adam.utils import log2
15
+ from adam.utils import ing, log2
16
16
 
17
17
  class DeployPod(Command):
18
18
  COMMAND = 'deploy pod'
@@ -36,76 +36,70 @@ class DeployPod(Command):
36
36
  if not(args := self.args(cmd)):
37
37
  return super().run(cmd, state)
38
38
 
39
- state, args = self.apply_state(args, state)
40
- if not self.validate_state(state):
41
- return state
42
-
43
- args, forced = Command.extract_options(args, '--force')
44
- if forced:
45
- UndeployPod().run(UndeployPod.COMMAND, state)
46
-
47
- if KubeContext.in_cluster():
48
- log2('This is doable only from outside of the Kubernetes cluster.')
49
- return state
50
-
51
- sa_name = Config().get('pod.sa.name', 'ops')
52
- sa_proto = Config().get('pod.sa.proto', 'c3')
53
- additional_cluster_roles = Config().get('pod.sa.additional-cluster-roles', 'c3aiops-k8ssandra-operator').split(',')
54
- label_selector = Config().get('pod.label-selector', 'run=ops')
55
- labels = gen_labels(label_selector)
56
-
57
- creating('service account', lambda: ServiceAccounts.replicate(sa_name,
58
- state.namespace,
59
- sa_proto, labels=labels,
60
- add_cluster_roles=additional_cluster_roles))
61
-
62
- settings_filename = 'settings.yaml'
63
- settings_path = f'/kaqing/{settings_filename}'
64
- settings_data = None
65
- try:
66
- with open(settings_filename, 'r') as file:
67
- settings_data = file.read()
68
- except:
69
- try:
70
- with open(settings_path, 'r') as file:
71
- settings_data = file.read()
72
- except:
73
- pass
74
-
75
- if not settings_data:
76
- log2(f'{settings_filename} not found.')
77
- return state
78
-
79
- cm_name = Config().get('pod.cm.name', 'ops')
80
- map_data = {
81
- settings_filename : settings_data
82
- }
83
- creating('config map', lambda: ConfigMaps.create(cm_name,
84
- state.namespace,
85
- map_data,
86
- labels=labels))
87
-
88
- pod_name = Config().get('pod.name', 'ops')
89
- image = Config().get('pod.image', 'seanahnsf/kaqing')
90
- security_context = client.V1SecurityContext(
91
- capabilities=client.V1Capabilities(
92
- add=["SYS_PTRACE"]
93
- )
94
- )
95
- creating('deployment', lambda: Deployments.create(state.namespace,
96
- pod_name,
97
- image,
98
- env={'NAMESPACE': state.namespace},
99
- container_security_context=security_context,
100
- labels=labels,
101
- sa_name=sa_name,
102
- config_map_mount=ConfigMapMount(cm_name, settings_filename, settings_path)))
103
-
104
- uri = deploy_frontend(pod_name, state.namespace, label_selector)
105
-
106
- Pods.wait_for_running(state.namespace, pod_name, msg=f'In moments, ops pod will be available at {uri}.', label_selector=label_selector)
107
-
108
- return state
39
+ with self.validate(args, state) as (args, state):
40
+ with extract_options(args, '--force') as (args, forced):
41
+ if forced:
42
+ UndeployPod().run(UndeployPod.COMMAND, state)
43
+
44
+ if KubeContext.in_cluster():
45
+ log2('This is doable only from outside of the Kubernetes cluster.')
46
+ return state
47
+
48
+ sa_name = Config().get('pod.sa.name', 'ops')
49
+ sa_proto = Config().get('pod.sa.proto', 'c3')
50
+ additional_cluster_roles = Config().get('pod.sa.additional-cluster-roles', 'c3aiops-k8ssandra-operator').split(',')
51
+ label_selector = Config().get('pod.label-selector', 'run=ops')
52
+ labels = gen_labels(label_selector)
53
+
54
+ with ing('Creating service account'):
55
+ ServiceAccounts.replicate(sa_name, state.namespace, sa_proto, labels=labels, add_cluster_roles=additional_cluster_roles)
56
+
57
+ settings_filename = 'settings.yaml'
58
+ settings_path = f'/kaqing/{settings_filename}'
59
+ settings_data = None
60
+ try:
61
+ with open(settings_filename, 'r') as file:
62
+ settings_data = file.read()
63
+ except:
64
+ try:
65
+ with open(settings_path, 'r') as file:
66
+ settings_data = file.read()
67
+ except:
68
+ pass
69
+
70
+ if not settings_data:
71
+ log2(f'{settings_filename} not found.')
72
+ return state
73
+
74
+ cm_name = Config().get('pod.cm.name', 'ops')
75
+ map_data = {
76
+ settings_filename : settings_data
77
+ }
78
+ with ing('Creating config map'):
79
+ ConfigMaps.create(cm_name, state.namespace, map_data, labels=labels)
80
+
81
+ pod_name = Config().get('pod.name', 'ops')
82
+ image = Config().get('pod.image', 'seanahnsf/kaqing')
83
+ security_context = client.V1SecurityContext(
84
+ capabilities=client.V1Capabilities(
85
+ add=["SYS_PTRACE"]
86
+ )
87
+ )
88
+ with ing('Creating deployment'):
89
+ Deployments.create(state.namespace,
90
+ pod_name,
91
+ image,
92
+ env={'NAMESPACE': state.namespace},
93
+ container_security_context=security_context,
94
+ labels=labels,
95
+ sa_name=sa_name,
96
+ config_map_mount=ConfigMapMount(cm_name, settings_filename, settings_path))
97
+
98
+ uri = deploy_frontend(pod_name, state.namespace, label_selector)
99
+
100
+ Pods.wait_for_running(state.namespace, pod_name, msg=f'In moments, ops pod will be available at {uri}.', label_selector=label_selector)
101
+
102
+ return state
109
103
 
110
104
  def completion(self, state: ReplState):
111
105
  return super().completion(state, {'--force': None})
@@ -1,39 +1,29 @@
1
- from typing import Callable
2
1
  from adam.app_session import AppSession
2
+ from adam.utils import ing
3
3
  from adam.utils_k8s.ingresses import Ingresses
4
4
  from adam.utils_k8s.services import Services
5
- from adam.utils import log2
6
5
 
7
6
  def deploy_frontend(name: str, namespace: str, label_selector: str):
8
7
  app_session: AppSession = AppSession.create('c3', 'c3', namespace)
9
8
  port = 7678
10
9
  labels = gen_labels(label_selector)
11
- creating('service', lambda: Services.create_service(name, namespace, port, labels, labels=labels))
12
- creating('ingress', lambda: Ingresses.create_ingress(name, namespace, app_session.host, '/c3/c3/ops($|/)', port, annotations={
13
- 'kubernetes.io/ingress.class': 'nginx',
14
- 'nginx.ingress.kubernetes.io/use-regex': 'true',
15
- 'nginx.ingress.kubernetes.io/rewrite-target': '/'
16
- }, labels=labels))
10
+ with ing('Creating service'):
11
+ Services.create_service(name, namespace, port, labels, labels=labels)
12
+ with ing('Creating ingress'):
13
+ Ingresses.create_ingress(name, namespace, app_session.host, '/c3/c3/ops($|/)', port, annotations={
14
+ 'kubernetes.io/ingress.class': 'nginx',
15
+ 'nginx.ingress.kubernetes.io/use-regex': 'true',
16
+ 'nginx.ingress.kubernetes.io/rewrite-target': '/'
17
+ }, labels=labels)
17
18
 
18
19
  return f'https://{app_session.host}/c3/c3/ops'
19
20
 
20
21
  def undeploy_frontend(namespace: str, label_selector: str):
21
- deleting('ingress', lambda: Ingresses.delete_ingresses(namespace, label_selector=label_selector))
22
- deleting('service', lambda: Services.delete_services(namespace, label_selector=label_selector))
22
+ with ing('Deleting ingress'):
23
+ Ingresses.delete_ingresses(namespace, label_selector=label_selector)
24
+ with ing('Deleting service'):
25
+ Services.delete_services(namespace, label_selector=label_selector)
23
26
 
24
27
  def gen_labels(label_selector: str):
25
28
  kv = label_selector.split('=')
26
- return {kv[0]: kv[1]}
27
-
28
- def creating(name: str, body: Callable[[], None]):
29
- log2(f'Creating {name}...', nl=False)
30
- body()
31
- log2(' OK')
32
-
33
- def deleting(name: str, body: Callable[[], None]):
34
- try:
35
- log2(f'Deleting {name}...', nl=False)
36
- body()
37
- log2(' OK')
38
- except Exception as e:
39
- log2(e)
29
+ return {kv[0]: kv[1]}
@@ -1,14 +1,12 @@
1
1
  import click
2
2
 
3
- from adam.commands.command import Command
4
3
  from adam.commands.deploy.undeploy_frontend import UndeployFrontend
5
4
  from adam.commands.deploy.undeploy_pg_agent import UndeployPgAgent
6
5
  from adam.commands.deploy.undeploy_pod import UndeployPod
7
- from adam.repl_state import ReplState
6
+ from adam.commands.intermediate_command import IntermediateCommand
8
7
 
9
- class Undeploy(Command):
8
+ class Undeploy(IntermediateCommand):
10
9
  COMMAND = 'undeploy'
11
- reaper_login = None
12
10
 
13
11
  # the singleton pattern
14
12
  def __new__(cls, *args, **kwargs):
@@ -16,27 +14,12 @@ class Undeploy(Command):
16
14
 
17
15
  return cls.instance
18
16
 
19
- def __init__(self, successor: Command=None):
20
- super().__init__(successor)
21
-
22
17
  def command(self):
23
18
  return Undeploy.COMMAND
24
19
 
25
- def run(self, cmd: str, state: ReplState):
26
- if not(args := self.args(cmd)):
27
- return super().run(cmd, state)
28
-
29
- return super().intermediate_run(cmd, state, args, Undeploy.cmd_list())
30
-
31
- def cmd_list():
20
+ def cmd_list(self):
32
21
  return [UndeployFrontend(), UndeployPod(), UndeployPgAgent()]
33
22
 
34
- def completion(self, state: ReplState):
35
- if state.sts:
36
- return super().completion(state)
37
-
38
- return {}
39
-
40
23
  class UndeployCommandHelper(click.Command):
41
24
  def get_help(self, ctx: click.Context):
42
- Command.intermediate_help(super().get_help(ctx), Undeploy.COMMAND, Undeploy.cmd_list())
25
+ IntermediateCommand.intermediate_help(super().get_help(ctx), Undeploy.COMMAND, Undeploy().cmd_list())
@@ -25,14 +25,11 @@ class UndeployFrontend(Command):
25
25
  if not(args := self.args(cmd)):
26
26
  return super().run(cmd, state)
27
27
 
28
- state, args = self.apply_state(args, state)
29
- if not self.validate_state(state):
30
- return state
31
-
32
- label_selector = Config().get('pod.label-selector', 'run=ops')
33
- undeploy_frontend(state.namespace, label_selector)
28
+ with self.validate(args, state) as (args, state):
29
+ label_selector = Config().get('pod.label-selector', 'run=ops')
30
+ undeploy_frontend(state.namespace, label_selector)
34
31
 
35
- return state
32
+ return state
36
33
 
37
34
  def completion(self, state: ReplState):
38
35
  return super().completion(state)
@@ -1,8 +1,8 @@
1
1
  from adam.commands.command import Command
2
- from adam.commands.deploy.deploy_utils import deleting
3
- from adam.commands.postgres.postgres_session import PostgresSession
2
+ from adam.commands.postgres.postgres_context import PostgresContext
4
3
  from adam.config import Config
5
4
  from adam.repl_state import ReplState, RequiredState
5
+ from adam.utils import ing
6
6
 
7
7
  class UndeployPgAgent(Command):
8
8
  COMMAND = 'undeploy pg-agent'
@@ -26,13 +26,11 @@ class UndeployPgAgent(Command):
26
26
  if not(args := self.args(cmd)):
27
27
  return super().run(cmd, state)
28
28
 
29
- state, args = self.apply_state(args, state)
30
- if not self.validate_state(state):
31
- return state
32
-
33
- deleting('pod', lambda: PostgresSession.undeploy_pg_agent(Config().get('pg.agent.name', 'ops-pg-agent'), state.namespace))
29
+ with self.validate(args, state) as (args, state):
30
+ with ing('Deleting pod'):
31
+ PostgresContext.undeploy_pg_agent(Config().get('pg.agent.name', 'ops-pg-agent'), state.namespace)
34
32
 
35
- return state
33
+ return state
36
34
 
37
35
  def completion(self, state: ReplState):
38
36
  return super().completion(state)
@@ -1,6 +1,7 @@
1
1
  from adam.commands.command import Command
2
- from adam.commands.deploy.deploy_utils import undeploy_frontend, deleting
2
+ from adam.commands.deploy.deploy_utils import undeploy_frontend
3
3
  from adam.config import Config
4
+ from adam.utils import ing
4
5
  from adam.utils_k8s.config_maps import ConfigMaps
5
6
  from adam.utils_k8s.deployment import Deployments
6
7
  from adam.utils_k8s.pods import Pods
@@ -29,18 +30,16 @@ class UndeployPod(Command):
29
30
  if not(args := self.args(cmd)):
30
31
  return super().run(cmd, state)
31
32
 
32
- state, args = self.apply_state(args, state)
33
- if not self.validate_state(state):
34
- return state
35
-
36
- label_selector = Config().get('pod.label-selector', 'run=ops')
37
- deleting('service account', lambda: ServiceAccounts.delete(state.namespace, label_selector=label_selector))
38
- deleting('config map', lambda: ConfigMaps.delete_with_selector(state.namespace, label_selector))
39
- deleting('deployment', lambda: Deployments.delete_with_selector(state.namespace, label_selector, grace_period_seconds=0))
40
- deleting('pod', lambda: Pods.delete_with_selector(state.namespace, label_selector, grace_period_seconds=0))
41
- undeploy_frontend(state.namespace, label_selector)
33
+ with self.validate(args, state) as (args, state):
34
+ label_selector = Config().get('pod.label-selector', 'run=ops')
35
+ with ing('Deleting service account'):
36
+ ServiceAccounts.delete(state.namespace, label_selector=label_selector)
37
+ with ing('Deleting config map'): ConfigMaps.delete_with_selector(state.namespace, label_selector)
38
+ with ing('Deleting deployment'): Deployments.delete_with_selector(state.namespace, label_selector, grace_period_seconds=0)
39
+ with ing('Deleting pod'): Pods.delete_with_selector(state.namespace, label_selector, grace_period_seconds=0)
40
+ undeploy_frontend(state.namespace, label_selector)
42
41
 
43
- return state
42
+ return state
44
43
 
45
44
  def completion(self, state: ReplState):
46
45
  return super().completion(state)
@@ -0,0 +1,118 @@
1
+ from abc import abstractmethod
2
+
3
+ from adam.commands.command import Command
4
+ from adam.config import Config
5
+ from adam.pod_exec_result import PodExecResult
6
+ from adam.repl_state import BashSession, ReplState
7
+ from adam.utils import log2
8
+
9
+ class Device:
10
+ @abstractmethod
11
+ def ls(self, cmd: str, state: ReplState):
12
+ pass
13
+
14
+ def ls_completion(self, cmd: str, state: ReplState, default: dict = {}):
15
+ return default
16
+
17
+ @abstractmethod
18
+ def cat(self, cmd: str, state: ReplState):
19
+ pass
20
+
21
+ def cat_completion(self, cmd: str, state: ReplState, default: dict = {}):
22
+ return default
23
+
24
+ def cd(self, dir: str, state: ReplState):
25
+ pass
26
+
27
+ def cd_completion(self, cmd: str, state: ReplState, default: dict = {}):
28
+ return default
29
+
30
+ @abstractmethod
31
+ def pwd(self, state: ReplState):
32
+ pass
33
+
34
+ def try_fallback_action(self, chain: Command, state: ReplState, cmd: str):
35
+ return False, None
36
+
37
+ def enter(self, state: ReplState):
38
+ pass
39
+
40
+ def preview(self, table: str, state: ReplState):
41
+ if not table:
42
+ if state.in_repl:
43
+ log2('Table is required.')
44
+ log2()
45
+ log2('Tables:')
46
+ self.show_tables(state)
47
+ else:
48
+ log2('* Table is missing.')
49
+ self.show_tables(state)
50
+
51
+ Command.display_help()
52
+
53
+ return 'command-missing'
54
+
55
+ rows = Config().get('preview.rows', 10)
56
+ self.show_table_preview(state, table, rows)
57
+
58
+ return state
59
+
60
+ @abstractmethod
61
+ def show_tables(self, state: ReplState):
62
+ pass
63
+
64
+ @abstractmethod
65
+ def show_table_preview(self, state: ReplState, table: str, rows: int):
66
+ pass
67
+
68
+ def bash(self, s0: ReplState, s1: ReplState, args: list[str]):
69
+ if s1.in_repl:
70
+ if self.bash_target_changed(s0, s1):
71
+ r = self._exec_with_dir(s1, args)
72
+ else:
73
+ r = self._exec_with_dir(s0, args)
74
+
75
+ if not r:
76
+ s1.exit_bash()
77
+
78
+ return 'inconsistent pwd'
79
+
80
+ return r
81
+ else:
82
+ self.exec_no_dir(' '.join(args), s1)
83
+
84
+ return s1
85
+
86
+ def _exec_with_dir(self, state: ReplState, args: list[str]) -> list[PodExecResult]:
87
+ session_just_created = False
88
+ if not args:
89
+ session_just_created = True
90
+ session = BashSession(state.device)
91
+ state.enter_bash(session)
92
+
93
+ if state.bash_session:
94
+ if args != ['pwd']:
95
+ if args:
96
+ args.append('&&')
97
+ args.extend(['pwd', '>', f'/tmp/.qing-{state.bash_session.session_id}'])
98
+
99
+ if not session_just_created:
100
+ if pwd := state.bash_session.pwd(state):
101
+ args = ['cd', pwd, '&&'] + args
102
+
103
+ return self.exec_with_dir(' '.join(args), session_just_created, state)
104
+
105
+ @abstractmethod
106
+ def bash_target_changed(self, s0: ReplState, s1: ReplState):
107
+ pass
108
+
109
+ @abstractmethod
110
+ def exec_no_dir(self, command: str, state: ReplState):
111
+ pass
112
+
113
+ @abstractmethod
114
+ def exec_with_dir(self, command: str, session_just_created: bool, state: ReplState):
115
+ pass
116
+
117
+ def bash_completion(self, cmd: str, state: ReplState, default: dict = {}):
118
+ return default
@@ -0,0 +1,173 @@
1
+ from adam.apps import Apps
2
+ from adam.commands import app
3
+ from adam.commands.bash.bash_completer import BashCompleter
4
+ from adam.commands.command import Command, InvalidState
5
+ from adam.commands.devices.device import Device
6
+ from adam.config import Config
7
+ from adam.repl_state import ReplState
8
+ from adam.utils import lines_to_tabular, log, wait_log
9
+ from adam.utils_k8s.app_pods import AppPods
10
+ from adam.utils_k8s.ingresses import Ingresses
11
+
12
+ class DeviceApp(Command, Device):
13
+ COMMAND = f'{ReplState.A}:'
14
+
15
+ # the singleton pattern
16
+ def __new__(cls, *args, **kwargs):
17
+ if not hasattr(cls, 'instance'): cls.instance = super(DeviceApp, cls).__new__(cls)
18
+
19
+ return cls.instance
20
+
21
+ def __init__(self, successor: Command=None):
22
+ super().__init__(successor)
23
+
24
+ def command(self):
25
+ return DeviceApp.COMMAND
26
+
27
+ def run(self, cmd: str, state: ReplState):
28
+ if not self.args(cmd):
29
+ return super().run(cmd, state)
30
+
31
+ state.device = ReplState.A
32
+
33
+ return state
34
+
35
+ def completion(self, state: ReplState):
36
+ return super().completion(state)
37
+
38
+ def help(self, _: ReplState):
39
+ return f'{DeviceApp.COMMAND}\t move to App Operations device'
40
+
41
+ def ls(self, cmd: str, state: ReplState):
42
+ if state.app_pod:
43
+ return self.bash(state, state, cmd.split(' '))
44
+ elif state.app_app:
45
+ pods = AppPods.pod_names(state.namespace, state.app_env, state.app_app)
46
+
47
+ log(lines_to_tabular(pods, 'POD_NAME'))
48
+ elif state.app_env:
49
+ def line(n: str, ns: str):
50
+ host = Ingresses.get_host(Config().get('app.login.ingress', '{app_id}-k8singr-appleader-001').replace('{app_id}', f'{ns}-{n}'), ns)
51
+ if not host:
52
+ return None
53
+
54
+ endpoint = Config().get('app.login.url', 'https://{host}/{env}/{app}').replace('{host}', host).replace('{env}', state.app_env).replace('{app}', 'c3')
55
+ if not endpoint:
56
+ return None
57
+
58
+ return f"{n.split('-')[1]},{Ingresses.get_host(f'{ns}-{n}-k8singr-appleader-001', ns)},{endpoint}"
59
+
60
+ svcs = [l for l in [line(n, ns) for n, ns in Apps.apps(state.app_env)] if l]
61
+
62
+ log(lines_to_tabular(svcs, 'APP,HOST,ENDPOINT', separator=','))
63
+ else:
64
+ svcs = [n for n, ns in Apps.envs()]
65
+
66
+ log(lines_to_tabular(svcs, 'ENV', separator=','))
67
+
68
+ def ls_completion(self, cmd, state, default: dict = {}):
69
+ if state.app_app:
70
+ def pod_names():
71
+ return [p for p in AppPods.pod_names(state.namespace, state.app_env, state.app_app)]
72
+
73
+ return super().completion(state) | {f'@{p}': {cmd: None} for p in pod_names()}
74
+
75
+ return default
76
+
77
+ def cat(self, cmd: str, state: ReplState):
78
+ if state.app_pod:
79
+ return self.bash(state, state, cmd.split(' '))
80
+
81
+ return InvalidState()
82
+
83
+ def cat_completion(self, cmd: str, state: ReplState, default: dict = {}):
84
+ if state.app_app:
85
+ return {f'@{p}': {cmd: None} for p in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}
86
+
87
+ return default
88
+
89
+ def cd(self, dir: str, state: ReplState):
90
+ if dir == '':
91
+ state.app_env = None
92
+ state.app_app = None
93
+ state.app_pod = None
94
+ elif dir == '..':
95
+ if state.app_pod:
96
+ state.app_pod = None
97
+ elif state.app_app:
98
+ state.app_app = None
99
+ else:
100
+ state.app_env = None
101
+ else:
102
+ if state.app_app:
103
+ state.app_pod = dir
104
+ elif not state.app_env:
105
+ tks = dir.split('@')
106
+ if len(tks) > 1:
107
+ state.namespace = tks[1]
108
+
109
+ state.app_env = dir.split('@')[0]
110
+ else:
111
+ state.app_app = dir
112
+
113
+ def cd_completion(self, cmd: str, state: ReplState, default: dict = {}):
114
+ if state.app_app:
115
+ return {cmd: {'..': None} | {pod: None for pod in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}}
116
+ elif state.app_env:
117
+ return {cmd: {'..': None} | {app[0].split('-')[1]: None for app in Apps.apps(state.app_env)}}
118
+ else:
119
+ return {cmd: {'..': None} | {env[0]: None for env in Apps.envs()}}
120
+
121
+ def pwd(self, state: ReplState):
122
+ words = []
123
+
124
+ if state.app_env:
125
+ words.append(f'env/{state.app_env}')
126
+ if state.app_app:
127
+ words.append(f'app/{state.app_app}')
128
+
129
+ return '\t'.join([f'{ReplState.X}:>'] + (words if words else ['/']))
130
+
131
+ def try_fallback_action(self, chain: Command, state: ReplState, cmd: str):
132
+ if state.app_app:
133
+ return True, chain.run(f'app {cmd}', state)
134
+
135
+ return False, None
136
+
137
+ def enter(self, state: ReplState):
138
+ if not state.app_env:
139
+ if auto_enter := Config().get('repl.a.auto-enter', 'c3/c3/*'):
140
+ if auto_enter != 'no':
141
+ ea = auto_enter.split('/')
142
+ state.app_env = ea[0]
143
+ if len(ea) > 2:
144
+ state.app_app = ea[1]
145
+ state.app_pod = ea[2]
146
+ if state.app_pod == '*':
147
+ if (pods := AppPods.pod_names(state.namespace, ea[0], ea[1])):
148
+ state.app_pod = pods[0]
149
+ wait_log(f'Moving to {state.app_env}/{state.app_app}/{state.app_pod}...')
150
+ else:
151
+ wait_log(f'No pods found, moving to {state.app_env}/{state.app_app}...')
152
+ else:
153
+ wait_log(f'Moving to {state.app_env}/{state.app_app}/{state.app_pod}...')
154
+ elif len(ea) > 1:
155
+ state.app_app = ea[1]
156
+ wait_log(f'Moving to {state.app_env}/{state.app_app}...')
157
+ else:
158
+ wait_log(f'Moving to {state.app_env}...')
159
+
160
+ def bash_target_changed(self, s0: ReplState, s1: ReplState):
161
+ return s0.app_env != s1.app_env or s0.app_app != s1.app_app or s0.app_pod != s1.app_pod
162
+
163
+ def exec_no_dir(self, command: str, state: ReplState):
164
+ with app(state) as pods:
165
+ return pods.exec(command)
166
+
167
+ def exec_with_dir(self, command: str, session_just_created: bool, state: ReplState):
168
+ with app(state) as pods:
169
+ return pods.exec(command, not session_just_created)
170
+
171
+ def bash_completion(self, cmd: str, state: ReplState, default: dict = {}):
172
+ return {cmd: BashCompleter(lambda: [])} | \
173
+ {f'@{p}': {cmd: BashCompleter(lambda: [])} for p in AppPods.pod_names(state.namespace, state.app_env, state.app_app)}