xpk 0.17.2__py3-none-any.whl → 0.17.3__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.
xpk/commands/shell.py DELETED
@@ -1,142 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- """
13
-
14
- from ..core.commands import run_command_with_full_controls, run_command_for_value, run_command_with_updates
15
- from ..core.cluster import get_cluster_credentials, add_zone_and_project, setup_k8s_service_accounts
16
- from ..utils.console import xpk_exit, xpk_print
17
- from ..utils.validation import validate_dependencies_list, SystemDependency, should_validate_dependencies
18
- from argparse import Namespace
19
-
20
- from ..core.kjob import (
21
- AppProfileDefaults,
22
- prepare_kjob,
23
- get_pod_template_interactive_command,
24
- get_storage_annotations,
25
- )
26
-
27
- exit_instructions = 'To exit the shell input "exit".'
28
-
29
-
30
- def shell(args: Namespace):
31
- """Enter interactive shell.
32
- Args:
33
- args: user provided arguments for running the command.
34
- Returns:
35
- 0 if successful and 1 otherwise.
36
- """
37
- if should_validate_dependencies(args):
38
- validate_dependencies_list([
39
- SystemDependency.KUBECTL,
40
- SystemDependency.KJOB,
41
- SystemDependency.GCLOUD,
42
- ])
43
- exisitng_shell_pod_name = get_existing_shell_pod_name(args)
44
-
45
- if exisitng_shell_pod_name is None:
46
- return_code = connect_to_new_interactive_shell(args)
47
- else:
48
- return_code = connect_to_existing_interactive_shell(exisitng_shell_pod_name)
49
-
50
- if return_code != 0:
51
- xpk_print(f'The command failed with code {return_code}.')
52
- xpk_exit(return_code)
53
-
54
- xpk_exit(0)
55
-
56
-
57
- def get_existing_shell_pod_name(args: Namespace) -> str | None:
58
- if not args.kind_cluster:
59
- add_zone_and_project(args)
60
- get_cluster_credentials(args)
61
-
62
- return_code, shell_name = run_command_for_value(
63
- command=(
64
- 'kubectl get pods --no-headers --field-selector status.phase=Running'
65
- ' -o custom-columns=":metadata.name"'
66
- ),
67
- task='Get existing interactive shell pod name.',
68
- )
69
- if return_code != 0:
70
- xpk_print(
71
- f'Encounter an error with a code {return_code} when checking for'
72
- ' existing running shell.'
73
- )
74
- xpk_exit(return_code)
75
-
76
- pod_names = shell_name.strip().split('\n')
77
- kjob_pod_names = [
78
- name for name in pod_names if AppProfileDefaults.NAME.value in name
79
- ]
80
- shell_pod_names = [name for name in kjob_pod_names if 'interactive' in name]
81
-
82
- return shell_pod_names[0] if shell_pod_names else None
83
-
84
-
85
- def connect_to_new_interactive_shell(args: Namespace) -> int:
86
- err_code = prepare_kjob(args)
87
- if err_code > 0:
88
- xpk_exit(err_code)
89
- setup_k8s_service_accounts()
90
-
91
- cmd = (
92
- 'kubectl-kjob create interactive --profile'
93
- f' {AppProfileDefaults.NAME.value} --pod-running-timeout 180s'
94
- )
95
-
96
- for annotation in get_storage_annotations(args):
97
- cmd += f' --pod-template-annotation {annotation}'
98
-
99
- return run_command_with_full_controls(
100
- command=cmd,
101
- task='Creating new interactive shell and entering it',
102
- instructions=exit_instructions,
103
- )
104
-
105
-
106
- def connect_to_existing_interactive_shell(pod_name: str) -> int:
107
- return run_command_with_full_controls(
108
- command=(
109
- f'kubectl exec --stdin --tty {pod_name} --'
110
- f' {get_pod_template_interactive_command()}'
111
- ),
112
- task='Entering existing interactive shell',
113
- instructions=exit_instructions,
114
- )
115
-
116
-
117
- def shell_stop(args: Namespace):
118
- """Stop the running interactive shell by deleting the pod.
119
- Args:
120
- args: user provided arguments for running the command.
121
- Returns:
122
- 0 if successful and 1 otherwise.
123
- """
124
- if should_validate_dependencies(args):
125
- validate_dependencies_list(
126
- [SystemDependency.KUBECTL, SystemDependency.GCLOUD]
127
- )
128
- exisitng_shell_pod_name = get_existing_shell_pod_name(args)
129
-
130
- if exisitng_shell_pod_name is None:
131
- xpk_print('There is no shell running to stop')
132
- xpk_exit(0)
133
-
134
- return_code = run_command_with_updates(
135
- command=f'kubectl delete pod {exisitng_shell_pod_name}',
136
- task='Deleting the existing shell.',
137
- )
138
- if return_code != 0:
139
- xpk_exit(return_code)
140
-
141
- xpk_print('The shell was deleted successfully.')
142
- xpk_exit(0)
xpk/parser/batch.py DELETED
@@ -1,43 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from .common import (
18
- add_shared_arguments,
19
- add_slurm_arguments,
20
- add_cluster_arguments,
21
- add_kind_cluster_arguments,
22
- )
23
- from ..commands.batch import batch
24
-
25
-
26
- def set_batch_parser(batch_parser):
27
- batch_required_arguments = batch_parser.add_argument_group(
28
- 'batch Built-in Arguments', 'Arguments required for `batch`.'
29
- )
30
- batch_optional_arguments = batch_parser.add_argument_group(
31
- 'Optional Arguments', 'Arguments optional for `batch`.'
32
- )
33
-
34
- ### "batch" Required arguments
35
- batch_required_arguments.add_argument(
36
- 'script', help='script with batch task to run'
37
- )
38
-
39
- add_cluster_arguments(batch_optional_arguments)
40
- add_kind_cluster_arguments(batch_optional_arguments)
41
- add_shared_arguments(batch_optional_arguments)
42
- add_slurm_arguments(batch_optional_arguments)
43
- batch_parser.set_defaults(func=batch)
xpk/parser/job.py DELETED
@@ -1,147 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- import argparse
18
- from ..commands.job import job_info, job_list, job_cancel
19
-
20
- from .common import add_shared_arguments
21
- from .validators import name_type
22
-
23
-
24
- def set_job_parser(job_parser: argparse.ArgumentParser):
25
- job_subcommands = job_parser.add_subparsers(
26
- title='job subcommands',
27
- dest='xpk_job_subcommands',
28
- help=(
29
- 'These are commands related to job management. Look at help for'
30
- ' specific subcommands for more details.'
31
- ),
32
- )
33
- set_job_info_parser(
34
- job_info_parser=job_subcommands.add_parser(
35
- 'info', help='Show information about specified job.'
36
- )
37
- )
38
- set_job_list_parser(
39
- job_list_parser=job_subcommands.add_parser('ls', help='List jobs.')
40
- )
41
- set_job_cancel_parser(
42
- job_cancel_parser=job_subcommands.add_parser(
43
- 'cancel', help='Cancel job execution.'
44
- )
45
- )
46
-
47
-
48
- def set_job_info_parser(job_info_parser: argparse.ArgumentParser):
49
- job_info_required_arguments = job_info_parser.add_argument_group(
50
- 'Required arguments',
51
- 'The basic information required to identify the job.',
52
- )
53
- job_info_optional_arguments = job_info_parser.add_argument_group(
54
- 'Optional Arguments', 'Arguments optional for job info.'
55
- )
56
-
57
- ### Required arguments
58
- job_info_required_arguments.add_argument(
59
- '--cluster',
60
- type=name_type,
61
- default=None,
62
- help='The name of the cluster to info jobs on.',
63
- required=True,
64
- )
65
-
66
- job_info_optional_arguments.add_argument(
67
- '--kind-cluster',
68
- type=bool,
69
- action=argparse.BooleanOptionalAction,
70
- default=False,
71
- help='Apply command to a local test cluster.',
72
- )
73
- job_info_required_arguments.add_argument(
74
- 'name',
75
- type=str,
76
- default=None,
77
- help='Name of the job.',
78
- )
79
- job_info_parser.set_defaults(func=job_info)
80
- add_shared_arguments(job_info_parser)
81
-
82
-
83
- def set_job_list_parser(job_list_parser: argparse.ArgumentParser):
84
- job_list_required_arguments = job_list_parser.add_argument_group(
85
- 'Required Arguments',
86
- 'Arguments required for job list.',
87
- )
88
- job_list_optional_arguments = job_list_parser.add_argument_group(
89
- 'Optional Arguments', 'Arguments optional for job list.'
90
- )
91
-
92
- ### Required arguments
93
- job_list_required_arguments.add_argument(
94
- '--cluster',
95
- type=name_type,
96
- default=None,
97
- help='The name of the cluster to list jobs on.',
98
- required=True,
99
- )
100
-
101
- job_list_optional_arguments.add_argument(
102
- '--kind-cluster',
103
- type=bool,
104
- action=argparse.BooleanOptionalAction,
105
- default=False,
106
- help='Apply command to a local test cluster.',
107
- )
108
-
109
- job_list_parser.set_defaults(func=job_list)
110
- add_shared_arguments(job_list_optional_arguments)
111
-
112
-
113
- def set_job_cancel_parser(job_cancel_parser: argparse.ArgumentParser):
114
- job_cancel_required_arguments = job_cancel_parser.add_argument_group(
115
- 'Required Arguments',
116
- 'Arguments required for job cancel.',
117
- )
118
- job_cancel_optional_arguments = job_cancel_parser.add_argument_group(
119
- 'Optional Arguments', 'Arguments optional for job cancel.'
120
- )
121
-
122
- job_cancel_required_arguments.add_argument(
123
- 'name',
124
- type=str,
125
- default=None,
126
- help='The name of the job to be cancelled.',
127
- nargs='+',
128
- )
129
-
130
- job_cancel_required_arguments.add_argument(
131
- '--cluster',
132
- type=name_type,
133
- default=None,
134
- help='The name of the cluster to delete the job on.',
135
- required=True,
136
- )
137
-
138
- job_cancel_optional_arguments.add_argument(
139
- '--kind-cluster',
140
- type=bool,
141
- action=argparse.BooleanOptionalAction,
142
- default=False,
143
- help='Apply command to a local test cluster.',
144
- )
145
-
146
- job_cancel_parser.set_defaults(func=job_cancel)
147
- add_shared_arguments(job_cancel_optional_arguments)
xpk/parser/run.py DELETED
@@ -1,47 +0,0 @@
1
- """
2
- Copyright 2025 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from ..commands.run import run
18
- from .common import (
19
- add_shared_arguments,
20
- add_slurm_arguments,
21
- add_cluster_arguments,
22
- add_kind_cluster_arguments,
23
- )
24
-
25
-
26
- def set_run_parser(run_parser):
27
- run_required_arguments = run_parser.add_argument_group(
28
- 'Required Arguments', 'Arguments required for `run`.'
29
- )
30
- run_optional_arguments = run_parser.add_argument_group(
31
- 'Optional Arguments', 'Arguments optional for `run`.'
32
- )
33
-
34
- run_required_arguments.add_argument('script', help='script with task to run')
35
- run_optional_arguments.add_argument(
36
- '--timeout',
37
- type=int,
38
- default=None,
39
- help='Amount of time to wait for job in seconds',
40
- required=False,
41
- )
42
-
43
- add_cluster_arguments(run_optional_arguments)
44
- add_kind_cluster_arguments(run_optional_arguments)
45
- add_slurm_arguments(run_optional_arguments)
46
- add_shared_arguments(run_parser)
47
- run_parser.set_defaults(func=run)
xpk/parser/shell.py DELETED
@@ -1,59 +0,0 @@
1
- """
2
- Copyright 2024 Google LLC
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- https://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from ..commands.shell import shell, shell_stop
18
- from .common import (
19
- add_shared_arguments,
20
- add_cluster_arguments,
21
- add_kind_cluster_arguments,
22
- )
23
- import argparse
24
-
25
-
26
- def set_shell_parser(shell_parser: argparse.ArgumentParser) -> None:
27
- shell_optional_arguments = shell_parser.add_argument_group(
28
- 'Optional Arguments', 'Arguments optional for shell.'
29
- )
30
- add_shared_arguments(shell_optional_arguments)
31
- shell_parser.set_defaults(func=shell)
32
-
33
- add_cluster_arguments(shell_optional_arguments)
34
- add_kind_cluster_arguments(shell_optional_arguments)
35
-
36
- shell_subcommands = shell_parser.add_subparsers(
37
- title='shell subcommands',
38
- dest='xpk_shell_subcommands',
39
- help=(
40
- 'These are commands related to interactive shell. Look at help for'
41
- ' specific subcommands for more details.'
42
- ),
43
- )
44
-
45
- set_shell_stop_parser(
46
- shell_stop_parser=shell_subcommands.add_parser(
47
- name='stop', help='Stop the running shell.'
48
- )
49
- )
50
-
51
-
52
- def set_shell_stop_parser(shell_stop_parser: argparse.ArgumentParser):
53
- shell_stop_optional_arguments = shell_stop_parser.add_argument_group(
54
- 'Optional Arguments', 'Arguments optional for shell stop.'
55
- )
56
- add_shared_arguments(shell_stop_optional_arguments)
57
- shell_stop_parser.set_defaults(func=shell_stop)
58
- add_cluster_arguments(shell_stop_parser)
59
- add_kind_cluster_arguments(shell_stop_optional_arguments)
File without changes