augint-shell 0.124.2__tar.gz → 0.125.0__tar.gz
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.
- {augint_shell-0.124.2 → augint_shell-0.125.0}/PKG-INFO +27 -1
- {augint_shell-0.124.2 → augint_shell-0.125.0}/README.md +26 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/pyproject.toml +1 -1
- {augint_shell-0.124.2 → augint_shell-0.125.0}/pyproject.toml.orig +1 -1
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/__init__.py +1 -1
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/__main__.py +2 -0
- augint_shell-0.125.0/src/ai_shell/cli/commands/ec2.py +396 -0
- augint_shell-0.125.0/src/ai_shell/ec2.py +752 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/remote.py +51 -23
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/acp.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/assets/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/assets/comfyui/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/assets/comfyui/provision.sh +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cache.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cdp_relay.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/claude_login.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/acp.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/llm.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/manage.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/tools.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/cli/commands/vpn.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/config.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/container.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/defaults.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/exceptions.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/expo.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/gpu.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/local_chrome.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/models.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/opencode.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/qr.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/scaffold.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/standalone.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/t3.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/ai-shell.yaml +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/augint-env.example +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/container/__init__.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/container/context.md +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/n8n/workflows/chat_with_ollama.json +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/n8n/workflows/text_to_speech.json +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/templates/n8n/workflows/transcribe_audio.json +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/typeahead.py +0 -0
- {augint_shell-0.124.2 → augint_shell-0.125.0}/src/ai_shell/vpn.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: augint-shell
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.125.0
|
|
4
4
|
Summary: Launch AI coding tools and local LLMs in Docker containers
|
|
5
5
|
Author: svange
|
|
6
6
|
Requires-Dist: docker>=7.0.0
|
|
@@ -161,6 +161,7 @@ If you need to work manually, [AGENTS.md](AGENTS.md) has the build, test and com
|
|
|
161
161
|
| `ai-shell opencode serve` | Headless server + attach all git repos as terminals |
|
|
162
162
|
| `ai-shell opencode status` | Show server URL, mDNS name, attached terminals |
|
|
163
163
|
| `ai-shell opencode remote [PROJECT]` | [OpenCode on a remote box](#6-opencode-on-a-remote-box-ssm) over AWS Systems Manager |
|
|
164
|
+
| `ai-shell ec2 ls\|connect\|stop` | [EC2 instances across your AWS profiles](#ec2-instances-ai-shell-ec2): list, start and connect (tunnel, `--ssh`, `--rdp`, `--shell`), stop |
|
|
164
165
|
| `ai-shell shell [bash\|zsh]` | Interactive shell in dev container (default: `bash`) |
|
|
165
166
|
| `ai-shell claude --rc [NAME]` | [Remote Control](#remote-control-rc) server: drive the project from claude.ai/code or the Claude app |
|
|
166
167
|
| `ai-shell claude --account NAME --rc` | Remote Control signed in as another Claude account (login profile) |
|
|
@@ -805,6 +806,31 @@ The command:
|
|
|
805
806
|
|
|
806
807
|
Ctrl+C ends the tunnel. `--instance` overrides `opencode.remote.instance` for one run.
|
|
807
808
|
|
|
809
|
+
## EC2 instances (`ai-shell ec2`)
|
|
810
|
+
|
|
811
|
+
`ai-shell ec2` finds an instance in any of your AWS profiles, starts it, connects to it and stops it. It uses the AWS CLI v2 and the profiles you already have; it has no config of its own.
|
|
812
|
+
|
|
813
|
+
```bash
|
|
814
|
+
ai-shell ec2 ls # every profile in ~/.aws/config
|
|
815
|
+
ai-shell ec2 ls --profile client-a # one profile (repeatable); signs in when needed
|
|
816
|
+
ai-shell ec2 connect # pick from a numbered list; a tunnel to 22 (3389 on Windows)
|
|
817
|
+
ai-shell ec2 connect web --ssh # by Name tag or instance id
|
|
818
|
+
ai-shell ec2 connect i-0abc... --rdp # opens mstsc, the macOS RDP app, xfreerdp or Remmina
|
|
819
|
+
ai-shell ec2 connect web --shell # an SSM shell, no key needed; else --ssh
|
|
820
|
+
ai-shell ec2 connect web --port 8080 # a tunnel to any port
|
|
821
|
+
ai-shell ec2 stop web
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
- **Listing.** All profiles are read at the same time. A profile whose SSO session has ended shows as `signed out` with the `aws sso login` command to run; no browser opens. `--region` sets the region for every profile, `--all-regions` reads every enabled region. A profile that asks for input (an MFA code) fails at once instead of waiting.
|
|
825
|
+
- **Starting.** `connect` starts a stopped instance and waits until it runs. When it started the instance, it asks at the end whether to stop it again (`--stop` and `--no-stop` answer before).
|
|
826
|
+
- **Getting in.** `connect` tries these one at a time, each only when it can work, and names the reason for each one that failed:
|
|
827
|
+
1. SSM port forwarding: the instance is registered with SSM and this computer has the [Session Manager plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html).
|
|
828
|
+
2. An EC2 Instance Connect Endpoint in the instance's VPC (ports 22 and 3389 only; AWS CLI 2.12 or later).
|
|
829
|
+
3. The instance's public IP, then its private IP (for a VPN or a computer in the VPC).
|
|
830
|
+
- **Signing in with SSH.** The user comes from the image name (`ubuntu`, `admin` on Debian, else `ec2-user`; `--user`). The key is `--key`, else the instance's key pair file in `~/.ssh` (`<name>` or `<name>.pem`), else a temporary key sent with EC2 Instance Connect (valid for 60 seconds), else your SSH agent.
|
|
831
|
+
- **RDP.** With the key pair file, `--rdp` also prints the Windows Administrator password.
|
|
832
|
+
- **No changes to the account.** `ai-shell ec2` never adds a security group rule, changes IAM or installs an agent to get in. It writes only start, stop and the temporary EC2 Instance Connect key.
|
|
833
|
+
|
|
808
834
|
## Requirements
|
|
809
835
|
|
|
810
836
|
- Docker, or Podman with the `docker` CLI shim
|
|
@@ -147,6 +147,7 @@ If you need to work manually, [AGENTS.md](AGENTS.md) has the build, test and com
|
|
|
147
147
|
| `ai-shell opencode serve` | Headless server + attach all git repos as terminals |
|
|
148
148
|
| `ai-shell opencode status` | Show server URL, mDNS name, attached terminals |
|
|
149
149
|
| `ai-shell opencode remote [PROJECT]` | [OpenCode on a remote box](#6-opencode-on-a-remote-box-ssm) over AWS Systems Manager |
|
|
150
|
+
| `ai-shell ec2 ls\|connect\|stop` | [EC2 instances across your AWS profiles](#ec2-instances-ai-shell-ec2): list, start and connect (tunnel, `--ssh`, `--rdp`, `--shell`), stop |
|
|
150
151
|
| `ai-shell shell [bash\|zsh]` | Interactive shell in dev container (default: `bash`) |
|
|
151
152
|
| `ai-shell claude --rc [NAME]` | [Remote Control](#remote-control-rc) server: drive the project from claude.ai/code or the Claude app |
|
|
152
153
|
| `ai-shell claude --account NAME --rc` | Remote Control signed in as another Claude account (login profile) |
|
|
@@ -791,6 +792,31 @@ The command:
|
|
|
791
792
|
|
|
792
793
|
Ctrl+C ends the tunnel. `--instance` overrides `opencode.remote.instance` for one run.
|
|
793
794
|
|
|
795
|
+
## EC2 instances (`ai-shell ec2`)
|
|
796
|
+
|
|
797
|
+
`ai-shell ec2` finds an instance in any of your AWS profiles, starts it, connects to it and stops it. It uses the AWS CLI v2 and the profiles you already have; it has no config of its own.
|
|
798
|
+
|
|
799
|
+
```bash
|
|
800
|
+
ai-shell ec2 ls # every profile in ~/.aws/config
|
|
801
|
+
ai-shell ec2 ls --profile client-a # one profile (repeatable); signs in when needed
|
|
802
|
+
ai-shell ec2 connect # pick from a numbered list; a tunnel to 22 (3389 on Windows)
|
|
803
|
+
ai-shell ec2 connect web --ssh # by Name tag or instance id
|
|
804
|
+
ai-shell ec2 connect i-0abc... --rdp # opens mstsc, the macOS RDP app, xfreerdp or Remmina
|
|
805
|
+
ai-shell ec2 connect web --shell # an SSM shell, no key needed; else --ssh
|
|
806
|
+
ai-shell ec2 connect web --port 8080 # a tunnel to any port
|
|
807
|
+
ai-shell ec2 stop web
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
- **Listing.** All profiles are read at the same time. A profile whose SSO session has ended shows as `signed out` with the `aws sso login` command to run; no browser opens. `--region` sets the region for every profile, `--all-regions` reads every enabled region. A profile that asks for input (an MFA code) fails at once instead of waiting.
|
|
811
|
+
- **Starting.** `connect` starts a stopped instance and waits until it runs. When it started the instance, it asks at the end whether to stop it again (`--stop` and `--no-stop` answer before).
|
|
812
|
+
- **Getting in.** `connect` tries these one at a time, each only when it can work, and names the reason for each one that failed:
|
|
813
|
+
1. SSM port forwarding: the instance is registered with SSM and this computer has the [Session Manager plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html).
|
|
814
|
+
2. An EC2 Instance Connect Endpoint in the instance's VPC (ports 22 and 3389 only; AWS CLI 2.12 or later).
|
|
815
|
+
3. The instance's public IP, then its private IP (for a VPN or a computer in the VPC).
|
|
816
|
+
- **Signing in with SSH.** The user comes from the image name (`ubuntu`, `admin` on Debian, else `ec2-user`; `--user`). The key is `--key`, else the instance's key pair file in `~/.ssh` (`<name>` or `<name>.pem`), else a temporary key sent with EC2 Instance Connect (valid for 60 seconds), else your SSH agent.
|
|
817
|
+
- **RDP.** With the key pair file, `--rdp` also prints the Windows Administrator password.
|
|
818
|
+
- **No changes to the account.** `ai-shell ec2` never adds a security group rule, changes IAM or installs an agent to get in. It writes only start, stop and the temporary EC2 Instance Connect key.
|
|
819
|
+
|
|
794
820
|
## Requirements
|
|
795
821
|
|
|
796
822
|
- Docker, or Podman with the `docker` CLI shim
|
|
@@ -9,6 +9,7 @@ import click
|
|
|
9
9
|
from ai_shell import __version__
|
|
10
10
|
from ai_shell.cli import CONTEXT_SETTINGS
|
|
11
11
|
from ai_shell.cli.commands.acp import acp_group
|
|
12
|
+
from ai_shell.cli.commands.ec2 import ec2_group
|
|
12
13
|
from ai_shell.cli.commands.llm import llm_group
|
|
13
14
|
from ai_shell.cli.commands.manage import doctor, manage_group
|
|
14
15
|
from ai_shell.cli.commands.tools import chrome, claude, codex, init, opencode, pi, shell
|
|
@@ -76,6 +77,7 @@ cli.add_command(llm_group, "llm")
|
|
|
76
77
|
cli.add_command(manage_group, "manage")
|
|
77
78
|
cli.add_command(vpn_group, "vpn")
|
|
78
79
|
cli.add_command(acp_group, "acp")
|
|
80
|
+
cli.add_command(ec2_group, "ec2")
|
|
79
81
|
|
|
80
82
|
|
|
81
83
|
def main():
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"""``ai-shell ec2``: list, connect to and stop EC2 instances across AWS profiles."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess # nosec B404 (fixed argv lists, no shell)
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
from rich.console import Console
|
|
12
|
+
from rich.table import Table
|
|
13
|
+
|
|
14
|
+
from ai_shell import ec2
|
|
15
|
+
from ai_shell import remote as core
|
|
16
|
+
from ai_shell.cli import CONTEXT_SETTINGS
|
|
17
|
+
from ai_shell.ec2 import Instance, ProfileResult, RemoteError, Scope
|
|
18
|
+
|
|
19
|
+
out = Console()
|
|
20
|
+
console = Console(stderr=True)
|
|
21
|
+
|
|
22
|
+
STATE_STYLE = {"running": "green", "stopped": "dim", "pending": "yellow", "stopping": "yellow"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _say(line: str) -> None:
|
|
26
|
+
console.print(f"[dim]{line}[/dim]", highlight=False)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _profile_options(func):
|
|
30
|
+
func = click.option(
|
|
31
|
+
"--all-regions",
|
|
32
|
+
is_flag=True,
|
|
33
|
+
default=False,
|
|
34
|
+
help="Every enabled region, not only each profile's own region.",
|
|
35
|
+
)(func)
|
|
36
|
+
func = click.option("--region", default="", help="Region for every profile.")(func)
|
|
37
|
+
func = click.option(
|
|
38
|
+
"--profile",
|
|
39
|
+
"profiles",
|
|
40
|
+
multiple=True,
|
|
41
|
+
help="Only this AWS profile (repeatable). Default: every profile in the AWS config.",
|
|
42
|
+
)(func)
|
|
43
|
+
return func
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _gather(profiles: tuple[str, ...], region: str, all_regions: bool) -> list[ProfileResult]:
|
|
47
|
+
"""Every instance the profiles see. A named profile that is signed out signs in first."""
|
|
48
|
+
try:
|
|
49
|
+
names = list(profiles) or ec2.list_profiles()
|
|
50
|
+
except RemoteError as exc:
|
|
51
|
+
raise click.ClickException(str(exc)) from exc
|
|
52
|
+
if not names:
|
|
53
|
+
raise click.ClickException("No AWS profiles. Run: aws configure sso")
|
|
54
|
+
config = ec2.read_aws_config()
|
|
55
|
+
with console.status(f"Reading {len(names)} AWS profile(s)..."):
|
|
56
|
+
results = ec2.gather(names, region, all_regions, config)
|
|
57
|
+
if profiles:
|
|
58
|
+
signed_out = [r for r in results if r.status == "signed out"]
|
|
59
|
+
for result in signed_out:
|
|
60
|
+
try:
|
|
61
|
+
core.ensure_login(Scope(result.profile, result.region))
|
|
62
|
+
except RemoteError as exc:
|
|
63
|
+
raise click.ClickException(str(exc)) from exc
|
|
64
|
+
if signed_out:
|
|
65
|
+
with console.status("Reading again after sign-in..."):
|
|
66
|
+
results = ec2.gather(names, region, all_regions, config)
|
|
67
|
+
return results
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _problems(results: list[ProfileResult]) -> None:
|
|
71
|
+
"""Say which profiles gave no answer, and how to sign in to the ones that are signed out."""
|
|
72
|
+
bad = [r for r in results if r.status != "ok"]
|
|
73
|
+
if not bad:
|
|
74
|
+
return
|
|
75
|
+
console.print()
|
|
76
|
+
for result in bad:
|
|
77
|
+
where = f"{result.profile} ({result.region})" if result.region else result.profile
|
|
78
|
+
detail = "" if result.status == "signed out" else f": {result.detail}"
|
|
79
|
+
console.print(
|
|
80
|
+
f"[yellow]{where}: {result.status}[/yellow][dim]{detail}[/dim]", highlight=False
|
|
81
|
+
)
|
|
82
|
+
commands = ec2.sign_in_commands(bad, ec2.read_aws_config())
|
|
83
|
+
if commands:
|
|
84
|
+
console.print("[bold]Sign in:[/bold]")
|
|
85
|
+
for command in commands:
|
|
86
|
+
console.print(f" {command}", highlight=False)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _table(instances: list[Instance], numbered: bool = False) -> Table:
|
|
90
|
+
table = Table(show_header=True, header_style="bold", box=None, pad_edge=False)
|
|
91
|
+
if numbered:
|
|
92
|
+
table.add_column("#", justify="right")
|
|
93
|
+
for column in (
|
|
94
|
+
"Name",
|
|
95
|
+
"Instance",
|
|
96
|
+
"State",
|
|
97
|
+
"Type",
|
|
98
|
+
"OS",
|
|
99
|
+
"SSM",
|
|
100
|
+
"Public IP",
|
|
101
|
+
"Private IP",
|
|
102
|
+
"Profile",
|
|
103
|
+
"Region",
|
|
104
|
+
):
|
|
105
|
+
table.add_column(column)
|
|
106
|
+
for n, instance in enumerate(instances, 1):
|
|
107
|
+
style = STATE_STYLE.get(instance.state, "")
|
|
108
|
+
ssm = {"": "-", "?": "?"}.get(instance.ssm, instance.ssm)
|
|
109
|
+
row = [
|
|
110
|
+
instance.name or "-",
|
|
111
|
+
instance.instance_id,
|
|
112
|
+
f"[{style}]{instance.state}[/{style}]" if style else instance.state,
|
|
113
|
+
instance.instance_type,
|
|
114
|
+
instance.platform,
|
|
115
|
+
ssm,
|
|
116
|
+
instance.public_ip or "-",
|
|
117
|
+
instance.private_ip or "-",
|
|
118
|
+
instance.profile,
|
|
119
|
+
instance.region,
|
|
120
|
+
]
|
|
121
|
+
table.add_row(*([str(n)] if numbered else []), *row)
|
|
122
|
+
return table
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _choose(
|
|
126
|
+
results: list[ProfileResult], target: str, states: tuple[str, ...] | None = None
|
|
127
|
+
) -> Instance:
|
|
128
|
+
"""The instance named *target*, or the one picked from a numbered table."""
|
|
129
|
+
instances = ec2.all_instances(results)
|
|
130
|
+
if states:
|
|
131
|
+
instances = [i for i in instances if i.state in states]
|
|
132
|
+
if target:
|
|
133
|
+
instances = ec2.matches(instances, target)
|
|
134
|
+
if not instances:
|
|
135
|
+
_problems(results)
|
|
136
|
+
what = f"named {target!r}" if target else "to choose from"
|
|
137
|
+
raise click.ClickException(f"No instance {what}.")
|
|
138
|
+
if len(instances) == 1:
|
|
139
|
+
return instances[0]
|
|
140
|
+
out.print(_table(instances, numbered=True))
|
|
141
|
+
number: int = click.prompt(
|
|
142
|
+
"Instance", type=click.IntRange(1, len(instances)), err=True, prompt_suffix=" #: "
|
|
143
|
+
)
|
|
144
|
+
return instances[number - 1]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@click.group("ec2", context_settings=CONTEXT_SETTINGS)
|
|
148
|
+
def ec2_group() -> None:
|
|
149
|
+
"""EC2 instances across your AWS profiles: list, connect, stop.
|
|
150
|
+
|
|
151
|
+
Uses the AWS CLI v2 and your profiles. Connecting tries SSM, then an EC2 Instance
|
|
152
|
+
Connect Endpoint, then the instance's IP, and never changes the account to get in.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@ec2_group.command("ls")
|
|
157
|
+
@_profile_options
|
|
158
|
+
def ls(profiles: tuple[str, ...], region: str, all_regions: bool) -> None:
|
|
159
|
+
"""List the instances of every profile (or of --profile)."""
|
|
160
|
+
results = _gather(profiles, region, all_regions)
|
|
161
|
+
instances = ec2.all_instances(results)
|
|
162
|
+
if instances:
|
|
163
|
+
out.print(_table(instances))
|
|
164
|
+
else:
|
|
165
|
+
console.print("No instances.")
|
|
166
|
+
_problems(results)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _stop_when_done(instance: Instance, started: bool, stop_after: bool | None) -> None:
|
|
170
|
+
if stop_after is None:
|
|
171
|
+
stop_after = (
|
|
172
|
+
started
|
|
173
|
+
and sys.stdin.isatty()
|
|
174
|
+
and click.confirm(f"Stop {instance.label} now?", default=True, err=True)
|
|
175
|
+
)
|
|
176
|
+
if stop_after:
|
|
177
|
+
try:
|
|
178
|
+
ec2.stop(instance)
|
|
179
|
+
except RemoteError as exc:
|
|
180
|
+
raise click.ClickException(str(exc)) from exc
|
|
181
|
+
console.print(f"Stopping {instance.label}.")
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _hold(endpoint: ec2.Endpoint) -> int:
|
|
185
|
+
"""Keep the tunnel open until it ends or Ctrl+C."""
|
|
186
|
+
if endpoint.process is None:
|
|
187
|
+
return 0
|
|
188
|
+
try:
|
|
189
|
+
return endpoint.process.wait()
|
|
190
|
+
except KeyboardInterrupt:
|
|
191
|
+
return 0
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _ssh(instance: Instance, endpoint: ec2.Endpoint, user: str, key: Path | None) -> int:
|
|
195
|
+
if shutil.which("ssh") is None:
|
|
196
|
+
raise RemoteError("ssh is not installed on this computer.")
|
|
197
|
+
user = user or ec2.login_user(instance)
|
|
198
|
+
folder: Path | None = None
|
|
199
|
+
try:
|
|
200
|
+
if key is None:
|
|
201
|
+
key = ec2.key_file(instance)
|
|
202
|
+
if key is None:
|
|
203
|
+
folder = ec2.temporary_folder()
|
|
204
|
+
try:
|
|
205
|
+
key = ec2.push_temporary_key(instance, user, folder)
|
|
206
|
+
_say("Sent a temporary key with EC2 Instance Connect (valid for 60 s).")
|
|
207
|
+
except RemoteError as exc:
|
|
208
|
+
_say(
|
|
209
|
+
f"No key file and no EC2 Instance Connect key ({ec2.first_line(str(exc))}); "
|
|
210
|
+
"ssh uses your agent and default keys."
|
|
211
|
+
)
|
|
212
|
+
args = ec2.ssh_args(endpoint, user, key, instance)
|
|
213
|
+
console.print(f"[bold]ssh {user}@{instance.label} via {endpoint.method}[/bold]")
|
|
214
|
+
code = subprocess.call(args) # nosec B603 (fixed argv, no shell)
|
|
215
|
+
if code == 255:
|
|
216
|
+
console.print(
|
|
217
|
+
f"[yellow]ssh failed. User {user} (--user), key "
|
|
218
|
+
f"{key or 'from your agent'} (--key).[/yellow]"
|
|
219
|
+
)
|
|
220
|
+
return code
|
|
221
|
+
finally:
|
|
222
|
+
if folder is not None:
|
|
223
|
+
shutil.rmtree(folder, ignore_errors=True)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _rdp(instance: Instance, endpoint: ec2.Endpoint, key: Path | None, hold: bool) -> int:
|
|
227
|
+
key = key or ec2.key_file(instance)
|
|
228
|
+
if instance.windows and key is not None:
|
|
229
|
+
try:
|
|
230
|
+
password = ec2.windows_password(instance, key)
|
|
231
|
+
if password:
|
|
232
|
+
console.print(f"User: Administrator\nPassword: {password}", highlight=False)
|
|
233
|
+
except RemoteError as exc:
|
|
234
|
+
_say(f"No Administrator password ({ec2.first_line(str(exc))}).")
|
|
235
|
+
elif instance.windows and instance.key_name:
|
|
236
|
+
_say(f"The Administrator password needs the key pair file: --key {instance.key_name}.pem")
|
|
237
|
+
address = f"{endpoint.host}:{endpoint.port}"
|
|
238
|
+
client = ec2.rdp_client_args(endpoint.host, endpoint.port)
|
|
239
|
+
console.print(f"[green bold]RDP: {address} via {endpoint.method}[/green bold]")
|
|
240
|
+
viewer = None
|
|
241
|
+
if client is None:
|
|
242
|
+
console.print("No RDP client found; open one on that address.")
|
|
243
|
+
else:
|
|
244
|
+
viewer = subprocess.Popen(client) # nosec B603 (fixed argv, no shell)
|
|
245
|
+
if endpoint.process is not None:
|
|
246
|
+
console.print("Ctrl+C ends the tunnel.")
|
|
247
|
+
return _hold(endpoint)
|
|
248
|
+
if viewer is not None and hold:
|
|
249
|
+
viewer.wait() # the session ends when the RDP window closes
|
|
250
|
+
return 0
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _tunnel(instance: Instance, endpoint: ec2.Endpoint, remote_port: int, hold: bool) -> int:
|
|
254
|
+
if endpoint.process is None:
|
|
255
|
+
console.print(
|
|
256
|
+
f"[green bold]{instance.label} answers on {endpoint.host}:{endpoint.port} "
|
|
257
|
+
f"({endpoint.method}); no tunnel needed.[/green bold]"
|
|
258
|
+
)
|
|
259
|
+
if hold and sys.stdin.isatty():
|
|
260
|
+
try:
|
|
261
|
+
click.prompt(
|
|
262
|
+
"Press Enter when you are done",
|
|
263
|
+
default="",
|
|
264
|
+
show_default=False,
|
|
265
|
+
err=True,
|
|
266
|
+
prompt_suffix="",
|
|
267
|
+
)
|
|
268
|
+
except (click.Abort, EOFError):
|
|
269
|
+
pass
|
|
270
|
+
return 0
|
|
271
|
+
console.print(
|
|
272
|
+
f"[green bold]localhost:{endpoint.port} -> {instance.label} port {remote_port} "
|
|
273
|
+
f"via {endpoint.method}[/green bold]\nCtrl+C ends the tunnel."
|
|
274
|
+
)
|
|
275
|
+
return _hold(endpoint)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
@ec2_group.command("connect")
|
|
279
|
+
@click.argument("target", required=False, default="")
|
|
280
|
+
@_profile_options
|
|
281
|
+
@click.option("--tunnel", "mode", flag_value="tunnel", default=True, help="A tunnel (default).")
|
|
282
|
+
@click.option("--ssh", "mode", flag_value="ssh", help="An SSH session.")
|
|
283
|
+
@click.option("--rdp", "mode", flag_value="rdp", help="Open the RDP client.")
|
|
284
|
+
@click.option("--shell", "mode", flag_value="shell", help="An SSM shell (no key); else --ssh.")
|
|
285
|
+
@click.option(
|
|
286
|
+
"--port", type=int, default=0, help="Port on the instance (default 22, 3389 on Windows)."
|
|
287
|
+
)
|
|
288
|
+
@click.option("--local-port", type=int, default=0, help="Local end of the tunnel.")
|
|
289
|
+
@click.option("--user", default="", help="SSH user (default: from the image).")
|
|
290
|
+
@click.option(
|
|
291
|
+
"--key",
|
|
292
|
+
type=click.Path(exists=True, dir_okay=False, path_type=Path),
|
|
293
|
+
default=None,
|
|
294
|
+
help="SSH private key or key pair file (default: ~/.ssh/<key pair name>).",
|
|
295
|
+
)
|
|
296
|
+
@click.option(
|
|
297
|
+
"--stop/--no-stop",
|
|
298
|
+
"stop_after",
|
|
299
|
+
default=None,
|
|
300
|
+
help="Stop the instance when the session ends (default: ask, when this command started it).",
|
|
301
|
+
)
|
|
302
|
+
def connect(
|
|
303
|
+
target: str,
|
|
304
|
+
profiles: tuple[str, ...],
|
|
305
|
+
region: str,
|
|
306
|
+
all_regions: bool,
|
|
307
|
+
mode: str,
|
|
308
|
+
port: int,
|
|
309
|
+
local_port: int,
|
|
310
|
+
user: str,
|
|
311
|
+
key: Path | None,
|
|
312
|
+
stop_after: bool | None,
|
|
313
|
+
) -> None:
|
|
314
|
+
"""Start an instance when needed and connect to it.
|
|
315
|
+
|
|
316
|
+
TARGET is an instance id or Name tag; without it, pick from a list.
|
|
317
|
+
"""
|
|
318
|
+
results = _gather(profiles, region, all_regions)
|
|
319
|
+
instance = _choose(results, target)
|
|
320
|
+
if mode == "rdp":
|
|
321
|
+
remote_port = port or 3389
|
|
322
|
+
elif mode in ("ssh", "shell"):
|
|
323
|
+
remote_port = port or 22
|
|
324
|
+
else:
|
|
325
|
+
remote_port = port or instance.default_port
|
|
326
|
+
started = False
|
|
327
|
+
code = 0
|
|
328
|
+
try:
|
|
329
|
+
try:
|
|
330
|
+
started = core.start_instance(instance.scope, instance.instance_id, say=_say)
|
|
331
|
+
if started:
|
|
332
|
+
instance = ec2.describe(instance)
|
|
333
|
+
hold = stop_after is True or (stop_after is None and started)
|
|
334
|
+
code = _connect(instance, mode, remote_port, local_port, user, key, started, hold)
|
|
335
|
+
except RemoteError as exc:
|
|
336
|
+
raise click.ClickException(str(exc)) from exc
|
|
337
|
+
finally:
|
|
338
|
+
_stop_when_done(instance, started, stop_after)
|
|
339
|
+
if code:
|
|
340
|
+
sys.exit(code)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _connect(
|
|
344
|
+
instance: Instance,
|
|
345
|
+
mode: str,
|
|
346
|
+
remote_port: int,
|
|
347
|
+
local_port: int,
|
|
348
|
+
user: str,
|
|
349
|
+
key: Path | None,
|
|
350
|
+
started: bool,
|
|
351
|
+
hold: bool,
|
|
352
|
+
) -> int:
|
|
353
|
+
"""Open the session; *hold* keeps a session with no process of its own (direct
|
|
354
|
+
tunnel, RDP) open until the user is done, because the instance is stopped after it."""
|
|
355
|
+
skip: tuple[str, ...] = ()
|
|
356
|
+
if mode == "shell":
|
|
357
|
+
reason = ec2.ssm_ready(instance, started, _say)
|
|
358
|
+
if reason is None:
|
|
359
|
+
console.print(f"[bold]SSM shell on {instance.label}[/bold]")
|
|
360
|
+
return subprocess.call(ec2.ssm_shell_args(instance), env=core.aws_env()) # nosec B603
|
|
361
|
+
_say(f"No SSM shell: {reason}. Trying SSH.")
|
|
362
|
+
mode, skip = "ssh", ("SSM",)
|
|
363
|
+
if mode == "ssh":
|
|
364
|
+
local = local_port or ec2.free_port()
|
|
365
|
+
else:
|
|
366
|
+
local = local_port or ec2.free_port(ec2.local_port_for(remote_port))
|
|
367
|
+
endpoint = ec2.open_endpoint(instance, remote_port, local, fresh=started, say=_say, skip=skip)
|
|
368
|
+
try:
|
|
369
|
+
if mode == "ssh":
|
|
370
|
+
return _ssh(instance, endpoint, user, key)
|
|
371
|
+
if mode == "rdp":
|
|
372
|
+
return _rdp(instance, endpoint, key, hold)
|
|
373
|
+
return _tunnel(instance, endpoint, remote_port, hold)
|
|
374
|
+
finally:
|
|
375
|
+
endpoint.close()
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
@ec2_group.command("stop")
|
|
379
|
+
@click.argument("target", required=False, default="")
|
|
380
|
+
@_profile_options
|
|
381
|
+
@click.option("--yes", "-y", is_flag=True, default=False, help="Do not ask first.")
|
|
382
|
+
def stop_command(
|
|
383
|
+
target: str, profiles: tuple[str, ...], region: str, all_regions: bool, yes: bool
|
|
384
|
+
) -> None:
|
|
385
|
+
"""Stop an instance. TARGET is an instance id or Name tag; without it, pick one."""
|
|
386
|
+
results = _gather(profiles, region, all_regions)
|
|
387
|
+
instance = _choose(results, target, states=("pending", "running"))
|
|
388
|
+
if not yes:
|
|
389
|
+
click.confirm(
|
|
390
|
+
f"Stop {instance.label} ({instance.profile})?", default=True, abort=True, err=True
|
|
391
|
+
)
|
|
392
|
+
try:
|
|
393
|
+
ec2.stop(instance)
|
|
394
|
+
except RemoteError as exc:
|
|
395
|
+
raise click.ClickException(str(exc)) from exc
|
|
396
|
+
console.print(f"Stopping {instance.label}.")
|