|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | 17 | import logging |
| 18 | +from typing import List, Optional |
18 | 19 |
|
| 20 | +import typer |
| 21 | +from snowflake.cli._plugins.remote.manager import RemoteManager |
19 | 22 | from snowflake.cli.api.commands.snow_typer import SnowTyperFactory |
| 23 | +from snowflake.cli.api.console import cli_console as cc |
| 24 | +from snowflake.cli.api.output.types import ( |
| 25 | + CommandResult, |
| 26 | + QueryResult, |
| 27 | + SingleQueryResult, |
| 28 | +) |
20 | 29 |
|
21 | | -log = logging.getLogger(__name__) |
22 | 30 | app = SnowTyperFactory( |
23 | 31 | name="remote", |
24 | | - help="Manages remote development environments.", |
| 32 | + help="Manages remote development environments on top of Snowpark Container Service.", |
| 33 | + short_help="Manages remote development environments.", |
| 34 | +) |
| 35 | + |
| 36 | +log = logging.getLogger(__name__) |
| 37 | + |
| 38 | +# Define argument for remote service names (accepts both customer names and full service names) |
| 39 | +RemoteNameArgument = typer.Argument( |
| 40 | + help="Remote service name. Can be either a customer name (e.g., 'myproject') or full service name (e.g., 'SNOW_REMOTE_admin_myproject')", |
| 41 | + show_default=False, |
25 | 42 | ) |
26 | 43 |
|
27 | 44 |
|
28 | 45 | @app.command("start", requires_connection=True) |
29 | | -def start_service(**options) -> None: |
| 46 | +def start( |
| 47 | + name: Optional[str] = typer.Argument( |
| 48 | + None, |
| 49 | + help="Service name to resume, or leave empty to create a new service with auto-generated name", |
| 50 | + ), |
| 51 | + compute_pool: Optional[str] = typer.Option( |
| 52 | + None, |
| 53 | + "--compute-pool", |
| 54 | + help="Name of the compute pool to use (required for new service creation)", |
| 55 | + show_default=False, |
| 56 | + ), |
| 57 | + eai_name: Optional[List[str]] = typer.Option( |
| 58 | + None, |
| 59 | + "--eai-name", |
| 60 | + help="List of external access integration names to enable network access to external resources", |
| 61 | + ), |
| 62 | + stage: Optional[str] = typer.Option( |
| 63 | + None, |
| 64 | + "--stage", |
| 65 | + help="Internal Snowflake stage to mount (e.g., @my_stage or @my_stage/folder).", |
| 66 | + ), |
| 67 | + image: Optional[str] = typer.Option( |
| 68 | + None, |
| 69 | + "--image", |
| 70 | + help="Custom image to use (can be full path like 'repo/image:tag' or just tag like '1.7.1')", |
| 71 | + ), |
| 72 | + **options, |
| 73 | +) -> None: |
30 | 74 | """ |
31 | | - Start a remote development environment. |
| 75 | + Starts a remote development environment. |
| 76 | +
|
| 77 | + This command creates a new VS Code Server remote development environment if it doesn't exist, |
| 78 | + or starts an existing one if it's suspended. If the environment is already running, it's a no-op. |
| 79 | + The environment is deployed as a Snowpark Container Service that provides |
| 80 | + a web-based development environment. |
| 81 | +
|
| 82 | + Usage examples: |
| 83 | + - Resume existing service: snow remote start myproject |
| 84 | + - Create new service: snow remote start --compute-pool my_pool |
| 85 | + - Create named service: snow remote start myproject --compute-pool my_pool |
32 | 86 |
|
33 | | - This is a placeholder command for the remote plugin. |
34 | | - Full functionality will be implemented in subsequent PRs. |
| 87 | + The --compute-pool parameter is only required when creating a new service. For resuming |
| 88 | + existing services, the compute pool is not needed. |
35 | 89 | """ |
36 | | - log.info("Start command called - functionality coming soon!") |
37 | | - log.info("Full functionality will be available in upcoming releases.") |
| 90 | + try: |
| 91 | + manager = RemoteManager() |
| 92 | + |
| 93 | + service_name, url, status = manager.start( |
| 94 | + name=name, |
| 95 | + compute_pool=compute_pool, |
| 96 | + external_access=eai_name, |
| 97 | + stage=stage, |
| 98 | + image=image, |
| 99 | + ) |
| 100 | + |
| 101 | + # Display appropriate success message based on what happened |
| 102 | + if status == "created": |
| 103 | + cc.message( |
| 104 | + f"✓ Remote Development Environment {service_name} created successfully!" |
| 105 | + ) |
| 106 | + elif status == "resumed": |
| 107 | + cc.message( |
| 108 | + f"✓ Remote Development Environment {service_name} resumed successfully!" |
| 109 | + ) |
| 110 | + elif status == "running": |
| 111 | + cc.message( |
| 112 | + f"✓ Remote Development Environment {service_name} is already running." |
| 113 | + ) |
| 114 | + |
| 115 | + cc.message(f"VS Code Server URL: {url}") |
| 116 | + |
| 117 | + # Log detailed information at debug level |
| 118 | + if stage: |
| 119 | + log.debug("Stage '%s' mounted:", stage) |
| 120 | + log.debug( |
| 121 | + " - Workspace: '%s/user-default' → '%s'", |
| 122 | + stage, |
| 123 | + "/home/user/workspace", |
| 124 | + ) |
| 125 | + log.debug( |
| 126 | + " - VS Code data: '%s/.vscode-server/data' → '%s'", |
| 127 | + stage, |
| 128 | + "/home/user/.vscode-server", |
| 129 | + ) |
| 130 | + if eai_name: |
| 131 | + log.debug("External access integrations: %s", ", ".join(eai_name)) |
| 132 | + if image: |
| 133 | + log.debug("Using custom image: %s", image) |
| 134 | + |
| 135 | + except ValueError as e: |
| 136 | + cc.warning(f"Error: {e}") |
| 137 | + raise typer.Exit(code=1) |
| 138 | + except Exception as e: |
| 139 | + cc.warning(f"Error starting remote environment: {e}") |
| 140 | + raise typer.Exit(code=1) |
38 | 141 |
|
39 | 142 |
|
40 | 143 | @app.command("list", requires_connection=True) |
41 | | -def list_services(**options) -> None: |
| 144 | +def list_services(**options) -> CommandResult: |
| 145 | + """ |
| 146 | + Lists all remote development environments. |
| 147 | + """ |
| 148 | + cursor = RemoteManager().list_services() |
| 149 | + return QueryResult(cursor) |
| 150 | + |
| 151 | + |
| 152 | +@app.command("stop", requires_connection=True) |
| 153 | +def stop( |
| 154 | + name: str = RemoteNameArgument, |
| 155 | + **options, |
| 156 | +) -> CommandResult: |
| 157 | + """ |
| 158 | + Suspends a remote development environment. |
42 | 159 | """ |
43 | | - List remote development environments. |
| 160 | + manager = RemoteManager() |
| 161 | + cursor = manager.stop(name) |
| 162 | + cc.message(f"Remote environment '{name}' suspended successfully.") |
| 163 | + return SingleQueryResult(cursor) |
44 | 164 |
|
45 | | - This is a placeholder command for the remote plugin. |
46 | | - Full functionality will be implemented in subsequent PRs. |
| 165 | + |
| 166 | +@app.command("delete", requires_connection=True) |
| 167 | +def delete( |
| 168 | + name: str = RemoteNameArgument, |
| 169 | + **options, |
| 170 | +) -> CommandResult: |
| 171 | + """ |
| 172 | + Deletes a remote development environment. |
47 | 173 | """ |
48 | | - log.info("Remote plugin registered successfully") |
49 | | - log.info("Remote development environments plugin is registered.") |
50 | | - log.info("Full functionality will be available in upcoming releases.") |
| 174 | + manager = RemoteManager() |
| 175 | + cursor = manager.delete(name) |
| 176 | + cc.message(f"Remote environment '{name}' deleted successfully.") |
| 177 | + return SingleQueryResult(cursor) |
0 commit comments