-
-
Notifications
You must be signed in to change notification settings - Fork 226
[UX]: Add dstack project CLI to configure, list and switching between projects
#2653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import argparse | ||
|
|
||
| from requests import HTTPError | ||
| from rich.table import Table | ||
|
|
||
| import dstack.api.server | ||
| from dstack._internal.cli.commands import BaseCommand | ||
| from dstack._internal.cli.utils.common import confirm_ask, console | ||
| from dstack._internal.core.errors import ClientError, CLIError | ||
| from dstack._internal.core.services.configs import ConfigManager | ||
| from dstack._internal.utils.logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class ProjectCommand(BaseCommand): | ||
| NAME = "project" | ||
| DESCRIPTION = "Manage projects" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd clarify that |
||
|
|
||
| def _register(self): | ||
| super()._register() | ||
| subparsers = self._parser.add_subparsers(dest="subcommand", help="Command to execute") | ||
|
|
||
| # Add subcommand | ||
| add_parser = subparsers.add_parser("add", help="Add or update a project") | ||
| add_parser.add_argument( | ||
| "--name", type=str, help="The name of the project to configure", required=True | ||
| ) | ||
| add_parser.add_argument("--url", type=str, help="Server url", required=True) | ||
| add_parser.add_argument("--token", type=str, help="User token", required=True) | ||
| add_parser.add_argument( | ||
| "-y", | ||
| "--yes", | ||
| help="Don't ask for confirmation (e.g. update the config)", | ||
| action="store_true", | ||
| ) | ||
| add_parser.add_argument( | ||
| "-n", | ||
| "--no", | ||
| help="Don't ask for confirmation (e.g. do not update the config)", | ||
| action="store_true", | ||
| ) | ||
| add_parser.set_defaults(subfunc=self._add) | ||
|
|
||
| # Delete subcommand | ||
| delete_parser = subparsers.add_parser("delete", help="Delete a project") | ||
| delete_parser.add_argument( | ||
| "--name", type=str, help="The name of the project to delete", required=True | ||
| ) | ||
| delete_parser.add_argument( | ||
| "-y", | ||
| "--yes", | ||
| help="Don't ask for confirmation", | ||
| action="store_true", | ||
| ) | ||
| delete_parser.set_defaults(subfunc=self._delete) | ||
|
|
||
| # List subcommand | ||
| list_parser = subparsers.add_parser("list", help="List configured projects") | ||
| list_parser.set_defaults(subfunc=self._list) | ||
|
|
||
| # Set default subcommand | ||
| set_default_parser = subparsers.add_parser("set-default", help="Set default project") | ||
| set_default_parser.add_argument( | ||
| "name", type=str, help="The name of the project to set as default" | ||
| ) | ||
| set_default_parser.set_defaults(subfunc=self._set_default) | ||
|
|
||
| def _command(self, args: argparse.Namespace): | ||
| if not hasattr(args, "subfunc"): | ||
| args.subfunc = self._list | ||
| args.subfunc(args) | ||
|
|
||
| def _add(self, args: argparse.Namespace): | ||
| config_manager = ConfigManager() | ||
| api_client = dstack.api.server.APIClient(base_url=args.url, token=args.token) | ||
| try: | ||
| api_client.projects.get(args.name) | ||
| except HTTPError as e: | ||
| if e.response.status_code == 403: | ||
| raise CLIError("Forbidden. Ensure the token is valid.") | ||
| elif e.response.status_code == 404: | ||
| raise CLIError(f"Project '{args.name}' not found.") | ||
| else: | ||
| raise e | ||
| default_project = config_manager.get_project_config() | ||
| if ( | ||
| default_project is None | ||
| or default_project.name != args.name | ||
| or default_project.url != args.url | ||
| or default_project.token != args.token | ||
| ): | ||
| set_it_as_default = ( | ||
| ( | ||
| args.yes | ||
| or not default_project | ||
| or confirm_ask(f"Set '{args.name}' as your default project?") | ||
| ) | ||
| if not args.no | ||
| else False | ||
| ) | ||
| config_manager.configure_project( | ||
| name=args.name, url=args.url, token=args.token, default=set_it_as_default | ||
| ) | ||
| config_manager.save() | ||
| logger.info( | ||
| f"Configuration updated at {config_manager.config_filepath}", {"show_path": False} | ||
| ) | ||
|
|
||
| def _delete(self, args: argparse.Namespace): | ||
| config_manager = ConfigManager() | ||
| if args.yes or confirm_ask(f"Are you sure you want to delete project '{args.name}'?"): | ||
| config_manager.delete_project(args.name) | ||
| config_manager.save() | ||
| console.print("[grey58]OK[/]") | ||
|
|
||
| def _list(self, args: argparse.Namespace): | ||
| config_manager = ConfigManager() | ||
| default_project = config_manager.get_project_config() | ||
|
|
||
| table = Table(box=None) | ||
| table.add_column("PROJECT", style="bold", no_wrap=True) | ||
| table.add_column("URL", style="grey58") | ||
| table.add_column("USER", style="grey58") | ||
| table.add_column("DEFAULT", justify="center") | ||
|
|
||
| for project_name in config_manager.list_projects(): | ||
| project_config = config_manager.get_project_config(project_name) | ||
| is_default = project_name == default_project.name if default_project else False | ||
|
|
||
| # Get username from API | ||
| try: | ||
| api_client = dstack.api.server.APIClient( | ||
| base_url=project_config.url, token=project_config.token | ||
| ) | ||
| user_info = api_client.users.get_my_user() | ||
| username = user_info.username | ||
| except ClientError: | ||
| username = "(invalid token)" | ||
|
|
||
| table.add_row( | ||
| project_name, | ||
| project_config.url, | ||
| username, | ||
| "✓" if is_default else "", | ||
| style="bold" if is_default else None, | ||
| ) | ||
|
|
||
| console.print(table) | ||
|
|
||
| def _set_default(self, args: argparse.Namespace): | ||
| config_manager = ConfigManager() | ||
| project_config = config_manager.get_project_config(args.name) | ||
| if project_config is None: | ||
| raise CLIError(f"Project '{args.name}' not found") | ||
|
|
||
| config_manager.configure_project( | ||
| name=args.name, url=project_config.url, token=project_config.token, default=True | ||
| ) | ||
| config_manager.save() | ||
| console.print("[grey58]OK[/]") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why leaving documentation for
dstack configwith an example fordstack project.