|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import typing |
| 5 | +import webbrowser |
| 6 | + |
| 7 | +import anyio |
| 8 | +import click |
| 9 | +from jumpstarter_driver_composite.client import CompositeClient |
| 10 | +from jumpstarter_driver_network.adapters.novnc import NovncAdapter |
| 11 | + |
| 12 | +from jumpstarter.client.decorators import driver_click_group |
| 13 | + |
| 14 | +if typing.TYPE_CHECKING: |
| 15 | + from jumpstarter_driver_network.client import TCPClient |
| 16 | + |
| 17 | + |
| 18 | +class VNClient(CompositeClient): |
| 19 | + """Client for interacting with a VNC server.""" |
| 20 | + |
| 21 | + @property |
| 22 | + def tcp(self) -> TCPClient: |
| 23 | + """ |
| 24 | + Access the underlying TCP client. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + TCPClient: The TCP client instance stored in this composite client's children mapping. |
| 28 | + """ |
| 29 | + return typing.cast("TCPClient", self.children["tcp"]) |
| 30 | + |
| 31 | + def stream(self, method="connect"): |
| 32 | + """Create a new stream, proxied to the underlying TCP driver.""" |
| 33 | + return self.tcp.stream(method) |
| 34 | + |
| 35 | + async def stream_async(self, method="connect"): |
| 36 | + """Create a new async stream, proxied to the underlying TCP driver.""" |
| 37 | + return await self.tcp.stream_async(method) |
| 38 | + |
| 39 | + @contextlib.contextmanager |
| 40 | + def session(self, *, encrypt: bool = True) -> typing.Iterator[str]: |
| 41 | + """ |
| 42 | + Open a noVNC session and yield the connection URL. |
| 43 | +
|
| 44 | + Parameters: |
| 45 | + encrypt (bool): If True, request an encrypted vnc connection. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + url (str): The URL to connect to the VNC session. |
| 49 | + """ |
| 50 | + with NovncAdapter(client=self.tcp, method="connect", encrypt=encrypt) as adapter: |
| 51 | + yield adapter |
| 52 | + |
| 53 | + def get_default_encrypt(self) -> bool: |
| 54 | + """Fetch the default encryption setting from the remote driver.""" |
| 55 | + return typing.cast(bool, self.call("get_default_encrypt")) |
| 56 | + |
| 57 | + def cli(self) -> click.Command: |
| 58 | + """ |
| 59 | + Provide a Click command group for running VNC sessions. |
| 60 | +
|
| 61 | + The returned command exposes a `session` subcommand that opens a VNC session, |
| 62 | + prints the connection URL, optionally opens it in the user's browser, |
| 63 | + and waits until the user cancels the session. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + click.Command: Click command group with a `session` subcommand that accepts |
| 67 | + `--browser/--no-browser` and `--encrypt/--no-encrypt` options. |
| 68 | + """ |
| 69 | + |
| 70 | + @driver_click_group(self) |
| 71 | + def vnc(): |
| 72 | + """ |
| 73 | + Open a VNC session and block until the user closes it. |
| 74 | +
|
| 75 | + When invoked, prints the connection URL for the noVNC session, optionally |
| 76 | + opens that URL in the user's web browser, and waits for user-initiated |
| 77 | + termination (for example, Ctrl+C). On exit, prints a message indicating |
| 78 | + the session is closing. |
| 79 | + """ |
| 80 | + |
| 81 | + @vnc.command() |
| 82 | + @click.option("--browser/--no-browser", default=True, help="Open the session in a web browser.") |
| 83 | + @click.option( |
| 84 | + "--encrypt", |
| 85 | + "encrypt_override", |
| 86 | + flag_value=True, |
| 87 | + default=None, |
| 88 | + help="Force an encrypted vnc connection. Overrides the driver default.", |
| 89 | + ) |
| 90 | + @click.option( |
| 91 | + "--no-encrypt", |
| 92 | + "encrypt_override", |
| 93 | + flag_value=False, |
| 94 | + help="Force an unencrypted vnc connection. Overrides the driver default.", |
| 95 | + ) |
| 96 | + def session(browser: bool, encrypt_override: bool | None): |
| 97 | + """ |
| 98 | + Open an interactive VNC session and wait for the user to terminate it. |
| 99 | +
|
| 100 | + Starts a VNC session using the client's session context, prints the connection |
| 101 | + URL, optionally opens that URL in a web browser, and blocks until the user |
| 102 | + cancels (e.g., Ctrl+C), then closes the session. |
| 103 | +
|
| 104 | + Parameters: |
| 105 | + browser (bool): If True, open the session URL in the default web browser. |
| 106 | + encrypt_override (bool | None): If provided, overrides the driver's default |
| 107 | + encryption setting. True for encrypted, |
| 108 | + False for unencrypted, None to use driver default. |
| 109 | + """ |
| 110 | + encrypt = encrypt_override if encrypt_override is not None else self.get_default_encrypt() |
| 111 | + # The NovncAdapter is a blocking context manager that runs in a thread. |
| 112 | + # We can enter it, open the browser, and then just wait for the user |
| 113 | + # to press Ctrl+C to exit. The adapter handles the background work. |
| 114 | + with self.session(encrypt=encrypt) as url: |
| 115 | + click.echo(f"To connect, please visit: {url}") |
| 116 | + if browser: |
| 117 | + webbrowser.open(url) |
| 118 | + click.echo("Press Ctrl+C to close the VNC session.") |
| 119 | + try: |
| 120 | + # Use the client's own portal to wait for cancellation. |
| 121 | + self.portal.call(anyio.sleep_forever) |
| 122 | + except (KeyboardInterrupt, anyio.get_cancelled_exc_class()): |
| 123 | + click.echo("\nClosing VNC session.") |
| 124 | + |
| 125 | + return vnc |
0 commit comments