kaqing 2.0.14__py3-none-any.whl → 2.0.189__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kaqing might be problematic. Click here for more details.
- adam/__init__.py +0 -2
- adam/app_session.py +9 -12
- adam/apps.py +20 -6
- adam/batch.py +16 -6
- adam/checks/check_utils.py +19 -49
- adam/checks/compactionstats.py +1 -1
- adam/checks/cpu.py +9 -3
- adam/checks/cpu_metrics.py +52 -0
- adam/checks/disk.py +3 -4
- adam/checks/gossip.py +1 -1
- adam/checks/memory.py +3 -3
- adam/checks/status.py +1 -1
- adam/columns/columns.py +3 -1
- adam/columns/cpu.py +3 -1
- adam/columns/cpu_metrics.py +22 -0
- adam/columns/memory.py +3 -4
- adam/commands/__init__.py +24 -0
- adam/commands/alter_tables.py +66 -0
- adam/commands/app/app.py +38 -0
- adam/commands/{app_ping.py → app/app_ping.py} +8 -14
- adam/commands/app/show_app_actions.py +49 -0
- adam/commands/{show → app}/show_app_id.py +9 -12
- adam/commands/{show → app}/show_app_queues.py +8 -14
- adam/commands/app/utils_app.py +106 -0
- adam/commands/audit/__init__.py +0 -0
- adam/commands/audit/audit.py +67 -0
- adam/commands/audit/audit_repair_tables.py +72 -0
- adam/commands/audit/audit_run.py +50 -0
- adam/commands/audit/completions_l.py +15 -0
- adam/commands/audit/show_last10.py +36 -0
- adam/commands/audit/show_slow10.py +36 -0
- adam/commands/audit/show_top10.py +36 -0
- adam/commands/audit/utils_show_top10.py +71 -0
- adam/commands/bash/__init__.py +5 -0
- adam/commands/bash/bash.py +36 -0
- adam/commands/bash/bash_completer.py +93 -0
- adam/commands/bash/utils_bash.py +16 -0
- adam/commands/cat.py +36 -0
- adam/commands/cd.py +14 -88
- adam/commands/check.py +18 -21
- adam/commands/cli_commands.py +11 -7
- adam/commands/clipboard_copy.py +87 -0
- adam/commands/code.py +57 -0
- adam/commands/command.py +220 -19
- adam/commands/commands_utils.py +28 -31
- adam/commands/cql/__init__.py +0 -0
- adam/commands/cql/completions_c.py +28 -0
- adam/commands/{cqlsh.py → cql/cqlsh.py} +13 -32
- adam/commands/cql/utils_cql.py +305 -0
- adam/commands/deploy/code_start.py +7 -10
- adam/commands/deploy/code_stop.py +4 -21
- adam/commands/deploy/code_utils.py +5 -5
- adam/commands/deploy/deploy.py +4 -40
- adam/commands/deploy/deploy_frontend.py +15 -18
- adam/commands/deploy/deploy_pg_agent.py +4 -7
- adam/commands/deploy/deploy_pod.py +74 -77
- adam/commands/deploy/deploy_utils.py +16 -26
- adam/commands/deploy/undeploy.py +4 -40
- adam/commands/deploy/undeploy_frontend.py +5 -8
- adam/commands/deploy/undeploy_pg_agent.py +7 -8
- adam/commands/deploy/undeploy_pod.py +16 -17
- adam/commands/devices/__init__.py +0 -0
- adam/commands/devices/device.py +149 -0
- adam/commands/devices/device_app.py +163 -0
- adam/commands/devices/device_auit_log.py +49 -0
- adam/commands/devices/device_cass.py +179 -0
- adam/commands/devices/device_export.py +87 -0
- adam/commands/devices/device_postgres.py +160 -0
- adam/commands/devices/devices.py +25 -0
- adam/commands/download_file.py +47 -0
- adam/commands/exit.py +1 -4
- adam/commands/export/__init__.py +0 -0
- adam/commands/export/clean_up_all_export_sessions.py +37 -0
- adam/commands/export/clean_up_export_sessions.py +39 -0
- adam/commands/export/completions_x.py +11 -0
- adam/commands/export/download_export_session.py +40 -0
- adam/commands/export/drop_export_database.py +39 -0
- adam/commands/export/drop_export_databases.py +37 -0
- adam/commands/export/export.py +37 -0
- adam/commands/export/export_databases.py +246 -0
- adam/commands/export/export_select.py +34 -0
- adam/commands/export/export_sessions.py +209 -0
- adam/commands/export/export_use.py +49 -0
- adam/commands/export/export_x_select.py +48 -0
- adam/commands/export/exporter.py +332 -0
- adam/commands/export/import_files.py +44 -0
- adam/commands/export/import_session.py +44 -0
- adam/commands/export/importer.py +81 -0
- adam/commands/export/importer_athena.py +148 -0
- adam/commands/export/importer_sqlite.py +67 -0
- adam/commands/export/show_column_counts.py +45 -0
- adam/commands/export/show_export_databases.py +39 -0
- adam/commands/export/show_export_session.py +39 -0
- adam/commands/export/show_export_sessions.py +37 -0
- adam/commands/export/utils_export.py +344 -0
- adam/commands/find_files.py +51 -0
- adam/commands/find_processes.py +76 -0
- adam/commands/head.py +36 -0
- adam/commands/help.py +14 -9
- adam/commands/intermediate_command.py +52 -0
- adam/commands/issues.py +14 -40
- adam/commands/kubectl.py +38 -0
- adam/commands/login.py +26 -25
- adam/commands/logs.py +5 -7
- adam/commands/ls.py +11 -115
- adam/commands/medusa/medusa.py +4 -46
- adam/commands/medusa/medusa_backup.py +22 -29
- adam/commands/medusa/medusa_restore.py +51 -49
- adam/commands/medusa/medusa_show_backupjobs.py +20 -21
- adam/commands/medusa/medusa_show_restorejobs.py +16 -21
- adam/commands/medusa/utils_medusa.py +15 -0
- adam/commands/nodetool.py +8 -17
- adam/commands/param_get.py +11 -14
- adam/commands/param_set.py +9 -13
- adam/commands/postgres/completions_p.py +22 -0
- adam/commands/postgres/postgres.py +49 -73
- adam/commands/postgres/postgres_databases.py +270 -0
- adam/commands/postgres/postgres_ls.py +4 -8
- adam/commands/postgres/postgres_preview.py +5 -9
- adam/commands/postgres/utils_postgres.py +79 -0
- adam/commands/preview_table.py +10 -69
- adam/commands/pwd.py +14 -43
- adam/commands/reaper/reaper.py +6 -49
- adam/commands/reaper/reaper_forward.py +49 -56
- adam/commands/reaper/reaper_forward_session.py +6 -0
- adam/commands/reaper/reaper_forward_stop.py +10 -16
- adam/commands/reaper/reaper_restart.py +8 -15
- adam/commands/reaper/reaper_run_abort.py +8 -33
- adam/commands/reaper/reaper_runs.py +43 -58
- adam/commands/reaper/reaper_runs_abort.py +29 -49
- adam/commands/reaper/reaper_schedule_activate.py +14 -33
- adam/commands/reaper/reaper_schedule_start.py +9 -33
- adam/commands/reaper/reaper_schedule_stop.py +9 -33
- adam/commands/reaper/reaper_schedules.py +4 -14
- adam/commands/reaper/reaper_status.py +8 -16
- adam/commands/reaper/utils_reaper.py +203 -0
- adam/commands/repair/repair.py +4 -46
- adam/commands/repair/repair_log.py +6 -12
- adam/commands/repair/repair_run.py +29 -36
- adam/commands/repair/repair_scan.py +33 -41
- adam/commands/repair/repair_stop.py +6 -13
- adam/commands/report.py +25 -21
- adam/commands/restart.py +27 -28
- adam/commands/rollout.py +20 -25
- adam/commands/shell.py +12 -4
- adam/commands/show/show.py +15 -46
- adam/commands/show/show_adam.py +3 -3
- adam/commands/show/show_cassandra_repairs.py +37 -0
- adam/commands/show/show_cassandra_status.py +48 -52
- adam/commands/show/show_cassandra_version.py +5 -18
- adam/commands/show/show_cli_commands.py +56 -0
- adam/commands/show/show_host.py +33 -0
- adam/commands/show/show_login.py +23 -27
- adam/commands/show/show_params.py +2 -5
- adam/commands/show/show_processes.py +18 -21
- adam/commands/show/show_storage.py +11 -20
- adam/commands/watch.py +27 -30
- adam/config.py +8 -6
- adam/embedded_params.py +1 -1
- adam/log.py +4 -4
- adam/pod_exec_result.py +13 -5
- adam/repl.py +136 -120
- adam/repl_commands.py +66 -24
- adam/repl_session.py +8 -1
- adam/repl_state.py +343 -73
- adam/sql/__init__.py +0 -0
- adam/sql/lark_completer.py +284 -0
- adam/sql/lark_parser.py +604 -0
- adam/sql/sql_completer.py +118 -0
- adam/sql/sql_state_machine.py +630 -0
- adam/sql/term_completer.py +76 -0
- adam/sso/authn_ad.py +7 -9
- adam/sso/authn_okta.py +4 -6
- adam/sso/cred_cache.py +4 -6
- adam/sso/idp.py +10 -13
- adam/utils.py +539 -11
- adam/utils_athena.py +145 -0
- adam/utils_audits.py +102 -0
- adam/utils_issues.py +32 -0
- adam/utils_k8s/__init__.py +0 -0
- adam/utils_k8s/app_clusters.py +28 -0
- adam/utils_k8s/app_pods.py +36 -0
- adam/utils_k8s/cassandra_clusters.py +44 -0
- adam/{k8s_utils → utils_k8s}/cassandra_nodes.py +12 -5
- adam/{k8s_utils → utils_k8s}/custom_resources.py +16 -17
- adam/{k8s_utils → utils_k8s}/deployment.py +2 -2
- adam/{k8s_utils → utils_k8s}/ingresses.py +2 -2
- adam/{k8s_utils → utils_k8s}/jobs.py +7 -11
- adam/utils_k8s/k8s.py +96 -0
- adam/{k8s_utils → utils_k8s}/kube_context.py +3 -3
- adam/{k8s_utils → utils_k8s}/pods.py +132 -83
- adam/{k8s_utils → utils_k8s}/secrets.py +7 -3
- adam/{k8s_utils → utils_k8s}/service_accounts.py +5 -4
- adam/{k8s_utils → utils_k8s}/services.py +2 -2
- adam/{k8s_utils → utils_k8s}/statefulsets.py +9 -16
- adam/utils_local.py +4 -0
- adam/utils_net.py +24 -0
- adam/utils_repl/__init__.py +0 -0
- adam/utils_repl/appendable_completer.py +6 -0
- adam/utils_repl/automata_completer.py +48 -0
- adam/utils_repl/repl_completer.py +172 -0
- adam/utils_repl/state_machine.py +173 -0
- adam/utils_sqlite.py +137 -0
- adam/version.py +1 -1
- {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/METADATA +1 -1
- kaqing-2.0.189.dist-info/RECORD +253 -0
- kaqing-2.0.189.dist-info/top_level.txt +2 -0
- teddy/__init__.py +0 -0
- teddy/lark_parser.py +436 -0
- teddy/lark_parser2.py +618 -0
- adam/commands/app.py +0 -67
- adam/commands/bash.py +0 -87
- adam/commands/cp.py +0 -95
- adam/commands/cql_utils.py +0 -53
- adam/commands/devices.py +0 -89
- adam/commands/postgres/postgres_session.py +0 -247
- adam/commands/reaper/reaper_session.py +0 -159
- adam/commands/show/show_app_actions.py +0 -53
- adam/commands/show/show_commands.py +0 -61
- adam/commands/show/show_repairs.py +0 -47
- adam/k8s_utils/cassandra_clusters.py +0 -48
- kaqing-2.0.14.dist-info/RECORD +0 -167
- kaqing-2.0.14.dist-info/top_level.txt +0 -1
- /adam/{k8s_utils → commands/app}/__init__.py +0 -0
- /adam/{k8s_utils → utils_k8s}/config_maps.py +0 -0
- /adam/{k8s_utils → utils_k8s}/volumes.py +0 -0
- {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/WHEEL +0 -0
- {kaqing-2.0.14.dist-info → kaqing-2.0.189.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from adam.config import Config
|
|
2
|
+
from adam.repl_state import ReplState
|
|
3
|
+
from adam.utils_k8s.custom_resources import CustomResources
|
|
4
|
+
from adam.utils_k8s.statefulsets import StatefulSets
|
|
5
|
+
|
|
6
|
+
def medusa_backup_names(state: ReplState, warm=False):
|
|
7
|
+
if warm and (auto := Config().get('medusa.restore-auto-complete', 'off')) in ['off', 'jit', 'lazy']:
|
|
8
|
+
return {}
|
|
9
|
+
|
|
10
|
+
ns = state.namespace
|
|
11
|
+
dc: str = StatefulSets.get_datacenter(state.sts, ns)
|
|
12
|
+
if not dc:
|
|
13
|
+
return {}
|
|
14
|
+
|
|
15
|
+
return {id: None for id in [f"{x['metadata']['name']}" for x in CustomResources.medusa_show_backupjobs(dc, ns)]}
|
adam/commands/nodetool.py
CHANGED
|
@@ -2,10 +2,10 @@ import click
|
|
|
2
2
|
|
|
3
3
|
from adam.commands.command import Command
|
|
4
4
|
from adam.commands.command_helpers import ClusterOrPodCommandHelper
|
|
5
|
+
from adam.commands.cql.utils_cql import cassandra
|
|
6
|
+
from adam.commands.devices.devices import Devices
|
|
5
7
|
from adam.commands.nodetool_commands import NODETOOL_COMMANDS
|
|
6
8
|
from adam.config import Config
|
|
7
|
-
from adam.k8s_utils.cassandra_clusters import CassandraClusters
|
|
8
|
-
from adam.k8s_utils.cassandra_nodes import CassandraNodes
|
|
9
9
|
from adam.repl_state import ReplState, RequiredState
|
|
10
10
|
from adam.utils import log
|
|
11
11
|
|
|
@@ -31,26 +31,17 @@ class NodeTool(Command):
|
|
|
31
31
|
if not(args := self.args(cmd)):
|
|
32
32
|
return super().run(cmd, state)
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
user, pw = state.user_pass()
|
|
39
|
-
command = f"nodetool -u {user} -pw {pw} {' '.join(args)}"
|
|
34
|
+
with self.validate(args, state) as (args, state):
|
|
35
|
+
with cassandra(state) as pods:
|
|
36
|
+
pods.nodetool(' '.join(args), status=(args[0] == 'status'))
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
return CassandraNodes.exec(state.pod, state.namespace, command, show_out=True)
|
|
43
|
-
elif state.sts:
|
|
44
|
-
return CassandraClusters.exec(state.sts, state.namespace, command, action='nodetool', show_out=True)
|
|
38
|
+
return state
|
|
45
39
|
|
|
46
40
|
def completion(self, state: ReplState):
|
|
47
|
-
|
|
48
|
-
return {NodeTool.COMMAND: {'help': None} | {c: None for c in NODETOOL_COMMANDS}}
|
|
49
|
-
|
|
50
|
-
return {}
|
|
41
|
+
return super().completion(state, {c: {'&': None} for c in NODETOOL_COMMANDS}, pods=Devices.device(state).pods(state, '-'))
|
|
51
42
|
|
|
52
43
|
def help(self, _: ReplState):
|
|
53
|
-
return f'{NodeTool.COMMAND} <sub-command
|
|
44
|
+
return f'{NodeTool.COMMAND} <sub-command> [&]\t run nodetool with arguments'
|
|
54
45
|
|
|
55
46
|
class NodeToolCommandHelper(click.Command):
|
|
56
47
|
def get_help(self, ctx: click.Context):
|
adam/commands/param_get.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
from adam.commands import validate_args
|
|
1
2
|
from adam.commands.command import Command
|
|
2
3
|
from adam.config import Config
|
|
3
4
|
from adam.repl_state import ReplState
|
|
4
|
-
from adam.utils import
|
|
5
|
+
from adam.utils import tabulize, log, log2
|
|
5
6
|
|
|
6
7
|
class GetParam(Command):
|
|
7
8
|
COMMAND = 'get'
|
|
@@ -22,21 +23,17 @@ class GetParam(Command):
|
|
|
22
23
|
if not(args := self.args(cmd)):
|
|
23
24
|
return super().run(cmd, state)
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
with self.validate(args, state) as (args, state):
|
|
27
|
+
def msg():
|
|
28
|
+
tabulize(Config().keys(), lambda key: f'{key}\t{Config().get(key, None)}', separator='\t')
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
with validate_args(args, state, msg=msg) as key:
|
|
31
|
+
if v := Config().get(key, None):
|
|
32
|
+
log(v)
|
|
33
|
+
else:
|
|
34
|
+
log2(f'{key} is not set.')
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
key = args[0]
|
|
34
|
-
if v := Config().get(key, None):
|
|
35
|
-
log(v)
|
|
36
|
-
else:
|
|
37
|
-
log2(f'{key} is not set.')
|
|
38
|
-
|
|
39
|
-
return v if v else state
|
|
36
|
+
return v if v else state
|
|
40
37
|
|
|
41
38
|
def completion(self, _: ReplState):
|
|
42
39
|
return {GetParam.COMMAND: {key: None for key in Config().keys()}}
|
adam/commands/param_set.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from adam.commands import validate_args
|
|
1
2
|
from adam.commands.command import Command
|
|
2
3
|
from adam.config import Config
|
|
3
4
|
from adam.repl_state import ReplState
|
|
@@ -22,23 +23,18 @@ class SetParam(Command):
|
|
|
22
23
|
if not(args := self.args(cmd)):
|
|
23
24
|
return super().run(cmd, state)
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
with self.validate(args, state) as (args, state):
|
|
27
|
+
with validate_args(args, state, exactly=2, msg=lambda: log2('set <key> <value>')):
|
|
28
|
+
key = args[0]
|
|
29
|
+
value = args[1]
|
|
30
|
+
Config().set(key, value)
|
|
26
31
|
|
|
27
|
-
|
|
28
|
-
log2('set <key> <value>')
|
|
32
|
+
log(Config().get(key, None))
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
key = args[0]
|
|
33
|
-
value = args[1]
|
|
34
|
-
Config().set(key, value)
|
|
35
|
-
|
|
36
|
-
log(Config().get(key, None))
|
|
37
|
-
|
|
38
|
-
return value
|
|
34
|
+
return value
|
|
39
35
|
|
|
40
36
|
def completion(self, _: ReplState):
|
|
41
|
-
return {SetParam.COMMAND: {key: None for key in Config().keys()}}
|
|
37
|
+
return {SetParam.COMMAND: {key: ({'true': None, 'false': None} if Config().get(key, None) in [True, False] else None) for key in Config().keys()}}
|
|
42
38
|
|
|
43
39
|
def help(self, _: ReplState):
|
|
44
40
|
return f"{SetParam.COMMAND} <key> <value>\t sets a Kaqing parameter to a different value"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from adam.commands.postgres.postgres_databases import PostgresDatabases
|
|
2
|
+
from adam.commands.postgres.utils_postgres import pg_table_names
|
|
3
|
+
from adam.repl_state import ReplState
|
|
4
|
+
from adam.sql.lark_completer import LarkCompleter
|
|
5
|
+
|
|
6
|
+
def completions_p(state: ReplState):
|
|
7
|
+
return {
|
|
8
|
+
'\h': None,
|
|
9
|
+
'\d': None,
|
|
10
|
+
'\dt': None,
|
|
11
|
+
'\du': None
|
|
12
|
+
} | LarkCompleter(expandables={
|
|
13
|
+
'tables': lambda x: pg_table_names(state),
|
|
14
|
+
'columns': lambda x: ['id'],
|
|
15
|
+
'hosts': lambda x: ['@' + PostgresDatabases.pod_and_container(state.namespace)[0]],
|
|
16
|
+
}, variant=ReplState.P).completions_for_nesting()
|
|
17
|
+
|
|
18
|
+
def psql0_completions(state: ReplState):
|
|
19
|
+
return {
|
|
20
|
+
'\h': None,
|
|
21
|
+
'\l': None,
|
|
22
|
+
}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import click
|
|
2
2
|
|
|
3
|
+
from adam.commands import extract_trailing_options, validate_args
|
|
3
4
|
from adam.commands.command import Command
|
|
4
|
-
from adam.commands.
|
|
5
|
+
from adam.commands.intermediate_command import IntermediateCommand
|
|
6
|
+
from adam.commands.postgres.postgres_databases import pg_path
|
|
7
|
+
from adam.commands.postgres.completions_p import psql0_completions, completions_p
|
|
8
|
+
from adam.commands.postgres.utils_postgres import pg_table_names, postgres
|
|
5
9
|
from .postgres_ls import PostgresLs
|
|
6
10
|
from .postgres_preview import PostgresPreview
|
|
7
|
-
from .postgres_session import PostgresSession
|
|
8
11
|
from adam.repl_state import ReplState
|
|
9
|
-
from adam.utils import
|
|
12
|
+
from adam.utils import log, log2, log_timing
|
|
10
13
|
|
|
11
|
-
class Postgres(
|
|
14
|
+
class Postgres(IntermediateCommand):
|
|
12
15
|
COMMAND = 'pg'
|
|
13
|
-
reaper_login = None
|
|
14
16
|
|
|
15
17
|
# the singleton pattern
|
|
16
18
|
def __new__(cls, *args, **kwargs):
|
|
@@ -28,86 +30,60 @@ class Postgres(Command):
|
|
|
28
30
|
if not(args := self.args(cmd)):
|
|
29
31
|
return super().run(cmd, state)
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
with self.validate(args, state) as (args, state):
|
|
34
|
+
with extract_trailing_options(args, '&') as (args, backgrounded):
|
|
35
|
+
with validate_args(args, state, name='SQL statement') as sql:
|
|
36
|
+
if not state.pg_path:
|
|
37
|
+
if state.in_repl:
|
|
38
|
+
log2('Enter "use <pg-name>" first.')
|
|
39
|
+
else:
|
|
40
|
+
log2('* pg-name is missing.')
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
if not args:
|
|
35
|
-
log2('Please use SQL statement. e.g. pg \l')
|
|
42
|
+
return state
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Command.display_help()
|
|
44
|
+
if state.in_repl:
|
|
45
|
+
with postgres(state) as pod:
|
|
46
|
+
pod.sql(args, backgrounded=backgrounded)
|
|
47
|
+
elif not self.run_subcommand(cmd, state):
|
|
48
|
+
with postgres(state) as pod:
|
|
49
|
+
pod.sql(args, backgrounded=backgrounded)
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
else:
|
|
47
|
-
# head with the Chain of Responsibility pattern
|
|
48
|
-
cmds = Command.chain(Postgres.cmd_list())
|
|
49
|
-
if not cmds.run(cmd, state) :
|
|
50
|
-
if not args:
|
|
51
|
-
log2('* Command or SQL statements is missing.')
|
|
52
|
-
Command.display_help()
|
|
51
|
+
return state
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
self.run_sql(state, args)
|
|
57
|
-
|
|
58
|
-
return state
|
|
59
|
-
|
|
60
|
-
def cmd_list():
|
|
61
|
-
return [PostgresLs(), PostgresPreview()]
|
|
62
|
-
|
|
63
|
-
def run_sql(self, state: ReplState, args: list[str]):
|
|
64
|
-
if not state.pg_path:
|
|
65
|
-
if state.in_repl:
|
|
66
|
-
log2('Enter "use <pg-name>" first.')
|
|
67
|
-
else:
|
|
68
|
-
log2('* pg-name is missing.')
|
|
69
|
-
|
|
70
|
-
return state
|
|
71
|
-
|
|
72
|
-
PostgresSession(state.namespace, state.pg_path).run_sql(' '.join(args))
|
|
53
|
+
def cmd_list(self):
|
|
54
|
+
return [PostgresLs(), PostgresPreview(), PostgresPg()]
|
|
73
55
|
|
|
74
56
|
def completion(self, state: ReplState):
|
|
75
|
-
|
|
76
|
-
if PostgresSession(state.namespace, state.pg_path).db:
|
|
77
|
-
leaf = {
|
|
78
|
-
'\h': None,
|
|
79
|
-
'\d': None,
|
|
80
|
-
'\dt': None,
|
|
81
|
-
'\du': None,
|
|
82
|
-
'delete': {'from': None},
|
|
83
|
-
'insert': {'into': None},
|
|
84
|
-
'select': None,
|
|
85
|
-
'update': None,
|
|
86
|
-
}
|
|
87
|
-
elif state.pg_path:
|
|
88
|
-
leaf = {
|
|
89
|
-
'\h': None,
|
|
90
|
-
'\l': None,
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if state.pg_path:
|
|
94
|
-
return super().completion(state, leaf) | leaf
|
|
95
|
-
else:
|
|
57
|
+
if state.device != state.P:
|
|
96
58
|
return {}
|
|
97
59
|
|
|
60
|
+
with pg_path(state) as (host, database):
|
|
61
|
+
if database:
|
|
62
|
+
if pg_table_names(state):
|
|
63
|
+
with log_timing('psql_completions'):
|
|
64
|
+
return completions_p(state)
|
|
65
|
+
elif host:
|
|
66
|
+
return psql0_completions(state)
|
|
67
|
+
|
|
68
|
+
return {}
|
|
69
|
+
|
|
98
70
|
def help(self, _: ReplState):
|
|
99
|
-
return f'
|
|
71
|
+
return f'<sql-statements> [&]\t run queries on Postgres databases'
|
|
100
72
|
|
|
101
73
|
class PostgresCommandHelper(click.Command):
|
|
102
74
|
def get_help(self, ctx: click.Context):
|
|
103
|
-
|
|
104
|
-
log()
|
|
105
|
-
log('Sub-Commands:')
|
|
106
|
-
|
|
107
|
-
log(lines_to_tabular([c.help(ReplState()).replace(f'{Postgres.COMMAND} ', ' ', 1) for c in Postgres.cmd_list()], separator='\t'))
|
|
108
|
-
log()
|
|
109
|
-
ClusterCommandHelper.cluster_help()
|
|
75
|
+
IntermediateCommand.intermediate_help(super().get_help(ctx), Postgres.COMMAND, Postgres().cmd_list(), show_cluster_help=True)
|
|
110
76
|
log('PG-Name: Kubernetes secret for Postgres credentials')
|
|
111
77
|
log(' e.g. stgawsscpsr-c3-c3-k8spg-cs-001')
|
|
112
78
|
log('Database: Postgres database name within a host')
|
|
113
|
-
log(' e.g. stgawsscpsr_c3_c3')
|
|
79
|
+
log(' e.g. stgawsscpsr_c3_c3')
|
|
80
|
+
|
|
81
|
+
# No action body, only for a help entry and auto-completion
|
|
82
|
+
class PostgresPg(Command):
|
|
83
|
+
COMMAND = 'pg'
|
|
84
|
+
|
|
85
|
+
def command(self):
|
|
86
|
+
return PostgresPg.COMMAND
|
|
87
|
+
|
|
88
|
+
def help(self, _: ReplState):
|
|
89
|
+
return f'pg <sql-statements>\t run queries on Postgres databases'
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import re
|
|
3
|
+
import subprocess
|
|
4
|
+
|
|
5
|
+
from adam.config import Config
|
|
6
|
+
from adam.repl_session import ReplSession
|
|
7
|
+
from adam.repl_state import ReplState
|
|
8
|
+
from adam.utils_k8s.kube_context import KubeContext
|
|
9
|
+
from adam.utils_k8s.pods import Pods
|
|
10
|
+
from adam.utils_k8s.secrets import Secrets
|
|
11
|
+
from adam.utils import log2, log_exc
|
|
12
|
+
|
|
13
|
+
class ConnectionDetails:
|
|
14
|
+
def __init__(self, state: ReplState, namespace: str, host: str):
|
|
15
|
+
self.state = state
|
|
16
|
+
self.namespace = namespace
|
|
17
|
+
self.host = host
|
|
18
|
+
|
|
19
|
+
def endpoint(self):
|
|
20
|
+
return PostgresDatabases._connection_property(self.state, 'pg.secret.endpoint-key', 'postgres-db-endpoint', host=self.host)
|
|
21
|
+
|
|
22
|
+
def port(self):
|
|
23
|
+
return PostgresDatabases._connection_property(self.state, 'pg.secret.port-key', 'postgres-db-port', host=self.host)
|
|
24
|
+
|
|
25
|
+
def username(self):
|
|
26
|
+
return PostgresDatabases._connection_property(self.state, 'pg.secret.username-key', 'postgres-admin-username', host=self.host)
|
|
27
|
+
|
|
28
|
+
def password(self):
|
|
29
|
+
return PostgresDatabases._connection_property(self.state, 'pg.secret.password-key', 'postgres-admin-password', host=self.host)
|
|
30
|
+
|
|
31
|
+
class PostgresDatabases:
|
|
32
|
+
def hosts(state: ReplState, namespace: str = None):
|
|
33
|
+
if not namespace:
|
|
34
|
+
namespace = state.namespace
|
|
35
|
+
|
|
36
|
+
return [ConnectionDetails(state, namespace, host) for host in PostgresDatabases.host_names(namespace)]
|
|
37
|
+
|
|
38
|
+
@functools.lru_cache()
|
|
39
|
+
def host_names(namespace: str):
|
|
40
|
+
ss = Secrets.list_secrets(namespace, name_pattern=Config().get('pg.name-pattern', '^{namespace}.*k8spg.*'))
|
|
41
|
+
|
|
42
|
+
def excludes(name: str):
|
|
43
|
+
exs = Config().get('pg.excludes', '.helm., -admin-secret')
|
|
44
|
+
if exs:
|
|
45
|
+
for ex in exs.split(','):
|
|
46
|
+
if ex.strip(' ') in name:
|
|
47
|
+
return True
|
|
48
|
+
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
return [s for s in ss if not excludes(s)]
|
|
52
|
+
|
|
53
|
+
def databases(state: ReplState, default_owner = False):
|
|
54
|
+
dbs = []
|
|
55
|
+
# List of databases
|
|
56
|
+
# Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
|
|
57
|
+
# ---------------------------------------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
|
|
58
|
+
# postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
|
|
59
|
+
# stgawsscpsr_c3_c3 | postgres | UTF8 | C | C | | libc |
|
|
60
|
+
# template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
|
|
61
|
+
# | | | | | | | postgres=CTc/postgres
|
|
62
|
+
# (48 rows)
|
|
63
|
+
if r := PostgresDatabases.run_sql(state, '\l', show_out=False):
|
|
64
|
+
s = 0
|
|
65
|
+
for line in r.stdout.split('\n'):
|
|
66
|
+
line: str = line.strip(' \r')
|
|
67
|
+
if s == 0:
|
|
68
|
+
if 'List of databases' in line:
|
|
69
|
+
s = 1
|
|
70
|
+
elif s == 1:
|
|
71
|
+
if 'Name' in line and 'Owner' in line and 'Encoding' in line:
|
|
72
|
+
s = 2
|
|
73
|
+
elif s == 2:
|
|
74
|
+
if line.startswith('---------'):
|
|
75
|
+
s = 3
|
|
76
|
+
elif s == 3:
|
|
77
|
+
groups = re.match(r'^\s*(\S*)\s*\|\s*(\S*)\s*\|.*', line)
|
|
78
|
+
if groups and groups[1] != '|':
|
|
79
|
+
dbs.append({'name': groups[1], 'owner': groups[2]})
|
|
80
|
+
|
|
81
|
+
if default_owner:
|
|
82
|
+
dbs = [db for db in dbs if db['owner'] == PostgresDatabases.default_owner()]
|
|
83
|
+
|
|
84
|
+
return dbs
|
|
85
|
+
|
|
86
|
+
def tables(state: ReplState, default_schema = False):
|
|
87
|
+
dbs = []
|
|
88
|
+
# List of relations
|
|
89
|
+
# Schema | Name | Type | Owner
|
|
90
|
+
# ----------+------------------------------------------------------------+-------+---------------
|
|
91
|
+
# postgres | c3_2_admin_aclpriv | table | postgres
|
|
92
|
+
# postgres | c3_2_admin_aclpriv_a | table | postgres
|
|
93
|
+
if r := PostgresDatabases.run_sql(state, '\dt', show_out=False):
|
|
94
|
+
s = 0
|
|
95
|
+
for line in r.stdout.split('\n'):
|
|
96
|
+
line: str = line.strip(' \r')
|
|
97
|
+
if s == 0:
|
|
98
|
+
if 'List of relations' in line:
|
|
99
|
+
s = 1
|
|
100
|
+
elif s == 1:
|
|
101
|
+
if 'Schema' in line and 'Name' in line and 'Type' in line:
|
|
102
|
+
s = 2
|
|
103
|
+
elif s == 2:
|
|
104
|
+
if line.startswith('---------'):
|
|
105
|
+
s = 3
|
|
106
|
+
elif s == 3:
|
|
107
|
+
groups = re.match(r'^\s*(\S*)\s*\|\s*(\S*)\s*\|.*', line)
|
|
108
|
+
if groups and groups[1] != '|':
|
|
109
|
+
dbs.append({'schema': groups[1], 'name': groups[2]})
|
|
110
|
+
|
|
111
|
+
if default_schema:
|
|
112
|
+
dbs = [db for db in dbs if db["schema"] == PostgresDatabases.default_schema()]
|
|
113
|
+
|
|
114
|
+
return dbs
|
|
115
|
+
|
|
116
|
+
def run_sql(state: ReplState, sql: str, database: str = None, show_out = True, backgrounded = False):
|
|
117
|
+
if not database:
|
|
118
|
+
database = PostgresDatabases.database(state)
|
|
119
|
+
if not database:
|
|
120
|
+
database = PostgresDatabases.default_db()
|
|
121
|
+
|
|
122
|
+
username = PostgresDatabases.username(state)
|
|
123
|
+
password = PostgresDatabases.password(state)
|
|
124
|
+
endpoint = PostgresDatabases.endpoint(state)
|
|
125
|
+
|
|
126
|
+
if KubeContext.in_cluster():
|
|
127
|
+
cmd1 = f'env PGPASSWORD={password} psql -h {endpoint} -p {PostgresDatabases.port()} -U {username} {database} --pset pager=off -c'
|
|
128
|
+
log2(f'{cmd1} "{sql}"')
|
|
129
|
+
# remove double quotes from the sql argument
|
|
130
|
+
cmd = cmd1.split(' ') + [sql]
|
|
131
|
+
|
|
132
|
+
r = subprocess.run(cmd, capture_output=not backgrounded, text=True)
|
|
133
|
+
if show_out:
|
|
134
|
+
log2(r.stdout)
|
|
135
|
+
log2(r.stderr)
|
|
136
|
+
|
|
137
|
+
return r
|
|
138
|
+
else:
|
|
139
|
+
pod_name, container_name = PostgresDatabases.pod_and_container(state.namespace)
|
|
140
|
+
if not pod_name:
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
cmd = f'psql -h {endpoint} -p {PostgresDatabases.port(state)} -U {username} {database} --pset pager=off -c "{sql}"'
|
|
144
|
+
env_prefix = f'PGPASSWORD="{password}"'
|
|
145
|
+
|
|
146
|
+
r = Pods.exec(pod_name, container_name, state.namespace, cmd, show_out=show_out, backgrounded=backgrounded, env_prefix=env_prefix)
|
|
147
|
+
if r and Config().get('repl.history.push-cat-remote-log-file', True):
|
|
148
|
+
if r.log_file and ReplSession().prompt_session:
|
|
149
|
+
ReplSession().prompt_session.history.append_string(f'@{r.pod} cat {r.log_file}')
|
|
150
|
+
|
|
151
|
+
return r
|
|
152
|
+
|
|
153
|
+
@functools.lru_cache()
|
|
154
|
+
def pod_and_container(namespace: str):
|
|
155
|
+
container_name = Config().get('pg.agent.name', 'ops-pg-agent')
|
|
156
|
+
|
|
157
|
+
if Config().get('pg.agent.just-in-time', False):
|
|
158
|
+
if not PostgresDatabases.deploy_pg_agent(container_name, namespace):
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
pod_name = container_name
|
|
162
|
+
try:
|
|
163
|
+
# try with dedicated pg agent pod name configured
|
|
164
|
+
Pods.get(namespace, container_name)
|
|
165
|
+
except:
|
|
166
|
+
try:
|
|
167
|
+
# try with the ops pod
|
|
168
|
+
container_name = Config().get('pod.name', 'ops')
|
|
169
|
+
pod_name = Pods.get_with_selector(namespace, label_selector = Config().get('pod.label-selector', 'run=ops')).metadata.name
|
|
170
|
+
except:
|
|
171
|
+
log2(f"Could not locate {container_name} pod.")
|
|
172
|
+
return None
|
|
173
|
+
|
|
174
|
+
return pod_name, container_name
|
|
175
|
+
|
|
176
|
+
def deploy_pg_agent(pod_name: str, namespace: str) -> str:
|
|
177
|
+
image = Config().get('pg.agent.image', 'seanahnsf/kaqing')
|
|
178
|
+
timeout = Config().get('pg.agent.timeout', 3600)
|
|
179
|
+
try:
|
|
180
|
+
Pods.create(namespace, pod_name, image, ['sleep', f'{timeout}'], env={'NAMESPACE': namespace}, sa_name='c3')
|
|
181
|
+
except Exception as e:
|
|
182
|
+
if e.status == 409:
|
|
183
|
+
if Pods.completed(namespace, pod_name):
|
|
184
|
+
with log_exc(lambda e2: "Exception when calling BatchV1Api->create_pod: %s\n" % e2):
|
|
185
|
+
Pods.delete(pod_name, namespace)
|
|
186
|
+
Pods.create(namespace, pod_name, image, ['sleep', f'{timeout}'], env={'NAMESPACE': namespace}, sa_name='c3')
|
|
187
|
+
|
|
188
|
+
return
|
|
189
|
+
else:
|
|
190
|
+
log2("Exception when calling BatchV1Api->create_pod: %s\n" % e)
|
|
191
|
+
|
|
192
|
+
return
|
|
193
|
+
|
|
194
|
+
Pods.wait_for_running(namespace, pod_name)
|
|
195
|
+
|
|
196
|
+
return pod_name
|
|
197
|
+
|
|
198
|
+
def undeploy_pg_agent(pod_name: str, namespace: str):
|
|
199
|
+
Pods.delete(pod_name, namespace, grace_period_seconds=0)
|
|
200
|
+
|
|
201
|
+
def endpoint(state: ReplState):
|
|
202
|
+
return PostgresDatabases._connection_property(state, 'pg.secret.endpoint-key', 'postgres-db-endpoint')
|
|
203
|
+
|
|
204
|
+
def port(state: ReplState):
|
|
205
|
+
return PostgresDatabases._connection_property(state, 'pg.secret.port-key', 'postgres-db-port')
|
|
206
|
+
|
|
207
|
+
def username(state: ReplState):
|
|
208
|
+
return PostgresDatabases._connection_property(state, 'pg.secret.username-key', 'postgres-admin-username')
|
|
209
|
+
|
|
210
|
+
def password(state: ReplState):
|
|
211
|
+
return PostgresDatabases._connection_property(state, 'pg.secret.password-key', 'postgres-admin-password')
|
|
212
|
+
|
|
213
|
+
def _connection_property(state: ReplState, config_key: str, default: str, host: str = None, database: str = None):
|
|
214
|
+
with pg_path(state, host=host, database=database) as (host, _):
|
|
215
|
+
if not (conn := PostgresDatabases.conn_details(state.namespace, host)):
|
|
216
|
+
return ''
|
|
217
|
+
|
|
218
|
+
key = Config().get(config_key, default)
|
|
219
|
+
return conn[key] if key in conn else ''
|
|
220
|
+
|
|
221
|
+
def default_db():
|
|
222
|
+
return Config().get('pg.default-db', 'postgres')
|
|
223
|
+
|
|
224
|
+
def default_owner():
|
|
225
|
+
return Config().get('pg.default-owner', 'postgres')
|
|
226
|
+
|
|
227
|
+
def default_schema():
|
|
228
|
+
return Config().get('pg.default-schema', 'postgres')
|
|
229
|
+
|
|
230
|
+
def host(state: ReplState):
|
|
231
|
+
if not state.pg_path:
|
|
232
|
+
return None
|
|
233
|
+
|
|
234
|
+
return state.pg_path.split('/')[0]
|
|
235
|
+
|
|
236
|
+
def database(state: ReplState):
|
|
237
|
+
if not state.pg_path:
|
|
238
|
+
return None
|
|
239
|
+
|
|
240
|
+
tokens = state.pg_path.split('/')
|
|
241
|
+
if len(tokens) > 1:
|
|
242
|
+
return tokens[1]
|
|
243
|
+
|
|
244
|
+
return None
|
|
245
|
+
|
|
246
|
+
@functools.lru_cache()
|
|
247
|
+
def conn_details(namespace: str, host: str):
|
|
248
|
+
return Secrets.get_data(namespace, host)
|
|
249
|
+
|
|
250
|
+
class PostgresPathHandler:
|
|
251
|
+
def __init__(self, state: ReplState, host: str = None, database: str = None):
|
|
252
|
+
self.state = state
|
|
253
|
+
self.host = host
|
|
254
|
+
self.database = database
|
|
255
|
+
|
|
256
|
+
def __enter__(self) -> tuple[str, str]:
|
|
257
|
+
if self.state and self.state.pg_path:
|
|
258
|
+
host_n_db = self.state.pg_path.split('/')
|
|
259
|
+
if not self.host:
|
|
260
|
+
self.host = host_n_db[0]
|
|
261
|
+
if not self.database and len(host_n_db) > 1:
|
|
262
|
+
self.database = host_n_db[1]
|
|
263
|
+
|
|
264
|
+
return self.host, self.database
|
|
265
|
+
|
|
266
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
267
|
+
return False
|
|
268
|
+
|
|
269
|
+
def pg_path(state: ReplState, host: str = None, database: str = None):
|
|
270
|
+
return PostgresPathHandler(state, host=host, database=database)
|
|
@@ -4,7 +4,6 @@ from adam.repl_state import ReplState, RequiredState
|
|
|
4
4
|
|
|
5
5
|
class PostgresLs(Command):
|
|
6
6
|
COMMAND = 'pg ls'
|
|
7
|
-
reaper_login = None
|
|
8
7
|
|
|
9
8
|
# the singleton pattern
|
|
10
9
|
def __new__(cls, *args, **kwargs):
|
|
@@ -25,15 +24,12 @@ class PostgresLs(Command):
|
|
|
25
24
|
if not(args := self.args(cmd)):
|
|
26
25
|
return super().run(cmd, state)
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return state
|
|
31
|
-
|
|
32
|
-
state.device = ReplState.P
|
|
27
|
+
with self.validate(args, state) as (args, state):
|
|
28
|
+
state.device = ReplState.P
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
Ls().run('ls', state)
|
|
35
31
|
|
|
36
|
-
|
|
32
|
+
return state
|
|
37
33
|
|
|
38
34
|
def completion(self, state: ReplState):
|
|
39
35
|
if state.sts:
|
|
@@ -4,7 +4,6 @@ from adam.repl_state import ReplState, RequiredState
|
|
|
4
4
|
|
|
5
5
|
class PostgresPreview(Command):
|
|
6
6
|
COMMAND = 'pg preview'
|
|
7
|
-
reaper_login = None
|
|
8
7
|
|
|
9
8
|
# the singleton pattern
|
|
10
9
|
def __new__(cls, *args, **kwargs):
|
|
@@ -19,21 +18,18 @@ class PostgresPreview(Command):
|
|
|
19
18
|
return PostgresPreview.COMMAND
|
|
20
19
|
|
|
21
20
|
def required(self):
|
|
22
|
-
return RequiredState.
|
|
21
|
+
return RequiredState.PG_DATABASE
|
|
23
22
|
|
|
24
23
|
def run(self, cmd: str, state: ReplState):
|
|
25
24
|
if not(args := self.args(cmd)):
|
|
26
25
|
return super().run(cmd, state)
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return state
|
|
31
|
-
|
|
32
|
-
state.device = ReplState.P
|
|
27
|
+
with self.validate(args, state) as (args, state):
|
|
28
|
+
state.device = ReplState.P
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
PreviewTable().run(f'preview {" ".join(args)}', state)
|
|
35
31
|
|
|
36
|
-
|
|
32
|
+
return state
|
|
37
33
|
|
|
38
34
|
def completion(self, state: ReplState):
|
|
39
35
|
if state.sts:
|