Compare commits
51
Commits
No files matched your search
@@ -334,7 +334,9 @@ jobs:
|
||||
run: npx vitest run
|
||||
|
||||
# `--locked`: test against the committed Cargo.lock, never a re-resolved
|
||||
# one, for the same reason the frontend uses `npm ci`.
|
||||
# one, for the same reason the frontend uses `npm ci`. No plugin fetch
|
||||
# here: a debug build gets a placeholder for the session-manager-plugin
|
||||
# sidecar from build.rs, so tests never build Go or download anything.
|
||||
- name: Backend tests (cargo test)
|
||||
working-directory: ./app/src-tauri
|
||||
run: |
|
||||
@@ -468,6 +470,14 @@ jobs:
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri --version || npm install @tauri-apps/cli
|
||||
|
||||
# The AWS Session Manager plugin, bundled as a sidecar (bundle.externalBin)
|
||||
# so AWS workspaces work without the user installing it. Built here from
|
||||
# AWS's Apache-2.0 source (pinned commit + archive SHA-256) with a pinned,
|
||||
# hash-checked Go; the output must match a pinned SHA-256 (reproducible
|
||||
# build). A release build refuses to start without it (build.rs).
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: bash scripts/build-ssm-plugin.sh x86_64-unknown-linux-gnu
|
||||
|
||||
- name: Build Tauri app
|
||||
working-directory: ./app
|
||||
env:
|
||||
@@ -597,6 +607,15 @@ jobs:
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri --version || npm install @tauri-apps/cli
|
||||
|
||||
# The AWS Session Manager plugin sidecar (bundle.externalBin), built from
|
||||
# source (see build-ssm-plugin.sh): both thin slices (tauri-build checks
|
||||
# each arch it compiles) and the lipo'd universal one the bundler copies
|
||||
# into Contents/MacOS. Each must match its pinned SHA-256. Signed exactly
|
||||
# like the app: Go ad-hoc signs the arm64 slice, and with APPLE_* signing
|
||||
# configured Tauri would sign the sidecar with the app's identity.
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: bash scripts/build-ssm-plugin.sh universal-apple-darwin
|
||||
|
||||
- name: Build Tauri app (universal)
|
||||
working-directory: ./app
|
||||
env:
|
||||
@@ -606,6 +625,21 @@ jobs:
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri build --target universal-apple-darwin
|
||||
|
||||
# The sidecar and its notices must be in the bundle Tauri made: the
|
||||
# plugin in Contents/MacOS with both slices, THIRD_PARTY_NOTICES in
|
||||
# Contents/Resources (bundle.resources).
|
||||
- name: Check the bundled Session Manager plugin
|
||||
run: |
|
||||
app_dir="$(ls -d app/src-tauri/target/universal-apple-darwin/release/bundle/macos/*.app | head -n1)"
|
||||
[ -n "$app_dir" ] || { echo "no .app bundle"; exit 1; }
|
||||
plugin="$app_dir/Contents/MacOS/session-manager-plugin"
|
||||
[ -x "$plugin" ] || { echo "missing $plugin"; exit 1; }
|
||||
archs="$(lipo -archs "$plugin")"
|
||||
case " $archs " in *" x86_64 "*) ;; *) echo "plugin lacks x86_64 ($archs)"; exit 1 ;; esac
|
||||
case " $archs " in *" arm64 "*) ;; *) echo "plugin lacks arm64 ($archs)"; exit 1 ;; esac
|
||||
"$plugin" --version
|
||||
[ -f "$app_dir/Contents/Resources/THIRD_PARTY_NOTICES" ] || { echo "missing Contents/Resources/THIRD_PARTY_NOTICES"; exit 1; }
|
||||
|
||||
- name: Collect artifacts
|
||||
run: |
|
||||
mkdir -p artifacts
|
||||
@@ -729,6 +763,13 @@ jobs:
|
||||
set "PATH=%USERPROFILE%\.cargo\bin;C:\Program Files\nodejs;%PATH%"
|
||||
npm run build
|
||||
|
||||
# The AWS Session Manager plugin sidecar (bundle.externalBin), built from
|
||||
# source (see build-ssm-plugin.ps1); must match its pinned SHA-256. It
|
||||
# comes out unsigned, so it ships unsigned in previews, like the
|
||||
# app (releases sign it: see build-app.yml).
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File scripts\build-ssm-plugin.ps1
|
||||
|
||||
- name: Build Tauri app
|
||||
working-directory: ./app
|
||||
# Previews are not code-signed: signing is metered, previews are built
|
||||
|
||||
@@ -227,6 +227,14 @@ jobs:
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri --version || npm install @tauri-apps/cli
|
||||
|
||||
# The AWS Session Manager plugin, bundled as a sidecar (bundle.externalBin)
|
||||
# so AWS workspaces work without the user installing it. Built here from
|
||||
# AWS's Apache-2.0 source (pinned commit + archive SHA-256) with a pinned,
|
||||
# hash-checked Go; the output must match a pinned SHA-256 (reproducible
|
||||
# build). A release build refuses to start without it (build.rs).
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: bash scripts/build-ssm-plugin.sh x86_64-unknown-linux-gnu
|
||||
|
||||
- name: Build Tauri app
|
||||
working-directory: ./app
|
||||
run: |
|
||||
@@ -417,12 +425,36 @@ jobs:
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri --version || npm install @tauri-apps/cli
|
||||
|
||||
# The AWS Session Manager plugin sidecar (bundle.externalBin), built from
|
||||
# source (see build-ssm-plugin.sh): both thin slices (tauri-build checks
|
||||
# each arch it compiles) and the lipo'd universal one the bundler copies
|
||||
# into Contents/MacOS. Each must match its pinned SHA-256. Signed exactly
|
||||
# like the app: Go ad-hoc signs the arm64 slice, and with APPLE_* signing
|
||||
# configured Tauri would sign the sidecar with the app's identity.
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: bash scripts/build-ssm-plugin.sh universal-apple-darwin
|
||||
|
||||
- name: Build Tauri app (universal)
|
||||
working-directory: ./app
|
||||
run: |
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
npx tauri build --target universal-apple-darwin
|
||||
|
||||
# The sidecar and its notices must be in the bundle Tauri made: the
|
||||
# plugin in Contents/MacOS with both slices, THIRD_PARTY_NOTICES in
|
||||
# Contents/Resources (bundle.resources).
|
||||
- name: Check the bundled Session Manager plugin
|
||||
run: |
|
||||
app_dir="$(ls -d app/src-tauri/target/universal-apple-darwin/release/bundle/macos/*.app | head -n1)"
|
||||
[ -n "$app_dir" ] || { echo "no .app bundle"; exit 1; }
|
||||
plugin="$app_dir/Contents/MacOS/session-manager-plugin"
|
||||
[ -x "$plugin" ] || { echo "missing $plugin"; exit 1; }
|
||||
archs="$(lipo -archs "$plugin")"
|
||||
case " $archs " in *" x86_64 "*) ;; *) echo "plugin lacks x86_64 ($archs)"; exit 1 ;; esac
|
||||
case " $archs " in *" arm64 "*) ;; *) echo "plugin lacks arm64 ($archs)"; exit 1 ;; esac
|
||||
"$plugin" --version
|
||||
[ -f "$app_dir/Contents/Resources/THIRD_PARTY_NOTICES" ] || { echo "missing Contents/Resources/THIRD_PARTY_NOTICES"; exit 1; }
|
||||
|
||||
- name: Collect artifacts
|
||||
run: |
|
||||
mkdir -p artifacts
|
||||
@@ -656,6 +688,13 @@ jobs:
|
||||
set "PATH=%USERPROFILE%\.cargo\bin;C:\Program Files\nodejs;%PATH%"
|
||||
npm run build
|
||||
|
||||
# The AWS Session Manager plugin sidecar (bundle.externalBin), built from
|
||||
# source (see build-ssm-plugin.ps1); must match its pinned SHA-256. It
|
||||
# comes out unsigned, so Tauri hands it to the sign command and
|
||||
# "Verify signatures" requires our signature on it in both installers.
|
||||
- name: Build the AWS Session Manager plugin
|
||||
run: powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File scripts\build-ssm-plugin.ps1
|
||||
|
||||
# Releases are signed with Azure Artifact Signing (scripts/windows-*.ps1).
|
||||
# The setup fetches the signing client and a job-local .NET runtime, and
|
||||
# writes the Tauri config holding the sign command, which "Build Tauri
|
||||
|
||||
@@ -27,3 +27,7 @@ screenshot_for_fix/
|
||||
*.tfstate
|
||||
*.tfstate.*
|
||||
.terraform.tfstate.lock.info
|
||||
|
||||
# The bundled AWS session-manager-plugin sidecar (scripts/build-ssm-plugin.*, built from source) and the
|
||||
# debug-build placeholder build.rs writes there. Never committed.
|
||||
app/src-tauri/binaries/
|
||||
@@ -14,7 +14,8 @@ All frontend/tauri commands run from the `app/` directory:
|
||||
cd app
|
||||
npm ci # Install dependencies (required first time)
|
||||
npx tauri dev # Launch app in dev mode with hot reload (Vite on port 1420)
|
||||
npx tauri build # Production build (outputs to src-tauri/target/release/bundle/)
|
||||
npx tauri build # Production build (outputs to src-tauri/target/release/bundle/);
|
||||
# run ../scripts/build-ssm-plugin.sh first (bundled AWS plugin, see Packaging)
|
||||
npm run build # Frontend-only build (tsc + vite)
|
||||
npm run test # Run Vitest once
|
||||
npm run test:watch # Run Vitest in watch mode
|
||||
@@ -292,7 +293,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
|
||||
when it drops, so an `AgentEndpoint` stays valid. The `StartSession` token goes to the plugin
|
||||
through its environment, never argv, and the plugin's environment is stripped of every
|
||||
inherited `AWS_*` variable before the session's own credentials are added.
|
||||
`aws_host.rs` is `ProjectHost::Aws` (an ECS Fargate task per project; a running task is
|
||||
`aws_host.rs` is `ProjectHost::Aws` (an ECS task per project, Fargate or, on the user's own instance, EC2; a running task is
|
||||
attached to with the token read back from its secret, never rotated; a task a failed start ran
|
||||
is stopped again) and `tunnels.rs` the registry of open tunnels by project — an AWS project's
|
||||
`ContainerTarget::for_project` is its tunnel's endpoint, its stored `container_id` is that
|
||||
@@ -309,8 +310,69 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
|
||||
that would be refused is skipped with a warning at connect. Opens are serialised per project and
|
||||
re-read the stored mappings under that lock; unchanged forwards are kept with their connections.
|
||||
Opened after every connect and on a save while connected; closed by every `tunnels::close*`
|
||||
path. A failing mapping is a `workspace-warning` (`topic: "ports"`). An idle listener makes no
|
||||
path. A failing mapping is a `workspace-warning` (`topic: "ports"`), and so is a Running project
|
||||
whose mappings could not be opened at all (no tunnel / tunnel replaced) — never silent; a stored
|
||||
row missing a port (0) is skipped with one too ("incomplete, both ports are needed").
|
||||
`PortMappingsEditor` saves only complete rows (half-filled ones stay local, marked), adopts a
|
||||
stored value only when it differs by value and no field in it has focus, and uses word
|
||||
placeholders — a "8080" placeholder once read as a typed 8080 → 8080 that was saved as 0 → 0. In the
|
||||
workspace each connection picks `127.0.0.1` or `[::1]` from `/proc/net/tcp{,6}`
|
||||
(`tunnel::mapping_command`; no probe connection), and the first socat "Connection refused" per
|
||||
mapping is a `ports` warning ("nothing is listening on port N"). An idle listener makes no
|
||||
agent request; a connection carrying traffic is activity (keeps the workspace from idling).
|
||||
**`persistent.rs` is persistent AWS terminals** (Docker projects never reach it): every tab on an
|
||||
AWS project — Claude and bash, desktop and web terminal — runs its **unchanged** launch command
|
||||
(update prelude and all, or `bash -l`) inside **its own tmux server**,
|
||||
`tmux -L triple-c-<session id> -f /etc/triple-c/tmux.conf new-session -A -s <claude|bash> -- <cmd>`.
|
||||
Rules, each for a reason:
|
||||
- **One server per tab, never one shared server**: a server takes its environment from the
|
||||
client that started it, so each tab keeps exactly its own `ExecSpec.env` (copy-at-connect
|
||||
`terminal_env` pairs). Never `new-session -e` — that puts the values on argv (`ps`). A later
|
||||
connect's new credentials do not reach a session already running (like any running process).
|
||||
The client gets `TERM=xterm-256color` (the agent would default a tty to `xterm`, 8 colours).
|
||||
- **The tab's session id is the tmux key** (validated `[A-Za-z0-9-]{1,64}`), remembered per
|
||||
project in `storage/terminal_store.rs` (`terminal-sessions.json` beside `projects.json`; a
|
||||
hint pruned by the workspace's own listing, cleared on Stop/Remove; not a `Project` field, for
|
||||
the reason notes aren't). Same id after a restart, so `renamed_session_names` still applies.
|
||||
- **Closing a tab ends the session** (`kill-server`); app exit, a dropped tunnel and the agent's
|
||||
SIGHUP on a vanished client only **detach** (the server daemonised out of the client's
|
||||
process group). **Reattach is `attach-session`, never `new-session -A`**: a session that
|
||||
ended while the app was away must not come back as a fresh `claude` (and the update prelude
|
||||
runs once, inside, never again on reattach).
|
||||
- **Stop ends every tmux server** (`persistent::END_ALL_SCRIPT`: the panes' pids are taken
|
||||
before `kill-server`, then a bounded 5 s wait for them and the servers) in the same
|
||||
`close_first` step that closes the tabs, **before** the stop guard's push snapshot. The
|
||||
entrypoint's TERM hook (`aws_end_terminals`) and the idle watcher's lifetime-cap push run the
|
||||
same script verbatim (a test pins all three). The store is cleared on Stop, Remove, a move to
|
||||
Docker and a death-watch stop.
|
||||
- **The probe** (`command -v tmux` + the config) runs once per tunnel, cached by target id and
|
||||
forgotten in `tunnels::register*`, and is **three-state**: yes → persistent; a real no (an
|
||||
image without tmux) → the old launch plus a one-time notice; **unanswered → `Err`**, never
|
||||
"no" — read as no, a listing would prune every remembered tab and a reattach would end tabs
|
||||
whose Claude still runs. Nothing is pruned or ended on an `Err`; the frontend asks again.
|
||||
Every tmux exec except the attach goes through `exec_oneshot_background` (never idle-stop
|
||||
activity). `TMUX`/`TMUX_TMPDIR` are dropped from the client env and reserved custom names.
|
||||
- **A tab closed while disconnected** is queued (`terminal_store` `pending_end`) and ended at the
|
||||
next connect (`list_workspace_terminals` runs it first); a reattach of a closed or queued id
|
||||
is `Ended`, and a reattach that finishes after its tab closed does not remember it again.
|
||||
- **The web terminal's exec ids are `web-<tmux key>`**, never the bare key the desktop uses:
|
||||
sharing one `ExecSessionManager` id would let either front end's attach supersede the
|
||||
other's exec (no exit callback, a frozen tab) or its close/disconnect end the other's.
|
||||
- **Cost, by ruling:** a detached `claude` still counts for the idle watcher — that *is* the
|
||||
feature (remote control keeps working with the app closed) — so a Claude left running keeps
|
||||
the workspace up and billed until it ends, it is ended (Project Home → Sessions flags
|
||||
detached Claude sessions, with End), Stop, or the lifetime cap. HostSection's notes say so
|
||||
(`PERSISTENT_TERMINALS_NOTE`). A detached shell does not keep it up.
|
||||
- Frontend: a persistent tab reattaches on exit (retrying while the tunnel comes back) and shows
|
||||
"[Session ended]" only when the workspace says the session is gone or the project stopped;
|
||||
`usePersistentTerminals` reopens this app's sessions once per connection (`id|container_id`)
|
||||
without taking focus; a reopened tab calls `reattach_terminal_session` only after its output
|
||||
*and* exit listeners are registered (history replay + tmux's first draw would be lost). Project
|
||||
Home → Sessions lists every session in the workspace (web terminal and other machines too:
|
||||
those are never auto-opened) with Reattach / End. `ExecSessionManager` ends an exec whose
|
||||
session id was taken over without its exit callback (else a reattach loops on its own exit).
|
||||
- The live check is `host::persistent::live_tests` (ignored; an agent image with tmux,
|
||||
`TRIPLEC_PERSIST_E2E_IMAGE`).
|
||||
`agent.rs` reuses `docker/exec.rs`'s `OneshotOutput`, host-file reader and uid probe so the two
|
||||
variants cannot drift; an `Agent` target's token never appears in `Debug` or error text.
|
||||
**Container-interior code must go through `ContainerTarget`, never `docker::exec`, bollard or
|
||||
@@ -367,8 +429,11 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
|
||||
push's snapshot), may push first (under an in-container `timeout`, since the agent does not kill an
|
||||
exec the client gave up on), then writes `pushed` or `discard` to `/run/triple-c/stop-reason`
|
||||
(root-owned; the entrypoint's TERM hook reads it, and the idle watcher writes `idle` or `lifetime`) so the hook
|
||||
doesn't push twice; the marker is cleared if StopTask fails. Claude Code's sandbox is forced off in
|
||||
the generated settings (bubblewrap can't create namespaces on Fargate, verified live).
|
||||
doesn't push twice; the marker is cleared if StopTask fails. **Claude Code's sandbox is forced off
|
||||
on every AWS project, Fargate and EC2** in the generated settings (`sandbox_allowed` is false; the
|
||||
Runtime toggle is locked off, `sandboxEffective` in `awsWorkspace.ts`): bubblewrap fails on Fargate
|
||||
and under Docker's default seccomp profile on EC2 too ("No permissions to create new namespace",
|
||||
verified live), so a `sandbox_allowed` that said yes would only break every command.
|
||||
**LLM gateway sidecar** (`gateway_sidecar`, Q7): a second, `essential: false` container `gateway`
|
||||
(`docker::gateway::registry_image_ref`, digest-pinned, amd64+arm64; `memoryReservation` 512; no
|
||||
`environment`, no `dependsOn`) whose `secrets` are exactly `TRIPLEC_GATEWAY_{CONFIG,API_KEY,MASTER_KEY}`
|
||||
@@ -384,6 +449,60 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
|
||||
`FILTERS`) → per-region x86/ARM rates cached at `<data_dir>/triple-c/aws-pricing/<region>.json` for
|
||||
24 h; `aws_cost_estimate(connection_id, region, config)` never errors — denied/offline/slow (8 s) is
|
||||
`None` ("Estimate unavailable"), a failed region isn't re-asked for 5 min, a stale cache still answers.
|
||||
EC2 projects (E10) pass `instance_type` (the user's, from `aws_ec2_capacity`): the `AmazonEC2`
|
||||
on-demand Linux/shared-tenancy hourly price (`EC2_FILTERS`), cached as `<region>--ec2-<type>.json`
|
||||
with the same rules — the instance's price, not the reservations — plus the 50 GiB gp3 root volume
|
||||
(`GP3_FILTERS`, `<region>--ebs-gp3.json`, best effort: without it the note says it's missing;
|
||||
`EC2_ROOT_VOLUME_GIB` is pinned to `ec2.tf`). A public IPv4 address ($0.005/h) is only in the note.
|
||||
**ECS on EC2 (phase 7; `aws/terraform/user-onboard/ec2.tf`, `workspace.rs` `ec2_plan`, `aws_host.rs`).**
|
||||
Opt-in per onboarding (`ec2_instance_type`); a project's `launch_type` is `Fargate | Ec2`. Why the
|
||||
rules are what they are:
|
||||
- **Per-user cluster `triple-c-<env>--u-<tag>`**, named by the principal tag `triple-c-user`
|
||||
(`user_ec2_stem` in `tofu_modules.rs`, the only place). One capacity provider per cluster,
|
||||
because `PutClusterCapacityProviders` replaces the list and two modules would undo each other on
|
||||
a shared cluster. Environment names and tags never contain `--`, so `triple-c-*--u-*` matches
|
||||
exactly the per-user clusters. **Two Denies in `workspace-user-tasks.json`** (`RunTask`/`StartTask`
|
||||
on a `--u-*` cluster other than the caller's own, and on any when the caller has **no**
|
||||
`triple-c-user` principal tag) are the whole isolation between EC2 users: don't loosen them, and
|
||||
keep the missing-tag Deny (a variable that resolves to nothing must not fall through). A denial
|
||||
maps to `EC2_TAG_MISMATCH` ("different triple-c-user value").
|
||||
- **The instance role is scoped by its own cluster tag.** `triple-c-<env>-<owner_key>-instance`
|
||||
(owner key, not user tag: role names are account-global) has only the ECS agent's calls, and the
|
||||
**boundary** allows them on `cluster/${aws:PrincipalTag/triple-c:cluster}` (must match `--u-*`).
|
||||
A Docker-access project is root on its instance and holds that role, so the tag is what stops it
|
||||
registering the instance into someone else's cluster. No SSM core and **no log rights** on the
|
||||
instance (awslogs writes with the execution role: `ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE`).
|
||||
The task and execution roles `PutLogEvents` only into `log-stream:<owner_key>/*` of the shared
|
||||
log group, so `log_stream_prefix` must keep starting with the owner key. The boundary
|
||||
is versioned (2): an account admin re-applies it once before anyone onboards with EC2.
|
||||
- **Launch templates and groups are pinned to a numeric template version, and the admin policy
|
||||
denies `$Latest`/`$Default`**: the admin's `iam:PassRole` and `ec2:RunInstances` checks look at the
|
||||
template when it is created, and a version edited afterwards would carry any instance type or
|
||||
role (a PassRole bypass). The type list (`ec2_instance_spec` in `models/project.rs`) lives in the
|
||||
model table, the HCL variable and the policy (`ec2:InstanceType` in the compute file,
|
||||
`autoscaling:InstanceTypes` twice in the scaling file); change all together. A template-only group
|
||||
create sends no `autoscaling:InstanceTypes`, so don't add a `Null` condition on it (live-verified).
|
||||
- **Bridge networking, not awsvpc (R2):** the instance is the user's alone, awsvpc tasks on EC2 get
|
||||
no public IP, and bridge keeps IMDS out of reach with hop limit 1 (the agent on the host network
|
||||
still reaches it). The ECS agent's introspection port (172.17.0.1:51678) stays reachable;
|
||||
accepted. The gateway sidecar is refused on EC2 (no shared loopback).
|
||||
- **Cold-start rerun + orphan rule.** ECS turns ECS Exec **off** for a task that has to wait for an
|
||||
instance (the agent tunnel needs it), so the first RunTask only brings the instance up; once
|
||||
placed, the app stops it, waits for STOPPED and runs again via `find_or_run` (so a concurrent
|
||||
start never doubles a task: `START_RACE`), requiring exec on (`EXEC_OFF`). A found exec-off task
|
||||
that is still waiting for an instance is another start's (`INSTANCE_RACE`) while younger than
|
||||
`ec2_wait + ORPHAN_MARGIN`; older is an orphan, stopped (`REPLACED_REASON`) and replaced. Keep
|
||||
both launches going through `find_or_run`.
|
||||
- **Docker access = the instance's own socket**, mounted at `/var/run/docker.sock` (root on the
|
||||
user's own instance, acceptable only because nobody else's task can land there); VPN =
|
||||
`NET_ADMIN` + `/dev/net/tun` + `src_valid_mark`. A Fargate project can't keep either: switching
|
||||
to Fargate clears both flags (`launchTypePatch`, R1).
|
||||
- **Collision check before an apply** (`ec2_collision_refusal`, `prepare_onboarding`): the module
|
||||
would adopt another user's cluster of the same name, so the app refuses a name owned by someone
|
||||
else (resources' `triple-c:owner` tag, the user record `/triple-c/users/<env>/<owner_key>` as
|
||||
fallback). Admin bundles carry the same check in `README.txt`.
|
||||
- Scale to zero (min 0): the first start after ~17 min idle is cold (~3.5-4 min), warm ~20-30 s.
|
||||
Re-applying onboarding without EC2 drains and deletes the group even with an instance running.
|
||||
**Environments are OpenTofu modules** (`aws/terraform/{environment,user-onboard,permissions-boundary}`;
|
||||
spec D2–D10). `tofu_modules.rs` embeds them and is the **only** place the shared names are built
|
||||
(environment, `/triple-c/environments/<env>` record, owner key — `sha256("<region>:<aws:userid>")`,
|
||||
@@ -505,6 +624,22 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
|
||||
The re-clone closes the apply lock's fd (a later connect never waits for it); `--prepare` takes it.
|
||||
The functions both scripts use live in `container/entrypoint-lib.sh`
|
||||
(`/usr/local/lib/triple-c/`), sourced by the entrypoint first; it only defines things.
|
||||
**Persistent terminals** (`host/persistent.rs`): the image has `tmux` (in the Python apt layer)
|
||||
and `/etc/triple-c/tmux.conf` (`container/triple-c-tmux.conf`, outside the seed-only home): no
|
||||
prefix key (Ctrl+B reaches Claude/readline), no status line, `escape-time 0` (ESC+CR stays one
|
||||
Meta-Enter), no mouse, `window-size latest`, and three outer-terminal overrides that were each
|
||||
**measured by feeding tmux's real output into xterm.js** — keep all three: `smcup@:rmcup@`
|
||||
(draw in xterm.js's normal screen, so its scrollback, copy cleanup and link detection work),
|
||||
`indn@` (tmux's CSI S scroll is a *delete* in xterm.js: 4 of 300 fast lines reached scrollback
|
||||
with it, 300 without) and `clear=\E[H` (tmux sends `clear` when its client exits, which wiped
|
||||
the session's last screen from the tab; its full redraws repaint every row anyway). The idle
|
||||
watcher still sees `claude` inside tmux (`pgrep -x claude`; tmux's processes are `tmux:
|
||||
server`/`tmux: client`), so a running Claude keeps the workspace up and a detached shell does
|
||||
not; the lifetime cap still applies.
|
||||
**The Docker-socket group step** (`# >>> docker_socket`) runs in aws mode as well as Docker mode: an
|
||||
EC2 project with Docker access mounts the instance's `/var/run/docker.sock`, and the step adds
|
||||
`claude` to a group with the socket's gid (`docker-host` if none exists). Fargate never has the
|
||||
socket, so it is inert there.
|
||||
**The `TRIPLEC_` env prefix is reserved** on both hosts (`RESERVED_ENV_PREFIXES`; `workspace.rs`
|
||||
drops custom variables in it): the entrypoint's settings live there.
|
||||
- **Browser runtime libraries are baked in; browser *binaries* are not.** A layer runs
|
||||
@@ -697,7 +832,10 @@ friendly message one line before `exec claude` ran it. `-E 0` makes losing the r
|
||||
because the holder just did the work. The per-session copy is what forced the non-Bedrock path from a bare `["claude", ...]`
|
||||
argv into a `bash -c` wrapper — the flags and the session name are interpolated into a shell
|
||||
string now, so **anything added there must go through `shell_quote_arg`**. Bash sessions are
|
||||
deliberately untouched.
|
||||
deliberately untouched. On AWS that whole command (not a copy of it) becomes the argv of a tmux
|
||||
`new-session` (`host::persistent::new_session_cmd`), so it runs once per session, never on a
|
||||
reattach; tmux reads an argument that *ends* in `;` as a command separator, so the scripts keep
|
||||
ending in a newline (`new_session_cmd` refuses otherwise).
|
||||
|
||||
### Container Lifecycle
|
||||
|
||||
@@ -1164,10 +1302,77 @@ verbatim copy yields a launcher entry that starts nothing. It keeps `StartupWMCl
|
||||
the bundle sets it, which is what lets the shell match the window to the entry. Extraction uses
|
||||
`--appimage-extract`, which needs no FUSE, so the script works before `fuse2` is installed.
|
||||
|
||||
### The bundled AWS Session Manager plugin
|
||||
|
||||
AWS workspaces connect through AWS's `session-manager-plugin` (`host/ssm.rs`), and the app
|
||||
**bundles it as a Tauri sidecar** (`bundle.externalBin: binaries/session-manager-plugin`) so users
|
||||
need not install it. Tauri copies it beside the app binary with the `-<target-triple>` suffix
|
||||
stripped — `usr/bin/session-manager-plugin` in the AppImage, `Contents/MacOS/` on macOS, the
|
||||
install directory on Windows (`session-manager-plugin.exe`), and `target/<profile>/` under
|
||||
`tauri dev`. `find_plugin()` resolves `TRIPLEC_SSM_PLUGIN` → **that sidecar (beside
|
||||
`current_exe()`)** → `PATH` → AWS's install locations → the install hint; the order is
|
||||
unit-tested (`host/ssm/tests.rs`, `plugin_resolution`).
|
||||
|
||||
- **We build it from source; we never ship AWS's binaries.** AWS distributes its prebuilt plugin
|
||||
as "AWS Content" under the AWS Customer Agreement; the source (github.com/aws/session-manager-plugin)
|
||||
is Apache-2.0. `scripts/build-ssm-plugin.sh <triple>…` (Linux/macOS; any triple, it
|
||||
cross-compiles) and `scripts/build-ssm-plugin.ps1` (the Windows runner, which has no bash) take
|
||||
the tag's **commit** as GitHub's archive (pinned SHA-256, checked before extraction; the
|
||||
vendored SDK's Java `codegen/` and test `snapshot/` trees are not extracted — they reach 258
|
||||
characters, past Windows' MAX_PATH, so the `.ps1` also works under a short `C:\ssm-<8hex>` root)
|
||||
and a
|
||||
**pinned Go** from go.dev (go.dev's published SHA-256), and run the repo's own build — GOPATH
|
||||
mode over its vendored deps, `-ldflags "-s -w"`, `sessionmanagerplugin-main` — with `VERSION`
|
||||
stamped to the tag (the repo's file says `1.3.0.0`; AWS's pipeline stamps the real one, so
|
||||
`--version` would otherwise lie), plus `-trimpath`, no VCS stamp, empty build id, `CGO_ENABLED=0`
|
||||
and a scrubbed Go environment. **The output must equal a pinned SHA-256 per target**: Go builds
|
||||
are reproducible across hosts, so every runner must produce the exact bytes recorded in the
|
||||
scripts (verified locally: Linux-hosted builds of every target, repeated in fresh directories,
|
||||
and the `.ps1` flow, are identical). A mismatch fails the build and means the source, toolchain
|
||||
or flags changed — never "update the hash" without knowing which.
|
||||
- **Each platform job builds its own targets** before `tauri build` (Linux: `x86_64-unknown-linux-gnu`;
|
||||
macOS: `universal-apple-darwin`, i.e. both thin slices — tauri-build checks the arch it compiles —
|
||||
plus the `lipo`'d universal file the bundler wants, whose slices must thin back out byte-identical
|
||||
to the pins; Windows: the `.ps1`). Not one Linux job feeding the others: workflow artifacts do
|
||||
not work on this Gitea (see `build-app-preview.yml`'s header), and with reproducible output and
|
||||
pinned hashes nothing is gained by building once.
|
||||
- **Bumping** the plugin or Go: new pins in **both** scripts, rebuild every target, record the new
|
||||
output hashes, and update `app/src-tauri/THIRD_PARTY_NOTICES` (the scripts refuse to run unless
|
||||
its `Version:`/`Commit:` lines match) including its dependency licences.
|
||||
- **Debug builds get a placeholder; release builds refuse one.** tauri-build fails *any* build
|
||||
whose externalBin file is missing, `cargo test` included. `build.rs` (`ensure_ssm_sidecar`)
|
||||
writes a marked placeholder (`src/sidecar_placeholder.rs`) for a missing file when
|
||||
`PROFILE=debug`, so `cargo test`/`cargo check`/`tauri dev` and CI's `test` job need neither Go
|
||||
nor the network, and `find_plugin()` skips it (falling back to an installed plugin). With
|
||||
`PROFILE=release` a missing file **or** a placeholder stops the build, so a local `cargo build
|
||||
--release` needs the build script first. **build.rs rejects only a missing file or the
|
||||
placeholder, not a stale or wrong binary**: re-run `scripts/build-ssm-plugin.sh` before every
|
||||
local release build (it is a no-op when the file already matches the pin, and rebuilds it
|
||||
otherwise — e.g. after a pin bump, or after a Windows release build signed it in place). `finalize-appimage.sh` asserts the shipped
|
||||
`usr/bin/session-manager-plugin` is an ELF that runs `--version`.
|
||||
- **Windows: our build is unsigned, so the release signs it with our certificate.** Tauri runs
|
||||
`signtool verify /pa` on each sidecar and hands the unsigned one to the sign command, in place
|
||||
under `app\src-tauri\binaries` before any bundle (both MSI and NSIS pack that file);
|
||||
`windows-sign.ps1`'s allowlist includes it (one more metered signature per release: 6).
|
||||
`windows-verify-signatures.ps1` requires exactly one `session-manager-plugin.exe` in the MSI and
|
||||
the file NSIS packed to be validly signed, timestamped, **by the same signing identity
|
||||
(subject + issuer) as the installer** — not the thumbprint: Artifact Signing certificates are
|
||||
short-lived and rotate — and the signing log to show it. The MSI must also carry
|
||||
`THIRD_PARTY_NOTICES`; on macOS a workflow step checks the `.app` holds the two-slice plugin in
|
||||
`Contents/MacOS` and the notices in `Contents/Resources`. Previews ship it unsigned, like the app.
|
||||
- **macOS: signed exactly like the app.** The app is not code-signed (no `APPLE_*`/`signingIdentity`
|
||||
in the workflows); Go's linker ad-hoc signs the arm64 slice (needed to run on Apple Silicon). If
|
||||
signing is ever configured, Tauri signs externalBin with the app's identity, inside-out.
|
||||
- **Licence:** `app/src-tauri/THIRD_PARTY_NOTICES` (bundled as a resource; `usr/lib/Triple-C/` in
|
||||
the AppImage, asserted by `finalize-appimage.sh`) carries the plugin's Apache-2.0 LICENSE and
|
||||
NOTICE, the commit we build, the repo's own `THIRD-PARTY` file verbatim, the licence of every
|
||||
vendored module linked in (`go list -deps` for linux/darwin/windows) and Go's own. Note that the
|
||||
repo's `THIRD-PARTY` file opens with the same "AWS Content" sentence as the binary packages.
|
||||
|
||||
### Windows code signing
|
||||
|
||||
Windows **releases** (`build-app.yml`) are signed with **Azure Artifact Signing**: the app binary,
|
||||
the MSI, the NSIS installer and its uninstaller. Three scripts do it, and "Verify signatures"
|
||||
the bundled `session-manager-plugin`, the MSI, the NSIS installer and its uninstaller. Three scripts do it, and "Verify signatures"
|
||||
fails the job if any of them is unsigned or untimestamped, so an unsigned installer cannot ship
|
||||
quietly. **PR previews are deliberately not signed**, and `build-app-preview.yml` must not
|
||||
reference the signing secrets. Two reasons: signing is metered (about 1000 signatures a month,
|
||||
@@ -1185,12 +1390,13 @@ without releasing (see Releases above).
|
||||
- `scripts/windows-sign.ps1` is the sign command: `signtool sign /dlib` with SHA-256 and the
|
||||
Microsoft timestamp server, retried. Credentials never reach a command line — the dlib reads
|
||||
`AZURE_TENANT_ID` / `AZURE_CLIENT_ID` / `AZURE_CLIENT_SECRET` from the environment.
|
||||
**It signs only an allowlist of what ships**: 5 signatures per release (the app binary twice,
|
||||
**It signs only an allowlist of what ships**: 6 signatures per release (the app binary twice,
|
||||
because Tauri re-patches it between the MSI and NSIS bundles; the MSI; the NSIS installer;
|
||||
and its uninstaller). Tauri also presents build-time tools, the WiX extension DLLs and NSIS
|
||||
its uninstaller; and the `session-manager-plugin` sidecar we build from source). Tauri also presents build-time tools, the WiX extension DLLs and NSIS
|
||||
plugins, and signing those would more than double the metered count for no user-visible
|
||||
benefit. If the app ever ships resource DLLs or sidecars, extend the
|
||||
allowlist, or they will go out unsigned. Tauri reports a failed sign command only as
|
||||
allowlist, or they will go out unsigned (as `session-manager-plugin`'s entry does; see the
|
||||
section above). Tauri reports a failed sign command only as
|
||||
"failed to run powershell", so the script keeps a transcript (`.code-signing/sign-output.log`,
|
||||
`signtool /debug` included), and the job prints it on failure.
|
||||
- `scripts/windows-verify-signatures.ps1` checks `signtool verify /pa` plus a timestamp on the
|
||||
|
||||
Generated
+27
@@ -367,6 +367,32 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-sdk-autoscaling"
|
||||
version = "1.144.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a08cb197f98dad7b1a3d4e7fcc94fbc13a66c2ea5465ba57fdb5dd00f865d6e8"
|
||||
dependencies = [
|
||||
"arc-swap",
|
||||
"aws-credential-types",
|
||||
"aws-runtime",
|
||||
"aws-smithy-async",
|
||||
"aws-smithy-http",
|
||||
"aws-smithy-json",
|
||||
"aws-smithy-observability",
|
||||
"aws-smithy-query",
|
||||
"aws-smithy-runtime",
|
||||
"aws-smithy-runtime-api",
|
||||
"aws-smithy-schema",
|
||||
"aws-smithy-types",
|
||||
"aws-smithy-xml",
|
||||
"aws-types",
|
||||
"fastrand",
|
||||
"http",
|
||||
"regex-lite",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-sdk-cloudwatchlogs"
|
||||
version = "1.165.0"
|
||||
@@ -7466,6 +7492,7 @@ dependencies = [
|
||||
"aws-config",
|
||||
"aws-credential-types",
|
||||
"aws-runtime",
|
||||
"aws-sdk-autoscaling",
|
||||
"aws-sdk-cloudwatchlogs",
|
||||
"aws-sdk-ecs",
|
||||
"aws-sdk-efs",
|
||||
|
||||
@@ -80,6 +80,9 @@ aws-runtime = { version = "1.10.4", default-features = false }
|
||||
# The default sleep and time source for the hand-built SdkConfig (no env/profile
|
||||
# loading); the version and feature already in the tree.
|
||||
aws-smithy-async = { version = "1.3.1", default-features = false, features = ["rt-tokio"] }
|
||||
# commands/environment_commands.rs: the pre-apply check that a user's EC2 names
|
||||
# (Auto Scaling group) are not another onboarding's (phase 7, R3).
|
||||
aws-sdk-autoscaling = { version = "1.144.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
|
||||
aws-sdk-s3 = { version = "1.159.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
|
||||
|
||||
# aws/tofu.rs: `setsid` puts the detached OpenTofu runner in its own session,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -14,6 +14,8 @@
|
||||
|
||||
#[path = "src/command_census.rs"]
|
||||
mod command_census;
|
||||
#[path = "src/sidecar_placeholder.rs"]
|
||||
mod sidecar_placeholder;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
@@ -79,6 +81,7 @@ fn main() {
|
||||
}
|
||||
|
||||
prune_permissions(&commands);
|
||||
ensure_ssm_sidecar();
|
||||
|
||||
// `AppManifest::commands` takes `&'static [&'static str]` and the struct is `Copy`, so
|
||||
// there is no owned form; leaking is fine in a process that exits right after.
|
||||
@@ -206,3 +209,50 @@ fn check_tauri_config() {
|
||||
fail("capabilities declared outside capabilities/", &problems, LAYOUT_HINT);
|
||||
}
|
||||
}
|
||||
|
||||
/// `bundle.externalBin` names `binaries/session-manager-plugin`, and tauri-build stops on a
|
||||
/// missing `binaries/session-manager-plugin-<target-triple>[.exe]` for every build, tests
|
||||
/// included. The real one is built from AWS's source by `scripts/build-ssm-plugin.sh` (`.ps1` on
|
||||
/// Windows), which CI runs before `tauri build`.
|
||||
///
|
||||
/// - **Release profile:** the real plugin is required. A missing file or a placeholder stops
|
||||
/// the build here, so a placeholder can never ship.
|
||||
/// - **Debug profile** (`cargo test`, `cargo check`, `tauri dev`): a missing file is replaced
|
||||
/// by a placeholder (`src/sidecar_placeholder.rs`) so no test run needs Go or the network. The app's
|
||||
/// `find_plugin` skips a placeholder and falls back to an installed plugin.
|
||||
fn ensure_ssm_sidecar() {
|
||||
let target = std::env::var("TARGET").expect("cargo sets TARGET");
|
||||
let ext = if target.contains("windows") { ".exe" } else { "" };
|
||||
let path = Path::new("binaries").join(format!("session-manager-plugin-{target}{ext}"));
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
let release = std::env::var("PROFILE").as_deref() == Ok("release");
|
||||
let head = std::fs::read(&path).ok().map(|b| sidecar_placeholder::is_placeholder(&b));
|
||||
let build = if target.contains("windows") {
|
||||
"scripts\\build-ssm-plugin.ps1"
|
||||
} else {
|
||||
"scripts/build-ssm-plugin.sh"
|
||||
};
|
||||
match (head, release) {
|
||||
(Some(false), _) => {}
|
||||
(state, true) => fail(
|
||||
"the AWS Session Manager plugin sidecar is missing",
|
||||
&[format!(
|
||||
"{} is {} (release builds bundle the real plugin)",
|
||||
path.display(),
|
||||
if state.is_some() { "a debug-build placeholder" } else { "missing" }
|
||||
)],
|
||||
&format!("Run {build} {target} from the repository root first."),
|
||||
),
|
||||
(None, false) => {
|
||||
std::fs::create_dir_all("binaries").expect("create src-tauri/binaries");
|
||||
std::fs::write(&path, sidecar_placeholder::contents())
|
||||
.unwrap_or_else(|e| panic!("cannot write {}: {e}", path.display()));
|
||||
println!(
|
||||
"cargo:warning=no {} - wrote a placeholder (debug only; AWS workspaces use an \
|
||||
installed plugin). Run {build} to build the real one.",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
(Some(true), false) => {}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
"dialog:allow-open",
|
||||
"dialog:allow-save",
|
||||
"allow-check-docker",
|
||||
"allow-check-docker-status",
|
||||
"allow-check-image-exists",
|
||||
"allow-build-image",
|
||||
"allow-get-container-info",
|
||||
@@ -22,6 +23,8 @@
|
||||
"allow-aws-task-log",
|
||||
"allow-aws-workspace-status",
|
||||
"allow-aws-environment-max-hours",
|
||||
"allow-aws-ec2-capacity",
|
||||
"allow-ec2-fit-problem",
|
||||
"allow-remove-aws-resources",
|
||||
"allow-rebuild-project-container",
|
||||
"allow-reconcile-project-statuses",
|
||||
@@ -82,6 +85,7 @@
|
||||
"allow-cancel-aws-deployment",
|
||||
"allow-push-aws-saved-state",
|
||||
"allow-onboard-aws-user",
|
||||
"allow-list-ec2-instance-types",
|
||||
"allow-deploy-aws-permissions-boundary",
|
||||
"allow-save-aws-admin-bundle",
|
||||
"allow-detect-host-timezone",
|
||||
@@ -92,6 +96,9 @@
|
||||
"allow-terminal-input",
|
||||
"allow-terminal-resize",
|
||||
"allow-close-terminal-session",
|
||||
"allow-list-workspace-terminals",
|
||||
"allow-reattach-terminal-session",
|
||||
"allow-end-workspace-terminal",
|
||||
"allow-paste-image-to-terminal",
|
||||
"allow-upload-host-file-to-terminal",
|
||||
"allow-start-audio-bridge",
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-cost-estimate":{"identifier":"allow-aws-cost-estimate","description":"Enables the aws_cost_estimate command without any pre-configured scope.","commands":{"allow":["aws_cost_estimate"],"deny":[]}},"allow-aws-environment-max-hours":{"identifier":"allow-aws-environment-max-hours","description":"Enables the aws_environment_max_hours command without any pre-configured scope.","commands":{"allow":["aws_environment_max_hours"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-aws-task-log":{"identifier":"allow-aws-task-log","description":"Enables the aws_task_log command without any pre-configured scope.","commands":{"allow":["aws_task_log"],"deny":[]}},"allow-aws-workspace-status":{"identifier":"allow-aws-workspace-status","description":"Enables the aws_workspace_status command without any pre-configured scope.","commands":{"allow":["aws_workspace_status"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command without any pre-configured scope.","commands":{"allow":["cancel_aws_deployment"],"deny":[]}},"allow-cancel-claude-token":{"identifier":"allow-cancel-claude-token","description":"Enables the cancel_claude_token cLine truncated
|
||||
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-cost-estimate":{"identifier":"allow-aws-cost-estimate","description":"Enables the aws_cost_estimate command without any pre-configured scope.","commands":{"allow":["aws_cost_estimate"],"deny":[]}},"allow-aws-ec2-capacity":{"identifier":"allow-aws-ec2-capacity","description":"Enables the aws_ec2_capacity command without any pre-configured scope.","commands":{"allow":["aws_ec2_capacity"],"deny":[]}},"allow-aws-environment-max-hours":{"identifier":"allow-aws-environment-max-hours","description":"Enables the aws_environment_max_hours command without any pre-configured scope.","commands":{"allow":["aws_environment_max_hours"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-aws-task-log":{"identifier":"allow-aws-task-log","description":"Enables the aws_task_log command without any pre-configured scope.","commands":{"allow":["aws_task_log"],"deny":[]}},"allow-aws-workspace-status":{"identifier":"allow-aws-workspace-status","description":"Enables the aws_workspace_status command without any pre-configured scope.","commands":{"allow":["aws_workspace_status"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command withoutLine truncated
|
||||
@@ -1 +1 @@
|
||||
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
|
||||
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
|
||||
@@ -422,6 +422,12 @@
|
||||
"const": "allow-aws-cost-estimate",
|
||||
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the aws_ec2_capacity command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-aws-ec2-capacity",
|
||||
"markdownDescription": "Enables the aws_ec2_capacity command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -506,6 +512,12 @@
|
||||
"const": "allow-check-docker",
|
||||
"markdownDescription": "Enables the check_docker command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the check_docker_status command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-check-docker-status",
|
||||
"markdownDescription": "Enables the check_docker_status command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the check_for_updates command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -644,6 +656,18 @@
|
||||
"const": "allow-download-container-file",
|
||||
"markdownDescription": "Enables the download_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the ec2_fit_problem command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-ec2-fit-problem",
|
||||
"markdownDescription": "Enables the ec2_fit_problem command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the end_workspace_terminal command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-end-workspace-terminal",
|
||||
"markdownDescription": "Enables the end_workspace_terminal command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the export_settings command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -824,6 +848,12 @@
|
||||
"const": "allow-list-container-files",
|
||||
"markdownDescription": "Enables the list_container_files command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_ec2_instance_types command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-list-ec2-instance-types",
|
||||
"markdownDescription": "Enables the list_ec2_instance_types command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_marketplace_snapshots command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -854,6 +884,12 @@
|
||||
"const": "allow-list-scheduled-tasks",
|
||||
"markdownDescription": "Enables the list_scheduled_tasks command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_workspace_terminals command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-list-workspace-terminals",
|
||||
"markdownDescription": "Enables the list_workspace_terminals command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the marketplace_gh_host_available command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -950,6 +986,12 @@
|
||||
"const": "allow-read-container-file",
|
||||
"markdownDescription": "Enables the read_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the reattach_terminal_session command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-reattach-terminal-session",
|
||||
"markdownDescription": "Enables the reattach_terminal_session command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the rebuild_project_container command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1394,6 +1436,12 @@
|
||||
"const": "deny-aws-cost-estimate",
|
||||
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the aws_ec2_capacity command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-aws-ec2-capacity",
|
||||
"markdownDescription": "Denies the aws_ec2_capacity command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1478,6 +1526,12 @@
|
||||
"const": "deny-check-docker",
|
||||
"markdownDescription": "Denies the check_docker command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the check_docker_status command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-check-docker-status",
|
||||
"markdownDescription": "Denies the check_docker_status command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the check_for_updates command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1616,6 +1670,18 @@
|
||||
"const": "deny-download-container-file",
|
||||
"markdownDescription": "Denies the download_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the ec2_fit_problem command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-ec2-fit-problem",
|
||||
"markdownDescription": "Denies the ec2_fit_problem command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the end_workspace_terminal command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-end-workspace-terminal",
|
||||
"markdownDescription": "Denies the end_workspace_terminal command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the export_settings command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1796,6 +1862,12 @@
|
||||
"const": "deny-list-container-files",
|
||||
"markdownDescription": "Denies the list_container_files command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_ec2_instance_types command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-list-ec2-instance-types",
|
||||
"markdownDescription": "Denies the list_ec2_instance_types command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_marketplace_snapshots command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1826,6 +1898,12 @@
|
||||
"const": "deny-list-scheduled-tasks",
|
||||
"markdownDescription": "Denies the list_scheduled_tasks command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_workspace_terminals command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-list-workspace-terminals",
|
||||
"markdownDescription": "Denies the list_workspace_terminals command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the marketplace_gh_host_available command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1922,6 +2000,12 @@
|
||||
"const": "deny-read-container-file",
|
||||
"markdownDescription": "Denies the read_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the reattach_terminal_session command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-reattach-terminal-session",
|
||||
"markdownDescription": "Denies the reattach_terminal_session command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the rebuild_project_container command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
|
||||
@@ -422,6 +422,12 @@
|
||||
"const": "allow-aws-cost-estimate",
|
||||
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the aws_ec2_capacity command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-aws-ec2-capacity",
|
||||
"markdownDescription": "Enables the aws_ec2_capacity command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -506,6 +512,12 @@
|
||||
"const": "allow-check-docker",
|
||||
"markdownDescription": "Enables the check_docker command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the check_docker_status command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-check-docker-status",
|
||||
"markdownDescription": "Enables the check_docker_status command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the check_for_updates command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -644,6 +656,18 @@
|
||||
"const": "allow-download-container-file",
|
||||
"markdownDescription": "Enables the download_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the ec2_fit_problem command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-ec2-fit-problem",
|
||||
"markdownDescription": "Enables the ec2_fit_problem command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the end_workspace_terminal command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-end-workspace-terminal",
|
||||
"markdownDescription": "Enables the end_workspace_terminal command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the export_settings command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -824,6 +848,12 @@
|
||||
"const": "allow-list-container-files",
|
||||
"markdownDescription": "Enables the list_container_files command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_ec2_instance_types command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-list-ec2-instance-types",
|
||||
"markdownDescription": "Enables the list_ec2_instance_types command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_marketplace_snapshots command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -854,6 +884,12 @@
|
||||
"const": "allow-list-scheduled-tasks",
|
||||
"markdownDescription": "Enables the list_scheduled_tasks command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the list_workspace_terminals command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-list-workspace-terminals",
|
||||
"markdownDescription": "Enables the list_workspace_terminals command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the marketplace_gh_host_available command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -950,6 +986,12 @@
|
||||
"const": "allow-read-container-file",
|
||||
"markdownDescription": "Enables the read_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the reattach_terminal_session command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-reattach-terminal-session",
|
||||
"markdownDescription": "Enables the reattach_terminal_session command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Enables the rebuild_project_container command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1394,6 +1436,12 @@
|
||||
"const": "deny-aws-cost-estimate",
|
||||
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the aws_ec2_capacity command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-aws-ec2-capacity",
|
||||
"markdownDescription": "Denies the aws_ec2_capacity command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1478,6 +1526,12 @@
|
||||
"const": "deny-check-docker",
|
||||
"markdownDescription": "Denies the check_docker command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the check_docker_status command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-check-docker-status",
|
||||
"markdownDescription": "Denies the check_docker_status command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the check_for_updates command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1616,6 +1670,18 @@
|
||||
"const": "deny-download-container-file",
|
||||
"markdownDescription": "Denies the download_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the ec2_fit_problem command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-ec2-fit-problem",
|
||||
"markdownDescription": "Denies the ec2_fit_problem command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the end_workspace_terminal command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-end-workspace-terminal",
|
||||
"markdownDescription": "Denies the end_workspace_terminal command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the export_settings command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1796,6 +1862,12 @@
|
||||
"const": "deny-list-container-files",
|
||||
"markdownDescription": "Denies the list_container_files command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_ec2_instance_types command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-list-ec2-instance-types",
|
||||
"markdownDescription": "Denies the list_ec2_instance_types command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_marketplace_snapshots command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1826,6 +1898,12 @@
|
||||
"const": "deny-list-scheduled-tasks",
|
||||
"markdownDescription": "Denies the list_scheduled_tasks command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the list_workspace_terminals command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-list-workspace-terminals",
|
||||
"markdownDescription": "Denies the list_workspace_terminals command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the marketplace_gh_host_available command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
@@ -1922,6 +2000,12 @@
|
||||
"const": "deny-read-container-file",
|
||||
"markdownDescription": "Denies the read_container_file command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the reattach_terminal_session command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-reattach-terminal-session",
|
||||
"markdownDescription": "Denies the reattach_terminal_session command without any pre-configured scope."
|
||||
},
|
||||
{
|
||||
"description": "Denies the rebuild_project_container command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
|
||||
@@ -54,6 +54,8 @@
|
||||
//! ([`IDLE_TIMEOUT`]).
|
||||
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::host::{AttachedExec, ContainerTarget, ExecChunk};
|
||||
@@ -158,19 +160,25 @@ impl PortForward {
|
||||
container_port: u16,
|
||||
family: PortFamily,
|
||||
) -> Result<Self, std::io::Error> {
|
||||
Self::bind_with(container, host_port, container_port, family, Gate::SignInCallback).await
|
||||
Self::bind_with(container, host_port, container_port, family, Gate::SignInCallback, None).await
|
||||
}
|
||||
|
||||
/// As [`bind`](Self::bind), for a project's port mapping on a host with
|
||||
/// no Docker port publishing (AWS): every connection is carried as is
|
||||
/// (see [`Gate::None`]).
|
||||
///
|
||||
/// Inside the workspace each connection goes to whichever loopback
|
||||
/// family has a listener on `container_port` ([`mapping_command`]), so a
|
||||
/// server bound to `[::1]` only is reached as well as one on
|
||||
/// `127.0.0.1`/`0.0.0.0`. `on_refused` is called (once per forward) the
|
||||
/// first time a connection finds nothing listening there.
|
||||
pub async fn bind_mapping(
|
||||
container: ContainerTarget,
|
||||
host_port: u16,
|
||||
container_port: u16,
|
||||
family: PortFamily,
|
||||
on_refused: Option<RefusedNotice>,
|
||||
) -> Result<Self, std::io::Error> {
|
||||
Self::bind_with(container, host_port, container_port, family, Gate::None).await
|
||||
Self::bind_with(container, host_port, container_port, PortFamily::Dual, Gate::None, on_refused).await
|
||||
}
|
||||
|
||||
async fn bind_with(
|
||||
@@ -179,6 +187,7 @@ impl PortForward {
|
||||
container_port: u16,
|
||||
family: PortFamily,
|
||||
gate: Gate,
|
||||
on_refused: Option<RefusedNotice>,
|
||||
) -> Result<Self, std::io::Error> {
|
||||
// SECURITY BOUNDARY: the host side binds loopback ONLY — 127.0.0.1 and
|
||||
// ::1, never 0.0.0.0 / ::. Everything reachable through this socket is
|
||||
@@ -222,8 +231,12 @@ impl PortForward {
|
||||
}
|
||||
};
|
||||
|
||||
let target = family.socat_target(container_port);
|
||||
let task = tokio::spawn(accept_loop(container, port, target, v4, v6, gate));
|
||||
let cmd = match gate {
|
||||
Gate::SignInCallback => socat_command(&family.socat_target(container_port)),
|
||||
Gate::None => mapping_command(container_port),
|
||||
};
|
||||
let refused = on_refused.map(|f| Arc::new(RefusedOnce { notice: f, fired: AtomicBool::new(false) }));
|
||||
let task = tokio::spawn(accept_loop(container, port, cmd, v4, v6, gate, refused));
|
||||
|
||||
Ok(Self {
|
||||
port,
|
||||
@@ -273,10 +286,11 @@ impl Gate {
|
||||
async fn accept_loop(
|
||||
container: ContainerTarget,
|
||||
port: u16,
|
||||
target: String,
|
||||
cmd: Vec<String>,
|
||||
v4: TcpListener,
|
||||
v6: Option<TcpListener>,
|
||||
gate: Gate,
|
||||
refused: Option<Arc<RefusedOnce>>,
|
||||
) {
|
||||
let mut conns: JoinSet<()> = JoinSet::new();
|
||||
|
||||
@@ -313,17 +327,18 @@ async fn accept_loop(
|
||||
match gate {
|
||||
Gate::SignInCallback => conns.spawn(tunnel_connection(
|
||||
container.clone(),
|
||||
target.clone(),
|
||||
cmd.clone(),
|
||||
stream,
|
||||
port,
|
||||
)),
|
||||
Gate::None => conns.spawn(tunnel_labelled(
|
||||
gate.label(),
|
||||
container.clone(),
|
||||
target.clone(),
|
||||
cmd.clone(),
|
||||
stream,
|
||||
port,
|
||||
Vec::new(),
|
||||
refused.clone(),
|
||||
)),
|
||||
};
|
||||
}
|
||||
@@ -353,7 +368,7 @@ async fn accept_optional(
|
||||
/// reaches the container at all — not even a `docker exec`.
|
||||
async fn tunnel_connection(
|
||||
container: ContainerTarget,
|
||||
target: String,
|
||||
cmd: Vec<String>,
|
||||
mut stream: TcpStream,
|
||||
port: u16,
|
||||
) {
|
||||
@@ -387,7 +402,7 @@ async fn tunnel_connection(
|
||||
}
|
||||
|
||||
// The bytes already off the socket go back on the wire first, byte-exact.
|
||||
tunnel_connection_with_prelude(container, target, stream, port, head.into_buffer()).await
|
||||
tunnel_labelled("Auth bridge", container, cmd, stream, port, head.into_buffer(), None).await
|
||||
}
|
||||
|
||||
/// What the first bytes of an accepted connection turned out to be.
|
||||
@@ -586,19 +601,20 @@ pub async fn tunnel_connection_with_prelude(
|
||||
port: u16,
|
||||
prelude: Vec<u8>,
|
||||
) {
|
||||
tunnel_labelled("Auth bridge", container, target, stream, port, prelude).await
|
||||
tunnel_labelled("Auth bridge", container, socat_command(&target), stream, port, prelude, None).await
|
||||
}
|
||||
|
||||
/// [`tunnel_connection_with_prelude`], logging under `label`.
|
||||
async fn tunnel_labelled(
|
||||
label: &'static str,
|
||||
container: ContainerTarget,
|
||||
target: String,
|
||||
cmd: Vec<String>,
|
||||
stream: TcpStream,
|
||||
port: u16,
|
||||
prelude: Vec<u8>,
|
||||
refused: Option<Arc<RefusedOnce>>,
|
||||
) {
|
||||
let cmd = vec!["socat".to_string(), "-".to_string(), target.clone()];
|
||||
let target = cmd.last().cloned().unwrap_or_default();
|
||||
|
||||
let AttachedExec {
|
||||
mut output,
|
||||
@@ -673,6 +689,12 @@ async fn tunnel_labelled(
|
||||
}
|
||||
}
|
||||
Ok(ExecChunk::StdErr(message)) => {
|
||||
if let Some(r) = &refused {
|
||||
if is_connection_refused(&message) {
|
||||
log::warn!("{}: host port {}: nothing is listening in the workspace", label, port);
|
||||
r.fire();
|
||||
}
|
||||
}
|
||||
log::debug!(
|
||||
"{}: socat stderr for port {}: {}", label,
|
||||
port,
|
||||
@@ -692,10 +714,113 @@ async fn tunnel_labelled(
|
||||
drop(upstream);
|
||||
}
|
||||
|
||||
/// Called the first time a port mapping's connection finds nothing
|
||||
/// listening on its workspace port.
|
||||
pub type RefusedNotice = Arc<dyn Fn() + Send + Sync>;
|
||||
|
||||
/// A [`RefusedNotice`] that fires once per forward.
|
||||
struct RefusedOnce {
|
||||
notice: RefusedNotice,
|
||||
fired: AtomicBool,
|
||||
}
|
||||
|
||||
impl RefusedOnce {
|
||||
fn fire(&self) {
|
||||
if !self.fired.swap(true, Ordering::SeqCst) {
|
||||
(self.notice)();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `socat` relaying stdio to `target`.
|
||||
fn socat_command(target: &str) -> Vec<String> {
|
||||
vec!["socat".to_string(), "-".to_string(), target.to_string()]
|
||||
}
|
||||
|
||||
/// Whether socat's stderr says the connect was refused (nothing listening).
|
||||
fn is_connection_refused(stderr: &[u8]) -> bool {
|
||||
String::from_utf8_lossy(stderr).contains("Connection refused")
|
||||
}
|
||||
|
||||
/// The in-workspace command of one port-mapping connection.
|
||||
///
|
||||
/// Docker publishes to the container's interface; a mapping reaches its
|
||||
/// loopback instead, where a server may listen on either family (Vite, or
|
||||
/// Node's `localhost` where it resolves to `::1`, listen on `[::1]` only).
|
||||
/// So the command picks, from `/proc/net/tcp{,6}` (the image has no `ss`),
|
||||
/// the family with a listener on `port`: `127.0.0.1` when one listens on
|
||||
/// `127.0.0.1` or `0.0.0.0`, else `[::1]` when one listens on `::1` or `::`.
|
||||
/// Nothing found: `127.0.0.1` anyway, and socat's "Connection refused" is
|
||||
/// what tells the host nothing listens. Reading `/proc` rather than trying
|
||||
/// a connect first means a server never sees a probe connection.
|
||||
pub(crate) fn mapping_command(port: u16) -> Vec<String> {
|
||||
// `port` is a u16: interpolating it into the script is safe.
|
||||
let script = format!(
|
||||
"h=$(printf ':%04X' {port}); \
|
||||
l() {{ awk -v a=\"$2$h\" -v b=\"$3$h\" '$4==\"0A\" && ($2==a || $2==b) {{f=1}} END {{exit !f}}' \"$1\" 2>/dev/null; }}; \
|
||||
if l /proc/net/tcp 0100007F 00000000; then exec socat - TCP:127.0.0.1:{port}; fi; \
|
||||
if l /proc/net/tcp6 00000000000000000000000001000000 00000000000000000000000000000000; \
|
||||
then exec socat - TCP6:[::1]:{port}; fi; \
|
||||
exec socat - TCP:127.0.0.1:{port}"
|
||||
);
|
||||
vec!["sh".to_string(), "-c".to_string(), script]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_mapping_command_names_only_its_port_and_both_loopbacks() {
|
||||
let cmd = mapping_command(8080);
|
||||
assert_eq!(&cmd[..2], ["sh", "-c"]);
|
||||
let s = &cmd[2];
|
||||
assert!(s.contains("printf ':%04X' 8080"), "{s}");
|
||||
assert!(s.contains("TCP:127.0.0.1:8080") && s.contains("TCP6:[::1]:8080"), "{s}");
|
||||
// v4 first; the last resort is v4 too.
|
||||
assert!(s.find("TCP:127.0.0.1").unwrap() < s.find("TCP6:[::1]").unwrap());
|
||||
assert!(s.trim_end().ends_with("exec socat - TCP:127.0.0.1:8080"), "{s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn socats_refusal_is_recognised() {
|
||||
assert!(is_connection_refused(
|
||||
b"2026/11/17 10:00:00 socat[42] E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused\n"
|
||||
));
|
||||
assert!(!is_connection_refused(b"socat[42] N exiting with status 0"));
|
||||
}
|
||||
|
||||
/// The command picks the family from `/proc/net/tcp{,6}`: run it against
|
||||
/// real listeners where `sh`, `awk` and `socat` exist (skipped elsewhere).
|
||||
#[tokio::test]
|
||||
async fn a_mapping_command_reaches_a_v4_or_a_v6_only_listener() {
|
||||
let have = |b: &str| std::process::Command::new("sh").args(["-c", &format!("command -v {b}")]).output()
|
||||
.is_ok_and(|o| o.status.success());
|
||||
if !(have("awk") && have("socat") && std::path::Path::new("/proc/net/tcp").exists()) {
|
||||
eprintln!("skipped: needs sh, awk, socat and /proc/net");
|
||||
return;
|
||||
}
|
||||
for addr in ["127.0.0.1:0", "[::1]:0"] {
|
||||
let Ok(l) = TcpListener::bind(addr).await else { continue };
|
||||
let port = l.local_addr().unwrap().port();
|
||||
tokio::spawn(async move {
|
||||
if let Ok((mut s, _)) = l.accept().await {
|
||||
let _ = s.write_all(b"hello").await;
|
||||
}
|
||||
});
|
||||
let cmd = mapping_command(port);
|
||||
let out = tokio::process::Command::new(&cmd[0]).args(&cmd[1..])
|
||||
.stdin(std::process::Stdio::null()).output().await.unwrap();
|
||||
assert_eq!(String::from_utf8_lossy(&out.stdout), "hello", "{addr}: {}", String::from_utf8_lossy(&out.stderr));
|
||||
}
|
||||
// Nothing listening: refused, in socat's words.
|
||||
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
|
||||
let cmd = mapping_command(port);
|
||||
let out = tokio::process::Command::new(&cmd[0]).args(&cmd[1..])
|
||||
.stdin(std::process::Stdio::null()).output().await.unwrap();
|
||||
assert!(is_connection_refused(&out.stderr), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
}
|
||||
|
||||
fn head(lines: &[&str]) -> String {
|
||||
format!("{}\r\n\r\n", lines.join("\r\n"))
|
||||
}
|
||||
|
||||
@@ -29,8 +29,9 @@ use serde_json::{json, Map, Value};
|
||||
use super::error::{classify_sdk_error, AwsError};
|
||||
use super::identity::CallerIdentity;
|
||||
use super::tofu_modules::{
|
||||
environment_parameter_name, owner_key, state_bucket_name, state_key, user_role_names,
|
||||
validate_environment_name, Deployment, BOUNDARY_POLICY_NAME,
|
||||
environment_parameter_name, owner_key, state_bucket_name, state_key, user_ec2_stem,
|
||||
user_record_parameter_name, user_role_names, validate_ec2_user_tag, validate_environment_name,
|
||||
Deployment, BOUNDARY_POLICY_NAME, EC2_MAX_INSTANCES,
|
||||
};
|
||||
use crate::models::host_connection::validate_supported_region;
|
||||
|
||||
@@ -309,15 +310,97 @@ pub enum EnvironmentAction {
|
||||
#[serde(tag = "state", rename_all = "snake_case")]
|
||||
pub enum OnboardingState {
|
||||
/// Their task role exists and, where the bucket is readable, their state
|
||||
/// records a finished apply (its `onboarding_complete` output).
|
||||
Joined,
|
||||
/// records a finished apply (its `onboarding_complete` output). `ec2` is
|
||||
/// their EC2 capacity, from their user record.
|
||||
Joined { ec2: Option<UserCompute> },
|
||||
NotJoined,
|
||||
/// A local runner, or a recent lock.
|
||||
InProgress { lock: Option<LockInfo> },
|
||||
/// The last onboarding run didn't finish: a stale lock, a failed run on
|
||||
/// this machine, or a state without the completion output. Join is a
|
||||
/// re-apply and finishes it.
|
||||
Incomplete { lock: Option<LockInfo> },
|
||||
/// re-apply and finishes it; `ec2` is what their record still holds, so
|
||||
/// the re-apply can keep it.
|
||||
Incomplete {
|
||||
lock: Option<LockInfo>,
|
||||
ec2: Option<UserCompute>,
|
||||
},
|
||||
}
|
||||
|
||||
impl OnboardingState {
|
||||
fn with_ec2(self, compute: Option<UserCompute>) -> Self {
|
||||
match self {
|
||||
OnboardingState::Joined { .. } => OnboardingState::Joined { ec2: compute },
|
||||
OnboardingState::Incomplete { lock, .. } => OnboardingState::Incomplete { lock, ec2: compute },
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A user's EC2 capacity in one environment: their validated user record
|
||||
/// (`tofu_modules::user_record_parameter_name`). A missing record means no
|
||||
/// NEW EC2 runs there, not that nothing runs (teardown removes it first).
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct UserCompute {
|
||||
pub instance_type: String,
|
||||
pub max_instances: u32,
|
||||
/// The `triple-c-user` value the cluster is named after.
|
||||
pub user_tag: String,
|
||||
pub cluster_arn: String,
|
||||
pub capacity_provider: String,
|
||||
}
|
||||
|
||||
/// Validate a user record read from `user_record_parameter_name(env, …)`:
|
||||
/// schema 1, a type the app's table knows, 1 to `EC2_MAX_INSTANCES`
|
||||
/// instances (absent in records written before the field: 1), a valid user
|
||||
/// tag, and the cluster and capacity provider of exactly that tag's per-user
|
||||
/// names in `account` and `region`. The error names the field, never its
|
||||
/// value.
|
||||
pub fn parse_user_record(
|
||||
env: &str,
|
||||
value: &str,
|
||||
account: &str,
|
||||
region: &str,
|
||||
) -> Result<UserCompute, String> {
|
||||
if value.len() > MAX_UNTRUSTED_BYTES {
|
||||
return Err("the record is larger than 4 KiB".into());
|
||||
}
|
||||
let parsed: Value =
|
||||
serde_json::from_str(value).map_err(|_| "the record is not JSON".to_string())?;
|
||||
let obj = parsed
|
||||
.as_object()
|
||||
.ok_or_else(|| "the record is not a JSON object".to_string())?;
|
||||
if field(obj, "schema")?.as_u64() != Some(RECORD_SCHEMA) {
|
||||
return Err("schema is not 1".into());
|
||||
}
|
||||
let instance_type = str_field(obj, "instance_type")?;
|
||||
if crate::models::project::ec2_instance_spec(instance_type).is_none() {
|
||||
return Err("instance_type is not a supported instance type".into());
|
||||
}
|
||||
let max_instances = match obj.get("max_instances") {
|
||||
None => 1,
|
||||
Some(v) => v
|
||||
.as_u64()
|
||||
.and_then(|v| u32::try_from(v).ok())
|
||||
.filter(|v| (1..=EC2_MAX_INSTANCES).contains(v))
|
||||
.ok_or_else(|| format!("max_instances is not a number from 1 to {EC2_MAX_INSTANCES}"))?,
|
||||
};
|
||||
let user_tag = validate_ec2_user_tag(str_field(obj, "user_tag")?)
|
||||
.map_err(|_| "user_tag is not a valid triple-c-user value".to_string())?;
|
||||
let stem = user_ec2_stem(env, &user_tag);
|
||||
let cluster_arn = str_field(obj, "cluster_arn")?;
|
||||
if cluster_arn != format!("arn:aws:ecs:{region}:{account}:cluster/{stem}") {
|
||||
return Err("cluster_arn is not this user's cluster in this account and region".into());
|
||||
}
|
||||
if str_field(obj, "capacity_provider")? != stem {
|
||||
return Err("capacity_provider is not this user's capacity provider".into());
|
||||
}
|
||||
Ok(UserCompute {
|
||||
instance_type: instance_type.to_string(),
|
||||
max_instances,
|
||||
user_tag,
|
||||
cluster_arn: cluster_arn.to_string(),
|
||||
capacity_provider: stem,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
@@ -565,9 +648,12 @@ fn onboarding_from_runs(
|
||||
Some(OnboardingState::InProgress { lock })
|
||||
} else if lock.is_some() {
|
||||
actions.push(Unlock);
|
||||
Some(OnboardingState::Incomplete { lock })
|
||||
Some(OnboardingState::Incomplete { lock, ec2: None })
|
||||
} else if run.failed || run.errored_state {
|
||||
Some(OnboardingState::Incomplete { lock: None })
|
||||
Some(OnboardingState::Incomplete {
|
||||
lock: None,
|
||||
ec2: None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -597,6 +683,8 @@ pub trait DetectApi: Send + Sync {
|
||||
account: &str,
|
||||
) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
|
||||
fn role_exists(&self, name: &str) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
|
||||
/// An SSM String parameter (a user record).
|
||||
fn parameter(&self, name: &str) -> impl Future<Output = Result<Param, AwsError>> + Send;
|
||||
/// A state object's contents, capped; `None` when it is missing, can't
|
||||
/// be read or is too large.
|
||||
fn state_document(
|
||||
@@ -675,8 +763,13 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
|
||||
// role), which is not Joined.
|
||||
None if objects.as_ref().is_some_and(|o| o.has_state(&user_state)) => {
|
||||
match d.state_document(bucket, &user_state).await? {
|
||||
Some(text) if onboarding_complete(&text) => OnboardingState::Joined,
|
||||
_ => OnboardingState::Incomplete { lock: None },
|
||||
Some(text) if onboarding_complete(&text) => {
|
||||
OnboardingState::Joined { ec2: None }
|
||||
}
|
||||
_ => OnboardingState::Incomplete {
|
||||
lock: None,
|
||||
ec2: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
// Without a state here (a workspace user, who can't list the
|
||||
@@ -685,11 +778,20 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
|
||||
None => {
|
||||
let (task_role, _) = user_role_names(&env.name, &key);
|
||||
match d.role_exists(&task_role).await? {
|
||||
Knowable::Yes => OnboardingState::Joined,
|
||||
Knowable::Yes => OnboardingState::Joined { ec2: None },
|
||||
Knowable::No | Knowable::Unknown => OnboardingState::NotJoined,
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Some(state) = env.onboarding.take() {
|
||||
let compute = match state {
|
||||
OnboardingState::Joined { .. } | OnboardingState::Incomplete { .. } => {
|
||||
user_compute(d, &env.name, &key, &who.account, region).await?
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
env.onboarding = Some(state.with_ec2(compute));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(EnvironmentScan {
|
||||
@@ -703,6 +805,234 @@ pub(crate) async fn scan_in_bucket<D: DetectApi>(
|
||||
})
|
||||
}
|
||||
|
||||
/// An SSM parameter read: its value, missing, or this caller can't read it.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Param {
|
||||
Value(String),
|
||||
Missing,
|
||||
Denied,
|
||||
}
|
||||
|
||||
/// A user record as read: none, valid, or present but unusable (unreadable
|
||||
/// or failing validation; the reason names a field, never a value).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum UserRecord {
|
||||
Absent,
|
||||
Valid(UserCompute),
|
||||
Unusable(String),
|
||||
}
|
||||
|
||||
impl UserRecord {
|
||||
pub fn valid(&self) -> Option<&UserCompute> {
|
||||
match self {
|
||||
UserRecord::Valid(c) => Some(c),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [`UserRecord`] from a read of `user_record_parameter_name(env, …)`.
|
||||
pub fn user_record_from(read: Param, env: &str, account: &str, region: &str) -> UserRecord {
|
||||
match read {
|
||||
Param::Missing => UserRecord::Absent,
|
||||
Param::Denied => UserRecord::Unusable("it can't be read with these credentials".into()),
|
||||
Param::Value(text) => match parse_user_record(env, &text, account, region) {
|
||||
Ok(c) => UserRecord::Valid(c),
|
||||
Err(reason) => UserRecord::Unusable(reason),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// The caller's EC2 capacity in `env` from their user record; `None` when
|
||||
/// there is no usable record (an unusable one is logged, never echoed).
|
||||
pub(crate) async fn user_compute<D: DetectApi>(
|
||||
d: &D,
|
||||
env: &str,
|
||||
owner_key: &str,
|
||||
account: &str,
|
||||
region: &str,
|
||||
) -> Result<Option<UserCompute>, AwsError> {
|
||||
let read = d.parameter(&user_record_parameter_name(env, owner_key)).await?;
|
||||
match user_record_from(read, env, account, region) {
|
||||
UserRecord::Valid(c) => Ok(Some(c)),
|
||||
UserRecord::Absent => Ok(None),
|
||||
UserRecord::Unusable(reason) => {
|
||||
log::warn!("Ignoring the EC2 user record for environment {env}: {reason}");
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Who holds a per-user EC2 name (`tofu_modules::user_ec2_stem`): an ECS
|
||||
/// cluster or Auto Scaling group of that name and its `triple-c:owner` tag.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct NameHolder {
|
||||
/// "ECS cluster" or "Auto Scaling group".
|
||||
pub kind: &'static str,
|
||||
pub owner: Option<String>,
|
||||
}
|
||||
|
||||
/// An ECS cluster or Auto Scaling group found by name: its status (clusters
|
||||
/// only) and tags.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub struct NamedResource {
|
||||
pub status: Option<String>,
|
||||
pub tags: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// What an onboarding apply asks AWS before it starts (R3): the caller's
|
||||
/// user record and whether the per-user EC2 names are taken. The cluster
|
||||
/// read needs `ecs:DescribeClusters` (tags included); the group read
|
||||
/// `autoscaling:DescribeAutoScalingGroups`, which only the environment-admin
|
||||
/// tier has.
|
||||
pub trait OnboardApi: Send + Sync {
|
||||
fn parameter(&self, name: &str) -> impl Future<Output = Result<Param, AwsError>> + Send;
|
||||
fn cluster(
|
||||
&self,
|
||||
name: &str,
|
||||
) -> impl Future<Output = Result<Option<NamedResource>, AwsError>> + Send;
|
||||
fn group(
|
||||
&self,
|
||||
name: &str,
|
||||
) -> impl Future<Output = Result<Option<NamedResource>, AwsError>> + Send;
|
||||
}
|
||||
|
||||
const OWNER_TAG: &str = "triple-c:owner";
|
||||
|
||||
fn owner_of(r: &NamedResource) -> Option<String> {
|
||||
r.tags
|
||||
.iter()
|
||||
.find(|(k, _)| k == OWNER_TAG)
|
||||
.map(|(_, v)| v.clone())
|
||||
}
|
||||
|
||||
/// The cluster holding `stem`, if any. Every status but INACTIVE (a deleted
|
||||
/// cluster) holds the name: PROVISIONING, DEPROVISIONING and FAILED too.
|
||||
pub async fn cluster_holder<A: OnboardApi>(
|
||||
api: &A,
|
||||
stem: &str,
|
||||
) -> Result<Option<NameHolder>, AwsError> {
|
||||
Ok(api
|
||||
.cluster(stem)
|
||||
.await?
|
||||
.filter(|c| c.status.as_deref() != Some("INACTIVE"))
|
||||
.map(|c| NameHolder {
|
||||
kind: "ECS cluster",
|
||||
owner: owner_of(&c),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Everything holding `stem`: its cluster and its Auto Scaling group.
|
||||
pub async fn ec2_name_holders<A: OnboardApi>(
|
||||
api: &A,
|
||||
stem: &str,
|
||||
) -> Result<Vec<NameHolder>, AwsError> {
|
||||
let mut out: Vec<NameHolder> = cluster_holder(api, stem).await?.into_iter().collect();
|
||||
if let Some(g) = api.group(stem).await? {
|
||||
out.push(NameHolder {
|
||||
kind: "Auto Scaling group",
|
||||
owner: owner_of(&g),
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `ssm:GetParameter` as a [`Param`].
|
||||
pub(crate) async fn get_parameter(ssm: &aws_sdk_ssm::Client, name: &str) -> Result<Param, AwsError> {
|
||||
let res = ssm
|
||||
.get_parameter()
|
||||
.name(name)
|
||||
.with_decryption(false)
|
||||
.send()
|
||||
.await;
|
||||
match res {
|
||||
Ok(out) => Ok(out
|
||||
.parameter()
|
||||
.and_then(|p| p.value())
|
||||
.map_or(Param::Missing, |v| Param::Value(v.to_string()))),
|
||||
Err(e) if e.as_service_error().is_some_and(|s| s.is_parameter_not_found()) => {
|
||||
Ok(Param::Missing)
|
||||
}
|
||||
Err(e) => {
|
||||
let (code, status) = error_parts(&e);
|
||||
match lookup_outcome(code.as_deref(), status) {
|
||||
Lookup::Missing => Ok(Param::Missing),
|
||||
Lookup::Denied => Ok(Param::Denied),
|
||||
Lookup::Error => Err(classify_sdk_error("ssm:GetParameter", &e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The real [`OnboardApi`], from a connection's `SdkConfig`.
|
||||
pub struct SdkOnboardApi {
|
||||
ssm: aws_sdk_ssm::Client,
|
||||
ecs: aws_sdk_ecs::Client,
|
||||
autoscaling: aws_sdk_autoscaling::Client,
|
||||
}
|
||||
|
||||
impl SdkOnboardApi {
|
||||
pub fn new(cfg: &SdkConfig) -> Self {
|
||||
SdkOnboardApi {
|
||||
ssm: aws_sdk_ssm::Client::new(cfg),
|
||||
ecs: aws_sdk_ecs::Client::new(cfg),
|
||||
autoscaling: aws_sdk_autoscaling::Client::new(cfg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OnboardApi for SdkOnboardApi {
|
||||
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
|
||||
get_parameter(&self.ssm, name).await
|
||||
}
|
||||
|
||||
async fn cluster(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
|
||||
// A missing name is a `failures` entry, not an error.
|
||||
let out = self
|
||||
.ecs
|
||||
.describe_clusters()
|
||||
.clusters(name)
|
||||
.include(aws_sdk_ecs::types::ClusterField::Tags)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| classify_sdk_error("ecs:DescribeClusters", &e))?;
|
||||
Ok(out
|
||||
.clusters()
|
||||
.iter()
|
||||
.find(|c| c.cluster_name() == Some(name))
|
||||
.map(|c| NamedResource {
|
||||
status: c.status().map(str::to_string),
|
||||
tags: c
|
||||
.tags()
|
||||
.iter()
|
||||
.filter_map(|t| Some((t.key()?.to_string(), t.value()?.to_string())))
|
||||
.collect(),
|
||||
}))
|
||||
}
|
||||
|
||||
async fn group(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
|
||||
let out = self
|
||||
.autoscaling
|
||||
.describe_auto_scaling_groups()
|
||||
.auto_scaling_group_names(name)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| classify_sdk_error("autoscaling:DescribeAutoScalingGroups", &e))?;
|
||||
Ok(out
|
||||
.auto_scaling_groups()
|
||||
.iter()
|
||||
.find(|g| g.auto_scaling_group_name() == Some(name))
|
||||
.map(|g| NamedResource {
|
||||
status: None,
|
||||
tags: g
|
||||
.tags()
|
||||
.iter()
|
||||
.filter_map(|t| Some((t.key()?.to_string(), t.value()?.to_string())))
|
||||
.collect(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// An OpenTofu state that manages nothing (what `tofu destroy` leaves):
|
||||
/// valid JSON whose `resources` is an empty list. Anything else, unreadable
|
||||
/// contents included, counts as a deployment.
|
||||
@@ -1015,6 +1345,10 @@ impl DetectApi for SdkDetectApi {
|
||||
knowable("iam:GetRole", res)
|
||||
}
|
||||
|
||||
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
|
||||
get_parameter(&self.ssm, name).await
|
||||
}
|
||||
|
||||
async fn state_document(&self, bucket: &str, key: &str) -> Result<Option<String>, AwsError> {
|
||||
const ACTION: &str = "s3:GetObject";
|
||||
let res = self
|
||||
|
||||
@@ -494,6 +494,8 @@ struct FakeDetect {
|
||||
roles: HashMap<String, Knowable>,
|
||||
/// State objects' contents, by key.
|
||||
documents: HashMap<String, String>,
|
||||
/// SSM parameters (user records), by name.
|
||||
parameters: HashMap<String, String>,
|
||||
calls: Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
@@ -506,6 +508,7 @@ impl FakeDetect {
|
||||
boundary: Knowable::Yes,
|
||||
roles: HashMap::new(),
|
||||
documents: HashMap::new(),
|
||||
parameters: HashMap::new(),
|
||||
calls: Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
@@ -548,6 +551,10 @@ impl DetectApi for FakeDetect {
|
||||
self.log(format!("role {name}"));
|
||||
Ok(self.roles.get(name).cloned().unwrap_or(Knowable::No))
|
||||
}
|
||||
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
|
||||
self.log(format!("parameter {name}"));
|
||||
Ok(self.parameters.get(name).cloned().map_or(Param::Missing, Param::Value))
|
||||
}
|
||||
async fn state_document(&self, bucket: &str, key: &str) -> Result<Option<String>, AwsError> {
|
||||
self.log(format!("document {bucket} {key}"));
|
||||
Ok(self.documents.get(key).cloned())
|
||||
@@ -612,8 +619,8 @@ fn onboarding_is_found_by_state_then_by_role() {
|
||||
assert_eq!(scan.owner_id, USER_ID);
|
||||
assert_eq!(scan.owner_key, owner_key(REGION, USER_ID));
|
||||
let onboarding = |n: &str| find(&scan.environments, n).onboarding.clone();
|
||||
assert_eq!(onboarding("bystate"), Some(OnboardingState::Joined));
|
||||
assert_eq!(onboarding("byrole"), Some(OnboardingState::Joined));
|
||||
assert_eq!(onboarding("bystate"), Some(OnboardingState::Joined { ec2: None }));
|
||||
assert_eq!(onboarding("byrole"), Some(OnboardingState::Joined { ec2: None }));
|
||||
assert_eq!(onboarding("denied"), Some(OnboardingState::NotJoined));
|
||||
assert_eq!(onboarding("absent"), Some(OnboardingState::NotJoined));
|
||||
assert!(matches!(
|
||||
@@ -662,7 +669,7 @@ fn onboarding_is_found_by_state_then_by_role() {
|
||||
assert_eq!(x.onboarding, Some(OnboardingState::InProgress { lock: None }));
|
||||
assert_eq!(x.onboarding_actions, vec![EnvironmentAction::Follow]);
|
||||
let y = find(&scan.environments, "y");
|
||||
let Some(OnboardingState::Incomplete { lock: Some(lock) }) = &y.onboarding else {
|
||||
let Some(OnboardingState::Incomplete { lock: Some(lock), ec2: None }) = &y.onboarding else {
|
||||
panic!("{y:#?}");
|
||||
};
|
||||
assert!(!lock.local_process);
|
||||
@@ -982,12 +989,12 @@ fn a_failed_onboarding_is_incomplete_not_joined() {
|
||||
let scan = run_scan(&d, &local);
|
||||
let row = |n: &str| find(&scan.environments, n).clone();
|
||||
|
||||
assert_eq!(row("done").onboarding, Some(OnboardingState::Joined));
|
||||
assert_eq!(row("done").onboarding, Some(OnboardingState::Joined { ec2: None }));
|
||||
for name in ["partial", "failedhere", "errored"] {
|
||||
let r = row(name);
|
||||
assert_eq!(
|
||||
r.onboarding,
|
||||
Some(OnboardingState::Incomplete { lock: None }),
|
||||
Some(OnboardingState::Incomplete { lock: None, ec2: None }),
|
||||
"{name}"
|
||||
);
|
||||
// Join is kept: an idempotent re-apply finishes it.
|
||||
@@ -1107,3 +1114,171 @@ fn the_tls_statement_is_added_to_a_bucket_policy_never_replacing_it() {
|
||||
// Something that isn't a policy is never overwritten.
|
||||
assert!(state_bucket_policy_update(Some("not json"), &bucket).is_err());
|
||||
}
|
||||
|
||||
fn user_record(env: &str, tag: &str) -> Value {
|
||||
let stem = crate::aws::tofu_modules::user_ec2_stem(env, tag);
|
||||
json!({
|
||||
"schema": 1,
|
||||
"instance_type": "m7g.large",
|
||||
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/{stem}"),
|
||||
"capacity_provider": stem,
|
||||
"user_tag": tag,
|
||||
"max_instances": 3,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_user_record_reads_as_the_users_compute() {
|
||||
let rec = user_record("dev", "alice");
|
||||
let c = parse_user_record("dev", &rec.to_string(), ACCOUNT, REGION).unwrap();
|
||||
assert_eq!(
|
||||
c,
|
||||
UserCompute {
|
||||
instance_type: "m7g.large".into(),
|
||||
max_instances: 3,
|
||||
user_tag: "alice".into(),
|
||||
cluster_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice"),
|
||||
capacity_provider: "triple-c-dev--u-alice".into(),
|
||||
}
|
||||
);
|
||||
// A record written before max_instances existed: the module's default.
|
||||
let mut old = rec.clone();
|
||||
old.as_object_mut().unwrap().remove("max_instances");
|
||||
let c = parse_user_record("dev", &old.to_string(), ACCOUNT, REGION).unwrap();
|
||||
assert_eq!(c.max_instances, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_user_record_is_untrusted() {
|
||||
let bad = |field: &str, value: Value, why: &str| {
|
||||
let mut rec = user_record("dev", "alice");
|
||||
rec[field] = value;
|
||||
let err = parse_user_record("dev", &rec.to_string(), ACCOUNT, REGION).unwrap_err();
|
||||
assert!(err.contains(why), "{field}: {err}");
|
||||
assert!(!err.contains("evil"), "{err}");
|
||||
};
|
||||
bad("schema", json!(2), "schema");
|
||||
bad("instance_type", json!("p5.48xlarge"), "instance_type");
|
||||
bad("instance_type", json!(7), "instance_type");
|
||||
bad("max_instances", json!(0), "max_instances");
|
||||
bad("max_instances", json!(5), "max_instances");
|
||||
bad("max_instances", json!("2"), "max_instances");
|
||||
bad("user_tag", json!("evil--tag"), "user_tag");
|
||||
// Another user's cluster, or one in another account or region.
|
||||
bad(
|
||||
"cluster_arn",
|
||||
json!(format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-bob")),
|
||||
"cluster_arn",
|
||||
);
|
||||
bad(
|
||||
"cluster_arn",
|
||||
json!(format!("arn:aws:ecs:us-east-1:{ACCOUNT}:cluster/triple-c-dev--u-alice")),
|
||||
"cluster_arn",
|
||||
);
|
||||
bad(
|
||||
"cluster_arn",
|
||||
json!(format!("arn:aws:ecs:{REGION}:999999999999:cluster/triple-c-dev--u-alice")),
|
||||
"cluster_arn",
|
||||
);
|
||||
bad("capacity_provider", json!("evil"), "capacity_provider");
|
||||
// Another environment's record.
|
||||
let rec = user_record("prod", "alice").to_string();
|
||||
assert!(parse_user_record("dev", &rec, ACCOUNT, REGION).is_err());
|
||||
for text in ["", "not json", "[]", &"x".repeat(5000)] {
|
||||
assert!(parse_user_record("dev", text, ACCOUNT, REGION).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_of(s: &OnboardingState) -> Option<&UserCompute> {
|
||||
match s {
|
||||
OnboardingState::Joined { ec2 } | OnboardingState::Incomplete { ec2, .. } => ec2.as_ref(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn onboarding_carries_the_users_compute_from_their_record() {
|
||||
use crate::aws::tofu_modules::user_record_parameter_name;
|
||||
let ok = owner_key(REGION, USER_ID);
|
||||
let mut d = FakeDetect::new()
|
||||
.with_record("joined", 1)
|
||||
.with_record("fargate", 1)
|
||||
.with_record("broken", 1)
|
||||
.with_record("partial", 1)
|
||||
.with_record("absent", 1);
|
||||
d.roles.insert(task_role("joined"), Knowable::Yes);
|
||||
d.roles.insert(task_role("fargate"), Knowable::Yes);
|
||||
d.roles.insert(task_role("broken"), Knowable::Yes);
|
||||
d.objects = Some(StateObjects {
|
||||
states: vec![
|
||||
env_key("joined"),
|
||||
env_key("fargate"),
|
||||
env_key("broken"),
|
||||
env_key("partial"),
|
||||
env_key("absent"),
|
||||
user_key("partial"),
|
||||
],
|
||||
locks: vec![],
|
||||
});
|
||||
d.documents.insert(user_key("partial"), "{\"resources\":[{}]}".into());
|
||||
for env in ["joined", "partial", "absent"] {
|
||||
d.parameters.insert(
|
||||
user_record_parameter_name(env, &ok),
|
||||
user_record(env, "alice").to_string(),
|
||||
);
|
||||
}
|
||||
d.parameters.insert(
|
||||
user_record_parameter_name("broken", &ok),
|
||||
user_record("other", "alice").to_string(),
|
||||
);
|
||||
// `joined`'s state is not readable here: found by role.
|
||||
d.objects.as_mut().unwrap().states.retain(|k| *k != user_key("joined"));
|
||||
|
||||
let scan = run_scan(&d, &nobody_local);
|
||||
let onboarding = |n: &str| find(&scan.environments, n).onboarding.clone().unwrap();
|
||||
let joined = onboarding("joined");
|
||||
let compute = compute_of(&joined).expect("joined has compute");
|
||||
assert_eq!(compute.instance_type, "m7g.large");
|
||||
assert_eq!(compute.max_instances, 3);
|
||||
assert_eq!(compute.user_tag, "alice");
|
||||
assert_eq!(onboarding("fargate"), OnboardingState::Joined { ec2: None });
|
||||
// A record that doesn't validate is no compute.
|
||||
assert_eq!(onboarding("broken"), OnboardingState::Joined { ec2: None });
|
||||
// An incomplete onboarding keeps what its record still says.
|
||||
let partial = onboarding("partial");
|
||||
assert!(matches!(partial, OnboardingState::Incomplete { .. }), "{partial:?}");
|
||||
assert_eq!(compute_of(&partial).map(|c| c.max_instances), Some(3));
|
||||
// Not joined: the record isn't even read.
|
||||
assert_eq!(onboarding("absent"), OnboardingState::NotJoined);
|
||||
assert!(!d
|
||||
.calls()
|
||||
.contains(&format!("parameter {}", user_record_parameter_name("absent", &ok))));
|
||||
|
||||
// Serialized for the UI: the state tag plus `ec2`.
|
||||
let v = serde_json::to_value(&joined).unwrap();
|
||||
assert_eq!(v["state"], "joined");
|
||||
assert_eq!(v["ec2"]["capacity_provider"], "triple-c-joined--u-alice");
|
||||
assert_eq!(
|
||||
serde_json::to_value(OnboardingState::Joined { ec2: None }).unwrap(),
|
||||
json!({"state": "joined", "ec2": null})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_user_record_read_is_absent_valid_or_unusable() {
|
||||
let ok = user_record("dev", "alice").to_string();
|
||||
assert_eq!(user_record_from(Param::Missing, "dev", ACCOUNT, REGION), UserRecord::Absent);
|
||||
assert!(matches!(
|
||||
user_record_from(Param::Value(ok), "dev", ACCOUNT, REGION),
|
||||
UserRecord::Valid(_)
|
||||
));
|
||||
// Denied or invalid: there may be capacity, so it is not "absent".
|
||||
assert!(matches!(
|
||||
user_record_from(Param::Denied, "dev", ACCOUNT, REGION),
|
||||
UserRecord::Unusable(_)
|
||||
));
|
||||
assert!(matches!(
|
||||
user_record_from(Param::Value("{}".into()), "dev", ACCOUNT, REGION),
|
||||
UserRecord::Unusable(_)
|
||||
));
|
||||
}
|
||||
@@ -642,6 +642,12 @@ async fn a_workspace_role_reads_fargate_prices_and_a_bare_role_cannot() {
|
||||
eprintln!("{}: x86 {x86:?}, ARM {arm:?}", e.region);
|
||||
assert!(x86.vcpu_hour > 0.0 && x86.gb_hour > 0.0 && x86.storage_gb_hour > 0.0);
|
||||
assert!(arm.vcpu_hour < x86.vcpu_hour, "ARM is cheaper per vCPU");
|
||||
// E10: an EC2 instance type's on-demand price, with the same rights.
|
||||
let m7i = super::pricing::fetch_ec2_price(&SdkPricingApi::new(&cfg), &e.region, "m7i.large")
|
||||
.await
|
||||
.expect("workspace role reads EC2 prices");
|
||||
eprintln!("{}: m7i.large {m7i}/hour", e.region);
|
||||
assert!(m7i > 0.0 && m7i < 1.0, "{m7i}");
|
||||
|
||||
let bare = e.role("noprice", None, &[]).await;
|
||||
let cfg = sdk_config_for_keys(&e.assume_keys(&bare).await, PRICING_REGION);
|
||||
@@ -1592,7 +1598,7 @@ mod environment {
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, &t.e.region, &owner),
|
||||
&onboarding_vars(&name, &t.e.region, &owner, None).unwrap(),
|
||||
TofuOp::Apply,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
@@ -1601,7 +1607,7 @@ mod environment {
|
||||
let found = t.scan(&cfg, &who).await;
|
||||
let env = found.environments.iter().find(|x| x.name == name).unwrap();
|
||||
// Joined from the state's onboarding_complete output, not just the role.
|
||||
assert_eq!(env.onboarding, Some(OnboardingState::Joined), "{env:#?}");
|
||||
assert_eq!(env.onboarding, Some(OnboardingState::Joined { ec2: None }), "{env:#?}");
|
||||
assert_eq!(env.onboarded_users, Some(1), "{env:#?}");
|
||||
let (task_role, exec_role) = user_role_names(&name, &okey);
|
||||
eprintln!("onboarded with region-scoped roles {task_role} / {exec_role}");
|
||||
@@ -1643,7 +1649,7 @@ mod environment {
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, &t.e.region, &owner),
|
||||
&onboarding_vars(&name, &t.e.region, &owner, None).unwrap(),
|
||||
TofuOp::Destroy,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
@@ -1767,6 +1773,13 @@ mod environment {
|
||||
config_file_before: Option<Option<std::ffi::OsString>>,
|
||||
/// `TRIPLEC_E2E_ECR_REPO`: the throwaway repository to force-delete.
|
||||
ecr_repo: Option<String>,
|
||||
/// Phase 7: the environment whose per-user EC2 capacity
|
||||
/// (`triple-c-<env>--u-*`) the guard tears down, and the user's
|
||||
/// owner key (instance role, user record).
|
||||
ec2_env: Option<String>,
|
||||
ec2_okey: Option<String>,
|
||||
/// Auto Scaling groups a probe made outside that prefix.
|
||||
probe_groups: Vec<String>,
|
||||
}
|
||||
|
||||
/// The whole phase-5 body: a hang anywhere becomes a panic the guard
|
||||
@@ -1796,6 +1809,9 @@ mod environment {
|
||||
Some(None) => std::env::remove_var("AWS_CONFIG_FILE"),
|
||||
None => {}
|
||||
}
|
||||
if let Some(env) = &p.ec2_env {
|
||||
ec2_cleanup(e, env, p.ec2_okey.as_deref(), &p.probe_groups).await;
|
||||
}
|
||||
let ecs = aws_sdk_ecs::Client::new(&e.cfg);
|
||||
if let Some(cluster) = &p.cluster {
|
||||
let mut arns = Vec::new();
|
||||
@@ -1912,6 +1928,211 @@ mod environment {
|
||||
}
|
||||
}
|
||||
|
||||
/// The AWS CLI as the e2e profile (`keys`: as that session instead):
|
||||
/// (success, stdout, stderr). The app has no EC2 SDK crate.
|
||||
async fn aws_cli(keys: Option<&AwsKeys>, region: &str, args: &[&str]) -> (bool, String, String) {
|
||||
let mut cmd = tokio::process::Command::new("aws");
|
||||
cmd.args(["--region", region, "--output", "json"]);
|
||||
match keys {
|
||||
None => {
|
||||
cmd.args(["--profile", &super::var("TRIPLEC_AWS_E2E_PROFILE")]);
|
||||
}
|
||||
Some(k) => {
|
||||
cmd.env_remove("AWS_PROFILE")
|
||||
.env("AWS_ACCESS_KEY_ID", &k.access_key_id)
|
||||
.env("AWS_SECRET_ACCESS_KEY", &k.secret_access_key);
|
||||
match &k.session_token {
|
||||
Some(t) => cmd.env("AWS_SESSION_TOKEN", t),
|
||||
None => cmd.env_remove("AWS_SESSION_TOKEN"),
|
||||
};
|
||||
}
|
||||
}
|
||||
cmd.args(args);
|
||||
match tokio::time::timeout(Duration::from_secs(120), cmd.output()).await {
|
||||
Ok(Ok(o)) => (
|
||||
o.status.success(),
|
||||
String::from_utf8_lossy(&o.stdout).into_owned(),
|
||||
String::from_utf8_lossy(&o.stderr).into_owned(),
|
||||
),
|
||||
Ok(Err(err)) => (false, String::new(), format!("aws cli: {err}")),
|
||||
Err(_) => (false, String::new(), "aws cli: timed out".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 7's guard, as the account admin: everything of `env`'s per-user
|
||||
/// EC2 capacity (`triple-c-<env>--u-*`) and the probes' groups, so no
|
||||
/// instance outlives a failed run. Tasks on the per-user clusters are
|
||||
/// stopped, groups force-deleted (terminating their instances) and waited
|
||||
/// for, leftover tagged instances terminated, then capacity providers,
|
||||
/// container instances, clusters, launch templates, the user record and
|
||||
/// the instance role/profile deleted. A resource already gone is fine
|
||||
/// (the modules' destroy then finds nothing). Logs, never fails.
|
||||
async fn ec2_cleanup(e: &E2e, env: &str, okey: Option<&str>, probe_groups: &[String]) {
|
||||
let prefix = format!("triple-c-{env}--u-");
|
||||
let ecs = aws_sdk_ecs::Client::new(&e.cfg);
|
||||
let asg = aws_sdk_autoscaling::Client::new(&e.cfg);
|
||||
// Tasks on the per-user clusters.
|
||||
let mut clusters = Vec::new();
|
||||
if let Ok(out) = ecs.list_clusters().send().await {
|
||||
clusters.extend(out.cluster_arns().iter().filter(|a| a.contains(&format!(":cluster/{prefix}"))).cloned());
|
||||
}
|
||||
for cluster in &clusters {
|
||||
if let Ok(out) = ecs.list_tasks().cluster(cluster).send().await {
|
||||
for arn in out.task_arns() {
|
||||
eprintln!("cleanup: stopping EC2 task {arn}");
|
||||
let _ = ecs.stop_task().cluster(cluster).task(arn).reason("triple-c-e2e cleanup").send().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Groups (force: terminates their instances, ignores protection).
|
||||
let mut groups: Vec<String> = probe_groups.to_vec();
|
||||
let mut next = None;
|
||||
loop {
|
||||
let Ok(out) = asg.describe_auto_scaling_groups().set_next_token(next.take()).send().await else {
|
||||
eprintln!("cleanup: WARNING: could not list Auto Scaling groups; only the known ones are deleted");
|
||||
break;
|
||||
};
|
||||
for g in out.auto_scaling_groups() {
|
||||
let n = g.auto_scaling_group_name().unwrap_or_default();
|
||||
if n.starts_with(&prefix) && !groups.iter().any(|x| x == n) {
|
||||
groups.push(n.to_string());
|
||||
}
|
||||
}
|
||||
next = out.next_token().map(str::to_string);
|
||||
if next.is_none() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for g in &groups {
|
||||
match asg.delete_auto_scaling_group().auto_scaling_group_name(g).force_delete(true).send().await {
|
||||
Ok(_) => eprintln!("cleanup: force-deleting Auto Scaling group {g}"),
|
||||
Err(err) => {
|
||||
let msg = format!("{err:?}");
|
||||
if !msg.contains("not found") && !msg.contains("ValidationError") {
|
||||
eprintln!("cleanup: delete group {g}: {msg}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !groups.is_empty() {
|
||||
for _ in 0..60 {
|
||||
// A failed read is "not yet gone", never "gone".
|
||||
let left = asg
|
||||
.describe_auto_scaling_groups()
|
||||
.set_auto_scaling_group_names(Some(groups.clone()))
|
||||
.send()
|
||||
.await
|
||||
.map(|o| o.auto_scaling_groups().len())
|
||||
.unwrap_or(usize::MAX);
|
||||
if left == 0 {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
}
|
||||
}
|
||||
// Any instance still tagged with this environment.
|
||||
let (ok_, out, err) = aws_cli(
|
||||
None,
|
||||
&e.region,
|
||||
&[
|
||||
"ec2", "describe-instances",
|
||||
"--filters", &format!("Name=tag:triple-c:environment,Values={env}"),
|
||||
"Name=instance-state-name,Values=pending,running,stopping,stopped",
|
||||
"--query", "Reservations[].Instances[].InstanceId",
|
||||
],
|
||||
)
|
||||
.await;
|
||||
if ok_ {
|
||||
let ids: Vec<String> = serde_json::from_str(&out).unwrap_or_default();
|
||||
if !ids.is_empty() {
|
||||
let mut args = vec!["ec2", "terminate-instances", "--instance-ids"];
|
||||
args.extend(ids.iter().map(String::as_str));
|
||||
let r = aws_cli(None, &e.region, &args).await;
|
||||
eprintln!("cleanup: terminate {ids:?}: ok {}", r.0);
|
||||
let mut args = vec!["ec2", "wait", "instance-terminated", "--instance-ids"];
|
||||
args.extend(ids.iter().map(String::as_str));
|
||||
let _ = aws_cli(None, &e.region, &args).await;
|
||||
}
|
||||
} else {
|
||||
eprintln!("cleanup: describe instances: {err}");
|
||||
}
|
||||
// Capacity providers: off their clusters, then deleted.
|
||||
for cluster in &clusters {
|
||||
let _ = ecs.put_cluster_capacity_providers().cluster(cluster).set_capacity_providers(Some(vec![])).set_default_capacity_provider_strategy(Some(vec![])).send().await;
|
||||
}
|
||||
if let Ok(out) = ecs.describe_capacity_providers().send().await {
|
||||
for cp in out.capacity_providers() {
|
||||
let n = cp.name().unwrap_or_default();
|
||||
if n.starts_with(&prefix) && cp.status() != Some(&aws_sdk_ecs::types::CapacityProviderStatus::Inactive) {
|
||||
match ecs.delete_capacity_provider().capacity_provider(n).send().await {
|
||||
Ok(_) => eprintln!("cleanup: deleted capacity provider {n}"),
|
||||
Err(err) => eprintln!("cleanup: delete capacity provider {n}: {err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for cluster in &clusters {
|
||||
if let Ok(out) = ecs.list_container_instances().cluster(cluster).send().await {
|
||||
for ci in out.container_instance_arns() {
|
||||
let _ = ecs.deregister_container_instance().cluster(cluster).container_instance(ci).force(true).send().await;
|
||||
}
|
||||
}
|
||||
// Busy (UpdateInProgress) while the provider detaches: retry.
|
||||
for attempt in 0..12 {
|
||||
match ecs.delete_cluster().cluster(cluster).send().await {
|
||||
Ok(_) => {
|
||||
eprintln!("cleanup: deleted cluster {cluster}");
|
||||
break;
|
||||
}
|
||||
Err(err) if attempt < 11 && format!("{err:?}").contains("UpdateInProgress") => {
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("cleanup: delete cluster {cluster}: {err:?}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Launch templates.
|
||||
let (ok_, out, _) = aws_cli(
|
||||
None,
|
||||
&e.region,
|
||||
&["ec2", "describe-launch-templates", "--filters", &format!("Name=launch-template-name,Values={prefix}*"), "--query", "LaunchTemplates[].LaunchTemplateName"],
|
||||
)
|
||||
.await;
|
||||
if ok_ {
|
||||
let names: Vec<String> = serde_json::from_str(&out).unwrap_or_default();
|
||||
for n in names {
|
||||
let r = aws_cli(None, &e.region, &["ec2", "delete-launch-template", "--launch-template-name", &n]).await;
|
||||
eprintln!("cleanup: delete launch template {n}: ok {} {}", r.0, r.2.trim());
|
||||
}
|
||||
}
|
||||
if let Some(okey) = okey {
|
||||
let ssm = aws_sdk_ssm::Client::new(&e.cfg);
|
||||
let param = crate::aws::tofu_modules::user_record_parameter_name(env, okey);
|
||||
if ssm.delete_parameter().name(¶m).send().await.is_ok() {
|
||||
eprintln!("cleanup: deleted {param}");
|
||||
}
|
||||
let role = crate::aws::tofu_modules::user_instance_role_name(env, okey);
|
||||
if e.iam.remove_role_from_instance_profile().instance_profile_name(&role).role_name(&role).send().await.is_ok() {
|
||||
eprintln!("cleanup: removed {role} from its instance profile");
|
||||
}
|
||||
if e.iam.delete_instance_profile().instance_profile_name(&role).send().await.is_ok() {
|
||||
eprintln!("cleanup: deleted instance profile {role}");
|
||||
}
|
||||
if let Ok(out) = e.iam.list_role_policies().role_name(&role).send().await {
|
||||
for pn in out.policy_names() {
|
||||
let _ = e.iam.delete_role_policy().role_name(&role).policy_name(pn).send().await;
|
||||
}
|
||||
match e.iam.delete_role().role_name(&role).send().await {
|
||||
Ok(_) => eprintln!("cleanup: deleted role {role}"),
|
||||
Err(err) => eprintln!("cleanup: delete role {role}: {err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Poll `f` every `every` until it returns `Some`, or `None` after `within`.
|
||||
async fn poll<T, F, Fut>(within: Duration, every: Duration, mut f: F) -> Option<T>
|
||||
where
|
||||
@@ -1954,6 +2175,8 @@ mod environment {
|
||||
conn: HostConnection,
|
||||
ws_cfg: SdkConfig,
|
||||
ws: CallerIdentity,
|
||||
/// The environment admin role (fresh sessions via `Env::session`).
|
||||
admin: Admin,
|
||||
}
|
||||
|
||||
/// The workspace tests' common start: the environment (with
|
||||
@@ -1967,6 +2190,7 @@ mod environment {
|
||||
p5: &std::sync::Mutex<Phase5>,
|
||||
image: &str,
|
||||
max_workspace_hours: u32,
|
||||
ec2: Option<&crate::aws::tofu_modules::Ec2Compute>,
|
||||
) -> Deployed {
|
||||
use crate::aws::tofu_modules::{environment_cluster_name, user_file_system_token};
|
||||
|
||||
@@ -1997,18 +2221,43 @@ mod environment {
|
||||
let fs_token = user_file_system_token(&name, &okey);
|
||||
p5_lock(p5).fs_token = Some(fs_token.clone());
|
||||
|
||||
// Onboarded by the admin (an onboarding from an admin bundle).
|
||||
let (keys, _, _) = t.session(&admin).await;
|
||||
// Onboarded by the admin (an onboarding from an admin bundle). With
|
||||
// EC2 capacity (phase 7): the user's role carries the triple-c-user
|
||||
// principal tag, and the vars come from the app's own pre-apply
|
||||
// step (record, choice, name-collision check) as the admin.
|
||||
let (keys, admin_cfg, _) = t.session(&admin).await;
|
||||
let vars = match ec2 {
|
||||
None => onboarding_vars(&name, ®ion, &owner, None).unwrap(),
|
||||
Some(c) => {
|
||||
use crate::aws::tofu_modules::EC2_USER_TAG_KEY;
|
||||
use crate::commands::environment_commands::{prepare_onboarding, Ec2Choice};
|
||||
t.e.tag_role(&user, EC2_USER_TAG_KEY, &c.user_tag).await;
|
||||
{
|
||||
let mut g = p5_lock(p5);
|
||||
g.ec2_env = Some(name.clone());
|
||||
g.ec2_okey = Some(okey.clone());
|
||||
}
|
||||
let api = crate::aws::environments::SdkOnboardApi::new(&admin_cfg);
|
||||
let t0 = std::time::Instant::now();
|
||||
let vars = prepare_onboarding(&api, &name, ®ion, &t.e.account, &owner, Some(Ec2Choice::Ec2(c.clone())))
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("the pre-apply onboarding step: {e}"));
|
||||
eprintln!("RESULT pre-apply EC2 collision check (free names) passed in {:?}: {vars}", t0.elapsed());
|
||||
vars
|
||||
}
|
||||
};
|
||||
let t0 = std::time::Instant::now();
|
||||
let (_, outcome, denied) = t
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, ®ion, &owner),
|
||||
&vars,
|
||||
TofuOp::Apply,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
.await;
|
||||
assert_ok("onboard the workspace user", &outcome, &denied);
|
||||
eprintln!("RESULT onboarding apply: {:?}", t0.elapsed());
|
||||
let (_, exec_role) = user_role_names(&name, &okey);
|
||||
|
||||
// Test-only: pull from the throwaway private ECR repository.
|
||||
@@ -2056,7 +2305,7 @@ mod environment {
|
||||
let ws = caller_identity(&ws_cfg).await.expect("workspace user identity");
|
||||
assert_eq!(ws.user_id, owner, "the profile session is the onboarded owner");
|
||||
eprintln!("workspace user: {}", ws.arn);
|
||||
Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws }
|
||||
Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws, admin }
|
||||
}
|
||||
|
||||
/// Phase 5: a Fargate workspace's whole life through `ProjectHost::Aws`,
|
||||
@@ -2113,8 +2362,8 @@ log_stream_prefix, project_secret_name, task_family};
|
||||
// Boxed: the body's state machine is far larger than a test
|
||||
// thread's 2 MiB stack once it is moved into the timeout.
|
||||
let inner = Box::pin(async {
|
||||
let Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws } =
|
||||
deploy_for_workspace_user(&t, &p5, &image, 0).await;
|
||||
let Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws, .. } =
|
||||
deploy_for_workspace_user(&t, &p5, &image, 0, None).await;
|
||||
|
||||
let mut settings = AppSettings::default();
|
||||
settings.hosts = vec![conn];
|
||||
@@ -2573,8 +2822,8 @@ log_stream_prefix, project_secret_name, task_family};
|
||||
let p5 = std::sync::Mutex::new(Phase5 { ecr_repo, ..Default::default() });
|
||||
let inner = Box::pin(async {
|
||||
// ── 1. the environment carries the lifetime cap ──
|
||||
let Deployed { region, name, owner: _, okey, fs_token: _, conn, ws_cfg, ws } =
|
||||
deploy_for_workspace_user(&t, &p5, &image, 1).await;
|
||||
let Deployed { region, name, okey, conn, ws_cfg, ws, .. } =
|
||||
deploy_for_workspace_user(&t, &p5, &image, 1, None).await;
|
||||
let env = crate::aws::runtime::read_environment(&SdkRuntimeApi::new(&ws_cfg), &name, &ws.account, ®ion)
|
||||
.await
|
||||
.expect("the environment record, as the workspace user");
|
||||
@@ -2921,4 +3170,596 @@ log_stream_prefix, project_secret_name, task_family};
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────── phase 7: ECS on EC2
|
||||
|
||||
/// The phase-7 body: two cold starts, a scale-in wait (managed scaling
|
||||
/// takes ~15 minutes to remove an idle instance) and three applies.
|
||||
const PHASE7_TIMEOUT: Duration = Duration::from_secs(110 * 60);
|
||||
|
||||
/// A group's (instance count, desired capacity, `id:state` list) as the
|
||||
/// account admin; `None` when it does not exist.
|
||||
async fn group_size(asg: &aws_sdk_autoscaling::Client, name: &str) -> Option<(usize, i32, Vec<String>)> {
|
||||
let out = asg.describe_auto_scaling_groups().auto_scaling_group_names(name).send().await.ok()?;
|
||||
let g = out.auto_scaling_groups().first()?;
|
||||
let ids = g
|
||||
.instances()
|
||||
.iter()
|
||||
.map(|i| format!("{}:{}", i.instance_id().unwrap_or_default(), i.lifecycle_state().map(|s| s.as_str()).unwrap_or("?")))
|
||||
.collect();
|
||||
Some((g.instances().len(), g.desired_capacity().unwrap_or(-1), ids))
|
||||
}
|
||||
|
||||
/// Instances still tagged with `env` and not terminated, as the account admin.
|
||||
async fn live_env_instances(region: &str, env: &str) -> Vec<String> {
|
||||
let (ok_, out, err) = aws_cli(
|
||||
None,
|
||||
region,
|
||||
&[
|
||||
"ec2", "describe-instances",
|
||||
"--filters", &format!("Name=tag:triple-c:environment,Values={env}"),
|
||||
"Name=instance-state-name,Values=pending,running,stopping,stopped,shutting-down",
|
||||
"--query", "Reservations[].Instances[].[InstanceId,State.Name]",
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert!(ok_, "describe-instances: {err}");
|
||||
let rows: Vec<Vec<String>> = serde_json::from_str(&out).unwrap_or_default();
|
||||
rows.into_iter().map(|r| r.join(":")).collect()
|
||||
}
|
||||
|
||||
/// Phase 7: an EC2 project's whole life on the user's own capacity
|
||||
/// (ECS on EC2, bridge networking, scale to zero), as a role holding
|
||||
/// only the workspace files and carrying the `triple-c-user` principal
|
||||
/// tag (a role tag: an assumed-role session's principal tags), onboarded
|
||||
/// with `m7i.large`, at most 1 instance, by the environment admin
|
||||
/// holding all four admin files. Run with `scripts/aws-e2e-workspace.sh`
|
||||
/// and `TRIPLEC_E2E_TEST=aws::live_tests::environment::a_phase7_ec2_workspace_round_trips`.
|
||||
///
|
||||
/// Covers: the onboarding's resources and its pre-apply collision check
|
||||
/// (and its refusal for another owner); admin-tier probes of the
|
||||
/// scaling file (`$Latest`, a pinned control, an unsupported type in a
|
||||
/// template version, a capacity provider over a non-Triple-C group);
|
||||
/// a cold start with Docker access, VPN and the sandbox on (instance
|
||||
/// launch, ECS Exec port forward into the bridge-mode task), `docker
|
||||
/// ps`, a WireGuard/TUN interface, `src_valid_mark`, bubblewrap, IMDS
|
||||
/// and the agent's introspection port from the container; RunTask on
|
||||
/// another user's cluster (the wrong-triple-c-user message); stop and
|
||||
/// the scale-in to zero; a second start; removal; re-applying the
|
||||
/// onboarding without EC2 (drain and delete with an instance still up);
|
||||
/// offboarding and the environment's destroy, all as the admin tier.
|
||||
/// Its guard (`ec2_cleanup`) force-deletes whatever EC2 capacity is
|
||||
/// left. About one m7i.large instance-hour.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn a_phase7_ec2_workspace_round_trips() {
|
||||
use crate::aws::environments::{OnboardApi, SdkOnboardApi, UserCompute, UserRecord};
|
||||
use crate::aws::runtime::{read_user_record, SdkRuntimeApi};
|
||||
use crate::aws::tasks::{ec2_run_error, ec2_run_task_request, SdkTaskApi, TaskApi, EC2_TAG_MISMATCH};
|
||||
use crate::aws::tofu_modules::{user_ec2_stem, user_instance_role_name, Ec2Compute};
|
||||
use crate::aws::workspace::workspace_tags;
|
||||
use crate::commands::environment_commands::{prepare_onboarding, Ec2Choice};
|
||||
use crate::host::aws_host::PROGRESS_INSTANCE;
|
||||
use crate::host::lifecycle::ProjectHost;
|
||||
use crate::models::app_settings::ImageSource;
|
||||
use crate::models::project::{AwsPlacement, AwsWorkspaceConfig, LaunchType, StorageMode};
|
||||
use crate::models::{AppSettings, Project};
|
||||
use crate::storage::projects_store::ProjectsStore;
|
||||
|
||||
let image = super::var("TRIPLEC_E2E_IMAGE");
|
||||
let ecr_repo = std::env::var("TRIPLEC_E2E_ECR_REPO").ok().filter(|r| !r.is_empty());
|
||||
env_e2e(|t| async move {
|
||||
let p5 = std::sync::Mutex::new(Phase5 { ecr_repo, ..Default::default() });
|
||||
let inner = Box::pin(async {
|
||||
// TRIPLEC_E2E_PHASE7_SHORT=1: the life cycle only (no probes, no
|
||||
// in-container checks or isolation, no scale-in wait; the second
|
||||
// start is then warm, on the instance still up).
|
||||
let quick = std::env::var("TRIPLEC_E2E_PHASE7_SHORT").is_ok_and(|v| v == "1");
|
||||
let short = uuid::Uuid::new_v4().simple().to_string();
|
||||
let user_tag = format!("e2e{}", &short[..6]);
|
||||
let compute = Ec2Compute { instance_type: "m7i.large".into(), max_instances: 1, user_tag: user_tag.clone() };
|
||||
let Deployed { region, name, owner, okey, conn, ws_cfg, ws, admin, .. } =
|
||||
deploy_for_workspace_user(&t, &p5, &image, 0, Some(&compute)).await;
|
||||
let account = t.e.account.clone();
|
||||
let stem = user_ec2_stem(&name, &user_tag);
|
||||
let asg_admin = aws_sdk_autoscaling::Client::new(&t.e.cfg);
|
||||
let ecs_admin = aws_sdk_ecs::Client::new(&t.e.cfg);
|
||||
|
||||
// ── 1. what the onboarding made ──
|
||||
let groups = ok("describe the group", asg_admin.describe_auto_scaling_groups().auto_scaling_group_names(&stem).send().await);
|
||||
let group = groups.auto_scaling_groups().first().expect("the user's Auto Scaling group").clone();
|
||||
let subnets = group.vpc_zone_identifier().unwrap_or_default().to_string();
|
||||
let group_tags: Vec<(String, String)> = group
|
||||
.tags()
|
||||
.iter()
|
||||
.map(|t| (t.key().unwrap_or_default().to_string(), t.value().unwrap_or_default().to_string()))
|
||||
.collect();
|
||||
eprintln!(
|
||||
"RESULT group {stem}: min {:?} max {:?} desired {:?}, template {:?}, protect {:?}, tags {group_tags:?}",
|
||||
group.min_size(),
|
||||
group.max_size(),
|
||||
group.desired_capacity(),
|
||||
group.launch_template().map(|l| (l.launch_template_name(), l.version())),
|
||||
group.new_instances_protected_from_scale_in()
|
||||
);
|
||||
assert_eq!((group.min_size(), group.max_size()), (Some(0), Some(1)));
|
||||
let cl = ok(
|
||||
"describe the user's cluster",
|
||||
ecs_admin.describe_clusters().clusters(&stem).include(aws_sdk_ecs::types::ClusterField::Tags).send().await,
|
||||
);
|
||||
let cluster = cl.clusters().first().expect("the user's cluster");
|
||||
eprintln!(
|
||||
"RESULT cluster {stem}: {:?}, providers {:?}, default strategy {:?}, tags {:?}",
|
||||
cluster.status(),
|
||||
cluster.capacity_providers(),
|
||||
cluster.default_capacity_provider_strategy().iter().map(|s| s.capacity_provider()).collect::<Vec<_>>(),
|
||||
cluster.tags().iter().map(|t| (t.key().unwrap_or_default(), t.value().unwrap_or_default())).collect::<Vec<_>>()
|
||||
);
|
||||
let cps = ok("describe the capacity provider", ecs_admin.describe_capacity_providers().capacity_providers(&stem).send().await);
|
||||
let cp = cps.capacity_providers().first().expect("the capacity provider");
|
||||
eprintln!(
|
||||
"RESULT capacity provider {stem}: {:?}, managed scaling {:?}, termination protection {:?}",
|
||||
cp.status(),
|
||||
cp.auto_scaling_group_provider().and_then(|a| a.managed_scaling()).map(|m| (m.status(), m.target_capacity())),
|
||||
cp.auto_scaling_group_provider().map(|a| a.managed_termination_protection())
|
||||
);
|
||||
let (_, lt, _) = aws_cli(
|
||||
None,
|
||||
®ion,
|
||||
&["ec2", "describe-launch-template-versions", "--launch-template-name", &stem, "--query",
|
||||
"LaunchTemplateVersions[].[VersionNumber,LaunchTemplateData.InstanceType,LaunchTemplateData.ImageId,LaunchTemplateData.MetadataOptions]"],
|
||||
)
|
||||
.await;
|
||||
eprintln!("RESULT launch template {stem}: {}", lt.split_whitespace().collect::<Vec<_>>().join(" "));
|
||||
let role_name = user_instance_role_name(&name, &okey);
|
||||
let prof = ok("get the instance profile", t.e.iam.get_instance_profile().instance_profile_name(&role_name).send().await);
|
||||
eprintln!(
|
||||
"RESULT instance profile {role_name}: roles {:?}",
|
||||
prof.instance_profile().map(|p| p.roles().iter().map(|r| r.role_name().to_string()).collect::<Vec<_>>())
|
||||
);
|
||||
let record = read_user_record(&SdkRuntimeApi::new(&ws_cfg), &name, &okey, &account, ®ion).await.expect("read the user record");
|
||||
eprintln!("RESULT user record, as the workspace user: {record:?}");
|
||||
let Some(rec) = record.valid().cloned() else { panic!("the user record is not valid: {record:?}") };
|
||||
assert_eq!((rec.instance_type.as_str(), rec.max_instances, rec.user_tag.as_str()), ("m7i.large", 1, user_tag.as_str()));
|
||||
assert!(rec.cluster_arn.ends_with(&format!(":cluster/{stem}")), "{}", rec.cluster_arn);
|
||||
|
||||
// ── 1b / 6. the collision check's reads, and its refusal ──
|
||||
let (akeys, acfg, _) = t.session(&admin).await;
|
||||
let oapi = SdkOnboardApi::new(&acfg);
|
||||
let held = oapi.cluster(&stem).await.expect("DescribeClusters include=TAGS as the admin tier");
|
||||
eprintln!("RESULT DescribeClusters include=TAGS as the environment admin: {held:?}");
|
||||
let held = held.expect("the cluster is found");
|
||||
assert!(held.tags.iter().any(|(k, v)| k == "triple-c:owner" && *v == owner), "{held:?}");
|
||||
let missing = oapi.cluster(&format!("{stem}zz")).await;
|
||||
eprintln!("RESULT DescribeClusters on a missing name: {missing:?}");
|
||||
assert!(matches!(missing, Ok(None)), "{missing:?}");
|
||||
let missing_group = oapi.group(&format!("{stem}zz")).await;
|
||||
eprintln!("RESULT DescribeAutoScalingGroups on a missing name: {missing_group:?}");
|
||||
assert!(matches!(missing_group, Ok(None)), "{missing_group:?}");
|
||||
let refused = prepare_onboarding(&oapi, &name, ®ion, &account, "AROAEXAMPLEOTHER0000:bob", Some(Ec2Choice::Ec2(compute.clone()))).await;
|
||||
eprintln!("RESULT onboarding another owner with the same triple-c-user value: {refused:?}");
|
||||
let refused = refused.expect_err("another owner's onboarding with the same tag is refused");
|
||||
assert!(refused.to_string().contains("another onboarding"), "{refused}");
|
||||
|
||||
if !quick {
|
||||
// ── 6. the scaling file's open points, as the environment admin ──
|
||||
let asg_env = aws_sdk_autoscaling::Client::new(&acfg);
|
||||
let ecs_env = aws_sdk_ecs::Client::new(&acfg);
|
||||
let probe_tags = |n: &str| -> Vec<aws_sdk_autoscaling::types::Tag> {
|
||||
group_tags
|
||||
.iter()
|
||||
.filter(|(k, _)| k.starts_with("triple-c:"))
|
||||
.map(|(k, v)| {
|
||||
aws_sdk_autoscaling::types::Tag::builder()
|
||||
.key(k)
|
||||
.value(v)
|
||||
.resource_id(n)
|
||||
.resource_type("auto-scaling-group")
|
||||
.propagate_at_launch(false)
|
||||
.build()
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let create_group = |client: aws_sdk_autoscaling::Client, n: String, version: String, tags: Vec<aws_sdk_autoscaling::types::Tag>| {
|
||||
let subnets = subnets.clone();
|
||||
let lt_name = stem.clone();
|
||||
async move {
|
||||
client
|
||||
.create_auto_scaling_group()
|
||||
.auto_scaling_group_name(&n)
|
||||
.launch_template(
|
||||
aws_sdk_autoscaling::types::LaunchTemplateSpecification::builder()
|
||||
.launch_template_name(lt_name)
|
||||
.version(version)
|
||||
.build(),
|
||||
)
|
||||
.min_size(0)
|
||||
.max_size(0)
|
||||
.desired_capacity(0)
|
||||
.vpc_zone_identifier(subnets)
|
||||
.set_tags(Some(tags))
|
||||
.send()
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|e| classify_sdk_error("autoscaling:CreateAutoScalingGroup", &e).to_string())
|
||||
}
|
||||
};
|
||||
let delete_group = |n: String| {
|
||||
let client = asg_admin.clone();
|
||||
async move {
|
||||
let _ = client.delete_auto_scaling_group().auto_scaling_group_name(&n).force_delete(true).send().await;
|
||||
}
|
||||
};
|
||||
// $Latest must be denied (LaunchTemplateVersionSpecified).
|
||||
let pa = format!("{stem}-pa");
|
||||
let r = create_group(asg_env.clone(), pa.clone(), "$Latest".into(), probe_tags(&pa)).await;
|
||||
eprintln!("RESULT probe: CreateAutoScalingGroup with $Latest as the environment admin: {r:?}");
|
||||
if r.is_ok() {
|
||||
eprintln!("RESULT WARNING: $Latest was allowed");
|
||||
delete_group(pa).await;
|
||||
}
|
||||
// The control: the same create pinned to version 1 is allowed
|
||||
// (InstanceTypes filled from the template).
|
||||
let pb = format!("{stem}-pb");
|
||||
let r = create_group(asg_env.clone(), pb.clone(), "1".into(), probe_tags(&pb)).await;
|
||||
eprintln!("RESULT probe: the same create pinned to version 1 (control): {r:?}");
|
||||
if r.is_ok() {
|
||||
let d = asg_env.delete_auto_scaling_group().auto_scaling_group_name(&pb).send().await;
|
||||
eprintln!("RESULT probe: DeleteAutoScalingGroup of it as the environment admin: {:?}", d.map(|_| ()).map_err(|e| classify_sdk_error("autoscaling:DeleteAutoScalingGroup", &e).to_string()));
|
||||
delete_group(pb).await;
|
||||
}
|
||||
// A template version naming an unsupported type, then a group pinned to it.
|
||||
let (made, out, err) = aws_cli(
|
||||
Some(&akeys),
|
||||
®ion,
|
||||
&["ec2", "create-launch-template-version", "--launch-template-name", &stem, "--source-version", "1",
|
||||
"--launch-template-data", r#"{"InstanceType":"p5.48xlarge"}"#, "--query", "LaunchTemplateVersion.VersionNumber"],
|
||||
)
|
||||
.await;
|
||||
eprintln!("RESULT probe: CreateLaunchTemplateVersion with p5.48xlarge as the environment admin: ok {made} {} {}", out.trim(), err.trim());
|
||||
if made {
|
||||
let pc = format!("{stem}-pc");
|
||||
let r = create_group(asg_env.clone(), pc.clone(), out.trim().to_string(), probe_tags(&pc)).await;
|
||||
eprintln!("RESULT probe: CreateAutoScalingGroup pinned to the p5.48xlarge version: {r:?}");
|
||||
if r.is_ok() {
|
||||
eprintln!("RESULT WARNING: a group of an unsupported type was allowed");
|
||||
delete_group(pc).await;
|
||||
}
|
||||
}
|
||||
// A Triple-C capacity provider over a group that isn't Triple-C's.
|
||||
let foreign_group = format!("e2e-untagged-{}", &short[..10]);
|
||||
p5_lock(&p5).probe_groups.push(foreign_group.clone());
|
||||
let r = create_group(asg_admin.clone(), foreign_group.clone(), "1".into(), Vec::new()).await;
|
||||
eprintln!("RESULT probe: account admin creates the untagged group {foreign_group}: {r:?}");
|
||||
if r.is_ok() {
|
||||
let arn = ok("describe the untagged group", asg_admin.describe_auto_scaling_groups().auto_scaling_group_names(&foreign_group).send().await)
|
||||
.auto_scaling_groups()
|
||||
.first()
|
||||
.and_then(|g| g.auto_scaling_group_arn().map(str::to_string))
|
||||
.unwrap();
|
||||
let pd = format!("{stem}-pd");
|
||||
let r = ecs_env
|
||||
.create_capacity_provider()
|
||||
.name(&pd)
|
||||
.auto_scaling_group_provider(
|
||||
aws_sdk_ecs::types::AutoScalingGroupProvider::builder()
|
||||
.auto_scaling_group_arn(arn)
|
||||
.managed_termination_protection(aws_sdk_ecs::types::ManagedTerminationProtection::Disabled)
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
.set_tags(Some(
|
||||
group_tags
|
||||
.iter()
|
||||
.filter(|(k, _)| k.starts_with("triple-c:"))
|
||||
.map(|(k, v)| aws_sdk_ecs::types::Tag::builder().key(k).value(v).build())
|
||||
.collect(),
|
||||
))
|
||||
.send()
|
||||
.await;
|
||||
eprintln!(
|
||||
"RESULT probe: CreateCapacityProvider {pd} over the untagged group, as the environment admin: {:?}",
|
||||
r.as_ref().map(|_| ()).map_err(|e| classify_sdk_error("ecs:CreateCapacityProvider", e).to_string())
|
||||
);
|
||||
if r.is_ok() {
|
||||
eprintln!("RESULT WARNING: a Triple-C capacity provider over a foreign group was allowed");
|
||||
let _ = ecs_admin.delete_capacity_provider().capacity_provider(&pd).send().await;
|
||||
}
|
||||
delete_group(foreign_group).await;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 2. start 1: cold, with an instance launch ──
|
||||
let mut settings = AppSettings::default();
|
||||
settings.hosts = vec![conn];
|
||||
settings.image_source = ImageSource::Custom;
|
||||
settings.custom_image_name = Some(image.clone());
|
||||
let mut project = Project::new("e2e-ec2".into(), Vec::new());
|
||||
project.host_id = Some("e2e-wsuser".into());
|
||||
project.aws_placement = Some(AwsPlacement { region: region.clone(), environment: name.clone() });
|
||||
project.allow_docker_access = true;
|
||||
project.vpn_support_enabled = true;
|
||||
project.sandbox_mode_enabled = true;
|
||||
project.aws = Some(AwsWorkspaceConfig {
|
||||
launch_type: LaunchType::Ec2,
|
||||
cpu: 1024,
|
||||
memory_mib: 4096,
|
||||
storage: StorageMode::Hybrid,
|
||||
wip_push_on_stop: true,
|
||||
..Default::default()
|
||||
});
|
||||
let id = project.id.clone();
|
||||
{
|
||||
let mut g = p5_lock(&p5);
|
||||
g.project_id = Some(id.clone());
|
||||
g.secrets.push(crate::aws::tofu_modules::project_secret_name(&name, &okey, &id));
|
||||
}
|
||||
let store_dir = tempfile::tempdir().unwrap();
|
||||
let store = ProjectsStore::over_file_for_test(store_dir.path().join("projects.json"), vec![project.clone()]);
|
||||
let host = ProjectHost::for_project(&project);
|
||||
let current = |store: &ProjectsStore| store.get(&id).unwrap();
|
||||
let api = SdkTaskApi::new(&ws_cfg);
|
||||
let steps: Arc<std::sync::Mutex<Vec<(Duration, String)>>> = Default::default();
|
||||
let t0 = std::sync::Mutex::new(std::time::Instant::now());
|
||||
let progress = |m: &str| {
|
||||
let at = t0.lock().unwrap().elapsed();
|
||||
eprintln!(" progress +{at:?}: {m}");
|
||||
steps.lock().unwrap().push((at, m.to_string()));
|
||||
};
|
||||
|
||||
let started = host.ensure_started(¤t(&store), &settings, &store, &progress).await;
|
||||
if let Err(err) = &started {
|
||||
// Debugging aid: hold the workspace for an admin to look
|
||||
// at before the guard tears it down.
|
||||
let pause: u64 = std::env::var("TRIPLEC_E2E_PAUSE_SECS").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
|
||||
if pause > 0 {
|
||||
eprintln!("RESULT start 1 failed ({err}); pausing {pause}s for inspection (task {:?})", current(&store).aws.and_then(|a| a.task_arn));
|
||||
tokio::time::sleep(Duration::from_secs(pause)).await;
|
||||
}
|
||||
}
|
||||
let target = started.expect("EC2 start 1");
|
||||
let first_start = t0.lock().unwrap().elapsed();
|
||||
let s1: Vec<(Duration, String)> = std::mem::take(&mut *steps.lock().unwrap());
|
||||
eprintln!("RESULT EC2 start 1 (cold, instance launch): {first_start:?}; steps {s1:?}");
|
||||
assert!(s1.iter().any(|(_, m)| m == PROGRESS_INSTANCE), "no \"Starting an instance\" step: {s1:?}");
|
||||
let aws1 = current(&store).aws.unwrap();
|
||||
let task1 = aws1.task_arn.clone().expect("the task ARN is stored");
|
||||
let revision1 = aws1.task_definition_arn.clone().expect("the revision is stored");
|
||||
assert!(task1.contains(&format!(":task/{stem}/")), "the task runs on the user's cluster: {task1}");
|
||||
let d = api.describe(&rec.cluster_arn, std::slice::from_ref(&task1)).await.expect("describe task 1");
|
||||
eprintln!("RESULT task 1: {:?}", d.first().map(|t| (&t.last_status, &t.container_instance_arn)));
|
||||
eprintln!("RESULT group after start 1: {:?}", group_size(&asg_admin, &stem).await);
|
||||
let td = ok("describe revision 1", ecs_admin.describe_task_definition().task_definition(&revision1).send().await);
|
||||
let td = td.task_definition().unwrap();
|
||||
eprintln!(
|
||||
"RESULT revision 1: network {:?}, compat {:?}, volumes {:?}",
|
||||
td.network_mode(),
|
||||
td.requires_compatibilities(),
|
||||
td.volumes().iter().map(|v| v.name().unwrap_or_default()).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
// The cold start's first (ECS Exec off) task is gone: exactly one
|
||||
// live task of the project on the user's cluster.
|
||||
let family = crate::aws::tofu_modules::task_family(&name, &okey, &id);
|
||||
let live = api.list_project_tasks(&rec.cluster_arn, &family).await.expect("list the project's tasks");
|
||||
eprintln!("RESULT live tasks on the user's cluster after start 1: {live:?}");
|
||||
assert_eq!(live, vec![task1.clone()], "exactly one live task");
|
||||
|
||||
// ── through the agent (ECS Exec port forward into the bridge task) ──
|
||||
let (out, code) = sh(&target, "claude", "echo hello-from-ec2; id").await;
|
||||
eprintln!("RESULT agent tunnel over ECS Exec into the bridge-mode task: exit {code}: {}", out.trim());
|
||||
assert_eq!(code, 0, "{out}");
|
||||
// awslogs on EC2 writes with the execution role (the instance
|
||||
// role has no log rights), into the user's own streams only.
|
||||
let env_rec = crate::aws::runtime::read_environment(&SdkRuntimeApi::new(&ws_cfg), &name, &account, ®ion)
|
||||
.await
|
||||
.expect("the environment record, as the workspace user");
|
||||
let stream = crate::aws::tasks::task_log_stream(&crate::aws::tofu_modules::log_stream_prefix(&okey, &id), &task1);
|
||||
let log1 = api.log_tail(&env_rec.log_group, &stream, 20).await.expect("read task 1's log");
|
||||
eprintln!("RESULT task 1 log ({stream}): {} lines, last {:?}", log1.len(), log1.last());
|
||||
assert!(!log1.is_empty(), "task 1 wrote no log lines: awslogs could not write with the execution role");
|
||||
// E8: the sandbox is forced off in the task's settings.
|
||||
let (settings_json, code) = sh(&target, "claude", "printenv CLAUDE_CODE_SETTINGS_JSON").await;
|
||||
assert_eq!(code, 0, "no CLAUDE_CODE_SETTINGS_JSON");
|
||||
let v: serde_json::Value = serde_json::from_str(settings_json.trim()).expect("settings JSON");
|
||||
eprintln!("RESULT sandbox in the task's Claude Code settings: {}", v["sandbox"]);
|
||||
assert_eq!(v["sandbox"]["enabled"], false, "the sandbox is off on EC2");
|
||||
let (mut docker_ok, mut vpn_ok, mut src_valid_mark, mut bwrap_ok) = (true, true, true, false);
|
||||
if !quick {
|
||||
let (out, code) = sh(&target, "claude", "ls -l /var/run/docker.sock; docker ps 2>&1; docker version --format '{{.Server.Version}}' 2>&1").await;
|
||||
eprintln!("RESULT docker ps as claude (exit {code}):\n{}", out.trim());
|
||||
docker_ok = code == 0;
|
||||
let (out, code) = sh(
|
||||
&target,
|
||||
"root",
|
||||
"ip link add wg0 type wireguard && ip link show wg0 && ip link del wg0 && echo WG-OK; \
|
||||
ip tuntap add tun0 mode tun && ip link show tun0 && ip link del tun0 && echo TUN-OK; \
|
||||
ls -l /dev/net/tun; sysctl net.ipv4.conf.all.src_valid_mark; grep CapEff /proc/self/status",
|
||||
)
|
||||
.await;
|
||||
eprintln!("RESULT VPN pieces as root (exit {code}):\n{}", out.trim());
|
||||
let (sudo_out, _) = sh(&target, "claude", "sudo -n ip link add wg1 type wireguard && sudo -n ip link del wg1 && echo SUDO-WG-OK 2>&1").await;
|
||||
eprintln!("RESULT wireguard via sudo as claude: {}", sudo_out.trim());
|
||||
vpn_ok = out.contains("WG-OK") || out.contains("TUN-OK");
|
||||
src_valid_mark = out.contains("src_valid_mark = 1");
|
||||
let (bwrap, _) = sh(
|
||||
&target,
|
||||
"claude",
|
||||
"bwrap --unshare-user --ro-bind / / true 2>&1; echo \"unshare-user rc=$?\"; \
|
||||
bwrap --ro-bind / / --dev /dev --proc /proc --unshare-all --die-with-parent true 2>&1; echo \"unshare-all rc=$?\"; \
|
||||
cat /proc/sys/user/max_user_namespaces 2>&1; uname -r",
|
||||
)
|
||||
.await;
|
||||
eprintln!("RESULT E8 bwrap on EC2:\n{}", bwrap.trim());
|
||||
bwrap_ok = bwrap.contains("unshare-user rc=0");
|
||||
let (imds, _) = sh(
|
||||
&target,
|
||||
"claude",
|
||||
"tok=$(curl -s -m 3 -X PUT http://169.254.169.254/latest/api/token -H 'X-aws-ec2-metadata-token-ttl-seconds: 60'); \
|
||||
echo \"token rc=$? len=${#tok}\"; \
|
||||
curl -s -m 3 -o /dev/null -w 'imdsv1 http=%{http_code}\\n' http://169.254.169.254/latest/meta-data/; \
|
||||
gw=$(ip route | awk '/default/ {print $3}'); echo \"gateway $gw\"; \
|
||||
for h in \"$gw\" 127.0.0.1; do curl -s -m 3 -o /dev/null -w \"introspection $h:51678 http=%{http_code}\\n\" http://$h:51678/v1/metadata; done; \
|
||||
curl -s -m 3 http://$gw:51678/v1/metadata | head -c 300; echo; \
|
||||
test -n \"$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI\" && curl -s -m 3 http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI | grep -q AccessKeyId && echo TASK-ROLE-CREDS-OK || echo task-role-creds-missing",
|
||||
)
|
||||
.await;
|
||||
eprintln!("RESULT IMDS / introspection from the container:\n{}", imds.trim());
|
||||
assert!(imds.contains("len=0"), "the container got an IMDS token: {imds}");
|
||||
|
||||
// ── 4. isolation: RunTask on another triple-c-user's cluster ──
|
||||
let other = format!("e2eother{}", &short[6..10]);
|
||||
let other_cluster = user_ec2_stem(&name, &other);
|
||||
let made = ok("admin creates another user's (empty) cluster", ecs_admin.create_cluster().cluster_name(&other_cluster).send().await);
|
||||
let other_arn = made.cluster().and_then(|c| c.cluster_arn()).unwrap().to_string();
|
||||
let fake = UserCompute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 1,
|
||||
user_tag: other.clone(),
|
||||
cluster_arn: other_arn.clone(),
|
||||
capacity_provider: other_cluster.clone(),
|
||||
};
|
||||
let r = api.run_task(ec2_run_task_request(&fake, &revision1, &workspace_tags(&owner, &id, &name))).await.map_err(ec2_run_error);
|
||||
eprintln!("RESULT RunTask on {other_cluster} as triple-c-user {user_tag}: {r:?}");
|
||||
match r {
|
||||
Err(crate::aws::error::AwsError::AccessDenied { message, .. }) => assert!(message.contains(EC2_TAG_MISMATCH), "{message}"),
|
||||
other => panic!("RunTask on another user's cluster: expected AccessDenied, got {other:?}"),
|
||||
}
|
||||
let _ = ecs_admin.delete_cluster().cluster(&other_arn).send().await;
|
||||
}
|
||||
|
||||
// ── 5. stop 1, then the scale-in to zero ──
|
||||
let t2 = std::time::Instant::now();
|
||||
host.stop_project(¤t(&store), &settings).await.expect("stop 1");
|
||||
poll(Duration::from_secs(300), Duration::from_secs(5), || async {
|
||||
let d = api.describe(&rec.cluster_arn, std::slice::from_ref(&task1)).await.ok()?;
|
||||
(d.first()?.last_status == "STOPPED").then_some(())
|
||||
})
|
||||
.await
|
||||
.expect("task 1 never stopped");
|
||||
let stopped_after = t2.elapsed();
|
||||
eprintln!("RESULT stop 1: STOPPED after {stopped_after:?}");
|
||||
store.set_aws_runtime(&id, None, aws1.access_point_id.clone(), Some(revision1.clone()), None).unwrap();
|
||||
let mut scale_in = Duration::ZERO;
|
||||
if !quick {
|
||||
let mut last: Option<(usize, i32)> = None;
|
||||
let mut scaled = None;
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(40 * 60);
|
||||
while std::time::Instant::now() < deadline {
|
||||
let now = group_size(&asg_admin, &stem).await;
|
||||
let key = now.as_ref().map(|n| (n.0, n.1));
|
||||
if key != last {
|
||||
eprintln!(" group +{:?} after STOPPED: {now:?}", t2.elapsed() - stopped_after);
|
||||
last = key;
|
||||
}
|
||||
if key == Some((0, 0)) {
|
||||
scaled = Some(());
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
scale_in = t2.elapsed() - stopped_after;
|
||||
eprintln!("RESULT scale-in to 0 instances: {scaled:?} after {:.1} min from STOPPED", scale_in.as_secs_f64() / 60.0);
|
||||
assert!(scaled.is_some(), "the group did not scale back to zero within 40 minutes");
|
||||
}
|
||||
|
||||
// ── start 2 ──
|
||||
*t0.lock().unwrap() = std::time::Instant::now();
|
||||
let target2 = host.ensure_started(¤t(&store), &settings, &store, &progress).await.expect("EC2 start 2");
|
||||
let second_start = t0.lock().unwrap().elapsed();
|
||||
let s2: Vec<(Duration, String)> = std::mem::take(&mut *steps.lock().unwrap());
|
||||
eprintln!("RESULT EC2 start 2 ({}): {second_start:?}; steps {s2:?}", if quick { "warm: right after stop 1" } else { "after scale-in" });
|
||||
let (out, code) = sh(&target2, "claude", "echo again; docker ps -q | wc -l").await;
|
||||
assert_eq!(code, 0, "{out}");
|
||||
let aws2 = current(&store).aws.unwrap();
|
||||
let task2 = aws2.task_arn.clone().unwrap();
|
||||
host.stop_project(¤t(&store), &settings).await.expect("stop 2");
|
||||
poll(Duration::from_secs(300), Duration::from_secs(5), || async {
|
||||
let d = api.describe(&rec.cluster_arn, std::slice::from_ref(&task2)).await.ok()?;
|
||||
(d.first()?.last_status == "STOPPED").then_some(())
|
||||
})
|
||||
.await
|
||||
.expect("task 2 never stopped");
|
||||
store.set_aws_runtime(&id, None, aws2.access_point_id.clone(), aws2.task_definition_arn.clone(), None).unwrap();
|
||||
let removed = host.remove_aws_resources(¤t(&store), &settings, true).await;
|
||||
eprintln!("RESULT remove_aws_resources: {:?}", removed.as_ref().map_err(|e| e.chars().take(400).collect::<String>()));
|
||||
removed.expect("remove the project's resources");
|
||||
|
||||
// ── 7. the onboarding re-applied without EC2, an instance still up ──
|
||||
eprintln!("RESULT group before the re-apply without EC2: {:?}", group_size(&asg_admin, &stem).await);
|
||||
let (keys, acfg, _) = t.session(&admin).await;
|
||||
let vars = prepare_onboarding(&SdkOnboardApi::new(&acfg), &name, ®ion, &account, &owner, Some(Ec2Choice::None))
|
||||
.await
|
||||
.expect("the pre-apply step without EC2");
|
||||
let t5 = std::time::Instant::now();
|
||||
let (_, outcome, denied) = t
|
||||
.run(ModuleKind::UserOnboard, Deployment::User { env: &name, owner_key: &okey }, &vars, TofuOp::Apply, t.keys_env(&keys))
|
||||
.await;
|
||||
eprintln!("RESULT re-apply without EC2: {:?} in {:?}", outcome.summary, t5.elapsed());
|
||||
assert_ok("re-apply the onboarding without EC2", &outcome, &denied);
|
||||
let g = group_size(&asg_admin, &stem).await;
|
||||
let inst = live_env_instances(®ion, &name).await;
|
||||
let cps = ecs_admin.describe_capacity_providers().capacity_providers(&stem).send().await;
|
||||
let cl = ecs_admin.describe_clusters().clusters(&stem).send().await;
|
||||
let (_, lt, lt_err) = aws_cli(None, ®ion, &["ec2", "describe-launch-templates", "--launch-template-names", &stem]).await;
|
||||
let prof = t.e.iam.get_instance_profile().instance_profile_name(&role_name).send().await;
|
||||
let role = t.e.iam.get_role().role_name(&role_name).send().await;
|
||||
let record = read_user_record(&SdkRuntimeApi::new(&ws_cfg), &name, &okey, &account, ®ion).await;
|
||||
eprintln!(
|
||||
"RESULT after the re-apply: group {g:?}; live instances {inst:?}; provider {:?}; cluster {:?}; template {} {}; profile gone {}; role gone {}; record {record:?}",
|
||||
cps.as_ref().map(|o| o.capacity_providers().iter().map(|c| c.status().cloned()).collect::<Vec<_>>()),
|
||||
cl.as_ref().map(|o| o.clusters().iter().map(|c| c.status().map(str::to_string)).collect::<Vec<_>>()),
|
||||
lt.trim().chars().take(100).collect::<String>(),
|
||||
lt_err.trim().chars().take(100).collect::<String>(),
|
||||
prof.is_err(),
|
||||
role.is_err(),
|
||||
);
|
||||
assert!(g.is_none(), "the group is gone");
|
||||
assert!(inst.is_empty(), "no instance is left: {inst:?}");
|
||||
assert!(prof.is_err() && role.is_err(), "the instance profile and role are gone");
|
||||
assert!(matches!(record, Ok(UserRecord::Absent)), "{record:?}");
|
||||
|
||||
// ── offboard, then destroy the environment, as the environment admin ──
|
||||
if let Some(role) = p5_lock(&p5).exec_role.take() {
|
||||
let _ = t.e.iam.delete_role_policy().role_name(&role).policy_name(ECR_POLICY).send().await;
|
||||
}
|
||||
let (keys, _, _) = t.session(&admin).await;
|
||||
let (_, outcome, denied) = t
|
||||
.run(ModuleKind::UserOnboard, Deployment::User { env: &name, owner_key: &okey }, &vars, TofuOp::Destroy, t.keys_env(&keys))
|
||||
.await;
|
||||
assert_ok("offboard", &outcome, &denied);
|
||||
let (_, outcome, denied) = t
|
||||
.run(
|
||||
ModuleKind::Environment,
|
||||
Deployment::Environment(&name),
|
||||
&environment_vars(&name, ®ion, &NetworkChoice::Public, 0).unwrap(),
|
||||
TofuOp::Destroy,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
.await;
|
||||
assert_ok("destroy the environment", &outcome, &denied);
|
||||
eprintln!(
|
||||
"RESULT summary: docker {docker_ok}, vpn {vpn_ok}, src_valid_mark {src_valid_mark}, bwrap {bwrap_ok}, start1 {first_start:?}, scale-in {:.1} min, start2 {second_start:?}",
|
||||
scale_in.as_secs_f64() / 60.0
|
||||
);
|
||||
let _ = ws;
|
||||
assert!(docker_ok, "docker ps failed as claude");
|
||||
// E8, stable in every run: Docker's seccomp profile refuses it.
|
||||
assert!(quick || !bwrap_ok, "bwrap worked in the task: revisit sandbox_allowed (E8)");
|
||||
assert!(vpn_ok && src_valid_mark, "the VPN pieces are missing");
|
||||
});
|
||||
let body = async {
|
||||
if tokio::time::timeout(PHASE7_TIMEOUT, inner).await.is_err() {
|
||||
panic!("the phase-7 body did not finish in {PHASE7_TIMEOUT:?}");
|
||||
}
|
||||
};
|
||||
let result = std::panic::AssertUnwindSafe(body).catch_unwind().await;
|
||||
let guard = std::mem::take(&mut *p5_lock(&p5));
|
||||
phase5_cleanup(&t.e, &guard).await;
|
||||
if let Err(panic) = result {
|
||||
std::panic::resume_unwind(panic);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -37,8 +37,20 @@ impl ContextEntry {
|
||||
pub fn is_list(&self) -> bool {
|
||||
self.values.len() > 1
|
||||
}
|
||||
|
||||
/// Keys IAM types as Boolean (a `Bool` condition does not match a
|
||||
/// String-typed simulation value).
|
||||
pub fn is_boolean(&self) -> bool {
|
||||
BOOLEAN_KEYS.contains(&self.key.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// The Boolean condition keys the checks send.
|
||||
const BOOLEAN_KEYS: &[&str] = &[
|
||||
"autoscaling:LaunchTemplateVersionSpecified",
|
||||
"ec2:IsLaunchTemplateResource",
|
||||
];
|
||||
|
||||
/// Builds the simulation for `check`: the resource and every literal context
|
||||
/// value rendered with `p`, `OwnerId` as `user_id`, and `aws:userid` =
|
||||
/// `user_id` added. A key appearing more than once becomes one entry holding
|
||||
@@ -175,7 +187,9 @@ impl PermissionApi for SdkPermissionApi {
|
||||
SdkEntry::builder()
|
||||
.context_key_name(&e.key)
|
||||
.set_context_key_values(Some(e.values.clone()))
|
||||
.context_key_type(if e.is_list() {
|
||||
.context_key_type(if e.is_boolean() {
|
||||
ContextKeyTypeEnum::Boolean
|
||||
} else if e.is_list() {
|
||||
ContextKeyTypeEnum::StringList
|
||||
} else {
|
||||
ContextKeyTypeEnum::String
|
||||
|
||||
@@ -344,6 +344,29 @@ fn a_repeated_context_key_is_one_string_list_entry() {
|
||||
);
|
||||
assert!(req.context[0].is_list());
|
||||
assert!(!req.context[1].is_list());
|
||||
assert!(!req.context[1].is_boolean());
|
||||
}
|
||||
|
||||
/// Every Boolean-typed key a check sends is one `is_boolean` knows, so the
|
||||
/// simulator gets it as Boolean (a `Bool` condition never matches a String).
|
||||
#[test]
|
||||
fn boolean_context_keys_are_sent_as_booleans() {
|
||||
let mut seen = 0;
|
||||
for tier in [Tier::WorkspaceUser, Tier::EnvironmentAdmin] {
|
||||
for c in checks(tier) {
|
||||
for (key, value) in c.context {
|
||||
if matches!(value, ContextValue::Literal("true" | "false")) {
|
||||
let e = ContextEntry {
|
||||
key: key.to_string(),
|
||||
values: vec!["true".into()],
|
||||
};
|
||||
assert!(e.is_boolean(), "{key} is sent as a String");
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(seen >= 2, "only {seen} Boolean keys");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -208,10 +208,11 @@ pub(crate) fn conditions_hold(stmt: &Value, req: &Request) -> bool {
|
||||
// A negated operator holds when the key is absent, and
|
||||
// otherwise when no value matches.
|
||||
let (matches, negated): (fn(&str, &str) -> bool, bool) = match base {
|
||||
"StringEquals" => (|p, v| p == v, false),
|
||||
// IAM's Bool compares "true"/"false"; the checks send strings.
|
||||
"StringEquals" | "Bool" => (|p, v| p == v, false),
|
||||
"StringLike" | "ArnLike" => (glob, false),
|
||||
"StringNotEquals" => (|p, v| p == v, true),
|
||||
"StringNotLike" => (glob, true),
|
||||
"StringNotLike" | "ArnNotLike" => (glob, true),
|
||||
other => panic!("{}: unsupported condition operator {other}", sid(stmt)),
|
||||
};
|
||||
entries
|
||||
@@ -268,6 +269,37 @@ mod tests {
|
||||
assert!(!glob(&literal, "arn:aws:ec2:us-west-2:1:elastic-ip/eipalloc-1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_needs_the_key_and_bool_if_exists_does_not() {
|
||||
let stmt = |op: &str| -> Value {
|
||||
serde_json::json!({
|
||||
"Sid": "T", "Effect": "Allow", "Action": "x:Y", "Resource": "*",
|
||||
"Condition": { op: { "k:Pinned": "true" } }
|
||||
})
|
||||
};
|
||||
let holds = |op: &str, context| {
|
||||
let c = Check {
|
||||
action: "x:Y",
|
||||
resource: "*",
|
||||
context,
|
||||
write: true,
|
||||
};
|
||||
conditions_hold(
|
||||
&stmt(op),
|
||||
&Request {
|
||||
check: &c,
|
||||
caller: ME,
|
||||
owner: ME,
|
||||
},
|
||||
)
|
||||
};
|
||||
assert!(holds("Bool", &[("k:Pinned", Literal("true"))]));
|
||||
assert!(!holds("Bool", &[("k:Pinned", Literal("false"))]));
|
||||
assert!(!holds("Bool", &[]));
|
||||
assert!(holds("BoolIfExists", &[]));
|
||||
assert!(!holds("BoolIfExists", &[("k:Pinned", Literal("false"))]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn for_all_values_is_vacuous_on_an_absent_key() {
|
||||
let stmt: Value = serde_json::json!({
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! The estimated hourly cost of a Fargate workspace (Q5): on-demand rates
|
||||
//! from the Price List API (`pricing:GetProducts`, service `AmazonECS`,
|
||||
//! served from us-east-1 only), cached on disk per region for a day.
|
||||
//! served from us-east-1 only), cached on disk per region for a day. An EC2
|
||||
//! workspace (phase 7 E10) is priced as its instance: the `AmazonEC2`
|
||||
//! on-demand Linux, shared-tenancy price of the user's instance type, cached
|
||||
//! per region and type the same way.
|
||||
//!
|
||||
//! Everything here is best effort. A denied call, no network, a slow answer
|
||||
//! or a price list we can't read all end in "no estimate" (`None`), never an
|
||||
@@ -13,6 +16,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -22,6 +26,8 @@ use crate::models::project::{Arch, AwsWorkspaceConfig, StorageMode};
|
||||
/// eu-central-1; any of them serves every region's prices).
|
||||
pub const PRICING_REGION: &str = "us-east-1";
|
||||
pub const SERVICE_CODE: &str = "AmazonECS";
|
||||
/// EC2 instances' prices (phase 7 E10).
|
||||
pub const EC2_SERVICE_CODE: &str = "AmazonEC2";
|
||||
/// How long a region's rates are used before they are fetched again.
|
||||
pub const CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
/// The whole fetch (every page), so the command answers quickly offline.
|
||||
@@ -45,6 +51,79 @@ pub const FILTERS: &[(&str, &str, &str)] = &[
|
||||
("CONTAINS", "usagetype", "Fargate"),
|
||||
];
|
||||
|
||||
/// `GetProducts` filters for one EC2 instance type's on-demand Linux price
|
||||
/// (`{region}`, `{instance_type}` filled in). Verified against the live API
|
||||
/// (2026-11-16): exactly one row per type and region (`USW2-BoxUsage:m7i.large`,
|
||||
/// $0.1008/hour), carrying both its OnDemand and Reserved terms.
|
||||
pub const EC2_FILTERS: &[(&str, &str, &str)] = &[
|
||||
("TERM_MATCH", "regionCode", "{region}"),
|
||||
("TERM_MATCH", "instanceType", "{instance_type}"),
|
||||
("TERM_MATCH", "operatingSystem", "Linux"),
|
||||
("TERM_MATCH", "tenancy", "Shared"),
|
||||
("TERM_MATCH", "preInstalledSw", "NA"),
|
||||
("TERM_MATCH", "capacitystatus", "Used"),
|
||||
("TERM_MATCH", "licenseModel", "No License required"),
|
||||
];
|
||||
|
||||
/// `GetProducts` filters for gp3 EBS storage in `{region}`. Verified against
|
||||
/// the live API (2026-11-16): one row (`USW2-EBS:VolumeUsage.gp3`, $0.08 per
|
||||
/// GB-month).
|
||||
pub const GP3_FILTERS: &[(&str, &str, &str)] = &[
|
||||
("TERM_MATCH", "regionCode", "{region}"),
|
||||
("TERM_MATCH", "productFamily", "Storage"),
|
||||
("TERM_MATCH", "volumeApiName", "gp3"),
|
||||
];
|
||||
|
||||
/// The EC2 instance's root volume (`ec2.tf`'s `volume_size`, gp3), billed
|
||||
/// while the instance exists.
|
||||
pub const EC2_ROOT_VOLUME_GIB: u32 = 50;
|
||||
|
||||
/// Hours in a billing month, as AWS converts GB-month prices.
|
||||
pub const HOURS_PER_MONTH: f64 = 730.0;
|
||||
|
||||
/// One `GetProducts` query: a service and its filters, values filled in.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Query {
|
||||
pub service_code: &'static str,
|
||||
pub filters: Vec<(&'static str, &'static str, String)>,
|
||||
}
|
||||
|
||||
fn fill(filters: &[(&'static str, &'static str, &str)], vars: &[(&str, &str)]) -> Vec<(&'static str, &'static str, String)> {
|
||||
filters
|
||||
.iter()
|
||||
.map(|(kind, field, value)| {
|
||||
let value = vars
|
||||
.iter()
|
||||
.fold(value.to_string(), |v, (name, with)| v.replace(&format!("{{{name}}}"), with));
|
||||
(*kind, *field, value)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The Fargate rates of `region` ([`FILTERS`]).
|
||||
pub fn fargate_query(region: &str) -> Query {
|
||||
Query {
|
||||
service_code: SERVICE_CODE,
|
||||
filters: fill(FILTERS, &[("region", region)]),
|
||||
}
|
||||
}
|
||||
|
||||
/// The on-demand price of `instance_type` in `region` ([`EC2_FILTERS`]).
|
||||
pub fn ec2_query(region: &str, instance_type: &str) -> Query {
|
||||
Query {
|
||||
service_code: EC2_SERVICE_CODE,
|
||||
filters: fill(EC2_FILTERS, &[("region", region), ("instance_type", instance_type)]),
|
||||
}
|
||||
}
|
||||
|
||||
/// The gp3 storage price of `region` ([`GP3_FILTERS`]).
|
||||
pub fn gp3_query(region: &str) -> Query {
|
||||
Query {
|
||||
service_code: EC2_SERVICE_CODE,
|
||||
filters: fill(GP3_FILTERS, &[("region", region)]),
|
||||
}
|
||||
}
|
||||
|
||||
/// One architecture's on-demand rates in a region, in USD.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
|
||||
pub struct FargateRates {
|
||||
@@ -74,12 +153,19 @@ impl RegionRates {
|
||||
/// runs. A gateway sidecar shares the task's CPU and memory, so it adds
|
||||
/// nothing; Spot isn't in the Price List API (`note_spot`: say it is
|
||||
/// usually cheaper); EFS is billed per GB-month, not per hour
|
||||
/// (`efs_billed_separately`).
|
||||
/// (`efs_billed_separately`). `ec2_instance`: the figure is the user's EC2
|
||||
/// instance's hourly price, whatever the project reserves of it, and
|
||||
/// instances scale to zero when no workspace is running (E10);
|
||||
/// `root_volume_included`: it also has the instance's 50 GiB gp3 root
|
||||
/// volume (when its price could be read). An instance's public IPv4
|
||||
/// address (public-mode environments) is never in it.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
|
||||
pub struct HourlyEstimate {
|
||||
pub usd_per_hour: f64,
|
||||
pub note_spot: bool,
|
||||
pub efs_billed_separately: bool,
|
||||
pub ec2_instance: bool,
|
||||
pub root_volume_included: bool,
|
||||
}
|
||||
|
||||
pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimate {
|
||||
@@ -92,9 +178,77 @@ pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimat
|
||||
+ billed_storage * rates.storage_gb_hour,
|
||||
note_spot: cfg.spot,
|
||||
efs_billed_separately: cfg.storage != StorageMode::Ephemeral,
|
||||
ec2_instance: false,
|
||||
root_volume_included: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// An EC2 project's estimate: its instance's on-demand hourly price — the
|
||||
/// instance is billed whole while it runs, so the reservations, the
|
||||
/// ephemeral size and Spot (none on EC2) change nothing — plus its
|
||||
/// [`EC2_ROOT_VOLUME_GIB`] gp3 root volume at `gp3_usd_per_gb_month` (per
|
||||
/// hour, over [`HOURS_PER_MONTH`]) when that price is known.
|
||||
pub fn estimate_ec2(
|
||||
cfg: &AwsWorkspaceConfig,
|
||||
instance_usd_per_hour: f64,
|
||||
gp3_usd_per_gb_month: Option<f64>,
|
||||
) -> HourlyEstimate {
|
||||
let root = gp3_usd_per_gb_month.map(|p| f64::from(EC2_ROOT_VOLUME_GIB) * p / HOURS_PER_MONTH);
|
||||
HourlyEstimate {
|
||||
usd_per_hour: instance_usd_per_hour + root.unwrap_or(0.0),
|
||||
note_spot: false,
|
||||
efs_billed_separately: cfg.storage != StorageMode::Ephemeral,
|
||||
ec2_instance: true,
|
||||
root_volume_included: root.is_some(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The gp3 storage price per GB-month in a [`GP3_FILTERS`] price list for
|
||||
/// `region`; `None` when no row matches.
|
||||
pub fn parse_gp3_price(region: &str, items: &[String]) -> Option<f64> {
|
||||
items.iter().find_map(|item| {
|
||||
let product = serde_json::from_str::<Value>(item).ok()?;
|
||||
let attrs = &product["product"]["attributes"];
|
||||
let is = |k: &str, v: &str| attrs[k].as_str() == Some(v);
|
||||
if !(is("regionCode", region) && is("volumeApiName", "gp3")) {
|
||||
return None;
|
||||
}
|
||||
on_demand_usd(&product).filter(|p| *p > 0.0)
|
||||
})
|
||||
}
|
||||
|
||||
/// The on-demand hourly price of `instance_type` (Linux, shared tenancy) in
|
||||
/// an `AmazonEC2` price list for `region`. The filters already narrow it to
|
||||
/// one row; the attributes are checked again so a looser answer can't price
|
||||
/// the wrong product. `None` when no row matches.
|
||||
pub fn parse_ec2_price(region: &str, instance_type: &str, items: &[String]) -> Option<f64> {
|
||||
items.iter().find_map(|item| {
|
||||
let product = serde_json::from_str::<Value>(item).ok()?;
|
||||
let attrs = &product["product"]["attributes"];
|
||||
let is = |k: &str, v: &str| attrs[k].as_str() == Some(v);
|
||||
let matches = is("regionCode", region)
|
||||
&& is("instanceType", instance_type)
|
||||
&& is("operatingSystem", "Linux")
|
||||
&& is("tenancy", "Shared")
|
||||
&& is("preInstalledSw", "NA")
|
||||
&& is("capacitystatus", "Used")
|
||||
&& is("licenseModel", "No License required");
|
||||
if !matches {
|
||||
return None;
|
||||
}
|
||||
on_demand_usd(&product).filter(|p| *p > 0.0)
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `instance_type` is safe in a cache file name and a filter value
|
||||
/// (`m7i.large`): lowercase letters, digits, one dot.
|
||||
fn plain_instance_type(instance_type: &str) -> bool {
|
||||
instance_type.split_once('.').is_some_and(|(family, size)| {
|
||||
let ok = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit());
|
||||
ok(family) && ok(size)
|
||||
}) && instance_type.len() <= 32
|
||||
}
|
||||
|
||||
/// The rates in a `GetProducts` price list (each item one product's JSON)
|
||||
/// for `region`. `None` when no architecture is complete.
|
||||
pub fn parse_price_list(region: &str, items: &[String]) -> Option<RegionRates> {
|
||||
@@ -174,10 +328,10 @@ pub struct Page {
|
||||
|
||||
/// The Price List API, faked in tests.
|
||||
pub trait PricingApi {
|
||||
/// One page of `AmazonECS` products matching [`FILTERS`] for `region`.
|
||||
/// One page of the products `query` matches.
|
||||
fn get_products(
|
||||
&self,
|
||||
region: &str,
|
||||
query: &Query,
|
||||
next_token: Option<String>,
|
||||
) -> impl Future<Output = Result<Page, String>> + Send;
|
||||
}
|
||||
@@ -197,20 +351,20 @@ impl SdkPricingApi {
|
||||
}
|
||||
|
||||
impl PricingApi for SdkPricingApi {
|
||||
async fn get_products(&self, region: &str, next_token: Option<String>) -> Result<Page, String> {
|
||||
async fn get_products(&self, query: &Query, next_token: Option<String>) -> Result<Page, String> {
|
||||
use aws_sdk_pricing::types::{Filter, FilterType};
|
||||
let mut req = self
|
||||
.client
|
||||
.get_products()
|
||||
.service_code(SERVICE_CODE)
|
||||
.service_code(query.service_code)
|
||||
.format_version("aws_v1")
|
||||
.max_results(100)
|
||||
.set_next_token(next_token);
|
||||
for (kind, field, value) in FILTERS {
|
||||
for (kind, field, value) in &query.filters {
|
||||
let filter = Filter::builder()
|
||||
.r#type(FilterType::from(*kind))
|
||||
.field(*field)
|
||||
.value(value.replace("{region}", region))
|
||||
.value(value)
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?;
|
||||
req = req.filters(filter);
|
||||
@@ -226,31 +380,79 @@ impl PricingApi for SdkPricingApi {
|
||||
}
|
||||
}
|
||||
|
||||
/// Every page for `region`, parsed.
|
||||
pub async fn fetch_rates<A: PricingApi>(api: &A, region: &str) -> Result<RegionRates, String> {
|
||||
/// Every page `query` matches.
|
||||
async fn fetch_items<A: PricingApi>(api: &A, query: &Query) -> Result<Vec<String>, String> {
|
||||
let mut items = Vec::new();
|
||||
let mut token = None;
|
||||
for _ in 0..MAX_PAGES {
|
||||
let page = api.get_products(region, token).await?;
|
||||
let page = api.get_products(query, token).await?;
|
||||
items.extend(page.items);
|
||||
token = page.next_token.filter(|t| !t.is_empty());
|
||||
if token.is_none() {
|
||||
return parse_price_list(region, &items)
|
||||
.ok_or_else(|| format!("no Fargate rates for {region} in the price list"));
|
||||
return Ok(items);
|
||||
}
|
||||
}
|
||||
Err(format!(
|
||||
"the {region} price list has more than {MAX_PAGES} pages"
|
||||
))
|
||||
Err(format!("the {} price list has more than {MAX_PAGES} pages", query.service_code))
|
||||
}
|
||||
|
||||
/// `<dir>/<region>.json`.
|
||||
/// Every page for `region`, parsed (the live tests' direct call).
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
pub async fn fetch_rates<A: PricingApi>(api: &A, region: &str) -> Result<RegionRates, String> {
|
||||
let items = fetch_items(api, &fargate_query(region)).await?;
|
||||
parse_price_list(region, &items).ok_or_else(|| format!("no Fargate rates for {region} in the price list"))
|
||||
}
|
||||
|
||||
/// The on-demand hourly price of `instance_type` in `region` (the live
|
||||
/// tests' direct call).
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
pub async fn fetch_ec2_price<A: PricingApi>(api: &A, region: &str, instance_type: &str) -> Result<f64, String> {
|
||||
if !plain_instance_type(instance_type) {
|
||||
return Err(format!("{instance_type:?} is not an instance type"));
|
||||
}
|
||||
let items = fetch_items(api, &ec2_query(region, instance_type)).await?;
|
||||
parse_ec2_price(region, instance_type, &items)
|
||||
.ok_or_else(|| format!("no {instance_type} price for {region} in the price list"))
|
||||
}
|
||||
|
||||
/// One cached answer: Fargate rates under `<region>.json`, an instance's
|
||||
/// price under `<region>--ec2-<type>.json`. `key` is what the file must say
|
||||
/// it holds (the region, or `<region> <type>`), and what failures are
|
||||
/// remembered under.
|
||||
struct CacheKey {
|
||||
key: String,
|
||||
file: String,
|
||||
}
|
||||
|
||||
impl CacheKey {
|
||||
fn fargate(region: &str) -> Self {
|
||||
Self {
|
||||
key: region.to_string(),
|
||||
file: format!("{region}.json"),
|
||||
}
|
||||
}
|
||||
fn ec2(region: &str, instance_type: &str) -> Self {
|
||||
Self {
|
||||
key: format!("{region} {instance_type}"),
|
||||
file: format!("{region}--ec2-{instance_type}.json"),
|
||||
}
|
||||
}
|
||||
fn gp3(region: &str) -> Self {
|
||||
Self {
|
||||
key: format!("{region} gp3"),
|
||||
file: format!("{region}--ebs-gp3.json"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cache file. `key` was `region` before EC2 prices were cached too (an
|
||||
/// older file still reads).
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct CacheFile {
|
||||
region: String,
|
||||
struct CacheFile<V> {
|
||||
#[serde(alias = "region")]
|
||||
key: String,
|
||||
/// Unix seconds.
|
||||
fetched_at: u64,
|
||||
rates: RegionRates,
|
||||
rates: V,
|
||||
}
|
||||
|
||||
/// The per-region rate files and the memory of recent failures.
|
||||
@@ -281,18 +483,38 @@ impl RatesCache {
|
||||
&self.dir
|
||||
}
|
||||
|
||||
/// The region must already be validated (it names a file).
|
||||
fn path(&self, region: &str) -> PathBuf {
|
||||
self.dir.join(format!("{region}.json"))
|
||||
}
|
||||
|
||||
/// The cached rates and whether they are still fresh at `now`. A
|
||||
/// missing, unreadable or corrupt file, or one for another region, is
|
||||
/// no cache.
|
||||
/// no cache. The region must already be validated (it names a file).
|
||||
pub fn read(&self, region: &str, now: SystemTime) -> Option<(RegionRates, bool)> {
|
||||
let text = std::fs::read_to_string(self.path(region)).ok()?;
|
||||
let file: CacheFile = serde_json::from_str(&text).ok()?;
|
||||
if file.region != region {
|
||||
self.read_key(&CacheKey::fargate(region), now)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn write(&self, region: &str, rates: &RegionRates, now: SystemTime) -> Result<(), String> {
|
||||
self.write_key(&CacheKey::fargate(region), rates, now)
|
||||
}
|
||||
|
||||
/// [`Self::read`] for an instance type's hourly price.
|
||||
pub fn read_ec2(&self, region: &str, instance_type: &str, now: SystemTime) -> Option<(f64, bool)> {
|
||||
if !plain_instance_type(instance_type) {
|
||||
return None;
|
||||
}
|
||||
self.read_key(&CacheKey::ec2(region, instance_type), now)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn write_ec2(&self, region: &str, instance_type: &str, usd_per_hour: f64, now: SystemTime) -> Result<(), String> {
|
||||
if !plain_instance_type(instance_type) {
|
||||
return Err(format!("{instance_type:?} is not an instance type"));
|
||||
}
|
||||
self.write_key(&CacheKey::ec2(region, instance_type), &usd_per_hour, now)
|
||||
}
|
||||
|
||||
fn read_key<V: DeserializeOwned>(&self, key: &CacheKey, now: SystemTime) -> Option<(V, bool)> {
|
||||
let text = std::fs::read_to_string(self.dir.join(&key.file)).ok()?;
|
||||
let file: CacheFile<V> = serde_json::from_str(&text).ok()?;
|
||||
if file.key != key.key {
|
||||
return None;
|
||||
}
|
||||
let now = unix_secs(now);
|
||||
@@ -301,20 +523,20 @@ impl RatesCache {
|
||||
Some((file.rates, fresh))
|
||||
}
|
||||
|
||||
pub fn write(&self, region: &str, rates: &RegionRates, now: SystemTime) -> Result<(), String> {
|
||||
fn write_key<V: Serialize>(&self, key: &CacheKey, rates: &V, now: SystemTime) -> Result<(), String> {
|
||||
std::fs::create_dir_all(&self.dir).map_err(|e| e.to_string())?;
|
||||
let file = CacheFile {
|
||||
region: region.to_string(),
|
||||
key: key.key.clone(),
|
||||
fetched_at: unix_secs(now),
|
||||
rates: *rates,
|
||||
rates,
|
||||
};
|
||||
let text = serde_json::to_string_pretty(&file).map_err(|e| e.to_string())?;
|
||||
// Write then rename, so a concurrent reader never sees half a file.
|
||||
let tmp = self
|
||||
.dir
|
||||
.join(format!(".{region}.json.tmp-{}", std::process::id()));
|
||||
.join(format!(".{}.tmp-{}", key.file, std::process::id()));
|
||||
std::fs::write(&tmp, text).map_err(|e| e.to_string())?;
|
||||
std::fs::rename(&tmp, self.path(region)).map_err(|e| {
|
||||
std::fs::rename(&tmp, self.dir.join(&key.file)).map_err(|e| {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
e.to_string()
|
||||
})
|
||||
@@ -360,17 +582,74 @@ impl RatesCache {
|
||||
region: &str,
|
||||
now: SystemTime,
|
||||
) -> Option<RegionRates> {
|
||||
let cached = self.read(region, now);
|
||||
if let Some((rates, true)) = cached {
|
||||
return Some(rates);
|
||||
let parse = |items: &[String]| {
|
||||
parse_price_list(region, items).ok_or_else(|| format!("no Fargate rates for {region} in the price list"))
|
||||
};
|
||||
self.cached::<A, B, _>(&CacheKey::fargate(region), api, fargate_query(region), parse, now)
|
||||
.await
|
||||
}
|
||||
|
||||
/// [`Self::rates_with`] for `instance_type`'s on-demand hourly price in
|
||||
/// `region` (E10): the same cache rules, failure back-off and timeout.
|
||||
pub async fn ec2_price_with<A: PricingApi, B: std::borrow::Borrow<A>>(
|
||||
&self,
|
||||
api: impl Future<Output = Option<B>>,
|
||||
region: &str,
|
||||
instance_type: &str,
|
||||
now: SystemTime,
|
||||
) -> Option<f64> {
|
||||
if !plain_instance_type(instance_type) {
|
||||
return None;
|
||||
}
|
||||
let stale = cached.map(|(rates, _)| rates);
|
||||
if self.recently_failed(region, now) {
|
||||
let parse = |items: &[String]| {
|
||||
parse_ec2_price(region, instance_type, items)
|
||||
.ok_or_else(|| format!("no {instance_type} price for {region} in the price list"))
|
||||
};
|
||||
let query = ec2_query(region, instance_type);
|
||||
self.cached::<A, B, _>(&CacheKey::ec2(region, instance_type), api, query, parse, now)
|
||||
.await
|
||||
}
|
||||
|
||||
/// [`Self::rates_with`] for `region`'s gp3 storage price per GB-month
|
||||
/// (an EC2 instance's root volume).
|
||||
pub async fn gp3_price_with<A: PricingApi, B: std::borrow::Borrow<A>>(
|
||||
&self,
|
||||
api: impl Future<Output = Option<B>>,
|
||||
region: &str,
|
||||
now: SystemTime,
|
||||
) -> Option<f64> {
|
||||
let parse = |items: &[String]| {
|
||||
parse_gp3_price(region, items).ok_or_else(|| format!("no gp3 price for {region} in the price list"))
|
||||
};
|
||||
self.cached::<A, B, _>(&CacheKey::gp3(region), api, gp3_query(region), parse, now)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Fresh cache, else a fetch of `query` (client build included, within
|
||||
/// [`FETCH_TIMEOUT`]) parsed by `parse` that refreshes the cache, else
|
||||
/// the stale cache, else `None`; a key whose fetch failed is not asked
|
||||
/// again for [`RETRY_AFTER_FAILURE`].
|
||||
async fn cached<A: PricingApi, B: std::borrow::Borrow<A>, V: Serialize + DeserializeOwned + Clone>(
|
||||
&self,
|
||||
key: &CacheKey,
|
||||
api: impl Future<Output = Option<B>>,
|
||||
query: Query,
|
||||
parse: impl FnOnce(&[String]) -> Result<V, String>,
|
||||
now: SystemTime,
|
||||
) -> Option<V> {
|
||||
let cached = self.read_key::<V>(key, now);
|
||||
let stale = match cached {
|
||||
Some((rates, true)) => return Some(rates),
|
||||
Some((rates, false)) => Some(rates),
|
||||
None => None,
|
||||
};
|
||||
let what = &key.key;
|
||||
if self.recently_failed(what, now) {
|
||||
return stale;
|
||||
}
|
||||
let fetch = async {
|
||||
let api = api.await?;
|
||||
Some(fetch_rates(api.borrow(), region).await)
|
||||
Some(fetch_items(api.borrow(), &query).await.and_then(|items| parse(&items)))
|
||||
};
|
||||
let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch).await {
|
||||
Ok(Some(r)) => r,
|
||||
@@ -378,23 +657,23 @@ impl RatesCache {
|
||||
// one reads credentials, which can be slow), and the stale cache
|
||||
// answers.
|
||||
Ok(None) => {
|
||||
log::info!("No Fargate rates for {region}: no Pricing client could be built");
|
||||
self.note_failure(region, now, true);
|
||||
log::info!("No prices for {what}: no Pricing client could be built");
|
||||
self.note_failure(what, now, true);
|
||||
return stale;
|
||||
}
|
||||
Err(_) => Err(format!("timed out after {}s", FETCH_TIMEOUT.as_secs())),
|
||||
};
|
||||
match fetched {
|
||||
Ok(rates) => {
|
||||
self.note_failure(region, now, false);
|
||||
if let Err(e) = self.write(region, &rates, now) {
|
||||
log::warn!("Could not cache the {region} Fargate rates: {e}");
|
||||
self.note_failure(what, now, false);
|
||||
if let Err(e) = self.write_key(key, &rates, now) {
|
||||
log::warn!("Could not cache the prices for {what}: {e}");
|
||||
}
|
||||
Some(rates)
|
||||
}
|
||||
Err(e) => {
|
||||
log::info!("No Fargate rates for {region}: {e}");
|
||||
self.note_failure(region, now, true);
|
||||
log::info!("No prices for {what}: {e}");
|
||||
self.note_failure(what, now, true);
|
||||
stale
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"PriceList": [
|
||||
"{\"product\":{\"productFamily\":\"Storage\",\"attributes\":{\"maxThroughputvolume\":\"2000 MiB/s\",\"volumeType\":\"General Purpose\",\"maxIopsvolume\":\"80000\",\"usagetype\":\"USW2-EBS:VolumeUsage.gp3\",\"locationType\":\"AWS Region\",\"maxVolumeSize\":\"64 TiB\",\"storageMedia\":\"SSD-backed\",\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonEC2\",\"volumeApiName\":\"gp3\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"operation\":\"\",\"group\":\"EBS Storage\"},\"sku\":\"BB8UJWJ4XPFJB95G\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF\":{\"priceDimensions\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"GB-Mo\",\"endRange\":\"Inf\",\"description\":\"$0.08 per GB-month of General Purpose (gp3) provisioned storage - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0800000000\"}}},\"sku\":\"BB8UJWJ4XPFJB95G\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261114012926\",\"publicationDate\":\"2026-11-14T01:29:26Z\"}"
|
||||
],
|
||||
"FormatVersion": "aws_v1"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"FormatVersion": "aws_v1",
|
||||
"PriceList": [
|
||||
"{\"product\":{\"productFamily\":\"Compute Instance\",\"attributes\":{\"enhancedNetworkingSupported\":\"Yes\",\"intelTurboAvailable\":\"Yes\",\"memory\":\"8 GiB\",\"dedicatedEbsThroughput\":\"Up to 10000 Mbps\",\"vcpu\":\"2\",\"classicnetworkingsupport\":\"false\",\"capacitystatus\":\"Used\",\"locationType\":\"AWS Region\",\"storage\":\"EBS only\",\"instanceFamily\":\"General purpose\",\"operatingSystem\":\"Linux\",\"intelAvx2Available\":\"Yes\",\"instanceFamilyCategory\":\"General Purpose\",\"regionCode\":\"us-west-2\",\"physicalProcessor\":\"Intel Xeon Scalable (Sapphire Rapids)\",\"clockSpeed\":\"3.2 GHz\",\"ecu\":\"NA\",\"networkPerformance\":\"Up to 12500 Megabit\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"gpuMemory\":\"NA\",\"dedicatedEbsThroughputDescription\":\"650 Mbps\",\"vpcnetworkingsupport\":\"true\",\"instanceType\":\"m7i.large\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-BoxUsage:m7i.large\",\"normalizationSizeFactor\":\"4\",\"intelAvxAvailable\":\"Yes\",\"processorFeatures\":\"Intel AVX; Intel AVX2; Intel AVX512; Intel Turbo; Intel AMX\",\"servicecode\":\"AmazonEC2\",\"licenseModel\":\"No License required\",\"currentGeneration\":\"Yes\",\"preInstalledSw\":\"NA\",\"location\":\"US West (Oregon)\",\"processorArchitecture\":\"64-bit\",\"marketoption\":\"OnDemand\",\"operation\":\"RunInstances\",\"availabilityzone\":\"NA\"},\"sku\":\"76SYQ5S6SHTVH6CA\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"76SYQ5S6SHTVH6CA.JRTCKXETXF\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"$0.1008 per On Demand Linux m7i.large Instance Hour\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.1008000000\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}},\"Reserved\":{\"76SYQ5S6SHTVH6CA.VJWZNREJX2\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.VJWZNREJX2.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.VJWZNREJX2.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0000000000\"}},\"76SYQ5S6SHTVH6CA.VJWZNREJX2.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.VJWZNREJX2.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"605\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:11Z\",\"offerTermCode\":\"VJWZNREJX2\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"All Upfront\"}},\"76SYQ5S6SHTVH6CA.BPH4J8HBKS\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.BPH4J8HBKS.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.BPH4J8HBKS.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0457200000\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"BPH4J8HBKS\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"No Upfront\"}},\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0000000000\"}},\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.NQ3QZPMQV9.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"1046\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:12Z\",\"offerTermCode\":\"NQ3QZPMQV9\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"All Upfront\"}},\"76SYQ5S6SHTVH6CA.HU7G6KETJZ\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7i.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0317500000\"}},\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"76SYQ5S6SHTVH6CA.HU7G6KETJZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"278\"}}},\"sku\":\"76SYQ5S6SHTVH6CA\",\"effectiveDate\":\"2026-11-14T01:50:08Z\",\"offerTermCode\":\"HU7G6KETJZ\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"76SYQ5S6SHTVH6CA.38NPMPTW36\":{\"priceDimensions\":{\"76SYQ5S6SHTVH6CA.Line truncated
|
||||
"{\"product\":{\"productFamily\":\"Compute Instance\",\"attributes\":{\"enhancedNetworkingSupported\":\"Yes\",\"intelTurboAvailable\":\"No\",\"memory\":\"8 GiB\",\"dedicatedEbsThroughput\":\"Up to 10000 Mbps\",\"vcpu\":\"2\",\"classicnetworkingsupport\":\"false\",\"capacitystatus\":\"Used\",\"locationType\":\"AWS Region\",\"storage\":\"EBS only\",\"instanceFamily\":\"General purpose\",\"operatingSystem\":\"Linux\",\"intelAvx2Available\":\"No\",\"instanceFamilyCategory\":\"General Purpose\",\"regionCode\":\"us-west-2\",\"physicalProcessor\":\"AWS Graviton3 Processor\",\"clockSpeed\":\"2.5 GHz\",\"ecu\":\"NA\",\"networkPerformance\":\"Up to 12500 Megabit\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"gpuMemory\":\"NA\",\"dedicatedEbsThroughputDescription\":\"630 Mbps\",\"vpcnetworkingsupport\":\"true\",\"instanceType\":\"m7g.large\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-BoxUsage:m7g.large\",\"normalizationSizeFactor\":\"4\",\"intelAvxAvailable\":\"No\",\"servicecode\":\"AmazonEC2\",\"licenseModel\":\"No License required\",\"currentGeneration\":\"Yes\",\"preInstalledSw\":\"NA\",\"location\":\"US West (Oregon)\",\"processorArchitecture\":\"64-bit\",\"marketoption\":\"OnDemand\",\"operation\":\"RunInstances\",\"availabilityzone\":\"NA\"},\"sku\":\"NWX8JZYSP4RSV4Q2\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"NWX8JZYSP4RSV4Q2.JRTCKXETXF\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"$0.0816 per On Demand Linux m7g.large Instance Hour\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0816000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}},\"Reserved\":{\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0257000000\"}},\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.HU7G6KETJZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"225\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:10Z\",\"offerTermCode\":\"HU7G6KETJZ\",\"termAttributes\":{\"LeaseContractLength\":\"1yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"503\"}},\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.R5XV2EPZQZ.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0192000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"R5XV2EPZQZ\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.38NPMPTW36\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.38NPMPTW36.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.38NPMPTW36.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0171000000\"}},\"NWX8JZYSP4RSV4Q2.38NPMPTW36.2TG2D8R56U\":{\"unit\":\"Quantity\",\"description\":\"Upfront Fee\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.38NPMPTW36.2TG2D8R56U\",\"pricePerUnit\":{\"USD\":\"450\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:09Z\",\"offerTermCode\":\"38NPMPTW36\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"standard\",\"PurchaseOption\":\"Partial Upfront\"}},\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"Linux/UNIX (Amazon VPC), m7g.large reserved instance applied\",\"appliesTo\":[],\"rateCode\":\"NWX8JZYSP4RSV4Q2.Z2E3P23VKM.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0414000000\"}}},\"sku\":\"NWX8JZYSP4RSV4Q2\",\"effectiveDate\":\"2026-11-14T01:50:12Z\",\"offerTermCode\":\"Z2E3P23VKM\",\"termAttributes\":{\"LeaseContractLength\":\"3yr\",\"OfferingClass\":\"convertible\",\"PurchaseOption\":\"No Upfront\"}},\"NWX8JZYSP4RSV4Q2.NQ3QZPMQV9\":{\"priceDimensions\":{\"NWX8JZYSP4RSV4Q2.NQ3QZPMQV9.6YS6EN2CT7\":{\"unit\":\"Hrs\",\"endRange\":\"Inf\",\"description\":\"USD 0.0 per Linux/UNIX (Amazon VPC),Line truncated
|
||||
]
|
||||
}
|
||||
@@ -173,6 +173,7 @@ fn the_filters_name_the_region_on_demand_and_fargate() {
|
||||
struct FakeApi {
|
||||
pages: Mutex<Vec<Result<Page, String>>>,
|
||||
calls: AtomicUsize,
|
||||
queries: Mutex<Vec<Query>>,
|
||||
hang: bool,
|
||||
}
|
||||
|
||||
@@ -181,6 +182,7 @@ impl FakeApi {
|
||||
Self {
|
||||
pages: Mutex::new(pages),
|
||||
calls: AtomicUsize::new(0),
|
||||
queries: Mutex::new(Vec::new()),
|
||||
hang: false,
|
||||
}
|
||||
}
|
||||
@@ -205,8 +207,12 @@ impl FakeApi {
|
||||
}
|
||||
|
||||
impl PricingApi for FakeApi {
|
||||
async fn get_products(&self, region: &str, _token: Option<String>) -> Result<Page, String> {
|
||||
assert_eq!(region, "us-west-2");
|
||||
async fn get_products(&self, query: &Query, _token: Option<String>) -> Result<Page, String> {
|
||||
assert!(
|
||||
query.filters.contains(&("TERM_MATCH", "regionCode", "us-west-2".to_string())),
|
||||
"{query:?}"
|
||||
);
|
||||
self.queries.lock().unwrap().push(query.clone());
|
||||
self.calls.fetch_add(1, Ordering::SeqCst);
|
||||
if self.hang {
|
||||
std::future::pending::<()>().await;
|
||||
@@ -416,3 +422,228 @@ async fn no_client_counts_as_a_failure_and_is_not_asked_again_for_a_while() {
|
||||
assert_eq!(c.rates(Some(&api), "us-west-2", at(later)).await, Some(RATES));
|
||||
assert_eq!(api.calls(), 1);
|
||||
}
|
||||
|
||||
// ---- EC2 (phase 7 E10) ----
|
||||
|
||||
/// Real `AmazonEC2` `GetProducts` answers for us-west-2 with [`EC2_FILTERS`]
|
||||
/// (recorded 2026-11-16): one row each for m7i.large and m7g.large.
|
||||
const EC2_FIXTURE: &str = include_str!("fixtures/ec2-get-products-us-west-2.json");
|
||||
const M7I_LARGE: f64 = 0.1008;
|
||||
const M7G_LARGE: f64 = 0.0816;
|
||||
|
||||
fn ec2_items() -> Vec<String> {
|
||||
let v: Value = serde_json::from_str(EC2_FIXTURE).unwrap();
|
||||
v["PriceList"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|s| s.as_str().unwrap().to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn ec2_cfg(cpu: u32, memory_mib: u32, storage: StorageMode) -> AwsWorkspaceConfig {
|
||||
AwsWorkspaceConfig {
|
||||
launch_type: crate::models::project::LaunchType::Ec2,
|
||||
cpu,
|
||||
memory_mib,
|
||||
storage,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_recorded_ec2_price_list_gives_each_instance_types_hourly_price() {
|
||||
let items = ec2_items();
|
||||
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &items), Some(M7I_LARGE));
|
||||
assert_eq!(parse_ec2_price("us-west-2", "m7g.large", &items), Some(M7G_LARGE));
|
||||
// Not a type in the list, or another region: nothing.
|
||||
assert_eq!(parse_ec2_price("us-west-2", "c7i.large", &items), None);
|
||||
assert_eq!(parse_ec2_price("eu-west-1", "m7i.large", &items), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_row_for_other_software_tenancy_or_capacity_never_counts() {
|
||||
let row = ec2_items().into_iter().find(|s| s.contains("m7i.large")).unwrap();
|
||||
for (from, to) in [
|
||||
("\"tenancy\":\"Shared\"", "\"tenancy\":\"Dedicated\""),
|
||||
("\"preInstalledSw\":\"NA\"", "\"preInstalledSw\":\"SQL Web\""),
|
||||
("\"capacitystatus\":\"Used\"", "\"capacitystatus\":\"UnusedCapacityReservation\""),
|
||||
("\"operatingSystem\":\"Linux\"", "\"operatingSystem\":\"Windows\""),
|
||||
("\"licenseModel\":\"No License required\"", "\"licenseModel\":\"Bring your own license\""),
|
||||
] {
|
||||
assert!(row.contains(from), "{from}");
|
||||
let other = row.replace(from, to);
|
||||
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &[other]), None, "{to}");
|
||||
}
|
||||
// A zero price (what an unused reservation row says) is not a price.
|
||||
let zero = row.replace("0.1008000000", "0.0000000000");
|
||||
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &[zero]), None);
|
||||
assert_eq!(parse_ec2_price("us-west-2", "m7i.large", &["garbage".into()]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_ec2_filters_name_linux_shared_tenancy_on_demand_for_the_type() {
|
||||
assert_eq!(EC2_SERVICE_CODE, "AmazonEC2");
|
||||
let q = ec2_query("us-west-2", "m7i.large");
|
||||
assert_eq!(q.service_code, "AmazonEC2");
|
||||
let f = |field: &'static str, value: &str| ("TERM_MATCH", field, value.to_string());
|
||||
assert_eq!(
|
||||
q.filters,
|
||||
vec![
|
||||
f("regionCode", "us-west-2"),
|
||||
f("instanceType", "m7i.large"),
|
||||
f("operatingSystem", "Linux"),
|
||||
f("tenancy", "Shared"),
|
||||
f("preInstalledSw", "NA"),
|
||||
f("capacitystatus", "Used"),
|
||||
f("licenseModel", "No License required"),
|
||||
]
|
||||
);
|
||||
// The Fargate query is unchanged.
|
||||
let fq = fargate_query("us-west-2");
|
||||
assert_eq!(fq.service_code, "AmazonECS");
|
||||
assert_eq!(
|
||||
fq.filters,
|
||||
vec![
|
||||
("TERM_MATCH", "regionCode", "us-west-2".to_string()),
|
||||
("TERM_MATCH", "termType", "OnDemand".to_string()),
|
||||
("CONTAINS", "usagetype", "Fargate".to_string()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_estimate_is_the_instances_price_whatever_the_reservations() {
|
||||
let small = estimate_ec2(&ec2_cfg(512, 1024, StorageMode::Ephemeral), M7I_LARGE, None);
|
||||
let big = estimate_ec2(&ec2_cfg(1920, 7373, StorageMode::Ephemeral), M7I_LARGE, None);
|
||||
assert!(close(small.usd_per_hour, 0.1008) && close(big.usd_per_hour, 0.1008));
|
||||
assert!(small.ec2_instance && !small.note_spot && !small.efs_billed_separately);
|
||||
assert!(!small.root_volume_included);
|
||||
// Ephemeral size and Spot don't apply on EC2; EFS is still billed apart.
|
||||
let mut c = ec2_cfg(1024, 4096, StorageMode::Hybrid);
|
||||
c.ephemeral_gib = 200;
|
||||
c.spot = true;
|
||||
let e = estimate_ec2(&c, M7G_LARGE, None);
|
||||
assert!(close(e.usd_per_hour, 0.0816));
|
||||
assert!(e.efs_billed_separately && !e.note_spot && e.ec2_instance);
|
||||
// Fargate estimates say they are not an instance's.
|
||||
assert!(!estimate(&cfg(1024, 4096, 21), &X86).ec2_instance);
|
||||
}
|
||||
|
||||
fn ec2_api() -> FakeApi {
|
||||
FakeApi::answering(vec![Ok(Page {
|
||||
items: ec2_items(),
|
||||
next_token: None,
|
||||
})])
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_price_is_fetched_once_and_cached_per_region_and_type() {
|
||||
let (_d, c) = cache();
|
||||
let api = ec2_api();
|
||||
let price = c.ec2_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", "m7i.large", at(T0)).await;
|
||||
assert_eq!(price, Some(M7I_LARGE));
|
||||
assert_eq!(api.calls(), 1);
|
||||
assert_eq!(api.queries.lock().unwrap()[0], ec2_query("us-west-2", "m7i.large"));
|
||||
assert!(c.dir().join("us-west-2--ec2-m7i.large.json").is_file());
|
||||
assert_eq!(c.read_ec2("us-west-2", "m7i.large", at(T0 + DAY - 1)), Some((M7I_LARGE, true)));
|
||||
assert_eq!(c.read_ec2("us-west-2", "m7i.large", at(T0 + DAY)), Some((M7I_LARGE, false)));
|
||||
// Fresh: no call. Another type, or the region's Fargate rates, are not it.
|
||||
let again = c
|
||||
.ec2_price_with::<FakeApi, FakeApi>(async { panic!("no client for a fresh cache") }, "us-west-2", "m7i.large", at(T0 + 60))
|
||||
.await;
|
||||
assert_eq!(again, Some(M7I_LARGE));
|
||||
assert_eq!(c.read_ec2("us-west-2", "m7g.large", at(T0)), None);
|
||||
assert_eq!(c.read("us-west-2", at(T0)), None);
|
||||
// A file renamed to another type's name is no cache.
|
||||
std::fs::copy(
|
||||
c.dir().join("us-west-2--ec2-m7i.large.json"),
|
||||
c.dir().join("us-west-2--ec2-m7g.large.json"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(c.read_ec2("us-west-2", "m7g.large", at(T0)), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_price_backs_off_after_a_failure_and_falls_back_to_the_stale_cache() {
|
||||
let (_d, c) = cache();
|
||||
let denied = FakeApi::denied();
|
||||
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "m7i.large", at(T0)).await, None);
|
||||
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "m7i.large", at(T0 + 60)).await, None);
|
||||
assert_eq!(denied.calls(), 1, "a recent failure is not retried");
|
||||
// The failure is per type: another type is still asked.
|
||||
let other = FakeApi::denied();
|
||||
let _ = c.ec2_price_with::<FakeApi, _>(async { Some(&other) }, "us-west-2", "m7g.large", at(T0 + 60)).await;
|
||||
assert_eq!(other.calls(), 1);
|
||||
// A stale price answers when the refresh fails.
|
||||
c.write_ec2("us-west-2", "c7i.large", 0.0893, at(T0)).unwrap();
|
||||
let r = c.ec2_price_with::<FakeApi, _>(async { Some(&denied) }, "us-west-2", "c7i.large", at(T0 + 2 * DAY)).await;
|
||||
assert_eq!(r, Some(0.0893));
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn a_hanging_ec2_price_times_out_to_none() {
|
||||
let (_d, c) = cache();
|
||||
let api = FakeApi::hanging();
|
||||
let started = tokio::time::Instant::now();
|
||||
assert_eq!(c.ec2_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", "m7i.large", at(T0)).await, None);
|
||||
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_instance_type_that_could_name_another_file_is_never_used() {
|
||||
let (_d, c) = cache();
|
||||
for bad in ["../x", "m7i.large/..", "M7I.large", "m7i", "", "m7i.large.json", "m7i .large"] {
|
||||
let r = c
|
||||
.ec2_price_with::<FakeApi, FakeApi>(async { panic!("no client for {bad:?}") }, "us-west-2", bad, at(T0))
|
||||
.await;
|
||||
assert_eq!(r, None, "{bad}");
|
||||
assert_eq!(c.read_ec2("us-west-2", bad, at(T0)), None);
|
||||
assert!(c.write_ec2("us-west-2", bad, 1.0, at(T0)).is_err(), "{bad}");
|
||||
assert!(fetch_ec2_price(&ec2_api(), "us-west-2", bad).await.is_err());
|
||||
}
|
||||
assert_eq!(fetch_ec2_price(&ec2_api(), "us-west-2", "m7g.large").await, Ok(M7G_LARGE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fargate_cache_file_written_before_ec2_prices_still_reads() {
|
||||
let (_d, c) = cache();
|
||||
std::fs::create_dir_all(c.dir()).unwrap();
|
||||
let old = serde_json::json!({ "region": "us-west-2", "fetched_at": T0, "rates": RATES });
|
||||
std::fs::write(c.dir().join("us-west-2.json"), old.to_string()).unwrap();
|
||||
assert_eq!(c.read("us-west-2", at(T0)), Some((RATES, true)));
|
||||
}
|
||||
|
||||
const GP3_FIXTURE: &str = include_str!("fixtures/ebs-gp3-get-products-us-west-2.json");
|
||||
|
||||
fn gp3_items() -> Vec<String> {
|
||||
let v: Value = serde_json::from_str(GP3_FIXTURE).unwrap();
|
||||
v["PriceList"].as_array().unwrap().iter().map(|s| s.as_str().unwrap().to_string()).collect()
|
||||
}
|
||||
|
||||
/// The root volume (50 GiB gp3, `ec2.tf`) is in an EC2 estimate when its
|
||||
/// price is known: $0.08/GB-month in us-west-2 (recorded 2026-11-16) is
|
||||
/// 50 × 0.08 / 730 ≈ $0.0055/h on top of the instance.
|
||||
#[test]
|
||||
fn an_ec2_estimate_adds_the_gp3_root_volume_when_its_price_is_known() {
|
||||
assert_eq!(parse_gp3_price("us-west-2", &gp3_items()), Some(0.08));
|
||||
assert_eq!(parse_gp3_price("eu-west-1", &gp3_items()), None, "another region's row");
|
||||
assert_eq!(parse_gp3_price("us-west-2", &ec2_items()), None, "not a gp3 row");
|
||||
let e = estimate_ec2(&ec2_cfg(1024, 4096, StorageMode::Ephemeral), M7I_LARGE, Some(0.08));
|
||||
assert!(close(e.usd_per_hour, 0.1008 + 50.0 * 0.08 / 730.0), "{}", e.usd_per_hour);
|
||||
assert!(e.root_volume_included && e.ec2_instance);
|
||||
assert_eq!(gp3_query("us-west-2").service_code, "AmazonEC2");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_gp3_price_is_fetched_once_and_cached_per_region() {
|
||||
let (_d, c) = cache();
|
||||
let api = FakeApi::answering(vec![Ok(Page { items: gp3_items(), next_token: None })]);
|
||||
let price = c.gp3_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", at(T0)).await;
|
||||
assert_eq!(price, Some(0.08));
|
||||
assert_eq!(api.queries.lock().unwrap()[0], gp3_query("us-west-2"));
|
||||
assert!(c.dir().join("us-west-2--ebs-gp3.json").is_file());
|
||||
let again = c.gp3_price_with::<FakeApi, _>(async { Some(&api) }, "us-west-2", at(T0 + 1)).await;
|
||||
assert_eq!(again, Some(0.08));
|
||||
assert_eq!(api.calls(), 1);
|
||||
}
|
||||
@@ -14,12 +14,15 @@ use std::time::Duration;
|
||||
use aws_config::SdkConfig;
|
||||
use aws_sdk_secretsmanager::error::ProvideErrorMetadata;
|
||||
|
||||
use super::environments::{knowable, parse_environment_record, EnvironmentInfo, Knowable};
|
||||
use super::environments::{
|
||||
get_parameter, knowable, parse_environment_record, user_record_from, EnvironmentInfo, Knowable,
|
||||
Param, UserRecord,
|
||||
};
|
||||
use super::error::{classify_sdk_error, AwsError};
|
||||
use super::tasks::{find_project_task, TaskApi, STOP_REASON};
|
||||
use super::tofu_modules::{
|
||||
access_point_root, workspace_access_point_root, environment_cluster_name, environment_parameter_name, project_secret_name, task_family,
|
||||
user_file_system_token, user_role_names,
|
||||
is_user_ec2_cluster, user_file_system_token, user_record_parameter_name, user_role_names,
|
||||
};
|
||||
use super::workspace::WorkspaceApi;
|
||||
|
||||
@@ -40,6 +43,13 @@ pub trait RuntimeApi: Send + Sync {
|
||||
&self,
|
||||
env: &str,
|
||||
) -> impl Future<Output = Result<Option<String>, AwsError>> + Send;
|
||||
/// `ssm:GetParameter` of the caller's EC2 record in `env`
|
||||
/// (`user_record_parameter_name`).
|
||||
fn user_record(
|
||||
&self,
|
||||
env: &str,
|
||||
owner_key: &str,
|
||||
) -> impl Future<Output = Result<Param, AwsError>> + Send;
|
||||
/// `iam:GetRole`.
|
||||
fn role_exists(&self, name: &str) -> impl Future<Output = Result<Knowable, AwsError>> + Send;
|
||||
/// `secretsmanager:GetSecretValue`'s `SecretString`.
|
||||
@@ -80,6 +90,50 @@ pub async fn read_environment<R: RuntimeApi>(
|
||||
})
|
||||
}
|
||||
|
||||
/// The caller's EC2 record in `env`, validated (untrusted: `parse_user_record`).
|
||||
/// Absent = no EC2 capacity for NEW runs; a task already running on the
|
||||
/// user's cluster may outlive the record (teardown removes it first).
|
||||
pub async fn read_user_record<R: RuntimeApi>(
|
||||
rt: &R,
|
||||
env: &str,
|
||||
owner_key: &str,
|
||||
account: &str,
|
||||
region: &str,
|
||||
) -> Result<UserRecord, AwsError> {
|
||||
Ok(user_record_from(rt.user_record(env, owner_key).await?, env, account, region))
|
||||
}
|
||||
|
||||
/// The per-user EC2 cluster (ARN) a stored task ARN names, when it is one of
|
||||
/// `env`'s: where a project that ran on EC2 is still looked for after its
|
||||
/// user record is gone.
|
||||
pub fn ec2_cluster_of_task(env: &str, task_arn: &str) -> Option<String> {
|
||||
cluster_of_task(task_arn)
|
||||
.filter(|c| is_user_ec2_cluster(env, c))
|
||||
.and_then(|_| cluster_arn_of_task(task_arn))
|
||||
}
|
||||
|
||||
/// Where a project's task may be: `env_cluster` (the environment's), then
|
||||
/// the user's EC2 cluster from their record, then the per-user cluster
|
||||
/// `stored_task_arn` names (the record may be gone while that task runs),
|
||||
/// without repeats.
|
||||
pub fn project_task_clusters(
|
||||
env: &str,
|
||||
env_cluster: String,
|
||||
ec2_cluster: Option<String>,
|
||||
stored_task_arn: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let mut out = vec![env_cluster];
|
||||
for c in [ec2_cluster, stored_task_arn.and_then(|a| ec2_cluster_of_task(env, a))]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if !out.contains(&c) {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether the caller's task role exists in the environment: the onboarding
|
||||
/// signal a workspace user can read. A denied lookup is not an answer, so
|
||||
/// it is let through (RunTask will say what is wrong).
|
||||
@@ -237,6 +291,9 @@ pub struct RemoveRequest<'a> {
|
||||
/// The EFS-mode `/workspace` access point the store has (kept after a
|
||||
/// switch to Hybrid or Ephemeral, Q1).
|
||||
pub stored_workspace_access_point_id: Option<&'a str>,
|
||||
/// The user's EC2 cluster (their record's), looked in too: a project
|
||||
/// may have run there.
|
||||
pub ec2_cluster: Option<&'a str>,
|
||||
/// Delete the EFS access points, home and `/workspace` (ruling R9). The
|
||||
/// files stay on the user's own file system (only their task role can
|
||||
/// read it) until offboarding destroys it; a later start of the same
|
||||
@@ -258,17 +315,33 @@ pub async fn remove_workspace_resources<T: TaskApi, W: WorkspaceApi, R: RuntimeA
|
||||
let cluster = environment_cluster_name(req.env);
|
||||
let family = task_family(req.env, req.owner_key, req.project_id);
|
||||
|
||||
// 1. The task.
|
||||
let live =
|
||||
find_project_task(tasks, &cluster, &family, req.owner_user_id, req.project_id).await?;
|
||||
let mut to_stop: Vec<String> = live.into_iter().map(|t| t.arn).collect();
|
||||
if let Some(arn) = req.stored_task_arn {
|
||||
if cluster_of_task(arn) == Some(cluster.as_str()) && !to_stop.iter().any(|a| a == arn) {
|
||||
to_stop.push(arn.to_string());
|
||||
// 1. The task: in the environment's cluster, the user's EC2 cluster (the
|
||||
// record's, and the stored task's when that is a per-user cluster of
|
||||
// this environment — the record may be gone).
|
||||
let clusters = project_task_clusters(
|
||||
req.env,
|
||||
cluster.clone(),
|
||||
req.ec2_cluster.map(str::to_string),
|
||||
req.stored_task_arn,
|
||||
);
|
||||
let mut to_stop: Vec<(String, String)> = Vec::new();
|
||||
for c in &clusters {
|
||||
if let Some(t) =
|
||||
find_project_task(tasks, c, &family, req.owner_user_id, req.project_id).await?
|
||||
{
|
||||
to_stop.push((c.clone(), t.arn));
|
||||
}
|
||||
}
|
||||
for arn in &to_stop {
|
||||
tasks.stop_task(&cluster, arn, STOP_REASON).await?;
|
||||
if let Some(arn) = req.stored_task_arn {
|
||||
let ours = cluster_of_task(arn) == Some(cluster.as_str())
|
||||
|| ec2_cluster_of_task(req.env, arn).is_some();
|
||||
if ours && !to_stop.iter().any(|(_, a)| a == arn) {
|
||||
let c = cluster_of_task(arn).map_or_else(|| cluster.clone(), str::to_string);
|
||||
to_stop.push((c, arn.to_string()));
|
||||
}
|
||||
}
|
||||
for (c, arn) in &to_stop {
|
||||
tasks.stop_task(c, arn, STOP_REASON).await?;
|
||||
}
|
||||
|
||||
let mut failures: Vec<String> = Vec::new();
|
||||
@@ -385,6 +458,10 @@ impl RuntimeApi for SdkRuntimeApi {
|
||||
}
|
||||
}
|
||||
|
||||
async fn user_record(&self, env: &str, owner_key: &str) -> Result<Param, AwsError> {
|
||||
get_parameter(&self.ssm, &user_record_parameter_name(env, owner_key)).await
|
||||
}
|
||||
|
||||
async fn role_exists(&self, name: &str) -> Result<Knowable, AwsError> {
|
||||
let res = self.iam.get_role().role_name(name).send().await;
|
||||
knowable("iam:GetRole", res)
|
||||
|
||||
@@ -36,6 +36,8 @@ struct Fake {
|
||||
revisions: Vec<TaskDefinitionRef>,
|
||||
access_points: Vec<AccessPointRef>,
|
||||
fail_secret_delete: bool,
|
||||
/// The clusters ListTasks was asked about, by name.
|
||||
lists: Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
impl Fake {
|
||||
@@ -52,6 +54,10 @@ impl RuntimeApi for Fake {
|
||||
self.log(format!("record {env}"));
|
||||
Ok(self.record.clone())
|
||||
}
|
||||
async fn user_record(&self, env: &str, owner_key: &str) -> Result<crate::aws::environments::Param, AwsError> {
|
||||
self.log(format!("user-record {env} {owner_key}"));
|
||||
Ok(crate::aws::environments::Param::Missing)
|
||||
}
|
||||
async fn role_exists(&self, name: &str) -> Result<Knowable, AwsError> {
|
||||
self.log(format!("role {name}"));
|
||||
Ok(self.role.clone().unwrap_or(Knowable::Yes))
|
||||
@@ -85,8 +91,16 @@ impl TaskApi for Fake {
|
||||
async fn describe(&self, _c: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
|
||||
Ok(self.live_tasks.iter().filter(|t| arns.contains(&t.arn)).cloned().collect())
|
||||
}
|
||||
async fn list_project_tasks(&self, _c: &str, _f: &str) -> Result<Vec<String>, AwsError> {
|
||||
Ok(self.live_tasks.iter().map(|t| t.arn.clone()).collect())
|
||||
async fn list_project_tasks(&self, c: &str, _f: &str) -> Result<Vec<String>, AwsError> {
|
||||
// By cluster name or ARN, like ECS.
|
||||
let name = c.rsplit_once(":cluster/").map_or(c, |(_, n)| n);
|
||||
self.lists.lock().unwrap().push(name.to_string());
|
||||
Ok(self
|
||||
.live_tasks
|
||||
.iter()
|
||||
.filter(|t| cluster_of_task(&t.arn) == Some(name))
|
||||
.map(|t| t.arn.clone())
|
||||
.collect())
|
||||
}
|
||||
async fn stop_task(&self, cluster: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
|
||||
self.log(format!("stop {cluster} {arn} {reason}"));
|
||||
@@ -262,6 +276,9 @@ fn live(id: &str) -> TaskInfo {
|
||||
family()
|
||||
),
|
||||
runtime_id: None,
|
||||
container_instance_arn: None,
|
||||
exec_enabled: true,
|
||||
created_unix: None,
|
||||
exec_agent_running: false,
|
||||
exit_code: None,
|
||||
owner: Some(USER.into()),
|
||||
@@ -305,6 +322,7 @@ async fn removal_stops_the_task_leaves_the_revisions_and_schedules_the_secret()
|
||||
stored_task_arn: Some(&t0),
|
||||
stored_access_point_id: Some("fsap-stored"),
|
||||
stored_workspace_access_point_id: None,
|
||||
ec2_cluster: None,
|
||||
discard_data: false,
|
||||
};
|
||||
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
|
||||
@@ -341,6 +359,7 @@ async fn a_failed_step_is_reported_but_the_rest_still_runs() {
|
||||
stored_task_arn: None,
|
||||
stored_access_point_id: Some("fsap-1"),
|
||||
stored_workspace_access_point_id: None,
|
||||
ec2_cluster: None,
|
||||
discard_data: true,
|
||||
};
|
||||
let e = remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap_err();
|
||||
@@ -381,6 +400,7 @@ async fn removal_with_discard_deletes_both_access_points_and_without_keeps_both(
|
||||
stored_access_point_id: Some("fsap-home"),
|
||||
// Kept in the store after switching EFS -> Hybrid.
|
||||
stored_workspace_access_point_id: Some("fsap-ws-stored"),
|
||||
ec2_cluster: None,
|
||||
discard_data: false,
|
||||
};
|
||||
let keep = Fake { access_points: points.clone(), ..Default::default() };
|
||||
@@ -402,3 +422,121 @@ async fn removal_with_discard_deletes_both_access_points_and_without_keeps_both(
|
||||
deleted.sort();
|
||||
assert_eq!(deleted, vec!["fsap-home", "fsap-ws", "fsap-ws-stored"]);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- EC2 (phase 7)
|
||||
|
||||
fn ec2_task_arn(id: &str) -> String {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev--u-alice/{id}")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_task_on_a_per_user_cluster_names_that_cluster() {
|
||||
assert_eq!(
|
||||
ec2_cluster_of_task("dev", &ec2_task_arn("t")).as_deref(),
|
||||
Some(format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice").as_str())
|
||||
);
|
||||
// The environment's own cluster, another environment's per-user
|
||||
// cluster and anything else are not.
|
||||
assert_eq!(ec2_cluster_of_task("dev", &task_arn("t")), None);
|
||||
assert_eq!(ec2_cluster_of_task("prod", &ec2_task_arn("t")), None);
|
||||
let odd = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/triple-c-dev--u-a--b/t");
|
||||
assert_eq!(ec2_cluster_of_task("dev", &odd), None);
|
||||
assert_eq!(ec2_cluster_of_task("dev", "not-an-arn"), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_user_record_is_read_and_validated() {
|
||||
struct Rec(Param);
|
||||
impl Rec {
|
||||
async fn read(&self) -> UserRecord {
|
||||
let fake = RecFake(self.0.clone(), Mutex::new(Vec::new()));
|
||||
let r = read_user_record(&fake, "dev", &ok(), ACCOUNT, REGION).await.unwrap();
|
||||
assert_eq!(*fake.1.lock().unwrap(), vec![format!("dev {}", ok())]);
|
||||
r
|
||||
}
|
||||
}
|
||||
let valid = serde_json::json!({
|
||||
"schema": 1, "instance_type": "m7i.large", "max_instances": 2, "user_tag": "alice",
|
||||
"cluster_arn": format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice"),
|
||||
"capacity_provider": "triple-c-dev--u-alice",
|
||||
})
|
||||
.to_string();
|
||||
let UserRecord::Valid(c) = Rec(Param::Value(valid)).read().await else { panic!() };
|
||||
assert_eq!((c.instance_type.as_str(), c.max_instances), ("m7i.large", 2));
|
||||
assert_eq!(Rec(Param::Missing).read().await, UserRecord::Absent);
|
||||
assert!(matches!(Rec(Param::Denied).read().await, UserRecord::Unusable(_)));
|
||||
assert!(matches!(Rec(Param::Value("{}".into())).read().await, UserRecord::Unusable(_)));
|
||||
}
|
||||
|
||||
/// Only `user_record` is asked of it.
|
||||
struct RecFake(Param, Mutex<Vec<String>>);
|
||||
|
||||
impl RuntimeApi for RecFake {
|
||||
async fn environment_record(&self, _env: &str) -> Result<Option<String>, AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
async fn user_record(&self, env: &str, owner_key: &str) -> Result<Param, AwsError> {
|
||||
self.1.lock().unwrap().push(format!("{env} {owner_key}"));
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
async fn role_exists(&self, _name: &str) -> Result<Knowable, AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
async fn secret_string(&self, _name: &str) -> Result<String, AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
async fn access_point_state(&self, _id: &str) -> Result<Option<String>, AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
async fn delete_secret(&self, _name: &str) -> Result<(), AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
async fn delete_access_point(&self, _id: &str) -> Result<(), AwsError> {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn removal_finds_and_stops_a_task_on_the_users_ec2_cluster() {
|
||||
let key = ok();
|
||||
let user_cluster = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice");
|
||||
let base = RemoveRequest {
|
||||
env: "dev",
|
||||
region: REGION,
|
||||
owner_user_id: USER,
|
||||
owner_key: &key,
|
||||
project_id: PROJECT,
|
||||
stored_task_arn: None,
|
||||
stored_access_point_id: None,
|
||||
stored_workspace_access_point_id: None,
|
||||
ec2_cluster: Some(&user_cluster),
|
||||
discard_data: false,
|
||||
};
|
||||
// Found through the record's cluster.
|
||||
let mut t = live("e1");
|
||||
t.arn = ec2_task_arn("e1");
|
||||
let fake = Fake { live_tasks: vec![t.clone()], ..Default::default() };
|
||||
remove_workspace_resources(&fake, &fake, &fake, &base).await.unwrap();
|
||||
assert_eq!(*fake.lists.lock().unwrap(), ["triple-c-dev", "triple-c-dev--u-alice"]);
|
||||
let calls = fake.calls();
|
||||
assert!(
|
||||
calls.contains(&format!("stop {user_cluster} {} {STOP_REASON}", ec2_task_arn("e1"))),
|
||||
"{calls:?}"
|
||||
);
|
||||
|
||||
// The record is gone: the stored ARN still leads to its cluster.
|
||||
let stored = ec2_task_arn("e1");
|
||||
let fake = Fake { live_tasks: vec![t], ..Default::default() };
|
||||
let r = RemoveRequest { ec2_cluster: None, stored_task_arn: Some(&stored), ..base };
|
||||
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
|
||||
let stops: Vec<String> = fake.calls().into_iter().filter(|c| c.starts_with("stop")).collect();
|
||||
assert_eq!(stops.len(), 1, "{stops:?}");
|
||||
assert!(stops[0].contains(&ec2_task_arn("e1")), "{stops:?}");
|
||||
|
||||
// A stored ARN on someone else's kind of cluster is not stopped.
|
||||
let foreign = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/other-cluster/x");
|
||||
let fake = Fake::default();
|
||||
let r = RemoveRequest { ec2_cluster: None, stored_task_arn: Some(&foreign), ..base };
|
||||
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
|
||||
assert!(!fake.calls().iter().any(|c| c.starts_with("stop")));
|
||||
}
|
||||
@@ -25,7 +25,7 @@ use aws_sdk_ecs::types::{
|
||||
ManagedAgentName, NetworkConfiguration, Tag, Task, TaskField,
|
||||
};
|
||||
|
||||
use super::environments::EnvironmentInfo;
|
||||
use super::environments::{EnvironmentInfo, UserCompute};
|
||||
use super::error::{classify_sdk_error, AwsError};
|
||||
pub use super::tofu_modules::task_log_stream;
|
||||
use super::workspace::{workspace_tags, CONTAINER_NAME, TAG_OWNER, TAG_PROJECT};
|
||||
@@ -62,8 +62,17 @@ pub struct TaskInfo {
|
||||
pub task_definition_arn: String,
|
||||
/// The `workspace` container's runtime id (needed for an SSM target).
|
||||
pub runtime_id: Option<String>,
|
||||
/// The EC2 container instance the task was placed on; `None` on Fargate,
|
||||
/// and on EC2 until an instance has room for it.
|
||||
pub container_instance_arn: Option<String>,
|
||||
/// The `workspace` container's ExecuteCommandAgent is RUNNING.
|
||||
pub exec_agent_running: bool,
|
||||
/// ECS Exec is on for the task (`enableExecuteCommand`). ECS turns it
|
||||
/// off on a RunTask that has to wait for an EC2 instance (verified live,
|
||||
/// phase 7), and such a task can never be reached.
|
||||
pub exec_enabled: bool,
|
||||
/// When ECS created the task (Unix seconds), if it says.
|
||||
pub created_unix: Option<i64>,
|
||||
/// The `workspace` container's exit code, once it has one.
|
||||
pub exit_code: Option<i32>,
|
||||
/// Its `triple-c:owner` / `triple-c:project` tags (DescribeTasks with
|
||||
@@ -112,6 +121,9 @@ pub(crate) fn task_info(t: &Task) -> TaskInfo {
|
||||
stopped_reason: t.stopped_reason().map(str::to_string),
|
||||
task_definition_arn: t.task_definition_arn().unwrap_or_default().to_string(),
|
||||
runtime_id: workspace.and_then(|c| c.runtime_id()).map(str::to_string),
|
||||
container_instance_arn: t.container_instance_arn().map(str::to_string),
|
||||
exec_enabled: t.enable_execute_command(),
|
||||
created_unix: t.created_at().map(|d| d.secs()),
|
||||
exec_agent_running: workspace.is_some_and(|c| {
|
||||
c.managed_agents().iter().any(|a| {
|
||||
a.name() == Some(&ManagedAgentName::ExecuteCommandAgent)
|
||||
@@ -216,6 +228,65 @@ pub fn run_task_request(
|
||||
req.build().expect(BUILT)
|
||||
}
|
||||
|
||||
/// Where a new task runs.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum RunOn<'a> {
|
||||
/// The environment's cluster, Fargate (or `FARGATE_SPOT`).
|
||||
Fargate { spot: bool },
|
||||
/// The user's own cluster and capacity provider (phase 7), from their
|
||||
/// record.
|
||||
Ec2(&'a UserCompute),
|
||||
}
|
||||
|
||||
impl RunOn<'_> {
|
||||
/// The cluster a task runs on, and is looked for in.
|
||||
pub fn cluster<'e>(&'e self, env: &'e EnvironmentInfo) -> &'e str {
|
||||
match self {
|
||||
RunOn::Fargate { .. } => &env.cluster_arn,
|
||||
RunOn::Ec2(c) => &c.cluster_arn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The RunTask request on the user's EC2 capacity: the exact revision, on
|
||||
/// their cluster, placed by their capacity provider (weight 1). No launch
|
||||
/// type (exclusive with a strategy) and no network configuration: EC2 tasks
|
||||
/// use `bridge` networking (ruling R2). ECS Exec, tags, the client token and
|
||||
/// no overrides as on Fargate (P1).
|
||||
pub fn ec2_run_task_request(
|
||||
compute: &UserCompute,
|
||||
task_def_arn: &str,
|
||||
tags: &[(String, String)],
|
||||
) -> RunTaskInput {
|
||||
const BUILT: &str = "every required field is set";
|
||||
RunTaskInput::builder()
|
||||
.cluster(&compute.cluster_arn)
|
||||
.task_definition(task_def_arn)
|
||||
.count(1)
|
||||
.enable_execute_command(true)
|
||||
.capacity_provider_strategy(
|
||||
CapacityProviderStrategyItem::builder()
|
||||
.capacity_provider(&compute.capacity_provider)
|
||||
.weight(1)
|
||||
.build()
|
||||
.expect(BUILT),
|
||||
)
|
||||
.set_tags(Some(
|
||||
tags.iter()
|
||||
.map(|(k, v)| Tag::builder().key(k).value(v).build())
|
||||
.collect(),
|
||||
))
|
||||
.client_token(uuid::Uuid::new_v4().to_string())
|
||||
.build()
|
||||
.expect(BUILT)
|
||||
}
|
||||
|
||||
/// RunTask refused on a per-user cluster: the workspace policy lets a caller
|
||||
/// run tasks only on the cluster named after their own `triple-c-user`
|
||||
/// principal tag.
|
||||
pub const EC2_TAG_MISMATCH: &str = "This EC2 capacity belongs to a different triple-c-user value \
|
||||
than your sign-in's (or your sign-in has no triple-c-user tag). Ask an admin.";
|
||||
|
||||
/// The family of `arn:...:task-definition/<family>:<revision>`, when the
|
||||
/// revision is a number. `None` for a bare family or anything else.
|
||||
pub fn revision_family(arn: &str) -> Option<&str> {
|
||||
@@ -271,14 +342,15 @@ pub struct TaskStart {
|
||||
}
|
||||
|
||||
/// The project's live task if there is one (Review Focus 2: never a second
|
||||
/// task for a project), else RunTask with the exact `task_def_arn`.
|
||||
/// task for a project), else RunTask with the exact `task_def_arn` where
|
||||
/// `run_on` says.
|
||||
pub async fn find_or_run<A: TaskApi>(
|
||||
api: &A,
|
||||
env: &EnvironmentInfo,
|
||||
owner_user_id: &str,
|
||||
project_id: &str,
|
||||
task_def_arn: &str,
|
||||
spot: bool,
|
||||
run_on: RunOn<'_>,
|
||||
) -> Result<TaskStart, AwsError> {
|
||||
let family = revision_family(task_def_arn).ok_or_else(|| {
|
||||
other(format!(
|
||||
@@ -291,20 +363,43 @@ pub async fn find_or_run<A: TaskApi>(
|
||||
)));
|
||||
}
|
||||
if let Some(task) =
|
||||
find_project_task(api, &env.cluster_arn, family, owner_user_id, project_id).await?
|
||||
find_project_task(api, run_on.cluster(env), family, owner_user_id, project_id).await?
|
||||
{
|
||||
return Ok(TaskStart { task, reused: true });
|
||||
}
|
||||
let tags = workspace_tags(owner_user_id, project_id, &env.name);
|
||||
let task = api
|
||||
.run_task(run_task_request(env, task_def_arn, spot, &tags))
|
||||
.await?;
|
||||
let task = match run_on {
|
||||
RunOn::Fargate { spot } => {
|
||||
api.run_task(run_task_request(env, task_def_arn, spot, &tags)).await?
|
||||
}
|
||||
RunOn::Ec2(compute) => api
|
||||
.run_task(ec2_run_task_request(compute, task_def_arn, &tags))
|
||||
.await
|
||||
.map_err(ec2_run_error)?,
|
||||
};
|
||||
Ok(TaskStart {
|
||||
task,
|
||||
reused: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// A RunTask error on the user's EC2 capacity, in its terms: access denied is
|
||||
/// the `triple-c-user` mismatch ([`EC2_TAG_MISMATCH`]); a placement failure
|
||||
/// (`RESOURCE:CPU`/`RESOURCE:MEMORY`) means no instance can take the task.
|
||||
pub fn ec2_run_error(e: AwsError) -> AwsError {
|
||||
match e {
|
||||
AwsError::AccessDenied { action, message } => AwsError::AccessDenied {
|
||||
action,
|
||||
message: format!("{EC2_TAG_MISMATCH} (AWS said: {message})"),
|
||||
},
|
||||
AwsError::Other { message } if message.contains("RESOURCE:") => other(format!(
|
||||
"{message}. No instance of your EC2 capacity has room for this task: lower its CPU \
|
||||
or memory reservation, or stop another EC2 workspace."
|
||||
)),
|
||||
e => e,
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ quotas
|
||||
|
||||
/// Whether an AWS message says an account limit or quota was hit.
|
||||
@@ -386,6 +481,30 @@ impl Default for WaitTiming {
|
||||
}
|
||||
}
|
||||
|
||||
impl WaitTiming {
|
||||
/// An EC2 task's wait: a cold start (capacity scaled to zero) launches
|
||||
/// an instance and pulls the image before the task can run.
|
||||
pub fn ec2() -> Self {
|
||||
WaitTiming {
|
||||
timeout: Duration::from_secs(10 * 60),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An EC2 task still waiting for an instance with room: ECS keeps it
|
||||
/// PROVISIONING (or PENDING) with no container instance while the capacity
|
||||
/// provider's managed scaling launches one. (A Fargate task never has a
|
||||
/// container instance, so ask this only of EC2 tasks.)
|
||||
pub fn waiting_for_instance(t: &TaskInfo) -> bool {
|
||||
t.container_instance_arn.is_none()
|
||||
&& t.desired_status == "RUNNING"
|
||||
&& matches!(t.last_status.as_str(), "PROVISIONING" | "PENDING")
|
||||
}
|
||||
|
||||
/// How a [`wait_ready`] timeout begins.
|
||||
pub const WAIT_TIMED_OUT: &str = "The task did not become ready within";
|
||||
|
||||
/// Where a project's task logs go: the environment's log group and the
|
||||
/// project's `awslogs-stream-prefix` (Task 4's `log_stream_prefix`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -440,7 +559,7 @@ pub async fn wait_ready<A: TaskApi>(
|
||||
}
|
||||
if tokio::time::Instant::now() + timing.interval > deadline {
|
||||
return Err(other(format!(
|
||||
"The task did not become ready within {} (last status {}{}). It may still \
|
||||
"{WAIT_TIMED_OUT} {} (last status {}{}). It may still \
|
||||
be starting; check the ECS console, or stop the project and start it again.",
|
||||
describe_timeout(timing.timeout),
|
||||
t.last_status,
|
||||
|
||||
@@ -64,6 +64,9 @@ fn info(id: &str, last: &str) -> TaskInfo {
|
||||
stopped_reason: None,
|
||||
task_definition_arn: td_arn(3),
|
||||
runtime_id: None,
|
||||
container_instance_arn: None,
|
||||
exec_enabled: true,
|
||||
created_unix: None,
|
||||
exec_agent_running: false,
|
||||
exit_code: None,
|
||||
owner: Some(USER.into()),
|
||||
@@ -230,7 +233,7 @@ async fn an_existing_running_task_is_reused_and_nothing_is_run() {
|
||||
..Default::default()
|
||||
}
|
||||
.describing(vec![vec![ready("t1")]]);
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(4), false)
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(4), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(got.reused);
|
||||
@@ -246,7 +249,7 @@ async fn a_pending_task_is_reused_too() {
|
||||
..Default::default()
|
||||
}
|
||||
.describing(vec![vec![info("t1", "PENDING")]]);
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), false)
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(got.reused);
|
||||
@@ -272,7 +275,7 @@ async fn a_task_of_another_owner_or_project_in_the_family_is_never_reused() {
|
||||
..Default::default()
|
||||
}
|
||||
.describing(vec![vec![foreign, other_project, untagged]]);
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), false)
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!got.reused);
|
||||
@@ -289,7 +292,7 @@ async fn a_task_already_stopping_is_not_reused() {
|
||||
..Default::default()
|
||||
}
|
||||
.describing(vec![vec![stopping, info("t2", "DEPROVISIONING")]]);
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), false)
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!got.reused);
|
||||
@@ -312,7 +315,7 @@ async fn a_running_task_is_preferred_over_a_pending_one() {
|
||||
#[tokio::test]
|
||||
async fn nothing_listed_means_no_describe_and_a_run_with_the_exact_revision_and_tags() {
|
||||
let api = Fake::default();
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(7), true)
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(7), RunOn::Fargate { spot: true })
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!got.reused);
|
||||
@@ -345,7 +348,7 @@ async fn a_bare_family_or_foreign_arn_is_refused_before_any_call() {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{FAMILY}:latest"),
|
||||
"arn:aws:ecs:eu-west-1:1:task-definition/someone-else:1".to_string(),
|
||||
] {
|
||||
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &bad, false)
|
||||
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &bad, RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, AwsError::Other { .. }), "{bad}: {err:?}");
|
||||
@@ -358,7 +361,7 @@ async fn a_quota_refusal_is_reported_once_without_retrying() {
|
||||
let api = Fake::default().running(Err(quota_error(
|
||||
"You've reached the limit on the number of vCPUs you can run concurrently",
|
||||
)));
|
||||
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), false)
|
||||
let err = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(is_quota_error(&err));
|
||||
@@ -748,3 +751,133 @@ fn task_info_reads_the_workspace_container_and_its_exec_agent() {
|
||||
assert_eq!(bare.exit_code, None);
|
||||
assert_eq!(bare.owner, None);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- EC2 (phase 7)
|
||||
|
||||
fn user_cluster() -> String {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice")
|
||||
}
|
||||
|
||||
fn compute() -> UserCompute {
|
||||
UserCompute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 2,
|
||||
user_tag: "alice".into(),
|
||||
cluster_arn: user_cluster(),
|
||||
capacity_provider: "triple-c-dev--u-alice".into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_run_uses_the_users_cluster_and_capacity_provider_and_no_network() {
|
||||
let c = compute();
|
||||
let req = ec2_run_task_request(&c, &td_arn(3), &tags());
|
||||
assert_eq!(req.cluster(), Some(user_cluster().as_str()));
|
||||
assert_eq!(req.task_definition(), Some(td_arn(3).as_str()));
|
||||
assert_eq!(req.launch_type(), None, "exclusive with a capacity provider strategy");
|
||||
let strategy = req.capacity_provider_strategy();
|
||||
assert_eq!(strategy.len(), 1);
|
||||
assert_eq!(strategy[0].capacity_provider(), "triple-c-dev--u-alice");
|
||||
assert_eq!(strategy[0].weight(), 1);
|
||||
assert!(req.network_configuration().is_none(), "bridge networking (R2)");
|
||||
assert_eq!(req.enable_execute_command(), Some(true));
|
||||
assert_eq!(req.count(), Some(1));
|
||||
assert!(req.client_token().is_some_and(|t| !t.is_empty()));
|
||||
let got: Vec<(String, String)> = req
|
||||
.tags()
|
||||
.iter()
|
||||
.map(|t| (t.key().unwrap().to_string(), t.value().unwrap().to_string()))
|
||||
.collect();
|
||||
assert_eq!(got, tags());
|
||||
assert!(req.overrides().is_none());
|
||||
assert!(req.propagate_tags().is_none());
|
||||
assert!(req.enable_ecs_managed_tags().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_find_or_run_looks_and_runs_on_the_users_cluster() {
|
||||
let api = Fake::default();
|
||||
let c = compute();
|
||||
let got = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Ec2(&c))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!got.reused);
|
||||
assert_eq!(api.calls(), vec![format!("list {} {FAMILY}", user_cluster()), "run".to_string()]);
|
||||
let req = &api.run_requests.lock().unwrap()[0];
|
||||
assert_eq!(req.cluster(), Some(user_cluster().as_str()));
|
||||
assert_eq!(req.capacity_provider_strategy()[0].capacity_provider(), "triple-c-dev--u-alice");
|
||||
assert_eq!(RunOn::Ec2(&c).cluster(&env_info()), user_cluster());
|
||||
assert_eq!(RunOn::Fargate { spot: true }.cluster(&env_info()), cluster());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn access_denied_on_the_users_cluster_says_the_triple_c_user_value_is_wrong() {
|
||||
let api = Fake::default().running(Err(AwsError::AccessDenied {
|
||||
action: "ecs:RunTask".into(),
|
||||
message: "not authorized to perform ecs:RunTask on cluster/triple-c-dev--u-alice".into(),
|
||||
}));
|
||||
let c = compute();
|
||||
let e = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Ec2(&c))
|
||||
.await
|
||||
.unwrap_err();
|
||||
let AwsError::AccessDenied { action, message } = &e else { panic!("{e:?}") };
|
||||
assert_eq!(action, "ecs:RunTask");
|
||||
assert!(message.starts_with(EC2_TAG_MISMATCH), "{message}");
|
||||
assert!(message.contains("not authorized"), "AWS's words are kept: {message}");
|
||||
assert!(EC2_TAG_MISMATCH.contains("different triple-c-user value"));
|
||||
assert!(EC2_TAG_MISMATCH.contains("Ask an admin"));
|
||||
|
||||
// The same refusal on Fargate is left as it is.
|
||||
let api = Fake::default().running(Err(AwsError::AccessDenied {
|
||||
action: "ecs:RunTask".into(),
|
||||
message: "nope".into(),
|
||||
}));
|
||||
let e = find_or_run(&api, &env_info(), USER, PROJECT_ID, &td_arn(3), RunOn::Fargate { spot: false })
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(e, AwsError::AccessDenied { action: "ecs:RunTask".into(), message: "nope".into() });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_placement_failure_on_ec2_says_no_instance_has_room() {
|
||||
let e = ec2_run_error(
|
||||
run_task_outcome(Vec::new(), vec![(Some("RESOURCE:MEMORY".into()), None)]).unwrap_err(),
|
||||
);
|
||||
assert!(e.to_string().contains("RESOURCE:MEMORY"), "{e}");
|
||||
assert!(e.to_string().contains("No instance of your EC2 capacity has room"), "{e}");
|
||||
// Anything else passes through.
|
||||
let q = quota_error("limit");
|
||||
assert_eq!(ec2_run_error(q.clone()), q);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_task_without_an_instance_is_waiting_for_one() {
|
||||
let provisioning = info("t", "PROVISIONING");
|
||||
assert!(waiting_for_instance(&provisioning));
|
||||
assert!(waiting_for_instance(&info("t", "PENDING")));
|
||||
let placed = TaskInfo {
|
||||
container_instance_arn: Some(format!(
|
||||
"arn:aws:ecs:{REGION}:{ACCOUNT}:container-instance/triple-c-dev--u-alice/abc"
|
||||
)),
|
||||
..info("t", "PENDING")
|
||||
};
|
||||
assert!(!waiting_for_instance(&placed));
|
||||
assert!(!waiting_for_instance(&info("t", "RUNNING")));
|
||||
assert!(!waiting_for_instance(&info("t", "STOPPED")));
|
||||
// The EC2 wait leaves room for an instance launch and an image pull.
|
||||
assert_eq!(WaitTiming::ec2().timeout, Duration::from_secs(600));
|
||||
assert_eq!(WaitTiming::ec2().interval, WaitTiming::default().interval);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn task_info_reads_the_container_instance() {
|
||||
let t = Task::builder()
|
||||
.task_arn(task_arn("t"))
|
||||
.container_instance_arn("arn:aws:ecs:eu-west-1:111122223333:container-instance/c/1")
|
||||
.build();
|
||||
assert_eq!(
|
||||
task_info(&t).container_instance_arn.as_deref(),
|
||||
Some("arn:aws:ecs:eu-west-1:111122223333:container-instance/c/1")
|
||||
);
|
||||
assert_eq!(task_info(&Task::builder().build()).container_instance_arn, None);
|
||||
}
|
||||
@@ -16,9 +16,16 @@ use crate::models::host_connection::validate_supported_region;
|
||||
/// `locals.template_version` (and the `triple-c:template-version` default
|
||||
/// tag) of `aws/terraform/environment`.
|
||||
pub const ENVIRONMENT_MODULE_VERSION: u32 = 2;
|
||||
/// The same for the user-onboarding module.
|
||||
/// The same for the user-onboarding module. 2: optional per-user EC2
|
||||
/// capacity (phase 7); an onboarding without it plans as version 1 did,
|
||||
/// apart from this tag.
|
||||
#[allow(dead_code)] // the onboarding module's version; checked against its HCL by a test
|
||||
pub const ONBOARD_MODULE_VERSION: u32 = 1;
|
||||
pub const ONBOARD_MODULE_VERSION: u32 = 2;
|
||||
/// The same for the permissions-boundary module. 2: the instance role's ECS
|
||||
/// agent actions, on its own cluster (phase 7, E4); an account admin
|
||||
/// re-applies the boundary once.
|
||||
#[allow(dead_code)] // checked against its HCL by a test; nothing reads the tag yet
|
||||
pub const BOUNDARY_MODULE_VERSION: u32 = 2;
|
||||
/// The permissions boundary every module-created role carries (no path).
|
||||
/// Equal to [`super::policies::PERMISSIONS_BOUNDARY_NAME`]; a test checks it.
|
||||
pub const BOUNDARY_POLICY_NAME: &str = "triple-c-permissions-boundary";
|
||||
@@ -52,6 +59,7 @@ const USER_ONBOARD_FILES: &[(&str, &str)] = module!("user-onboard":
|
||||
"main.tf",
|
||||
"iam.tf",
|
||||
"efs.tf",
|
||||
"ec2.tf",
|
||||
"outputs.tf",
|
||||
".terraform.lock.hcl",
|
||||
);
|
||||
@@ -132,6 +140,100 @@ pub fn user_role_names(env: &str, owner_key: &str) -> (String, String) {
|
||||
)
|
||||
}
|
||||
|
||||
/// The principal tag every EC2 user's sign-in carries (phase 7, amended E1):
|
||||
/// an admin sets it (an IAM Identity Center attribute for access control,
|
||||
/// or a tag on the IAM user or role), and the workspace policy lets a
|
||||
/// caller run tasks only on the per-user cluster named after its value.
|
||||
#[allow(dead_code)] // the onboarding module (Task 3) and the UI read it
|
||||
pub const EC2_USER_TAG_KEY: &str = "triple-c-user";
|
||||
|
||||
/// Checks a `triple-c-user` tag value: 1 to 32 characters of
|
||||
/// `[A-Za-z0-9-]`, not starting or ending with a hyphen and without `--`
|
||||
/// (the separator in [`user_ec2_stem`], which environment names can't hold
|
||||
/// either, so a per-user name can be read back only one way).
|
||||
pub fn validate_ec2_user_tag(tag: &str) -> Result<String, String> {
|
||||
let ok = (1..=32).contains(&tag.len())
|
||||
&& tag.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-')
|
||||
&& !tag.starts_with('-')
|
||||
&& !tag.ends_with('-')
|
||||
&& !tag.contains("--");
|
||||
if ok {
|
||||
Ok(tag.to_string())
|
||||
} else {
|
||||
Err(format!(
|
||||
"\"{}\" is not a valid triple-c-user tag: use 1 to 32 letters, digits and single \
|
||||
hyphens, not starting or ending with a hyphen",
|
||||
tag.escape_debug()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of a user's EC2 cluster, capacity provider, Auto Scaling group
|
||||
/// and launch template: `triple-c-<env>--u-<user tag>` (at most 65
|
||||
/// characters). Environment names and user tags never contain `--`, so the
|
||||
/// workspace policy's `cluster/triple-c-*--u-*` matches exactly the
|
||||
/// per-user clusters, and `triple-c-*--u-<tag>` exactly that user's.
|
||||
pub fn user_ec2_stem(env: &str, user_tag: &str) -> String {
|
||||
format!("triple-c-{env}--u-{user_tag}")
|
||||
}
|
||||
|
||||
/// Whether `cluster` (a cluster *name*) is one of `env`'s per-user EC2
|
||||
/// clusters ([`user_ec2_stem`] of a valid tag).
|
||||
pub fn is_user_ec2_cluster(env: &str, cluster: &str) -> bool {
|
||||
cluster
|
||||
.strip_prefix(&format!("{}--u-", environment_cluster_name(env)))
|
||||
.is_some_and(|tag| validate_ec2_user_tag(tag).is_ok())
|
||||
}
|
||||
|
||||
/// A user's EC2 instance role (and instance profile): named by owner key,
|
||||
/// not the user tag, because role names are account-global and at most 64
|
||||
/// characters (this is at most 51). The admin's PassRole to EC2 names
|
||||
/// `role/triple-c-*-instance`.
|
||||
#[allow(dead_code)] // the onboarding module builds it; pinned by tests
|
||||
pub fn user_instance_role_name(env: &str, owner_key: &str) -> String {
|
||||
format!("triple-c-{env}-{owner_key}-instance")
|
||||
}
|
||||
|
||||
/// The SSM parameter holding a user's EC2 record (E11), written by their
|
||||
/// onboarding module only while it has EC2 capacity: a String of JSON with
|
||||
/// exactly [`USER_RECORD_FIELDS`]. Workspace users can read it
|
||||
/// (`parameter/triple-c/*`) though not the state bucket; it lives outside
|
||||
/// `/triple-c/environments/`, which environment detection lists.
|
||||
#[allow(dead_code)] // the app reads it from Task 4 on
|
||||
pub fn user_record_parameter_name(env: &str, owner_key: &str) -> String {
|
||||
format!("/triple-c/users/{env}/{owner_key}")
|
||||
}
|
||||
|
||||
/// The fields of a user's EC2 record, schema 1: `instance_type`,
|
||||
/// `cluster_arn` (the user's cluster, [`user_ec2_stem`]),
|
||||
/// `capacity_provider` (its one provider, the same name) and `user_tag`
|
||||
/// (the `triple-c-user` value the cluster is named after) and
|
||||
/// `max_instances` (the group's maximum), so a re-apply can keep them.
|
||||
/// `environments::parse_user_record` reads them; a test pins the HCL to it.
|
||||
#[allow(dead_code)]
|
||||
pub const USER_RECORD_FIELDS: [&str; 6] = [
|
||||
"schema",
|
||||
"instance_type",
|
||||
"cluster_arn",
|
||||
"capacity_provider",
|
||||
"user_tag",
|
||||
"max_instances",
|
||||
];
|
||||
|
||||
/// The most instances a user's EC2 capacity may run at once.
|
||||
pub const EC2_MAX_INSTANCES: u32 = 4;
|
||||
|
||||
/// A user's optional EC2 capacity, as the onboarding module takes it.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Ec2Compute {
|
||||
/// One of the types `ec2_instance_spec` knows.
|
||||
pub instance_type: String,
|
||||
/// 1 to [`EC2_MAX_INSTANCES`].
|
||||
pub max_instances: u32,
|
||||
/// The user's `triple-c-user` principal tag value.
|
||||
pub user_tag: String,
|
||||
}
|
||||
|
||||
/// Whether a project id is used in names as it is: 1 to 64 characters of
|
||||
/// `[A-Za-z0-9-]`. Every UUID (how the app makes ids) is.
|
||||
fn is_clean_project_id(id: &str) -> bool {
|
||||
@@ -347,16 +449,46 @@ pub fn environment_vars(
|
||||
}
|
||||
|
||||
/// The onboarding module's `terraform.tfvars.json` for the user whose
|
||||
/// `aws:userid` is `owner_id`. Bedrock stays on the workspace user's own
|
||||
/// credentials unless a later setting opts the task role in.
|
||||
pub fn onboarding_vars(env: &str, region: &str, owner_id: &str) -> serde_json::Value {
|
||||
json!({
|
||||
/// `aws:userid` is `owner_id`, with their EC2 capacity or none (empty
|
||||
/// `ec2_instance_type`: the module makes nothing for EC2). Applies the same
|
||||
/// rules as the module's EC2 variable validations. Bedrock stays on the
|
||||
/// workspace user's own credentials unless a later setting opts the task
|
||||
/// role in.
|
||||
pub fn onboarding_vars(
|
||||
env: &str,
|
||||
region: &str,
|
||||
owner_id: &str,
|
||||
ec2: Option<&Ec2Compute>,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let (instance_type, max_instances, user_tag) = match ec2 {
|
||||
None => (String::new(), 1, String::new()),
|
||||
Some(c) => {
|
||||
if crate::models::project::ec2_instance_spec(&c.instance_type).is_none() {
|
||||
return Err(format!(
|
||||
"\"{}\" is not an instance type Triple-C supports (m7i, m7g, c7i, c7g, r7i, \
|
||||
r7g from large to 4xlarge; t3, t4g from large to 2xlarge)",
|
||||
c.instance_type.escape_debug()
|
||||
));
|
||||
}
|
||||
if !(1..=EC2_MAX_INSTANCES).contains(&c.max_instances) {
|
||||
return Err(format!(
|
||||
"The number of instances must be from 1 to {EC2_MAX_INSTANCES}"
|
||||
));
|
||||
}
|
||||
let tag = validate_ec2_user_tag(&c.user_tag)?;
|
||||
(c.instance_type.clone(), c.max_instances, tag)
|
||||
}
|
||||
};
|
||||
Ok(json!({
|
||||
"environment_name": env,
|
||||
"region": region,
|
||||
"owner_id": owner_id,
|
||||
"owner_key": owner_key(region, owner_id),
|
||||
"bedrock_on_task_role": false,
|
||||
})
|
||||
"ec2_instance_type": instance_type,
|
||||
"ec2_max_instances": max_instances,
|
||||
"ec2_user_tag": user_tag,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The boundary module's `terraform.tfvars.json`: the shipped boundary
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -27,10 +27,11 @@ use std::future::Future;
|
||||
use aws_config::SdkConfig;
|
||||
use aws_sdk_ecs::operation::register_task_definition::RegisterTaskDefinitionInput;
|
||||
use aws_sdk_ecs::types::{
|
||||
Compatibility, ContainerDefinition, CpuArchitecture, EfsAuthorizationConfig,
|
||||
EfsAuthorizationConfigIam, EfsTransitEncryption, EfsVolumeConfiguration, EphemeralStorage,
|
||||
HealthCheck, KernelCapabilities, KeyValuePair, LinuxParameters, LogConfiguration, LogDriver,
|
||||
MountPoint, NetworkMode, OsFamily, RuntimePlatform, Secret, SortOrder, Tag,
|
||||
Compatibility, ContainerDefinition, CpuArchitecture, Device, DeviceCgroupPermission,
|
||||
EfsAuthorizationConfig, EfsAuthorizationConfigIam, EfsTransitEncryption,
|
||||
EfsVolumeConfiguration, EphemeralStorage, HealthCheck, HostVolumeProperties,
|
||||
KernelCapabilities, KeyValuePair, LinuxParameters, LogConfiguration, LogDriver, MountPoint,
|
||||
NetworkMode, OsFamily, RuntimePlatform, Secret, SortOrder, SystemControl, Tag,
|
||||
TaskDefinitionField, TaskDefinitionStatus, Volume,
|
||||
};
|
||||
use aws_sdk_secretsmanager::error::ProvideErrorMetadata;
|
||||
@@ -39,7 +40,7 @@ use rand::RngCore;
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use super::environments::EnvironmentInfo;
|
||||
use super::environments::{EnvironmentInfo, UserCompute};
|
||||
use super::error::{classify_sdk_error, AwsError};
|
||||
use super::identity::CallerIdentity;
|
||||
use super::tofu_modules::{
|
||||
@@ -47,7 +48,10 @@ use super::tofu_modules::{
|
||||
user_file_system_token, user_role_names, workspace_access_point_root,
|
||||
};
|
||||
use crate::host::ssm::AGENT_PORT;
|
||||
use crate::models::project::{validate_aws_workspace, Arch, RepoSource, SecretsDelivery, StorageMode};
|
||||
use crate::models::project::{
|
||||
ec2_fit_problem, fits_instance, validate_aws_workspace, Arch, Fit, LaunchType, RepoSource,
|
||||
SecretsDelivery, StorageMode,
|
||||
};
|
||||
use crate::models::Project;
|
||||
|
||||
pub const TAG_OWNER: &str = "triple-c:owner";
|
||||
@@ -103,6 +107,11 @@ pub const MAX_GATEWAY_CONFIG_BYTES: usize = 8_192;
|
||||
/// Seconds ECS waits after SIGTERM before SIGKILL (Fargate allows 2–120).
|
||||
pub const STOP_TIMEOUT_SECS: i32 = 120;
|
||||
|
||||
/// The instance's Docker socket, mounted at the same path in an EC2 task
|
||||
/// with Docker access (E6).
|
||||
pub const DOCKER_SOCKET_PATH: &str = "/var/run/docker.sock";
|
||||
const DOCKER_SOCKET_VOLUME: &str = "docker-socket";
|
||||
|
||||
/// The task definition's whole `environment` (R2): constants, no project data.
|
||||
const FIXED_ENVIRONMENT: [(&str, &str); 2] = [("TRIPLEC_HOST", "aws"), ("COLORTERM", "truecolor")];
|
||||
|
||||
@@ -205,6 +214,12 @@ pub fn gateway_sidecar_problem(
|
||||
if !project.aws.as_ref().is_some_and(|a| a.gateway_sidecar) {
|
||||
return None;
|
||||
}
|
||||
if project.aws.as_ref().is_some_and(|a| a.launch_type == LaunchType::Ec2) {
|
||||
// EC2 tasks use `bridge` networking (ruling R2): the containers of a
|
||||
// task don't share a loopback, and the sidecar must never listen
|
||||
// beyond it. Not offered on EC2 yet.
|
||||
return Some(EC2_NO_SIDECAR.into());
|
||||
}
|
||||
if project.backend != crate::models::project::Backend::OpenAiCompatible {
|
||||
return Some(
|
||||
"The LLM gateway sidecar needs the OpenAI Compatible backend: choose it in the \
|
||||
@@ -222,6 +237,10 @@ pub fn gateway_sidecar_problem(
|
||||
None
|
||||
}
|
||||
|
||||
/// Why an EC2 project can't run the gateway sidecar (yet).
|
||||
pub const EC2_NO_SIDECAR: &str = "The LLM gateway sidecar is not available on EC2 yet: turn it \
|
||||
off, or run the project on Fargate";
|
||||
|
||||
/// `workspace_env`'s fingerprint of the custom variables' values.
|
||||
const CUSTOM_ENV_FINGERPRINT: &str = "TRIPLE_C_CUSTOM_ENV";
|
||||
|
||||
@@ -473,6 +492,21 @@ pub struct WorkspacePlan {
|
||||
/// The gateway sidecar's image (Q7) when the project runs it; `None`
|
||||
/// leaves the request exactly as it was before the sidecar existed.
|
||||
pub gateway_image: Option<String>,
|
||||
/// Run on the user's own EC2 instances (phase 7) rather than Fargate;
|
||||
/// `None` leaves the request exactly as it was before EC2 existed.
|
||||
pub ec2: Option<Ec2Plan>,
|
||||
}
|
||||
|
||||
/// What an EC2 task definition adds (E6, E7). `cpu`/`memory` are the task's
|
||||
/// reservations on the instance; arch and ephemeral storage don't apply (the
|
||||
/// instance decides both).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
pub struct Ec2Plan {
|
||||
/// Mount the instance's Docker socket (`allow_docker_access`, E6).
|
||||
pub docker_socket: bool,
|
||||
/// What a VPN client needs (`vpn_support_enabled`, E7): `NET_ADMIN`,
|
||||
/// `/dev/net/tun`, `net.ipv4.conf.all.src_valid_mark=1`.
|
||||
pub vpn: bool,
|
||||
}
|
||||
|
||||
/// sha256 (hex) over the task definition `plan` produces, minus the label
|
||||
@@ -583,12 +617,7 @@ fn build_request(plan: &WorkspacePlan) -> RegisterTaskDefinitionInput {
|
||||
.build()
|
||||
.expect(BUILT),
|
||||
)
|
||||
.linux_parameters(
|
||||
LinuxParameters::builder()
|
||||
.init_process_enabled(true)
|
||||
.capabilities(KernelCapabilities::builder().add("SYS_PTRACE").build())
|
||||
.build(),
|
||||
)
|
||||
.linux_parameters(linux_parameters(plan.ec2))
|
||||
.log_configuration(
|
||||
LogConfiguration::builder()
|
||||
.log_driver(LogDriver::Awslogs)
|
||||
@@ -616,29 +645,64 @@ fn build_request(plan: &WorkspacePlan) -> RegisterTaskDefinitionInput {
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
let docker_socket = plan.ec2.is_some_and(|e| e.docker_socket);
|
||||
if docker_socket {
|
||||
container = container.mount_points(
|
||||
MountPoint::builder()
|
||||
.source_volume(DOCKER_SOCKET_VOLUME)
|
||||
.container_path(DOCKER_SOCKET_PATH)
|
||||
.read_only(false)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
if plan.ec2.is_some_and(|e| e.vpn) {
|
||||
// Namespaced `net.*` sysctls are allowed in `bridge` mode: each
|
||||
// container has its own network namespace, so this sets the
|
||||
// workspace's, never the instance's (ECS warns only for `host`, and
|
||||
// for several containers of one `awsvpc` task).
|
||||
for (key, value) in crate::docker::container::vpn_requirements().sysctls {
|
||||
container = container
|
||||
.system_controls(SystemControl::builder().namespace(key).value(value).build());
|
||||
}
|
||||
}
|
||||
|
||||
let mut req = RegisterTaskDefinitionInput::builder()
|
||||
.family(&plan.family)
|
||||
.task_role_arn(&plan.task_role_arn)
|
||||
.execution_role_arn(&plan.exec_role_arn)
|
||||
.network_mode(NetworkMode::Awsvpc)
|
||||
.requires_compatibilities(Compatibility::Fargate)
|
||||
.cpu(plan.cpu.to_string())
|
||||
.memory(plan.memory.to_string())
|
||||
.runtime_platform(
|
||||
RuntimePlatform::builder()
|
||||
.cpu_architecture(match plan.arch {
|
||||
Arch::X86_64 => CpuArchitecture::X8664,
|
||||
Arch::Arm64 => CpuArchitecture::Arm64,
|
||||
})
|
||||
.operating_system_family(OsFamily::Linux)
|
||||
.build(),
|
||||
)
|
||||
.ephemeral_storage(
|
||||
EphemeralStorage::builder()
|
||||
.size_in_gib(plan.ephemeral_gib as i32)
|
||||
.build(),
|
||||
)
|
||||
.execution_role_arn(&plan.exec_role_arn);
|
||||
req = match plan.ec2 {
|
||||
// Fargate: exactly the request from before EC2 existed (pinned).
|
||||
None => req
|
||||
.network_mode(NetworkMode::Awsvpc)
|
||||
.requires_compatibilities(Compatibility::Fargate)
|
||||
.cpu(plan.cpu.to_string())
|
||||
.memory(plan.memory.to_string())
|
||||
.runtime_platform(
|
||||
RuntimePlatform::builder()
|
||||
.cpu_architecture(match plan.arch {
|
||||
Arch::X86_64 => CpuArchitecture::X8664,
|
||||
Arch::Arm64 => CpuArchitecture::Arm64,
|
||||
})
|
||||
.operating_system_family(OsFamily::Linux)
|
||||
.build(),
|
||||
)
|
||||
.ephemeral_storage(
|
||||
EphemeralStorage::builder()
|
||||
.size_in_gib(plan.ephemeral_gib as i32)
|
||||
.build(),
|
||||
),
|
||||
// EC2 (ruling R2: `bridge`, the instance is the user's alone). No
|
||||
// runtime platform — the instance's architecture decides and the
|
||||
// image is multi-arch — and no ephemeral storage (the instance's
|
||||
// disk). Task-level cpu/memory are the reservations `fits_instance`
|
||||
// checked against the user's instance type.
|
||||
Some(_) => req
|
||||
.network_mode(NetworkMode::Bridge)
|
||||
.requires_compatibilities(Compatibility::Ec2)
|
||||
.cpu(plan.cpu.to_string())
|
||||
.memory(plan.memory.to_string()),
|
||||
};
|
||||
let mut req = req
|
||||
.container_definitions(container.build())
|
||||
.set_tags(Some(
|
||||
plan.tags
|
||||
@@ -655,9 +719,45 @@ fn build_request(plan: &WorkspacePlan) -> RegisterTaskDefinitionInput {
|
||||
{
|
||||
req = req.volumes(efs_volume(name, fs, ap));
|
||||
}
|
||||
if docker_socket {
|
||||
req = req.volumes(
|
||||
Volume::builder()
|
||||
.name(DOCKER_SOCKET_VOLUME)
|
||||
.host(HostVolumeProperties::builder().source_path(DOCKER_SOCKET_PATH).build())
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
req.build().expect(BUILT)
|
||||
}
|
||||
|
||||
/// The workspace container's Linux parameters: docker-init and `SYS_PTRACE`
|
||||
/// always; on EC2 with VPN support, [`vpn_requirements`]' capability and
|
||||
/// device too (Fargate allows neither).
|
||||
///
|
||||
/// [`vpn_requirements`]: crate::docker::container::vpn_requirements
|
||||
fn linux_parameters(ec2: Option<Ec2Plan>) -> LinuxParameters {
|
||||
let mut caps = KernelCapabilities::builder().add("SYS_PTRACE");
|
||||
let mut lp = LinuxParameters::builder().init_process_enabled(true);
|
||||
if ec2.is_some_and(|e| e.vpn) {
|
||||
let vpn = crate::docker::container::vpn_requirements();
|
||||
for cap in vpn.capabilities {
|
||||
caps = caps.add(cap);
|
||||
}
|
||||
for (host, container, perms) in vpn.devices {
|
||||
let mut device = Device::builder().host_path(host).container_path(container);
|
||||
for p in perms.chars() {
|
||||
device = device.permissions(match p {
|
||||
'r' => DeviceCgroupPermission::Read,
|
||||
'w' => DeviceCgroupPermission::Write,
|
||||
_ => DeviceCgroupPermission::Mknod,
|
||||
});
|
||||
}
|
||||
lp = lp.devices(device.build().expect("every required field is set"));
|
||||
}
|
||||
}
|
||||
lp.capabilities(caps.build()).build()
|
||||
}
|
||||
|
||||
/// `secrets` entries for `keys`, each by `valueFrom: <arn>:<KEY>::`.
|
||||
fn secrets<'k>(plan: &WorkspacePlan, keys: impl Iterator<Item = &'k String>) -> Vec<Secret> {
|
||||
keys.map(|k| {
|
||||
@@ -902,9 +1002,56 @@ pub struct PreparedWorkspace {
|
||||
pub secret_arn: String,
|
||||
}
|
||||
|
||||
/// Why an EC2 project can't start on `compute` (the user's record, `None`
|
||||
/// when they have none), or what its task definition adds. A Fargate project
|
||||
/// gets `Ok(None)` whatever `compute` is.
|
||||
pub fn ec2_plan(
|
||||
project: &Project,
|
||||
env: &str,
|
||||
compute: Option<&UserCompute>,
|
||||
) -> Result<Option<Ec2Plan>, String> {
|
||||
let Some(cfg) = project.aws.as_ref().filter(|c| c.launch_type == LaunchType::Ec2) else {
|
||||
return Ok(None);
|
||||
};
|
||||
if cfg.gateway_sidecar {
|
||||
return Err(EC2_NO_SIDECAR.into());
|
||||
}
|
||||
let compute = compute.ok_or_else(|| no_ec2_capacity(env))?;
|
||||
match fits_instance(cfg, &compute.instance_type) {
|
||||
Fit::Ok => {}
|
||||
Fit::TooBig { .. } => {
|
||||
let problem = ec2_fit_problem(cfg, &compute.instance_type).unwrap_or_default();
|
||||
return Err(format!(
|
||||
"{problem} Lower the reservations in the project's Host settings, or ask an \
|
||||
admin for a larger instance type."
|
||||
));
|
||||
}
|
||||
// The record is validated against the same table, so this is a
|
||||
// newer app's type: let ECS decide.
|
||||
Fit::UnknownType => log::warn!(
|
||||
"Project {}: instance type {} is not in the app's table; its fit is unchecked",
|
||||
project.id,
|
||||
compute.instance_type
|
||||
),
|
||||
}
|
||||
Ok(Some(Ec2Plan {
|
||||
docker_socket: project.allow_docker_access,
|
||||
vpn: project.vpn_support_enabled,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The user has no EC2 record in `env` (none was added, or it was removed).
|
||||
pub fn no_ec2_capacity(env: &str) -> String {
|
||||
format!(
|
||||
"You have no EC2 capacity in {env}; ask an admin to add it to your onboarding (or run \
|
||||
the project on Fargate)."
|
||||
)
|
||||
}
|
||||
|
||||
/// Make the project's secret, access points (Hybrid, EFS) and task definition
|
||||
/// ready. Everything that can be checked locally — placement, sizes, the
|
||||
/// payload's size and names — is checked before the first write.
|
||||
/// payload's size and names, an EC2 project's fit on `compute` — is checked
|
||||
/// before the first write.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn prepare_workspace<W: WorkspaceApi>(
|
||||
api: &W,
|
||||
@@ -916,6 +1063,7 @@ pub async fn prepare_workspace<W: WorkspaceApi>(
|
||||
custom_env_keys: &[String],
|
||||
image: &str,
|
||||
gateway: Option<GatewayPayload<'_>>,
|
||||
compute: Option<&UserCompute>,
|
||||
) -> Result<PreparedWorkspace, AwsError> {
|
||||
let (Some(placement), Some(cfg)) = (project.aws_placement.as_ref(), project.aws.as_ref())
|
||||
else {
|
||||
@@ -940,6 +1088,7 @@ pub async fn prepare_workspace<W: WorkspaceApi>(
|
||||
)));
|
||||
}
|
||||
validate_aws_workspace(cfg).map_err(other)?;
|
||||
let ec2 = ec2_plan(project, &env.name, compute).map_err(other)?;
|
||||
if image.trim().is_empty() {
|
||||
return Err(other("No container image is set for AWS"));
|
||||
}
|
||||
@@ -1100,6 +1249,7 @@ pub async fn prepare_workspace<W: WorkspaceApi>(
|
||||
workspace_access_point_id: workspace_access_point_id.clone(),
|
||||
tags,
|
||||
gateway_image: gateway.map(|_| crate::docker::gateway::registry_image_ref()),
|
||||
ec2,
|
||||
};
|
||||
let fingerprint = workspace_fingerprint(&plan);
|
||||
let task_definition_arn =
|
||||
|
||||
@@ -102,6 +102,7 @@ fn sample_plan(storage: StorageMode) -> WorkspacePlan {
|
||||
.then(|| "fsap-4567efab".to_string()),
|
||||
tags: workspace_tags(USER, PROJECT_ID, "dev"),
|
||||
gateway_image: None,
|
||||
ec2: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -962,7 +963,7 @@ async fn prepare(
|
||||
p: &Project,
|
||||
env: &[(String, String)],
|
||||
) -> Result<PreparedWorkspace, AwsError> {
|
||||
prepare_workspace(fake, &who(), &env_info(), p, env, &[], &[], IMAGE, None).await
|
||||
prepare_workspace(fake, &who(), &env_info(), p, env, &[], &[], IMAGE, None, None).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -972,7 +973,7 @@ async fn prepare_puts_the_setup_script_and_the_environments_cap_in_the_secret()
|
||||
p.aws.as_mut().unwrap().setup_script = Some("sudo apt-get install -y jq".into());
|
||||
let mut env = env_info();
|
||||
env.max_workspace_hours = 12;
|
||||
prepare_workspace(&fake, &who(), &env, &p, &[], &[], &[], IMAGE, None).await.unwrap();
|
||||
prepare_workspace(&fake, &who(), &env, &p, &[], &[], &[], IMAGE, None, None).await.unwrap();
|
||||
let m = payload_map(&fake.secrets.lock().unwrap()[0].2);
|
||||
assert_eq!(m["TRIPLEC_SETUP_SCRIPT"], "sudo apt-get install -y jq");
|
||||
assert_eq!(m["TRIPLEC_MAX_LIFETIME_MINUTES"], "720");
|
||||
@@ -1397,6 +1398,7 @@ async fn files_travel_in_the_secret_only() {
|
||||
&[],
|
||||
IMAGE,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1638,7 +1640,7 @@ async fn prepare_leaves_the_credentials_out_of_a_copy_at_connect_secret() {
|
||||
p.aws.as_mut().unwrap().secrets_delivery = crate::models::project::SecretsDelivery::CopyAtConnect;
|
||||
let files = vec![("ssh/id_ed25519".to_string(), b"PRIVATE KEY".to_vec())];
|
||||
let env = pairs(&[("GIT_TOKEN", "ghp_secret"), ("MINE", "x"), ("TZ", "UTC")]);
|
||||
prepare_workspace(&fake, &who(), &env_info(), &p, &env, &files, &["MINE".to_string()], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), &p, &env, &files, &["MINE".to_string()], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let m = payload_map(&fake.secrets.lock().unwrap()[0].2);
|
||||
@@ -1662,7 +1664,7 @@ async fn switching_to_copy_at_connect_replaces_a_secret_that_held_credentials()
|
||||
Some(r#"{"GIT_TOKEN":"ghp_old","TRIPLEC_AGENT_TOKEN":"t","TZ":"UTC"}"#.into());
|
||||
*fake.pending_puts.lock().unwrap() = 2; // AWS still finishing the delete
|
||||
let env = pairs(&[("GIT_TOKEN", "ghp_new"), ("TZ", "UTC")]);
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(*fake.secret_reads.lock().unwrap(), 1, "read only once it exists");
|
||||
@@ -1675,7 +1677,7 @@ async fn switching_to_copy_at_connect_replaces_a_secret_that_held_credentials()
|
||||
*fake.existing_secret.lock().unwrap() = Some(
|
||||
r#"{"TRIPLEC_AGENT_TOKEN":"t","TRIPLEC_CREDENTIALS":"connect","TZ":"UTC","TRIPLEC_REPOS":"[]"}"#.into(),
|
||||
);
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(fake.deleted_secrets.lock().unwrap().is_empty());
|
||||
@@ -1684,7 +1686,7 @@ async fn switching_to_copy_at_connect_replaces_a_secret_that_held_credentials()
|
||||
// No secret yet (a first start): created, never read — a read of a
|
||||
// secret that doesn't exist may be refused — nothing deleted.
|
||||
let fake = Fake::onboarded();
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(*fake.creates.lock().unwrap(), 1);
|
||||
@@ -1737,7 +1739,7 @@ async fn the_start_after_a_timed_out_replace_retries_the_create() {
|
||||
let env = pairs(&[("TZ", "UTC")]);
|
||||
let fake = Fake::onboarded();
|
||||
*fake.pending_puts.lock().unwrap() = 3;
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(fake.secrets.lock().unwrap().len(), 1, "made once the name is free");
|
||||
@@ -1752,7 +1754,7 @@ async fn the_start_after_a_timed_out_replace_retries_the_create() {
|
||||
// Still taken past the limit: try again in a minute, never "restore".
|
||||
let fake = Fake::onboarded();
|
||||
*fake.pending_puts.lock().unwrap() = 1000;
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
@@ -1770,7 +1772,7 @@ async fn a_copy_at_connect_read_of_a_secret_being_deleted_is_retried() {
|
||||
r#"{"TRIPLEC_AGENT_TOKEN":"t","TRIPLEC_CREDENTIALS":"connect","TZ":"UTC","TRIPLEC_REPOS":"[]"}"#.into(),
|
||||
);
|
||||
*fake.pending_reads.lock().unwrap() = 3;
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(*fake.secret_reads.lock().unwrap(), 4);
|
||||
@@ -1780,7 +1782,7 @@ async fn a_copy_at_connect_read_of_a_secret_being_deleted_is_retried() {
|
||||
let fake = Fake::onboarded();
|
||||
*fake.existing_secret.lock().unwrap() = Some(r#"{"TZ":"UTC"}"#.into());
|
||||
*fake.pending_reads.lock().unwrap() = 1000;
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None)
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
@@ -1985,7 +1987,7 @@ fn sidecar_project() -> Project {
|
||||
async fn prepare_registers_the_sidecar_only_when_the_project_runs_it() {
|
||||
let fake = Fake::onboarded();
|
||||
let p = sidecar_project();
|
||||
prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, Some(gw()))
|
||||
prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, Some(gw()), None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(fake.registered.lock().unwrap()[0].container_definitions().len(), 2);
|
||||
@@ -1994,7 +1996,7 @@ async fn prepare_registers_the_sidecar_only_when_the_project_runs_it() {
|
||||
|
||||
// Toggle off: a payload handed in anyway is ignored.
|
||||
let fake = Fake::onboarded();
|
||||
prepare_workspace(&fake, &who(), &env_info(), &project(StorageMode::Ephemeral), &[], &[], &[], IMAGE, Some(gw()))
|
||||
prepare_workspace(&fake, &who(), &env_info(), &project(StorageMode::Ephemeral), &[], &[], &[], IMAGE, Some(gw()), None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(fake.registered.lock().unwrap()[0].container_definitions().len(), 1);
|
||||
@@ -2006,7 +2008,7 @@ async fn prepare_registers_the_sidecar_only_when_the_project_runs_it() {
|
||||
async fn prepare_refuses_a_sidecar_without_its_payload_or_with_an_oversized_config() {
|
||||
let fake = Fake::onboarded();
|
||||
let p = sidecar_project();
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, None)
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(e.to_string().contains("gateway"), "{e}");
|
||||
@@ -2021,6 +2023,7 @@ async fn prepare_refuses_a_sidecar_without_its_payload_or_with_an_oversized_conf
|
||||
&[],
|
||||
IMAGE,
|
||||
Some(GatewayPayload { config: &big, ..gw() }),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
@@ -2035,6 +2038,7 @@ async fn prepare_refuses_a_sidecar_without_its_payload_or_with_an_oversized_conf
|
||||
&[],
|
||||
IMAGE,
|
||||
Some(GatewayPayload { api_key: " ", ..gw() }),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
@@ -2084,3 +2088,222 @@ fn the_sidecars_token_is_not_a_leftover_credential_but_a_dropped_provider_key_is
|
||||
[("TRIPLEC_AGENT_TOKEN", "t")].iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
|
||||
assert!(holds(sidecar_old, &off));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- EC2 (phase 7)
|
||||
|
||||
fn ec2_sample(storage: StorageMode, docker_socket: bool, vpn: bool) -> WorkspacePlan {
|
||||
WorkspacePlan { ec2: Some(Ec2Plan { docker_socket, vpn }), ..sample_plan(storage) }
|
||||
}
|
||||
|
||||
fn compute(instance_type: &str) -> UserCompute {
|
||||
UserCompute {
|
||||
instance_type: instance_type.into(),
|
||||
max_instances: 2,
|
||||
user_tag: "alice".into(),
|
||||
cluster_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/triple-c-dev--u-alice"),
|
||||
capacity_provider: "triple-c-dev--u-alice".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ec2_project(storage: StorageMode, cpu: u32, memory_mib: u32) -> Project {
|
||||
let mut p = project(storage);
|
||||
let a = p.aws.as_mut().unwrap();
|
||||
a.launch_type = crate::models::LaunchType::Ec2;
|
||||
a.cpu = cpu;
|
||||
a.memory_mib = memory_mib;
|
||||
p
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_request_is_bridge_mode_with_the_reservations_and_no_fargate_fields() {
|
||||
for storage in [StorageMode::Ephemeral, StorageMode::Hybrid, StorageMode::Efs] {
|
||||
let plan = ec2_sample(storage, false, false);
|
||||
let req = task_definition_request(&plan);
|
||||
assert_eq!(req.requires_compatibilities(), &[aws_sdk_ecs::types::Compatibility::Ec2]);
|
||||
assert_eq!(req.network_mode(), Some(&aws_sdk_ecs::types::NetworkMode::Bridge));
|
||||
assert_eq!((req.cpu(), req.memory()), (Some("1024"), Some("4096")));
|
||||
assert!(req.runtime_platform().is_none(), "the instance's architecture decides");
|
||||
assert!(req.ephemeral_storage().is_none(), "the instance's disk");
|
||||
// Everything else as on Fargate: roles, tags, health check, stop
|
||||
// timeout, init, logs, the EFS volumes and the fingerprint label.
|
||||
let fargate = task_definition_request(&sample_plan(storage));
|
||||
assert_eq!(req.task_role_arn(), fargate.task_role_arn());
|
||||
assert_eq!(req.execution_role_arn(), fargate.execution_role_arn());
|
||||
assert_eq!(req.tags(), fargate.tags());
|
||||
assert_eq!(req.volumes(), fargate.volumes());
|
||||
let (c, f) = (&req.container_definitions()[0], &fargate.container_definitions()[0]);
|
||||
assert_eq!(c.health_check(), f.health_check());
|
||||
assert_eq!(c.stop_timeout(), Some(STOP_TIMEOUT_SECS));
|
||||
assert_eq!(c.mount_points(), f.mount_points());
|
||||
assert_eq!(c.secrets(), f.secrets());
|
||||
assert_eq!(c.environment(), f.environment());
|
||||
assert_eq!(c.log_configuration(), f.log_configuration());
|
||||
assert_eq!(c.linux_parameters(), f.linux_parameters(), "SYS_PTRACE and init only");
|
||||
assert!(c.system_controls().is_empty());
|
||||
assert!(c.port_mappings().is_empty());
|
||||
assert_eq!(
|
||||
c.docker_labels().unwrap().get(FINGERPRINT_LABEL),
|
||||
Some(&workspace_fingerprint(&plan))
|
||||
);
|
||||
assert_ne!(workspace_fingerprint(&plan), workspace_fingerprint(&sample_plan(storage)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ec2_request_for_each_toggle_combination() {
|
||||
let mut fingerprints = std::collections::BTreeSet::new();
|
||||
for (docker, vpn) in [(false, false), (true, false), (false, true), (true, true)] {
|
||||
let plan = ec2_sample(StorageMode::Hybrid, docker, vpn);
|
||||
let req = task_definition_request(&plan);
|
||||
fingerprints.insert(workspace_fingerprint(&plan));
|
||||
let c = &req.container_definitions()[0];
|
||||
let lp = c.linux_parameters().unwrap();
|
||||
assert_eq!(lp.init_process_enabled(), Some(true));
|
||||
|
||||
// E6: the instance's Docker socket at the same path.
|
||||
let socket_volume: Vec<_> =
|
||||
req.volumes().iter().filter(|v| v.name() == Some("docker-socket")).collect();
|
||||
let socket_mount: Vec<_> = c
|
||||
.mount_points()
|
||||
.iter()
|
||||
.filter(|m| m.container_path() == Some(DOCKER_SOCKET_PATH))
|
||||
.collect();
|
||||
if docker {
|
||||
assert_eq!(socket_volume.len(), 1);
|
||||
assert_eq!(
|
||||
socket_volume[0].host().and_then(|h| h.source_path()),
|
||||
Some("/var/run/docker.sock")
|
||||
);
|
||||
assert!(socket_volume[0].efs_volume_configuration().is_none());
|
||||
assert_eq!(socket_mount.len(), 1);
|
||||
assert_eq!(socket_mount[0].source_volume(), Some("docker-socket"));
|
||||
} else {
|
||||
assert!(socket_volume.is_empty() && socket_mount.is_empty());
|
||||
}
|
||||
// The home access point is mounted either way.
|
||||
assert!(c.mount_points().iter().any(|m| m.container_path() == Some(HOME_MOUNT_PATH)));
|
||||
|
||||
// E7: the three VPN pieces, all or none, plus SYS_PTRACE as always.
|
||||
let caps = lp.capabilities().unwrap().add();
|
||||
let sysctls: Vec<(&str, &str)> = c
|
||||
.system_controls()
|
||||
.iter()
|
||||
.map(|s| (s.namespace().unwrap(), s.value().unwrap()))
|
||||
.collect();
|
||||
if vpn {
|
||||
assert_eq!(caps, ["SYS_PTRACE".to_string(), "NET_ADMIN".to_string()]);
|
||||
assert_eq!(lp.devices().len(), 1);
|
||||
let d = &lp.devices()[0];
|
||||
assert_eq!((d.host_path(), d.container_path()), ("/dev/net/tun", Some("/dev/net/tun")));
|
||||
use aws_sdk_ecs::types::DeviceCgroupPermission as P;
|
||||
assert_eq!(d.permissions(), [P::Read, P::Write, P::Mknod]);
|
||||
assert_eq!(sysctls, [("net.ipv4.conf.all.src_valid_mark", "1")]);
|
||||
} else {
|
||||
assert_eq!(caps, ["SYS_PTRACE".to_string()]);
|
||||
assert!(lp.devices().is_empty() && sysctls.is_empty());
|
||||
}
|
||||
}
|
||||
assert_eq!(fingerprints.len(), 4, "every combination is its own revision");
|
||||
}
|
||||
|
||||
/// The VPN pieces are Docker's own (`vpn_host_config`), not a second list.
|
||||
#[test]
|
||||
fn the_ec2_vpn_pieces_are_the_docker_ones() {
|
||||
let vpn = crate::docker::container::vpn_requirements();
|
||||
assert_eq!(vpn.capabilities, ["NET_ADMIN"]);
|
||||
assert_eq!(
|
||||
vpn.devices,
|
||||
[("/dev/net/tun".to_string(), "/dev/net/tun".to_string(), "rwm".to_string())]
|
||||
);
|
||||
assert_eq!(vpn.sysctls, [("net.ipv4.conf.all.src_valid_mark".to_string(), "1".to_string())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ec2_plan_checks_the_record_and_the_fit_and_carries_the_toggles() {
|
||||
// Fargate: nothing, whatever the record says.
|
||||
let fargate = project(StorageMode::Hybrid);
|
||||
assert_eq!(ec2_plan(&fargate, "dev", None), Ok(None));
|
||||
assert_eq!(ec2_plan(&fargate, "dev", Some(&compute("m7i.large"))), Ok(None));
|
||||
|
||||
// No record: the onboarding has no EC2 capacity (or it was removed).
|
||||
let mut p = ec2_project(StorageMode::Hybrid, 1024, 4096);
|
||||
let e = ec2_plan(&p, "dev", None).unwrap_err();
|
||||
assert_eq!(
|
||||
e,
|
||||
"You have no EC2 capacity in dev; ask an admin to add it to your onboarding (or run the \
|
||||
project on Fargate)."
|
||||
);
|
||||
|
||||
// Fits: the toggles ride along.
|
||||
p.allow_docker_access = true;
|
||||
assert_eq!(
|
||||
ec2_plan(&p, "dev", Some(&compute("m7i.large"))),
|
||||
Ok(Some(Ec2Plan { docker_socket: true, vpn: false }))
|
||||
);
|
||||
p.vpn_support_enabled = true;
|
||||
assert_eq!(
|
||||
ec2_plan(&p, "dev", Some(&compute("m7i.large"))),
|
||||
Ok(Some(Ec2Plan { docker_socket: true, vpn: true }))
|
||||
);
|
||||
|
||||
// Too big: names what the instance has for a workspace (10 % memory
|
||||
// margin: ECS registers less than the nominal 8 GiB).
|
||||
let big = ec2_project(StorageMode::Hybrid, 2048, 8192);
|
||||
let e = ec2_plan(&big, "dev", Some(&compute("m7i.large"))).unwrap_err();
|
||||
assert!(e.contains("m7i.large"), "{e}");
|
||||
assert!(e.contains("1920 CPU units, 7373 MiB"), "{e}");
|
||||
assert!(ec2_plan(&big, "dev", Some(&compute("m7i.xlarge"))).is_ok());
|
||||
|
||||
// The gateway sidecar is not offered on EC2 (bridge networking).
|
||||
let mut gw_on = ec2_project(StorageMode::Hybrid, 1024, 4096);
|
||||
gw_on.aws.as_mut().unwrap().gateway_sidecar = true;
|
||||
assert_eq!(ec2_plan(&gw_on, "dev", Some(&compute("m7i.large"))).unwrap_err(), EC2_NO_SIDECAR);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sidecar_is_refused_on_ec2() {
|
||||
use crate::models::gateway_settings::{GatewayModel, GatewaySettings};
|
||||
let models = GatewaySettings {
|
||||
models: vec![GatewayModel { name: "gpt".into(), model_id: "gpt-5".into() }],
|
||||
..Default::default()
|
||||
};
|
||||
let mut p = ec2_project(StorageMode::Hybrid, 1024, 4096);
|
||||
p.backend = crate::models::project::Backend::OpenAiCompatible;
|
||||
p.aws.as_mut().unwrap().gateway_sidecar = true;
|
||||
assert_eq!(gateway_sidecar_problem(&p, &models).as_deref(), Some(EC2_NO_SIDECAR));
|
||||
p.aws.as_mut().unwrap().gateway_sidecar = false;
|
||||
assert_eq!(gateway_sidecar_problem(&p, &models), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_project_registers_an_ec2_revision_on_the_users_instance() {
|
||||
let fake = Fake::onboarded();
|
||||
let mut p = ec2_project(StorageMode::Hybrid, 1024, 4096);
|
||||
p.allow_docker_access = true;
|
||||
let c = compute("m7i.large");
|
||||
prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, None, Some(&c))
|
||||
.await
|
||||
.unwrap();
|
||||
let reg = fake.registered.lock().unwrap();
|
||||
assert_eq!(reg.len(), 1);
|
||||
assert_eq!(reg[0].requires_compatibilities(), &[aws_sdk_ecs::types::Compatibility::Ec2]);
|
||||
assert_eq!(reg[0].network_mode(), Some(&aws_sdk_ecs::types::NetworkMode::Bridge));
|
||||
assert!(reg[0].volumes().iter().any(|v| v.name() == Some("docker-socket")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_project_without_capacity_or_room_fails_before_any_write() {
|
||||
let p = ec2_project(StorageMode::Hybrid, 1024, 4096);
|
||||
let fake = Fake::onboarded();
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), &p, &[], &[], &[], IMAGE, None, None)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(e.to_string().contains("no EC2 capacity in dev"), "{e}");
|
||||
let big = ec2_project(StorageMode::Hybrid, 2048, 8192);
|
||||
let c = compute("m7i.large");
|
||||
let e = prepare_workspace(&fake, &who(), &env_info(), &big, &[], &[], &[], IMAGE, None, Some(&c))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(e.to_string().contains("more than your EC2 instance type"), "{e}");
|
||||
assert_eq!(fake.writes(), 0);
|
||||
}
|
||||
@@ -9,6 +9,12 @@ pub async fn check_docker() -> Result<bool, String> {
|
||||
docker::check_docker_available().await
|
||||
}
|
||||
|
||||
/// Like `check_docker`, but says *why* Docker is unavailable.
|
||||
#[tauri::command]
|
||||
pub async fn check_docker_status() -> Result<docker::status::DockerStatus, String> {
|
||||
Ok(docker::client::docker_status().await)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn check_image_exists(state: State<'_, AppState>) -> Result<bool, String> {
|
||||
let settings = state.settings_store.get();
|
||||
|
||||
@@ -30,7 +30,7 @@ use std::sync::{Mutex, OnceLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use aws_config::SdkConfig;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
|
||||
@@ -38,17 +38,20 @@ use aws_sdk_s3::error::ProvideErrorMetadata;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
use crate::aws::environments::{
|
||||
ensure_state_bucket, scan, state_bucket_policy, LIVE_LOCK_SECS, DetectedEnvironment,
|
||||
EnvironmentAction, EnvironmentInfo, EnvironmentScan, EnvironmentState, Knowable, LocalRun,
|
||||
OnboardingState, SdkDetectApi,
|
||||
cluster_holder, ec2_name_holders, ensure_state_bucket, scan, state_bucket_policy,
|
||||
user_record_from, DetectedEnvironment, EnvironmentAction, EnvironmentInfo, EnvironmentScan,
|
||||
EnvironmentState, Knowable, LocalRun, NameHolder, OnboardApi, OnboardingState, SdkDetectApi,
|
||||
SdkOnboardApi, UserCompute, UserRecord, LIVE_LOCK_SECS,
|
||||
};
|
||||
use crate::aws::tofu::{
|
||||
self, backend_hcl, StartError, TofuEvent, TofuOp, TofuOutcome, Workdir,
|
||||
};
|
||||
use crate::aws::tofu_modules::{
|
||||
boundary_vars, environment_vars, module_files, onboarding_vars, owner_key, state_bucket_name,
|
||||
state_key, validate_environment_name, Deployment, ModuleKind, NetworkChoice,
|
||||
state_key, user_ec2_stem, user_record_parameter_name, validate_ec2_user_tag,
|
||||
validate_environment_name, Deployment, Ec2Compute, ModuleKind, NetworkChoice,
|
||||
};
|
||||
use crate::models::project::{ec2_instance_types, Ec2InstanceType, DEFAULT_EC2_INSTANCE_TYPE};
|
||||
use crate::aws::{caller_identity, sdk_config, AwsError, CallerIdentity};
|
||||
use crate::models::{validate_supported_region, AwsAuth, HostConnection, HostKind};
|
||||
use crate::storage::secure;
|
||||
@@ -272,7 +275,7 @@ pub(crate) fn onboarding_unlock_refusal(
|
||||
}
|
||||
let env = find(scan, name).ok_or_else(|| format!("No environment named \"{name}\" was found"))?;
|
||||
match &env.onboarding {
|
||||
Some(OnboardingState::Incomplete { lock: Some(l) })
|
||||
Some(OnboardingState::Incomplete { lock: Some(l), .. })
|
||||
if !l.local_process && env.onboarding_actions.contains(&EnvironmentAction::Unlock) =>
|
||||
{
|
||||
Ok(())
|
||||
@@ -298,6 +301,140 @@ pub(crate) fn onboard_refusal(scan: &EnvironmentScan, name: &str) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The EC2 capacity an onboarding asks for: none (Fargate only), or EC2
|
||||
/// with these values. A command given no choice keeps what the user's
|
||||
/// record holds ([`resolve_ec2`]).
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum Ec2Choice {
|
||||
None,
|
||||
Ec2(Ec2Compute),
|
||||
}
|
||||
|
||||
/// What an onboarding apply uses: the explicit choice, or with none given
|
||||
/// the recorded capacity (a re-apply keeps it unless changed). With none
|
||||
/// given and a record that exists but can't be used, it refuses rather than
|
||||
/// apply without EC2, which would tear the capacity down.
|
||||
pub(crate) fn resolve_ec2(
|
||||
choice: Option<Ec2Choice>,
|
||||
record: &UserRecord,
|
||||
) -> Result<Option<Ec2Compute>, String> {
|
||||
match (choice, record) {
|
||||
(Some(Ec2Choice::None), _) => Ok(None),
|
||||
(Some(Ec2Choice::Ec2(c)), _) => Ok(Some(c)),
|
||||
(None, UserRecord::Absent) => Ok(None),
|
||||
(None, UserRecord::Valid(r)) => Ok(Some(Ec2Compute {
|
||||
instance_type: r.instance_type.clone(),
|
||||
max_instances: r.max_instances,
|
||||
user_tag: r.user_tag.clone(),
|
||||
})),
|
||||
(None, UserRecord::Unusable(reason)) => Err(format!(
|
||||
"Your EC2 record can't be used ({reason}), so Triple-C can't keep your EC2 capacity \
|
||||
as it is; choose the EC2 capacity explicitly"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// R3 (amended): an apply with EC2 capacity would adopt (or fail on) a
|
||||
/// per-user cluster or Auto Scaling group named `stem` that already exists.
|
||||
/// Each one found must be the caller's: its `triple-c:owner` tag (the
|
||||
/// module's default tags) is `owner_id`, or, for an untagged one only, the
|
||||
/// caller's record names the cluster. A half-built capacity of the caller's
|
||||
/// own (a failed apply that wrote no record yet) is therefore re-applied;
|
||||
/// one tagged as someone else's is always refused (a record can't vouch for
|
||||
/// it), and an untagged one is refused unless the record names it.
|
||||
pub(crate) fn ec2_collision_refusal(
|
||||
stem: &str,
|
||||
holders: &[NameHolder],
|
||||
owner_id: &str,
|
||||
recorded: Option<&UserCompute>,
|
||||
) -> Result<(), String> {
|
||||
let suffix = format!(":cluster/{stem}");
|
||||
let recorded_here = recorded.is_some_and(|r| r.cluster_arn.ends_with(&suffix));
|
||||
for h in holders {
|
||||
match h.owner.as_deref() {
|
||||
Some(o) if o == owner_id => continue,
|
||||
None if recorded_here => continue,
|
||||
_ => {}
|
||||
}
|
||||
let whose = if h.owner.is_some() {
|
||||
"belongs to another onboarding"
|
||||
} else {
|
||||
"exists but isn't tagged as any Triple-C user's"
|
||||
};
|
||||
return Err(format!(
|
||||
"An {} named {stem} already {whose}. The triple-c-user value is probably wrong: use \
|
||||
the value an admin set as your triple-c-user sign-in tag",
|
||||
h.kind
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Everything a self-onboarding apply needs settled before it starts: the
|
||||
/// caller's record, the capacity ([`resolve_ec2`]), the module variables
|
||||
/// and, with EC2 capacity, the name check ([`ec2_collision_refusal`]).
|
||||
/// Check-then-apply leaves a narrow race (someone else creating the same
|
||||
/// names in between); the module would then fail on create rather than
|
||||
/// adopt. Anyone may start this: there is no tier check in the app, and AWS
|
||||
/// refuses (mapped to `AccessDenied`) what the caller isn't permitted.
|
||||
pub(crate) async fn prepare_onboarding<A: OnboardApi>(
|
||||
api: &A,
|
||||
env: &str,
|
||||
region: &str,
|
||||
account: &str,
|
||||
owner_id: &str,
|
||||
choice: Option<Ec2Choice>,
|
||||
) -> Result<Value, AwsError> {
|
||||
let okey = owner_key(region, owner_id);
|
||||
let read = api.parameter(&user_record_parameter_name(env, &okey)).await?;
|
||||
let record = user_record_from(read, env, account, region);
|
||||
let compute = resolve_ec2(choice, &record).map_err(other)?;
|
||||
let vars = onboarding_vars(env, region, owner_id, compute.as_ref()).map_err(other)?;
|
||||
if let Some(c) = &compute {
|
||||
// The caller's own triple-c-user tag can't be read (session tags
|
||||
// aren't visible to GetCallerIdentity); a wrong value fails later
|
||||
// as access denied.
|
||||
let stem = user_ec2_stem(env, &c.user_tag);
|
||||
let holders = ec2_name_holders(api, &stem).await?;
|
||||
ec2_collision_refusal(&stem, &holders, owner_id, record.valid()).map_err(other)?;
|
||||
}
|
||||
Ok(vars)
|
||||
}
|
||||
|
||||
/// The bundle's version of the check, with what a workspace user can read
|
||||
/// (the cluster, via `ecs:Describe*`, and the record; not the Auto Scaling
|
||||
/// group): a warning to show, never a refusal. The admin runs the full
|
||||
/// check from the README.
|
||||
pub(crate) async fn bundle_ec2_warning<A: OnboardApi>(
|
||||
api: &A,
|
||||
env: &str,
|
||||
region: &str,
|
||||
account: &str,
|
||||
owner_id: &str,
|
||||
compute: &Ec2Compute,
|
||||
) -> Option<String> {
|
||||
let stem = user_ec2_stem(env, &compute.user_tag);
|
||||
let checked = async {
|
||||
let holder = cluster_holder(api, &stem).await?;
|
||||
let read = api
|
||||
.parameter(&user_record_parameter_name(env, &owner_key(region, owner_id)))
|
||||
.await?;
|
||||
let record = user_record_from(read, env, account, region);
|
||||
Ok::<_, AwsError>(
|
||||
ec2_collision_refusal(&stem, &holder.into_iter().collect::<Vec<_>>(), owner_id, record.valid())
|
||||
.err(),
|
||||
)
|
||||
};
|
||||
match checked.await {
|
||||
Ok(warning) => warning,
|
||||
Err(_) => Some(format!(
|
||||
"Triple-C couldn't check whether the EC2 name {stem} is already in use; the README has \
|
||||
the admin check it"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// `AlreadyRunning` is told apart before it becomes an `AwsError`.
|
||||
pub(crate) fn start_error(e: StartError, label: &str) -> AwsError {
|
||||
match e {
|
||||
@@ -415,7 +552,54 @@ const README: &str = "README.txt";
|
||||
const BACKEND: &str = "backend.hcl";
|
||||
const TFVARS: &str = "terraform.tfvars.json";
|
||||
|
||||
fn bundle_readme(d: Deployment, account: &str, region: &str, bucket: &str, key: &str) -> String {
|
||||
/// The onboarding bundle's EC2 step: before applying with EC2 capacity, the
|
||||
/// admin checks that the per-user names are free or already this user's
|
||||
/// (the module can't: a name taken by someone else's onboarding would be
|
||||
/// adopted, and a collision only fails at create). Ownership is the
|
||||
/// resources' `triple-c:owner` tag, or failing that the user's record, as
|
||||
/// in [`ec2_collision_refusal`]. `user_tag` is the bundle's `ec2_user_tag`
|
||||
/// when set and valid; otherwise the commands show `<tag>` for the admin to
|
||||
/// fill in.
|
||||
fn ec2_collision_step(
|
||||
env: &str,
|
||||
owner_key: &str,
|
||||
owner_id: &str,
|
||||
region: &str,
|
||||
user_tag: Option<&str>,
|
||||
) -> String {
|
||||
let stem = user_ec2_stem(env, user_tag.unwrap_or("<tag>"));
|
||||
let record = user_record_parameter_name(env, owner_key);
|
||||
format!(
|
||||
"EC2 capacity (only if ec2_instance_type is set)
|
||||
------------------------------------------------
|
||||
ec2_user_tag must be the value of the user's triple-c-user principal tag.
|
||||
BEFORE tofu apply, check that the per-user EC2 names are not already in use
|
||||
by another onboarding. Run:
|
||||
|
||||
aws ecs describe-clusters --region {region} --clusters {stem} --include TAGS --query 'clusters[?status!=`INACTIVE`].[clusterName,tags[?key==`triple-c:owner`].value|[0]]' --output text
|
||||
aws autoscaling describe-auto-scaling-groups --region {region} --auto-scaling-group-names {stem} --query 'AutoScalingGroups[].[AutoScalingGroupName,Tags[?Key==`triple-c:owner`].Value|[0]]' --output text
|
||||
aws ssm get-parameter --region {region} --name {record} --query Parameter.Value --output text
|
||||
|
||||
If either of the first two prints {stem}, apply ONLY if the owner printed
|
||||
next to it is this user's owner_id ({owner_id}) or the third (this user's
|
||||
record) succeeds and its cluster_arn ends in cluster/{stem}; otherwise the
|
||||
name belongs to someone else's onboarding (or to nobody's): stop and
|
||||
choose the correct ec2_user_tag. (The app runs the same check before
|
||||
applying; run the check right before the apply, since nothing holds the
|
||||
name in between.)
|
||||
|
||||
"
|
||||
)
|
||||
}
|
||||
|
||||
fn bundle_readme(
|
||||
d: Deployment,
|
||||
account: &str,
|
||||
region: &str,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
vars: &Value,
|
||||
) -> String {
|
||||
let what = match d {
|
||||
Deployment::Environment(env) => format!(
|
||||
"the Triple-C environment \"{env}\" (ECS cluster, security group, KMS key, log \
|
||||
@@ -442,6 +626,16 @@ fn bundle_readme(d: Deployment, account: &str, region: &str, bucket: &str, key:
|
||||
format!(r#"{{"LocationConstraint":"{region}",{tags}}}"#)
|
||||
};
|
||||
let policy = state_bucket_policy(bucket);
|
||||
let ec2_step = match d {
|
||||
Deployment::User { env, owner_key } => {
|
||||
let tag = vars["ec2_user_tag"]
|
||||
.as_str()
|
||||
.filter(|t| validate_ec2_user_tag(t).is_ok());
|
||||
let owner_id = vars["owner_id"].as_str().unwrap_or("<owner_id>");
|
||||
ec2_collision_step(env, owner_key, owner_id, region, tag)
|
||||
}
|
||||
_ => String::new(),
|
||||
};
|
||||
let credentials = match d {
|
||||
Deployment::Boundary => format!(
|
||||
"- Credentials of an account administrator for account {account} WITHOUT
|
||||
@@ -489,7 +683,7 @@ AWS CLI v2 recent enough that create-bucket accepts Tags, e.g. 2.39):
|
||||
aws s3api put-public-access-block --bucket {bucket} --region {region} --expected-bucket-owner {account} --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
|
||||
aws s3api put-bucket-policy --bucket {bucket} --region {region} --expected-bucket-owner {account} --policy '{policy}'
|
||||
|
||||
2. Deploy
|
||||
{ec2_step}2. Deploy
|
||||
---------
|
||||
From this folder:
|
||||
|
||||
@@ -529,7 +723,7 @@ fn bundle_files(
|
||||
let json = serde_json::to_string_pretty(vars)
|
||||
.map_err(|e| format!("Could not encode the module variables: {e}"))?;
|
||||
files.push((TFVARS.into(), json + "\n"));
|
||||
files.push((README.into(), bundle_readme(d, account, ®ion, &bucket, &key)));
|
||||
files.push((README.into(), bundle_readme(d, account, ®ion, &bucket, &key, vars)));
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
@@ -1317,12 +1511,15 @@ pub async fn cancel_aws_deployment(
|
||||
}
|
||||
|
||||
/// Onboard the caller to `environment`, as the owner their own
|
||||
/// `aws:userid` names.
|
||||
/// `aws:userid` names, with the EC2 capacity `ec2` asks for (omitted: keep
|
||||
/// what their user record holds, none without one). With EC2 capacity the
|
||||
/// per-user names must be free or already theirs (R3).
|
||||
#[tauri::command]
|
||||
pub async fn onboard_aws_user(
|
||||
connection_id: String,
|
||||
region: String,
|
||||
environment: String,
|
||||
ec2: Option<Ec2Choice>,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<String, AwsError> {
|
||||
@@ -1331,7 +1528,15 @@ pub async fn onboard_aws_user(
|
||||
let owner = validate_owner_id(&ctx.who.user_id).map_err(other)?;
|
||||
let found = detect(&ctx).await?;
|
||||
onboard_refusal(&found, &env).map_err(other)?;
|
||||
let vars = onboarding_vars(&env, &ctx.region, &owner);
|
||||
let vars = prepare_onboarding(
|
||||
&SdkOnboardApi::new(&ctx.cfg),
|
||||
&env,
|
||||
&ctx.region,
|
||||
&ctx.who.account,
|
||||
&owner,
|
||||
ec2,
|
||||
)
|
||||
.await?;
|
||||
let key = DeploymentKey::User {
|
||||
owner_key: owner_key(&ctx.region, &owner),
|
||||
env,
|
||||
@@ -1339,6 +1544,22 @@ pub async fn onboard_aws_user(
|
||||
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
|
||||
}
|
||||
|
||||
/// The EC2 instance types onboarding offers (the app's built-in table) and
|
||||
/// the one it offers first.
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct Ec2InstanceTable {
|
||||
pub default_type: &'static str,
|
||||
pub types: Vec<Ec2InstanceType>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn list_ec2_instance_types() -> Ec2InstanceTable {
|
||||
Ec2InstanceTable {
|
||||
default_type: DEFAULT_EC2_INSTANCE_TYPE,
|
||||
types: ec2_instance_types(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Deploy (or re-apply) the account's permissions boundary. Refused when
|
||||
/// the boundary exists but this region's bucket has no state for it.
|
||||
#[tauri::command]
|
||||
@@ -1378,8 +1599,10 @@ async fn pick_folder(window: &tauri::Window) -> Option<PathBuf> {
|
||||
/// Write an admin bundle (D5) for `kind` into a folder the user picks:
|
||||
/// `environment` names the environment (environment and onboarding),
|
||||
/// `owner_id` the user to onboard (an `aws:userid`), `network` an
|
||||
/// environment's network (public when omitted). `None` when the dialog was
|
||||
/// dismissed; otherwise the folder written.
|
||||
/// environment's network (public when omitted), `ec2` an onboarding's EC2
|
||||
/// capacity (none when omitted; the bundle's README has the admin check
|
||||
/// its names first). `None` when the dialog was dismissed; otherwise the
|
||||
/// folder written and any warning.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[tauri::command]
|
||||
pub async fn save_aws_admin_bundle(
|
||||
@@ -1390,9 +1613,10 @@ pub async fn save_aws_admin_bundle(
|
||||
owner_id: Option<String>,
|
||||
network: Option<NetworkChoice>,
|
||||
max_workspace_hours: Option<u32>,
|
||||
ec2: Option<Ec2Compute>,
|
||||
window: tauri::Window,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Option<String>, AwsError> {
|
||||
) -> Result<Option<SavedBundle>, AwsError> {
|
||||
let need_env = || {
|
||||
environment
|
||||
.as_deref()
|
||||
@@ -1420,7 +1644,7 @@ pub async fn save_aws_admin_bundle(
|
||||
.map(str::trim)
|
||||
.ok_or_else(|| other("The user's owner id (aws:userid) is required"))
|
||||
.and_then(|o| validate_owner_id(o).map_err(other))?;
|
||||
let vars = onboarding_vars(&env, ®ion_ok, &owner);
|
||||
let vars = onboarding_vars(&env, ®ion_ok, &owner, ec2.as_ref()).map_err(other)?;
|
||||
(
|
||||
DeploymentKey::User {
|
||||
owner_key: owner_key(®ion_ok, &owner),
|
||||
@@ -1432,11 +1656,29 @@ pub async fn save_aws_admin_bundle(
|
||||
ModuleKind::PermissionsBoundary => (DeploymentKey::Boundary, boundary_vars(®ion_ok)),
|
||||
};
|
||||
let ctx = context(&state, &connection_id, ®ion).await?;
|
||||
let warning = match (&key, &ec2, &owner_id) {
|
||||
(DeploymentKey::User { env, .. }, Some(c), Some(owner)) => {
|
||||
let api = SdkOnboardApi::new(&ctx.cfg);
|
||||
bundle_ec2_warning(&api, env, &ctx.region, &ctx.who.account, owner.trim(), c).await
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let Some(dir) = pick_folder(&window).await else {
|
||||
return Ok(None);
|
||||
};
|
||||
write_bundle(&dir, key.deployment(), &ctx.who.account, &ctx.region, &vars).map_err(other)?;
|
||||
Ok(Some(dir.to_string_lossy().into_owned()))
|
||||
Ok(Some(SavedBundle {
|
||||
folder: dir.to_string_lossy().into_owned(),
|
||||
warning,
|
||||
}))
|
||||
}
|
||||
|
||||
/// A bundle written: its folder and, for an onboarding with EC2 capacity,
|
||||
/// a warning when its EC2 name looks taken ([`bundle_ec2_warning`]).
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct SavedBundle {
|
||||
pub folder: String,
|
||||
pub warning: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -6,7 +6,7 @@ use serde_json::json;
|
||||
use super::*;
|
||||
use crate::aws::tofu::Workdir;
|
||||
use crate::aws::environments::{
|
||||
EnvironmentState, Knowable, LockInfo, OnboardingState,
|
||||
EnvironmentState, Knowable, LockInfo, NamedResource, OnboardingState, Param, UserCompute,
|
||||
};
|
||||
use crate::aws::tofu_modules::{
|
||||
environment_vars, module_files, onboarding_vars, owner_key, state_key, ModuleKind,
|
||||
@@ -195,7 +195,7 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let ok = owner_key(REGION, USER_ID);
|
||||
let d = Deployment::User { env: "dev", owner_key: &ok };
|
||||
let vars = onboarding_vars("dev", REGION, USER_ID);
|
||||
let vars = onboarding_vars("dev", REGION, USER_ID, None).unwrap();
|
||||
let written = write_bundle(dir.path(), d, ACCOUNT, REGION, &vars).unwrap();
|
||||
|
||||
for (name, text) in module_files(ModuleKind::UserOnboard) {
|
||||
@@ -253,6 +253,45 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
|
||||
}
|
||||
assert!(!readme.contains("put-bucket-tagging"), "a later tag can't satisfy the create");
|
||||
|
||||
// The EC2 name-collision check (the app does it too, Task 4): the
|
||||
// exact per-user names, and this user's record to compare against.
|
||||
let record = format!("/triple-c/users/dev/{ok}");
|
||||
for needle in [
|
||||
"BEFORE tofu apply",
|
||||
&format!("aws ecs describe-clusters --region {REGION} --clusters triple-c-dev--u-<tag>"),
|
||||
&format!(
|
||||
"aws autoscaling describe-auto-scaling-groups --region {REGION} \
|
||||
--auto-scaling-group-names triple-c-dev--u-<tag>"
|
||||
),
|
||||
&format!("aws ssm get-parameter --region {REGION} --name {record}"),
|
||||
"cluster/triple-c-dev--u-<tag>",
|
||||
"--include TAGS",
|
||||
"triple-c:owner",
|
||||
&format!("owner_id ({USER_ID})"),
|
||||
] {
|
||||
assert!(readme.contains(needle), "README lacks {needle:?}:\n{readme}");
|
||||
}
|
||||
// With a tag in the variables, the commands name it.
|
||||
let mut tagged = vars.clone();
|
||||
tagged["ec2_instance_type"] = "m7i.large".into();
|
||||
tagged["ec2_user_tag"] = "alice".into();
|
||||
let other = tempfile::tempdir().unwrap();
|
||||
write_bundle(other.path(), d, ACCOUNT, REGION, &tagged).unwrap();
|
||||
let readme_tagged = fs::read_to_string(other.path().join("README.txt")).unwrap();
|
||||
assert!(readme_tagged.contains("--clusters triple-c-dev--u-alice "), "{readme_tagged}");
|
||||
assert!(readme_tagged.contains("--auto-scaling-group-names triple-c-dev--u-alice "));
|
||||
assert!(!readme_tagged.contains("<tag>"));
|
||||
// The check comes before the apply it guards.
|
||||
let check = readme.find("BEFORE tofu apply").unwrap();
|
||||
let apply = readme.find(" tofu apply").unwrap();
|
||||
assert!(check < apply, "{readme}");
|
||||
// Not in the other bundles.
|
||||
let env_dir = tempfile::tempdir().unwrap();
|
||||
let env_vars = environment_vars("dev", REGION, &NetworkChoice::Public, 0).unwrap();
|
||||
write_bundle(env_dir.path(), Deployment::Environment("dev"), ACCOUNT, REGION, &env_vars).unwrap();
|
||||
let env_readme = fs::read_to_string(env_dir.path().join("README.txt")).unwrap();
|
||||
assert!(!env_readme.contains("EC2 capacity"), "{env_readme}");
|
||||
|
||||
// Writing again over our own files is fine...
|
||||
write_bundle(dir.path(), d, ACCOUNT, REGION, &vars).unwrap();
|
||||
// ...but an unrelated file (or folder) means the wrong folder.
|
||||
@@ -507,12 +546,12 @@ fn destroy_is_refused_while_users_are_onboarded() {
|
||||
fn only_your_own_stale_onboarding_lock_can_be_removed() {
|
||||
use EnvironmentAction::*;
|
||||
let mut stale = env("dev", EnvironmentState::Ready, vec![Join]);
|
||||
stale.onboarding = Some(OnboardingState::Incomplete { lock: Some(lock(9000, false)) });
|
||||
stale.onboarding = Some(OnboardingState::Incomplete { lock: Some(lock(9000, false)), ec2: None });
|
||||
stale.onboarding_actions = vec![Unlock];
|
||||
let mut live = env("live", EnvironmentState::Ready, vec![Join]);
|
||||
live.onboarding = Some(OnboardingState::InProgress { lock: Some(lock(10, false)) });
|
||||
let mut none = env("none", EnvironmentState::Ready, vec![Join]);
|
||||
none.onboarding = Some(OnboardingState::Incomplete { lock: None });
|
||||
none.onboarding = Some(OnboardingState::Incomplete { lock: None, ec2: None });
|
||||
let s = scan(Knowable::Yes, vec![stale, live, none]);
|
||||
let mine = owner_key(REGION, USER_ID);
|
||||
assert!(onboarding_unlock_refusal(&s, "dev", &mine).is_ok());
|
||||
@@ -631,3 +670,319 @@ fn ending_a_launch_never_clears_a_following_slot() {
|
||||
assert_eq!(slots.begin_launch(dir).unwrap_err(), SlotBusy::Following);
|
||||
slots.end_launch(Path::new("/w/env/none")); // nothing there: no-op
|
||||
}
|
||||
|
||||
fn recorded(env: &str, tag: &str, max: u32) -> UserCompute {
|
||||
let stem = crate::aws::tofu_modules::user_ec2_stem(env, tag);
|
||||
UserCompute {
|
||||
instance_type: "m7g.xlarge".into(),
|
||||
max_instances: max,
|
||||
user_tag: tag.into(),
|
||||
cluster_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/{stem}"),
|
||||
capacity_provider: stem,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reapply_keeps_the_recorded_compute_unless_changed() {
|
||||
let rec = UserRecord::Valid(recorded("dev", "alice", 3));
|
||||
let kept = Ec2Compute {
|
||||
instance_type: "m7g.xlarge".into(),
|
||||
max_instances: 3,
|
||||
user_tag: "alice".into(),
|
||||
};
|
||||
// Omitted: keep what the record says (or none without one).
|
||||
assert_eq!(resolve_ec2(None, &rec), Ok(Some(kept.clone())));
|
||||
assert_eq!(resolve_ec2(None, &UserRecord::Absent), Ok(None));
|
||||
// Explicitly none: the re-apply removes it.
|
||||
assert_eq!(resolve_ec2(Some(Ec2Choice::None), &rec), Ok(None));
|
||||
// A new choice wins.
|
||||
let new = Ec2Compute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 1,
|
||||
user_tag: "alice".into(),
|
||||
};
|
||||
assert_eq!(
|
||||
resolve_ec2(Some(Ec2Choice::Ec2(new.clone())), &rec),
|
||||
Ok(Some(new.clone()))
|
||||
);
|
||||
// A record that exists but can't be used is never read as "none" (that
|
||||
// would tear the capacity down); an explicit choice still goes.
|
||||
let bad = UserRecord::Unusable("schema is not 1".into());
|
||||
let e = resolve_ec2(None, &bad).unwrap_err();
|
||||
assert!(e.contains("schema is not 1") && e.contains("explicitly"), "{e}");
|
||||
assert_eq!(resolve_ec2(Some(Ec2Choice::None), &bad), Ok(None));
|
||||
assert_eq!(resolve_ec2(Some(Ec2Choice::Ec2(new.clone())), &bad), Ok(Some(new.clone())));
|
||||
// The kept values reach the module's variables unchanged.
|
||||
let vars = onboarding_vars("dev", REGION, USER_ID, resolve_ec2(None, &rec).unwrap().as_ref()).unwrap();
|
||||
assert_eq!(vars["ec2_instance_type"], "m7g.xlarge");
|
||||
assert_eq!(vars["ec2_max_instances"], 3);
|
||||
assert_eq!(vars["ec2_user_tag"], "alice");
|
||||
|
||||
// The UI's JSON for each choice.
|
||||
let none: Ec2Choice = serde_json::from_value(json!({"kind": "none"})).unwrap();
|
||||
assert_eq!(none, Ec2Choice::None);
|
||||
let ec2: Ec2Choice = serde_json::from_value(json!({
|
||||
"kind": "ec2", "instance_type": "m7i.large", "max_instances": 1, "user_tag": "alice"
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(ec2, Ec2Choice::Ec2(new));
|
||||
}
|
||||
|
||||
fn held(kind: &'static str, owner: Option<&str>) -> NameHolder {
|
||||
NameHolder {
|
||||
kind,
|
||||
owner: owner.map(String::from),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ec2_names_held_by_another_owner_are_refused() {
|
||||
let stem = "triple-c-dev--u-alice";
|
||||
let me = USER_ID;
|
||||
// Free names: fine, with or without a record.
|
||||
assert!(ec2_collision_refusal(stem, &[], me, None).is_ok());
|
||||
assert!(ec2_collision_refusal(stem, &[], me, Some(&recorded("dev", "bob", 1))).is_ok());
|
||||
let cluster = |o| held("ECS cluster", o);
|
||||
let group = |o| held("Auto Scaling group", o);
|
||||
// Tagged as mine: a half-built capacity (no record yet) is re-applied.
|
||||
assert!(ec2_collision_refusal(stem, &[cluster(Some(me)), group(Some(me))], me, None).is_ok());
|
||||
// Another owner's, or untagged: refused, naming the resource.
|
||||
let e = ec2_collision_refusal(stem, &[cluster(Some("AROAOTHER:bob"))], me, None).unwrap_err();
|
||||
assert!(e.contains("ECS cluster") && e.contains(stem) && e.contains("another onboarding"), "{e}");
|
||||
assert!(e.contains("triple-c-user"), "{e}");
|
||||
let e = ec2_collision_refusal(stem, &[cluster(Some(me)), group(None)], me, None).unwrap_err();
|
||||
assert!(e.contains("Auto Scaling group") && e.contains("isn't tagged"), "{e}");
|
||||
// The record naming the cluster is the fallback for an untagged one
|
||||
// only: one tagged as someone else's is refused even then.
|
||||
let mine = recorded("dev", "alice", 2);
|
||||
assert!(ec2_collision_refusal(stem, &[cluster(None), group(None)], me, Some(&mine)).is_ok());
|
||||
assert!(ec2_collision_refusal(stem, &[cluster(None), group(Some(me))], me, Some(&mine)).is_ok());
|
||||
let e = ec2_collision_refusal(stem, &[cluster(None), group(Some("x"))], me, Some(&mine)).unwrap_err();
|
||||
assert!(e.contains("Auto Scaling group") && e.contains("another onboarding"), "{e}");
|
||||
// A record naming another cluster doesn't help.
|
||||
assert!(ec2_collision_refusal(stem, &[cluster(None)], me, Some(&recorded("dev", "bob", 1))).is_err());
|
||||
// A cluster that merely ends in the same text is not the same cluster.
|
||||
let mut lookalike = recorded("dev", "alice", 1);
|
||||
lookalike.cluster_arn = format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/x{stem}");
|
||||
assert!(ec2_collision_refusal(stem, &[cluster(None)], me, Some(&lookalike)).is_err());
|
||||
}
|
||||
|
||||
/// An `OnboardApi` answering from fixed data, recording what it was asked.
|
||||
#[derive(Default)]
|
||||
struct FakeOnboard {
|
||||
record: Option<String>,
|
||||
record_denied: bool,
|
||||
cluster: Option<NamedResource>,
|
||||
group: Option<NamedResource>,
|
||||
group_denied: bool,
|
||||
calls: std::sync::Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
impl FakeOnboard {
|
||||
fn calls(&self) -> Vec<String> {
|
||||
self.calls.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl OnboardApi for FakeOnboard {
|
||||
async fn parameter(&self, name: &str) -> Result<Param, AwsError> {
|
||||
self.calls.lock().unwrap().push(format!("parameter {name}"));
|
||||
Ok(match (&self.record, self.record_denied) {
|
||||
(_, true) => Param::Denied,
|
||||
(Some(v), _) => Param::Value(v.clone()),
|
||||
(None, _) => Param::Missing,
|
||||
})
|
||||
}
|
||||
async fn cluster(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
|
||||
self.calls.lock().unwrap().push(format!("cluster {name}"));
|
||||
Ok(self.cluster.clone())
|
||||
}
|
||||
async fn group(&self, name: &str) -> Result<Option<NamedResource>, AwsError> {
|
||||
self.calls.lock().unwrap().push(format!("group {name}"));
|
||||
if self.group_denied {
|
||||
return Err(AwsError::AccessDenied {
|
||||
action: "autoscaling:DescribeAutoScalingGroups".into(),
|
||||
message: "denied".into(),
|
||||
});
|
||||
}
|
||||
Ok(self.group.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn named(status: Option<&str>, owner: Option<&str>) -> NamedResource {
|
||||
NamedResource {
|
||||
status: status.map(String::from),
|
||||
tags: owner
|
||||
.map(|o| vec![("triple-c:owner".to_string(), o.to_string())])
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn record_json(env: &str, tag: &str) -> String {
|
||||
let r = recorded(env, tag, 2);
|
||||
json!({
|
||||
"schema": 1, "instance_type": r.instance_type, "cluster_arn": r.cluster_arn,
|
||||
"capacity_provider": r.capacity_provider, "user_tag": r.user_tag, "max_instances": 2,
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn prepare(api: &FakeOnboard, choice: Option<Ec2Choice>) -> Result<Value, AwsError> {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(prepare_onboarding(api, "dev", REGION, ACCOUNT, USER_ID, choice))
|
||||
}
|
||||
|
||||
fn alice() -> Option<Ec2Choice> {
|
||||
Some(Ec2Choice::Ec2(Ec2Compute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 1,
|
||||
user_tag: "alice".into(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn onboarding_checks_the_ec2_names_before_it_applies() {
|
||||
let stem = "triple-c-dev--u-alice";
|
||||
// Fargate only: no name lookups at all.
|
||||
let api = FakeOnboard::default();
|
||||
let vars = prepare(&api, Some(Ec2Choice::None)).unwrap();
|
||||
assert_eq!(vars["ec2_instance_type"], "");
|
||||
assert!(api.calls().iter().all(|c| c.starts_with("parameter ")), "{:?}", api.calls());
|
||||
|
||||
// Free names: the vars come back.
|
||||
let api = FakeOnboard::default();
|
||||
let vars = prepare(&api, alice()).unwrap();
|
||||
assert_eq!(vars["ec2_user_tag"], "alice");
|
||||
assert!(api.calls().contains(&format!("cluster {stem}")));
|
||||
assert!(api.calls().contains(&format!("group {stem}")));
|
||||
|
||||
// Another owner's cluster: refused, so nothing is launched.
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some("ACTIVE"), Some("AROAOTHER:bob"))),
|
||||
..Default::default()
|
||||
};
|
||||
let e = prepare(&api, alice()).unwrap_err();
|
||||
assert!(format!("{e:?}").contains("another onboarding"), "{e:?}");
|
||||
|
||||
// My own half-built cluster and group (a failed apply, no record yet).
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some("PROVISIONING"), Some(USER_ID))),
|
||||
group: Some(named(None, Some(USER_ID))),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_ok());
|
||||
|
||||
// Untagged: refused unless my record names it.
|
||||
let untagged = || FakeOnboard {
|
||||
cluster: Some(named(Some("ACTIVE"), None)),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&untagged(), alice()).is_err());
|
||||
let api = FakeOnboard {
|
||||
record: Some(record_json("dev", "alice")),
|
||||
..untagged()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_ok());
|
||||
|
||||
// A deleted (INACTIVE) cluster doesn't hold the name; every other
|
||||
// status does.
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some("INACTIVE"), None)),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_ok());
|
||||
for status in ["ACTIVE", "PROVISIONING", "DEPROVISIONING", "FAILED"] {
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some(status), Some("AROAOTHER:bob"))),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_err(), "{status}");
|
||||
}
|
||||
|
||||
// An Auto Scaling group alone is enough to refuse.
|
||||
let api = FakeOnboard {
|
||||
group: Some(named(None, Some("AROAOTHER:bob"))),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_err());
|
||||
|
||||
// A denied group lookup is an error, never "free".
|
||||
let api = FakeOnboard {
|
||||
group_denied: true,
|
||||
..Default::default()
|
||||
};
|
||||
assert!(prepare(&api, alice()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capacity_kept_from_the_record_is_checked_too() {
|
||||
let stem = "triple-c-dev--u-alice";
|
||||
// Omitted choice + a record: the record's capacity, and its names are
|
||||
// checked (here another owner's group appeared under it).
|
||||
let api = FakeOnboard {
|
||||
record: Some(record_json("dev", "alice")),
|
||||
cluster: Some(named(Some("ACTIVE"), Some(USER_ID))),
|
||||
group: Some(named(None, Some(USER_ID))),
|
||||
..Default::default()
|
||||
};
|
||||
let vars = prepare(&api, None).unwrap();
|
||||
assert_eq!(vars["ec2_instance_type"], "m7g.xlarge");
|
||||
assert_eq!(vars["ec2_max_instances"], 2);
|
||||
assert!(api.calls().contains(&format!("group {stem}")));
|
||||
|
||||
// A record that can't be used (unreadable or invalid) and no choice:
|
||||
// refused before any name lookup or launch.
|
||||
for api in [
|
||||
FakeOnboard {
|
||||
record_denied: true,
|
||||
..Default::default()
|
||||
},
|
||||
FakeOnboard {
|
||||
record: Some("{\"schema\": 9}".into()),
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
let e = prepare(&api, None).unwrap_err();
|
||||
assert!(format!("{e:?}").contains("explicitly"), "{e:?}");
|
||||
assert!(api.calls().iter().all(|c| c.starts_with("parameter ")));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bundle_warns_when_its_cluster_name_looks_taken() {
|
||||
let run = |api: &FakeOnboard| {
|
||||
let c = Ec2Compute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 1,
|
||||
user_tag: "alice".into(),
|
||||
};
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(bundle_ec2_warning(api, "dev", REGION, ACCOUNT, USER_ID, &c))
|
||||
};
|
||||
assert_eq!(run(&FakeOnboard::default()), None);
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some("ACTIVE"), Some("AROAOTHER:bob"))),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(run(&api).unwrap().contains("another onboarding"));
|
||||
// Only the cluster half: a workspace user can't read Auto Scaling.
|
||||
assert!(!api.calls().iter().any(|c| c.starts_with("group ")));
|
||||
let api = FakeOnboard {
|
||||
cluster: Some(named(Some("ACTIVE"), Some(USER_ID))),
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(run(&api), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_ui_gets_the_apps_instance_table() {
|
||||
let table = list_ec2_instance_types();
|
||||
assert_eq!(table.types, crate::models::project::ec2_instance_types());
|
||||
assert_eq!(table.types.len(), 30);
|
||||
assert_eq!(table.default_type, "m7i.large");
|
||||
assert!(table.types.iter().any(|t| t.instance_type == table.default_type));
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use crate::aws::permissions::{check_permissions, PermissionReport, SdkPermission
|
||||
use crate::aws::policies::{policy_files, Tier};
|
||||
use crate::aws::pricing::{self, HourlyEstimate, RatesCache, SdkPricingApi};
|
||||
use crate::aws::{caller_identity, sdk_config, sdk_config_for_keys, AwsError, CallerIdentity};
|
||||
use crate::models::project::{ec2_instance_spec, LaunchType};
|
||||
use crate::models::{
|
||||
validate_connection_name, validate_profile_name, validate_supported_region, AppSettings, AwsAuth,
|
||||
AwsWorkspaceConfig, HostConnection, HostKind,
|
||||
@@ -322,18 +323,33 @@ fn pricing_cache() -> Option<&'static RatesCache> {
|
||||
/// error:** a bad region, a missing connection, a denied or slow call (8 s
|
||||
/// bound) or an unreadable price list is `None`, shown as "Estimate
|
||||
/// unavailable". Rates are cached a day per region.
|
||||
///
|
||||
/// An EC2 project (E10) is priced as `instance_type` (the user's, from their
|
||||
/// onboarding record, which the UI already holds): its on-demand hourly
|
||||
/// price, the same cache and back-off rules. No instance type, or one not in
|
||||
/// the app's table, is `None`.
|
||||
#[tauri::command]
|
||||
pub async fn aws_cost_estimate(
|
||||
connection_id: String,
|
||||
region: String,
|
||||
config: AwsWorkspaceConfig,
|
||||
instance_type: Option<String>,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Option<HourlyEstimate>, String> {
|
||||
let Some(cache) = pricing_cache() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let settings = state.settings_store.get();
|
||||
Ok(cost_estimate(cache, &connection_id, ®ion, &config, &settings, SystemTime::now()).await)
|
||||
Ok(cost_estimate(
|
||||
cache,
|
||||
&connection_id,
|
||||
®ion,
|
||||
&config,
|
||||
instance_type.as_deref(),
|
||||
&settings,
|
||||
SystemTime::now(),
|
||||
)
|
||||
.await)
|
||||
}
|
||||
|
||||
async fn cost_estimate(
|
||||
@@ -341,24 +357,41 @@ async fn cost_estimate(
|
||||
connection_id: &str,
|
||||
region: &str,
|
||||
config: &AwsWorkspaceConfig,
|
||||
instance_type: Option<&str>,
|
||||
settings: &AppSettings,
|
||||
now: SystemTime,
|
||||
) -> Option<HourlyEstimate> {
|
||||
let region = validate_supported_region(region).ok()?;
|
||||
// Only now touch the connection (a keys connection reads the keychain),
|
||||
// and inside the fetch's 8 s bound: reading a profile's credentials can
|
||||
// hang as well (an SSO or process provider).
|
||||
let api = || async {
|
||||
let conn = aws_conn(connection_id, settings).ok()?;
|
||||
let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?;
|
||||
Some(SdkPricingApi::new(&cfg))
|
||||
};
|
||||
if config.launch_type == LaunchType::Ec2 {
|
||||
let instance_type = instance_type.filter(|t| ec2_instance_spec(t).is_some())?;
|
||||
let instance = async {
|
||||
match cache.read_ec2(®ion, instance_type, now) {
|
||||
Some((price, true)) => Some(price),
|
||||
_ => {
|
||||
cache
|
||||
.ec2_price_with::<SdkPricingApi, _>(api(), ®ion, instance_type, now)
|
||||
.await
|
||||
}
|
||||
}
|
||||
};
|
||||
// The root volume's price is best effort: without it the estimate
|
||||
// is the instance's alone (and says so).
|
||||
let gp3 = cache.gp3_price_with::<SdkPricingApi, _>(api(), ®ion, now);
|
||||
let (price, gp3) = tokio::join!(instance, gp3);
|
||||
return Some(pricing::estimate_ec2(config, price?, gp3));
|
||||
}
|
||||
let rates = match cache.read(®ion, now) {
|
||||
Some((rates, true)) => Some(rates),
|
||||
_ => {
|
||||
// Only now touch the connection (a keys connection reads the
|
||||
// keychain); without one, a stale cache still answers.
|
||||
// Built inside the fetch's 8 s bound: reading a profile's
|
||||
// credentials can hang as well (an SSO or process provider).
|
||||
let api = async {
|
||||
let conn = aws_conn(connection_id, settings).ok()?;
|
||||
let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?;
|
||||
Some(SdkPricingApi::new(&cfg))
|
||||
};
|
||||
cache.rates_with::<SdkPricingApi, _>(api, ®ion, now).await
|
||||
}
|
||||
// Without a connection, a stale cache still answers.
|
||||
_ => cache.rates_with::<SdkPricingApi, _>(api(), ®ion, now).await,
|
||||
}?;
|
||||
Some(pricing::estimate(config, &rates.for_arch(config.arch)?))
|
||||
}
|
||||
@@ -604,7 +637,15 @@ mod tests {
|
||||
fn the_policy_document_is_every_file_of_the_tier() {
|
||||
let admin = aws_policy_document(Tier::EnvironmentAdmin);
|
||||
let names: Vec<&str> = admin.iter().map(|f| f.name.as_str()).collect();
|
||||
assert_eq!(names, ["environment-admin.json", "environment-admin-network.json"]);
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
"environment-admin.json",
|
||||
"environment-admin-network.json",
|
||||
"environment-admin-compute.json",
|
||||
"environment-admin-scaling.json"
|
||||
]
|
||||
);
|
||||
for f in &admin {
|
||||
serde_json::from_str::<serde_json::Value>(&f.json).expect("policy JSON");
|
||||
}
|
||||
@@ -646,7 +687,7 @@ mod tests {
|
||||
spot: true,
|
||||
..Default::default()
|
||||
};
|
||||
let e = cost_estimate(&cache, "gone", "us-west-2", &config, &settings, now)
|
||||
let e = cost_estimate(&cache, "gone", "us-west-2", &config, None, &settings, now)
|
||||
.await
|
||||
.expect("estimate");
|
||||
assert!((e.usd_per_hour - (0.04 + 2.0 * 0.004)).abs() < 1e-9);
|
||||
@@ -656,7 +697,7 @@ mod tests {
|
||||
arch: crate::models::Arch::Arm64,
|
||||
..config.clone()
|
||||
};
|
||||
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &arm, &settings, now).await, None);
|
||||
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &arm, None, &settings, now).await, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -667,11 +708,46 @@ mod tests {
|
||||
let config = AwsWorkspaceConfig::default();
|
||||
let now = SystemTime::now();
|
||||
// No cache and no such connection.
|
||||
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, &settings, now).await, None);
|
||||
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, None, &settings, now).await, None);
|
||||
// A region that isn't one (it would name the cache file).
|
||||
cache.write("us-west-2", &sample_rates(), now).unwrap();
|
||||
for bad in ["../us-west-2", "", "us-west-2/x"] {
|
||||
assert_eq!(cost_estimate(&cache, "gone", bad, &config, &settings, now).await, None);
|
||||
assert_eq!(cost_estimate(&cache, "gone", bad, &config, None, &settings, now).await, None);
|
||||
}
|
||||
}
|
||||
|
||||
/// E10: an EC2 project is priced as the user's instance type, from the
|
||||
/// same cache; without a known type there is no estimate.
|
||||
#[tokio::test]
|
||||
async fn an_ec2_projects_estimate_is_its_instance_types_price() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let cache = RatesCache::new(dir.path().to_path_buf());
|
||||
let now = SystemTime::now();
|
||||
cache.write("us-west-2", &sample_rates(), now).unwrap();
|
||||
cache.write_ec2("us-west-2", "m7i.large", 0.1008, now).unwrap();
|
||||
let settings = AppSettings::default();
|
||||
let config = AwsWorkspaceConfig {
|
||||
launch_type: LaunchType::Ec2,
|
||||
cpu: 1024,
|
||||
memory_mib: 2048,
|
||||
storage: crate::models::project::StorageMode::Ephemeral,
|
||||
..Default::default()
|
||||
};
|
||||
let e = cost_estimate(&cache, "gone", "us-west-2", &config, Some("m7i.large"), &settings, now)
|
||||
.await
|
||||
.expect("estimate");
|
||||
assert!((e.usd_per_hour - 0.1008).abs() < 1e-9);
|
||||
assert!(e.ec2_instance && !e.note_spot && !e.efs_billed_separately);
|
||||
// No instance type, one not in the table, or one with no cached price
|
||||
// (and no connection to ask): no estimate — never Fargate's.
|
||||
for t in [None, Some("x9.huge"), Some("m7g.large")] {
|
||||
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, t, &settings, now).await, None, "{t:?}");
|
||||
}
|
||||
// A Fargate project ignores an instance type.
|
||||
let fargate = AwsWorkspaceConfig { launch_type: LaunchType::Fargate, ..config.clone() };
|
||||
let f = cost_estimate(&cache, "gone", "us-west-2", &fargate, Some("m7i.large"), &settings, now)
|
||||
.await
|
||||
.expect("fargate");
|
||||
assert!(!f.ec2_instance);
|
||||
}
|
||||
}
|
||||
@@ -724,6 +724,7 @@ pub async fn remove_project(
|
||||
if let Some(target) = ContainerTarget::for_project(&project) {
|
||||
state.exec_manager.close_sessions_for_container(target.id()).await;
|
||||
}
|
||||
crate::storage::terminal_store::clear(&project_id);
|
||||
let connection_exists = project
|
||||
.host_id
|
||||
.as_deref()
|
||||
@@ -1152,14 +1153,20 @@ pub async fn update_project(
|
||||
// status change or a container id landing since `stored` was read is kept
|
||||
// rather than written over.
|
||||
let mappings_changed = stored.port_mappings != project.port_mappings;
|
||||
let left_aws = stored.is_aws() && !project.is_aws();
|
||||
let saved = state
|
||||
.projects_store
|
||||
.update_restoring(project, restore_store_owned_fields)?;
|
||||
|
||||
// AWS (Q6): a connected workspace's forwards follow the saved mappings
|
||||
// now — there is no container to recreate. Nothing when not connected.
|
||||
if left_aws {
|
||||
// Moved to Docker: its workspace terminals went with the task.
|
||||
crate::storage::terminal_store::clear(&saved.id);
|
||||
}
|
||||
if saved.is_aws() && mappings_changed {
|
||||
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &saved.id).await {
|
||||
let warn = ports_warner(&app_handle, &saved.id, &saved.name);
|
||||
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &saved.id, Some(warn)).await {
|
||||
emit_ports_warning(&app_handle, &saved.id, &saved.name, &detail);
|
||||
}
|
||||
}
|
||||
@@ -1225,6 +1232,10 @@ fn validate_gateway_sidecar_update(
|
||||
let Some(problem) = crate::aws::workspace::gateway_sidecar_problem(project, gateway) else {
|
||||
return Ok(());
|
||||
};
|
||||
// Not on EC2 at all (bridge networking): refused whatever was stored.
|
||||
if problem == crate::aws::workspace::EC2_NO_SIDECAR {
|
||||
return Err(problem);
|
||||
}
|
||||
let was_on = stored.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
|
||||
if was_on && project.backend == crate::models::project::Backend::OpenAiCompatible {
|
||||
return Ok(());
|
||||
@@ -1248,6 +1259,15 @@ fn validate_aws_update(
|
||||
crate::models::validate_region(&place.region)?;
|
||||
crate::aws::tofu_modules::validate_environment_name(&place.environment)?;
|
||||
crate::models::validate_aws_workspace(cfg)?;
|
||||
// Docker access and VPN need the user's own instance (E6, E7).
|
||||
if cfg.launch_type != crate::models::LaunchType::Ec2 {
|
||||
if project.allow_docker_access {
|
||||
return Err("Docker access on AWS needs the EC2 launch type; turn it off first".into());
|
||||
}
|
||||
if project.vpn_support_enabled {
|
||||
return Err("VPN support on AWS needs the EC2 launch type; turn it off first".into());
|
||||
}
|
||||
}
|
||||
// Q6: forwarded over loopback, TCP only, never a reserved port
|
||||
// (rows the save adds or edits; everything when newly on AWS).
|
||||
crate::host::port_forwards::validate_aws_port_mappings_update(
|
||||
@@ -1538,7 +1558,8 @@ async fn start_project_container_locked(
|
||||
// AWS (Q6): the project's port mappings become loopback forwards into
|
||||
// the workspace; a mapping that can't open is a warning, the rest work.
|
||||
if project.is_aws() {
|
||||
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &project_id).await {
|
||||
let warn = ports_warner(&app_handle, &project_id, &project.name);
|
||||
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &project_id, Some(warn)).await {
|
||||
emit_ports_warning(&app_handle, &project_id, &project.name, &detail);
|
||||
}
|
||||
}
|
||||
@@ -1589,6 +1610,20 @@ fn emit_connect_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &
|
||||
let _ = app_handle.emit("workspace-warning", connect_warning_payload(project_id, name, detail));
|
||||
}
|
||||
|
||||
/// A start that an app restart interrupted (EC2 cold start): topic "start".
|
||||
fn emit_interrupted_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &str, detail: &str) {
|
||||
let _ = app_handle.emit("workspace-warning", interrupted_warning_payload(project_id, name, detail));
|
||||
}
|
||||
|
||||
fn interrupted_warning_payload(project_id: &str, name: &str, detail: &str) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"project_id": project_id,
|
||||
"message": format!("“{name}”: a start was interrupted"),
|
||||
"detail": detail,
|
||||
"topic": "start",
|
||||
})
|
||||
}
|
||||
|
||||
fn connect_warning_payload(project_id: &str, name: &str, detail: &str) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"project_id": project_id,
|
||||
@@ -1601,12 +1636,23 @@ fn connect_warning_payload(project_id: &str, name: &str, detail: &str) -> serde_
|
||||
/// Port mappings that could not be forwarded to a connected AWS workspace
|
||||
/// (Q6): a busy host port, a host port mapped twice. Same `workspace-warning`
|
||||
/// event, with its own `topic` so it does not replace the other warnings.
|
||||
/// Where a port mapping's later notices ("nothing is listening") go: the
|
||||
/// same `ports` warning toast.
|
||||
fn ports_warner(app_handle: &tauri::AppHandle, project_id: &str, name: &str) -> crate::host::port_forwards::PortsWarn {
|
||||
let (handle, id, name) = (app_handle.clone(), project_id.to_string(), name.to_string());
|
||||
std::sync::Arc::new(move |detail: String| emit_ports_warning(&handle, &id, &name, &detail))
|
||||
}
|
||||
|
||||
fn emit_ports_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &str, detail: &str) {
|
||||
let _ = app_handle.emit(
|
||||
"workspace-warning",
|
||||
serde_json::json!({
|
||||
"project_id": project_id,
|
||||
"message": format!("“{name}”: some port mappings could not be opened"),
|
||||
"message": if detail.contains("nothing is listening") {
|
||||
format!("“{name}”: a port mapping found nothing listening in the workspace")
|
||||
} else {
|
||||
format!("“{name}”: some port mappings could not be opened")
|
||||
},
|
||||
"detail": detail,
|
||||
"topic": "ports",
|
||||
}),
|
||||
@@ -1646,6 +1692,29 @@ pub async fn aws_environment_max_hours(
|
||||
Ok(crate::host::aws_host::environment_max_hours(&project, &settings).await)
|
||||
}
|
||||
|
||||
/// The caller's EC2 capacity in an AWS project's environment, for Host
|
||||
/// settings' launch-type control and reservation limits (phase 7). Never an
|
||||
/// error: a local project or a failed read is `unknown`.
|
||||
#[tauri::command]
|
||||
pub async fn aws_ec2_capacity(
|
||||
project_id: String,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<crate::host::aws_host::Ec2Capacity, String> {
|
||||
let Some(project) = state.projects_store.get(&project_id).filter(|p| p.is_aws()) else {
|
||||
return Ok(crate::host::aws_host::Ec2Capacity::Unknown);
|
||||
};
|
||||
let settings = state.settings_store.get();
|
||||
Ok(crate::host::aws_host::ec2_capacity(&project, &settings).await)
|
||||
}
|
||||
|
||||
/// Why `config`'s CPU/memory reservations don't fit `instance_type`, or
|
||||
/// null (`models::project::ec2_fit_problem`: the start's own rule, so Host
|
||||
/// settings never disagree with it).
|
||||
#[tauri::command]
|
||||
pub fn ec2_fit_problem(config: AwsWorkspaceConfig, instance_type: String) -> Option<String> {
|
||||
crate::models::project::ec2_fit_problem(&config, &instance_type)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn stop_project_container(
|
||||
project_id: String,
|
||||
@@ -1689,6 +1758,9 @@ pub async fn stop_project_container(
|
||||
state.auth_bridge.stop(&project_id).await;
|
||||
if let Some(container) = guard_target.as_ref() {
|
||||
state.exec_manager.close_sessions_for_container(container.id()).await;
|
||||
// Closing the tabs only detaches persistent terminals: end
|
||||
// their tmux servers too, before the push's snapshot.
|
||||
crate::host::persistent::end_all(container).await;
|
||||
}
|
||||
if wip == Some(WipChoice::Push) {
|
||||
emit_progress(&app_handle, &project_id, "Pushing unsaved work...");
|
||||
@@ -1747,6 +1819,8 @@ pub async fn stop_project_container(
|
||||
)?;
|
||||
}
|
||||
state.projects_store.set_container_id(&project_id, None)?;
|
||||
// Its terminals ended with the task.
|
||||
crate::storage::terminal_store::clear(&project_id);
|
||||
} else if let Some(container) = ContainerTarget::for_project(&project) {
|
||||
// Close exec sessions for this project
|
||||
emit_progress(&app_handle, &project_id, "Stopping container...");
|
||||
@@ -2173,6 +2247,10 @@ pub async fn reconcile_aws_projects(
|
||||
let on_ports_warning = move |detail: String| {
|
||||
emit_ports_warning(&ports_handle, &ports_id, &ports_name, &detail)
|
||||
};
|
||||
let (int_handle, int_id, int_name) = (handle.clone(), id.clone(), name.clone());
|
||||
let on_interrupted_warning = move |detail: String| {
|
||||
emit_interrupted_warning(&int_handle, &int_id, &int_name, &detail)
|
||||
};
|
||||
let on_connect_warning =
|
||||
move |detail: String| emit_connect_warning(&handle, &id, &name, &detail);
|
||||
if tokio::time::timeout(
|
||||
@@ -2183,6 +2261,7 @@ pub async fn reconcile_aws_projects(
|
||||
store,
|
||||
on_connect_warning,
|
||||
on_ports_warning,
|
||||
on_interrupted_warning,
|
||||
),
|
||||
)
|
||||
.await
|
||||
@@ -2213,6 +2292,16 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_interrupted_warning_has_its_own_topic_and_headline() {
|
||||
let v = interrupted_warning_payload("p1", "demo", "retry");
|
||||
assert_eq!(v["topic"], "start");
|
||||
assert_eq!(v["detail"], "retry");
|
||||
let m = v["message"].as_str().unwrap();
|
||||
assert!(m.contains("demo") && m.contains("a start was interrupted"));
|
||||
assert!(!m.contains("credentials"));
|
||||
}
|
||||
|
||||
// ── Pending-cleanup aging ────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
@@ -2894,6 +2983,43 @@ mod tests {
|
||||
assert!(validate_gateway_sidecar_update(&on, &on, &half).is_ok());
|
||||
// Off: nothing to check.
|
||||
assert!(validate_gateway_sidecar_update(&off, &off, &half).is_ok());
|
||||
|
||||
// EC2 (bridge networking): refused even when it was already on.
|
||||
let mut ec2 = on.clone();
|
||||
ec2.aws.as_mut().unwrap().launch_type = crate::models::LaunchType::Ec2;
|
||||
let e = validate_gateway_sidecar_update(&on, &ec2, &with_models).unwrap_err();
|
||||
assert_eq!(e, crate::aws::workspace::EC2_NO_SIDECAR);
|
||||
assert!(validate_gateway_sidecar_update(&ec2, &ec2, &with_models).is_err());
|
||||
ec2.aws.as_mut().unwrap().gateway_sidecar = false;
|
||||
assert!(validate_gateway_sidecar_update(&on, &ec2, &with_models).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docker_and_vpn_on_aws_need_the_ec2_launch_type() {
|
||||
use crate::models::LaunchType;
|
||||
let hosts = vec![host("h1")];
|
||||
let stored = aws_project();
|
||||
for (docker, vpn) in [(true, false), (false, true)] {
|
||||
let mut p = stored.clone();
|
||||
p.allow_docker_access = docker;
|
||||
p.vpn_support_enabled = vpn;
|
||||
assert!(validate_aws_update(&stored, &p, &hosts).is_err());
|
||||
// Already stored that way on Fargate: still refused (no grandfathering).
|
||||
assert!(validate_aws_update(&p, &p, &hosts).is_err());
|
||||
// Ec2 -> Fargate with the flags still on: refused.
|
||||
let mut ec2 = p.clone();
|
||||
ec2.aws.as_mut().unwrap().launch_type = LaunchType::Ec2;
|
||||
assert!(validate_aws_update(&ec2, &ec2, &hosts).is_ok());
|
||||
assert!(validate_aws_update(&ec2, &p, &hosts).is_err());
|
||||
// Local -> AWS(Fargate) with the flags on: refused.
|
||||
let mut local = stored.clone();
|
||||
local.host_id = None;
|
||||
local.aws_placement = None;
|
||||
local.aws = None;
|
||||
local.allow_docker_access = docker;
|
||||
local.vpn_support_enabled = vpn;
|
||||
assert!(validate_aws_update(&local, &p, &hosts).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -3116,6 +3242,14 @@ mod tests {
|
||||
let stop = &stop[..stop.find("\n}\n").unwrap()];
|
||||
let close = stop.find("close_sessions_for_container").unwrap();
|
||||
assert!(close < stop.find("aws_guard_before_stop(").unwrap());
|
||||
// Closing a tab only detaches a persistent terminal: its tmux server
|
||||
// is ended in the same step, before the push's snapshot.
|
||||
let end_all = stop.find("persistent::end_all(container)").unwrap();
|
||||
assert!(close < end_all && end_all < stop.find("aws_guard_before_stop(").unwrap());
|
||||
let close_first = &stop[stop.find("let close_first = async").unwrap()..stop.find("let guarded").unwrap()];
|
||||
assert!(close_first.contains("persistent::end_all"), "inside the step that runs first");
|
||||
// A stopped task's terminals are gone: forgotten.
|
||||
assert!(stop.find("terminal_store::clear").unwrap() > stop.find("stop_project(&project").unwrap());
|
||||
assert!(stop.find("auth_bridge.stop").unwrap() < stop.find("aws_guard_before_stop(").unwrap());
|
||||
}
|
||||
|
||||
@@ -3242,20 +3376,21 @@ mod tests {
|
||||
let start = fn_body(src, "start_project_container_locked");
|
||||
let running = start.find("update_status(&project_id, ProjectStatus::Running)").unwrap();
|
||||
let open = start
|
||||
.find("port_forwards::open_for_project(&state.projects_store, &project_id)")
|
||||
.find("port_forwards::open_for_project(&state.projects_store, &project_id, Some(warn))")
|
||||
.expect("the start opens them");
|
||||
assert!(open > running);
|
||||
let guard = start[..open].rfind("if project.is_aws() {").expect("AWS-only");
|
||||
assert!(start[guard..open].lines().count() <= 2);
|
||||
assert!(start[guard..open].lines().count() <= 3);
|
||||
assert!(start[guard..open].contains("let warn = ports_warner(&app_handle, &project_id, &project.name);"));
|
||||
assert!(start[open..].lines().nth(1).unwrap().contains("emit_ports_warning("));
|
||||
let update = fn_body(src, "update_project");
|
||||
assert!(update.contains("port_forwards::open_for_project(&state.projects_store, &saved.id)"));
|
||||
assert!(update.contains("port_forwards::open_for_project(&state.projects_store, &saved.id, Some(warn))"));
|
||||
let reconcile = fn_body(src, "reconcile_aws_projects");
|
||||
assert!(reconcile.contains("on_ports_warning,"));
|
||||
assert!(reconcile.contains("emit_ports_warning(&ports_handle, &ports_id, &ports_name, &detail)"));
|
||||
let aws_host = include_str!("../host/aws_host.rs");
|
||||
let reattached = aws_host.find("Ok(true) => {").unwrap();
|
||||
let opened = aws_host.find("port_forwards::open_for_project(&store, &project.id)").unwrap();
|
||||
let opened = aws_host.find("port_forwards::open_for_project(&store, &project.id, Some(warn.clone()))").unwrap();
|
||||
let pushed = aws_host[reattached..].find("connect::apply_connect(").unwrap() + reattached;
|
||||
assert!(reattached < opened && opened < pushed);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
|
||||
use crate::commands::aws_commands;
|
||||
use crate::host::persistent::{self, TerminalKind};
|
||||
use crate::host::ContainerTarget;
|
||||
use crate::models::{Backend, BedrockAuthMethod, Project};
|
||||
use crate::models::{
|
||||
Backend, BedrockAuthMethod, OpenedTerminal, Project, ProjectStatus, ReattachOutcome,
|
||||
WorkspaceTerminal,
|
||||
};
|
||||
use crate::storage::terminal_store::{self, PersistedTerminal};
|
||||
use crate::AppState;
|
||||
|
||||
/// Build the command to run in the container terminal.
|
||||
@@ -145,6 +150,10 @@ fi
|
||||
]
|
||||
}
|
||||
|
||||
/// Open a terminal tab. On an AWS project whose image has tmux the command
|
||||
/// runs inside its own tmux server keyed by `session_id`
|
||||
/// (`host::persistent`), so it outlives the app; the tab is remembered so the
|
||||
/// app reopens it on its next connect.
|
||||
#[tauri::command]
|
||||
pub async fn open_terminal_session(
|
||||
project_id: String,
|
||||
@@ -154,7 +163,7 @@ pub async fn open_terminal_session(
|
||||
resume_session_id: Option<String>,
|
||||
app_handle: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<OpenedTerminal, String> {
|
||||
let project = state
|
||||
.projects_store
|
||||
.get(&project_id)
|
||||
@@ -169,6 +178,7 @@ pub async fn open_terminal_session(
|
||||
crate::commands::inspect_commands::validate_session_id(id)?;
|
||||
}
|
||||
|
||||
let kind = if session_type.as_deref() == Some("bash") { TerminalKind::Bash } else { TerminalKind::Claude };
|
||||
let cmd = match session_type.as_deref() {
|
||||
Some("bash") if resume_session_id.is_some() => {
|
||||
return Err("Only a Claude session can resume a conversation".to_string());
|
||||
@@ -186,6 +196,17 @@ pub async fn open_terminal_session(
|
||||
// environment (never the agent's). Empty for everything else.
|
||||
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
|
||||
|
||||
let plan = persistent::plan(
|
||||
project.is_aws(),
|
||||
&target,
|
||||
&session_id,
|
||||
kind,
|
||||
session_name.as_deref(),
|
||||
cmd,
|
||||
env,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let output_event = format!("terminal-output-{}", session_id);
|
||||
let exit_event = format!("terminal-exit-{}", session_id);
|
||||
let app_handle_output = app_handle.clone();
|
||||
@@ -196,8 +217,8 @@ pub async fn open_terminal_session(
|
||||
.create_session(
|
||||
&target,
|
||||
&session_id,
|
||||
cmd,
|
||||
env,
|
||||
plan.cmd,
|
||||
plan.env,
|
||||
move |data| {
|
||||
let _ = app_handle_output.emit(&output_event, data);
|
||||
},
|
||||
@@ -205,7 +226,19 @@ pub async fn open_terminal_session(
|
||||
let _ = app_handle_exit.emit(&exit_event, ());
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.await?;
|
||||
|
||||
if plan.persistent {
|
||||
let entry = PersistedTerminal {
|
||||
id: session_id.clone(),
|
||||
kind,
|
||||
name: persistent::clean_name(session_name.as_deref()),
|
||||
};
|
||||
if let Err(e) = terminal_store::add(&project_id, entry) {
|
||||
log::warn!("Could not remember terminal {session_id}: {e}");
|
||||
}
|
||||
}
|
||||
Ok(OpenedTerminal { persistent: plan.persistent, notice: plan.notice })
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -227,6 +260,8 @@ pub async fn terminal_resize(
|
||||
state.exec_manager.resize(&session_id, cols, rows).await
|
||||
}
|
||||
|
||||
/// Close a tab. A persistent AWS terminal is *ended* (its tmux server
|
||||
/// killed) and forgotten: closing a tab is the one gesture that ends one.
|
||||
#[tauri::command]
|
||||
pub async fn close_terminal_session(
|
||||
session_id: String,
|
||||
@@ -235,8 +270,254 @@ pub async fn close_terminal_session(
|
||||
// Close audio bridge if it exists
|
||||
let audio_session_id = format!("audio-{}", session_id);
|
||||
state.exec_manager.close_session(&audio_session_id).await;
|
||||
let session_target = state.exec_manager.get_target(&session_id).await.ok();
|
||||
// Close terminal session
|
||||
state.exec_manager.close_session(&session_id).await;
|
||||
|
||||
if let Some((project_id, _)) = terminal_store::find(&session_id) {
|
||||
// A reattach of this tab still in flight must not remember it again.
|
||||
mark_closed(&session_id);
|
||||
if let Err(e) = terminal_store::remove(&session_id) {
|
||||
log::warn!("Could not forget terminal {session_id}: {e}");
|
||||
}
|
||||
// The project's current tunnel first: the session's own endpoint is
|
||||
// stale after a re-attach on a new port.
|
||||
let target = state
|
||||
.projects_store
|
||||
.get(&project_id)
|
||||
.and_then(|p| ContainerTarget::for_project(&p))
|
||||
.or(session_target);
|
||||
let ended = match target {
|
||||
Some(t) => persistent::end(&t, &session_id).await,
|
||||
None => Err("not connected".to_string()),
|
||||
};
|
||||
if let Err(e) = ended {
|
||||
// Still running in the workspace (a leftover Claude would keep
|
||||
// it up): end it at the next connect.
|
||||
log::warn!("Terminal {session_id}: could not end it now ({e}); ending it at the next connect");
|
||||
if let Err(e) = terminal_store::add_pending_end(&project_id, &session_id) {
|
||||
log::warn!("Could not remember to end terminal {session_id}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Session ids whose tab was closed in this run of the app.
|
||||
fn closed_ids() -> &'static std::sync::Mutex<std::collections::HashSet<String>> {
|
||||
static C: std::sync::OnceLock<std::sync::Mutex<std::collections::HashSet<String>>> =
|
||||
std::sync::OnceLock::new();
|
||||
C.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
fn mark_closed(session_id: &str) {
|
||||
closed_ids().lock().unwrap_or_else(|e| e.into_inner()).insert(session_id.to_string());
|
||||
}
|
||||
|
||||
fn was_closed(session_id: &str) -> bool {
|
||||
closed_ids().lock().unwrap_or_else(|e| e.into_inner()).contains(session_id)
|
||||
}
|
||||
|
||||
/// End the project's sessions whose tabs were closed while it could not be
|
||||
/// reached; returns the ones ended (or found gone).
|
||||
async fn end_pending(project_id: &str, target: &ContainerTarget, running: &[persistent::RunningSession]) -> Vec<String> {
|
||||
let pending = terminal_store::pending_end(project_id);
|
||||
let mut done = Vec::new();
|
||||
for key in pending {
|
||||
if !running.iter().any(|r| r.key == key) {
|
||||
done.push(key);
|
||||
continue;
|
||||
}
|
||||
match persistent::end(target, &key).await {
|
||||
Ok(()) => done.push(key),
|
||||
Err(e) => log::warn!("Could not end closed terminal {key}: {e}"),
|
||||
}
|
||||
}
|
||||
if !done.is_empty() {
|
||||
if let Err(e) = terminal_store::done_pending_end(project_id, &done) {
|
||||
log::warn!("Could not update the terminals to end: {e}");
|
||||
}
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
/// A connected AWS project's target, waiting for a re-attach in progress.
|
||||
async fn aws_target(project: &Project) -> Result<ContainerTarget, String> {
|
||||
if !project.is_aws() {
|
||||
return Err("Only AWS workspaces keep terminals".into());
|
||||
}
|
||||
if project.status != ProjectStatus::Running {
|
||||
return Err(format!("“{}” is not running", project.name));
|
||||
}
|
||||
ContainerTarget::for_project_waiting(project)
|
||||
.await
|
||||
.ok_or_else(|| "Triple-C is not connected to the workspace".to_string())
|
||||
}
|
||||
|
||||
/// The workspace's running terminals with what the app remembers of them,
|
||||
/// remembered ones first in the order they were opened. Prunes remembered
|
||||
/// entries the workspace no longer runs. Empty for a Docker project, a
|
||||
/// stopped one and an image without tmux.
|
||||
#[tauri::command]
|
||||
pub async fn list_workspace_terminals(
|
||||
project_id: String,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Vec<WorkspaceTerminal>, String> {
|
||||
let project = state
|
||||
.projects_store
|
||||
.get(&project_id)
|
||||
.ok_or_else(|| format!("Project {} not found", project_id))?;
|
||||
if !project.is_aws() || project.status != ProjectStatus::Running {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let target = aws_target(&project).await?;
|
||||
// An `Err` (an unanswered probe or listing) says nothing about what runs:
|
||||
// nothing is pruned or ended on it, and the caller asks again.
|
||||
let mut running = persistent::list(&target).await?;
|
||||
let ended = end_pending(&project_id, &target, &running).await;
|
||||
running.retain(|r| !ended.contains(&r.key));
|
||||
let keys = running.iter().map(|r| r.key.clone()).collect();
|
||||
if let Err(e) = terminal_store::retain(&project_id, &keys) {
|
||||
log::warn!("Could not prune remembered terminals: {e}");
|
||||
}
|
||||
Ok(merge_terminals(&terminal_store::list(&project_id), running))
|
||||
}
|
||||
|
||||
/// Remembered sessions first (in their order), then the others oldest first.
|
||||
pub(crate) fn merge_terminals(
|
||||
remembered: &[PersistedTerminal],
|
||||
mut running: Vec<persistent::RunningSession>,
|
||||
) -> Vec<WorkspaceTerminal> {
|
||||
running.sort_by_key(|r| {
|
||||
(
|
||||
remembered.iter().position(|m| m.id == r.key).unwrap_or(usize::MAX),
|
||||
r.created_unix,
|
||||
)
|
||||
});
|
||||
running
|
||||
.into_iter()
|
||||
.map(|r| {
|
||||
let mine = remembered.iter().find(|m| m.id == r.key);
|
||||
WorkspaceTerminal {
|
||||
kind: mine.map(|m| m.kind).unwrap_or(r.kind),
|
||||
name: mine.map(|m| m.name.clone()).unwrap_or(r.name),
|
||||
known: mine.is_some(),
|
||||
session_id: r.key,
|
||||
created_unix: r.created_unix,
|
||||
attached: r.attached,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Attach a tab to a terminal still running in the workspace: a tab the app
|
||||
/// reopens after a restart or a dropped connection, or one picked in Project
|
||||
/// Home → Sessions. Never starts anything: a session that is gone is
|
||||
/// `Ended` (and forgotten). With `rows` (a fresh xterm), the pane's history
|
||||
/// is replayed into the tab's output first, so its scrollback comes back.
|
||||
/// The tab's output listener must be registered before this is called.
|
||||
#[tauri::command]
|
||||
pub async fn reattach_terminal_session(
|
||||
project_id: String,
|
||||
session_id: String,
|
||||
rows: Option<u16>,
|
||||
app_handle: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<ReattachOutcome, String> {
|
||||
persistent::validate_key(&session_id)?;
|
||||
let project = state
|
||||
.projects_store
|
||||
.get(&project_id)
|
||||
.ok_or_else(|| format!("Project {} not found", project_id))?;
|
||||
if was_closed(&session_id) {
|
||||
return Ok(ReattachOutcome::Ended);
|
||||
}
|
||||
let target = aws_target(&project).await?;
|
||||
// Unanswered → `Err` (the tab retries); only a real "no tmux" ends it.
|
||||
if !persistent::supported(&target).await? {
|
||||
terminal_store::remove(&session_id).ok();
|
||||
return Ok(ReattachOutcome::Ended);
|
||||
}
|
||||
if terminal_store::pending_end(&project_id).contains(&session_id) {
|
||||
return Ok(ReattachOutcome::Ended);
|
||||
}
|
||||
let running = persistent::list(&target).await?;
|
||||
let Some(found) = running.into_iter().find(|r| r.key == session_id) else {
|
||||
if let Err(e) = terminal_store::remove(&session_id) {
|
||||
log::warn!("Could not forget terminal {session_id}: {e}");
|
||||
}
|
||||
return Ok(ReattachOutcome::Ended);
|
||||
};
|
||||
let remembered = terminal_store::find(&session_id).filter(|(p, _)| *p == project_id);
|
||||
let (kind, name) = match remembered {
|
||||
Some((_, m)) => (m.kind, m.name),
|
||||
None => (found.kind, found.name),
|
||||
};
|
||||
|
||||
let output_event = format!("terminal-output-{}", session_id);
|
||||
if let Some(rows) = rows {
|
||||
// Exactly one chunk, empty when there is no history: the tab treats
|
||||
// the first chunk after asking for a replay as the replay, so live
|
||||
// output after it gets its URL/OSC/SSO handling again.
|
||||
let bytes = persistent::history(&target, &session_id)
|
||||
.await
|
||||
.map(|h| persistent::replay_bytes(&h, rows))
|
||||
.unwrap_or_default();
|
||||
let _ = app_handle.emit(&output_event, bytes);
|
||||
}
|
||||
|
||||
let exit_event = format!("terminal-exit-{}", session_id);
|
||||
let app_handle_output = app_handle.clone();
|
||||
let app_handle_exit = app_handle.clone();
|
||||
state
|
||||
.exec_manager
|
||||
.create_session(
|
||||
&target,
|
||||
&session_id,
|
||||
persistent::attach_cmd(&session_id)?,
|
||||
persistent::client_env(Vec::new()),
|
||||
move |data| {
|
||||
let _ = app_handle_output.emit(&output_event, data);
|
||||
},
|
||||
Box::new(move || {
|
||||
let _ = app_handle_exit.emit(&exit_event, ());
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Closed while this attach was under way: its close already ended (or
|
||||
// queued the end of) the session; don't remember it again.
|
||||
if was_closed(&session_id) {
|
||||
state.exec_manager.close_session(&session_id).await;
|
||||
return Ok(ReattachOutcome::Ended);
|
||||
}
|
||||
let entry = PersistedTerminal { id: session_id.clone(), kind, name: name.clone() };
|
||||
if let Err(e) = terminal_store::add(&project_id, entry) {
|
||||
log::warn!("Could not remember terminal {session_id}: {e}");
|
||||
}
|
||||
Ok(ReattachOutcome::Reattached { kind, name })
|
||||
}
|
||||
|
||||
/// End a terminal running in the workspace that has no tab here (Project
|
||||
/// Home → Sessions → End). A tab's own close goes through
|
||||
/// `close_terminal_session`.
|
||||
#[tauri::command]
|
||||
pub async fn end_workspace_terminal(
|
||||
project_id: String,
|
||||
session_id: String,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
persistent::validate_key(&session_id)?;
|
||||
let project = state
|
||||
.projects_store
|
||||
.get(&project_id)
|
||||
.ok_or_else(|| format!("Project {} not found", project_id))?;
|
||||
let target = aws_target(&project).await?;
|
||||
state.exec_manager.close_session(&session_id).await;
|
||||
persistent::end(&target, &session_id).await?;
|
||||
if let Err(e) = terminal_store::remove(&session_id) {
|
||||
log::warn!("Could not forget terminal {session_id}: {e}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -571,6 +852,40 @@ mod tests {
|
||||
assert!(!cmd[2].contains(" -n "), "resume must not rename: {}", cmd[2]);
|
||||
}
|
||||
|
||||
/// The app's own tabs come first in the order they were opened, with
|
||||
/// the app's labels; sessions from elsewhere follow, oldest first, and
|
||||
/// are not "known" (so they are listed, never auto-opened).
|
||||
#[test]
|
||||
fn workspace_terminals_merge_what_the_app_remembers_with_what_runs() {
|
||||
use super::merge_terminals;
|
||||
use crate::host::persistent::{RunningSession, TerminalKind};
|
||||
use crate::storage::terminal_store::PersistedTerminal;
|
||||
let run = |key: &str, kind, created, name: Option<&str>| RunningSession {
|
||||
key: key.into(),
|
||||
kind,
|
||||
created_unix: created,
|
||||
attached: false,
|
||||
name: name.map(str::to_string),
|
||||
};
|
||||
let remembered = vec![
|
||||
PersistedTerminal { id: "b".into(), kind: TerminalKind::Claude, name: Some("second".into()) },
|
||||
PersistedTerminal { id: "a".into(), kind: TerminalKind::Bash, name: None },
|
||||
PersistedTerminal { id: "gone".into(), kind: TerminalKind::Bash, name: None },
|
||||
];
|
||||
let running = vec![
|
||||
run("x", TerminalKind::Bash, 50, Some("web terminal")),
|
||||
run("a", TerminalKind::Bash, 10, None),
|
||||
run("w", TerminalKind::Claude, 5, None),
|
||||
run("b", TerminalKind::Claude, 20, Some("tmux label")),
|
||||
];
|
||||
let got = merge_terminals(&remembered, running);
|
||||
let ids: Vec<_> = got.iter().map(|t| t.session_id.as_str()).collect();
|
||||
assert_eq!(ids, ["b", "a", "w", "x"]);
|
||||
assert!(got[0].known && got[1].known && !got[2].known && !got[3].known);
|
||||
assert_eq!(got[0].name.as_deref(), Some("second"), "the app's label wins");
|
||||
assert_eq!(got[3].name.as_deref(), Some("web terminal"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dropped_file_keeps_the_name_the_user_dropped() {
|
||||
use crate::commands::file_commands::host_upload_name;
|
||||
|
||||
@@ -117,7 +117,14 @@ async fn settle(store: &ProjectsStore, container: &ContainerTarget) -> Option<Pr
|
||||
return None;
|
||||
}
|
||||
match store.mark_exited(container_id, &seen, project_busy) {
|
||||
ExitMark::Flipped(p) => return Some(*p),
|
||||
ExitMark::Flipped(p) => {
|
||||
// An AWS task that stopped by itself (idle, lifetime cap,
|
||||
// crash) took its persistent terminals with it.
|
||||
if p.is_aws() {
|
||||
crate::storage::terminal_store::clear(&p.id);
|
||||
}
|
||||
return Some(*p);
|
||||
}
|
||||
ExitMark::NotApplicable => return None,
|
||||
// The store moved while the host was answering — possibly a stop and
|
||||
// start of this very container. The inspect is stale; ask again.
|
||||
|
||||
@@ -1321,6 +1321,59 @@ fn aws_stop_skips_its_push_when_the_app_already_pushed_or_the_user_declined() {
|
||||
assert_eq!(fs::read_to_string(&log).unwrap(), "--reason sigterm\n");
|
||||
}
|
||||
|
||||
/// Persistent terminals (tmux) end before the hook's own push takes its
|
||||
/// snapshot, and only their servers: the socket prefix is the app's.
|
||||
#[test]
|
||||
fn aws_stop_ends_the_persistent_terminals_before_its_push() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let log = tmp.path().join("calls");
|
||||
let bin = tmp.path().join("bin");
|
||||
fs::create_dir_all(&bin).unwrap();
|
||||
let fake_push = write_script(
|
||||
tmp.path(),
|
||||
"triple-c-wip-push",
|
||||
&format!("#!/bin/bash\necho \"push $*\" >> '{}'\n", log.display()),
|
||||
);
|
||||
write_script(&bin, "tmux", &format!("#!/bin/bash\necho \"tmux $*\" >> '{}'\n", log.display()));
|
||||
// Two of ours and someone else's server in the same socket directory.
|
||||
let socks = tmp.path().join("socks");
|
||||
let dir = socks.join(format!("tmux-{}", unsafe { libc::getuid() }));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let mut keep = Vec::new();
|
||||
for name in ["triple-c-aaa", "triple-c-bbb", "mine"] {
|
||||
keep.push(std::os::unix::net::UnixListener::bind(dir.join(name)).unwrap());
|
||||
}
|
||||
let blocks = aws_blocks(&["aws_env", "aws_stop"])
|
||||
.replace("/usr/local/bin/triple-c-wip-push", fake_push.to_str().unwrap())
|
||||
.replace("/run/triple-c/stop-reason", tmp.path().join("reason").to_str().unwrap());
|
||||
let path = format!("{}:{}", bin.display(), std::env::var("PATH").unwrap());
|
||||
let script = format!("{blocks}\naws_before_stop\n");
|
||||
let env = [
|
||||
("TRIPLEC_HOST", "aws"),
|
||||
("TRIPLEC_WIP_PUSH", "1"),
|
||||
("PATH", path.as_str()),
|
||||
("TMUX_TMPDIR", socks.to_str().unwrap()),
|
||||
];
|
||||
let out = bash(&script, &[], &env, &[]);
|
||||
assert!(out.status.success(), "{}", text(&out));
|
||||
let calls = fs::read_to_string(&log).unwrap();
|
||||
assert_eq!(
|
||||
calls,
|
||||
"tmux -L triple-c-aaa list-panes -a -F #{pane_pid}\ntmux -L triple-c-aaa kill-server\n\
|
||||
tmux -L triple-c-bbb list-panes -a -F #{pane_pid}\ntmux -L triple-c-bbb kill-server\n\
|
||||
push --reason sigterm\n"
|
||||
);
|
||||
drop(keep);
|
||||
// The app's own Stop and the idle watcher's lifetime stop run the very
|
||||
// same script.
|
||||
let script = crate::host::persistent::END_ALL_SCRIPT;
|
||||
assert!(block(ENTRYPOINT, "aws_stop").contains(script));
|
||||
assert!(IDLE_WATCH.contains(script));
|
||||
assert_eq!(crate::host::persistent::end_all_cmd()[2], script);
|
||||
let lifetime = &IDLE_WATCH[IDLE_WATCH.find("lifetime_stop() {").unwrap()..];
|
||||
assert!(lifetime.find("end_terminals").unwrap() < lifetime.find("--reason lifetime").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_idle_exit_status_is_the_one_the_app_reads() {
|
||||
assert!(
|
||||
@@ -2666,3 +2719,91 @@ fn two_connects_at_once_never_mix_their_files() {
|
||||
assert_eq!(c.run(&["../../etc"]).status.code(), Some(2));
|
||||
assert_eq!(c.run(&[&a]).status.code(), Some(1));
|
||||
}
|
||||
|
||||
// ------------------------------------------- Docker access and VPN (phase 7, EC2)
|
||||
|
||||
/// The `docker_socket` block with the socket at `sock` and the account tools
|
||||
/// faked (they log to stdout): `getent` knows no group, so one is added.
|
||||
fn docker_socket_step(sock: &Path, aws: bool) -> Output {
|
||||
let script = format!(
|
||||
"getent() {{ return 2; }}\n\
|
||||
groupadd() {{ echo \"groupadd $*\"; }}\n\
|
||||
usermod() {{ echo \"usermod $*\"; }}\n\
|
||||
triplec_aws={}\n{}",
|
||||
u8::from(aws),
|
||||
eblock("docker_socket").replace("/var/run/docker.sock", &sock.display().to_string())
|
||||
);
|
||||
bash(&script, &[], &[], &[])
|
||||
}
|
||||
|
||||
fn unix_socket(path: &Path) -> std::os::unix::net::UnixListener {
|
||||
std::os::unix::net::UnixListener::bind(path).expect("bind socket")
|
||||
}
|
||||
|
||||
/// E6: an EC2 project's Docker access is its instance's socket, so the group
|
||||
/// step runs in aws mode too (it was Docker-only), and as before without one.
|
||||
#[test]
|
||||
fn the_docker_socket_group_step_runs_in_aws_mode_when_the_socket_exists() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let sock = tmp.path().join("docker.sock");
|
||||
let _listener = unix_socket(&sock);
|
||||
let gid = fs::metadata(&sock).map(|m| std::os::unix::fs::MetadataExt::gid(&m)).unwrap();
|
||||
for aws in [true, false] {
|
||||
let out = docker_socket_step(&sock, aws);
|
||||
let t = text(&out);
|
||||
assert!(out.status.success(), "{t}");
|
||||
assert!(t.contains(&format!("groupadd -g {gid} docker-host")), "aws={aws}: {t}");
|
||||
assert!(t.contains("usermod -aG"), "aws={aws}: {t}");
|
||||
assert!(t.trim_end().ends_with(" claude"), "aws={aws}: {t}");
|
||||
}
|
||||
// No socket (Fargate, or Docker access off): nothing happens.
|
||||
let out = docker_socket_step(&tmp.path().join("absent.sock"), true);
|
||||
assert!(out.status.success());
|
||||
assert_eq!(text(&out), "");
|
||||
// A regular file there is not a socket either.
|
||||
let file = tmp.path().join("file.sock");
|
||||
fs::write(&file, "").unwrap();
|
||||
assert_eq!(text(&docker_socket_step(&file, true)), "");
|
||||
assert!(!eblock("docker_socket").contains("triplec_aws"), "no aws gate");
|
||||
}
|
||||
|
||||
/// The feature-skills block with the skill source and destination in `tmp`
|
||||
/// and `chown` switched off.
|
||||
fn feature_skills(tmp: &Path, vpn: &str) -> Output {
|
||||
let src = tmp.join("opt-skills");
|
||||
let dest = tmp.join("home-skills");
|
||||
let script = format!(
|
||||
"chown() {{ :; }}\ntriplec_aws=1\n{}\n[ -z \"${{VPN_SUPPORT_ENABLED+x}}\" ] && echo unset-ok",
|
||||
eblock("feature_skills")
|
||||
.replace("/opt/triple-c-skills", &src.display().to_string())
|
||||
.replace("/home/claude/.claude/skills", &dest.display().to_string())
|
||||
);
|
||||
bash(&script, &[], &[("VPN_SUPPORT_ENABLED", vpn)], &[])
|
||||
}
|
||||
|
||||
/// E7: in aws mode the VPN skill follows `VPN_SUPPORT_ENABLED` as on Docker
|
||||
/// (installed when 1, removed when 0), and the variable is gone after.
|
||||
#[test]
|
||||
fn the_vpn_skill_follows_vpn_support_in_aws_mode() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let src = tmp.path().join("opt-skills/pia-vpn");
|
||||
fs::create_dir_all(&src).unwrap();
|
||||
fs::write(src.join("SKILL.md"), "vpn").unwrap();
|
||||
let dest = tmp.path().join("home-skills/pia-vpn/SKILL.md");
|
||||
|
||||
let out = feature_skills(tmp.path(), "1");
|
||||
let t = text(&out);
|
||||
assert!(t.contains("pia-vpn skill installed"), "{t}");
|
||||
assert!(t.contains("unset-ok"), "{t}");
|
||||
assert_eq!(fs::read_to_string(&dest).unwrap(), "vpn");
|
||||
|
||||
let t = text(&feature_skills(tmp.path(), "0"));
|
||||
assert!(t.contains("pia-vpn skill removed"), "{t}");
|
||||
assert!(!dest.exists());
|
||||
// The call itself is not behind any mode check.
|
||||
let call = ENTRYPOINT
|
||||
.lines()
|
||||
.find(|l| l.starts_with("install_feature_skill pia-vpn"))
|
||||
.expect("the call");
|
||||
assert!(!call.contains("triplec_aws"));
|
||||
}
|
||||
@@ -15,16 +15,35 @@ pub fn get_docker() -> Result<Docker, String> {
|
||||
}
|
||||
|
||||
pub async fn check_docker_available() -> Result<bool, String> {
|
||||
let docker = get_docker()?;
|
||||
match docker.ping().await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(_) => {
|
||||
// Connection object exists but daemon not responding — clear cache
|
||||
let mut guard = DOCKER.lock().map_err(|e| format!("Lock poisoned: {}", e))?;
|
||||
*guard = None;
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
Ok(docker_status().await.state == super::status::DockerState::Running)
|
||||
}
|
||||
|
||||
/// Ping the daemon and, when it does not answer, say why: not installed, installed
|
||||
/// but not running, or running but refusing us. See `docker/status.rs`.
|
||||
pub async fn docker_status() -> super::status::DockerStatus {
|
||||
use super::status::{self, PingOutcome};
|
||||
let docker_host = std::env::var("DOCKER_HOST").ok();
|
||||
let os = status::current_os();
|
||||
let endpoint = status::endpoint(docker_host.as_deref());
|
||||
|
||||
let (ping, detail) = match get_docker() {
|
||||
Err(e) => (PingOutcome::Unreachable, Some(e)),
|
||||
Ok(docker) => match docker.ping().await {
|
||||
Ok(_) => (PingOutcome::Ok, None),
|
||||
Err(e) => {
|
||||
// Connection object exists but daemon not responding — clear cache
|
||||
if let Ok(mut guard) = DOCKER.lock() {
|
||||
*guard = None;
|
||||
}
|
||||
(status::ping_outcome_from_error(&e), Some(e.to_string()))
|
||||
}
|
||||
},
|
||||
};
|
||||
let installed = ping == PingOutcome::Unreachable
|
||||
&& (status::docker_installed(os, docker_host.as_deref())
|
||||
// An explicit DOCKER_HOST means the user has a daemon somewhere.
|
||||
|| docker_host.as_deref().is_some_and(|h| !h.trim().is_empty()));
|
||||
status::DockerStatus { state: status::classify(ping, installed), endpoint, detail, os }
|
||||
}
|
||||
|
||||
/// The daemon socket a project container gets bind-mounted when Docker access
|
||||
|
||||
@@ -260,6 +260,10 @@ const RESERVED_ENV_PREFIXES: &[&str] = &["ANTHROPIC_", "AWS_", "GIT_", "HOST_",
|
||||
/// value would silently outrank the keychain-held shared token and be invisible
|
||||
/// to the auth UI.
|
||||
const RESERVED_ENV_EXACT: &[&str] = &[
|
||||
// Persistent AWS terminals (`host::persistent`): either would point a
|
||||
// tab's tmux at another socket than the one the app lists and ends.
|
||||
"TMUX",
|
||||
"TMUX_TMPDIR",
|
||||
"CLAUDE_INSTRUCTIONS",
|
||||
"MCP_SERVERS_JSON",
|
||||
"CLAUDE_CODE_SETTINGS_JSON",
|
||||
@@ -1112,6 +1116,37 @@ fn vpn_host_config(enabled: bool) -> VpnHostConfigParts {
|
||||
)
|
||||
}
|
||||
|
||||
/// What a VPN client needs, as plain data, for hosts that aren't Docker (an
|
||||
/// AWS EC2 task definition): the capabilities, the devices as
|
||||
/// `(host path, container path, cgroup permissions)`, and the sysctls,
|
||||
/// sorted. Derived from [`vpn_host_config`] so the two can't drift.
|
||||
pub(crate) struct VpnRequirements {
|
||||
pub capabilities: Vec<String>,
|
||||
pub devices: Vec<(String, String, String)>,
|
||||
pub sysctls: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
pub(crate) fn vpn_requirements() -> VpnRequirements {
|
||||
let (caps, devices, sysctls) = vpn_host_config(true);
|
||||
let mut sysctls: Vec<(String, String)> = sysctls.unwrap_or_default().into_iter().collect();
|
||||
sysctls.sort();
|
||||
VpnRequirements {
|
||||
capabilities: caps.unwrap_or_default(),
|
||||
devices: devices
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|d| {
|
||||
(
|
||||
d.path_on_host.unwrap_or_default(),
|
||||
d.path_in_container.unwrap_or_default(),
|
||||
d.cgroup_permissions.unwrap_or_default(),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
sysctls,
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn the daemon's device-passthrough failure into an explanation.
|
||||
///
|
||||
/// **This fires on `start`, not `create`.** Verified against Docker 29.7:
|
||||
|
||||
@@ -14,6 +14,8 @@ const ENTRYPOINT: &str = include_str!("../../../../container/entrypoint.sh");
|
||||
/// `sync_host_ssh` block the tests below run.
|
||||
const ENTRYPOINT_LIB: &str = include_str!("../../../../container/entrypoint-lib.sh");
|
||||
const APPLY_CONNECT: &str = include_str!("../../../../container/triple-c-apply-connect");
|
||||
/// `/etc/triple-c/tmux.conf`: persistent AWS terminals (`host::persistent`).
|
||||
const TMUX_CONF: &str = include_str!("../../../../container/triple-c-tmux.conf");
|
||||
const SCHEDULER: &str = include_str!("../../../../container/triple-c-scheduler");
|
||||
const TASK_RUNNER: &str = include_str!("../../../../container/triple-c-task-runner");
|
||||
const OSC52_CLIPBOARD: &str = include_str!("../../../../container/osc52-clipboard");
|
||||
@@ -247,6 +249,7 @@ fn create_build_context() -> Result<Vec<u8>, std::io::Error> {
|
||||
append_file_to_archive(&mut archive, "entrypoint.sh", ENTRYPOINT.as_bytes(), 0o755)?;
|
||||
append_file_to_archive(&mut archive, "entrypoint-lib.sh", ENTRYPOINT_LIB.as_bytes(), 0o644)?;
|
||||
append_file_to_archive(&mut archive, "triple-c-apply-connect", APPLY_CONNECT.as_bytes(), 0o755)?;
|
||||
append_file_to_archive(&mut archive, "triple-c-tmux.conf", TMUX_CONF.as_bytes(), 0o644)?;
|
||||
append_file_to_archive(&mut archive, "triple-c-scheduler", SCHEDULER.as_bytes(), 0o755)?;
|
||||
append_file_to_archive(&mut archive, "triple-c-task-runner", TASK_RUNNER.as_bytes(), 0o755)?;
|
||||
append_file_to_archive(&mut archive, "osc52-clipboard", OSC52_CLIPBOARD.as_bytes(), 0o755)?;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod ca_certs;
|
||||
pub mod client;
|
||||
pub mod status;
|
||||
pub mod events;
|
||||
pub mod container;
|
||||
pub mod workspace_env;
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
//! Why Docker is unavailable, not just whether it is.
|
||||
//!
|
||||
//! A daemon that failed to start (systemd `start-limit-hit`, Docker Desktop
|
||||
//! still booting) and a socket the user may not open both used to surface as
|
||||
//! "Docker not detected — install it", which sends people to reinstall
|
||||
//! something they already have. This module tells the cases apart from the
|
||||
//! ping's error and a few cheap filesystem probes. It never runs a process
|
||||
//! (no `systemctl`, no `docker info`).
|
||||
|
||||
use serde::Serialize;
|
||||
use std::error::Error as StdError;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Mirrors `DockerStatus` in `src/lib/types.ts`.
|
||||
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
|
||||
pub struct DockerStatus {
|
||||
/// `running` | `not_running` | `no_permission` | `not_installed`
|
||||
pub state: DockerState,
|
||||
/// The endpoint that was tried — `DOCKER_HOST` when set, else the default socket/pipe.
|
||||
pub endpoint: String,
|
||||
/// The connection error, for the "details" line. `None` when running.
|
||||
pub detail: Option<String>,
|
||||
/// `linux` | `macos` | `windows` | `unknown` — picks the guidance to show.
|
||||
pub os: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum DockerState {
|
||||
Running,
|
||||
NotRunning,
|
||||
NoPermission,
|
||||
NotInstalled,
|
||||
}
|
||||
|
||||
/// What the ping told us, reduced to what matters for the classification.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PingOutcome {
|
||||
Ok,
|
||||
/// The socket/pipe exists but the OS refused to let us open it.
|
||||
PermissionDenied,
|
||||
/// Nothing answered: socket missing, connection refused, timeout, other.
|
||||
Unreachable,
|
||||
}
|
||||
|
||||
/// The pure decision. `installed` is true when any installed-ness probe hit.
|
||||
pub fn classify(ping: PingOutcome, installed: bool) -> DockerState {
|
||||
match ping {
|
||||
PingOutcome::Ok => DockerState::Running,
|
||||
// Getting EACCES means something is listening on a socket we can't
|
||||
// open — Docker is there; the user lacks access.
|
||||
PingOutcome::PermissionDenied => DockerState::NoPermission,
|
||||
PingOutcome::Unreachable if installed => DockerState::NotRunning,
|
||||
PingOutcome::Unreachable => DockerState::NotInstalled,
|
||||
}
|
||||
}
|
||||
|
||||
/// Walk an error's `source()` chain for an `io::Error` and map its kind.
|
||||
/// Falls back to the rendered message, since some layers (hyper-util's
|
||||
/// connect errors, named pipes) only keep the OS error as text.
|
||||
pub fn ping_outcome_from_error(err: &(dyn StdError + 'static)) -> PingOutcome {
|
||||
let mut cur: Option<&(dyn StdError + 'static)> = Some(err);
|
||||
let mut text = String::new();
|
||||
while let Some(e) = cur {
|
||||
if let Some(ioe) = e.downcast_ref::<io::Error>() {
|
||||
if ioe.kind() == io::ErrorKind::PermissionDenied {
|
||||
return PingOutcome::PermissionDenied;
|
||||
}
|
||||
}
|
||||
text.push_str(&e.to_string().to_lowercase());
|
||||
text.push('\n');
|
||||
cur = e.source();
|
||||
}
|
||||
if text.contains("permission denied") || text.contains("access is denied") || text.contains("os error 13") {
|
||||
PingOutcome::PermissionDenied
|
||||
} else {
|
||||
PingOutcome::Unreachable
|
||||
}
|
||||
}
|
||||
|
||||
/// The endpoint bollard's `connect_with_local_defaults` will use.
|
||||
pub fn endpoint(docker_host: Option<&str>) -> String {
|
||||
match docker_host.map(str::trim).filter(|h| !h.is_empty()) {
|
||||
Some(h) => h.to_string(),
|
||||
None if cfg!(windows) => "npipe:////./pipe/docker_engine".to_string(),
|
||||
None => "unix:///var/run/docker.sock".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_os() -> &'static str {
|
||||
if cfg!(target_os = "linux") {
|
||||
"linux"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"macos"
|
||||
} else if cfg!(windows) {
|
||||
"windows"
|
||||
} else {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
/// Paths whose existence means Docker is installed on this OS. Pure so the
|
||||
/// list can be tested; `docker_installed` does the filesystem checks.
|
||||
pub fn install_markers(os: &str, home: Option<&Path>, docker_host: Option<&str>) -> Vec<PathBuf> {
|
||||
let mut v: Vec<PathBuf> = Vec::new();
|
||||
// A unix:// DOCKER_HOST whose socket file exists counts, whatever the OS.
|
||||
if let Some(p) = docker_host.and_then(|h| h.trim().strip_prefix("unix://")) {
|
||||
if !p.is_empty() {
|
||||
v.push(PathBuf::from(p));
|
||||
}
|
||||
}
|
||||
match os {
|
||||
"linux" => {
|
||||
v.extend(
|
||||
["/usr/bin/dockerd", "/usr/local/bin/dockerd", "/usr/bin/docker", "/var/run/docker.sock", "/run/docker.sock"]
|
||||
.iter()
|
||||
.map(PathBuf::from),
|
||||
);
|
||||
if let Some(h) = home {
|
||||
v.push(h.join(".docker/desktop"));
|
||||
v.push(h.join("bin/dockerd")); // rootless install script
|
||||
}
|
||||
}
|
||||
"macos" => {
|
||||
v.push(PathBuf::from("/Applications/Docker.app"));
|
||||
v.push(PathBuf::from("/var/run/docker.sock"));
|
||||
v.push(PathBuf::from("/usr/local/bin/docker"));
|
||||
if let Some(h) = home {
|
||||
v.push(h.join("Applications/Docker.app"));
|
||||
v.push(h.join(".docker/run/docker.sock"));
|
||||
}
|
||||
}
|
||||
"windows" => {
|
||||
v.push(PathBuf::from(r"C:\Program Files\Docker\Docker\Docker Desktop.exe"));
|
||||
v.push(PathBuf::from(r"C:\Program Files\Docker\Docker\resources\bin\docker.exe"));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// Executable names to look for on `PATH`.
|
||||
pub fn path_binaries(os: &str) -> &'static [&'static str] {
|
||||
match os {
|
||||
"windows" => &["docker.exe"],
|
||||
_ => &["docker", "dockerd"],
|
||||
}
|
||||
}
|
||||
|
||||
fn on_path(name: &str) -> bool {
|
||||
std::env::var_os("PATH")
|
||||
.map(|p| std::env::split_paths(&p).any(|dir| dir.join(name).is_file()))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Cheap installed-ness probe: stats a handful of paths, runs nothing.
|
||||
pub fn docker_installed(os: &str, docker_host: Option<&str>) -> bool {
|
||||
let home = dirs::home_dir();
|
||||
if install_markers(os, home.as_deref(), docker_host).iter().any(|p| p.exists()) {
|
||||
return true;
|
||||
}
|
||||
if os == "linux" {
|
||||
// Rootless Docker's socket lives in the user's runtime dir.
|
||||
if let Some(rt) = std::env::var_os("XDG_RUNTIME_DIR") {
|
||||
if Path::new(&rt).join("docker.sock").exists() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
path_binaries(os).iter().any(|b| on_path(b))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fmt;
|
||||
|
||||
#[test]
|
||||
fn ping_ok_is_running_regardless_of_probes() {
|
||||
assert_eq!(classify(PingOutcome::Ok, false), DockerState::Running);
|
||||
assert_eq!(classify(PingOutcome::Ok, true), DockerState::Running);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_and_installed_is_not_running() {
|
||||
// The reported case: dockerd present, systemd gave up restarting it.
|
||||
assert_eq!(classify(PingOutcome::Unreachable, true), DockerState::NotRunning);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_and_nothing_found_is_not_installed() {
|
||||
assert_eq!(classify(PingOutcome::Unreachable, false), DockerState::NotInstalled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_denied_is_no_permission_even_without_probes() {
|
||||
assert_eq!(classify(PingOutcome::PermissionDenied, false), DockerState::NoPermission);
|
||||
assert_eq!(classify(PingOutcome::PermissionDenied, true), DockerState::NoPermission);
|
||||
}
|
||||
|
||||
/// An error that wraps another, the way bollard → hyper → io do.
|
||||
#[derive(Debug)]
|
||||
struct Wrap(&'static str, Option<io::Error>);
|
||||
impl fmt::Display for Wrap {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.0)
|
||||
}
|
||||
}
|
||||
impl StdError for Wrap {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
self.1.as_ref().map(|e| e as _)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_io_permission_denied_is_found() {
|
||||
let e = Wrap("error trying to connect", Some(io::Error::from(io::ErrorKind::PermissionDenied)));
|
||||
assert_eq!(ping_outcome_from_error(&e), PingOutcome::PermissionDenied);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_connection_refused_and_not_found_are_unreachable() {
|
||||
let refused = Wrap("error trying to connect", Some(io::Error::from(io::ErrorKind::ConnectionRefused)));
|
||||
let missing = Wrap("error trying to connect", Some(io::Error::from(io::ErrorKind::NotFound)));
|
||||
assert_eq!(ping_outcome_from_error(&refused), PingOutcome::Unreachable);
|
||||
assert_eq!(ping_outcome_from_error(&missing), PingOutcome::Unreachable);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_denied_only_in_text_is_found() {
|
||||
let e = Wrap("client error (Connect): Permission denied (os error 13)", None);
|
||||
assert_eq!(ping_outcome_from_error(&e), PingOutcome::PermissionDenied);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_respects_docker_host() {
|
||||
assert_eq!(endpoint(Some("tcp://10.0.0.5:2375")), "tcp://10.0.0.5:2375");
|
||||
assert_eq!(endpoint(Some(" ")), endpoint(None));
|
||||
let default = endpoint(None);
|
||||
assert!(default.starts_with("unix://") || default.starts_with("npipe://"), "{default}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn install_markers_cover_each_os() {
|
||||
let home = Path::new("/home/u");
|
||||
let linux = install_markers("linux", Some(home), None);
|
||||
assert!(linux.contains(&PathBuf::from("/usr/bin/dockerd")));
|
||||
assert!(linux.contains(&PathBuf::from("/var/run/docker.sock")));
|
||||
assert!(linux.contains(&home.join(".docker/desktop")));
|
||||
|
||||
let mac = install_markers("macos", Some(home), None);
|
||||
assert!(mac.contains(&PathBuf::from("/Applications/Docker.app")));
|
||||
assert!(mac.contains(&home.join(".docker/run/docker.sock")));
|
||||
|
||||
let win = install_markers("windows", None, None);
|
||||
assert!(win.contains(&PathBuf::from(r"C:\Program Files\Docker\Docker\Docker Desktop.exe")));
|
||||
assert_eq!(path_binaries("windows"), &["docker.exe"]);
|
||||
assert!(path_binaries("linux").contains(&"dockerd"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unix_docker_host_socket_is_a_marker() {
|
||||
let m = install_markers("linux", None, Some("unix:///run/user/1000/docker.sock"));
|
||||
assert_eq!(m[0], PathBuf::from("/run/user/1000/docker.sock"));
|
||||
let tcp = install_markers("linux", None, Some("tcp://host:2375"));
|
||||
assert!(!tcp.iter().any(|p| p.to_string_lossy().contains("host")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_serializes_with_snake_case_state() {
|
||||
let s = DockerStatus {
|
||||
state: DockerState::NotRunning,
|
||||
endpoint: "unix:///var/run/docker.sock".into(),
|
||||
detail: Some("refused".into()),
|
||||
os: "linux",
|
||||
};
|
||||
let json = serde_json::to_value(&s).unwrap();
|
||||
assert_eq!(json["state"], "not_running");
|
||||
assert_eq!(json["os"], "linux");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
//! `ProjectHost::Aws`: a project's container is an ECS Fargate task in one
|
||||
//! of the account's environments, reached through an SSM port-forward to
|
||||
//! the in-task agent (`ssm.rs`, `agent.rs`).
|
||||
//! `ProjectHost::Aws`: a project's container is an ECS task in one of the
|
||||
//! account's environments — Fargate on the environment's cluster, or (phase
|
||||
//! 7, `LaunchType::Ec2`) on the user's own EC2 capacity, their per-user
|
||||
//! cluster from their record — reached through an SSM port-forward to the
|
||||
//! in-task agent (`ssm.rs`, `agent.rs`). A task's cluster is always read
|
||||
//! back from its ARN, so stop, reconcile and the watch need no record.
|
||||
//!
|
||||
//! Start: connection → `SdkConfig` → caller identity → environment record →
|
||||
//! onboarding → the project's live task, if there is one (its token is read
|
||||
@@ -27,27 +30,30 @@ use super::tunnels::{self, LiveTunnel, TunnelHandle};
|
||||
use super::watch::{AwsTaskLookup, TaskGroup, WatchedTask};
|
||||
use super::ContainerTarget;
|
||||
use crate::aws::environments::EnvironmentInfo;
|
||||
use crate::aws::environments::UserRecord;
|
||||
use crate::aws::runtime::{
|
||||
check_onboarded, cluster_arn_of_task, cluster_of_task, ensure_agent_token, read_agent_token,
|
||||
read_environment,
|
||||
check_onboarded, cluster_arn_of_task, cluster_of_task, ec2_cluster_of_task, project_task_clusters,
|
||||
ensure_agent_token, read_agent_token, read_environment, read_user_record,
|
||||
remove_workspace_resources, wait_access_point, PollTiming, RemoveRequest, RuntimeApi,
|
||||
SdkRuntimeApi,
|
||||
};
|
||||
use crate::aws::tasks::{
|
||||
find_or_run, find_project_task, stopped_among, task_log_stream, wait_ready, SdkTaskApi,
|
||||
StopKind, TaskApi, TaskInfo, TaskLogs, WaitTiming, LOG_TAIL_LINES, STOP_REASON,
|
||||
find_or_run, find_project_task, stopped_among, task_log_stream, wait_ready,
|
||||
waiting_for_instance, WAIT_TIMED_OUT, RunOn, SdkTaskApi, StopKind, TaskApi, TaskInfo, TaskLogs, WaitTiming,
|
||||
LOG_TAIL_LINES, STOP_REASON,
|
||||
};
|
||||
use crate::aws::tofu_modules::{
|
||||
environment_cluster_name, log_stream_prefix, owner_key, task_family,
|
||||
};
|
||||
use crate::aws::workspace::{
|
||||
gateway_sidecar_problem, new_agent_token, prepare_workspace, GatewayPayload, SdkWorkspaceApi,
|
||||
WorkspaceApi,
|
||||
gateway_sidecar_problem, new_agent_token, no_ec2_capacity, prepare_workspace, GatewayPayload,
|
||||
SdkWorkspaceApi, WorkspaceApi,
|
||||
};
|
||||
use crate::aws::{caller_identity, sdk_config, CallerIdentity};
|
||||
use crate::docker::ca_certs::{container_cert_name, ResolvedCa};
|
||||
use crate::models::app_settings::ImageSource;
|
||||
use crate::models::container_config::REGISTRY_IMAGE;
|
||||
use crate::models::project::LaunchType;
|
||||
use crate::models::{AppSettings, Project, ProjectStatus};
|
||||
use crate::storage::projects_store::ProjectsStore;
|
||||
use crate::storage::settings_store::SettingsStore;
|
||||
@@ -58,6 +64,8 @@ pub const PROGRESS_READING: &str = "Reading environment…";
|
||||
pub const PROGRESS_PREPARING: &str = "Preparing workspace…";
|
||||
pub const PROGRESS_STARTING: &str = "Starting task…";
|
||||
pub const PROGRESS_WAITING: &str = "Waiting for the task (about 2 minutes)…";
|
||||
/// An EC2 task waiting for an instance with room (capacity scaled to zero).
|
||||
pub const PROGRESS_INSTANCE: &str = "Starting an instance (about 3 minutes)…";
|
||||
pub const PROGRESS_CONNECTING: &str = "Connecting…";
|
||||
|
||||
/// A concurrent start won the RunTask race: the secret now holds a token
|
||||
@@ -137,16 +145,26 @@ fn is_registry_reference(name: &str) -> bool {
|
||||
/// [`aws_env`]'s answer: the pairs, and the resolved CA whose files ship too.
|
||||
pub type AwsEnv = (Vec<(String, String)>, Option<ResolvedCa>);
|
||||
|
||||
/// Whether Claude Code's sandbox (bubblewrap) may run where `project`'s task
|
||||
/// runs — the one place this is decided. Not on AWS: Fargate can't create
|
||||
/// namespaces (P9), and neither can an ECS task on the user's EC2 instance
|
||||
/// (E8, measured live in phase 7: `bwrap --unshare-user` fails with "No
|
||||
/// permissions to create new namespace" — Docker's default seccomp profile
|
||||
/// refuses it without CAP_SYS_ADMIN, though the AMI allows user namespaces).
|
||||
pub fn sandbox_allowed(_project: &Project) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// The environment pairs an AWS task gets: [`workspace_env`] with the same
|
||||
/// inputs Docker resolves, and Claude Code's sandbox forced off (R4: Fargate
|
||||
/// support for bubblewrap is unverified). Returns the resolved CA too.
|
||||
/// inputs Docker resolves, and Claude Code's sandbox forced off where
|
||||
/// [`sandbox_allowed`] says no. Returns the resolved CA too.
|
||||
///
|
||||
/// [`workspace_env`]: crate::docker::workspace_env::workspace_env
|
||||
pub fn aws_env(project: &Project, settings: &AppSettings) -> Result<AwsEnv, String> {
|
||||
let mut p = project.clone();
|
||||
if p.sandbox_mode_enabled {
|
||||
if p.sandbox_mode_enabled && !sandbox_allowed(project) {
|
||||
log::info!(
|
||||
"Project {}: Claude Code's sandbox is turned off on AWS (not yet supported on Fargate)",
|
||||
"Project {}: Claude Code's sandbox is turned off on AWS (bubblewrap can't run in an ECS task)",
|
||||
project.id
|
||||
);
|
||||
p.sandbox_mode_enabled = false;
|
||||
@@ -356,6 +374,25 @@ fn refusal_message(e: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// The edge image: what `main` builds, ahead of `:latest` between releases.
|
||||
const EDGE_IMAGE: &str = "ghcr.io/shadowdao/triple-c-sandbox:edge";
|
||||
|
||||
/// What the user is told when the agent never answers through the tunnel.
|
||||
/// The usual cause is an image whose entrypoint predates AWS mode and never
|
||||
/// starts the agent: the registry's `:latest` moves only on a release.
|
||||
/// `image` is the one the task ran, when the caller knows it.
|
||||
fn agent_silent_message(image: Option<&str>) -> String {
|
||||
let which = match image {
|
||||
Some(i) => format!("The image \"{}\"", i.escape_debug()),
|
||||
None => "The workspace's image".to_string(),
|
||||
};
|
||||
format!(
|
||||
"The workspace's agent did not answer through the tunnel. {which} may be older than this \
|
||||
app's AWS support: the registry's :latest moves only when a release is cut. Until then, \
|
||||
set Settings → Docker → Custom image to {EDGE_IMAGE}, then start the project again."
|
||||
)
|
||||
}
|
||||
|
||||
/// Wait for the agent behind `ep` to answer. A refusal (wrong token) is
|
||||
/// final; silence is retried until [`HEALTH_TIMEOUT`].
|
||||
async fn wait_healthy(ep: &AgentEndpoint) -> Result<(), String> {
|
||||
@@ -367,11 +404,7 @@ async fn wait_healthy(ep: &AgentEndpoint) -> Result<(), String> {
|
||||
Err(e) => return Err(refusal_message(&e)),
|
||||
}
|
||||
if tokio::time::Instant::now() + HEALTH_INTERVAL > deadline {
|
||||
return Err(
|
||||
"The workspace's agent did not answer through the tunnel. Check that the image \
|
||||
includes triple-c-agent, then start the project again."
|
||||
.into(),
|
||||
);
|
||||
return Err(agent_silent_message(None));
|
||||
}
|
||||
tokio::time::sleep(HEALTH_INTERVAL).await;
|
||||
}
|
||||
@@ -410,6 +443,9 @@ pub struct StartInputs<'a> {
|
||||
/// attaching to a running one never needs the provider key or models.
|
||||
pub gateway: &'a (dyn Fn() -> Result<Option<SidecarSecrets>, String> + Send + Sync),
|
||||
pub wait: WaitTiming,
|
||||
/// The wait for a task on the user's EC2 capacity (an instance may have
|
||||
/// to launch first: [`WaitTiming::ec2`]).
|
||||
pub ec2_wait: WaitTiming,
|
||||
pub access_point_wait: PollTiming,
|
||||
/// Called as soon as the project has a task (found, or RunTask
|
||||
/// returned) so the store holds its ARN before the long wait — a crash
|
||||
@@ -468,6 +504,236 @@ async fn stop_created<T: TaskApi>(api: &T, cluster: &str, arn: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for `arn` on `cluster`, telling the user when an EC2 task is waiting
|
||||
/// for an instance ([`PROGRESS_INSTANCE`], then [`PROGRESS_WAITING`] again
|
||||
/// once one has room). An EC2 task that never got an instance fails with
|
||||
/// what to check rather than ECS's last status.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn wait_task<T: TaskApi>(
|
||||
tasks: &T,
|
||||
cluster: &str,
|
||||
arn: &str,
|
||||
logs: &TaskLogs,
|
||||
timing: WaitTiming,
|
||||
ec2: Option<u32>,
|
||||
progress: &(dyn Fn(&str) + Send + Sync),
|
||||
) -> Result<TaskInfo, String> {
|
||||
let waiting = std::sync::atomic::AtomicBool::new(false);
|
||||
let r = wait_ready(tasks, cluster, arn, logs, timing, |t| {
|
||||
if ec2.is_none() {
|
||||
return;
|
||||
}
|
||||
let now = waiting_for_instance(t);
|
||||
if now != waiting.swap(now, std::sync::atomic::Ordering::Relaxed) {
|
||||
progress(if now { PROGRESS_INSTANCE } else { PROGRESS_WAITING });
|
||||
}
|
||||
})
|
||||
.await;
|
||||
match r {
|
||||
Ok(t) => Ok(t),
|
||||
Err(e)
|
||||
if waiting.load(std::sync::atomic::Ordering::Relaxed)
|
||||
&& e.to_string().starts_with(WAIT_TIMED_OUT) =>
|
||||
{
|
||||
let max = ec2.unwrap_or(1);
|
||||
Err(format!(
|
||||
"No EC2 instance had room for the task within {} minutes. Your capacity may \
|
||||
already be running its maximum of {max} instance{} (stop another EC2 \
|
||||
workspace), or its Auto Scaling group could not launch one (an admin can see \
|
||||
why in the group's activity in the EC2 console).",
|
||||
timing.timeout.as_secs() / 60,
|
||||
if max == 1 { "" } else { "s" }
|
||||
))
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// What a start says when ECS ran its task with ECS Exec off even with an
|
||||
/// instance up (it should not happen; the task can't be reached).
|
||||
pub const EXEC_OFF: &str = "ECS started the task with ECS Exec turned off, so Triple-C can't \
|
||||
reach it. Start the project again.";
|
||||
|
||||
/// The most a start waits for a task it stopped to reach STOPPED before
|
||||
/// running another (its own budget, apart from the placement wait).
|
||||
const STOP_WAIT: Duration = Duration::from_secs(3 * 60);
|
||||
|
||||
/// A start found the project's task with ECS Exec off and still waiting for
|
||||
/// an instance, recently started: another start is bringing one up.
|
||||
pub const INSTANCE_RACE: &str = "Another start of this workspace is bringing up an EC2 \
|
||||
instance; try again in a minute (once it is up, Start connects to it), or Stop the \
|
||||
workspace to cancel it";
|
||||
|
||||
/// The stop reason of a task a start replaced (one with ECS Exec off).
|
||||
pub const REPLACED_REASON: &str = "Replaced by another Triple-C start";
|
||||
|
||||
/// How long past the EC2 placement wait an exec-off task still waiting for
|
||||
/// an instance counts as another start's, before it is taken as orphaned.
|
||||
/// The task's age is ECS's `createdAt` against this machine's clock, so the
|
||||
/// margin also absorbs clock skew: a local clock more than this far ahead
|
||||
/// could take another start's waiting task for an orphan and replace it (the
|
||||
/// other start then says so: "Another start … replaced this one"); one far
|
||||
/// behind only delays the replacement. NTP-synced clocks are well inside it.
|
||||
const ORPHAN_MARGIN: Duration = Duration::from_secs(10 * 60);
|
||||
|
||||
/// Seconds since the Unix epoch on this machine's clock (0 if it is before).
|
||||
fn now_unix() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs() as i64)
|
||||
}
|
||||
|
||||
/// Whether an exec-off task still waiting for an instance is older than
|
||||
/// any start could still be waiting for it (`ec2_wait` + a margin): a
|
||||
/// crashed start's orphan. No creation time reads as fresh.
|
||||
fn is_orphaned(t: &TaskInfo, ec2_wait: WaitTiming, now_unix: i64) -> bool {
|
||||
let limit = (ec2_wait.timeout + ORPHAN_MARGIN).as_secs() as i64;
|
||||
t.created_unix.is_some_and(|c| now_unix - c > limit)
|
||||
}
|
||||
|
||||
/// Why [`rerun_once_placed`] did not give a new task.
|
||||
enum RerunError {
|
||||
/// A concurrent start's live task was found: record it, report
|
||||
/// [`START_RACE`].
|
||||
Race(TaskInfo),
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
impl From<String> for RerunError {
|
||||
fn from(e: String) -> Self {
|
||||
RerunError::Failed(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait (at most `min(timeout, STOP_WAIT)`) until `arn` is STOPPED or gone.
|
||||
async fn wait_stopped<T: TaskApi>(tasks: &T, cluster: &str, arn: &str, timing: WaitTiming) -> Result<(), String> {
|
||||
let deadline = tokio::time::Instant::now() + timing.timeout.min(STOP_WAIT);
|
||||
let wanted = [arn.to_string()];
|
||||
loop {
|
||||
let gone = tasks
|
||||
.describe(cluster, &wanted)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.into_iter()
|
||||
.find(|t| t.arn == arn)
|
||||
.is_none_or(|t| t.last_status == "STOPPED");
|
||||
if gone {
|
||||
return Ok(());
|
||||
}
|
||||
if tokio::time::Instant::now() + timing.interval > deadline {
|
||||
return Err(format!(
|
||||
"The task {arn} did not stop in time; start the project again in a minute."
|
||||
));
|
||||
}
|
||||
tokio::time::sleep(timing.interval).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// A cold EC2 start's second RunTask. ECS turns `enableExecuteCommand` off
|
||||
/// on a RunTask that has to wait for an instance (PROVISIONING; verified
|
||||
/// live, phase 7) and the task keeps it off once placed, so it could never
|
||||
/// be reached. That first task still makes the capacity provider launch an
|
||||
/// instance: once an instance has taken it (and it is still meant to run),
|
||||
/// it is stopped and the task run again — through [`find_or_run`], so a
|
||||
/// concurrent start's task is never doubled ([`START_RACE`]) — now straight
|
||||
/// onto that instance with ECS Exec. A first task that stops by itself (a
|
||||
/// user's Stop, a failed placement) ends the start with its reason; nothing
|
||||
/// is run again. Returns the new task.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn rerun_once_placed<T: TaskApi>(
|
||||
tasks: &T,
|
||||
env: &EnvironmentInfo,
|
||||
owner: &str,
|
||||
project_id: &str,
|
||||
compute: &crate::aws::environments::UserCompute,
|
||||
first: &str,
|
||||
task_def_arn: &str,
|
||||
timing: WaitTiming,
|
||||
progress: &(dyn Fn(&str) + Send + Sync),
|
||||
) -> Result<TaskInfo, RerunError> {
|
||||
let cluster = compute.cluster_arn.as_str();
|
||||
let deadline = tokio::time::Instant::now() + timing.timeout;
|
||||
let wanted = [first.to_string()];
|
||||
progress(PROGRESS_INSTANCE);
|
||||
// Until an instance has taken the first task.
|
||||
loop {
|
||||
let Some(t) = tasks
|
||||
.describe(cluster, &wanted)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.into_iter()
|
||||
.find(|t| t.arn == first)
|
||||
else {
|
||||
return Err(format!("The task {first} is no longer known to ECS; start the project again").into());
|
||||
};
|
||||
if t.desired_status == "STOPPED" || t.last_status == "STOPPED" {
|
||||
if t.stopped_reason.as_deref() == Some(REPLACED_REASON) {
|
||||
return Err(RerunError::Failed(
|
||||
"Another start of this workspace replaced this one; Start again in a minute \
|
||||
to connect to it."
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
return Err(format!(
|
||||
"The task stopped before an EC2 instance took it ({}); start the project again.",
|
||||
t.stopped_reason.as_deref().unwrap_or("no reason given")
|
||||
)
|
||||
.into());
|
||||
}
|
||||
if t.container_instance_arn.is_some() && t.desired_status == "RUNNING" {
|
||||
break;
|
||||
}
|
||||
if tokio::time::Instant::now() + timing.interval > deadline {
|
||||
let max = compute.max_instances;
|
||||
return Err(format!(
|
||||
"No EC2 instance had room for the task within {} minutes. Your capacity may \
|
||||
already be running its maximum of {max} instance{} (stop another EC2 \
|
||||
workspace), or its Auto Scaling group could not launch one (an admin can see \
|
||||
why in the group's activity in the EC2 console).",
|
||||
timing.timeout.as_secs() / 60,
|
||||
if max == 1 { "" } else { "s" }
|
||||
)
|
||||
.into());
|
||||
}
|
||||
tokio::time::sleep(timing.interval).await;
|
||||
}
|
||||
// Free its reservation before running again, or the second task would
|
||||
// wait for an instance too.
|
||||
tasks
|
||||
.stop_task(cluster, first, STOP_REASON)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
wait_stopped(tasks, cluster, first, timing).await?;
|
||||
progress(PROGRESS_WAITING);
|
||||
let start = find_or_run(tasks, env, owner, project_id, task_def_arn, RunOn::Ec2(compute))
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
if start.reused {
|
||||
return Err(RerunError::Race(start.task));
|
||||
}
|
||||
if !start.task.exec_enabled {
|
||||
let _ = stop_created(tasks, cluster, &start.task.arn).await;
|
||||
return Err(RerunError::Failed(EXEC_OFF.into()));
|
||||
}
|
||||
Ok(start.task)
|
||||
}
|
||||
|
||||
/// Where a project's task may be ([`project_task_clusters`]): the
|
||||
/// environment's cluster, the record's EC2 cluster, the stored task's.
|
||||
fn task_clusters(env: &EnvironmentInfo, record: Option<&UserRecord>, stored: Option<&str>) -> Vec<String> {
|
||||
project_task_clusters(
|
||||
&env.name,
|
||||
env.cluster_arn.clone(),
|
||||
record.and_then(UserRecord::valid).map(|c| c.cluster_arn.clone()),
|
||||
stored,
|
||||
)
|
||||
}
|
||||
|
||||
/// The cluster (ARN) a task runs on, from its ARN.
|
||||
fn task_cluster(env: &EnvironmentInfo, task_arn: &str) -> String {
|
||||
cluster_arn_of_task(task_arn).unwrap_or_else(|| env.cluster_arn.clone())
|
||||
}
|
||||
|
||||
/// The start sequence; see the module docs.
|
||||
pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector>(
|
||||
tasks: &T,
|
||||
@@ -496,9 +762,56 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
stream_prefix: log_stream_prefix(&ok, &project.id),
|
||||
};
|
||||
|
||||
let existing = find_project_task(tasks, &env.cluster_arn, &family, &inp.who.user_id, &project.id)
|
||||
.await
|
||||
.map_err(s)?;
|
||||
// EC2 (phase 7): the user's record says where new tasks run. Read it
|
||||
// only for EC2 projects; a Fargate start asks exactly what it did. A
|
||||
// failed read still lets a live task (found in the environment's
|
||||
// cluster or the stored task's) be attached; only a new run needs it.
|
||||
let (record, record_err) = if cfg.launch_type == LaunchType::Ec2 {
|
||||
match read_user_record(rt, &env.name, &ok, &inp.who.account, region).await {
|
||||
Ok(r) => (Some(r), None),
|
||||
Err(e) => {
|
||||
log::warn!("Project {}: could not read the EC2 record ({e})", project.id);
|
||||
(None, Some(format!("Could not read your EC2 capacity record: {e}")))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
// The EC2 wait (and its progress) for a task on a per-user cluster.
|
||||
let ec2_of = |task_arn: &str| {
|
||||
ec2_cluster_of_task(&env.name, task_arn).map(|_| {
|
||||
record.as_ref().and_then(UserRecord::valid).map_or(1, |c| c.max_instances)
|
||||
})
|
||||
};
|
||||
let timing_of = |ec2: Option<u32>| if ec2.is_some() { inp.ec2_wait } else { inp.wait };
|
||||
|
||||
let stored = cfg.task_arn.as_deref();
|
||||
let mut existing = None;
|
||||
for cluster in task_clusters(&env, record.as_ref(), stored) {
|
||||
existing = find_project_task(tasks, &cluster, &family, &inp.who.user_id, &project.id)
|
||||
.await
|
||||
.map_err(s)?;
|
||||
if existing.is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// A task with ECS Exec off (a cold EC2 start that stopped halfway) can
|
||||
// never be attached to: stop it and start afresh.
|
||||
// One still waiting for an instance belongs to a start in progress.
|
||||
// An old one no start can still be waiting for is an orphan (a crashed
|
||||
// start): it is replaced like a placed one.
|
||||
if let Some(t) = existing.take_if(|t| !t.exec_enabled) {
|
||||
if waiting_for_instance(&t) && !is_orphaned(&t, inp.ec2_wait, now_unix()) {
|
||||
return Err(INSTANCE_RACE.into());
|
||||
}
|
||||
log::warn!("Project {}: its task {} has ECS Exec off; stopping it", project.id, t.arn);
|
||||
let cluster = task_cluster(&env, &t.arn);
|
||||
tasks
|
||||
.stop_task(&cluster, &t.arn, REPLACED_REASON)
|
||||
.await
|
||||
.map_err(|e| format!("Could not stop the workspace's unreachable task {}: {e}", t.arn))?;
|
||||
wait_stopped(tasks, &cluster, &t.arn, timing_of(Some(1))).await?;
|
||||
}
|
||||
|
||||
let (task, token, access_point_id, workspace_access_point_id, created) = if let Some(task) = existing {
|
||||
// Attach: never rotate a running task's token (P2).
|
||||
@@ -511,12 +824,32 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
}));
|
||||
let token = read_agent_token(rt, &env.name, &ok, &project.id).await.map_err(s)?;
|
||||
progress(PROGRESS_WAITING);
|
||||
let task = match wait_ready(tasks, &env.cluster_arn, &task.arn, &logs, inp.wait, |_| {}).await {
|
||||
let ec2 = ec2_of(&task.arn);
|
||||
let cluster = task_cluster(&env, &task.arn);
|
||||
let task = match wait_task(tasks, &cluster, &task.arn, &logs, timing_of(ec2), ec2, progress).await {
|
||||
Ok(t) => t,
|
||||
Err(e) => return Err(with_log_tail(tasks, &logs, &task.arn, e.to_string()).await),
|
||||
Err(e) => return Err(with_log_tail(tasks, &logs, &task.arn, e).await),
|
||||
};
|
||||
(task, token, None, None, false)
|
||||
} else {
|
||||
// Where the new task runs; an EC2 project without usable capacity
|
||||
// (or whose record could not be read) fails here, before anything
|
||||
// is written.
|
||||
if let Some(e) = record_err {
|
||||
return Err(e);
|
||||
}
|
||||
let run_on = match &record {
|
||||
None => RunOn::Fargate { spot: cfg.spot },
|
||||
Some(UserRecord::Valid(c)) => RunOn::Ec2(c),
|
||||
Some(UserRecord::Absent) => return Err(no_ec2_capacity(&env.name)),
|
||||
Some(UserRecord::Unusable(why)) => {
|
||||
return Err(format!(
|
||||
"Your EC2 capacity record in {} can't be used ({why}); ask an admin to \
|
||||
re-apply your onboarding.",
|
||||
env.name
|
||||
))
|
||||
}
|
||||
};
|
||||
progress(PROGRESS_PREPARING);
|
||||
let sidecar = (inp.gateway)()?;
|
||||
let prepared = prepare_workspace(
|
||||
@@ -529,6 +862,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
inp.custom_env_keys,
|
||||
inp.image,
|
||||
sidecar.as_ref().map(SidecarSecrets::payload),
|
||||
record.as_ref().and_then(UserRecord::valid),
|
||||
)
|
||||
.await
|
||||
.map_err(s)?;
|
||||
@@ -545,7 +879,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
&inp.who.user_id,
|
||||
&project.id,
|
||||
&prepared.task_definition_arn,
|
||||
cfg.spot,
|
||||
run_on,
|
||||
)
|
||||
.await
|
||||
.map_err(s)?;
|
||||
@@ -560,19 +894,47 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
}));
|
||||
return Err(START_RACE.into());
|
||||
}
|
||||
let arn = start.task.arn.clone();
|
||||
(inp.on_task)(Some(&TaskRecord {
|
||||
task_arn: arn.clone(),
|
||||
access_point_id: prepared.access_point_id.clone(),
|
||||
task_definition_arn: Some(prepared.task_definition_arn.clone()),
|
||||
workspace_access_point_id: prepared.workspace_access_point_id.clone(),
|
||||
}));
|
||||
let mut arn = start.task.arn.clone();
|
||||
let record_task = |arn: &str| {
|
||||
(inp.on_task)(Some(&TaskRecord {
|
||||
task_arn: arn.to_string(),
|
||||
access_point_id: prepared.access_point_id.clone(),
|
||||
task_definition_arn: Some(prepared.task_definition_arn.clone()),
|
||||
workspace_access_point_id: prepared.workspace_access_point_id.clone(),
|
||||
}))
|
||||
};
|
||||
record_task(&arn);
|
||||
progress(PROGRESS_WAITING);
|
||||
let task = match wait_ready(tasks, &env.cluster_arn, &arn, &logs, inp.wait, |_| {}).await {
|
||||
let cluster = run_on.cluster(&env).to_string();
|
||||
if let (RunOn::Ec2(compute), false) = (run_on, start.task.exec_enabled) {
|
||||
match rerun_once_placed(tasks, &env, &inp.who.user_id, &project.id, compute, &arn, &prepared.task_definition_arn, inp.ec2_wait, progress).await {
|
||||
Ok(t) => {
|
||||
arn = t.arn;
|
||||
record_task(&arn);
|
||||
}
|
||||
Err(RerunError::Race(winner)) => {
|
||||
// As on the first launch: the winner's task is the project's.
|
||||
record_task(&winner.arn);
|
||||
return Err(START_RACE.into());
|
||||
}
|
||||
Err(RerunError::Failed(e)) => {
|
||||
let msg = with_log_tail(tasks, &logs, &arn, e).await;
|
||||
if stop_created(tasks, &cluster, &arn).await {
|
||||
(inp.on_task)(None);
|
||||
}
|
||||
return Err(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
let ec2 = match run_on {
|
||||
RunOn::Ec2(c) => Some(c.max_instances),
|
||||
RunOn::Fargate { .. } => None,
|
||||
};
|
||||
let task = match wait_task(tasks, &cluster, &arn, &logs, timing_of(ec2), ec2, progress).await {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
let msg = with_log_tail(tasks, &logs, &arn, e.to_string()).await;
|
||||
if stop_created(tasks, &env.cluster_arn, &arn).await {
|
||||
let msg = with_log_tail(tasks, &logs, &arn, e).await;
|
||||
if stop_created(tasks, &cluster, &arn).await {
|
||||
(inp.on_task)(None);
|
||||
}
|
||||
return Err(msg);
|
||||
@@ -617,10 +979,11 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
if created && stop_created(tasks, &env.cluster_arn, &task.arn).await {
|
||||
if created && stop_created(tasks, &task_cluster(&env, &task.arn), &task.arn).await {
|
||||
(inp.on_task)(None);
|
||||
}
|
||||
Err(e)
|
||||
// Name the image this start ran (the connector doesn't know it).
|
||||
Err(if e == agent_silent_message(None) { agent_silent_message(Some(inp.image)) } else { e })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -654,6 +1017,60 @@ pub async fn environment_max_hours(project: &Project, settings: &AppSettings) ->
|
||||
Some(info.max_workspace_hours)
|
||||
}
|
||||
|
||||
/// What Host settings know of the user's EC2 capacity in an AWS project's
|
||||
/// environment (phase 7): the launch-type control offers EC2 only with
|
||||
/// `Ec2`, and sizes the reservations against `available_*`
|
||||
/// ([`ec2_available`]; `None` for a type the app's table doesn't know).
|
||||
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
|
||||
#[serde(tag = "state", rename_all = "snake_case")]
|
||||
pub enum Ec2Capacity {
|
||||
Ec2 {
|
||||
compute: crate::aws::environments::UserCompute,
|
||||
available_cpu: Option<u32>,
|
||||
available_memory_mib: Option<u32>,
|
||||
},
|
||||
/// No user record: no EC2 capacity in this environment.
|
||||
None,
|
||||
/// A record that can't be used (the reason names a field, never a value).
|
||||
Unusable { reason: String },
|
||||
/// It couldn't be read (offline, no connection, slow, a local project).
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// [`Ec2Capacity`] from a read of the user record (`None`: no read).
|
||||
pub fn ec2_capacity_from(read: Option<UserRecord>) -> Ec2Capacity {
|
||||
match read {
|
||||
Some(UserRecord::Valid(compute)) => {
|
||||
let available = crate::models::project::ec2_available(&compute.instance_type);
|
||||
Ec2Capacity::Ec2 {
|
||||
available_cpu: available.map(|a| a.0),
|
||||
available_memory_mib: available.map(|a| a.1),
|
||||
compute,
|
||||
}
|
||||
}
|
||||
Some(UserRecord::Absent) => Ec2Capacity::None,
|
||||
Some(UserRecord::Unusable(reason)) => Ec2Capacity::Unusable { reason },
|
||||
None => Ec2Capacity::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
/// The caller's EC2 capacity in `project`'s environment, from their user
|
||||
/// record. Never an error: anything that stops the read (10 s bound) is
|
||||
/// [`Ec2Capacity::Unknown`].
|
||||
pub async fn ec2_capacity(project: &Project, settings: &AppSettings) -> Ec2Capacity {
|
||||
let read = async {
|
||||
let placement = project.aws_placement.as_ref()?;
|
||||
let (_, region, sdk) = project_sdk(project, settings).await.ok()?;
|
||||
let who = caller_identity(&sdk).await.ok()?;
|
||||
let ok = owner_key(®ion, &who.user_id);
|
||||
read_user_record(&SdkRuntimeApi::new(&sdk), &placement.environment, &ok, &who.account, ®ion)
|
||||
.await
|
||||
.map_err(|e| log::info!("Project {}: could not read the EC2 record ({e})", project.id))
|
||||
.ok()
|
||||
};
|
||||
ec2_capacity_from(tokio::time::timeout(std::time::Duration::from_secs(10), read).await.ok().flatten())
|
||||
}
|
||||
|
||||
/// `ProjectHost::Aws::ensure_started`.
|
||||
pub async fn ensure_started(
|
||||
project: &Project,
|
||||
@@ -730,6 +1147,7 @@ pub async fn ensure_started_with(
|
||||
image: &image,
|
||||
gateway: &sidecar,
|
||||
wait: WaitTiming::default(),
|
||||
ec2_wait: WaitTiming::ec2(),
|
||||
access_point_wait: PollTiming::default(),
|
||||
on_task: &on_task,
|
||||
},
|
||||
@@ -756,7 +1174,7 @@ pub async fn ensure_started_with(
|
||||
LiveTunnel {
|
||||
task: ecs,
|
||||
task_arn: started.task.arn.clone(),
|
||||
cluster_arn: started.env.cluster_arn.clone(),
|
||||
cluster_arn: task_cluster(&started.env, &started.task.arn),
|
||||
connection_id,
|
||||
region,
|
||||
sdk,
|
||||
@@ -903,22 +1321,28 @@ pub async fn stop_endpoint(ep: &AgentEndpoint) -> Option<Result<(), String>> {
|
||||
/// Where to look for a project's live task when no ARN is stored (a crash
|
||||
/// before the store write, or a store from an older build).
|
||||
pub struct FindTask {
|
||||
pub cluster: String,
|
||||
/// The environment's cluster, then the user's EC2 cluster when known.
|
||||
pub clusters: Vec<String>,
|
||||
pub family: String,
|
||||
pub owner_user_id: String,
|
||||
pub project_id: String,
|
||||
}
|
||||
|
||||
impl FindTask {
|
||||
fn new(project: &Project, region: &str, owner_user_id: &str) -> Self {
|
||||
/// `ec2_cluster`: the user's EC2 cluster from their record, if they
|
||||
/// have one ([`user_ec2_cluster`]); the stored task's per-user cluster
|
||||
/// is added either way.
|
||||
fn new(project: &Project, region: &str, owner_user_id: &str, ec2_cluster: Option<String>) -> Self {
|
||||
let env = project
|
||||
.aws_placement
|
||||
.as_ref()
|
||||
.map(|p| p.environment.clone())
|
||||
.unwrap_or_default();
|
||||
let ok = owner_key(region, owner_user_id);
|
||||
let stored = project.aws.as_ref().and_then(|a| a.task_arn.as_deref());
|
||||
let clusters = project_task_clusters(&env, environment_cluster_name(&env), ec2_cluster, stored);
|
||||
FindTask {
|
||||
cluster: environment_cluster_name(&env),
|
||||
clusters,
|
||||
family: task_family(&env, &ok, &project.id),
|
||||
owner_user_id: owner_user_id.to_string(),
|
||||
project_id: project.id.clone(),
|
||||
@@ -926,6 +1350,43 @@ impl FindTask {
|
||||
}
|
||||
}
|
||||
|
||||
/// The project's live task in any of `find`'s clusters.
|
||||
async fn find_in_clusters<T: TaskApi>(api: &T, find: &FindTask) -> Result<Option<TaskInfo>, String> {
|
||||
for cluster in &find.clusters {
|
||||
if let Some(t) =
|
||||
find_project_task(api, cluster, &find.family, &find.owner_user_id, &find.project_id)
|
||||
.await
|
||||
.map_err(s)?
|
||||
{
|
||||
return Ok(Some(t));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// The user's EC2 cluster (ARN) for an EC2 project, from their record; `None`
|
||||
/// for a Fargate project, or when there is no usable record (best effort:
|
||||
/// only used to look for a task no ARN was stored for).
|
||||
pub async fn user_ec2_cluster<R: RuntimeApi>(
|
||||
rt: &R,
|
||||
project: &Project,
|
||||
who: &CallerIdentity,
|
||||
region: &str,
|
||||
) -> Option<String> {
|
||||
if !project.aws.as_ref().is_some_and(|a| a.launch_type == LaunchType::Ec2) {
|
||||
return None;
|
||||
}
|
||||
let env = &project.aws_placement.as_ref()?.environment;
|
||||
match read_user_record(rt, env, &owner_key(region, &who.user_id), &who.account, region).await {
|
||||
Ok(UserRecord::Valid(c)) => Some(c.cluster_arn),
|
||||
Ok(_) => None,
|
||||
Err(e) => {
|
||||
log::warn!("Project {}: could not read the EC2 record ({e})", project.id);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The project's task ARN: the stored one, else its live task's.
|
||||
async fn task_arn_or_find<T: TaskApi>(
|
||||
api: &T,
|
||||
@@ -935,12 +1396,7 @@ async fn task_arn_or_find<T: TaskApi>(
|
||||
if let Some(a) = stored {
|
||||
return Ok(Some(a.to_string()));
|
||||
}
|
||||
Ok(
|
||||
find_project_task(api, &find.cluster, &find.family, &find.owner_user_id, &find.project_id)
|
||||
.await
|
||||
.map_err(s)?
|
||||
.map(|t| t.arn),
|
||||
)
|
||||
Ok(find_in_clusters(api, find).await?.map(|t| t.arn))
|
||||
}
|
||||
|
||||
/// Stop an AWS project's task: through its tunnel when there is one, else
|
||||
@@ -960,7 +1416,8 @@ pub async fn stop_project(project: &Project, settings: &AppSettings) -> Result<(
|
||||
Some(a) => Some(a),
|
||||
None => {
|
||||
let who = caller_identity(&sdk).await.map_err(s)?;
|
||||
task_arn_or_find(&api, None, &FindTask::new(project, ®ion, &who.user_id)).await?
|
||||
let ec2 = user_ec2_cluster(&SdkRuntimeApi::new(&sdk), project, &who, ®ion).await;
|
||||
task_arn_or_find(&api, None, &FindTask::new(project, ®ion, &who.user_id, ec2)).await?
|
||||
}
|
||||
};
|
||||
if let Some(arn) = arn {
|
||||
@@ -987,10 +1444,12 @@ pub async fn remove_resources(
|
||||
let ok = owner_key(®ion, &who.user_id);
|
||||
let env = project.aws_placement.as_ref().map(|p| p.environment.clone()).unwrap_or_default();
|
||||
let aws = project.aws.as_ref();
|
||||
let rt = SdkRuntimeApi::new(&sdk);
|
||||
let ec2_cluster = user_ec2_cluster(&rt, project, &who, ®ion).await;
|
||||
remove_workspace_resources(
|
||||
&SdkTaskApi::new(&sdk),
|
||||
&SdkWorkspaceApi::new(&sdk),
|
||||
&SdkRuntimeApi::new(&sdk),
|
||||
&rt,
|
||||
&RemoveRequest {
|
||||
env: &env,
|
||||
region: ®ion,
|
||||
@@ -1001,6 +1460,7 @@ pub async fn remove_resources(
|
||||
stored_access_point_id: aws.and_then(|a| a.access_point_id.as_deref()),
|
||||
stored_workspace_access_point_id: aws
|
||||
.and_then(|a| a.workspace_access_point_id.as_deref()),
|
||||
ec2_cluster: ec2_cluster.as_deref(),
|
||||
discard_data,
|
||||
},
|
||||
)
|
||||
@@ -1018,9 +1478,20 @@ pub enum Reconciled {
|
||||
/// The task is up (or on its way): re-open the tunnel.
|
||||
Live(Box<TaskInfo>),
|
||||
/// The task stopped, or ECS no longer knows it: the project is Stopped.
|
||||
/// Also an EC2 task with ECS Exec off that could never be attached to
|
||||
/// (placed, or orphaned): reconcile stopped it.
|
||||
Gone,
|
||||
/// An EC2 task with ECS Exec off, still waiting for an instance and
|
||||
/// recent: a start (perhaps this app's, before a restart) was
|
||||
/// interrupted mid cold start. The project is Error, its ARN kept so
|
||||
/// Stop still reaches the task; [`INTERRUPTED`] says what to do.
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
/// What reconcile says about [`Reconciled::Interrupted`].
|
||||
pub const INTERRUPTED: &str = "A start of this workspace was interrupted while its EC2 \
|
||||
instance came up; Start again (or Stop it).";
|
||||
|
||||
/// Ask ECS about a project's task: the stored ARN, else (none stored) its
|
||||
/// live task found by family and tags.
|
||||
pub async fn reconcile_task<T: TaskApi>(
|
||||
@@ -1041,11 +1512,9 @@ pub async fn reconcile_task<T: TaskApi>(
|
||||
// or a crash before the store write) may have left the project a newer
|
||||
// one: look before calling it Gone (m3).
|
||||
if task_arn.is_some() {
|
||||
if let Some(t) =
|
||||
find_project_task(api, &find.cluster, &find.family, &find.owner_user_id, &find.project_id)
|
||||
.await
|
||||
.map_err(s)?
|
||||
.filter(|t| t.arn != arn && is_live(t))
|
||||
if let Some(t) = find_in_clusters(api, find)
|
||||
.await?
|
||||
.filter(|t| t.arn != arn && is_live(t))
|
||||
{
|
||||
return Ok(Reconciled::Live(Box::new(t)));
|
||||
}
|
||||
@@ -1100,7 +1569,33 @@ pub async fn reconcile_project<T: TaskApi>(
|
||||
find: &FindTask,
|
||||
) -> Result<Reconciled, String> {
|
||||
let arn = project.aws.as_ref().and_then(|a| a.task_arn.clone());
|
||||
let r = reconcile_task(api, arn.as_deref(), find).await?;
|
||||
let mut r = reconcile_task(api, arn.as_deref(), find).await?;
|
||||
// A task with ECS Exec off can never be attached to (a cold EC2 start
|
||||
// the app was closed during): never Running. Placed, or too old for any
|
||||
// start to still wait for it → stop it (then it's Gone); still waiting
|
||||
// and recent → Interrupted, left for a Start or Stop.
|
||||
if let Reconciled::Live(t) = &r {
|
||||
if !t.exec_enabled {
|
||||
if waiting_for_instance(t) && !is_orphaned(t, WaitTiming::ec2(), now_unix()) {
|
||||
if arn.as_deref() != Some(t.arn.as_str()) {
|
||||
record_task(store, &project.id, Some(&live_record(t)));
|
||||
}
|
||||
store.update_status(&project.id, ProjectStatus::Error)?;
|
||||
return Ok(Reconciled::Interrupted);
|
||||
}
|
||||
log::warn!("Project {}: its task {} has ECS Exec off; stopping it", project.id, t.arn);
|
||||
let cluster = cluster_of_task(&t.arn).ok_or_else(|| format!("{} is not a task ARN with a cluster", t.arn))?;
|
||||
if let Err(e) = bounded_stop(api.stop_task(cluster, &t.arn, REPLACED_REASON)).await {
|
||||
// It may still run: keep (or record) its ARN so Stop finds it.
|
||||
if arn.as_deref() != Some(t.arn.as_str()) {
|
||||
record_task(store, &project.id, Some(&live_record(t)));
|
||||
}
|
||||
store.update_status(&project.id, ProjectStatus::Error)?;
|
||||
return Err(format!("Could not stop the workspace's unreachable task {}: {e}", t.arn));
|
||||
}
|
||||
r = Reconciled::Gone;
|
||||
}
|
||||
}
|
||||
match &r {
|
||||
Reconciled::Gone => {
|
||||
store.update_status(&project.id, ProjectStatus::Stopped)?;
|
||||
@@ -1108,37 +1603,42 @@ pub async fn reconcile_project<T: TaskApi>(
|
||||
}
|
||||
Reconciled::Live(t) => {
|
||||
if arn.as_deref() != Some(t.arn.as_str()) {
|
||||
record_task(
|
||||
store,
|
||||
&project.id,
|
||||
Some(&TaskRecord {
|
||||
task_arn: t.arn.clone(),
|
||||
access_point_id: None,
|
||||
task_definition_arn: Some(t.task_definition_arn.clone()),
|
||||
workspace_access_point_id: None,
|
||||
}),
|
||||
);
|
||||
record_task(store, &project.id, Some(&live_record(t)));
|
||||
}
|
||||
if project.status != ProjectStatus::Running {
|
||||
store.update_status(&project.id, ProjectStatus::Running)?;
|
||||
}
|
||||
}
|
||||
Reconciled::Interrupted => {}
|
||||
}
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
/// The store record of a live task reconcile found (no access points: the
|
||||
/// stored ones stand).
|
||||
fn live_record(t: &TaskInfo) -> TaskRecord {
|
||||
TaskRecord {
|
||||
task_arn: t.arn.clone(),
|
||||
access_point_id: None,
|
||||
task_definition_arn: Some(t.task_definition_arn.clone()),
|
||||
workspace_access_point_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `reconcile_project_statuses` for an AWS project: decide, and when the
|
||||
/// task is live re-open its tunnel in the background (terminals wait for
|
||||
/// it through [`tunnels::wait_endpoint`]), then — copy-at-connect — copy the
|
||||
/// credentials in, calling `on_connect_warning` if that fails — after
|
||||
/// opening its port mappings (`on_ports_warning` for any that fail). Never
|
||||
/// blocks on the tunnel.
|
||||
/// blocks on the tunnel. `on_interrupted_warning` reports a start that an
|
||||
/// app restart interrupted ([`INTERRUPTED`]), on its own toast topic.
|
||||
pub async fn reconcile(
|
||||
project: &Project,
|
||||
settings: &AppSettings,
|
||||
store: Arc<ProjectsStore>,
|
||||
on_connect_warning: impl FnOnce(String) + Send + 'static,
|
||||
on_ports_warning: impl FnOnce(String) + Send + 'static,
|
||||
on_ports_warning: impl Fn(String) + Send + Sync + 'static,
|
||||
on_interrupted_warning: impl FnOnce(String) + Send + 'static,
|
||||
) {
|
||||
let (connection_id, region, sdk) = match project_sdk(project, settings).await {
|
||||
Ok(v) => v,
|
||||
@@ -1155,7 +1655,8 @@ pub async fn reconcile(
|
||||
}
|
||||
};
|
||||
let api = SdkTaskApi::new(&sdk);
|
||||
let find = FindTask::new(project, ®ion, &who.user_id);
|
||||
let ec2 = user_ec2_cluster(&SdkRuntimeApi::new(&sdk), project, &who, ®ion).await;
|
||||
let find = FindTask::new(project, ®ion, &who.user_id, ec2);
|
||||
match reconcile_project(&api, &store, project, &find).await {
|
||||
Ok(Reconciled::Live(task)) => {
|
||||
let generation = tunnels::begin_reattach(&project.id);
|
||||
@@ -1167,8 +1668,11 @@ pub async fn reconcile(
|
||||
Ok(true) => {
|
||||
// Q6: the port mappings' forwards, before the
|
||||
// (slower) credential copy.
|
||||
if let Some(detail) = super::port_forwards::open_for_project(&store, &project.id).await {
|
||||
on_ports_warning(detail);
|
||||
let warn: super::port_forwards::PortsWarn = Arc::new(on_ports_warning);
|
||||
if let Some(detail) =
|
||||
super::port_forwards::open_for_project(&store, &project.id, Some(warn.clone())).await
|
||||
{
|
||||
warn(detail);
|
||||
}
|
||||
if let Err(e) = connect::apply_connect(&project, &settings).await {
|
||||
log::warn!("Project {}: could not copy the credentials in: {e}", project.id);
|
||||
@@ -1188,6 +1692,10 @@ pub async fn reconcile(
|
||||
Ok(Reconciled::Gone) => {
|
||||
log::info!("Project '{}' ({}): its task is gone — set to Stopped", project.name, project.id)
|
||||
}
|
||||
Ok(Reconciled::Interrupted) => {
|
||||
log::info!("Project '{}' ({}): {INTERRUPTED}", project.name, project.id);
|
||||
on_interrupted_warning(INTERRUPTED.to_string());
|
||||
}
|
||||
Err(e) => log::warn!("Project {}: could not check its task ({e}); status left as is", project.id),
|
||||
}
|
||||
}
|
||||
@@ -1227,14 +1735,26 @@ async fn reattach(
|
||||
.map_err(s)?;
|
||||
let token = read_agent_token(&rt, &env.name, &ok, &project.id).await.map_err(s)?;
|
||||
let logs = TaskLogs { group: env.log_group.clone(), stream_prefix: log_stream_prefix(&ok, &project.id) };
|
||||
let task = wait_ready(&api, &env.cluster_arn, &task.arn, &logs, WaitTiming::default(), |_| {})
|
||||
// An EC2 task may still be waiting for its instance after a restart.
|
||||
let cluster = task_cluster(&env, &task.arn);
|
||||
let timing = if ec2_cluster_of_task(&env.name, &task.arn).is_some() {
|
||||
WaitTiming::ec2()
|
||||
} else {
|
||||
WaitTiming::default()
|
||||
};
|
||||
let task = wait_ready(&api, &cluster, &task.arn, &logs, timing, |_| {})
|
||||
.await
|
||||
.map_err(s)?;
|
||||
// Reconcile never hands over an exec-off task; never wait on a tunnel
|
||||
// ECS Exec can't open either.
|
||||
if !task.exec_enabled {
|
||||
return Err(format!("The task {} has ECS Exec off; Start the project again", task.arn));
|
||||
}
|
||||
let (forward, endpoint) = SsmConnector { sdk: ctx.sdk.clone() }.connect(&task, &token).await?;
|
||||
let tunnel = LiveTunnel {
|
||||
task: ecs_task(&task)?,
|
||||
task_arn: task.arn.clone(),
|
||||
cluster_arn: env.cluster_arn.clone(),
|
||||
cluster_arn: cluster,
|
||||
connection_id: ctx.connection_id,
|
||||
region: ctx.region,
|
||||
sdk: ctx.sdk,
|
||||
|
||||
@@ -56,6 +56,9 @@ fn task(p: &Project, id: &str, last: &str, ready: bool) -> TaskInfo {
|
||||
stopped_reason: (last == "STOPPED").then(|| "Essential container exited".into()),
|
||||
task_definition_arn: format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task-definition/{}:1", family(p)),
|
||||
runtime_id: ready.then(|| "rt-1".into()),
|
||||
container_instance_arn: None,
|
||||
exec_enabled: true,
|
||||
created_unix: None,
|
||||
exec_agent_running: ready,
|
||||
exit_code: None,
|
||||
owner: Some(USER.into()),
|
||||
@@ -85,19 +88,40 @@ struct Fake {
|
||||
tasks: Mutex<HashMap<String, TaskInfo>>,
|
||||
/// What RunTask starts.
|
||||
run: Option<TaskInfo>,
|
||||
/// When non-empty, what successive RunTasks start (before `run`).
|
||||
runs: Mutex<Vec<TaskInfo>>,
|
||||
logs: Vec<String>,
|
||||
role: Option<Knowable>,
|
||||
connect_fails: bool,
|
||||
/// The connector times out waiting for the agent (an image without it).
|
||||
agent_silent: bool,
|
||||
connect_tokens: Mutex<Vec<String>>,
|
||||
secret_json: Mutex<Option<String>>,
|
||||
/// A losing start rotates the secret's token while this one connects.
|
||||
rotated_by_loser: bool,
|
||||
/// StopTask is refused (expired credentials, say).
|
||||
stop_fails: bool,
|
||||
/// StopTask is accepted but the task never reaches STOPPED.
|
||||
stop_noop: bool,
|
||||
/// What `on_task` was told, in order.
|
||||
recorded: Mutex<Vec<Option<String>>>,
|
||||
/// `group stream limit` of each log read.
|
||||
log_reads: Mutex<Vec<String>>,
|
||||
/// The caller's EC2 record (absent by default).
|
||||
user_record: Option<crate::aws::environments::Param>,
|
||||
/// Reading the EC2 record fails (throttled, say).
|
||||
user_record_fails: bool,
|
||||
/// RunTask requests, in order.
|
||||
run_requests: Mutex<Vec<RunTaskInput>>,
|
||||
/// The clusters ListTasks / StopTask / DescribeTasks were asked about.
|
||||
list_clusters: Mutex<Vec<String>>,
|
||||
stop_clusters: Mutex<Vec<String>>,
|
||||
describe_clusters: Mutex<Vec<String>>,
|
||||
/// When set, ListTasks answers per cluster (empty for any other).
|
||||
listed_by_cluster: Option<HashMap<String, Vec<String>>>,
|
||||
/// Successive states of the run task, one per DescribeTasks (the last
|
||||
/// repeats); overrides `tasks` for that ARN.
|
||||
script: Mutex<Vec<TaskInfo>>,
|
||||
}
|
||||
|
||||
impl Fake {
|
||||
@@ -117,26 +141,54 @@ impl Fake {
|
||||
}
|
||||
|
||||
impl TaskApi for Fake {
|
||||
async fn run_task(&self, _req: RunTaskInput) -> Result<TaskInfo, AwsError> {
|
||||
async fn run_task(&self, req: RunTaskInput) -> Result<TaskInfo, AwsError> {
|
||||
self.log("run");
|
||||
let t = self.run.clone().expect("a run was expected");
|
||||
self.run_requests.lock().unwrap().push(req);
|
||||
let queued = {
|
||||
let mut q = self.runs.lock().unwrap();
|
||||
(!q.is_empty()).then(|| q.remove(0))
|
||||
};
|
||||
let t = queued.or_else(|| self.run.clone()).expect("a run was expected");
|
||||
self.tasks.lock().unwrap().insert(t.arn.clone(), t.clone());
|
||||
Ok(t)
|
||||
}
|
||||
async fn describe(&self, _c: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
|
||||
async fn describe(&self, c: &str, arns: &[String]) -> Result<Vec<TaskInfo>, AwsError> {
|
||||
self.describe_clusters.lock().unwrap().push(c.into());
|
||||
{
|
||||
let mut script = self.script.lock().unwrap();
|
||||
if let Some(first) = script.first().cloned().filter(|t| arns.contains(&t.arn)) {
|
||||
if script.len() > 1 {
|
||||
script.remove(0);
|
||||
}
|
||||
return Ok(vec![first]);
|
||||
}
|
||||
}
|
||||
let t = self.tasks.lock().unwrap();
|
||||
Ok(arns.iter().filter_map(|a| t.get(a).cloned()).collect())
|
||||
}
|
||||
async fn list_project_tasks(&self, _c: &str, _f: &str) -> Result<Vec<String>, AwsError> {
|
||||
async fn list_project_tasks(&self, c: &str, _f: &str) -> Result<Vec<String>, AwsError> {
|
||||
self.log("list");
|
||||
self.list_clusters.lock().unwrap().push(c.into());
|
||||
if let Some(by) = &self.listed_by_cluster {
|
||||
return Ok(by.get(c).cloned().unwrap_or_default());
|
||||
}
|
||||
let mut l = self.listed.lock().unwrap();
|
||||
Ok(if l.len() > 1 { l.remove(0) } else { l.first().cloned().unwrap_or_default() })
|
||||
}
|
||||
async fn stop_task(&self, _c: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
|
||||
async fn stop_task(&self, c: &str, arn: &str, reason: &str) -> Result<(), AwsError> {
|
||||
self.log(format!("stop {arn} {reason}"));
|
||||
self.stop_clusters.lock().unwrap().push(c.into());
|
||||
if self.stop_fails {
|
||||
return Err(AwsError::Other { message: "ExpiredToken".into() });
|
||||
}
|
||||
// As ECS does: a stopped task is no longer live.
|
||||
if self.stop_noop {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(t) = self.tasks.lock().unwrap().get_mut(arn) {
|
||||
t.desired_status = "STOPPED".into();
|
||||
t.last_status = "STOPPED".into();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn log_tail(&self, g: &str, st: &str, l: i32) -> Result<Vec<String>, AwsError> {
|
||||
@@ -213,6 +265,13 @@ impl RuntimeApi for Fake {
|
||||
self.log(format!("record {env}"));
|
||||
Ok(Some(record()))
|
||||
}
|
||||
async fn user_record(&self, env: &str, _owner_key: &str) -> Result<crate::aws::environments::Param, AwsError> {
|
||||
self.log(format!("user-record {env}"));
|
||||
if self.user_record_fails {
|
||||
return Err(AwsError::Other { message: "Throttling".into() });
|
||||
}
|
||||
Ok(self.user_record.clone().unwrap_or(crate::aws::environments::Param::Missing))
|
||||
}
|
||||
async fn role_exists(&self, _name: &str) -> Result<Knowable, AwsError> {
|
||||
self.log("role");
|
||||
Ok(self.role.clone().unwrap_or(Knowable::Yes))
|
||||
@@ -252,6 +311,9 @@ impl Connector for Fake {
|
||||
v["TRIPLEC_AGENT_TOKEN"] = "the-losers-token".into();
|
||||
*j = Some(v.to_string());
|
||||
}
|
||||
if self.agent_silent {
|
||||
return Err(agent_silent_message(None));
|
||||
}
|
||||
if self.connect_fails {
|
||||
return Err("session-manager-plugin was not found".into());
|
||||
}
|
||||
@@ -284,6 +346,10 @@ fn inputs<'a>(
|
||||
interval: Duration::from_millis(1),
|
||||
timeout: Duration::from_millis(30),
|
||||
},
|
||||
ec2_wait: WaitTiming {
|
||||
interval: Duration::from_millis(1),
|
||||
timeout: Duration::from_millis(30),
|
||||
},
|
||||
access_point_wait: PollTiming {
|
||||
interval: Duration::from_millis(1),
|
||||
timeout: Duration::from_millis(30),
|
||||
@@ -525,6 +591,25 @@ async fn a_tunnel_failure_stops_a_task_this_start_ran_but_never_an_attached_one(
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_silent_agent_names_the_image_and_the_edge_tag() {
|
||||
let p = aws_project(StorageMode::Ephemeral);
|
||||
let fake = Fake { run: Some(task(&p, "new", "RUNNING", true)), agent_silent: true, ..Default::default() };
|
||||
let e = start(&fake, &p).await.0.err().unwrap();
|
||||
assert!(e.contains(&format!("\"{REGISTRY_IMAGE}\"")), "{e}");
|
||||
assert!(e.contains("older than this app's AWS support"), "{e}");
|
||||
assert!(e.contains(":latest moves only when a release is cut"), "{e}");
|
||||
assert!(e.contains("Settings → Docker → Custom image to ghcr.io/shadowdao/triple-c-sandbox:edge"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_silent_agent_message_escapes_the_image_name() {
|
||||
let m = agent_silent_message(Some("evil\"\nimage"));
|
||||
assert!(m.contains(r#""evil\"\nimage""#), "{m}");
|
||||
assert!(!m.contains('\n'));
|
||||
assert!(agent_silent_message(None).contains("The workspace's image may be older"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_user_who_has_not_joined_is_told_before_anything_is_written() {
|
||||
let p = aws_project(StorageMode::Hybrid);
|
||||
@@ -561,6 +646,21 @@ fn the_sandbox_is_forced_off_on_aws() {
|
||||
let settings = &pairs.iter().find(|(k, _)| k == "CLAUDE_CODE_SETTINGS_JSON").unwrap().1;
|
||||
let v: serde_json::Value = serde_json::from_str(settings).unwrap();
|
||||
assert_eq!(v["sandbox"]["enabled"], false, "{settings}");
|
||||
assert!(!sandbox_allowed(&p));
|
||||
}
|
||||
|
||||
/// E8, measured live (phase 7): bubblewrap can't create a user namespace
|
||||
/// in an ECS task on the ECS-optimized AMI either (Docker's default seccomp
|
||||
/// profile refuses it without CAP_SYS_ADMIN), so EC2 keeps it off too.
|
||||
#[test]
|
||||
fn the_sandbox_is_forced_off_on_ec2_too() {
|
||||
let mut p = ec2_project();
|
||||
p.sandbox_mode_enabled = true;
|
||||
assert!(!sandbox_allowed(&p));
|
||||
let (pairs, _) = aws_env(&p, &AppSettings::default()).unwrap();
|
||||
let settings = &pairs.iter().find(|(k, _)| k == "CLAUDE_CODE_SETTINGS_JSON").unwrap().1;
|
||||
let v: serde_json::Value = serde_json::from_str(settings).unwrap();
|
||||
assert_eq!(v["sandbox"]["enabled"], false, "{settings}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -636,7 +736,7 @@ async fn an_aws_projects_target_is_its_registered_tunnel() {
|
||||
}
|
||||
|
||||
fn find(p: &Project) -> FindTask {
|
||||
FindTask::new(p, REGION, USER)
|
||||
FindTask::new(p, REGION, USER, None)
|
||||
}
|
||||
|
||||
fn store_with(p: &Project) -> (tempfile::TempDir, ProjectsStore) {
|
||||
@@ -1241,3 +1341,597 @@ mod gateway_sidecar_inputs {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------- EC2 (phase 7)
|
||||
|
||||
const USER_CLUSTER: &str = "triple-c-dev--u-alice";
|
||||
|
||||
fn user_cluster_arn() -> String {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:cluster/{USER_CLUSTER}")
|
||||
}
|
||||
|
||||
fn ec2_task_arn(id: &str) -> String {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:task/{USER_CLUSTER}/{id}")
|
||||
}
|
||||
|
||||
fn ec2_project() -> Project {
|
||||
let mut p = aws_project(StorageMode::Ephemeral);
|
||||
let a = p.aws.as_mut().unwrap();
|
||||
a.launch_type = LaunchType::Ec2;
|
||||
a.cpu = 1024;
|
||||
a.memory_mib = 4096;
|
||||
p
|
||||
}
|
||||
|
||||
fn ec2_task(p: &Project, id: &str, last: &str, ready: bool, placed: bool) -> TaskInfo {
|
||||
TaskInfo {
|
||||
arn: ec2_task_arn(id),
|
||||
container_instance_arn: placed.then(|| {
|
||||
format!("arn:aws:ecs:{REGION}:{ACCOUNT}:container-instance/{USER_CLUSTER}/ci-1")
|
||||
}),
|
||||
..task(p, id, last, ready)
|
||||
}
|
||||
}
|
||||
|
||||
fn user_record_param() -> crate::aws::environments::Param {
|
||||
crate::aws::environments::Param::Value(
|
||||
serde_json::json!({
|
||||
"schema": 1, "instance_type": "m7i.large", "max_instances": 2, "user_tag": "alice",
|
||||
"cluster_arn": user_cluster_arn(),
|
||||
"capacity_provider": USER_CLUSTER,
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_start_runs_on_the_users_capacity_and_says_when_it_waits_for_an_instance() {
|
||||
let p = ec2_project();
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
run: Some(ec2_task(&p, "e1", "PROVISIONING", false, false)),
|
||||
..Default::default()
|
||||
};
|
||||
// Cold start: no instance, then one is placed, then the task is up.
|
||||
*fake.script.lock().unwrap() = vec![
|
||||
ec2_task(&p, "e1", "PROVISIONING", false, false),
|
||||
ec2_task(&p, "e1", "PROVISIONING", false, false),
|
||||
ec2_task(&p, "e1", "PENDING", false, true),
|
||||
ec2_task(&p, "e1", "RUNNING", true, true),
|
||||
];
|
||||
let (r, steps) = start(&fake, &p).await;
|
||||
let started = r.unwrap();
|
||||
assert!(started.created);
|
||||
assert_eq!(started.task.arn, ec2_task_arn("e1"));
|
||||
assert_eq!(
|
||||
steps,
|
||||
vec![
|
||||
PROGRESS_READING,
|
||||
PROGRESS_PREPARING,
|
||||
PROGRESS_STARTING,
|
||||
PROGRESS_WAITING,
|
||||
PROGRESS_INSTANCE,
|
||||
PROGRESS_WAITING,
|
||||
PROGRESS_CONNECTING
|
||||
]
|
||||
);
|
||||
assert_eq!(PROGRESS_INSTANCE, "Starting an instance (about 3 minutes)…");
|
||||
// The record is read before anything is written; the project's task is
|
||||
// looked for in both clusters, then once more where it will run.
|
||||
let calls = fake.calls();
|
||||
let pos = |c: &str| calls.iter().position(|x| x == c).unwrap();
|
||||
assert!(pos("user-record dev") < pos("put-secret"), "{calls:?}");
|
||||
assert_eq!(
|
||||
*fake.list_clusters.lock().unwrap(),
|
||||
[cluster_arn(), user_cluster_arn(), user_cluster_arn()]
|
||||
);
|
||||
let req = &fake.run_requests.lock().unwrap()[0];
|
||||
assert_eq!(req.cluster(), Some(user_cluster_arn().as_str()));
|
||||
assert_eq!(req.capacity_provider_strategy()[0].capacity_provider(), USER_CLUSTER);
|
||||
assert!(req.network_configuration().is_none() && req.launch_type().is_none());
|
||||
assert!(fake.describe_clusters.lock().unwrap().iter().all(|c| *c == user_cluster_arn()));
|
||||
}
|
||||
|
||||
/// ECS turns ECS Exec off on a RunTask that has to wait for an instance
|
||||
/// (verified live): that task only brings the instance up; once placed it
|
||||
/// is stopped and the task run again, onto the instance, with ECS Exec.
|
||||
#[tokio::test]
|
||||
async fn a_cold_ec2_start_runs_the_task_again_once_an_instance_is_up() {
|
||||
let p = ec2_project();
|
||||
let execless = |last: &str, placed: bool| TaskInfo { exec_enabled: false, ..ec2_task(&p, "e1", last, false, placed) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
runs: Mutex::new(vec![execless("PROVISIONING", false), ec2_task(&p, "e9", "PENDING", false, true)]),
|
||||
..Default::default()
|
||||
};
|
||||
*fake.script.lock().unwrap() = vec![
|
||||
execless("PROVISIONING", false),
|
||||
execless("PENDING", true),
|
||||
TaskInfo { desired_status: "STOPPED".into(), ..execless("PENDING", true) },
|
||||
execless("STOPPED", true),
|
||||
ec2_task(&p, "e9", "RUNNING", true, true),
|
||||
];
|
||||
let (r, steps) = start(&fake, &p).await;
|
||||
let started = r.unwrap();
|
||||
assert_eq!(started.task.arn, ec2_task_arn("e9"));
|
||||
let calls = fake.calls();
|
||||
assert_eq!(calls.iter().filter(|c| *c == "run").count(), 2, "{calls:?}");
|
||||
let stop = format!("stop {} {STOP_REASON}", ec2_task_arn("e1"));
|
||||
let pos = |c: &str| calls.iter().rposition(|x| x == c).unwrap();
|
||||
assert!(calls.iter().position(|c| *c == stop).unwrap() < pos("run"), "{calls:?}");
|
||||
assert_eq!(*fake.stop_clusters.lock().unwrap(), [user_cluster_arn()]);
|
||||
// Both requests ask for ECS Exec on the user's capacity.
|
||||
for req in fake.run_requests.lock().unwrap().iter() {
|
||||
assert_eq!(req.enable_execute_command(), Some(true));
|
||||
assert_eq!(req.capacity_provider_strategy()[0].capacity_provider(), USER_CLUSTER);
|
||||
}
|
||||
// The second task is the one recorded.
|
||||
assert_eq!(*fake.recorded.lock().unwrap(), vec![Some(ec2_task_arn("e1")), Some(ec2_task_arn("e9"))]);
|
||||
assert_eq!(
|
||||
steps,
|
||||
vec![
|
||||
PROGRESS_READING,
|
||||
PROGRESS_PREPARING,
|
||||
PROGRESS_STARTING,
|
||||
PROGRESS_WAITING,
|
||||
PROGRESS_INSTANCE,
|
||||
PROGRESS_WAITING,
|
||||
PROGRESS_CONNECTING
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_second_task_without_ecs_exec_is_stopped_and_reported() {
|
||||
let p = ec2_project();
|
||||
let execless = |id: &str, last: &str, placed: bool| TaskInfo { exec_enabled: false, ..ec2_task(&p, id, last, false, placed) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
runs: Mutex::new(vec![execless("e1", "PROVISIONING", false), execless("e9", "PENDING", true)]),
|
||||
..Default::default()
|
||||
};
|
||||
*fake.script.lock().unwrap() = vec![execless("e1", "PENDING", true), execless("e1", "STOPPED", true)];
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
assert_eq!(r.err().unwrap().lines().next(), Some(EXEC_OFF));
|
||||
let calls = fake.calls();
|
||||
assert!(calls.contains(&format!("stop {} {STOP_REASON}", ec2_task_arn("e9"))), "{calls:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_cold_ec2_task_stopped_before_placement_ends_the_start_with_its_reason() {
|
||||
let p = ec2_project();
|
||||
let execless = |last: &str| TaskInfo { exec_enabled: false, ..ec2_task(&p, "e1", last, false, false) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
runs: Mutex::new(vec![execless("PROVISIONING")]),
|
||||
..Default::default()
|
||||
};
|
||||
*fake.script.lock().unwrap() = vec![
|
||||
execless("PROVISIONING"),
|
||||
TaskInfo {
|
||||
desired_status: "STOPPED".into(),
|
||||
stopped_reason: Some("Task stopped by user".into()),
|
||||
..execless("PROVISIONING")
|
||||
},
|
||||
];
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("Task stopped by user"), "{e}");
|
||||
assert_eq!(fake.calls().iter().filter(|c| *c == "run").count(), 1, "no second RunTask");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_execless_task_still_waiting_for_an_instance_is_another_starts() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("other"));
|
||||
let other = TaskInfo { exec_enabled: false, ..ec2_task(&p, "other", "PROVISIONING", false, false) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![ec2_task_arn("other")])])),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(other);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
assert_eq!(r.err().unwrap(), INSTANCE_RACE);
|
||||
assert!(!fake.called("run") && !fake.called("stop"), "{:?}", fake.calls());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_cold_reruns_launch_never_doubles_another_starts_task() {
|
||||
let p = ec2_project();
|
||||
let execless = |last: &str, placed: bool| TaskInfo { exec_enabled: false, ..ec2_task(&p, "e1", last, false, placed) };
|
||||
let winner = ec2_task(&p, "w1", "PENDING", false, true);
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
runs: Mutex::new(vec![execless("PROVISIONING", false)]),
|
||||
// Nothing for the start's own finds; the winner's task by the rerun.
|
||||
listed: Mutex::new(vec![vec![], vec![], vec![], vec![ec2_task_arn("w1")]]),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(winner);
|
||||
*fake.script.lock().unwrap() = vec![execless("PENDING", true), execless("STOPPED", true)];
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
assert_eq!(r.err().unwrap(), START_RACE);
|
||||
assert_eq!(fake.calls().iter().filter(|c| *c == "run").count(), 1, "{:?}", fake.calls());
|
||||
// The winner's task is recorded, as on the first launch's race.
|
||||
assert_eq!(fake.recorded.lock().unwrap().last().cloned().flatten(), Some(ec2_task_arn("w1")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_execless_task_that_cannot_be_stopped_fails_the_start() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("old"));
|
||||
let old = TaskInfo { exec_enabled: false, ..ec2_task(&p, "old", "RUNNING", false, true) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![ec2_task_arn("old")])])),
|
||||
stop_fails: true,
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(old);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("Could not stop") && e.contains("ExpiredToken"), "{e}");
|
||||
assert!(!fake.called("run"));
|
||||
}
|
||||
|
||||
fn now_unix() -> i64 {
|
||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
|
||||
}
|
||||
|
||||
/// An exec-off task still waiting for an instance, older than any start
|
||||
/// could still be waiting (a crashed start's orphan), is replaced; a fresh
|
||||
/// one is another start's.
|
||||
#[tokio::test]
|
||||
async fn an_orphaned_waiting_execless_task_is_replaced_and_a_fresh_one_is_left_alone() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("old"));
|
||||
let waiting = |created: i64| TaskInfo {
|
||||
exec_enabled: false,
|
||||
created_unix: Some(created),
|
||||
..ec2_task(&p, "old", "PROVISIONING", false, false)
|
||||
};
|
||||
let fake_with = |t: TaskInfo| {
|
||||
Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![ec2_task_arn("old")])])),
|
||||
run: Some(ec2_task(&p, "new", "RUNNING", true, true)),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(t)
|
||||
};
|
||||
let fake = fake_with(waiting(now_unix() - 3600));
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
assert_eq!(r.unwrap().task.arn, ec2_task_arn("new"));
|
||||
assert!(fake.calls().contains(&format!("stop {} {REPLACED_REASON}", ec2_task_arn("old"))), "{:?}", fake.calls());
|
||||
|
||||
let fake = fake_with(waiting(now_unix()));
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert_eq!(e, INSTANCE_RACE);
|
||||
assert!(e.contains("Stop"), "{e}");
|
||||
assert!(!fake.called("run") && !fake.called("stop"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_cold_start_whose_task_another_start_replaced_says_so() {
|
||||
let p = ec2_project();
|
||||
let execless = |last: &str| TaskInfo { exec_enabled: false, ..ec2_task(&p, "e1", last, false, false) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
runs: Mutex::new(vec![execless("PROVISIONING")]),
|
||||
..Default::default()
|
||||
};
|
||||
*fake.script.lock().unwrap() = vec![TaskInfo {
|
||||
desired_status: "STOPPED".into(),
|
||||
stopped_reason: Some(REPLACED_REASON.into()),
|
||||
..execless("PROVISIONING")
|
||||
}];
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.starts_with("Another start of this workspace replaced this one"), "{e}");
|
||||
assert_eq!(fake.calls().iter().filter(|c| *c == "run").count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_execless_task_that_never_stops_fails_the_start_without_a_new_run() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("old"));
|
||||
let old = TaskInfo { exec_enabled: false, ..ec2_task(&p, "old", "RUNNING", false, true) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![ec2_task_arn("old")])])),
|
||||
stop_noop: true,
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(old);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("did not stop in time"), "{e}");
|
||||
assert!(fake.called("stop") && !fake.called("run"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_live_task_with_ecs_exec_off_is_stopped_and_replaced() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("old"));
|
||||
let old = TaskInfo { exec_enabled: false, ..ec2_task(&p, "old", "RUNNING", false, true) };
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![ec2_task_arn("old")])])),
|
||||
run: Some(ec2_task(&p, "new", "RUNNING", true, true)),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(old);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
assert_eq!(r.unwrap().task.arn, ec2_task_arn("new"));
|
||||
let calls = fake.calls();
|
||||
let stop = calls.iter().position(|c| *c == format!("stop {} {REPLACED_REASON}", ec2_task_arn("old"))).expect("the old task is stopped");
|
||||
assert!(stop < calls.iter().position(|c| c == "run").unwrap(), "{calls:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_fargate_start_never_reads_the_ec2_record() {
|
||||
let p = aws_project(StorageMode::Ephemeral);
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
run: Some(task(&p, "f1", "RUNNING", true)),
|
||||
..Default::default()
|
||||
};
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
r.unwrap();
|
||||
assert!(!fake.called("user-record"));
|
||||
assert_eq!(*fake.list_clusters.lock().unwrap(), [cluster_arn(), cluster_arn()]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_start_without_capacity_says_so_before_anything_is_written() {
|
||||
let p = ec2_project();
|
||||
let fake = Fake::default();
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert_eq!(
|
||||
e,
|
||||
"You have no EC2 capacity in dev; ask an admin to add it to your onboarding (or run the \
|
||||
project on Fargate)."
|
||||
);
|
||||
assert!(!fake.called("put-secret") && !fake.called("register") && !fake.called("run"));
|
||||
|
||||
// A record that can't be used is not mistaken for none.
|
||||
let fake = Fake {
|
||||
user_record: Some(crate::aws::environments::Param::Value("{}".into())),
|
||||
..Default::default()
|
||||
};
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("can't be used") && e.contains("re-apply your onboarding"), "{e}");
|
||||
assert!(!fake.called("put-secret"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_task_that_never_gets_an_instance_is_stopped_with_what_to_check() {
|
||||
let p = ec2_project();
|
||||
let fake = Fake {
|
||||
user_record: Some(user_record_param()),
|
||||
run: Some(ec2_task(&p, "e2", "PROVISIONING", false, false)),
|
||||
..Default::default()
|
||||
};
|
||||
let (r, steps) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("No EC2 instance had room"), "{e}");
|
||||
assert!(e.contains("maximum of 2 instances"), "{e}");
|
||||
assert!(steps.contains(&PROGRESS_INSTANCE.to_string()));
|
||||
assert!(fake.calls().contains(&format!("stop {} {STOP_REASON}", ec2_task_arn("e2"))));
|
||||
assert_eq!(*fake.stop_clusters.lock().unwrap(), [user_cluster_arn()]);
|
||||
assert_eq!(*fake.recorded.lock().unwrap(), vec![Some(ec2_task_arn("e2")), None]);
|
||||
}
|
||||
|
||||
/// The record is gone (capacity removed) while the task still runs: the
|
||||
/// stored ARN leads to it, and attaching needs no record.
|
||||
#[tokio::test]
|
||||
async fn a_running_ec2_task_is_attached_to_even_after_the_record_is_gone() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("e3"));
|
||||
let live = ec2_task(&p, "e3", "RUNNING", true, true);
|
||||
let fake = Fake {
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![live.arn.clone()])])),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(live);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let started = r.unwrap();
|
||||
assert!(!started.created);
|
||||
assert_eq!(started.task.arn, ec2_task_arn("e3"));
|
||||
assert!(!fake.called("run") && !fake.called("put-secret"));
|
||||
assert_eq!(*fake.list_clusters.lock().unwrap(), [cluster_arn(), user_cluster_arn()]);
|
||||
assert!(fake.describe_clusters.lock().unwrap().iter().all(|c| *c == user_cluster_arn()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_ec2_projects_task_is_found_and_watched_on_the_users_cluster() {
|
||||
// FindTask: the environment's cluster, the record's, and the stored
|
||||
// task's per-user cluster, without repeats.
|
||||
let mut p = ec2_project();
|
||||
let f = FindTask::new(&p, REGION, USER, Some(user_cluster_arn()));
|
||||
assert_eq!(f.clusters, ["triple-c-dev".to_string(), user_cluster_arn()]);
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("e4"));
|
||||
assert_eq!(find(&p).clusters, ["triple-c-dev".to_string(), user_cluster_arn()]);
|
||||
let f = FindTask::new(&p, REGION, USER, Some(user_cluster_arn()));
|
||||
assert_eq!(f.clusters.len(), 2);
|
||||
|
||||
// Reconcile with no ARN stored finds the task on the user's cluster.
|
||||
let mut orphan = ec2_project();
|
||||
orphan.status = ProjectStatus::Running;
|
||||
let live = ec2_task(&orphan, "e5", "RUNNING", true, true);
|
||||
let fake = Fake {
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![live.arn.clone()])])),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(live);
|
||||
let (_d, store) = store_with(&orphan);
|
||||
let find = FindTask::new(&orphan, REGION, USER, Some(user_cluster_arn()));
|
||||
assert!(matches!(
|
||||
reconcile_project(&fake, &store, &orphan, &find).await.unwrap(),
|
||||
Reconciled::Live(_)
|
||||
));
|
||||
assert_eq!(store.get(&orphan.id).unwrap().aws.unwrap().task_arn, Some(ec2_task_arn("e5")));
|
||||
|
||||
// The watch groups by the task's own cluster.
|
||||
let mut running = ec2_project();
|
||||
running.status = ProjectStatus::Running;
|
||||
running.container_id = Some("http://127.0.0.1:61301/".into());
|
||||
running.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("e6"));
|
||||
let w = watched_task(&running).expect("watched");
|
||||
assert_eq!(w.group.cluster_arn, user_cluster_arn());
|
||||
let mut fargate = aws_project(StorageMode::Ephemeral);
|
||||
fargate.status = ProjectStatus::Running;
|
||||
fargate.container_id = Some("http://127.0.0.1:61302/".into());
|
||||
fargate.aws.as_mut().unwrap().task_arn = Some(task_arn("f6"));
|
||||
assert_ne!(watched_task(&fargate).unwrap().group, w.group);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stopping_an_ec2_task_by_its_arn_uses_its_cluster() {
|
||||
let fake = Fake::default();
|
||||
stop_task_arn(&fake, &ec2_task_arn("e7")).await.unwrap();
|
||||
assert_eq!(*fake.stop_clusters.lock().unwrap(), [USER_CLUSTER]);
|
||||
}
|
||||
|
||||
/// E7: the entrypoint installs the VPN skill from `VPN_SUPPORT_ENABLED`, as
|
||||
/// on Docker; an EC2 project with VPN support gets `1`, Fargate `0`.
|
||||
#[test]
|
||||
fn vpn_support_reaches_an_ec2_workspace_as_on_docker() {
|
||||
let vpn = |p: &Project| {
|
||||
let (pairs, _) = aws_env(p, &AppSettings::default()).unwrap();
|
||||
pairs.into_iter().find(|(k, _)| k == "VPN_SUPPORT_ENABLED").map(|(_, v)| v)
|
||||
};
|
||||
let mut p = ec2_project();
|
||||
p.vpn_support_enabled = true;
|
||||
assert_eq!(vpn(&p).as_deref(), Some("1"));
|
||||
p.vpn_support_enabled = false;
|
||||
assert_eq!(vpn(&p).as_deref(), Some("0"));
|
||||
assert_eq!(vpn(&aws_project(StorageMode::Ephemeral)).as_deref(), Some("0"));
|
||||
}
|
||||
|
||||
/// Host settings' view of the user's EC2 capacity: the record's compute with
|
||||
/// the fit limits, none, unusable (with the reason) or unknown.
|
||||
#[test]
|
||||
fn ec2_capacity_reads_as_host_settings_need_it() {
|
||||
use crate::aws::environments::{UserCompute, UserRecord};
|
||||
let compute = UserCompute {
|
||||
instance_type: "m7i.large".into(),
|
||||
max_instances: 1,
|
||||
user_tag: "alice".into(),
|
||||
cluster_arn: "arn:aws:ecs:us-west-2:123456789012:cluster/triple-c-dev--u-alice".into(),
|
||||
capacity_provider: "triple-c-dev--u-alice".into(),
|
||||
};
|
||||
let c = ec2_capacity_from(Some(UserRecord::Valid(compute.clone())));
|
||||
assert_eq!(
|
||||
c,
|
||||
Ec2Capacity::Ec2 { compute: compute.clone(), available_cpu: Some(1920), available_memory_mib: Some(7373) }
|
||||
);
|
||||
let v = serde_json::to_value(&c).unwrap();
|
||||
assert_eq!(v["state"], "ec2");
|
||||
assert_eq!(v["compute"]["instance_type"], "m7i.large");
|
||||
assert_eq!(v["available_memory_mib"], 7373);
|
||||
let unknown_type = UserCompute { instance_type: "x9.huge".into(), ..compute };
|
||||
assert!(matches!(
|
||||
ec2_capacity_from(Some(UserRecord::Valid(unknown_type))),
|
||||
Ec2Capacity::Ec2 { available_cpu: None, available_memory_mib: None, .. }
|
||||
));
|
||||
assert_eq!(serde_json::to_value(ec2_capacity_from(Some(UserRecord::Absent))).unwrap()["state"], "none");
|
||||
let v = serde_json::to_value(ec2_capacity_from(Some(UserRecord::Unusable("schema is not 1".into())))).unwrap();
|
||||
assert_eq!(v, serde_json::json!({ "state": "unusable", "reason": "schema is not 1" }));
|
||||
assert_eq!(serde_json::to_value(ec2_capacity_from(None)).unwrap(), serde_json::json!({ "state": "unknown" }));
|
||||
}
|
||||
|
||||
/// Reconcile never marks an exec-off task Running (an app restart during a
|
||||
/// cold EC2 start): a placed or orphaned one is stopped (its ARN cleared
|
||||
/// only once the stop succeeded); a recent one still waiting is Error with
|
||||
/// its ARN kept.
|
||||
#[tokio::test]
|
||||
async fn reconcile_never_attaches_to_a_task_with_ecs_exec_off() {
|
||||
let mut p = ec2_project();
|
||||
p.status = ProjectStatus::Running;
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("x"));
|
||||
let execless = |last: &str, placed: bool, created: i64| TaskInfo {
|
||||
exec_enabled: false,
|
||||
created_unix: Some(created),
|
||||
..ec2_task(&p, "x", last, false, placed)
|
||||
};
|
||||
let find = FindTask::new(&p, REGION, USER, Some(user_cluster_arn()));
|
||||
let stopped_with_replaced = |f: &Fake| f.calls().contains(&format!("stop {} {REPLACED_REASON}", ec2_task_arn("x")));
|
||||
|
||||
// Placed: stopped, Stopped, ARN cleared.
|
||||
let fake = Fake::default().with_task(execless("PENDING", true, now_unix()));
|
||||
let (_d, store) = store_with(&p);
|
||||
assert_eq!(reconcile_project(&fake, &store, &p, &find).await.unwrap(), Reconciled::Gone);
|
||||
assert!(stopped_with_replaced(&fake), "{:?}", fake.calls());
|
||||
let after = store.get(&p.id).unwrap();
|
||||
assert_eq!(after.status, ProjectStatus::Stopped);
|
||||
assert_eq!(after.aws.unwrap().task_arn, None);
|
||||
|
||||
// Orphaned (still waiting, but older than any start waits): the same.
|
||||
let fake = Fake::default().with_task(execless("PROVISIONING", false, now_unix() - 3600));
|
||||
let (_d, store) = store_with(&p);
|
||||
assert_eq!(reconcile_project(&fake, &store, &p, &find).await.unwrap(), Reconciled::Gone);
|
||||
assert!(stopped_with_replaced(&fake));
|
||||
assert_eq!(store.get(&p.id).unwrap().status, ProjectStatus::Stopped);
|
||||
|
||||
// The stop fails: Error, the ARN kept.
|
||||
let fake = Fake { stop_fails: true, ..Default::default() }.with_task(execless("RUNNING", true, now_unix()));
|
||||
let (_d, store) = store_with(&p);
|
||||
let e = reconcile_project(&fake, &store, &p, &find).await.unwrap_err();
|
||||
assert!(e.contains("Could not stop") && e.contains("ExpiredToken"), "{e}");
|
||||
let after = store.get(&p.id).unwrap();
|
||||
assert_eq!(after.status, ProjectStatus::Error);
|
||||
assert_eq!(after.aws.unwrap().task_arn, Some(ec2_task_arn("x")));
|
||||
|
||||
// Recent and still waiting: Interrupted (Error), ARN kept, nothing stopped.
|
||||
let fake = Fake::default().with_task(execless("PROVISIONING", false, now_unix() - 60));
|
||||
let (_d, store) = store_with(&p);
|
||||
assert_eq!(reconcile_project(&fake, &store, &p, &find).await.unwrap(), Reconciled::Interrupted);
|
||||
assert!(!fake.called("stop"));
|
||||
let after = store.get(&p.id).unwrap();
|
||||
assert_eq!(after.status, ProjectStatus::Error);
|
||||
assert_eq!(after.aws.unwrap().task_arn, Some(ec2_task_arn("x")));
|
||||
assert!(INTERRUPTED.contains("Start again"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_orphan_is_older_than_the_ec2_wait_plus_ten_minutes() {
|
||||
assert_eq!(ORPHAN_MARGIN, Duration::from_secs(10 * 60));
|
||||
let p = ec2_project();
|
||||
let at = |age: i64| TaskInfo { created_unix: Some(1_000_000 - age), ..ec2_task(&p, "o", "PROVISIONING", false, false) };
|
||||
let wait = WaitTiming::ec2();
|
||||
assert!(!is_orphaned(&at(19 * 60), wait, 1_000_000));
|
||||
assert!(is_orphaned(&at(21 * 60), wait, 1_000_000));
|
||||
assert!(!is_orphaned(&TaskInfo { created_unix: None, ..at(0) }, wait, 1_000_000));
|
||||
}
|
||||
|
||||
/// A failed read of the EC2 record does not stop an attach: the stored
|
||||
/// task's cluster is still searched. Only a new run needs the record.
|
||||
#[tokio::test]
|
||||
async fn an_ec2_record_read_error_still_attaches_and_fails_only_a_new_run() {
|
||||
let mut p = ec2_project();
|
||||
p.aws.as_mut().unwrap().task_arn = Some(ec2_task_arn("e8"));
|
||||
let live = ec2_task(&p, "e8", "RUNNING", true, true);
|
||||
let fake = Fake {
|
||||
user_record_fails: true,
|
||||
listed_by_cluster: Some(HashMap::from([(user_cluster_arn(), vec![live.arn.clone()])])),
|
||||
..Default::default()
|
||||
}
|
||||
.with_task(live);
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let started = r.unwrap();
|
||||
assert!(!started.created);
|
||||
assert_eq!(started.task.arn, ec2_task_arn("e8"));
|
||||
assert_eq!(*fake.list_clusters.lock().unwrap(), [cluster_arn(), user_cluster_arn()]);
|
||||
|
||||
// Nothing live: the start fails with the read error, before any write.
|
||||
let fake = Fake { user_record_fails: true, ..Default::default() };
|
||||
let (r, _) = start(&fake, &p).await;
|
||||
let e = r.err().unwrap();
|
||||
assert!(e.contains("Could not read your EC2 capacity record") && e.contains("Throttling"), "{e}");
|
||||
assert!(!fake.called("put-secret") && !fake.called("run"));
|
||||
}
|
||||
@@ -30,6 +30,7 @@
|
||||
pub mod agent;
|
||||
pub mod aws_host;
|
||||
pub mod lifecycle;
|
||||
pub mod persistent;
|
||||
pub mod port_forwards;
|
||||
pub mod sessions;
|
||||
pub mod ssm;
|
||||
|
||||
@@ -0,0 +1,509 @@
|
||||
//! Persistent terminals on AWS workspaces: every terminal tab (Claude and
|
||||
//! bash) runs inside its own tmux server in the workspace, so it outlives the
|
||||
//! app closing or the SSM tunnel dropping, and the app attaches to it again on
|
||||
//! its next connect. Docker projects never come here.
|
||||
//!
|
||||
//! The rules, and why:
|
||||
//!
|
||||
//! * **One tmux server per tab**, `tmux -L triple-c-<session id>`. A server
|
||||
//! takes its environment from the client that started it, so each tab's
|
||||
//! server has exactly that tab's exec environment — the copy-at-connect
|
||||
//! credential pairs (`aws_host::terminal_env`) travel in `ExecSpec.env` as
|
||||
//! before and never on argv (`new-session -e` would put them in `ps`).
|
||||
//! * **The tab's session id is the key.** The frontend's UUID, persisted per
|
||||
//! project by `storage::terminal_store`, so a tab reopened after an app
|
||||
//! restart gets the same id (its rename, `renamed_session_names`, too).
|
||||
//! * **Opening** a tab is `new-session -A` (attach when it already exists);
|
||||
//! **reattaching** is `attach-session`, which never creates: a session that
|
||||
//! ended while the app was away must not come back as a fresh `claude`.
|
||||
//! * **Closing a tab ends the session** (`kill-server`), as today. App exit, a
|
||||
//! dropped tunnel and the agent's SIGHUP on a client that went away only
|
||||
//! **detach** — the server daemonised into its own session, so the hangup
|
||||
//! reaches the client alone.
|
||||
//! * The tmux config is `/etc/triple-c/tmux.conf` (`container/triple-c-tmux.conf`):
|
||||
//! no prefix key, no status line, output in xterm.js's own scrollback.
|
||||
//! * An image without tmux or that config runs terminals the old way, with a
|
||||
//! one-time notice per tunnel ([`plan`]).
|
||||
//!
|
||||
//! Everything in here runs through [`ContainerTarget`].
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::ContainerTarget;
|
||||
|
||||
/// The tmux config baked into the image (outside the seed-only home).
|
||||
pub const TMUX_CONF: &str = "/etc/triple-c/tmux.conf";
|
||||
/// Socket name prefix (`-L`): `/tmp/tmux-<uid>/triple-c-<key>`.
|
||||
pub const SOCKET_PREFIX: &str = "triple-c-";
|
||||
/// The outer terminal the tmux client talks to: xterm.js. The agent would
|
||||
/// otherwise default a tty exec to `TERM=xterm`, and tmux would then
|
||||
/// downsample every colour to 8.
|
||||
pub const OUTER_TERM: &str = "TERM=xterm-256color";
|
||||
/// tmux user option holding the tab's label, for a session another machine
|
||||
/// opened (the app's own sessions keep theirs in the store).
|
||||
pub const NAME_OPTION: &str = "@triple_c_name";
|
||||
/// How much pane history a re-opened tab gets replayed.
|
||||
pub const HISTORY_LINES: u32 = 5000;
|
||||
/// Byte cap on that replay (escape sequences included).
|
||||
pub const HISTORY_LIMIT: usize = 4 * 1024 * 1024;
|
||||
/// Every small tmux exec (probe, list, has-session, kill).
|
||||
pub const SMALL_EXEC_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
/// The notice an image without tmux gets, once per tunnel.
|
||||
pub const NO_TMUX_NOTICE: &str = "This workspace image has no tmux, so terminals end when the app \
|
||||
closes. Use an image built after persistent terminals were added (e.g. \
|
||||
ghcr.io/shadowdao/triple-c-sandbox:edge).";
|
||||
/// The longest tab label kept in tmux.
|
||||
const MAX_NAME_CHARS: usize = 80;
|
||||
|
||||
/// What a terminal runs. Also the tmux session name (`-s`), so a session the
|
||||
/// app does not know still says what it is.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TerminalKind {
|
||||
Claude,
|
||||
Bash,
|
||||
}
|
||||
|
||||
impl TerminalKind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
TerminalKind::Claude => "claude",
|
||||
TerminalKind::Bash => "bash",
|
||||
}
|
||||
}
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"claude" => Some(TerminalKind::Claude),
|
||||
"bash" => Some(TerminalKind::Bash),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A session key is a socket name and an argv element: UUID characters only.
|
||||
pub fn validate_key(key: &str) -> Result<(), String> {
|
||||
if key.is_empty()
|
||||
|| key.len() > 64
|
||||
|| !key.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
|
||||
{
|
||||
return Err("Invalid terminal session id".to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn socket_name(key: &str) -> String {
|
||||
format!("{SOCKET_PREFIX}{key}")
|
||||
}
|
||||
|
||||
/// A tab label as tmux keeps it: no control characters (the listing is one
|
||||
/// tab-separated line per session), at most [`MAX_NAME_CHARS`], and no
|
||||
/// trailing `;` or `\` — tmux splits its argv into commands at an argument
|
||||
/// that *ends* in `;`.
|
||||
pub fn clean_name(name: Option<&str>) -> Option<String> {
|
||||
let cleaned: String = name?
|
||||
.chars()
|
||||
.filter(|c| !c.is_control())
|
||||
.take(MAX_NAME_CHARS)
|
||||
.collect();
|
||||
let cleaned = cleaned.trim().trim_end_matches([';', '\\']).trim().to_string();
|
||||
(!cleaned.is_empty()).then_some(cleaned)
|
||||
}
|
||||
|
||||
fn tmux_base(key: &str) -> Vec<String> {
|
||||
vec![
|
||||
"tmux".into(),
|
||||
"-L".into(),
|
||||
socket_name(key),
|
||||
"-f".into(),
|
||||
TMUX_CONF.into(),
|
||||
]
|
||||
}
|
||||
|
||||
/// The attached exec of a tab: `inner` (the unchanged command — the Claude
|
||||
/// launch with its update prelude, or `bash -l`) as the only window of a new
|
||||
/// tmux server, or an attach to it if it is already there. The label goes in
|
||||
/// [`NAME_OPTION`] as a second tmux command. No environment value is ever an
|
||||
/// argument here.
|
||||
pub fn new_session_cmd(
|
||||
key: &str,
|
||||
kind: TerminalKind,
|
||||
name: Option<&str>,
|
||||
inner: Vec<String>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
validate_key(key)?;
|
||||
if inner.is_empty() {
|
||||
return Err("No command to run".into());
|
||||
}
|
||||
// tmux would read an argument ending in `;` as the end of a command and
|
||||
// run the rest as tmux commands. Our scripts end in a newline; refuse
|
||||
// rather than guess if that ever changes.
|
||||
if inner.iter().any(|a| a.ends_with(';')) {
|
||||
return Err("A terminal command argument may not end with ';'".into());
|
||||
}
|
||||
let mut cmd = tmux_base(key);
|
||||
cmd.extend([
|
||||
"new-session".into(),
|
||||
"-A".into(),
|
||||
"-s".into(),
|
||||
kind.as_str().into(),
|
||||
"--".into(),
|
||||
]);
|
||||
cmd.extend(inner);
|
||||
if let Some(n) = clean_name(name) {
|
||||
cmd.extend([";".into(), "set-option".into(), "-q".into(), NAME_OPTION.into(), n]);
|
||||
}
|
||||
Ok(cmd)
|
||||
}
|
||||
|
||||
/// Attach to an existing session; fails (and creates nothing) when it is gone.
|
||||
pub fn attach_cmd(key: &str) -> Result<Vec<String>, String> {
|
||||
validate_key(key)?;
|
||||
let mut cmd = tmux_base(key);
|
||||
cmd.push("attach-session".into());
|
||||
Ok(cmd)
|
||||
}
|
||||
|
||||
/// End one session: its server and everything in it (SIGHUP to the panes).
|
||||
pub fn kill_cmd(key: &str) -> Result<Vec<String>, String> {
|
||||
validate_key(key)?;
|
||||
Ok(vec!["tmux".into(), "-L".into(), socket_name(key), "kill-server".into()])
|
||||
}
|
||||
|
||||
/// The pane's history above the screen, colours kept, wrapped lines joined
|
||||
/// (xterm.js rewraps them at its own width).
|
||||
pub fn history_cmd(key: &str) -> Result<Vec<String>, String> {
|
||||
validate_key(key)?;
|
||||
Ok(vec![
|
||||
"tmux".into(),
|
||||
"-L".into(),
|
||||
socket_name(key),
|
||||
"capture-pane".into(),
|
||||
"-p".into(),
|
||||
"-e".into(),
|
||||
"-J".into(),
|
||||
"-S".into(),
|
||||
format!("-{HISTORY_LINES}"),
|
||||
"-E".into(),
|
||||
"-1".into(),
|
||||
])
|
||||
}
|
||||
|
||||
/// Exit 0 when the image can keep terminals: tmux and our config.
|
||||
pub fn probe_cmd() -> Vec<String> {
|
||||
vec![
|
||||
"sh".into(),
|
||||
"-c".into(),
|
||||
format!("command -v tmux >/dev/null 2>&1 && [ -r {TMUX_CONF} ]"),
|
||||
]
|
||||
}
|
||||
|
||||
/// The tmux socket directory of the exec's user, as tmux computes it.
|
||||
const SOCKET_DIR: &str = r#"d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)""#;
|
||||
|
||||
/// One line per running session: `key \t kind \t created \t attached \t name`.
|
||||
/// A socket whose server is gone (tmux leaves the file) answers nothing and
|
||||
/// is skipped.
|
||||
pub fn list_cmd() -> Vec<String> {
|
||||
vec![
|
||||
"sh".into(),
|
||||
"-c".into(),
|
||||
format!(
|
||||
r#"{SOCKET_DIR}
|
||||
command -v tmux >/dev/null 2>&1 || exit 0
|
||||
for s in "$d"/{SOCKET_PREFIX}*; do
|
||||
[ -S "$s" ] || continue
|
||||
n=${{s##*/}}
|
||||
line=$(tmux -L "$n" display-message -p '#{{session_name}} #{{session_created}} #{{session_attached}} #{{{NAME_OPTION}}}' 2>/dev/null) || continue
|
||||
printf '%s\t%s\n' "${{n#{SOCKET_PREFIX}}}" "$line"
|
||||
done
|
||||
exit 0"#
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// End every session (Stop, before the WIP push's snapshot), then wait —
|
||||
/// bounded, 5 s — until the panes' processes (taken before the kill) and
|
||||
/// the servers are gone, so a pane's last writes land before the snapshot.
|
||||
/// The same script is `entrypoint.sh`'s `aws_end_terminals` and the idle
|
||||
/// watcher's (a test pins them together).
|
||||
pub fn end_all_cmd() -> Vec<String> {
|
||||
vec!["sh".into(), "-c".into(), END_ALL_SCRIPT.into()]
|
||||
}
|
||||
|
||||
/// See [`end_all_cmd`].
|
||||
pub const END_ALL_SCRIPT: &str = r#"d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)"
|
||||
command -v tmux >/dev/null 2>&1 || exit 0
|
||||
pids=""
|
||||
for s in "$d"/triple-c-*; do
|
||||
[ -S "$s" ] || continue
|
||||
pids="$pids $(tmux -L "${s##*/}" list-panes -a -F '#{pane_pid}' 2>/dev/null)"
|
||||
tmux -L "${s##*/}" kill-server 2>/dev/null
|
||||
done
|
||||
i=0
|
||||
while [ $i -lt 50 ]; do
|
||||
alive=""
|
||||
for p in $pids; do kill -0 "$p" 2>/dev/null && alive=1; done
|
||||
pgrep -u "$(id -u)" -f '^tmux -L triple-c-' >/dev/null 2>&1 && alive=1
|
||||
[ -n "$alive" ] || break
|
||||
sleep 0.1; i=$((i + 1))
|
||||
done
|
||||
exit 0"#;
|
||||
|
||||
/// A session found running in the workspace.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RunningSession {
|
||||
pub key: String,
|
||||
pub kind: TerminalKind,
|
||||
pub created_unix: u64,
|
||||
pub attached: bool,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
/// [`list_cmd`]'s output; lines that do not parse (a session someone made by
|
||||
/// hand under our prefix) are skipped.
|
||||
pub fn parse_list(out: &str) -> Vec<RunningSession> {
|
||||
out.lines()
|
||||
.filter_map(|line| {
|
||||
let mut f = line.splitn(5, '\t');
|
||||
let key = f.next()?.to_string();
|
||||
validate_key(&key).ok()?;
|
||||
let kind = TerminalKind::parse(f.next()?)?;
|
||||
let created_unix = f.next()?.trim().parse().ok()?;
|
||||
let attached = f.next()?.trim().parse::<u32>().ok()? > 0;
|
||||
let name = clean_name(f.next());
|
||||
Some(RunningSession { key, kind, created_unix, attached, name })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The exec environment of a tmux client: the caller's pairs (copy-at-connect
|
||||
/// credentials, which the server it starts inherits) plus [`OUTER_TERM`].
|
||||
/// `TMUX`/`TMUX_TMPDIR` are dropped (and reserved as custom variable
|
||||
/// names): either would point the client at another socket than the one the
|
||||
/// app lists and ends.
|
||||
pub fn client_env(mut env: Vec<String>) -> Vec<String> {
|
||||
env.retain(|e| {
|
||||
!e.starts_with("TERM=") && !e.starts_with("TMUX=") && !e.starts_with("TMUX_TMPDIR=")
|
||||
});
|
||||
env.push(OUTER_TERM.to_string());
|
||||
env
|
||||
}
|
||||
|
||||
// ───────────────────────────────────────────────────────────── probe cache
|
||||
|
||||
/// Per target id (an endpoint URL, new for every tunnel): does the image
|
||||
/// keep terminals, and was the "no tmux" notice given yet.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
struct Probe {
|
||||
supported: bool,
|
||||
noticed: bool,
|
||||
}
|
||||
|
||||
fn probes() -> &'static Mutex<HashMap<String, Probe>> {
|
||||
static P: std::sync::OnceLock<Mutex<HashMap<String, Probe>>> = std::sync::OnceLock::new();
|
||||
P.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
/// Forget what was probed through `target_id` — a new tunnel registered on
|
||||
/// it (`tunnels::register`), which may reach another task's image.
|
||||
pub fn forget_probe(target_id: &str) {
|
||||
probes().lock().unwrap_or_else(|e| e.into_inner()).remove(target_id);
|
||||
}
|
||||
|
||||
/// Whether `target`'s image has tmux and the config; asked once per tunnel.
|
||||
/// `None` when the probe could not run (asked again next time).
|
||||
pub async fn probe(target: &ContainerTarget) -> Option<bool> {
|
||||
if let Some(p) = probes().lock().unwrap_or_else(|e| e.into_inner()).get(target.id()) {
|
||||
return Some(p.supported);
|
||||
}
|
||||
let res = tokio::time::timeout(
|
||||
SMALL_EXEC_TIMEOUT,
|
||||
target.exec_oneshot_background("claude", probe_cmd(), 4096),
|
||||
)
|
||||
.await;
|
||||
let supported = match res {
|
||||
Ok(Ok((_, code))) => code == 0,
|
||||
Ok(Err(e)) => {
|
||||
log::warn!("Could not ask the workspace whether it has tmux: {e}");
|
||||
return None;
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("Asking the workspace whether it has tmux timed out");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
probes()
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.entry(target.id().to_string())
|
||||
.or_insert(Probe { supported, noticed: false });
|
||||
Some(supported)
|
||||
}
|
||||
|
||||
/// [`probe`] for callers that must not guess: an unanswered probe is an
|
||||
/// error (the caller retries), never "no tmux" — reading it as no would
|
||||
/// prune remembered sessions and end tabs whose Claude still runs.
|
||||
pub async fn supported(target: &ContainerTarget) -> Result<bool, String> {
|
||||
probe(target)
|
||||
.await
|
||||
.ok_or_else(|| "The workspace did not answer whether it can keep terminals".to_string())
|
||||
}
|
||||
|
||||
/// The "no tmux" notice, the first time it is asked for on a tunnel whose
|
||||
/// probe said no (never for a probe that could not run).
|
||||
fn take_notice(target_id: &str) -> Option<String> {
|
||||
let mut p = probes().lock().unwrap_or_else(|e| e.into_inner());
|
||||
let entry = p.get_mut(target_id)?;
|
||||
if entry.supported || entry.noticed {
|
||||
return None;
|
||||
}
|
||||
entry.noticed = true;
|
||||
Some(NO_TMUX_NOTICE.to_string())
|
||||
}
|
||||
|
||||
// ───────────────────────────────────────────────────────────────── launch
|
||||
|
||||
/// How a terminal is launched.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LaunchPlan {
|
||||
pub cmd: Vec<String>,
|
||||
pub env: Vec<String>,
|
||||
/// Runs in tmux and outlives the app.
|
||||
pub persistent: bool,
|
||||
/// Say this once (no tmux in the image).
|
||||
pub notice: Option<String>,
|
||||
}
|
||||
|
||||
/// The pure decision: a persistent launch for an AWS project whose image
|
||||
/// keeps terminals, the old launch otherwise.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn plan_with(
|
||||
is_aws: bool,
|
||||
supported: bool,
|
||||
notice: Option<String>,
|
||||
key: &str,
|
||||
kind: TerminalKind,
|
||||
name: Option<&str>,
|
||||
inner: Vec<String>,
|
||||
env: Vec<String>,
|
||||
) -> Result<LaunchPlan, String> {
|
||||
if !is_aws {
|
||||
return Ok(LaunchPlan { cmd: inner, env, persistent: false, notice: None });
|
||||
}
|
||||
if !supported {
|
||||
return Ok(LaunchPlan { cmd: inner, env, persistent: false, notice });
|
||||
}
|
||||
Ok(LaunchPlan {
|
||||
cmd: new_session_cmd(key, kind, name, inner)?,
|
||||
env: client_env(env),
|
||||
persistent: true,
|
||||
notice: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// [`plan_with`] for a real target: probes (cached per tunnel) on AWS only.
|
||||
pub async fn plan(
|
||||
is_aws: bool,
|
||||
target: &ContainerTarget,
|
||||
key: &str,
|
||||
kind: TerminalKind,
|
||||
name: Option<&str>,
|
||||
inner: Vec<String>,
|
||||
env: Vec<String>,
|
||||
) -> Result<LaunchPlan, String> {
|
||||
if !is_aws {
|
||||
return plan_with(false, false, None, key, kind, name, inner, env);
|
||||
}
|
||||
validate_key(key)?;
|
||||
let ok = supported(target).await?;
|
||||
let notice = if ok { None } else { take_notice(target.id()) };
|
||||
plan_with(true, ok, notice, key, kind, name, inner, env)
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────── workspace actions
|
||||
|
||||
async fn small(target: &ContainerTarget, cmd: Vec<String>) -> Result<(String, i64), String> {
|
||||
tokio::time::timeout(SMALL_EXEC_TIMEOUT, target.exec_oneshot_background("claude", cmd, 256 * 1024))
|
||||
.await
|
||||
.map_err(|_| "The workspace did not answer in time".to_string())?
|
||||
}
|
||||
|
||||
/// The sessions running in the workspace (none on an image without tmux).
|
||||
/// An `Err` (including an unanswered probe) says nothing about what runs.
|
||||
pub async fn list(target: &ContainerTarget) -> Result<Vec<RunningSession>, String> {
|
||||
if !supported(target).await? {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let (out, code) = small(target, list_cmd()).await?;
|
||||
if code != 0 {
|
||||
return Err(format!("Listing the workspace's terminals failed (exit {code})"));
|
||||
}
|
||||
Ok(parse_list(&out))
|
||||
}
|
||||
|
||||
/// End the session `key` (closing its tab). Gone already is success.
|
||||
pub async fn end(target: &ContainerTarget, key: &str) -> Result<(), String> {
|
||||
small(target, kill_cmd(key)?).await.map(|_| ())
|
||||
}
|
||||
|
||||
/// End every session in the workspace, bounded; logged, never an error (a
|
||||
/// Stop must go on).
|
||||
pub async fn end_all(target: &ContainerTarget) {
|
||||
let res = tokio::time::timeout(
|
||||
SMALL_EXEC_TIMEOUT,
|
||||
target.exec_oneshot_as("claude", end_all_cmd(), Vec::new()),
|
||||
)
|
||||
.await;
|
||||
match res {
|
||||
Ok(Ok(_)) => {}
|
||||
Ok(Err(e)) => log::warn!("Could not end the workspace's terminals: {e}"),
|
||||
Err(_) => log::warn!("Ending the workspace's terminals timed out"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The pane history of `key`, for a freshly opened tab; `None` when it can't
|
||||
/// be read (the attach still goes ahead).
|
||||
pub async fn history(target: &ContainerTarget, key: &str) -> Option<String> {
|
||||
let cmd = history_cmd(key).ok()?;
|
||||
let res = tokio::time::timeout(
|
||||
SMALL_EXEC_TIMEOUT,
|
||||
target.exec_oneshot_background("claude", cmd, HISTORY_LIMIT),
|
||||
)
|
||||
.await;
|
||||
match res {
|
||||
Ok(Ok((out, 0))) => Some(out),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// What a fresh xterm is fed before the attach: the history, one line per
|
||||
/// row, then `rows` newlines so all of it scrolls into xterm.js's scrollback
|
||||
/// before tmux clears and draws the screen (an erase does not push lines
|
||||
/// into xterm.js's scrollback).
|
||||
pub fn replay_bytes(history: &str, rows: u16) -> Vec<u8> {
|
||||
let body = history.trim_end_matches('\n');
|
||||
if body.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut out = String::with_capacity(body.len() + rows as usize * 2 + 16);
|
||||
for line in body.split('\n') {
|
||||
out.push_str(line);
|
||||
// A captured line ends with its own SGR state; reset so colour
|
||||
// never bleeds into the padding.
|
||||
out.push_str("\x1b[0m\r\n");
|
||||
}
|
||||
for _ in 0..rows {
|
||||
out.push_str("\r\n");
|
||||
}
|
||||
out.into_bytes()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod live_tests;
|
||||
@@ -0,0 +1,157 @@
|
||||
//! Persistent terminals end to end, through the real agent in a real
|
||||
//! container: a tab's tmux server survives its client going away (the
|
||||
//! agent's SIGHUP), is listed, keeps its own environment (and nothing secret
|
||||
//! on argv), replays its history, reattaches, and ends.
|
||||
//!
|
||||
//! Needs a Docker daemon and an image whose entrypoint starts the agent and
|
||||
//! that has tmux and `/etc/triple-c/tmux.conf`: `TRIPLEC_PERSIST_E2E_IMAGE`,
|
||||
//! default `tc-tmux-e2e:latest` (e.g. the sandbox `:edge` image plus
|
||||
//! `apt-get install tmux` and `COPY triple-c-tmux.conf /etc/triple-c/tmux.conf`).
|
||||
//!
|
||||
//! `cargo test host::persistent::live_tests -- --ignored`
|
||||
|
||||
use super::*;
|
||||
use crate::host::{AgentEndpoint, ContainerTarget, ExecSpec};
|
||||
use futures_util::StreamExt;
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
fn docker_cli(args: &[&str]) -> String {
|
||||
let out = Command::new("docker").args(args).output().expect("docker cli");
|
||||
assert!(out.status.success(), "docker {:?}: {}", args, String::from_utf8_lossy(&out.stderr));
|
||||
String::from_utf8_lossy(&out.stdout).trim().to_string()
|
||||
}
|
||||
|
||||
/// Read an attached exec's output until `needle` shows up (or time runs out).
|
||||
async fn read_until(out: &mut crate::host::ExecOutputStream, needle: &str, secs: u64) -> String {
|
||||
let mut seen = String::new();
|
||||
let deadline = tokio::time::Instant::now() + Duration::from_secs(secs);
|
||||
while !seen.contains(needle) {
|
||||
match tokio::time::timeout_at(deadline, out.next()).await {
|
||||
Ok(Some(Ok(c))) => seen.push_str(&String::from_utf8_lossy(&c.into_bytes())),
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
seen
|
||||
}
|
||||
|
||||
async fn start_agent_container(image: &str) -> (String, ContainerTarget) {
|
||||
let token = format!("{}{}", uuid::Uuid::new_v4().simple(), uuid::Uuid::new_v4().simple());
|
||||
let name = format!("tc-persist-e2e-{}", uuid::Uuid::new_v4().simple());
|
||||
docker_cli(&[
|
||||
"run", "-d", "--init", "--name", &name, "-w", "/workspace",
|
||||
"-e", &format!("TRIPLEC_AGENT_TOKEN={token}"),
|
||||
"-e", "TRIPLEC_AGENT_ADDR=0.0.0.0:47900",
|
||||
"-p", "127.0.0.1::47900",
|
||||
image,
|
||||
]);
|
||||
let mapped = docker_cli(&["port", &name, "47900/tcp"]);
|
||||
let port: u16 = mapped.lines().next().and_then(|l| l.rsplit(':').next()).and_then(|p| p.parse().ok()).unwrap();
|
||||
let ip = docker_cli(&["inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}", &name]);
|
||||
let mut candidates = vec![AgentEndpoint::loopback(port, token.clone())];
|
||||
for addr in ip.split_whitespace() {
|
||||
let url = url::Url::parse(&format!("http://{addr}:47900/")).unwrap();
|
||||
candidates.push(AgentEndpoint::new(url, token.clone()).unwrap());
|
||||
}
|
||||
for _ in 0..240 {
|
||||
for ep in &candidates {
|
||||
let t = ContainerTarget::Agent(ep.clone());
|
||||
if t.is_running().await.unwrap_or(false) {
|
||||
return (name, t);
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
let logs = docker_cli(&["logs", "--tail", "40", &name]);
|
||||
let _ = Command::new("docker").args(["rm", "-f", &name]).output();
|
||||
panic!("agent never answered; logs:\n{logs}");
|
||||
}
|
||||
|
||||
async fn scenario(t: &ContainerTarget) -> Result<(), String> {
|
||||
let check = |ok: bool, what: &str| if ok { Ok(()) } else { Err(what.to_string()) };
|
||||
check(probe(t).await == Some(true), "the image has tmux and the config")?;
|
||||
|
||||
let k1 = uuid::Uuid::new_v4().to_string();
|
||||
let k2 = uuid::Uuid::new_v4().to_string();
|
||||
let inner = |marker: &str| {
|
||||
vec![
|
||||
"bash".to_string(),
|
||||
"-c".to_string(),
|
||||
format!("printf 'secret=%s\\n' \"$TC_SECRET\"; for i in $(seq 1 300); do echo hist$i; done; echo {marker}; exec bash --norc\n"),
|
||||
]
|
||||
};
|
||||
let p1 = plan_with(true, true, None, &k1, TerminalKind::Bash, Some("first"), inner("READY1"), vec!["TC_SECRET=alpha-not-real".into()])?;
|
||||
let p2 = plan_with(true, true, None, &k2, TerminalKind::Claude, None, inner("READY2"), vec!["TC_SECRET=beta-not-real".into()])?;
|
||||
|
||||
// Open both tabs.
|
||||
let mut a1 = t.attach(ExecSpec::new(p1.cmd.clone()).tty(true).env(p1.env.clone())).await?;
|
||||
let seen = read_until(&mut a1.output, "READY1", 20).await;
|
||||
check(seen.contains("secret=alpha-not-real"), &format!("tab 1 has its own env: {seen:?}"))?;
|
||||
let mut a2 = t.attach(ExecSpec::new(p2.cmd.clone()).tty(true).env(p2.env.clone())).await?;
|
||||
let seen = read_until(&mut a2.output, "READY2", 20).await;
|
||||
check(seen.contains("secret=beta-not-real"), &format!("tab 2 has its own env: {seen:?}"))?;
|
||||
|
||||
// No secret on any command line in the workspace.
|
||||
let (ps, _) = t.exec_oneshot_as("claude", vec!["ps".into(), "-eo".into(), "args".into()], vec![]).await?;
|
||||
check(!ps.contains("alpha-not-real") && !ps.contains("beta-not-real"), &format!("secret on argv: {ps}"))?;
|
||||
|
||||
// The app goes away: drop the client. The agent SIGHUPs its process group.
|
||||
drop(a1);
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
let listed = list(t).await?;
|
||||
let s1 = listed.iter().find(|s| s.key == k1).ok_or(format!("tab 1 survived its client: {listed:?}"))?;
|
||||
check(!s1.attached && s1.kind == TerminalKind::Bash && s1.name.as_deref() == Some("first"), &format!("{s1:?}"))?;
|
||||
let s2 = listed.iter().find(|s| s.key == k2).ok_or("tab 2 listed")?;
|
||||
check(s2.attached && s2.kind == TerminalKind::Claude, &format!("{s2:?}"))?;
|
||||
|
||||
// History for a fresh tab, then reattach (never creating).
|
||||
let h = history(t, &k1).await.ok_or("history readable")?;
|
||||
check(h.contains("hist1\n") && h.contains("hist250"), &format!("history: {} bytes", h.len()))?;
|
||||
let mut r1 = t.attach(ExecSpec::new(attach_cmd(&k1)?).tty(true).env(client_env(vec![]))).await?;
|
||||
r1.input.write_all(b"echo back-$((40+2))\r").await.map_err(|e| e.to_string())?;
|
||||
let seen = read_until(&mut r1.output, "back-42", 20).await;
|
||||
check(seen.contains("back-42"), &format!("reattached and typing works: {seen:?}"))?;
|
||||
// Still the original server's environment.
|
||||
r1.input.write_all(b"echo env-$TC_SECRET\r").await.map_err(|e| e.to_string())?;
|
||||
let seen = read_until(&mut r1.output, "env-alpha-not-real", 20).await;
|
||||
check(seen.contains("env-alpha-not-real"), "the server kept its env")?;
|
||||
|
||||
// Attaching to a key that is gone fails and creates nothing.
|
||||
let ghost = uuid::Uuid::new_v4().to_string();
|
||||
let mut g = t.attach(ExecSpec::new(attach_cmd(&ghost)?).tty(true).env(client_env(vec![]))).await?;
|
||||
let _ = read_until(&mut g.output, "\u{0}", 5).await;
|
||||
check(!list(t).await?.iter().any(|s| s.key == ghost), "attach-session created nothing")?;
|
||||
|
||||
// A `claude` inside tmux is what the idle watcher sees; tmux is not.
|
||||
r1.input.write_all(b"cp /bin/sleep /tmp/claude && /tmp/claude 30 &\r").await.map_err(|e| e.to_string())?;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
let (pg, code) = t.exec_oneshot_as("claude", vec!["pgrep".into(), "-x".into(), "claude".into()], vec![]).await?;
|
||||
check(code == 0 && !pg.trim().is_empty(), "pgrep -x claude sees it")?;
|
||||
let (_, code) = t.exec_oneshot_as("claude", vec!["pgrep".into(), "-x".into(), "tmux".into()], vec![]).await?;
|
||||
check(code != 0, "no process is named plain `tmux` (servers are `tmux: server`)")?;
|
||||
|
||||
// Closing a tab ends it; Stop ends the rest.
|
||||
end(t, &k1).await?;
|
||||
let _ = read_until(&mut r1.output, "\u{0}", 5).await;
|
||||
check(!list(t).await?.iter().any(|s| s.key == k1), "tab 1 ended")?;
|
||||
end_all(t).await;
|
||||
check(list(t).await?.is_empty(), "every terminal ended")?;
|
||||
let (pg, _) = t.exec_oneshot_as("claude", vec!["sh".into(), "-c".into(), "pgrep -f '^tmux -L triple-c-' || true".into()], vec![]).await?;
|
||||
check(pg.trim().is_empty(), &format!("no tmux left: {pg}"))?;
|
||||
drop(a2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "needs a Docker daemon and an agent image with tmux (TRIPLEC_PERSIST_E2E_IMAGE)"]
|
||||
async fn a_terminal_outlives_its_client_and_reattaches_through_the_agent() {
|
||||
let image = std::env::var("TRIPLEC_PERSIST_E2E_IMAGE").unwrap_or_else(|_| "tc-tmux-e2e:latest".into());
|
||||
let (name, target) = start_agent_container(&image).await;
|
||||
let outcome = scenario(&target).await;
|
||||
let logs = docker_cli(&["logs", "--tail", "20", &name]);
|
||||
let _ = Command::new("docker").args(["rm", "-f", &name]).output();
|
||||
if let Err(e) = outcome {
|
||||
panic!("{e}\n--- container log tail ---\n{logs}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
use super::*;
|
||||
|
||||
const KEY: &str = "0b9c6f7e-1d2a-4c3b-9e8f-7a6b5c4d3e2f";
|
||||
|
||||
fn claude_inner() -> Vec<String> {
|
||||
vec!["bash".into(), "-c".into(), "flock … claude update\nexec claude '-n' 'x'\n".into()]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_tab_runs_its_unchanged_command_as_the_only_window_of_its_own_server() {
|
||||
let cmd = new_session_cmd(KEY, TerminalKind::Claude, Some("main"), claude_inner()).unwrap();
|
||||
assert_eq!(
|
||||
&cmd[..10],
|
||||
&[
|
||||
"tmux", "-L", &format!("triple-c-{KEY}"), "-f", "/etc/triple-c/tmux.conf",
|
||||
"new-session", "-A", "-s", "claude", "--",
|
||||
]
|
||||
.map(String::from)
|
||||
);
|
||||
// The inner command is passed through as argv, untouched.
|
||||
assert_eq!(&cmd[10..13], claude_inner().as_slice());
|
||||
assert_eq!(&cmd[13..], &[";", "set-option", "-q", "@triple_c_name", "main"].map(String::from));
|
||||
// Bash tabs say so in the session name; no label, no second command.
|
||||
let bash = new_session_cmd(KEY, TerminalKind::Bash, None, vec!["bash".into(), "-l".into()]).unwrap();
|
||||
assert_eq!(bash[8], "bash");
|
||||
assert_eq!(bash.last().unwrap(), "-l");
|
||||
assert!(!bash.contains(&";".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_are_socket_safe_and_an_argument_ending_in_a_semicolon_is_refused() {
|
||||
for bad in ["", "../x", "a b", "a;b", "x/y", &"a".repeat(65)] {
|
||||
assert!(validate_key(bad).is_err(), "{bad:?}");
|
||||
assert!(new_session_cmd(bad, TerminalKind::Bash, None, vec!["bash".into()]).is_err());
|
||||
assert!(attach_cmd(bad).is_err() && kill_cmd(bad).is_err() && history_cmd(bad).is_err());
|
||||
}
|
||||
assert!(validate_key(KEY).is_ok());
|
||||
// tmux would run what follows as tmux commands.
|
||||
let e = new_session_cmd(KEY, TerminalKind::Bash, None, vec!["sh".into(), "-c".into(), "x;".into()]);
|
||||
assert!(e.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_label_is_cleaned_so_it_cannot_split_the_tmux_command_or_the_listing() {
|
||||
assert_eq!(clean_name(Some("Bob's tab")).as_deref(), Some("Bob's tab"));
|
||||
assert_eq!(clean_name(Some("a\tb\nc")).as_deref(), Some("abc"));
|
||||
assert_eq!(clean_name(Some("x;")).as_deref(), Some("x"));
|
||||
assert_eq!(clean_name(Some(r"x \;; ")).as_deref(), Some("x"));
|
||||
assert_eq!(clean_name(Some(" ;; ")), None);
|
||||
assert_eq!(clean_name(None), None);
|
||||
assert_eq!(clean_name(Some(&"é".repeat(200))).unwrap().chars().count(), 80);
|
||||
let cmd = new_session_cmd(KEY, TerminalKind::Claude, Some("evil;"), claude_inner()).unwrap();
|
||||
assert_eq!(cmd.last().unwrap(), "evil");
|
||||
assert!(cmd.iter().filter(|a| a.ends_with(';')).count() == 1, "only our separator: {cmd:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secrets_travel_in_the_exec_environment_never_on_argv() {
|
||||
let env = vec!["ANTHROPIC_API_KEY=sk-test-not-real".to_string(), "TERM=xterm".to_string()];
|
||||
let plan = plan_with(true, true, None, KEY, TerminalKind::Claude, None, claude_inner(), env).unwrap();
|
||||
assert!(plan.persistent);
|
||||
assert!(plan.cmd.iter().all(|a| !a.contains("sk-test-not-real")), "{:?}", plan.cmd);
|
||||
assert!(!plan.cmd.iter().any(|a| a == "-e"), "no new-session -e");
|
||||
// The server inherits the client's environment; the outer TERM is xterm.js's.
|
||||
assert_eq!(plan.env, vec!["ANTHROPIC_API_KEY=sk-test-not-real".to_string(), OUTER_TERM.to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docker_projects_and_images_without_tmux_launch_as_before() {
|
||||
let inner = claude_inner();
|
||||
let env = vec!["A=1".to_string()];
|
||||
// Docker: untouched, whatever the key looks like.
|
||||
let docker = plan_with(false, true, None, "not a key", TerminalKind::Claude, None, inner.clone(), env.clone()).unwrap();
|
||||
assert_eq!((docker.cmd, docker.env, docker.persistent, docker.notice), (inner.clone(), env.clone(), false, None));
|
||||
// AWS without tmux: the old launch, plus the notice when it is due.
|
||||
let old = plan_with(true, false, Some(NO_TMUX_NOTICE.into()), KEY, TerminalKind::Claude, None, inner.clone(), env.clone()).unwrap();
|
||||
assert_eq!((old.cmd, old.env, old.persistent), (inner, env, false));
|
||||
assert!(old.notice.unwrap().contains("no tmux"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_no_tmux_notice_is_given_once_per_tunnel_and_never_for_an_unanswered_probe() {
|
||||
let id = format!("http://127.0.0.1:1/{}/", uuid::Uuid::new_v4());
|
||||
assert_eq!(take_notice(&id), None, "nothing probed: no notice");
|
||||
probes().lock().unwrap().insert(id.clone(), Probe { supported: false, noticed: false });
|
||||
assert!(take_notice(&id).is_some());
|
||||
assert_eq!(take_notice(&id), None, "once");
|
||||
// A new tunnel on the same port probes (and may notice) again.
|
||||
forget_probe(&id);
|
||||
assert!(probes().lock().unwrap().get(&id).is_none());
|
||||
probes().lock().unwrap().insert(id.clone(), Probe { supported: true, noticed: false });
|
||||
assert_eq!(take_notice(&id), None, "tmux present");
|
||||
forget_probe(&id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_listing_parses_and_skips_what_is_not_ours() {
|
||||
let out = format!(
|
||||
"{KEY}\tclaude\t1794954824\t1\tmain tab\n\
|
||||
11111111-2222-3333-4444-555555555555\tbash\t1794954800\t0\t\n\
|
||||
bad key!\tbash\t1\t0\t\n\
|
||||
abc\tvim\t1\t0\t\n\
|
||||
abc\tbash\tnot-a-time\t0\t\n"
|
||||
);
|
||||
let got = parse_list(&out);
|
||||
assert_eq!(got.len(), 2);
|
||||
assert_eq!(
|
||||
got[0],
|
||||
RunningSession { key: KEY.into(), kind: TerminalKind::Claude, created_unix: 1794954824, attached: true, name: Some("main tab".into()) }
|
||||
);
|
||||
assert!(!got[1].attached && got[1].name.is_none() && got[1].kind == TerminalKind::Bash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_workspace_scripts_only_touch_our_sockets() {
|
||||
for script in [list_cmd(), end_all_cmd()] {
|
||||
assert_eq!(&script[..2], &["sh".to_string(), "-c".to_string()]);
|
||||
assert!(script[2].contains("\"$d\"/triple-c-*"), "{}", script[2]);
|
||||
assert!(script[2].contains("${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)"));
|
||||
}
|
||||
let list = &list_cmd()[2];
|
||||
assert!(list.contains("display-message -p '#{session_name}\t#{session_created}\t#{session_attached}\t#{@triple_c_name}'"), "{list}");
|
||||
let end = &end_all_cmd()[2];
|
||||
assert!(end.contains("kill-server") && end.contains("pgrep -u \"$(id -u)\" -f '^tmux -L triple-c-'"));
|
||||
assert!(end.contains("[ $i -lt 50 ]"), "bounded wait");
|
||||
assert_eq!(probe_cmd()[2], "command -v tmux >/dev/null 2>&1 && [ -r /etc/triple-c/tmux.conf ]");
|
||||
// Reattach never creates; history is the part above the screen.
|
||||
assert_eq!(attach_cmd(KEY).unwrap()[5], "attach-session");
|
||||
assert!(!attach_cmd(KEY).unwrap().contains(&"-A".to_string()));
|
||||
let h = history_cmd(KEY).unwrap();
|
||||
assert!(h.ends_with(&["-S".into(), format!("-{HISTORY_LINES}"), "-E".into(), "-1".into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replay_pushes_the_whole_history_into_scrollback_before_tmux_draws() {
|
||||
assert!(replay_bytes("", 30).is_empty());
|
||||
assert!(replay_bytes("\n\n", 30).is_empty());
|
||||
let b = String::from_utf8(replay_bytes("one\n\x1b[31mtwo\n", 3)).unwrap();
|
||||
assert_eq!(b, "one\x1b[0m\r\n\x1b[31mtwo\x1b[0m\r\n\r\n\r\n\r\n");
|
||||
}
|
||||
|
||||
/// The config tmux is started with, as the image ships it.
|
||||
#[test]
|
||||
fn the_image_ships_the_config_and_tmux() {
|
||||
let conf = include_str!("../../../../../container/triple-c-tmux.conf");
|
||||
for line in [
|
||||
"set -g prefix None",
|
||||
"unbind-key C-b",
|
||||
"set -g status off",
|
||||
"set -s escape-time 0",
|
||||
"set -g mouse off",
|
||||
"set -g remain-on-exit off",
|
||||
"set -g destroy-unattached off",
|
||||
"set -g window-size latest",
|
||||
"set -g default-terminal \"tmux-256color\"",
|
||||
r#"set -as terminal-overrides ",xterm*:smcup@:rmcup@:indn@:clear=\\E[H""#,
|
||||
] {
|
||||
assert!(conf.lines().any(|l| l == line), "missing: {line}");
|
||||
}
|
||||
let df = include_str!("../../../../../container/Dockerfile");
|
||||
assert!(df.contains("COPY triple-c-tmux.conf /etc/triple-c/tmux.conf"));
|
||||
assert!(df.lines().any(|l| l.trim() == "tmux \\"), "tmux in an apt layer");
|
||||
}
|
||||
|
||||
/// An unanswered probe (a tunnel reopening) is an error, never "no tmux":
|
||||
/// read as no, the listing would prune every remembered tab and a reattach
|
||||
/// would end tabs whose Claude still runs.
|
||||
#[tokio::test]
|
||||
async fn an_unanswered_probe_is_an_error_not_a_no() {
|
||||
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
|
||||
let target = ContainerTarget::Agent(
|
||||
crate::host::AgentEndpoint::loopback(port, "x".repeat(64)),
|
||||
);
|
||||
assert_eq!(probe(&target).await, None);
|
||||
assert!(supported(&target).await.is_err());
|
||||
assert!(list(&target).await.is_err(), "no listing, so nothing to prune");
|
||||
let planned = plan(true, &target, KEY, TerminalKind::Claude, None, claude_inner(), vec![]).await;
|
||||
assert!(planned.is_err(), "not silently a non-persistent tab");
|
||||
assert!(probes().lock().unwrap().get(target.id()).is_none(), "asked again next time");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_client_env_drops_what_would_point_tmux_elsewhere() {
|
||||
let env = client_env(vec!["TMUX=/tmp/x,1,0".into(), "TMUX_TMPDIR=/elsewhere".into(), "A=1".into()]);
|
||||
assert_eq!(env, vec!["A=1".to_string(), OUTER_TERM.to_string()]);
|
||||
}
|
||||
@@ -11,7 +11,9 @@
|
||||
//! ## Two differences from Docker, both deliberate
|
||||
//!
|
||||
//! * **It reaches the workspace's loopback, not its network interface**
|
||||
//! (`socat … TCP:127.0.0.1:<port>` inside the task). So a mapping can
|
||||
//! (`socat` to `127.0.0.1:<port>`, or `[::1]:<port>` when only an IPv6
|
||||
//! loopback/wildcard socket listens there — see
|
||||
//! [`crate::auth_bridge::tunnel::mapping_command`]). So a mapping can
|
||||
//! reach a service that bound loopback precisely to be private — which is
|
||||
//! why the agent's port, the browser view's range and (when the project
|
||||
//! runs it) the gateway sidecar's [`GATEWAY_SIDECAR_PORT`] are refused.
|
||||
@@ -46,7 +48,6 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::auth_bridge::proc_net::PortFamily;
|
||||
use crate::auth_bridge::tunnel::PortForward;
|
||||
use crate::auth_bridge::{RESERVED_CONTAINER_PORTS, RESERVED_HOST_PORTS};
|
||||
use crate::models::PortMapping;
|
||||
@@ -54,6 +55,18 @@ use crate::storage::projects_store::ProjectsStore;
|
||||
|
||||
use super::ContainerTarget;
|
||||
|
||||
/// Where a warning about a mapping goes after the open has returned: the
|
||||
/// first connection that finds nothing listening in the workspace.
|
||||
pub type PortsWarn = Arc<dyn Fn(String) + Send + Sync>;
|
||||
|
||||
/// What the user is told when a mapping's workspace port has no listener.
|
||||
pub fn nothing_listening(host_port: u16, container_port: u16) -> String {
|
||||
format!(
|
||||
"localhost:{host_port}: nothing is listening on port {container_port} in the workspace \
|
||||
(127.0.0.1 or ::1)"
|
||||
)
|
||||
}
|
||||
|
||||
/// What became of one mapping: `error` is `None` when its forward is open.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PortForwardResult {
|
||||
@@ -137,12 +150,23 @@ fn mapping_problem(m: &PortMapping, gateway_sidecar: bool) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// A row the editor saved before both its ports were typed (the editor
|
||||
/// saves on blur): nothing to check or forward yet, and nothing to say.
|
||||
/// A row stored without both its ports (an older editor saved a new row as
|
||||
/// 0 → 0 before anything was typed): nothing to check on save, and nothing
|
||||
/// to forward — but a connect says so ([`incomplete_warning`]), never silent.
|
||||
fn incomplete(m: &PortMapping) -> bool {
|
||||
m.host_port == 0 || m.container_port == 0
|
||||
}
|
||||
|
||||
/// The `ports` warning line for a stored row a connect skips as incomplete.
|
||||
pub fn incomplete_warning(m: &PortMapping) -> String {
|
||||
let port = |p: u16| if p == 0 { "?".to_string() } else { p.to_string() };
|
||||
format!(
|
||||
"{} → {}: incomplete, both ports are needed",
|
||||
port(m.host_port),
|
||||
port(m.container_port)
|
||||
)
|
||||
}
|
||||
|
||||
/// An AWS project's port mappings, checked in full: TCP only, no reserved
|
||||
/// port (the browser view's two ranges, the agent's port, the gateway
|
||||
/// sidecar's when it runs), and no host port twice. The first problem,
|
||||
@@ -215,7 +239,7 @@ pub async fn open_for(
|
||||
mappings: &[PortMapping],
|
||||
) -> Vec<PortForwardResult> {
|
||||
let mappings = mappings.to_vec();
|
||||
open_latest(project_id, target, move || Some((mappings, false))).await
|
||||
open_latest(project_id, target, None, move || Some((mappings, false))).await
|
||||
}
|
||||
|
||||
/// Open the project's mappings into `target`, replacing the set it had.
|
||||
@@ -232,21 +256,31 @@ pub async fn open_for(
|
||||
pub async fn open_latest(
|
||||
project_id: &str,
|
||||
target: &ContainerTarget,
|
||||
warn: Option<PortsWarn>,
|
||||
read: impl FnOnce() -> Option<(Vec<PortMapping>, bool)> + Send,
|
||||
) -> Vec<PortForwardResult> {
|
||||
let plock = project_lock(project_id);
|
||||
let _held = plock.lock().await;
|
||||
let Some((mappings, gateway_sidecar)) = read() else {
|
||||
log::info!("Project {project_id}: no longer an AWS project; closing its port forwards");
|
||||
close_unlocked(project_id).await;
|
||||
return Vec::new();
|
||||
};
|
||||
log::info!(
|
||||
"Project {project_id}: opening port mappings into {} ({} stored, {} incomplete)",
|
||||
target.id(),
|
||||
mappings.len(),
|
||||
mappings.iter().filter(|m| incomplete(m)).count()
|
||||
);
|
||||
|
||||
// What can be forwarded, in order; the first row wins a host port.
|
||||
let mut results: Vec<PortForwardResult> = Vec::new();
|
||||
let mut wanted: Vec<(usize, u16, u16)> = Vec::new();
|
||||
let mut taken = HashSet::new();
|
||||
for m in mappings.iter().filter(|m| !incomplete(m)) {
|
||||
let error = if let Some(problem) = mapping_problem(m, gateway_sidecar) {
|
||||
for m in &mappings {
|
||||
let error = if incomplete(m) {
|
||||
Some(incomplete_warning(m))
|
||||
} else if let Some(problem) = mapping_problem(m, gateway_sidecar) {
|
||||
Some(problem)
|
||||
} else if !taken.insert(m.host_port) {
|
||||
Some(format!(
|
||||
@@ -284,7 +318,12 @@ pub async fn open_latest(
|
||||
opened.push(f);
|
||||
continue;
|
||||
}
|
||||
match PortForward::bind_mapping(target.clone(), host_port, container_port, PortFamily::V4).await {
|
||||
let on_refused = warn.clone().map(|w| {
|
||||
let notice: crate::auth_bridge::tunnel::RefusedNotice =
|
||||
Arc::new(move || w(nothing_listening(host_port, container_port)));
|
||||
notice
|
||||
});
|
||||
match PortForward::bind_mapping(target.clone(), host_port, container_port, on_refused).await {
|
||||
Ok(fwd) => {
|
||||
if let Some(w) = &fwd.ipv6_warning {
|
||||
log::warn!("Project {project_id}: port mapping {host_port}: {w}");
|
||||
@@ -313,11 +352,25 @@ pub async fn open_latest(
|
||||
// Checked *after* the set is registered: a tunnel close that ran before
|
||||
// this sees it gone here; one that runs after removes it itself.
|
||||
if !still_connected(project_id, target) {
|
||||
log::warn!(
|
||||
"Project {project_id}: the workspace's tunnel is no longer {} — closing the port forwards just opened",
|
||||
target.id()
|
||||
);
|
||||
close_unlocked(project_id).await;
|
||||
for r in results.iter_mut().filter(|r| r.error.is_none()) {
|
||||
r.error = Some("the workspace disconnected".into());
|
||||
}
|
||||
}
|
||||
for r in &results {
|
||||
match &r.error {
|
||||
None => log::info!(
|
||||
"Project {project_id}: port mapping localhost:{} → workspace port {} is open",
|
||||
r.mapping.host_port,
|
||||
r.mapping.container_port
|
||||
),
|
||||
Some(e) => log::warn!("Project {project_id}: port mapping not open: {e}"),
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
@@ -337,6 +390,9 @@ pub async fn close(project_id: &str) {
|
||||
async fn close_unlocked(project_id: &str) {
|
||||
let set = lock().remove(project_id);
|
||||
if let Some(set) = set {
|
||||
if !set.forwards.is_empty() {
|
||||
log::info!("Project {project_id}: closing {} port forward(s)", set.forwards.len());
|
||||
}
|
||||
shutdown_all(set.forwards).await;
|
||||
}
|
||||
}
|
||||
@@ -352,20 +408,38 @@ pub async fn close_all() {
|
||||
/// After a connect (or a save of the mappings while connected): open an
|
||||
/// AWS project's mappings, **as stored when the open runs**, into its
|
||||
/// connected workspace. The warning detail for the mappings that failed, if
|
||||
/// any did; `None` as well for a local project (Docker publishes its own) or
|
||||
/// a workspace that is not (or no longer) connected.
|
||||
pub async fn open_for_project(store: &ProjectsStore, project_id: &str) -> Option<String> {
|
||||
/// any did; `None` for a local project (Docker publishes its own), and for a
|
||||
/// workspace that is not (or no longer) connected **unless the project is
|
||||
/// Running** — mappings that should be open and aren't are never silent.
|
||||
/// `warn` receives the later "nothing is listening" notices.
|
||||
pub async fn open_for_project(
|
||||
store: &ProjectsStore,
|
||||
project_id: &str,
|
||||
warn: Option<PortsWarn>,
|
||||
) -> Option<String> {
|
||||
let project = store.get(project_id).filter(|p| p.is_aws())?;
|
||||
let target = ContainerTarget::Agent(super::tunnels::get_endpoint(&project.id)?);
|
||||
let results = open_latest(project_id, &target, || {
|
||||
let wanted = project.port_mappings.iter().filter(|m| !incomplete(m)).count();
|
||||
let running = || {
|
||||
store.get(project_id).is_some_and(|p| p.status == crate::models::ProjectStatus::Running)
|
||||
};
|
||||
let Some(endpoint) = super::tunnels::get_endpoint(&project.id) else {
|
||||
log::info!("Project {project_id}: no tunnel to the workspace; {wanted} port mapping(s) not opened");
|
||||
return (wanted > 0 && running()).then(|| {
|
||||
"The workspace is not connected, so its port mappings were not opened. Start the \
|
||||
project again."
|
||||
.to_string()
|
||||
});
|
||||
};
|
||||
let target = ContainerTarget::Agent(endpoint);
|
||||
let results = open_latest(project_id, &target, warn, || {
|
||||
store.get(project_id).filter(|p| p.is_aws()).map(|p| {
|
||||
let gateway = p.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
|
||||
(p.port_mappings, gateway)
|
||||
})
|
||||
})
|
||||
.await;
|
||||
if !still_connected(project_id, &target) {
|
||||
return None;
|
||||
if !still_connected(project_id, &target) && !running() {
|
||||
return None; // a Stop raced the open: nothing to say
|
||||
}
|
||||
failure_detail(&results)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ struct Fake {
|
||||
requests: AtomicUsize,
|
||||
/// The command of each attach.
|
||||
cmds: std::sync::Mutex<Vec<Vec<String>>>,
|
||||
/// Answer every attach as socat finding nothing listening.
|
||||
refuse: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
async fn attach_ws(State(f): State<Arc<Fake>>, ws: WebSocketUpgrade) -> Response {
|
||||
@@ -41,6 +43,14 @@ async fn echo(f: Arc<Fake>, mut ws: WebSocket) {
|
||||
.collect();
|
||||
f.cmds.lock().unwrap().push(cmd);
|
||||
let _ = ws.send(WsMessage::Text(r#"{"type":"started","exec_id":"a1"}"#.into())).await;
|
||||
if f.refuse.load(Ordering::SeqCst) {
|
||||
let mut err = vec![2u8];
|
||||
err.extend_from_slice(b"socat[7] E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused\n");
|
||||
let _ = ws.send(WsMessage::Binary(err.into())).await;
|
||||
let _ = ws.send(WsMessage::Text(r#"{"type":"exit","code":1}"#.into())).await;
|
||||
let _ = ws.close().await;
|
||||
return;
|
||||
}
|
||||
while let Some(Ok(msg)) = ws.recv().await {
|
||||
match msg {
|
||||
WsMessage::Binary(b) => {
|
||||
@@ -129,7 +139,7 @@ async fn a_busy_host_port_fails_its_mapping_alone_and_the_other_forwards() {
|
||||
assert_eq!(round_trip(free, b"ping").await, b"ping");
|
||||
assert_eq!(
|
||||
fake.cmds.lock().unwrap().as_slice(),
|
||||
[vec!["socat".to_string(), "-".into(), "TCP:127.0.0.1:5432".into()]]
|
||||
[crate::auth_bridge::tunnel::mapping_command(5432)]
|
||||
);
|
||||
|
||||
// Stop / Remove / task death all close the tunnel: the mappings go too.
|
||||
@@ -188,7 +198,8 @@ async fn udp_reserved_and_duplicate_mappings_are_refused_per_mapping() {
|
||||
)
|
||||
.await;
|
||||
let errors: Vec<Option<&str>> = results.iter().map(|r| r.error.as_deref()).collect();
|
||||
assert_eq!(errors.len(), 6, "an unfinished row is skipped: {errors:?}");
|
||||
assert_eq!(errors.len(), 7, "{errors:?}");
|
||||
assert_eq!(errors[6], Some("? → ?: incomplete, both ports are needed"), "skipped, not silently");
|
||||
assert!(errors[0].unwrap().contains("only TCP"), "{errors:?}");
|
||||
assert!(errors[1].is_none());
|
||||
assert!(errors[2].unwrap().contains("another mapping"), "{errors:?}");
|
||||
@@ -310,7 +321,7 @@ async fn overlapping_opens_converge_on_the_last_stored_set_without_false_errors(
|
||||
let mine = sets[i % sets.len()].clone();
|
||||
opens.push(tokio::spawn(async move {
|
||||
go2.wait().await;
|
||||
open_latest(&p2, &t2, move || {
|
||||
open_latest(&p2, &t2, None, move || {
|
||||
let mut s = s2.lock().unwrap();
|
||||
*s = mine; // this save lands, then its open reads the store
|
||||
Some((s.clone(), false))
|
||||
@@ -352,7 +363,7 @@ async fn a_save_keeps_unchanged_forwards_and_their_live_connections() {
|
||||
let r = open_for(&p, &target, &[tcp(a, 9090)]).await;
|
||||
assert!(r[0].error.is_none(), "{:?}", r[0].error);
|
||||
assert_eq!(round_trip(a, b"x").await, b"x");
|
||||
assert_eq!(fake.cmds.lock().unwrap().last().unwrap()[2], "TCP:127.0.0.1:9090");
|
||||
assert_eq!(fake.cmds.lock().unwrap().last().unwrap(), &crate::auth_bridge::tunnel::mapping_command(9090));
|
||||
assert!(port_is_free(b));
|
||||
tunnels::close(&p).await;
|
||||
}
|
||||
@@ -371,9 +382,9 @@ async fn a_stored_project_is_read_when_the_open_runs() {
|
||||
let busy_port = busy.local_addr().unwrap().port();
|
||||
project.port_mappings = vec![tcp(port, 8080), tcp(busy_port, 8081)];
|
||||
let store = ProjectsStore::over_file_for_test(dir.path().join("projects.json"), vec![project]);
|
||||
assert_eq!(open_for_project(&store, &p).await, None, "not connected: nothing, no warning");
|
||||
assert_eq!(open_for_project(&store, &p, None).await, None, "not connected: nothing, no warning");
|
||||
let (_target, _, _) = connected_agent(&p).await;
|
||||
let warning = open_for_project(&store, &p).await.expect("the busy port is reported");
|
||||
let warning = open_for_project(&store, &p, None).await.expect("the busy port is reported");
|
||||
assert!(warning.contains(&busy_port.to_string()) && warning.contains("already in use"), "{warning}");
|
||||
assert_eq!(open_ports(&p), vec![port]);
|
||||
tunnels::close(&p).await;
|
||||
@@ -386,7 +397,7 @@ async fn the_auth_bridges_forward_still_waits_for_a_client_to_speak() {
|
||||
let p = pid("bridge");
|
||||
let (target, fake, _) = connected_agent(&p).await;
|
||||
let port = free_port();
|
||||
let mut fwd = PortForward::bind(target, port, port, PortFamily::V4).await.unwrap();
|
||||
let mut fwd = PortForward::bind(target, port, port, crate::auth_bridge::proc_net::PortFamily::V4).await.unwrap();
|
||||
assert_eq!((fwd.port, fwd.container_port), (port, port));
|
||||
let _client = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
@@ -394,3 +405,202 @@ async fn the_auth_bridges_forward_still_waits_for_a_client_to_speak() {
|
||||
fwd.shutdown().await;
|
||||
tunnels::close(&p).await;
|
||||
}
|
||||
|
||||
fn aws_project_in_store(p: &str, status: crate::models::ProjectStatus, mappings: Vec<PortMapping>) -> (tempfile::TempDir, ProjectsStore) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut project = crate::models::Project::new("x".into(), vec![]);
|
||||
project.id = p.to_string();
|
||||
project.host_id = Some("h1".into());
|
||||
project.aws_placement =
|
||||
Some(crate::models::AwsPlacement { region: "us-west-2".into(), environment: "dev".into() });
|
||||
project.aws = Some(Default::default());
|
||||
project.status = status;
|
||||
project.port_mappings = mappings;
|
||||
let store = ProjectsStore::over_file_for_test(dir.path().join("projects.json"), vec![project]);
|
||||
(dir, store)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_running_project_whose_mappings_cannot_open_is_never_silent() {
|
||||
use crate::models::ProjectStatus;
|
||||
let p = pid("silent");
|
||||
// Stopped (a save while stopped): nothing to open, nothing to say.
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Stopped, vec![tcp(free_port(), 8080)]);
|
||||
assert_eq!(open_for_project(&store, &p, None).await, None);
|
||||
// Running with no tunnel: the mappings should be open and are not.
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Running, vec![tcp(free_port(), 8080)]);
|
||||
let w = open_for_project(&store, &p, None).await.expect("a warning");
|
||||
assert!(w.contains("not connected"), "{w}");
|
||||
// Only incomplete rows: nothing to say.
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Running, vec![tcp(0, 8080)]);
|
||||
assert_eq!(open_for_project(&store, &p, None).await, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_connect_warns_about_an_incomplete_row_and_opens_the_rest() {
|
||||
use crate::models::ProjectStatus;
|
||||
// The reported case: an older editor stored a new row as 0 → 0. The
|
||||
// connect used to skip it in silence.
|
||||
let p = pid("incomplete");
|
||||
let port = free_port();
|
||||
let (_d, store) =
|
||||
aws_project_in_store(&p, ProjectStatus::Stopped, vec![tcp(0, 0), tcp(port, 8080), tcp(9000, 0)]);
|
||||
let (_target, _fake, _) = connected_agent(&p).await;
|
||||
store.update_status(&p, ProjectStatus::Running).unwrap();
|
||||
let w = open_for_project(&store, &p, None).await.expect("a ports warning");
|
||||
assert_eq!(
|
||||
w,
|
||||
"? → ?: incomplete, both ports are needed\n9000 → ?: incomplete, both ports are needed"
|
||||
);
|
||||
assert_eq!(open_ports(&p), vec![port]);
|
||||
tunnels::close(&p).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_start_opens_a_listener_for_a_mapping_saved_before_it() {
|
||||
use crate::models::ProjectStatus;
|
||||
// The reported case: 8080 → 8080 saved while stopped, then a start
|
||||
// registers the tunnel, sets Running and opens the mappings.
|
||||
let p = pid("start");
|
||||
let port = free_port();
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Stopped, vec![tcp(port, 8080)]);
|
||||
let (_target, fake, _) = connected_agent(&p).await;
|
||||
store.update_status(&p, ProjectStatus::Running).unwrap();
|
||||
assert_eq!(open_for_project(&store, &p, None).await, None);
|
||||
assert_eq!(open_ports(&p), vec![port]);
|
||||
assert_eq!(round_trip(port, b"ping").await, b"ping");
|
||||
// Over IPv6 loopback too (`localhost` may resolve to ::1 first).
|
||||
if let Ok(mut c) = TcpStream::connect(("::1", port)).await {
|
||||
c.write_all(b"six").await.unwrap();
|
||||
let mut buf = [0u8; 3];
|
||||
c.read_exact(&mut buf).await.unwrap();
|
||||
assert_eq!(&buf, b"six");
|
||||
}
|
||||
assert_eq!(fake.cmds.lock().unwrap()[0], crate::auth_bridge::tunnel::mapping_command(8080));
|
||||
tunnels::close(&p).await;
|
||||
assert!(port_is_free(port));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nothing_listening_in_the_workspace_is_reported_once_per_mapping() {
|
||||
use crate::models::ProjectStatus;
|
||||
let p = pid("refused");
|
||||
let port = free_port();
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Running, vec![tcp(port, 8080)]);
|
||||
let (_target, fake, _) = connected_agent(&p).await;
|
||||
fake.refuse.store(true, Ordering::SeqCst);
|
||||
let seen = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
|
||||
let s2 = seen.clone();
|
||||
let warn: PortsWarn = Arc::new(move |d| s2.lock().unwrap().push(d));
|
||||
assert_eq!(open_for_project(&store, &p, Some(warn)).await, None);
|
||||
for _ in 0..3 {
|
||||
let mut c = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
|
||||
let mut buf = Vec::new();
|
||||
let _ = tokio::time::timeout(std::time::Duration::from_secs(5), c.read_to_end(&mut buf)).await;
|
||||
}
|
||||
let seen = seen.lock().unwrap().clone();
|
||||
assert_eq!(seen, vec![nothing_listening(port, 8080)]);
|
||||
assert!(seen[0].contains("nothing is listening on port 8080 in the workspace (127.0.0.1 or ::1)"));
|
||||
tunnels::close(&p).await;
|
||||
}
|
||||
|
||||
/// The reported case end to end against a **real** agent in a real
|
||||
/// container (no SSM; the tunnel is registered by hand): a mapping saved
|
||||
/// before the start, a Python server on `0.0.0.0:8080` (the user's), one on
|
||||
/// `[::1]` only, and a port nothing listens on. Needs Docker and an image
|
||||
/// whose entrypoint starts the agent (`TRIPLEC_AGENT_E2E_IMAGE`, default
|
||||
/// `ghcr.io/shadowdao/triple-c-sandbox:edge`).
|
||||
#[tokio::test]
|
||||
#[ignore = "needs a Docker daemon and an image whose entrypoint starts triple-c-agent"]
|
||||
async fn live_a_mapping_reaches_v4_and_v6_servers_in_a_real_workspace() {
|
||||
use crate::models::ProjectStatus;
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
let docker = |args: &[&str]| -> String {
|
||||
let out = Command::new("docker").args(args).output().expect("docker cli");
|
||||
assert!(out.status.success(), "docker {args:?}: {}", String::from_utf8_lossy(&out.stderr));
|
||||
String::from_utf8_lossy(&out.stdout).trim().to_string()
|
||||
};
|
||||
let image = std::env::var("TRIPLEC_AGENT_E2E_IMAGE")
|
||||
.unwrap_or_else(|_| "ghcr.io/shadowdao/triple-c-sandbox:edge".into());
|
||||
let token = format!("{}{}", uuid::Uuid::new_v4().simple(), uuid::Uuid::new_v4().simple());
|
||||
let name = format!("tc-ports-e2e-{}", uuid::Uuid::new_v4().simple());
|
||||
docker(&[
|
||||
"run", "-d", "--name", &name, "-w", "/workspace",
|
||||
"-e", &format!("TRIPLEC_AGENT_TOKEN={token}"),
|
||||
"-e", "TRIPLEC_AGENT_ADDR=0.0.0.0:47900",
|
||||
"-p", "127.0.0.1::47900",
|
||||
&image,
|
||||
]);
|
||||
let p = pid("live");
|
||||
let outcome = async {
|
||||
let mapped = docker(&["port", &name, "47900/tcp"]);
|
||||
let lport: u16 = mapped.lines().next().and_then(|l| l.rsplit(':').next()).and_then(|x| x.parse().ok()).unwrap();
|
||||
let ip = docker(&["inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}", &name]);
|
||||
let mut candidates = vec![AgentEndpoint::loopback(lport, token.clone())];
|
||||
for a in ip.split_whitespace() {
|
||||
candidates.push(AgentEndpoint::new(url::Url::parse(&format!("http://{a}:47900/")).unwrap(), token.clone()).unwrap());
|
||||
}
|
||||
let mut ep = None;
|
||||
'wait: for _ in 0..240 {
|
||||
for c in &candidates {
|
||||
if ContainerTarget::Agent(c.clone()).is_running().await.unwrap_or(false) {
|
||||
ep = Some(c.clone());
|
||||
break 'wait;
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
let ep = ep.ok_or("agent never answered")?;
|
||||
docker(&["exec", "-d", "-u", "claude", &name, "python3", "-m", "http.server", "8080", "--bind", "0.0.0.0"]);
|
||||
docker(&["exec", "-d", "-u", "claude", &name, "python3", "-m", "http.server", "8081", "--bind", "::1"]);
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
|
||||
let (h80, h81, h82) = (free_port(), free_port(), free_port());
|
||||
let (_d, store) = aws_project_in_store(&p, ProjectStatus::Stopped, vec![tcp(h80, 8080), tcp(h81, 8081), tcp(h82, 8082)]);
|
||||
// The start: tunnel registered, Running, then the open.
|
||||
let mut t = tunnels::tests::fake_tunnel(lport, Arc::new(AtomicUsize::new(0)));
|
||||
t.endpoint = ep;
|
||||
tunnels::register(&p, t);
|
||||
store.update_status(&p, ProjectStatus::Running).unwrap();
|
||||
let seen = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
|
||||
let s2 = seen.clone();
|
||||
let warn: PortsWarn = Arc::new(move |d| s2.lock().unwrap().push(d));
|
||||
if let Some(w) = open_for_project(&store, &p, Some(warn)).await {
|
||||
return Err(format!("open warned: {w}"));
|
||||
}
|
||||
async fn get(addr: (&str, u16)) -> Result<String, String> {
|
||||
let mut c = TcpStream::connect(addr).await.map_err(|e| format!("{addr:?}: {e}"))?;
|
||||
c.write_all(b"GET / HTTP/1.0\r\nHost: localhost\r\n\r\n").await.map_err(|e| e.to_string())?;
|
||||
let mut buf = Vec::new();
|
||||
tokio::time::timeout(Duration::from_secs(20), c.read_to_end(&mut buf)).await.map_err(|_| "timeout".to_string())?.map_err(|e| e.to_string())?;
|
||||
Ok(String::from_utf8_lossy(&buf).into_owned())
|
||||
}
|
||||
for (port, what) in [(h80, "0.0.0.0:8080"), (h81, "[::1]:8081")] {
|
||||
for host in ["127.0.0.1", "::1"] {
|
||||
let r = get((host, port)).await?;
|
||||
if !r.starts_with("HTTP/1.0 200") {
|
||||
return Err(format!("{what} via {host}:{port}: {r:?}"));
|
||||
}
|
||||
eprintln!("{what} via {host}:{port}: 200");
|
||||
}
|
||||
}
|
||||
let r = get(("127.0.0.1", h82)).await?;
|
||||
if !r.is_empty() {
|
||||
return Err(format!("nothing listens on 8082, yet: {r:?}"));
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
let seen = seen.lock().unwrap().clone();
|
||||
if seen != vec![nothing_listening(h82, 8082)] {
|
||||
return Err(format!("warnings: {seen:?}"));
|
||||
}
|
||||
Ok::<(), String>(())
|
||||
}
|
||||
.await;
|
||||
tunnels::close(&p).await;
|
||||
let logs = Command::new("docker").args(["logs", "--tail", "20", &name]).output().map(|o| String::from_utf8_lossy(&o.stdout).into_owned()).unwrap_or_default();
|
||||
let _ = Command::new("docker").args(["rm", "-f", &name]).output();
|
||||
if let Err(e) = outcome {
|
||||
panic!("{e}\n--- container log tail ---\n{logs}");
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
@@ -22,6 +23,10 @@ pub struct ExecSession {
|
||||
pub target: ContainerTarget,
|
||||
pub input_tx: mpsc::UnboundedSender<Vec<u8>>,
|
||||
shutdown_tx: mpsc::Sender<()>,
|
||||
/// Set when a new exec took this session id over (a persistent AWS
|
||||
/// terminal re-attached): this one then ends without its exit callback,
|
||||
/// so the tab is not told its session ended.
|
||||
superseded: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl ExecSession {
|
||||
@@ -106,6 +111,8 @@ impl ExecSessionManager {
|
||||
|
||||
let (input_tx, mut input_rx) = mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
|
||||
let superseded = Arc::new(AtomicBool::new(false));
|
||||
let superseded_reader = superseded.clone();
|
||||
|
||||
// Output reader task
|
||||
let session_id_clone = session_id.to_string();
|
||||
@@ -134,7 +141,9 @@ impl ExecSessionManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
on_exit();
|
||||
if !superseded_reader.load(Ordering::SeqCst) {
|
||||
on_exit();
|
||||
}
|
||||
let _ = shutdown_tx_clone;
|
||||
});
|
||||
|
||||
@@ -153,12 +162,18 @@ impl ExecSessionManager {
|
||||
target: target.clone(),
|
||||
input_tx,
|
||||
shutdown_tx,
|
||||
superseded,
|
||||
};
|
||||
|
||||
self.sessions
|
||||
let old = self
|
||||
.sessions
|
||||
.lock()
|
||||
.await
|
||||
.insert(session_id.to_string(), session);
|
||||
if let Some(old) = old {
|
||||
old.superseded.store(true, Ordering::SeqCst);
|
||||
old.shutdown();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -198,27 +198,48 @@ fn sdk_error(e: &(dyn std::error::Error + 'static)) -> String {
|
||||
|
||||
// ── The plugin ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Find `session-manager-plugin`: `TRIPLEC_SSM_PLUGIN` if set, then `PATH`,
|
||||
/// then where AWS's installers put it (the GUI app on macOS does not get the
|
||||
/// shell's `PATH`, so the install locations are not optional).
|
||||
/// The plugin's file name on this platform: what the bundle holds (Tauri strips the
|
||||
/// `-<target-triple>` suffix of `bundle.externalBin` when it copies the sidecar in) and what
|
||||
/// AWS's installers put on `PATH`.
|
||||
const PLUGIN_FILE: &str =
|
||||
if cfg!(windows) { "session-manager-plugin.exe" } else { "session-manager-plugin" };
|
||||
|
||||
const PLUGIN_INSTALL_HINT: &str = "The AWS Session Manager plugin (session-manager-plugin) is \
|
||||
not installed. It is what connects to a workspace on AWS; install it from \
|
||||
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html";
|
||||
|
||||
/// Find `session-manager-plugin`, in this order:
|
||||
///
|
||||
/// 1. `TRIPLEC_SSM_PLUGIN`, if set (an error if it names no file — an explicit choice is never
|
||||
/// silently replaced by another plugin);
|
||||
/// 2. **the copy bundled with the app** (`bundle.externalBin`), beside the app's own
|
||||
/// executable: `usr/bin` in the AppImage, `Contents/MacOS` in the macOS bundle, the install
|
||||
/// directory on Windows (and `target/<profile>` under `tauri dev`, where tauri-build copies
|
||||
/// it). A debug build's placeholder (`sidecar_placeholder`) is skipped;
|
||||
/// 3. `PATH`;
|
||||
/// 4. where AWS's installers put it (the GUI app on macOS does not get the shell's `PATH`, so
|
||||
/// the install locations are not optional).
|
||||
///
|
||||
/// 3 and 4 remain for dev builds without a fetched sidecar.
|
||||
pub fn find_plugin() -> Result<PathBuf, String> {
|
||||
if let Some(p) = std::env::var_os("TRIPLEC_SSM_PLUGIN").filter(|p| !p.is_empty()) {
|
||||
let p = PathBuf::from(p);
|
||||
return if p.is_file() {
|
||||
Ok(p)
|
||||
} else {
|
||||
Err(format!("TRIPLEC_SSM_PLUGIN names {}, which is not a file", p.display()))
|
||||
};
|
||||
}
|
||||
let name = if cfg!(windows) { "session-manager-plugin.exe" } else { "session-manager-plugin" };
|
||||
let on_path = std::env::var_os("PATH")
|
||||
.map(|paths| std::env::split_paths(&paths).map(|d| d.join(name)).collect::<Vec<_>>())
|
||||
.unwrap_or_default();
|
||||
let installed: Vec<PathBuf> = if cfg!(windows) {
|
||||
let exe_dir = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|exe| exe.parent().map(Path::to_path_buf));
|
||||
resolve_plugin(&PluginSearch {
|
||||
override_path: std::env::var_os("TRIPLEC_SSM_PLUGIN"),
|
||||
exe_dir,
|
||||
path_var: std::env::var_os("PATH"),
|
||||
install_dirs: plugin_install_dirs(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Where AWS's installers put the plugin, in the order they are tried.
|
||||
fn plugin_install_dirs() -> Vec<PathBuf> {
|
||||
if cfg!(windows) {
|
||||
["ProgramFiles", "ProgramFiles(x86)"]
|
||||
.iter()
|
||||
.filter_map(|v| std::env::var_os(v))
|
||||
.map(|d| PathBuf::from(d).join(r"Amazon\SessionManagerPlugin\bin").join(name))
|
||||
.map(|d| PathBuf::from(d).join(r"Amazon\SessionManagerPlugin\bin"))
|
||||
.collect()
|
||||
} else {
|
||||
[
|
||||
@@ -228,19 +249,54 @@ pub fn find_plugin() -> Result<PathBuf, String> {
|
||||
"/usr/bin",
|
||||
]
|
||||
.iter()
|
||||
.map(|d| Path::new(d).join(name))
|
||||
.map(PathBuf::from)
|
||||
.collect()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything [`find_plugin`] reads from the process, so the order is testable.
|
||||
struct PluginSearch {
|
||||
override_path: Option<std::ffi::OsString>,
|
||||
exe_dir: Option<PathBuf>,
|
||||
path_var: Option<std::ffi::OsString>,
|
||||
install_dirs: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
fn resolve_plugin(search: &PluginSearch) -> Result<PathBuf, String> {
|
||||
if let Some(p) = search.override_path.as_ref().filter(|p| !p.is_empty()) {
|
||||
let p = PathBuf::from(p);
|
||||
return if p.is_file() {
|
||||
Ok(p)
|
||||
} else {
|
||||
Err(format!("TRIPLEC_SSM_PLUGIN names {}, which is not a file", p.display()))
|
||||
};
|
||||
}
|
||||
if let Some(bundled) = search.exe_dir.as_ref().map(|d| d.join(PLUGIN_FILE)) {
|
||||
if bundled.is_file() && !is_sidecar_placeholder(&bundled) {
|
||||
return Ok(bundled);
|
||||
}
|
||||
}
|
||||
let on_path = search
|
||||
.path_var
|
||||
.as_ref()
|
||||
.map(|paths| std::env::split_paths(paths).map(|d| d.join(PLUGIN_FILE)).collect::<Vec<_>>())
|
||||
.unwrap_or_default();
|
||||
on_path
|
||||
.into_iter()
|
||||
.chain(installed)
|
||||
.chain(search.install_dirs.iter().map(|d| d.join(PLUGIN_FILE)))
|
||||
.find(|p| p.is_file())
|
||||
.ok_or_else(|| {
|
||||
"The AWS Session Manager plugin (session-manager-plugin) is not installed. It is \
|
||||
what connects to a workspace on AWS; install it from \
|
||||
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
|
||||
.to_string()
|
||||
})
|
||||
.ok_or_else(|| PLUGIN_INSTALL_HINT.to_string())
|
||||
}
|
||||
|
||||
/// Whether `path` is the stand-in a debug build bundles when no real plugin was fetched.
|
||||
/// Unreadable counts as "not a placeholder": the spawn then reports the real error.
|
||||
fn is_sidecar_placeholder(path: &Path) -> bool {
|
||||
use std::io::Read;
|
||||
let mut head = [0u8; crate::sidecar_placeholder::MARKER.len()];
|
||||
std::fs::File::open(path)
|
||||
.and_then(|mut f| f.read_exact(&mut head))
|
||||
.map(|()| crate::sidecar_placeholder::is_placeholder(&head))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// A running plugin process with a listener on the local port.
|
||||
|
||||
@@ -400,3 +400,135 @@ async fn the_keepalive_asks_for_health_without_a_token() {
|
||||
assert!(req.starts_with("GET /v1/health HTTP/1.1\r\n"), "{req}");
|
||||
assert!(!req.to_ascii_lowercase().contains("authorization"), "{req}");
|
||||
}
|
||||
|
||||
// ── Finding the plugin ───────────────────────────────────────────────────────
|
||||
|
||||
mod plugin_resolution {
|
||||
use super::super::{resolve_plugin, PluginSearch, PLUGIN_FILE, PLUGIN_INSTALL_HINT};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// A directory holding a fake plugin (contents are never run).
|
||||
fn dir_with_plugin(root: &Path, name: &str) -> PathBuf {
|
||||
let d = root.join(name);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
std::fs::write(d.join(PLUGIN_FILE), b"\x7fELF fake plugin").unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
fn search(
|
||||
override_path: Option<&Path>,
|
||||
exe_dir: Option<&Path>,
|
||||
path: &[&Path],
|
||||
install: &[&Path],
|
||||
) -> PluginSearch {
|
||||
PluginSearch {
|
||||
override_path: override_path.map(|p| p.as_os_str().to_owned()),
|
||||
exe_dir: exe_dir.map(Path::to_path_buf),
|
||||
path_var: Some(std::env::join_paths(path).unwrap()),
|
||||
install_dirs: install.iter().map(|p| p.to_path_buf()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_override_wins_over_everything() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let chosen = t.path().join("my-plugin");
|
||||
std::fs::write(&chosen, b"x").unwrap();
|
||||
let bundled = dir_with_plugin(t.path(), "app");
|
||||
let on_path = dir_with_plugin(t.path(), "path");
|
||||
let s = search(Some(&chosen), Some(&bundled), &[&on_path], &[]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), chosen);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_override_naming_no_file_is_an_error_not_a_fallback() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let bundled = dir_with_plugin(t.path(), "app");
|
||||
let s = search(Some(&t.path().join("missing")), Some(&bundled), &[], &[]);
|
||||
let e = resolve_plugin(&s).unwrap_err();
|
||||
assert!(e.contains("TRIPLEC_SSM_PLUGIN names"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_override_is_ignored() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let bundled = dir_with_plugin(t.path(), "app");
|
||||
let mut s = search(None, Some(&bundled), &[], &[]);
|
||||
s.override_path = Some(std::ffi::OsString::new());
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), bundled.join(PLUGIN_FILE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_sidecar_wins_over_path_and_installs() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let bundled = dir_with_plugin(t.path(), "app");
|
||||
let on_path = dir_with_plugin(t.path(), "path");
|
||||
let installed = dir_with_plugin(t.path(), "installed");
|
||||
let s = search(None, Some(&bundled), &[&on_path], &[&installed]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), bundled.join(PLUGIN_FILE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_a_sidecar_path_comes_before_the_install_locations() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let empty_app = t.path().join("app");
|
||||
std::fs::create_dir_all(&empty_app).unwrap();
|
||||
let first = t.path().join("path-first-empty");
|
||||
std::fs::create_dir_all(&first).unwrap();
|
||||
let on_path = dir_with_plugin(t.path(), "path");
|
||||
let installed = dir_with_plugin(t.path(), "installed");
|
||||
let s = search(None, Some(&empty_app), &[&first, &on_path], &[&installed]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), on_path.join(PLUGIN_FILE));
|
||||
|
||||
let s = search(None, Some(&empty_app), &[&first], &[&installed]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), installed.join(PLUGIN_FILE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_debug_placeholder_beside_the_exe_is_skipped() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let app = t.path().join("app");
|
||||
std::fs::create_dir_all(&app).unwrap();
|
||||
std::fs::write(app.join(PLUGIN_FILE), crate::sidecar_placeholder::contents()).unwrap();
|
||||
let on_path = dir_with_plugin(t.path(), "path");
|
||||
let s = search(None, Some(&app), &[&on_path], &[]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), on_path.join(PLUGIN_FILE));
|
||||
|
||||
// And with nothing else installed, the install hint, not the placeholder.
|
||||
let s = search(None, Some(&app), &[], &[]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap_err(), PLUGIN_INSTALL_HINT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_directory_named_like_the_plugin_is_not_a_plugin() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let app = t.path().join("app");
|
||||
std::fs::create_dir_all(app.join(PLUGIN_FILE)).unwrap();
|
||||
let installed = dir_with_plugin(t.path(), "installed");
|
||||
let s = search(None, Some(&app), &[], &[&installed]);
|
||||
assert_eq!(resolve_plugin(&s).unwrap(), installed.join(PLUGIN_FILE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_anywhere_is_the_install_hint() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let s = search(None, None, &[t.path()], &[t.path()]);
|
||||
let e = resolve_plugin(&s).unwrap_err();
|
||||
assert!(e.contains("is not installed") && e.contains("docs.aws.amazon.com"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_name_is_what_tauri_gives_the_sidecar() {
|
||||
// tauri-build/-bundler strip "-<target-triple>" from bundle.externalBin's
|
||||
// "binaries/session-manager-plugin"; the .exe stays on Windows.
|
||||
let conf: serde_json::Value =
|
||||
serde_json::from_str(include_str!("../../../tauri.conf.json")).unwrap();
|
||||
let bins = conf["bundle"]["externalBin"].as_array().expect("bundle.externalBin");
|
||||
let stems: Vec<&str> = bins
|
||||
.iter()
|
||||
.map(|b| b.as_str().unwrap().rsplit('/').next().unwrap())
|
||||
.collect();
|
||||
let expected = PLUGIN_FILE.trim_end_matches(".exe");
|
||||
assert!(stems.contains(&expected), "{stems:?} has no {expected}");
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,8 @@ pub fn register(project_id: &str, tunnel: LiveTunnel) -> Option<LiveTunnel> {
|
||||
r.stopped.remove(tunnel.endpoint.base_url.as_str());
|
||||
r.stopped.remove(&unattached_id(project_id));
|
||||
r.pending.remove(project_id);
|
||||
// A port reused by a new tunnel may reach another task's image.
|
||||
super::persistent::forget_probe(tunnel.endpoint.base_url.as_str());
|
||||
r.live.insert(project_id.to_string(), tunnel)
|
||||
};
|
||||
notify();
|
||||
@@ -197,6 +199,8 @@ pub fn register_if_current(
|
||||
r.stopped.remove(tunnel.endpoint.base_url.as_str());
|
||||
r.stopped.remove(&unattached_id(project_id));
|
||||
r.pending.remove(project_id);
|
||||
// A port reused by a new tunnel may reach another task's image.
|
||||
super::persistent::forget_probe(tunnel.endpoint.base_url.as_str());
|
||||
r.live.insert(project_id.to_string(), tunnel)
|
||||
};
|
||||
notify();
|
||||
|
||||
@@ -15,6 +15,7 @@ mod logging;
|
||||
mod marketplace;
|
||||
mod models;
|
||||
mod project_lock;
|
||||
mod sidecar_placeholder;
|
||||
mod storage;
|
||||
pub mod url_open;
|
||||
pub mod web_terminal;
|
||||
@@ -541,6 +542,7 @@ pub fn run() {
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// Docker
|
||||
commands::docker_commands::check_docker,
|
||||
commands::docker_commands::check_docker_status,
|
||||
commands::docker_commands::check_image_exists,
|
||||
commands::docker_commands::build_image,
|
||||
commands::docker_commands::get_container_info,
|
||||
@@ -555,6 +557,8 @@ pub fn run() {
|
||||
commands::project_commands::aws_task_log,
|
||||
commands::project_commands::aws_workspace_status,
|
||||
commands::project_commands::aws_environment_max_hours,
|
||||
commands::project_commands::aws_ec2_capacity,
|
||||
commands::project_commands::ec2_fit_problem,
|
||||
commands::project_commands::remove_aws_resources,
|
||||
commands::project_commands::rebuild_project_container,
|
||||
commands::project_commands::reconcile_project_statuses,
|
||||
@@ -644,6 +648,7 @@ pub fn run() {
|
||||
commands::environment_commands::cancel_aws_deployment,
|
||||
commands::environment_commands::push_aws_saved_state,
|
||||
commands::environment_commands::onboard_aws_user,
|
||||
commands::environment_commands::list_ec2_instance_types,
|
||||
commands::environment_commands::deploy_aws_permissions_boundary,
|
||||
commands::environment_commands::save_aws_admin_bundle,
|
||||
commands::settings_commands::detect_host_timezone,
|
||||
@@ -656,6 +661,9 @@ pub fn run() {
|
||||
commands::terminal_commands::terminal_input,
|
||||
commands::terminal_commands::terminal_resize,
|
||||
commands::terminal_commands::close_terminal_session,
|
||||
commands::terminal_commands::list_workspace_terminals,
|
||||
commands::terminal_commands::reattach_terminal_session,
|
||||
commands::terminal_commands::end_workspace_terminal,
|
||||
commands::terminal_commands::paste_image_to_terminal,
|
||||
commands::terminal_commands::upload_host_file_to_terminal,
|
||||
commands::terminal_commands::start_audio_bridge,
|
||||
|
||||
@@ -7,6 +7,7 @@ pub mod migration;
|
||||
pub mod note;
|
||||
pub mod project;
|
||||
pub mod settings_export;
|
||||
pub mod terminal;
|
||||
pub mod update_info;
|
||||
|
||||
pub use app_settings::*;
|
||||
@@ -17,4 +18,5 @@ pub use migration::*;
|
||||
pub use note::*;
|
||||
pub use project::*;
|
||||
pub use settings_export::*;
|
||||
pub use terminal::*;
|
||||
pub use update_info::*;
|
||||
@@ -521,8 +521,20 @@ fn default_aws_ephemeral_gib() -> u32 {
|
||||
21
|
||||
}
|
||||
|
||||
/// Where an AWS workspace's task runs.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum LaunchType {
|
||||
#[default]
|
||||
Fargate,
|
||||
Ec2,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct AwsWorkspaceConfig {
|
||||
/// Fargate (default; absent in old JSON) or the user's own EC2 instances.
|
||||
#[serde(default)]
|
||||
pub launch_type: LaunchType,
|
||||
#[serde(default = "default_aws_cpu")]
|
||||
pub cpu: u32,
|
||||
#[serde(default = "default_aws_memory_mib")]
|
||||
@@ -568,6 +580,7 @@ pub const MAX_SETUP_SCRIPT_BYTES: usize = 32_768;
|
||||
impl Default for AwsWorkspaceConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
launch_type: LaunchType::default(),
|
||||
cpu: default_aws_cpu(),
|
||||
memory_mib: default_aws_memory_mib(),
|
||||
arch: Arch::default(),
|
||||
@@ -700,23 +713,179 @@ fn describe_sizes(sizes: &[u32]) -> String {
|
||||
format!("{} to {} MiB in steps of {}", first, last, sizes[1] - sizes[0])
|
||||
}
|
||||
|
||||
pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
|
||||
let sizes = fargate_memory_sizes(cfg.cpu).ok_or_else(|| {
|
||||
format!(
|
||||
"{} is not a Fargate CPU size (256, 512, 1024, 2048, 4096, 8192 or 16384)",
|
||||
cfg.cpu
|
||||
)
|
||||
})?;
|
||||
if !sizes.contains(&cfg.memory_mib) {
|
||||
return Err(format!(
|
||||
"{} MiB of memory is not valid with {} CPU units: use {}",
|
||||
cfg.memory_mib,
|
||||
cfg.cpu,
|
||||
describe_sizes(&sizes)
|
||||
));
|
||||
/// CPU units and memory (MiB) held back on an EC2 instance for the ECS agent
|
||||
/// and the OS.
|
||||
pub const EC2_RESERVED_CPU_UNITS: u32 = 128;
|
||||
pub const EC2_RESERVED_MEMORY_MIB: u32 = 256;
|
||||
|
||||
/// The instance families the built-in table knows, in the order the app
|
||||
/// lists them; a family ending in `g` is Graviton (ARM).
|
||||
const EC2_FAMILIES: [&str; 8] = ["m7i", "c7i", "r7i", "t3", "m7g", "c7g", "r7g", "t4g"];
|
||||
const EC2_SIZES: [&str; 4] = ["large", "xlarge", "2xlarge", "4xlarge"];
|
||||
|
||||
/// The instance type onboarding offers first (x86; `m7g.large` is its ARM
|
||||
/// counterpart).
|
||||
pub const DEFAULT_EC2_INSTANCE_TYPE: &str = "m7i.large";
|
||||
|
||||
/// One row of the built-in instance table, for the onboarding UI.
|
||||
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
|
||||
pub struct Ec2InstanceType {
|
||||
pub instance_type: String,
|
||||
pub arch: Arch,
|
||||
pub vcpu: u32,
|
||||
pub memory_mib: u32,
|
||||
}
|
||||
|
||||
/// Every type [`ec2_instance_spec`] knows (x86 families first), so the UI's
|
||||
/// list and the app's validation share one table.
|
||||
pub fn ec2_instance_types() -> Vec<Ec2InstanceType> {
|
||||
EC2_FAMILIES
|
||||
.iter()
|
||||
.flat_map(|family| EC2_SIZES.iter().map(move |size| format!("{family}.{size}")))
|
||||
.filter_map(|instance_type| {
|
||||
let (cpu, memory_mib) = ec2_instance_spec(&instance_type)?;
|
||||
let arch = if instance_type.split('.').next()?.ends_with('g') {
|
||||
Arch::Arm64
|
||||
} else {
|
||||
Arch::X86_64
|
||||
};
|
||||
Some(Ec2InstanceType {
|
||||
instance_type,
|
||||
arch,
|
||||
vcpu: cpu / 1024,
|
||||
memory_mib,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `(vcpu * 1024, memory MiB)` of a common EC2 instance type, or None when the
|
||||
/// type is not in the built-in table (m7i, m7g, c7i, c7g, r7i, r7g, t3, t4g).
|
||||
pub fn ec2_instance_spec(instance_type: &str) -> Option<(u32, u32)> {
|
||||
let (family, size) = instance_type.split_once('.')?;
|
||||
let vcpu: u32 = match size {
|
||||
"large" => 2,
|
||||
"xlarge" => 4,
|
||||
"2xlarge" => 8,
|
||||
"4xlarge" => 16,
|
||||
_ => return None,
|
||||
};
|
||||
// GiB per vCPU: general purpose 4, compute 2, memory 8.
|
||||
let gib_per_vcpu: u32 = match family {
|
||||
"m7i" | "m7g" => 4,
|
||||
"c7i" | "c7g" => 2,
|
||||
"r7i" | "r7g" => 8,
|
||||
"t3" | "t4g" => {
|
||||
// Burstable families: memory is 4 GiB per vCPU at xlarge and above,
|
||||
// 8 GiB at large; AWS offers no 4xlarge in these families.
|
||||
return match size {
|
||||
"large" => Some((2048, 8192)),
|
||||
"xlarge" => Some((4096, 16384)),
|
||||
"2xlarge" => Some((8192, 32768)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some((vcpu * 1024, vcpu * gib_per_vcpu * 1024))
|
||||
}
|
||||
|
||||
/// Whether an EC2 workspace's reservations fit an instance type.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum Fit {
|
||||
Ok,
|
||||
/// The reservations exceed what the instance leaves for tasks.
|
||||
TooBig { available_cpu: u32, available_memory_mib: u32 },
|
||||
/// The type is not in the table: the caller shows a "must fit" warning.
|
||||
UnknownType,
|
||||
}
|
||||
|
||||
/// Memory (MiB) an instance of `memory_mib` nominal memory keeps from tasks
|
||||
/// in [`fits_instance`]: a tenth of it (what ECS doesn't register, with
|
||||
/// room to spare), and never less than [`EC2_RESERVED_MEMORY_MIB`].
|
||||
pub fn ec2_memory_reserve_mib(memory_mib: u32) -> u32 {
|
||||
(memory_mib / 10).max(EC2_RESERVED_MEMORY_MIB)
|
||||
}
|
||||
|
||||
/// Check an EC2 config's reservations against `instance_type`, less the agent
|
||||
/// and OS reserve. A Fargate config always fits (no instance involved).
|
||||
///
|
||||
/// Nominal sizes only, so memory keeps a margin: ECS registers less memory
|
||||
/// than the instance's nominal (the ECS-optimized AMI and kernel keep some —
|
||||
/// about 7.5 GiB of an 8 GiB type is registered), and a task asking for more
|
||||
/// than the registered amount stays PROVISIONING with nothing able to place
|
||||
/// it. The registered figure can't be read before an instance exists (scale
|
||||
/// to zero), so the reserve is [`ec2_memory_reserve_mib`]: 10 % of the
|
||||
/// nominal memory, at least [`EC2_RESERVED_MEMORY_MIB`].
|
||||
pub fn fits_instance(cfg: &AwsWorkspaceConfig, instance_type: &str) -> Fit {
|
||||
if cfg.launch_type != LaunchType::Ec2 {
|
||||
return Fit::Ok;
|
||||
}
|
||||
if !(21..=200).contains(&cfg.ephemeral_gib) {
|
||||
return Err("Ephemeral storage must be 21 to 200 GiB".into());
|
||||
let Some((available_cpu, available_memory_mib)) = ec2_available(instance_type) else {
|
||||
return Fit::UnknownType;
|
||||
};
|
||||
if cfg.cpu > available_cpu || cfg.memory_mib > available_memory_mib {
|
||||
Fit::TooBig { available_cpu, available_memory_mib }
|
||||
} else {
|
||||
Fit::Ok
|
||||
}
|
||||
}
|
||||
|
||||
/// What `instance_type` leaves for one workspace's reservations, as
|
||||
/// `(CPU units, MiB)`: [`fits_instance`]'s limits (the agent's CPU and the
|
||||
/// memory margin taken off). `None` for a type not in the table.
|
||||
pub fn ec2_available(instance_type: &str) -> Option<(u32, u32)> {
|
||||
let (cpu, mem) = ec2_instance_spec(instance_type)?;
|
||||
Some((cpu - EC2_RESERVED_CPU_UNITS, mem - ec2_memory_reserve_mib(mem)))
|
||||
}
|
||||
|
||||
/// Why `cfg`'s reservations don't fit `instance_type`, or `None` when they
|
||||
/// do (or the type is unknown, or `cfg` is Fargate). The one wording of the
|
||||
/// fit rule: the start's refusal and Host settings both use it.
|
||||
pub fn ec2_fit_problem(cfg: &AwsWorkspaceConfig, instance_type: &str) -> Option<String> {
|
||||
match fits_instance(cfg, instance_type) {
|
||||
Fit::TooBig { available_cpu, available_memory_mib } => Some(format!(
|
||||
"This project reserves {} CPU units and {} MiB of memory, more than your EC2 \
|
||||
instance type {instance_type} has for a workspace ({available_cpu} CPU units, \
|
||||
{available_memory_mib} MiB).",
|
||||
cfg.cpu, cfg.memory_mib
|
||||
)),
|
||||
Fit::Ok | Fit::UnknownType => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
|
||||
match cfg.launch_type {
|
||||
LaunchType::Fargate => {
|
||||
let sizes = fargate_memory_sizes(cfg.cpu).ok_or_else(|| {
|
||||
format!(
|
||||
"{} is not a Fargate CPU size (256, 512, 1024, 2048, 4096, 8192 or 16384)",
|
||||
cfg.cpu
|
||||
)
|
||||
})?;
|
||||
if !sizes.contains(&cfg.memory_mib) {
|
||||
return Err(format!(
|
||||
"{} MiB of memory is not valid with {} CPU units: use {}",
|
||||
cfg.memory_mib,
|
||||
cfg.cpu,
|
||||
describe_sizes(&sizes)
|
||||
));
|
||||
}
|
||||
if !(21..=200).contains(&cfg.ephemeral_gib) {
|
||||
return Err("Ephemeral storage must be 21 to 200 GiB".into());
|
||||
}
|
||||
}
|
||||
LaunchType::Ec2 => {
|
||||
// Reservations on the user's instance; the instance type is not on
|
||||
// the project, so `fits_instance` does the fit check later.
|
||||
if cfg.cpu == 0 || cfg.memory_mib == 0 {
|
||||
return Err("An EC2 workspace needs a positive CPU and memory reservation".into());
|
||||
}
|
||||
if cfg.spot {
|
||||
return Err("Spot is not available on EC2 yet".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(m) = cfg.idle_stop_minutes {
|
||||
if !(5..=1440).contains(&m) {
|
||||
@@ -1436,6 +1605,126 @@ mod tests {
|
||||
assert!(p.is_aws());
|
||||
}
|
||||
|
||||
fn ec2(cpu: u32, mem: u32) -> AwsWorkspaceConfig {
|
||||
AwsWorkspaceConfig { launch_type: LaunchType::Ec2, cpu, memory_mib: mem, ..Default::default() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_json_is_fargate_and_ec2_round_trips() {
|
||||
let c: AwsWorkspaceConfig = serde_json::from_str("{}").unwrap();
|
||||
assert_eq!(c.launch_type, LaunchType::Fargate);
|
||||
let e: AwsWorkspaceConfig = serde_json::from_str(r#"{"launch_type":"ec2"}"#).unwrap();
|
||||
assert_eq!(e.launch_type, LaunchType::Ec2);
|
||||
assert!(serde_json::to_string(&e).unwrap().contains(r#""launch_type":"ec2""#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ec2_validation_shape() {
|
||||
// Reservations need not be a Fargate pair.
|
||||
assert!(validate_aws_workspace(&ec2(1500, 3000)).is_ok());
|
||||
assert!(validate_aws_workspace(&ec2(0, 3000)).is_err());
|
||||
assert!(validate_aws_workspace(&ec2(1024, 0)).is_err());
|
||||
let mut s = ec2(1024, 2048);
|
||||
s.spot = true;
|
||||
assert!(validate_aws_workspace(&s).unwrap_err().contains("Spot"));
|
||||
// Ephemeral size is ignored on EC2.
|
||||
let mut e = ec2(1024, 2048);
|
||||
e.ephemeral_gib = 5;
|
||||
assert!(validate_aws_workspace(&e).is_ok());
|
||||
// Fargate still rejects the non-pair.
|
||||
assert!(validate_aws_workspace(&cfg(1500, 3000)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_instance_type_list_is_the_table() {
|
||||
let list = ec2_instance_types();
|
||||
assert_eq!(list.len(), 30);
|
||||
for t in &list {
|
||||
assert_eq!(
|
||||
ec2_instance_spec(&t.instance_type),
|
||||
Some((t.vcpu * 1024, t.memory_mib)),
|
||||
"{}",
|
||||
t.instance_type
|
||||
);
|
||||
}
|
||||
// And the other way: every type the spec table sizes is listed (a
|
||||
// family added to one but not the other fails here).
|
||||
let families = [
|
||||
"m7i", "m7g", "m7a", "m6i", "c7i", "c7g", "c7a", "r7i", "r7g", "r7a", "t3", "t3a",
|
||||
"t4g", "p5", "g6",
|
||||
];
|
||||
for family in families {
|
||||
for size in ["medium", "large", "xlarge", "2xlarge", "4xlarge", "8xlarge", "metal"] {
|
||||
let t = format!("{family}.{size}");
|
||||
if ec2_instance_spec(&t).is_some() {
|
||||
assert!(list.iter().any(|row| row.instance_type == t), "{t} missing");
|
||||
}
|
||||
}
|
||||
}
|
||||
let arch = |name: &str| list.iter().find(|t| t.instance_type == name).unwrap().arch;
|
||||
assert_eq!(arch("m7i.large"), Arch::X86_64);
|
||||
assert_eq!(arch("t3.xlarge"), Arch::X86_64);
|
||||
assert_eq!(arch("m7g.large"), Arch::Arm64);
|
||||
assert_eq!(arch("t4g.2xlarge"), Arch::Arm64);
|
||||
assert!(list.iter().any(|t| t.instance_type == DEFAULT_EC2_INSTANCE_TYPE));
|
||||
assert_eq!(arch(DEFAULT_EC2_INSTANCE_TYPE), Arch::X86_64);
|
||||
let first = &list[0];
|
||||
assert_eq!(
|
||||
serde_json::to_value(first).unwrap(),
|
||||
serde_json::json!({"instance_type": "m7i.large", "arch": "x86_64", "vcpu": 2, "memory_mib": 8192})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_table_values() {
|
||||
assert_eq!(ec2_instance_spec("m7i.large"), Some((2048, 8192)));
|
||||
assert_eq!(ec2_instance_spec("m7g.4xlarge"), Some((16384, 65536)));
|
||||
assert_eq!(ec2_instance_spec("c7i.xlarge"), Some((4096, 8192)));
|
||||
assert_eq!(ec2_instance_spec("r7g.2xlarge"), Some((8192, 65536)));
|
||||
assert_eq!(ec2_instance_spec("t3.large"), Some((2048, 8192)));
|
||||
assert_eq!(ec2_instance_spec("t4g.2xlarge"), Some((8192, 32768)));
|
||||
assert_eq!(ec2_instance_spec("t3.4xlarge"), None);
|
||||
assert_eq!(ec2_instance_spec("p5.48xlarge"), None);
|
||||
assert_eq!(ec2_instance_spec("m7i"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fit_checks_reserve_for_the_agent() {
|
||||
// m7i.large: 2048 / 8192 less 128 CPU units and 10 % of the memory
|
||||
// (819 MiB: ECS registers less than the nominal 8 GiB).
|
||||
assert_eq!(fits_instance(&ec2(1920, 7373), "m7i.large"), Fit::Ok);
|
||||
assert_eq!(
|
||||
fits_instance(&ec2(1921, 4096), "m7i.large"),
|
||||
Fit::TooBig { available_cpu: 1920, available_memory_mib: 7373 }
|
||||
);
|
||||
assert!(matches!(fits_instance(&ec2(1024, 7374), "m7i.large"), Fit::TooBig { .. }));
|
||||
// The reserve is never under the agent's 256 MiB, and grows with
|
||||
// the instance (r7i.4xlarge: 128 GiB nominal).
|
||||
assert_eq!(ec2_memory_reserve_mib(2048), 256);
|
||||
assert_eq!(ec2_memory_reserve_mib(131072), 13107);
|
||||
assert_eq!(fits_instance(&ec2(1024, 2048), "z9.huge"), Fit::UnknownType);
|
||||
// Fargate configs have no instance to fit.
|
||||
assert_eq!(fits_instance(&cfg(16384, 122880), "m7i.large"), Fit::Ok);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_fit_problem_words_the_fit_rule_once() {
|
||||
assert_eq!(ec2_available("m7i.large"), Some((1920, 7373)));
|
||||
assert_eq!(ec2_available("t4g.large"), Some((1920, 7373)));
|
||||
assert_eq!(ec2_available("z9.huge"), None);
|
||||
assert_eq!(ec2_fit_problem(&ec2(1920, 7373), "m7i.large"), None);
|
||||
assert_eq!(
|
||||
ec2_fit_problem(&ec2(1024, 7374), "m7i.large").as_deref(),
|
||||
Some(
|
||||
"This project reserves 1024 CPU units and 7374 MiB of memory, more than your EC2 \
|
||||
instance type m7i.large has for a workspace (1920 CPU units, 7373 MiB)."
|
||||
)
|
||||
);
|
||||
// Unknown types and Fargate configs: no problem to report here.
|
||||
assert_eq!(ec2_fit_problem(&ec2(99999, 99999), "z9.huge"), None);
|
||||
assert_eq!(ec2_fit_problem(&cfg(16384, 122880), "m7i.large"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cpu_and_memory_must_be_a_fargate_pair() {
|
||||
// Shared with the frontend's copy (`lib/awsWorkspace.test.ts`).
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//! IPC shapes of persistent AWS terminals (`host::persistent`); mirrored in
|
||||
//! `lib/types.ts`.
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
pub use crate::host::persistent::TerminalKind;
|
||||
|
||||
/// What `open_terminal_session` tells the tab it opened.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
|
||||
pub struct OpenedTerminal {
|
||||
/// Runs in tmux in the workspace and outlives the app (AWS only).
|
||||
pub persistent: bool,
|
||||
/// Something to say once (the image has no tmux).
|
||||
pub notice: Option<String>,
|
||||
}
|
||||
|
||||
/// A terminal session running in an AWS workspace.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub struct WorkspaceTerminal {
|
||||
pub session_id: String,
|
||||
pub kind: TerminalKind,
|
||||
/// The app's label for it when it opened it, else the label tmux keeps.
|
||||
pub name: Option<String>,
|
||||
pub created_unix: u64,
|
||||
/// A client (this app, another machine, the web terminal) is attached.
|
||||
pub attached: bool,
|
||||
/// This app opened it and reopens it on connect.
|
||||
pub known: bool,
|
||||
}
|
||||
|
||||
/// What `reattach_terminal_session` did.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "snake_case", tag = "outcome")]
|
||||
pub enum ReattachOutcome {
|
||||
/// Attached again; the tab keeps running.
|
||||
Reattached { kind: TerminalKind, name: Option<String> },
|
||||
/// The session is no longer running in the workspace (it ended, or the
|
||||
/// workspace was replaced); it has been forgotten.
|
||||
Ended,
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//! The stand-in `build.rs` writes for the bundled `session-manager-plugin` sidecar in a
|
||||
//! **debug** build that has no real one, and how the app recognises it.
|
||||
//!
|
||||
//! `bundle.externalBin` makes tauri-build fail outright when the per-target file is missing,
|
||||
//! for `cargo test`, `cargo check` and `tauri dev` as much as for a release. The real binary
|
||||
//! is built from AWS's pinned source by `scripts/build-ssm-plugin.sh` (`.ps1` on Windows), with
|
||||
//! a downloaded Go toolchain; requiring that for every test run would put a Go build and the
|
||||
//! network on the path of every `cargo test`. So debug builds get this placeholder instead, a
|
||||
//! release build refuses it (`build.rs`), and `host::ssm::find_plugin` skips it and falls back to
|
||||
//! an installed plugin.
|
||||
//!
|
||||
//! Shared by `build.rs` (through `#[path]`) and the app, so the two cannot disagree.
|
||||
|
||||
/// The first bytes of a placeholder. Not an executable format on any platform.
|
||||
pub const MARKER: &[u8] = b"TRIPLE-C SSM PLUGIN PLACEHOLDER";
|
||||
|
||||
/// The whole placeholder file.
|
||||
#[allow(dead_code)] // build.rs only
|
||||
pub fn contents() -> Vec<u8> {
|
||||
let mut v = MARKER.to_vec();
|
||||
v.extend_from_slice(
|
||||
b"\nNot the AWS Session Manager plugin. Debug builds get this when \
|
||||
app/src-tauri/binaries has no real one; run scripts/build-ssm-plugin.sh \
|
||||
(scripts/build-ssm-plugin.ps1 on Windows) to build the real plugin.\n",
|
||||
);
|
||||
v
|
||||
}
|
||||
|
||||
/// Whether `bytes` (the start of a file) is a placeholder.
|
||||
pub fn is_placeholder(bytes: &[u8]) -> bool {
|
||||
bytes.starts_with(MARKER)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub mod projects_store;
|
||||
pub mod secure;
|
||||
pub mod settings_crypto;
|
||||
pub mod settings_store;
|
||||
pub mod terminal_store;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub use projects_store::*;
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
//! Which persistent AWS terminals (`host::persistent`) the app has open, per
|
||||
//! project: `<data_dir>/triple-c/terminal-sessions.json`.
|
||||
//!
|
||||
//! The session id is the tmux key, so this is what lets the app reopen a
|
||||
//! project's tabs — same ids, kinds and labels — after it was closed. It is
|
||||
//! a hint, never the truth: the workspace's own listing decides what is
|
||||
//! running, and an entry it does not list is forgotten (`retain`).
|
||||
//!
|
||||
//! Not a `Project` field, for the reason notes are not: the webview saves
|
||||
//! whole projects, and a tab opening while a Config save is in flight would
|
||||
//! lose one of the two. Same free-function shape as `notes_store`, with
|
||||
//! `_in` variants the tests drive against a temp directory. An unreadable
|
||||
//! file is treated as empty: losing it costs only the automatic reopen (the
|
||||
//! sessions still show in Project Home → Sessions).
|
||||
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::host::persistent::{validate_key, TerminalKind};
|
||||
|
||||
const FORMAT_VERSION: u32 = 1;
|
||||
/// More tabs than anyone keeps open: a bound on what a bug could grow.
|
||||
const MAX_PER_PROJECT: usize = 64;
|
||||
|
||||
/// One open persistent terminal.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PersistedTerminal {
|
||||
/// The tab's session id, which is also the tmux key.
|
||||
pub id: String,
|
||||
pub kind: TerminalKind,
|
||||
/// The label the tab was opened with (a rename lives in
|
||||
/// `Project::renamed_session_names`, keyed by the same id).
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
struct File {
|
||||
version: u32,
|
||||
#[serde(default)]
|
||||
projects: BTreeMap<String, Vec<PersistedTerminal>>,
|
||||
/// Tabs closed while the workspace could not be reached: their tmux
|
||||
/// servers still run (a leftover Claude would keep a billed workspace
|
||||
/// up), so they are ended at the next connect.
|
||||
#[serde(default)]
|
||||
pending_end: BTreeMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
fn lock() -> &'static Mutex<()> {
|
||||
static L: OnceLock<Mutex<()>> = OnceLock::new();
|
||||
L.get_or_init(|| Mutex::new(()))
|
||||
}
|
||||
|
||||
fn data_dir() -> Result<PathBuf, String> {
|
||||
let dir = dirs::data_dir()
|
||||
.ok_or_else(|| "Could not determine data directory".to_string())?
|
||||
.join("triple-c");
|
||||
fs::create_dir_all(&dir).map_err(|e| format!("Failed to create data directory: {e}"))?;
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
fn path_in(dir: &Path) -> PathBuf {
|
||||
dir.join("terminal-sessions.json")
|
||||
}
|
||||
|
||||
fn load_file(dir: &Path) -> File {
|
||||
let path = path_in(dir);
|
||||
let Ok(data) = fs::read_to_string(&path) else {
|
||||
return File::default();
|
||||
};
|
||||
match serde_json::from_str::<File>(&data) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
log::warn!("{} is unreadable ({e}); starting with no remembered terminals", path.display());
|
||||
File::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save_file(dir: &Path, mut file: File) -> Result<(), String> {
|
||||
file.version = FORMAT_VERSION;
|
||||
file.projects.retain(|_, v| !v.is_empty());
|
||||
file.pending_end.retain(|_, v| !v.is_empty());
|
||||
let path = path_in(dir);
|
||||
let tmp = path.with_extension("json.tmp");
|
||||
let data = serde_json::to_string_pretty(&file).map_err(|e| e.to_string())?;
|
||||
fs::write(&tmp, data).map_err(|e| format!("Failed to write {}: {e}", tmp.display()))?;
|
||||
fs::rename(&tmp, &path).map_err(|e| format!("Failed to commit {}: {e}", path.display()))
|
||||
}
|
||||
|
||||
fn edit_in<T>(dir: &Path, f: impl FnOnce(&mut File) -> T) -> Result<T, String> {
|
||||
let _g = lock().lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut file = load_file(dir);
|
||||
let out = f(&mut file);
|
||||
save_file(dir, file)?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
// ── `_in` variants (tests) ────────────────────────────────────────────────
|
||||
|
||||
pub fn list_in(dir: &Path, project_id: &str) -> Vec<PersistedTerminal> {
|
||||
let _g = lock().lock().unwrap_or_else(|e| e.into_inner());
|
||||
load_file(dir).projects.remove(project_id).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn add_in(dir: &Path, project_id: &str, t: PersistedTerminal) -> Result<(), String> {
|
||||
validate_key(&t.id)?;
|
||||
edit_in(dir, |f| {
|
||||
let list = f.projects.entry(project_id.to_string()).or_default();
|
||||
if let Some(existing) = list.iter_mut().find(|e| e.id == t.id) {
|
||||
*existing = t;
|
||||
} else if list.len() < MAX_PER_PROJECT {
|
||||
list.push(t);
|
||||
} else {
|
||||
log::warn!("Project {project_id}: too many remembered terminals; not remembering another");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Forget `session_id`; returns the project it belonged to.
|
||||
pub fn remove_in(dir: &Path, session_id: &str) -> Result<Option<String>, String> {
|
||||
edit_in(dir, |f| {
|
||||
let mut owner = None;
|
||||
for (pid, list) in f.projects.iter_mut() {
|
||||
let before = list.len();
|
||||
list.retain(|e| e.id != session_id);
|
||||
if list.len() != before {
|
||||
owner = Some(pid.clone());
|
||||
}
|
||||
}
|
||||
owner
|
||||
})
|
||||
}
|
||||
|
||||
/// The project and record of `session_id`, if remembered.
|
||||
pub fn find_in(dir: &Path, session_id: &str) -> Option<(String, PersistedTerminal)> {
|
||||
let _g = lock().lock().unwrap_or_else(|e| e.into_inner());
|
||||
load_file(dir).projects.into_iter().find_map(|(pid, list)| {
|
||||
list.into_iter().find(|e| e.id == session_id).map(|e| (pid, e))
|
||||
})
|
||||
}
|
||||
|
||||
/// Keep only the project's entries the workspace still runs.
|
||||
pub fn retain_in(dir: &Path, project_id: &str, running: &HashSet<String>) -> Result<(), String> {
|
||||
edit_in(dir, |f| {
|
||||
if let Some(list) = f.projects.get_mut(project_id) {
|
||||
list.retain(|e| running.contains(&e.id));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Forget all of a project's terminals (stopped, removed, moved off AWS):
|
||||
/// remembered and pending-end alike.
|
||||
pub fn clear_in(dir: &Path, project_id: &str) -> Result<(), String> {
|
||||
edit_in(dir, |f| {
|
||||
f.projects.remove(project_id);
|
||||
f.pending_end.remove(project_id);
|
||||
})
|
||||
}
|
||||
|
||||
/// Remember to end `session_id` at the project's next connect.
|
||||
pub fn add_pending_end_in(dir: &Path, project_id: &str, session_id: &str) -> Result<(), String> {
|
||||
validate_key(session_id)?;
|
||||
edit_in(dir, |f| {
|
||||
let list = f.pending_end.entry(project_id.to_string()).or_default();
|
||||
if !list.iter().any(|k| k == session_id) && list.len() < MAX_PER_PROJECT {
|
||||
list.push(session_id.to_string());
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// The project's sessions waiting to be ended.
|
||||
pub fn pending_end_in(dir: &Path, project_id: &str) -> Vec<String> {
|
||||
let _g = lock().lock().unwrap_or_else(|e| e.into_inner());
|
||||
load_file(dir).pending_end.remove(project_id).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// They were ended (or are gone): stop remembering them.
|
||||
pub fn done_pending_end_in(dir: &Path, project_id: &str, ended: &[String]) -> Result<(), String> {
|
||||
edit_in(dir, |f| {
|
||||
if let Some(list) = f.pending_end.get_mut(project_id) {
|
||||
list.retain(|k| !ended.contains(k));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ── Public API ────────────────────────────────────────────────────────────
|
||||
|
||||
pub fn list(project_id: &str) -> Vec<PersistedTerminal> {
|
||||
data_dir().map(|d| list_in(&d, project_id)).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn add(project_id: &str, t: PersistedTerminal) -> Result<(), String> {
|
||||
add_in(&data_dir()?, project_id, t)
|
||||
}
|
||||
|
||||
pub fn remove(session_id: &str) -> Result<Option<String>, String> {
|
||||
remove_in(&data_dir()?, session_id)
|
||||
}
|
||||
|
||||
pub fn find(session_id: &str) -> Option<(String, PersistedTerminal)> {
|
||||
data_dir().ok().and_then(|d| find_in(&d, session_id))
|
||||
}
|
||||
|
||||
pub fn retain(project_id: &str, running: &HashSet<String>) -> Result<(), String> {
|
||||
retain_in(&data_dir()?, project_id, running)
|
||||
}
|
||||
|
||||
pub fn add_pending_end(project_id: &str, session_id: &str) -> Result<(), String> {
|
||||
add_pending_end_in(&data_dir()?, project_id, session_id)
|
||||
}
|
||||
|
||||
pub fn pending_end(project_id: &str) -> Vec<String> {
|
||||
data_dir().map(|d| pending_end_in(&d, project_id)).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn done_pending_end(project_id: &str, ended: &[String]) -> Result<(), String> {
|
||||
done_pending_end_in(&data_dir()?, project_id, ended)
|
||||
}
|
||||
|
||||
/// Best effort: a failure is logged (the next listing prunes anyway).
|
||||
pub fn clear(project_id: &str) {
|
||||
match data_dir().and_then(|d| clear_in(&d, project_id)) {
|
||||
Ok(()) => {}
|
||||
Err(e) => log::warn!("Could not forget project {project_id}'s terminals: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dir(tag: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("tc-term-store-{tag}-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
fn t(id: &str, kind: TerminalKind, name: Option<&str>) -> PersistedTerminal {
|
||||
PersistedTerminal { id: id.into(), kind, name: name.map(str::to_string) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn terminals_are_remembered_per_project_in_order_and_survive_a_reload() {
|
||||
let d = dir("order");
|
||||
add_in(&d, "p1", t("a-1", TerminalKind::Claude, Some("main"))).unwrap();
|
||||
add_in(&d, "p1", t("b-2", TerminalKind::Bash, None)).unwrap();
|
||||
add_in(&d, "p2", t("c-3", TerminalKind::Bash, None)).unwrap();
|
||||
// Re-adding replaces in place (a reattach records it again).
|
||||
add_in(&d, "p1", t("a-1", TerminalKind::Claude, Some("renamed"))).unwrap();
|
||||
let p1 = list_in(&d, "p1");
|
||||
assert_eq!(p1.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(), ["a-1", "b-2"]);
|
||||
assert_eq!(p1[0].name.as_deref(), Some("renamed"));
|
||||
assert_eq!(find_in(&d, "c-3").unwrap().0, "p2");
|
||||
let raw = fs::read_to_string(path_in(&d)).unwrap();
|
||||
assert!(raw.contains("\"version\": 1") && raw.contains("\"kind\": \"claude\""), "{raw}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_pruning_and_stopping_forget_entries() {
|
||||
let d = dir("forget");
|
||||
for id in ["a", "b", "c"] {
|
||||
add_in(&d, "p", t(id, TerminalKind::Bash, None)).unwrap();
|
||||
}
|
||||
assert_eq!(remove_in(&d, "b").unwrap().as_deref(), Some("p"));
|
||||
assert_eq!(remove_in(&d, "zzz").unwrap(), None);
|
||||
retain_in(&d, "p", &HashSet::from(["c".to_string()])).unwrap();
|
||||
assert_eq!(list_in(&d, "p"), vec![t("c", TerminalKind::Bash, None)]);
|
||||
clear_in(&d, "p").unwrap();
|
||||
assert!(list_in(&d, "p").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tab_closed_while_disconnected_is_ended_at_the_next_connect() {
|
||||
let d = dir("pending");
|
||||
add_pending_end_in(&d, "p", "a-1").unwrap();
|
||||
add_pending_end_in(&d, "p", "a-1").unwrap();
|
||||
add_pending_end_in(&d, "p", "b-2").unwrap();
|
||||
add_pending_end_in(&d, "q", "c-3").unwrap();
|
||||
assert!(add_pending_end_in(&d, "p", "../x").is_err());
|
||||
assert_eq!(pending_end_in(&d, "p"), ["a-1", "b-2"]);
|
||||
done_pending_end_in(&d, "p", &["a-1".to_string()]).unwrap();
|
||||
assert_eq!(pending_end_in(&d, "p"), ["b-2"]);
|
||||
clear_in(&d, "p").unwrap();
|
||||
assert!(pending_end_in(&d, "p").is_empty());
|
||||
assert_eq!(pending_end_in(&d, "q"), ["c-3"], "per project");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bad_key_is_refused_and_a_corrupt_file_reads_as_empty() {
|
||||
let d = dir("bad");
|
||||
assert!(add_in(&d, "p", t("../x", TerminalKind::Bash, None)).is_err());
|
||||
fs::write(path_in(&d), "{not json").unwrap();
|
||||
assert!(list_in(&d, "p").is_empty());
|
||||
// …and is replaced by the next write.
|
||||
add_in(&d, "p", t("ok", TerminalKind::Bash, None)).unwrap();
|
||||
assert_eq!(list_in(&d, "p").len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -85,8 +85,9 @@ pub async fn handle_connection(socket: WebSocket, state: Arc<WebTerminalState>)
|
||||
// Channel for sending messages from session output tasks → WS writer
|
||||
let (out_tx, mut out_rx) = mpsc::unbounded_channel::<ServerMessage>();
|
||||
|
||||
// Track session IDs owned by this connection for cleanup
|
||||
let owned_sessions: Arc<tokio::sync::Mutex<Vec<String>>> =
|
||||
// Session ids owned by this connection, with whether each is a
|
||||
// persistent AWS terminal (tmux). A disconnect only detaches those.
|
||||
let owned_sessions: Arc<tokio::sync::Mutex<Vec<(String, bool)>>> =
|
||||
Arc::new(tokio::sync::Mutex::new(Vec::new()));
|
||||
|
||||
// Writer task: serializes ServerMessages and sends as WS text frames
|
||||
@@ -186,20 +187,27 @@ pub async fn handle_connection(socket: WebSocket, state: Arc<WebTerminalState>)
|
||||
}
|
||||
|
||||
ClientMessage::Close { session_id } => {
|
||||
state.exec_manager.close_session(&session_id).await;
|
||||
// Remove from owned list
|
||||
owned_sessions
|
||||
.lock()
|
||||
.await
|
||||
.retain(|id| id != &session_id);
|
||||
// Only a session this connection opened: the id is the tmux
|
||||
// key of a persistent AWS terminal, which a close ends.
|
||||
let owned = {
|
||||
let mut owned = owned_sessions.lock().await;
|
||||
let found = owned.iter().find(|(id, _)| id == &session_id).map(|(_, p)| *p);
|
||||
owned.retain(|(id, _)| id != &session_id);
|
||||
found
|
||||
};
|
||||
if let Some(persistent) = owned {
|
||||
close_owned(&state, &session_id, persistent).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Connection closed — clean up all owned sessions
|
||||
// Connection closed — clean up all owned sessions. A persistent AWS
|
||||
// terminal is only detached, like the desktop app's on exit: it keeps
|
||||
// running and Project Home → Sessions lists it.
|
||||
log::info!("Web terminal WebSocket disconnected, cleaning up sessions");
|
||||
let sessions = owned_sessions.lock().await.clone();
|
||||
for session_id in sessions {
|
||||
for (session_id, _) in sessions {
|
||||
state.exec_manager.close_session(&session_id).await;
|
||||
}
|
||||
|
||||
@@ -278,13 +286,41 @@ fi
|
||||
vec!["bash".to_string(), "-c".to_string(), script]
|
||||
}
|
||||
|
||||
/// A Close from the client: detach, and end a persistent terminal's tmux
|
||||
/// server (closing a tab is what ends one, as in the desktop app).
|
||||
async fn close_owned(state: &WebTerminalState, session_id: &str, persistent: bool) {
|
||||
let target = state.exec_manager.get_target(session_id).await.ok();
|
||||
state.exec_manager.close_session(session_id).await;
|
||||
let Some(target) = target else { return };
|
||||
let Some(key) = tmux_key_of(session_id) else { return };
|
||||
if persistent {
|
||||
if let Err(e) = crate::host::persistent::end(&target, key).await {
|
||||
log::warn!("Web terminal: could not end terminal {session_id}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The web terminal's exec ids are `web-<tmux key>`, never the bare key: the
|
||||
/// desktop app attaches to a workspace terminal under the bare key in the
|
||||
/// same `ExecSessionManager`, and sharing the id would make either front
|
||||
/// end's attach take over (or its close/disconnect end) the other's exec.
|
||||
const WEB_EXEC_PREFIX: &str = "web-";
|
||||
|
||||
fn web_exec_id(key: &str) -> String {
|
||||
format!("{WEB_EXEC_PREFIX}{key}")
|
||||
}
|
||||
|
||||
fn tmux_key_of(exec_id: &str) -> Option<&str> {
|
||||
exec_id.strip_prefix(WEB_EXEC_PREFIX)
|
||||
}
|
||||
|
||||
/// Open a new terminal session for a project.
|
||||
async fn handle_open(
|
||||
state: &WebTerminalState,
|
||||
project_id: &str,
|
||||
session_type: Option<&str>,
|
||||
out_tx: &mpsc::UnboundedSender<ServerMessage>,
|
||||
owned_sessions: &Arc<tokio::sync::Mutex<Vec<String>>>,
|
||||
owned_sessions: &Arc<tokio::sync::Mutex<Vec<(String, bool)>>>,
|
||||
) -> Result<(), String> {
|
||||
let project = state
|
||||
.projects_store
|
||||
@@ -308,9 +344,33 @@ async fn handle_open(
|
||||
// environment (never the agent's). Empty for everything else.
|
||||
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
|
||||
|
||||
let session_id = uuid::Uuid::new_v4().to_string();
|
||||
let key = uuid::Uuid::new_v4().to_string();
|
||||
let session_id = web_exec_id(&key);
|
||||
let project_name = project.name.clone();
|
||||
|
||||
// AWS: in its own tmux server, like the desktop app's tabs
|
||||
// (`host::persistent`), so a dropped connection only detaches it. Not
|
||||
// remembered for the desktop app to reopen: it shows in Project Home →
|
||||
// Sessions as a session running in the workspace.
|
||||
let kind = if session_type == Some("bash") {
|
||||
crate::host::persistent::TerminalKind::Bash
|
||||
} else {
|
||||
crate::host::persistent::TerminalKind::Claude
|
||||
};
|
||||
let plan = crate::host::persistent::plan(
|
||||
project.is_aws(),
|
||||
&container,
|
||||
&key,
|
||||
kind,
|
||||
Some("web terminal"),
|
||||
cmd,
|
||||
env,
|
||||
)
|
||||
.await?;
|
||||
if let Some(notice) = plan.notice.clone() {
|
||||
let _ = out_tx.send(ServerMessage::Error { message: notice });
|
||||
}
|
||||
|
||||
// Set up output routing through the WS channel
|
||||
let out_tx_output = out_tx.clone();
|
||||
let session_id_output = session_id.clone();
|
||||
@@ -335,15 +395,15 @@ async fn handle_open(
|
||||
.create_session(
|
||||
&container,
|
||||
&session_id,
|
||||
cmd,
|
||||
env,
|
||||
plan.cmd,
|
||||
plan.env,
|
||||
on_output,
|
||||
on_exit,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Track this session for cleanup on disconnect
|
||||
owned_sessions.lock().await.push(session_id.clone());
|
||||
owned_sessions.lock().await.push((session_id.clone(), plan.persistent));
|
||||
|
||||
let _ = out_tx.send(ServerMessage::Opened {
|
||||
session_id,
|
||||
@@ -357,3 +417,22 @@ async fn handle_open(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A desktop reattach (exec id = the bare key) and the web terminal's own
|
||||
/// exec of the same tmux session never share an `ExecSessionManager` id,
|
||||
/// in either direction, and the web side maps back to the right key.
|
||||
#[test]
|
||||
fn web_and_desktop_execs_of_one_terminal_never_share_an_id() {
|
||||
let key = "0b9c6f7e-1d2a-4c3b-9e8f-7a6b5c4d3e2f";
|
||||
let web = web_exec_id(key);
|
||||
assert_ne!(web, key, "a desktop reattach must not supersede the web exec");
|
||||
assert_eq!(tmux_key_of(&web), Some(key));
|
||||
// A desktop exec id is not one the web side would end or detach.
|
||||
assert_eq!(tmux_key_of(key), None);
|
||||
crate::host::persistent::validate_key(&web).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,12 @@
|
||||
"icons/icon.icns",
|
||||
"icons/icon.png"
|
||||
],
|
||||
"externalBin": [
|
||||
"binaries/session-manager-plugin"
|
||||
],
|
||||
"resources": {
|
||||
"THIRD_PARTY_NOTICES": "THIRD_PARTY_NOTICES"
|
||||
},
|
||||
"linux": {
|
||||
"appimage": {
|
||||
"bundleMediaFramework": true
|
||||
|
||||
+6
-2
@@ -6,6 +6,7 @@ import TopBar from "./components/layout/TopBar";
|
||||
import StatusBar from "./components/layout/StatusBar";
|
||||
import NotesDock from "./components/layout/NotesDock";
|
||||
import TerminalView from "./components/terminal/TerminalView";
|
||||
import { dockerFailLabel } from "./lib/dockerStatus";
|
||||
import DockerInstallDialog from "./components/DockerInstallDialog";
|
||||
import ProjectHome from "./components/projects/home/ProjectHome";
|
||||
import AddProjectDialog from "./components/projects/AddProjectDialog";
|
||||
@@ -24,6 +25,7 @@ import { useKeyboardShortcuts } from "./hooks/useKeyboardShortcuts";
|
||||
import { useMarketplaceSyncToasts } from "./hooks/useMarketplace";
|
||||
import { useProjectStatusEvents } from "./hooks/useProjectStatusEvents";
|
||||
import { useWorkspaceWarnings } from "./hooks/useWorkspaceWarnings";
|
||||
import { usePersistentTerminals } from "./hooks/usePersistentTerminals";
|
||||
import MarketplaceView from "./components/marketplace/MarketplaceView";
|
||||
import { useAppState, isHomeTab, tabKeyId, homeTabKey, MARKETPLACE_TAB_KEY } from "./store/appState";
|
||||
import { reconcileAwsProjects, reconcileProjectStatuses } from "./lib/tauri-commands";
|
||||
@@ -79,6 +81,7 @@ export default function App() {
|
||||
useMarketplaceSyncToasts();
|
||||
useProjectStatusEvents();
|
||||
useWorkspaceWarnings();
|
||||
usePersistentTerminals();
|
||||
|
||||
// Initialize on mount
|
||||
useEffect(() => {
|
||||
@@ -213,9 +216,10 @@ export default function App() {
|
||||
* tracks and ends in a real button.
|
||||
*/
|
||||
function WelcomeScreen() {
|
||||
const { dockerAvailable, imageExists, projects, openProjectHome } = useAppState(
|
||||
const { dockerAvailable, dockerStatus, imageExists, projects, openProjectHome } = useAppState(
|
||||
useShallow((s) => ({
|
||||
dockerAvailable: s.dockerAvailable,
|
||||
dockerStatus: s.dockerStatus,
|
||||
imageExists: s.imageExists,
|
||||
projects: s.projects,
|
||||
openProjectHome: s.openProjectHome,
|
||||
@@ -233,7 +237,7 @@ function WelcomeScreen() {
|
||||
label: "Docker detected",
|
||||
state: dockerAvailable,
|
||||
pendingLabel: "Checking for Docker…",
|
||||
failLabel: "Docker not available",
|
||||
failLabel: dockerFailLabel(dockerStatus),
|
||||
},
|
||||
{
|
||||
label: "Container image ready",
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, act } from "@testing-library/react";
|
||||
import DockerInstallDialog from "./DockerInstallDialog";
|
||||
import { useAppState } from "../store/appState";
|
||||
import type { DockerStatus } from "../lib/types";
|
||||
|
||||
vi.mock("../lib/tauri-commands", () => ({
|
||||
getDockerStatus: vi.fn(),
|
||||
checkImageExists: vi.fn(),
|
||||
buildImage: vi.fn(),
|
||||
pullImage: vi.fn(),
|
||||
openUrlExternal: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../hooks/useInstallHelper", () => ({
|
||||
useInstallHelper: () => ({
|
||||
options: {
|
||||
os: "linux",
|
||||
product_name: "Docker Engine",
|
||||
can_auto_install: false,
|
||||
auto_install_method: null,
|
||||
auto_install_blocker: "no pkexec",
|
||||
docs_url: "https://docs.docker.com",
|
||||
manual_steps: [],
|
||||
post_install_notes: [],
|
||||
},
|
||||
loadOptions: vi.fn(),
|
||||
runInstall: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
const status = (state: DockerStatus["state"], os: DockerStatus["os"] = "linux"): DockerStatus => ({
|
||||
state,
|
||||
endpoint: "unix:///var/run/docker.sock",
|
||||
detail: state === "running" ? null : "connection refused",
|
||||
os,
|
||||
});
|
||||
|
||||
function setStatus(s: DockerStatus) {
|
||||
act(() => useAppState.getState().setDockerStatus(s));
|
||||
}
|
||||
|
||||
describe("DockerInstallDialog", () => {
|
||||
beforeEach(() => {
|
||||
useAppState.setState({ dockerStatus: null, dockerAvailable: null });
|
||||
});
|
||||
|
||||
it("offers install guidance when Docker is not installed", () => {
|
||||
setStatus(status("not_installed"));
|
||||
render(<DockerInstallDialog onClose={vi.fn()} />);
|
||||
expect(screen.getByRole("dialog", { name: "Docker is not installed" })).toBeTruthy();
|
||||
expect(screen.getByText(/AWS projects don't need local Docker/)).toBeTruthy();
|
||||
expect(screen.getByText("unix:///var/run/docker.sock")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("says installed-but-not-running with systemd steps on Linux, and offers no install", () => {
|
||||
setStatus(status("not_running"));
|
||||
render(<DockerInstallDialog onClose={vi.fn()} />);
|
||||
expect(screen.getByRole("dialog", { name: "Docker is installed but not running" })).toBeTruthy();
|
||||
expect(screen.getByText("sudo systemctl start docker")).toBeTruthy();
|
||||
expect(screen.getByText("sudo systemctl reset-failed docker")).toBeTruthy();
|
||||
expect(screen.getByText("journalctl -u docker -b")).toBeTruthy();
|
||||
expect(screen.queryByText(/Install Docker Engine/)).toBeNull();
|
||||
expect(screen.getByText(/AWS projects don't need local Docker/)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("tells macOS/Windows users to start Docker Desktop", () => {
|
||||
setStatus(status("not_running", "macos"));
|
||||
render(<DockerInstallDialog onClose={vi.fn()} />);
|
||||
expect(screen.getByText("Docker Desktop")).toBeTruthy();
|
||||
expect(screen.queryByText("sudo systemctl start docker")).toBeNull();
|
||||
});
|
||||
|
||||
it("explains the docker group on permission denied", () => {
|
||||
setStatus(status("no_permission"));
|
||||
render(<DockerInstallDialog onClose={vi.fn()} />);
|
||||
expect(screen.getByRole("dialog", { name: "Docker is running but not accessible" })).toBeTruthy();
|
||||
expect(screen.getByText("sudo usermod -aG docker $USER")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("updates live and closes itself when Docker becomes available", () => {
|
||||
setStatus(status("not_installed"));
|
||||
const onClose = vi.fn();
|
||||
render(<DockerInstallDialog onClose={onClose} />);
|
||||
setStatus(status("not_running"));
|
||||
expect(screen.getByRole("dialog", { name: "Docker is installed but not running" })).toBeTruthy();
|
||||
expect(onClose).not.toHaveBeenCalled();
|
||||
setStatus(status("running"));
|
||||
expect(onClose).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,10 +1,79 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useInstallHelper } from "../hooks/useInstallHelper";
|
||||
import { openUrlExternal } from "../lib/tauri-commands";
|
||||
import type { DockerStatus } from "../lib/types";
|
||||
import { useDocker } from "../hooks/useDocker";
|
||||
import Modal from "./ui/Modal";
|
||||
import Button from "./ui/Button";
|
||||
|
||||
const AWS_NOTE = "AWS projects don't need local Docker and still work.";
|
||||
|
||||
function Cmd({ children }: { children: string }) {
|
||||
return (
|
||||
<code className="block font-mono text-[var(--text-primary)] bg-[var(--bg-primary)] border border-[var(--border-color)] rounded-[var(--radius-control)] px-2 py-1 my-1 select-all break-all">
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
}
|
||||
|
||||
/** Guidance for a Docker that is installed but not answering. */
|
||||
function NotRunningHelp({ status }: { status: DockerStatus }) {
|
||||
if (status.os === "linux") {
|
||||
return (
|
||||
<div className="text-xs text-[var(--text-secondary)] space-y-2">
|
||||
<p>Start the daemon:</p>
|
||||
<Cmd>sudo systemctl start docker</Cmd>
|
||||
<p>
|
||||
If systemd gave up restarting it (<span className="font-mono">start-limit-hit</span>),
|
||||
clear that first, then start it again:
|
||||
</p>
|
||||
<Cmd>sudo systemctl reset-failed docker</Cmd>
|
||||
<p>To see why it stopped:</p>
|
||||
<Cmd>systemctl status docker</Cmd>
|
||||
<Cmd>journalctl -u docker -b</Cmd>
|
||||
<p>
|
||||
To start it at every boot, use{" "}
|
||||
<span className="font-mono">sudo systemctl enable --now docker</span>. Rootless Docker
|
||||
runs as your user instead: <span className="font-mono">systemctl --user start docker</span>.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="text-xs text-[var(--text-secondary)] space-y-2">
|
||||
<p>
|
||||
Start <span className="text-[var(--text-primary)]">Docker Desktop</span> and wait until it
|
||||
says the engine is running — the first start can take a minute. This dialog closes on its
|
||||
own once Docker answers.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function NoPermissionHelp({ status }: { status: DockerStatus }) {
|
||||
if (status.os === "linux") {
|
||||
return (
|
||||
<div className="text-xs text-[var(--text-secondary)] space-y-2">
|
||||
<p>
|
||||
Your user is not in the <span className="font-mono">docker</span> group, so the socket
|
||||
refuses the connection. Add yourself:
|
||||
</p>
|
||||
<Cmd>sudo usermod -aG docker $USER</Cmd>
|
||||
<p>Then log out and back in (or reboot) so the new group applies, and reopen Triple-C.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="text-xs text-[var(--text-secondary)] space-y-2">
|
||||
<p>
|
||||
The Docker endpoint refused access. Make sure Docker Desktop is running for your user
|
||||
{status.os === "windows" ? " and that your account is in the docker-users group" : ""}, then
|
||||
re-check.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onClose: () => void;
|
||||
}
|
||||
@@ -13,7 +82,7 @@ type Phase = "idle" | "installing" | "done" | "error";
|
||||
|
||||
export default function DockerInstallDialog({ onClose }: Props) {
|
||||
const { options, loadOptions, runInstall } = useInstallHelper();
|
||||
const { checkDocker } = useDocker();
|
||||
const { checkDocker, dockerStatus } = useDocker();
|
||||
const [showManual, setShowManual] = useState(false);
|
||||
const [phase, setPhase] = useState<Phase>("idle");
|
||||
const [log, setLog] = useState<string[]>([]);
|
||||
@@ -23,6 +92,13 @@ export default function DockerInstallDialog({ onClose }: Props) {
|
||||
loadOptions();
|
||||
}, [loadOptions]);
|
||||
|
||||
// Docker came up while the dialog was open (polling keeps `dockerStatus`
|
||||
// current): nothing left to say. Not mid-install, whose notes still matter.
|
||||
const running = dockerStatus?.state === "running";
|
||||
useEffect(() => {
|
||||
if (running && (phase === "idle" || phase === "error")) onClose();
|
||||
}, [running, phase, onClose]);
|
||||
|
||||
const handleInstall = async () => {
|
||||
setPhase("installing");
|
||||
setLog([]);
|
||||
@@ -52,6 +128,50 @@ export default function DockerInstallDialog({ onClose }: Props) {
|
||||
if (available) onClose();
|
||||
};
|
||||
|
||||
const state = dockerStatus?.state ?? "not_installed";
|
||||
const endpointLine = dockerStatus && (
|
||||
<p className="text-[11px] text-[var(--text-secondary)] mt-3 break-all">
|
||||
Tried <span className="font-mono">{dockerStatus.endpoint}</span>
|
||||
{dockerStatus.detail ? <> — {dockerStatus.detail}</> : null}
|
||||
</p>
|
||||
);
|
||||
const awsLine = (
|
||||
<p className="text-[11px] text-[var(--text-secondary)] mt-2">{AWS_NOTE}</p>
|
||||
);
|
||||
|
||||
if (dockerStatus && (state === "not_running" || state === "no_permission")) {
|
||||
return (
|
||||
<Modal
|
||||
title={state === "not_running" ? "Docker is installed but not running" : "Docker is running but not accessible"}
|
||||
onClose={onClose}
|
||||
widthClassName="w-[34rem]"
|
||||
footer={
|
||||
<>
|
||||
<Button variant="ghost" onClick={onClose}>
|
||||
Dismiss
|
||||
</Button>
|
||||
<Button variant="primary" onClick={handleRecheck}>
|
||||
Re-check
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<p className="text-[13px] text-[var(--text-secondary)] mb-3">
|
||||
{state === "not_running"
|
||||
? "Triple-C found Docker on this machine, but the Docker daemon is not answering. There is nothing to install."
|
||||
: "The Docker daemon is up, but this user is not allowed to talk to it."}
|
||||
</p>
|
||||
{state === "not_running" ? (
|
||||
<NotRunningHelp status={dockerStatus} />
|
||||
) : (
|
||||
<NoPermissionHelp status={dockerStatus} />
|
||||
)}
|
||||
{endpointLine}
|
||||
{awsLine}
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
if (!options) {
|
||||
return null;
|
||||
}
|
||||
@@ -61,7 +181,7 @@ export default function DockerInstallDialog({ onClose }: Props) {
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title="Docker not detected"
|
||||
title="Docker is not installed"
|
||||
onClose={onClose}
|
||||
widthClassName="w-[34rem]"
|
||||
// Closing mid-install would orphan a privileged installer.
|
||||
@@ -80,6 +200,8 @@ export default function DockerInstallDialog({ onClose }: Props) {
|
||||
<span className="text-[var(--text-primary)]">{options.product_name}</span> for
|
||||
you, or you can follow the official instructions.
|
||||
</p>
|
||||
{awsLine}
|
||||
<div className="mb-4">{endpointLine}</div>
|
||||
|
||||
{phase === "idle" && (
|
||||
<div className="flex flex-col gap-2">
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dockerFailLabel } from "../../lib/dockerStatus";
|
||||
import { useState } from "react";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import MainTabs from "./MainTabs";
|
||||
@@ -9,9 +10,10 @@ import HelpDialog from "./HelpDialog";
|
||||
import StatusIndicator, { type StatusTone } from "../ui/StatusIndicator";
|
||||
|
||||
export default function TopBar() {
|
||||
const { dockerAvailable, imageExists, updateInfo, imageUpdateInfo, appVersion, setUpdateInfo, setImageUpdateInfo } = useAppState(
|
||||
const { dockerAvailable, dockerStatus, imageExists, updateInfo, imageUpdateInfo, appVersion, setUpdateInfo, setImageUpdateInfo } = useAppState(
|
||||
useShallow(s => ({
|
||||
dockerAvailable: s.dockerAvailable,
|
||||
dockerStatus: s.dockerStatus,
|
||||
imageExists: s.imageExists,
|
||||
updateInfo: s.updateInfo,
|
||||
imageUpdateInfo: s.imageUpdateInfo,
|
||||
@@ -76,7 +78,7 @@ export default function TopBar() {
|
||||
<HealthDot
|
||||
state={dockerAvailable}
|
||||
okLabel="Docker"
|
||||
failLabel="Docker unavailable"
|
||||
failLabel={dockerFailLabel(dockerStatus)}
|
||||
pendingLabel="Docker — checking"
|
||||
/>
|
||||
<HealthDot
|
||||
|
||||
@@ -149,4 +149,11 @@ describe("PermissionModeControl", () => {
|
||||
/Caution/i,
|
||||
);
|
||||
});
|
||||
|
||||
it("shows the sandbox OFF on an AWS project whatever the setting says", () => {
|
||||
const aws = { ...baseProject, host_id: "aws-1", sandbox_mode_enabled: true, permission_mode: "bypass" as const };
|
||||
render(<PermissionModeControl project={aws} onChange={vi.fn()} />);
|
||||
expect(screen.getByTestId("sandbox-state")).toHaveTextContent(/Sandbox OFF/);
|
||||
expect(screen.getByTestId("permission-mode-hint")).toHaveTextContent(/Caution/i);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { PermissionMode, Project } from "../../lib/types";
|
||||
import SegmentedControl, { type Segment } from "../ui/SegmentedControl";
|
||||
import { sandboxEffective } from "../../lib/awsWorkspace";
|
||||
|
||||
export const PERMISSION_MODES: Segment<PermissionMode>[] = [
|
||||
{ value: "plan", label: "Plan", hint: "Claude proposes a plan and makes no changes." },
|
||||
@@ -56,7 +57,7 @@ export default function PermissionModeControl({
|
||||
disabledReason,
|
||||
}: Props) {
|
||||
const mode = effectivePermissionMode(project);
|
||||
const sandboxOn = project.sandbox_mode_enabled;
|
||||
const sandboxOn = sandboxEffective(project);
|
||||
const uncontainedBypass = mode === "bypass" && !sandboxOn;
|
||||
|
||||
const segments = PERMISSION_MODES.map((segment) =>
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { render, screen, fireEvent, act } from "@testing-library/react";
|
||||
import PortMappingsEditor from "./PortMappingsEditor";
|
||||
import type { PortMapping } from "../../lib/types";
|
||||
|
||||
const tcp = (host_port: number, container_port: number): PortMapping => ({
|
||||
host_port,
|
||||
container_port,
|
||||
protocol: "tcp",
|
||||
});
|
||||
|
||||
function setup(initial: PortMapping[], extra: { tcpOnly?: boolean } = {}) {
|
||||
const onSave = vi.fn().mockResolvedValue(true);
|
||||
const view = render(
|
||||
<PortMappingsEditor portMappings={initial} disabled={false} onSave={onSave} {...extra} />,
|
||||
);
|
||||
const rerender = (next: PortMapping[]) =>
|
||||
view.rerender(
|
||||
<PortMappingsEditor portMappings={next} disabled={false} onSave={onSave} {...extra} />,
|
||||
);
|
||||
return { onSave, rerender };
|
||||
}
|
||||
|
||||
const host = (n = 1) => screen.getByLabelText(`Host port ${n}`) as HTMLInputElement;
|
||||
const container = (n = 1) => screen.getByLabelText(`Container port ${n}`) as HTMLInputElement;
|
||||
|
||||
function type(el: HTMLInputElement, value: string) {
|
||||
act(() => el.focus());
|
||||
fireEvent.change(el, { target: { value } });
|
||||
}
|
||||
|
||||
describe("PortMappingsEditor", () => {
|
||||
it("keeps typed ports when the project refreshes mid-edit (the reported 0/0 save)", () => {
|
||||
// A stored half-row, as the old "+ Add" saved it, then a project refresh
|
||||
// (status event, save response, list reload) hands over a new-but-equal
|
||||
// array while the user is typing.
|
||||
const { onSave, rerender } = setup([tcp(0, 0)]);
|
||||
type(host(), "8080");
|
||||
rerender([tcp(0, 0)]);
|
||||
type(container(), "8080");
|
||||
rerender([tcp(0, 0)]);
|
||||
expect(host().value).toBe("8080");
|
||||
expect(container().value).toBe("8080");
|
||||
fireEvent.blur(container());
|
||||
expect(onSave).toHaveBeenLastCalledWith([tcp(8080, 8080)]);
|
||||
});
|
||||
|
||||
it("keeps typed ports when its own earlier save's response arrives mid-edit", () => {
|
||||
const { onSave, rerender } = setup([]);
|
||||
fireEvent.click(screen.getByRole("button", { name: /add port mapping/i }));
|
||||
type(host(), "8080");
|
||||
fireEvent.blur(host());
|
||||
type(container(), "80");
|
||||
// A stale echo of an older stored value must not replace the edit in focus.
|
||||
rerender([]);
|
||||
type(container(), "8080");
|
||||
fireEvent.blur(container());
|
||||
expect(onSave).toHaveBeenLastCalledWith([tcp(8080, 8080)]);
|
||||
expect(container().value).toBe("8080");
|
||||
});
|
||||
|
||||
it("does not save a newly added empty row", () => {
|
||||
const { onSave } = setup([]);
|
||||
fireEvent.click(screen.getByRole("button", { name: /add port mapping/i }));
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(host()).toHaveFocus();
|
||||
expect(screen.getByText(/fill in both ports/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not present an empty row as a filled-in mapping (no port-number placeholder)", () => {
|
||||
// The reported case: the empty row's "8080" placeholders read as 8080 → 8080.
|
||||
setup([]);
|
||||
fireEvent.click(screen.getByRole("button", { name: /add port mapping/i }));
|
||||
for (const el of [host(), container()]) {
|
||||
expect(el.value).toBe("");
|
||||
expect(el.placeholder).not.toMatch(/\d/);
|
||||
}
|
||||
expect(screen.getByText(/isn't saved or active/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("never saves a half-filled row, and saves it once both ports are in", () => {
|
||||
const { onSave } = setup([tcp(3000, 3000)]);
|
||||
fireEvent.click(screen.getByRole("button", { name: /add port mapping/i }));
|
||||
type(host(2), "8080");
|
||||
fireEvent.blur(host(2));
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText(/fill in both ports/i)).toBeInTheDocument();
|
||||
type(container(2), "9090");
|
||||
fireEvent.blur(container(2));
|
||||
expect(onSave).toHaveBeenLastCalledWith([tcp(3000, 3000), tcp(8080, 9090)]);
|
||||
expect(screen.queryByText(/fill in both ports/i)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows a stored incomplete row as incomplete", () => {
|
||||
setup([tcp(0, 0), tcp(8080, 8080)]);
|
||||
expect(screen.getAllByText(/fill in both ports/i)).toHaveLength(1);
|
||||
expect(host(1).value).toBe("");
|
||||
});
|
||||
|
||||
it("refuses an out-of-range port without saving it", () => {
|
||||
const { onSave } = setup([]);
|
||||
fireEvent.click(screen.getByRole("button", { name: /add port mapping/i }));
|
||||
type(host(), "70000");
|
||||
type(container(), "80");
|
||||
fireEvent.blur(container());
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText(/1 and 65535/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("takes a changed stored value once the editor is not being edited", () => {
|
||||
const { rerender } = setup([tcp(3000, 3000)]);
|
||||
rerender([tcp(4000, 4000)]);
|
||||
expect(host().value).toBe("4000");
|
||||
});
|
||||
|
||||
it("removing a row saves the rest; the protocol saves at once (Docker keeps UDP)", () => {
|
||||
const { onSave } = setup([tcp(3000, 3000), tcp(4000, 4000)]);
|
||||
fireEvent.change(screen.getByLabelText("Protocol 2"), { target: { value: "udp" } });
|
||||
expect(onSave).toHaveBeenLastCalledWith([tcp(3000, 3000), { ...tcp(4000, 4000), protocol: "udp" }]);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Remove port mapping 1" }));
|
||||
expect(onSave).toHaveBeenLastCalledWith([{ ...tcp(4000, 4000), protocol: "udp" }]);
|
||||
});
|
||||
|
||||
it("does not save when nothing changed", () => {
|
||||
const { onSave } = setup([tcp(3000, 3000)]);
|
||||
act(() => host().focus());
|
||||
fireEvent.blur(host());
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { PortMapping } from "../../lib/types";
|
||||
import Button from "../ui/Button";
|
||||
import { monoInputClass, selectClass } from "../ui/Field";
|
||||
@@ -12,43 +12,158 @@ interface Props {
|
||||
onSave: (mappings: PortMapping[]) => Promise<unknown>;
|
||||
}
|
||||
|
||||
/** One row as typed: ports stay text until they are valid, so a half-typed
|
||||
* row is never turned into a 0. */
|
||||
interface Row {
|
||||
key: number;
|
||||
host: string;
|
||||
container: string;
|
||||
protocol: string;
|
||||
}
|
||||
|
||||
/** Placeholders are words, not port numbers: a "8080" placeholder in an
|
||||
* empty row read as a filled-in 8080 → 8080 mapping that was never typed. */
|
||||
const placeholderClass = "placeholder:italic placeholder:text-[var(--text-disabled)]";
|
||||
|
||||
function parsePort(text: string): number | null {
|
||||
if (!/^\d+$/.test(text.trim())) return null;
|
||||
const n = Number(text.trim());
|
||||
return n >= 1 && n <= 65535 ? n : null;
|
||||
}
|
||||
|
||||
/** Why a row is not a mapping yet, or `null` when it is one. */
|
||||
export function rowProblem(row: Pick<Row, "host" | "container">): string | null {
|
||||
if (row.host.trim() === "" || row.container.trim() === "") return "Fill in both ports";
|
||||
if (parsePort(row.host) === null || parsePort(row.container) === null) {
|
||||
return "Ports must be whole numbers between 1 and 65535";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The rows that are complete mappings — the only ones ever saved. */
|
||||
function completeMappings(rows: Row[]): PortMapping[] {
|
||||
return rows.flatMap((r) => {
|
||||
const host_port = parsePort(r.host);
|
||||
const container_port = parsePort(r.container);
|
||||
return host_port !== null && container_port !== null && rowProblem(r) === null
|
||||
? [{ host_port, container_port, protocol: r.protocol }]
|
||||
: [];
|
||||
});
|
||||
}
|
||||
|
||||
let nextKey = 1;
|
||||
const portText = (n: number) => (n > 0 ? String(n) : "");
|
||||
function toRows(mappings: PortMapping[]): Row[] {
|
||||
return mappings.map((m) => ({
|
||||
key: nextKey++,
|
||||
host: portText(m.host_port),
|
||||
container: portText(m.container_port),
|
||||
protocol: m.protocol,
|
||||
}));
|
||||
}
|
||||
|
||||
export default function PortMappingsEditor({
|
||||
portMappings: initial,
|
||||
portMappings: stored,
|
||||
disabled,
|
||||
disabledReason,
|
||||
tcpOnly = false,
|
||||
onSave,
|
||||
}: Props) {
|
||||
const [mappings, setMappings] = useState<PortMapping[]>(initial);
|
||||
const [rows, setRowsState] = useState<Row[]>(() => toRows(stored));
|
||||
// The latest rows, for blur handlers that run before a re-render.
|
||||
const rowsRef = useRef(rows);
|
||||
const setRows = (next: Row[]) => {
|
||||
rowsRef.current = next;
|
||||
setRowsState(next);
|
||||
};
|
||||
|
||||
// What is stored, as this editor last loaded or saved it. A project refresh
|
||||
// hands over a new array on every update (status events, save responses,
|
||||
// list reloads); only a value that differs from this is a real change.
|
||||
const storedKey = JSON.stringify(stored);
|
||||
const syncedKey = useRef(storedKey);
|
||||
const wrapper = useRef<HTMLDivElement>(null);
|
||||
const [focusLeft, setFocusLeft] = useState(0);
|
||||
const focusHost = useRef<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setMappings(initial);
|
||||
}, [initial]);
|
||||
if (storedKey === syncedKey.current) return;
|
||||
// Never replace what the user is typing; this runs again when focus
|
||||
// leaves the editor.
|
||||
if (wrapper.current?.contains(document.activeElement)) return;
|
||||
syncedKey.current = storedKey;
|
||||
const fresh = toRows(JSON.parse(storedKey) as PortMapping[]);
|
||||
// Rows still being filled in are not stored; keep them.
|
||||
const drafts = rowsRef.current.filter((r) => rowProblem(r) !== null);
|
||||
const freshKeys = new Set(fresh.map((r) => `${r.host}|${r.container}`));
|
||||
setRows([...fresh, ...drafts.filter((r) => !freshKeys.has(`${r.host}|${r.container}`))]);
|
||||
}, [storedKey, focusLeft]);
|
||||
|
||||
const updatePort = (
|
||||
index: number,
|
||||
field: "host_port" | "container_port",
|
||||
value: string,
|
||||
) => {
|
||||
const updated = [...mappings];
|
||||
const num = parseInt(value, 10);
|
||||
updated[index] = { ...updated[index], [field]: isNaN(num) ? 0 : num };
|
||||
setMappings(updated);
|
||||
useEffect(() => {
|
||||
if (focusHost.current === null) return;
|
||||
const el = wrapper.current?.querySelector<HTMLInputElement>(
|
||||
`[data-row="${focusHost.current}"][data-field="host"]`,
|
||||
);
|
||||
focusHost.current = null;
|
||||
el?.focus();
|
||||
}, [rows]);
|
||||
|
||||
/** Save the complete rows, if they differ from what is stored. */
|
||||
const commit = (next: Row[]) => {
|
||||
const mappings = completeMappings(next);
|
||||
const key = JSON.stringify(mappings);
|
||||
if (key === syncedKey.current) return;
|
||||
syncedKey.current = key;
|
||||
void onSave(mappings);
|
||||
};
|
||||
|
||||
const edit = (index: number, field: "host" | "container", value: string) => {
|
||||
const next = [...rowsRef.current];
|
||||
next[index] = { ...next[index], [field]: value };
|
||||
setRows(next);
|
||||
};
|
||||
|
||||
const portInput = (row: Row, i: number, field: "host" | "container") => {
|
||||
const value = row[field];
|
||||
const missing = rowProblem(row) !== null && parsePort(value) === null;
|
||||
return (
|
||||
<input
|
||||
type="text"
|
||||
inputMode="numeric"
|
||||
value={value}
|
||||
onChange={(e) => edit(i, field, e.target.value)}
|
||||
onBlur={() => commit(rowsRef.current)}
|
||||
placeholder={field === "host" ? "Host port" : "Container port"}
|
||||
aria-label={`${field === "host" ? "Host" : "Container"} port ${i + 1}`}
|
||||
aria-invalid={missing || undefined}
|
||||
aria-describedby={rowProblem(row) ? `pm-problem-${row.key}` : undefined}
|
||||
data-row={row.key}
|
||||
data-field={field}
|
||||
disabled={disabled}
|
||||
className={`w-[28%] ${monoInputClass} ${placeholderClass}`}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div
|
||||
ref={wrapper}
|
||||
className="space-y-2"
|
||||
onBlur={(e) => {
|
||||
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setFocusLeft((n) => n + 1);
|
||||
}}
|
||||
>
|
||||
{disabled && disabledReason && (
|
||||
<p className="px-2 py-1.5 bg-[var(--warning-muted)] border border-[var(--warning)]/30 rounded-[var(--radius-control)] text-xs text-[var(--warning)]">
|
||||
{disabledReason}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{mappings.length === 0 && (
|
||||
{rows.length === 0 && (
|
||||
<p className="text-xs text-[var(--text-secondary)]">No port mappings configured.</p>
|
||||
)}
|
||||
|
||||
{mappings.length > 0 && (
|
||||
{rows.length > 0 && (
|
||||
<div className="flex gap-2 items-center text-xs text-[var(--text-secondary)] px-0.5">
|
||||
<span className="w-[28%]">Host port</span>
|
||||
<span className="w-[28%]">Container port</span>
|
||||
@@ -57,71 +172,58 @@ export default function PortMappingsEditor({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{mappings.map((pm, i) => (
|
||||
<div key={i} className="flex gap-2 items-center">
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
max="65535"
|
||||
value={pm.host_port || ""}
|
||||
onChange={(e) => updatePort(i, "host_port", e.target.value)}
|
||||
onBlur={() => onSave(mappings)}
|
||||
placeholder="8080"
|
||||
aria-label={`Host port ${i + 1}`}
|
||||
disabled={disabled}
|
||||
className={`w-[28%] ${monoInputClass}`}
|
||||
/>
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
max="65535"
|
||||
value={pm.container_port || ""}
|
||||
onChange={(e) => updatePort(i, "container_port", e.target.value)}
|
||||
onBlur={() => onSave(mappings)}
|
||||
placeholder="8080"
|
||||
aria-label={`Container port ${i + 1}`}
|
||||
disabled={disabled}
|
||||
className={`w-[28%] ${monoInputClass}`}
|
||||
/>
|
||||
<select
|
||||
value={pm.protocol}
|
||||
aria-label={`Protocol ${i + 1}`}
|
||||
onChange={(e) => {
|
||||
const updated = [...mappings];
|
||||
updated[i] = { ...updated[i], protocol: e.target.value };
|
||||
setMappings(updated);
|
||||
onSave(updated);
|
||||
}}
|
||||
disabled={disabled}
|
||||
className={`w-[22%] ${selectClass}`}
|
||||
>
|
||||
<option value="tcp">TCP</option>
|
||||
{(!tcpOnly || pm.protocol === "udp") && <option value="udp">UDP</option>}
|
||||
</select>
|
||||
<Button
|
||||
variant="danger"
|
||||
disabled={disabled}
|
||||
aria-label={`Remove port mapping ${i + 1}`}
|
||||
onClick={() => {
|
||||
const updated = mappings.filter((_, j) => j !== i);
|
||||
setMappings(updated);
|
||||
onSave(updated);
|
||||
}}
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
{rows.map((row, i) => {
|
||||
const problem = rowProblem(row);
|
||||
return (
|
||||
<div key={row.key} className="space-y-1">
|
||||
<div className="flex gap-2 items-center">
|
||||
{portInput(row, i, "host")}
|
||||
{portInput(row, i, "container")}
|
||||
<select
|
||||
value={row.protocol}
|
||||
aria-label={`Protocol ${i + 1}`}
|
||||
onChange={(e) => {
|
||||
const next = [...rowsRef.current];
|
||||
next[i] = { ...next[i], protocol: e.target.value };
|
||||
setRows(next);
|
||||
commit(next);
|
||||
}}
|
||||
disabled={disabled}
|
||||
className={`w-[22%] ${selectClass}`}
|
||||
>
|
||||
<option value="tcp">TCP</option>
|
||||
{(!tcpOnly || row.protocol === "udp") && <option value="udp">UDP</option>}
|
||||
</select>
|
||||
<Button
|
||||
variant="danger"
|
||||
disabled={disabled}
|
||||
aria-label={`Remove port mapping ${i + 1}`}
|
||||
onClick={() => {
|
||||
const next = rowsRef.current.filter((_, j) => j !== i);
|
||||
setRows(next);
|
||||
commit(next);
|
||||
}}
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
</div>
|
||||
{problem && (
|
||||
<p id={`pm-problem-${row.key}`} className="text-xs text-[var(--warning)] px-0.5">
|
||||
⚠ {problem} — this mapping isn't saved or active.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
|
||||
<Button
|
||||
disabled={disabled}
|
||||
onClick={() => {
|
||||
const updated = [
|
||||
...mappings,
|
||||
{ host_port: 0, container_port: 0, protocol: "tcp" },
|
||||
];
|
||||
setMappings(updated);
|
||||
onSave(updated);
|
||||
// Kept here until both ports are filled in: an empty row is never
|
||||
// saved (it used to be stored as 0 → 0 and silently skipped).
|
||||
const row: Row = { key: nextKey++, host: "", container: "", protocol: "tcp" };
|
||||
focusHost.current = row.key;
|
||||
setRows([...rowsRef.current, row]);
|
||||
}}
|
||||
>
|
||||
+ Add port mapping
|
||||
|
||||
@@ -4,10 +4,12 @@ import type { AwsWorkspaceConfig, HourlyEstimate } from "../../../lib/types";
|
||||
import { defaultAwsWorkspace } from "../../../lib/awsWorkspace";
|
||||
|
||||
const awsCostEstimate =
|
||||
vi.fn<(id: string, region: string, config: AwsWorkspaceConfig) => Promise<HourlyEstimate | null>>();
|
||||
vi.fn<
|
||||
(id: string, region: string, config: AwsWorkspaceConfig, instanceType: string | null) => Promise<HourlyEstimate | null>
|
||||
>();
|
||||
vi.mock("../../../lib/tauri-commands", () => ({
|
||||
awsCostEstimate: (id: string, region: string, config: AwsWorkspaceConfig) =>
|
||||
awsCostEstimate(id, region, config),
|
||||
awsCostEstimate: (id: string, region: string, config: AwsWorkspaceConfig, instanceType: string | null) =>
|
||||
awsCostEstimate(id, region, config, instanceType),
|
||||
}));
|
||||
|
||||
import AwsCostEstimate, { ESTIMATE_DEBOUNCE_MS, formatHourly } from "./AwsCostEstimate";
|
||||
@@ -43,12 +45,14 @@ describe("AwsCostEstimate", () => {
|
||||
usd_per_hour: 0.05826,
|
||||
note_spot: false,
|
||||
efs_billed_separately: false,
|
||||
ec2_instance: false,
|
||||
root_volume_included: false,
|
||||
});
|
||||
const c = config();
|
||||
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={c} />);
|
||||
expect(screen.getByText("Estimating…")).toBeInTheDocument();
|
||||
await settle();
|
||||
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c);
|
||||
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c, null);
|
||||
expect(screen.getByText("≈ $0.058/hour")).toBeInTheDocument();
|
||||
expect(screen.queryByText(/Spot/)).toBeNull();
|
||||
expect(screen.queryByText(/EFS/)).toBeNull();
|
||||
@@ -59,6 +63,8 @@ describe("AwsCostEstimate", () => {
|
||||
usd_per_hour: 0.05,
|
||||
note_spot: true,
|
||||
efs_billed_separately: true,
|
||||
ec2_instance: false,
|
||||
root_volume_included: false,
|
||||
});
|
||||
render(
|
||||
<AwsCostEstimate
|
||||
@@ -96,7 +102,7 @@ describe("AwsCostEstimate", () => {
|
||||
});
|
||||
|
||||
it("asks once per settled change of a cost field, not for unrelated edits", async () => {
|
||||
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false });
|
||||
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false, ec2_instance: false, root_volume_included: false });
|
||||
const { rerender } = render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
|
||||
await settle();
|
||||
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
|
||||
@@ -113,4 +119,41 @@ describe("AwsCostEstimate", () => {
|
||||
expect(awsCostEstimate).toHaveBeenCalledTimes(2);
|
||||
expect(awsCostEstimate.mock.calls[1][2].cpu).toBe(4096);
|
||||
});
|
||||
|
||||
it("says when an EC2 estimate lacks the root volume's price", async () => {
|
||||
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: false, ec2_instance: true, root_volume_included: false });
|
||||
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ launch_type: "ec2" })} instanceType="m7i.large" />);
|
||||
await settle();
|
||||
expect(screen.getByText(/Its 50 GiB gp3 root volume \(about \$4 a month\) is not included/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/public IPv4 address adds/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("prices an EC2 project as its instance type, and says instances scale to zero", async () => {
|
||||
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: true, ec2_instance: true, root_volume_included: true });
|
||||
const c = config({ launch_type: "ec2", storage: "hybrid" });
|
||||
const { rerender } = render(
|
||||
<AwsCostEstimate connectionId="c1" region="us-west-2" config={c} instanceType="m7i.large" />,
|
||||
);
|
||||
await settle();
|
||||
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c, "m7i.large");
|
||||
expect(screen.getByText("≈ $0.10/hour")).toBeInTheDocument();
|
||||
expect(screen.getByText(/Instances scale to zero when no workspace is running/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/EFS storage is billed separately/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Includes its 50 GiB gp3 root volume/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/public IPv4 address adds \$0.005\/hour/)).toBeInTheDocument();
|
||||
expect(screen.queryByText(/While the task runs/)).toBeNull();
|
||||
// The reservations don't change an instance's price: no new call.
|
||||
rerender(
|
||||
<AwsCostEstimate connectionId="c1" region="us-west-2" config={{ ...c, cpu: 512, memory_mib: 1024 }} instanceType="m7i.large" />,
|
||||
);
|
||||
await settle();
|
||||
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not ask for an EC2 project whose instance type is unknown", async () => {
|
||||
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ launch_type: "ec2" })} instanceType={null} />);
|
||||
await settle();
|
||||
expect(awsCostEstimate).not.toHaveBeenCalled();
|
||||
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,8 @@ interface Props {
|
||||
connectionId: string | null;
|
||||
region: string | null | undefined;
|
||||
config: AwsWorkspaceConfig;
|
||||
/** An EC2 project's instance type (the user's); null when unknown. */
|
||||
instanceType?: string | null;
|
||||
}
|
||||
|
||||
type State = { kind: "loading" } | { kind: "done"; estimate: HourlyEstimate | null };
|
||||
@@ -25,21 +27,27 @@ type State = { kind: "loading" } | { kind: "done"; estimate: HourlyEstimate | nu
|
||||
* network or no connection reads "Estimate unavailable" — never an error toast, and nothing waits
|
||||
* on it. Re-asked only when a field that changes the price changes, after edits settle.
|
||||
*/
|
||||
export default function AwsCostEstimate({ connectionId, region, config }: Props) {
|
||||
export default function AwsCostEstimate({ connectionId, region, config, instanceType = null }: Props) {
|
||||
const [state, setState] = useState<State>({ kind: "loading" });
|
||||
const { cpu, memory_mib, arch, ephemeral_gib, spot, storage } = config;
|
||||
const { launch_type, cpu, memory_mib, arch, ephemeral_gib, spot, storage } = config;
|
||||
const ec2 = launch_type === "ec2";
|
||||
// Only what the backend prices or notes; the rest of the config is sent but changes nothing.
|
||||
const key = JSON.stringify([connectionId, region, cpu, memory_mib, arch, ephemeral_gib, spot, storage]);
|
||||
// An EC2 project is priced as its instance (E10): only the type and storage matter.
|
||||
const key = JSON.stringify(
|
||||
ec2
|
||||
? [connectionId, region, launch_type, instanceType, storage]
|
||||
: [connectionId, region, launch_type, cpu, memory_mib, arch, ephemeral_gib, spot, storage],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!connectionId || !region) {
|
||||
if (!connectionId || !region || (ec2 && !instanceType)) {
|
||||
setState({ kind: "done", estimate: null });
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
setState({ kind: "loading" });
|
||||
const timer = setTimeout(() => {
|
||||
awsCostEstimate(connectionId, region, config)
|
||||
awsCostEstimate(connectionId, region, config, ec2 ? instanceType : null)
|
||||
.catch(() => null)
|
||||
.then((estimate) => {
|
||||
if (!cancelled) setState({ kind: "done", estimate });
|
||||
@@ -65,7 +73,17 @@ export default function AwsCostEstimate({ connectionId, region, config }: Props)
|
||||
}`
|
||||
: "Estimate unavailable"}
|
||||
</p>
|
||||
{estimate && (
|
||||
{estimate?.ec2_instance && (
|
||||
<p className="text-[var(--text-secondary)]">
|
||||
While your instance runs. Instances scale to zero when no workspace is running.
|
||||
{estimate.root_volume_included
|
||||
? " Includes its 50 GiB gp3 root volume."
|
||||
: " Its 50 GiB gp3 root volume (about $4 a month) is not included."}
|
||||
{" In an environment with public networking, the instance's public IPv4 address adds $0.005/hour."}
|
||||
{estimate.efs_billed_separately && " EFS storage is billed separately."}
|
||||
</p>
|
||||
)}
|
||||
{estimate && !estimate.ec2_instance && (
|
||||
<p className="text-[var(--text-secondary)]">
|
||||
While the task runs; nothing while it is stopped.
|
||||
{estimate.efs_billed_separately && " EFS storage is billed separately."}
|
||||
|
||||
@@ -15,6 +15,7 @@ import ContainerMigrationBanner from "./ContainerMigrationBanner";
|
||||
import AwsWorkspaceStatusCard from "./AwsWorkspaceStatusCard";
|
||||
import AwsCostEstimate from "./AwsCostEstimate";
|
||||
import { isAwsProject } from "../../../lib/awsWorkspace";
|
||||
import { useEc2Capacity } from "../../../lib/ec2Capacity";
|
||||
import type { ContainerMigration } from "../../../hooks/useContainerMigration";
|
||||
import SaveIndicator from "../../ui/SaveIndicator";
|
||||
import Button from "../../ui/Button";
|
||||
@@ -53,6 +54,11 @@ export default function OverviewTab({
|
||||
onOpenMigration,
|
||||
}: Props) {
|
||||
const [sessions, setSessions] = useState<ClaudeSession[]>([]);
|
||||
// An EC2 project's estimate is its instance type's price (E10).
|
||||
const ec2Capacity = useEc2Capacity(
|
||||
project,
|
||||
isAwsProject(project) && project.status === "running" && project.aws?.launch_type === "ec2",
|
||||
);
|
||||
const [tasks, setTasks] = useState<ScheduledTask[]>([]);
|
||||
const [notificationCount, setNotificationCount] = useState(0);
|
||||
const running = project.status === "running";
|
||||
@@ -152,6 +158,7 @@ export default function OverviewTab({
|
||||
connectionId={project.host_id}
|
||||
region={project.aws_placement?.region}
|
||||
config={project.aws}
|
||||
instanceType={ec2Capacity?.state === "ec2" ? ec2Capacity.compute.instance_type : null}
|
||||
/>
|
||||
</section>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import type { Project, WorkspaceTerminal } from "../../../lib/types";
|
||||
|
||||
const listWorkspaceTerminals = vi.fn();
|
||||
const endWorkspaceTerminal = vi.fn(async () => {});
|
||||
const closeTerminalSession = vi.fn(async () => {});
|
||||
vi.mock("../../../lib/tauri-commands", () => ({
|
||||
listClaudeSessions: vi.fn(async () => []),
|
||||
listWorkspaceTerminals: (id: string) => listWorkspaceTerminals(id),
|
||||
endWorkspaceTerminal: (p: string, s: string) => endWorkspaceTerminal(p, s),
|
||||
closeTerminalSession: (s: string) => closeTerminalSession(s),
|
||||
updateProject: vi.fn(),
|
||||
}));
|
||||
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() }));
|
||||
|
||||
import SessionsTab from "./SessionsTab";
|
||||
import { useAppState } from "../../../store/appState";
|
||||
|
||||
const actions = { resumeClaudeSession: vi.fn() } as never;
|
||||
const aws = { id: "p1", name: "api", host_id: "h1", status: "running", container_id: "http://x/" } as unknown as Project;
|
||||
const t = (id: string, over: Partial<WorkspaceTerminal> = {}): WorkspaceTerminal => ({
|
||||
session_id: id,
|
||||
kind: "claude",
|
||||
name: null,
|
||||
created_unix: Math.floor(Date.now() / 1000) - 600,
|
||||
attached: false,
|
||||
known: true,
|
||||
...over,
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
listWorkspaceTerminals.mockReset();
|
||||
endWorkspaceTerminal.mockClear();
|
||||
closeTerminalSession.mockClear();
|
||||
useAppState.setState({ projects: [aws], sessions: [], tabOrder: [], activeTabKey: null } as never);
|
||||
});
|
||||
|
||||
describe("SessionsTab — terminals in an AWS workspace", () => {
|
||||
it("lists them with their state and reattaches one in a tab", async () => {
|
||||
listWorkspaceTerminals.mockResolvedValue([
|
||||
t("open-1", { name: "main" }),
|
||||
t("det-2", { kind: "bash" }),
|
||||
t("web-3", { known: false, attached: true, name: "web terminal" }),
|
||||
]);
|
||||
useAppState.getState().addSession({ id: "open-1", projectId: "p1", projectName: "api", sessionType: "claude", sessionName: "main", persistent: true });
|
||||
render(<SessionsTab project={aws} actions={actions} />);
|
||||
|
||||
const list = await screen.findByRole("list", { name: "Terminals in the workspace" });
|
||||
expect(list.textContent).toContain("main");
|
||||
expect(list.textContent).toContain("Open here");
|
||||
expect(list.textContent).toContain("Shell · Detached");
|
||||
expect(list.textContent).toContain("Attached elsewhere");
|
||||
expect(list.textContent).toContain("opened elsewhere");
|
||||
// Open tabs have nothing to reattach.
|
||||
expect(screen.getAllByRole("button", { name: "Reattach" })).toHaveLength(2);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Reattach" })[0]);
|
||||
const tab = useAppState.getState().sessions.find((s) => s.id === "det-2");
|
||||
expect(tab).toMatchObject({ sessionType: "bash", persistent: true, pendingReattach: true, projectId: "p1" });
|
||||
expect(useAppState.getState().activeSessionId).toBe("det-2");
|
||||
});
|
||||
|
||||
it("ends a detached terminal in the workspace and an open one by closing its tab, after a confirm", async () => {
|
||||
listWorkspaceTerminals.mockResolvedValue([t("open-1"), t("det-2")]);
|
||||
useAppState.getState().addSession({ id: "open-1", projectId: "p1", projectName: "api", sessionType: "claude", sessionName: null, persistent: true });
|
||||
render(<SessionsTab project={aws} actions={actions} />);
|
||||
await screen.findByRole("list", { name: "Terminals in the workspace" });
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "End" })[1]);
|
||||
expect(endWorkspaceTerminal).not.toHaveBeenCalled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Confirm end" }));
|
||||
await waitFor(() => expect(endWorkspaceTerminal).toHaveBeenCalledWith("p1", "det-2"));
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "End" })[0]);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Confirm end" }));
|
||||
await waitFor(() => expect(closeTerminalSession).toHaveBeenCalledWith("open-1"));
|
||||
expect(useAppState.getState().sessions.find((s) => s.id === "open-1")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("is not shown for a Docker project", async () => {
|
||||
const docker = { ...aws, host_id: null } as unknown as Project;
|
||||
render(<SessionsTab project={docker} actions={actions} />);
|
||||
await screen.findByText(/No sessions recorded yet/);
|
||||
expect(screen.queryByText("Terminals in the workspace")).toBeNull();
|
||||
expect(listWorkspaceTerminals).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,18 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import type { ClaudeSession, Project } from "../../../lib/types";
|
||||
import { listClaudeSessions } from "../../../lib/tauri-commands";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import type { ClaudeSession, Project, WorkspaceTerminal } from "../../../lib/types";
|
||||
import {
|
||||
endWorkspaceTerminal,
|
||||
listClaudeSessions,
|
||||
listWorkspaceTerminals,
|
||||
} from "../../../lib/tauri-commands";
|
||||
import type { useProjectActions } from "../../../hooks/useProjectActions";
|
||||
import { useTerminal } from "../../../hooks/useTerminal";
|
||||
import { useAppState } from "../../../store/appState";
|
||||
import { isAwsProject } from "../../../lib/awsWorkspace";
|
||||
import { reattachTab, terminalLabel } from "../../../lib/persistentTerminals";
|
||||
import Button from "../../ui/Button";
|
||||
import { formatAge, formatBytes } from "./format";
|
||||
import { formatAge, formatBytes, formatElapsed } from "./format";
|
||||
|
||||
interface Props {
|
||||
project: Project;
|
||||
@@ -38,6 +47,7 @@ export default function SessionsTab({ project, actions }: Props) {
|
||||
|
||||
return (
|
||||
<div className="p-4 max-w-4xl">
|
||||
{isAwsProject(project) && running && <WorkspaceTerminals project={project} />}
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<p className="text-xs text-[var(--text-secondary)]">
|
||||
Conversations stored on this project’s config volume. Resume opens a
|
||||
@@ -88,3 +98,117 @@ export default function SessionsTab({ project, actions }: Props) {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS: the terminals running in the workspace — the ones open here, the ones
|
||||
* this app left detached, and any another machine or the web terminal
|
||||
* opened. Reattach opens one in a tab; End stops it (and closes its tab).
|
||||
*/
|
||||
function WorkspaceTerminals({ project }: { project: Project }) {
|
||||
const [terminals, setTerminals] = useState<WorkspaceTerminal[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [confirmEnd, setConfirmEnd] = useState<string | null>(null);
|
||||
const openIds = useAppState(useShallow((s) => s.sessions.map((x) => x.id)));
|
||||
const { close } = useTerminal();
|
||||
const pushToast = useAppState((s) => s.pushToast);
|
||||
|
||||
const load = useCallback(() => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
listWorkspaceTerminals(project.id)
|
||||
.then(setTerminals)
|
||||
.catch((e) => {
|
||||
setTerminals([]);
|
||||
setError(String(e));
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
}, [project.id]);
|
||||
|
||||
useEffect(load, [load]);
|
||||
|
||||
const isOpen = (id: string) => openIds.includes(id);
|
||||
|
||||
const reattach = (t: WorkspaceTerminal) => {
|
||||
useAppState.getState().addSession(reattachTab(project.id, project.name, t));
|
||||
};
|
||||
|
||||
const end = async (t: WorkspaceTerminal) => {
|
||||
setConfirmEnd(null);
|
||||
try {
|
||||
if (isOpen(t.session_id)) await close(t.session_id);
|
||||
else await endWorkspaceTerminal(project.id, t.session_id);
|
||||
} catch (e) {
|
||||
pushToast({ kind: "error", message: "Could not end that terminal", detail: String(e) });
|
||||
}
|
||||
load();
|
||||
};
|
||||
|
||||
return (
|
||||
<section className="mb-6" aria-labelledby="workspace-terminals-heading">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<div>
|
||||
<h3 id="workspace-terminals-heading" className="text-[13px] font-medium text-[var(--text-primary)]">
|
||||
Terminals in the workspace
|
||||
</h3>
|
||||
<p className="text-xs text-[var(--text-secondary)]">
|
||||
Terminals keep running in the workspace when Triple-C closes or loses its
|
||||
connection. Closing a tab ends its terminal. A Claude session left running keeps
|
||||
the workspace up until it ends, you end it here, you Stop, or the lifetime cap.
|
||||
</p>
|
||||
</div>
|
||||
<Button onClick={load} disabled={loading}>
|
||||
{loading ? "Refreshing…" : "Refresh"}
|
||||
</Button>
|
||||
</div>
|
||||
{error ? (
|
||||
<p className="text-[13px] text-[var(--text-secondary)]">{error}</p>
|
||||
) : terminals.length === 0 && !loading ? (
|
||||
<p className="text-[13px] text-[var(--text-secondary)]">No terminals are running in the workspace.</p>
|
||||
) : (
|
||||
<ul className="space-y-1" aria-label="Terminals in the workspace">
|
||||
{terminals.map((t) => {
|
||||
const open = isOpen(t.session_id);
|
||||
const state = open ? "Open here" : t.attached ? "Attached elsewhere" : "Detached";
|
||||
// The cost of a Claude that outlives the app: it keeps the
|
||||
// workspace up (and billed) until it ends or is ended.
|
||||
const keepsUp = !open && !t.attached && t.kind === "claude";
|
||||
return (
|
||||
<li
|
||||
key={t.session_id}
|
||||
className="flex items-center gap-3 px-3 py-2 bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-control)]"
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[13px] text-[var(--text-primary)] truncate">{terminalLabel(t)}</div>
|
||||
<div className="text-xs text-[var(--text-secondary)] truncate">
|
||||
{t.kind === "claude" ? "Claude Code" : "Shell"} · {state} · started{" "}
|
||||
{formatElapsed(Date.now() - t.created_unix * 1000)}
|
||||
{t.known ? "" : " · opened elsewhere"}
|
||||
</div>
|
||||
{keepsUp && (
|
||||
<div className="text-xs text-[var(--warning)]">
|
||||
Keeps the workspace running while it runs — End it if you no longer need it.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{!open && (
|
||||
<Button variant="primary" onClick={() => reattach(t)}>
|
||||
Reattach
|
||||
</Button>
|
||||
)}
|
||||
{confirmEnd === t.session_id ? (
|
||||
<>
|
||||
<Button variant="danger" onClick={() => void end(t)}>Confirm end</Button>
|
||||
<Button onClick={() => setConfirmEnd(null)}>Cancel</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button onClick={() => setConfirmEnd(t.session_id)}>End</Button>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -9,11 +9,16 @@ import { awsProject, localProject } from "../../../../test/awsFixtures";
|
||||
import type { AppSettings, AuthBridgeStatus, Project } from "../../../../lib/types";
|
||||
import { useAppState } from "../../../../store/appState";
|
||||
import { clearEnvironmentMaxHoursCache } from "../../../../lib/environmentMaxHours";
|
||||
import { clearEc2CapacityCache } from "../../../../lib/ec2Capacity";
|
||||
import type { Ec2Capacity } from "../../../../lib/types";
|
||||
|
||||
const OFF_BRIDGE: AuthBridgeStatus = { enabled: false, active_ports: [], conflicts: [] };
|
||||
const removeAwsResources = vi.fn();
|
||||
const awsCostEstimate = vi.fn(async (..._args: unknown[]) => null as unknown);
|
||||
const awsEnvironmentMaxHours = vi.fn(async (_id: string) => 0 as number | null);
|
||||
const NO_EC2: Ec2Capacity = { state: "none" };
|
||||
const awsEc2Capacity = vi.fn(async (_id: string): Promise<Ec2Capacity> => NO_EC2);
|
||||
const ec2FitProblem = vi.fn(async (..._args: unknown[]) => null as string | null);
|
||||
|
||||
vi.mock("../../../../lib/tauri-commands", () => ({
|
||||
getAuthBridgeStatus: vi.fn(async () => OFF_BRIDGE),
|
||||
@@ -22,6 +27,8 @@ vi.mock("../../../../lib/tauri-commands", () => ({
|
||||
removeAwsResources: (id: string, discard: boolean) => removeAwsResources(id, discard),
|
||||
awsCostEstimate: (...args: unknown[]) => awsCostEstimate(...args),
|
||||
awsEnvironmentMaxHours: (id: string) => awsEnvironmentMaxHours(id),
|
||||
awsEc2Capacity: (id: string) => awsEc2Capacity(id),
|
||||
ec2FitProblem: (...args: unknown[]) => ec2FitProblem(...args),
|
||||
}));
|
||||
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => {}) }));
|
||||
vi.mock("@tauri-apps/plugin-dialog", () => ({ open: vi.fn(async () => null) }));
|
||||
@@ -31,6 +38,9 @@ const save = vi.fn().mockResolvedValue(true);
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
clearEnvironmentMaxHoursCache();
|
||||
clearEc2CapacityCache();
|
||||
awsEc2Capacity.mockImplementation(async () => NO_EC2);
|
||||
ec2FitProblem.mockImplementation(async () => null);
|
||||
});
|
||||
|
||||
function toggle(name: string) {
|
||||
@@ -244,7 +254,7 @@ describe("HostSection", () => {
|
||||
await waitFor(() =>
|
||||
expect(within(card).getByText(/≈ \$0\.050\/hour \(on-demand; Spot is usually up to 70% cheaper\)/)).toBeInTheDocument(),
|
||||
);
|
||||
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "eu-west-1", p.aws);
|
||||
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "eu-west-1", p.aws, null);
|
||||
expect(within(card).getByText(/EFS storage is billed separately/)).toBeInTheDocument();
|
||||
useAppState.setState({ appSettings: null });
|
||||
});
|
||||
@@ -595,3 +605,272 @@ describe("HostSection", () => {
|
||||
expect(screen.queryByLabelText("Repository 1 URL")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ---- phase 7: EC2 projects ----
|
||||
|
||||
const M7I: Ec2Capacity = {
|
||||
state: "ec2",
|
||||
compute: {
|
||||
instance_type: "m7i.large",
|
||||
max_instances: 1,
|
||||
user_tag: "alice",
|
||||
cluster_arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/triple-c-dev--u-alice",
|
||||
capacity_provider: "triple-c-dev--u-alice",
|
||||
},
|
||||
available_cpu: 1920,
|
||||
available_memory_mib: 7373,
|
||||
};
|
||||
|
||||
const ec2Project = (over: Partial<Project> = {}): Project => {
|
||||
const p = awsProject(over);
|
||||
return { ...p, aws: { ...p.aws!, launch_type: "ec2", cpu: 1024, memory_mib: 4096 } };
|
||||
};
|
||||
|
||||
describe("HostSection — launch type (EC2)", () => {
|
||||
const renderHost = (p: Project = awsProject(), disabled = false) =>
|
||||
render(<HostSection project={p} save={save} disabled={disabled} />);
|
||||
const ec2Segment = () => screen.getByRole("radio", { name: /^EC2/ });
|
||||
|
||||
it("offers EC2 with the user's instance type when their onboarding has EC2 capacity", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
renderHost();
|
||||
await waitFor(() => expect(ec2Segment()).toHaveTextContent("EC2 (your instance: m7i.large)"));
|
||||
expect(ec2Segment()).toBeEnabled();
|
||||
expect(screen.queryByText(/Ask an admin/)).toBeNull();
|
||||
});
|
||||
|
||||
it("disables EC2 with the reason when the user has no EC2 capacity, or it can't be read", async () => {
|
||||
renderHost();
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText(/Ask an admin to add EC2 capacity to your onboarding/)).toBeInTheDocument(),
|
||||
);
|
||||
expect(ec2Segment()).toBeDisabled();
|
||||
expect(screen.getByRole("radio", { name: "Fargate" })).toBeEnabled();
|
||||
cleanup();
|
||||
|
||||
clearEc2CapacityCache();
|
||||
awsEc2Capacity.mockImplementation(async () => ({ state: "unusable", reason: "schema is not 1" }));
|
||||
renderHost();
|
||||
await waitFor(() => expect(screen.getByText(/can't be used \(schema is not 1\)/)).toBeInTheDocument());
|
||||
expect(ec2Segment()).toBeDisabled();
|
||||
cleanup();
|
||||
|
||||
clearEc2CapacityCache();
|
||||
awsEc2Capacity.mockImplementation(async () => ({ state: "unknown" }));
|
||||
renderHost();
|
||||
await waitFor(() => expect(screen.getByText(/Could not read your EC2 capacity/)).toBeInTheDocument());
|
||||
expect(ec2Segment()).toBeDisabled();
|
||||
});
|
||||
|
||||
it("switches to EC2 through launchTypePatch: sidecar and Spot off, sizes fitted", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
const p = awsProject();
|
||||
p.aws = { ...p.aws!, cpu: 4096, memory_mib: 16384, spot: true, gateway_sidecar: true };
|
||||
renderHost(p);
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
fireEvent.click(ec2Segment());
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
||||
expect(save.mock.calls[0][0].aws).toMatchObject({
|
||||
launch_type: "ec2",
|
||||
gateway_sidecar: false,
|
||||
spot: false,
|
||||
cpu: 1920,
|
||||
memory_mib: 7373,
|
||||
});
|
||||
expect(save.mock.calls[0][0].allow_docker_access).toBeUndefined();
|
||||
});
|
||||
|
||||
it("switches back to Fargate turning Docker access and VPN off, with a Fargate size", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
const p = ec2Project({ allow_docker_access: true, vpn_support_enabled: true });
|
||||
p.aws = { ...p.aws!, cpu: 1920, memory_mib: 7373 };
|
||||
renderHost(p);
|
||||
fireEvent.click(screen.getByRole("radio", { name: "Fargate" }));
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
||||
const patch = save.mock.calls[0][0];
|
||||
expect(patch).toMatchObject({ allow_docker_access: false, vpn_support_enabled: false });
|
||||
expect(patch.aws).toMatchObject({ launch_type: "fargate", cpu: 1024, memory_mib: 7168 });
|
||||
});
|
||||
|
||||
it("an EC2 project keeps its launch type selectable back to Fargate even without capacity, and says why it can't start", async () => {
|
||||
renderHost(ec2Project());
|
||||
await waitFor(() => expect(screen.getByText(/can't start on EC2 now/)).toBeInTheDocument());
|
||||
expect(ec2Segment()).toHaveAttribute("aria-checked", "true");
|
||||
expect(screen.getByRole("radio", { name: "Fargate" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("on EC2: reservation inputs with the instance's limits, no ephemeral size, Spot or architecture, and the notes", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
renderHost(ec2Project());
|
||||
await waitFor(() => expect(screen.getByText(/Up to 1920 on your m7i.large/)).toBeInTheDocument());
|
||||
expect(screen.getByText(/Up to 7373 MiB on your m7i.large/)).toBeInTheDocument();
|
||||
expect(screen.getByLabelText("CPU reservation (units)")).toHaveValue("1024");
|
||||
expect(screen.getByLabelText("Memory reservation (MiB)")).toHaveValue("4096");
|
||||
expect(screen.queryByLabelText("Ephemeral storage (GiB)")).toBeNull();
|
||||
expect(screen.queryByRole("switch", { name: "Spot" })).toBeNull();
|
||||
expect(screen.queryByRole("radiogroup", { name: "Architecture" })).toBeNull();
|
||||
const notes = screen.getByRole("note", { name: "On EC2" });
|
||||
expect(notes).toHaveTextContent(/first start after idle boots an instance — about 3 minutes/);
|
||||
expect(notes).toHaveTextContent(/root on your own instance/);
|
||||
expect(notes).toHaveTextContent(/gateway sidecar is not available on EC2/);
|
||||
// Fargate shows none of this.
|
||||
cleanup();
|
||||
renderHost(awsProject());
|
||||
expect(screen.queryByRole("note", { name: "On EC2" })).toBeNull();
|
||||
expect(screen.getByLabelText("Ephemeral storage (GiB)")).toBeInTheDocument();
|
||||
expect(screen.getByRole("switch", { name: "Spot" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("checks the reservations with the backend's fit rule before saving", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
ec2FitProblem.mockImplementation(async (config) =>
|
||||
(config as { memory_mib: number }).memory_mib === 8000
|
||||
? "This project reserves 1024 CPU units and 8000 MiB of memory, more than …"
|
||||
: null,
|
||||
);
|
||||
renderHost(ec2Project());
|
||||
await waitFor(() => expect(screen.getByText(/Up to 1920/)).toBeInTheDocument());
|
||||
// The load-time check found the stored 4096 MiB fits: no alert.
|
||||
await waitFor(() => expect(ec2FitProblem).toHaveBeenCalledTimes(1));
|
||||
expect(screen.queryByRole("alert")).toBeNull();
|
||||
const mem = screen.getByLabelText("Memory reservation (MiB)");
|
||||
fireEvent.change(mem, { target: { value: "8000" } });
|
||||
fireEvent.blur(mem);
|
||||
await waitFor(() => expect(screen.getByRole("alert")).toHaveTextContent(/more than/));
|
||||
expect(ec2FitProblem).toHaveBeenLastCalledWith(expect.objectContaining({ cpu: 1024, memory_mib: 8000 }), "m7i.large");
|
||||
expect(save).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.change(mem, { target: { value: "6000" } });
|
||||
fireEvent.blur(mem);
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
||||
expect(save.mock.calls[0][0].aws).toMatchObject({ cpu: 1024, memory_mib: 6000 });
|
||||
|
||||
const cpu = screen.getByLabelText("CPU reservation (units)");
|
||||
fireEvent.change(cpu, { target: { value: "0" } });
|
||||
fireEvent.blur(cpu);
|
||||
await waitFor(() => expect(screen.getByRole("alert")).toHaveTextContent(/positive whole numbers/));
|
||||
});
|
||||
|
||||
it("warns on load when the saved reservations no longer fit the instance type, once", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
ec2FitProblem.mockImplementation(async () => "This project reserves 4096 CPU units …, more than an m7i.large");
|
||||
const p = ec2Project();
|
||||
p.aws = { ...p.aws!, cpu: 4096 };
|
||||
const { rerender } = renderHost(p);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole("alert")).toHaveTextContent(/saved reservations no longer fit: .*more than an m7i.large/),
|
||||
);
|
||||
expect(ec2FitProblem).toHaveBeenCalledWith(expect.objectContaining({ cpu: 4096 }), "m7i.large");
|
||||
rerender(<HostSection project={{ ...p }} save={save} disabled={false} />);
|
||||
await act(async () => {});
|
||||
expect(ec2FitProblem).toHaveBeenCalledTimes(1);
|
||||
expect(save).not.toHaveBeenCalled();
|
||||
cleanup();
|
||||
|
||||
// Fargate projects, and EC2 without a known instance type, aren't checked.
|
||||
ec2FitProblem.mockClear();
|
||||
renderHost(awsProject());
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
cleanup();
|
||||
clearEc2CapacityCache();
|
||||
awsEc2Capacity.mockImplementation(async () => NO_EC2);
|
||||
renderHost(ec2Project());
|
||||
await waitFor(() => expect(screen.getByText(/can't start on EC2 now/)).toBeInTheDocument());
|
||||
expect(ec2FitProblem).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("says what a launch-type switch changed besides the type", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
const p = awsProject();
|
||||
p.aws = { ...p.aws!, cpu: 4096, memory_mib: 16384, spot: true, gateway_sidecar: true };
|
||||
renderHost(p);
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
fireEvent.click(ec2Segment());
|
||||
expect(await screen.findByRole("status")).toHaveTextContent(
|
||||
"Switched to EC2: CPU 4096 → 1920 units, memory 16384 → 7373 MiB, Spot turned off, LLM gateway sidecar turned off.",
|
||||
);
|
||||
cleanup();
|
||||
|
||||
const back = ec2Project({ allow_docker_access: true, vpn_support_enabled: true });
|
||||
back.aws = { ...back.aws!, cpu: 1920, memory_mib: 7373 };
|
||||
renderHost(back);
|
||||
fireEvent.click(screen.getByRole("radio", { name: "Fargate" }));
|
||||
expect(await screen.findByRole("status")).toHaveTextContent(
|
||||
"Switched to Fargate: CPU 1920 → 1024 units, memory 7373 → 7168 MiB, Docker access turned off, VPN support turned off.",
|
||||
);
|
||||
});
|
||||
|
||||
const switchedProject = () => {
|
||||
const p = awsProject();
|
||||
p.aws = { ...p.aws!, cpu: 4096, memory_mib: 16384, spot: true, gateway_sidecar: true };
|
||||
return p;
|
||||
};
|
||||
|
||||
it("shows no switch note when the save was refused", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
renderHost(switchedProject());
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
save.mockResolvedValueOnce(false);
|
||||
fireEvent.click(ec2Segment());
|
||||
await waitFor(() => expect(save).toHaveBeenCalled());
|
||||
await new Promise((r) => setTimeout(r, 0));
|
||||
expect(screen.queryByText(/Switched to/)).toBeNull();
|
||||
});
|
||||
|
||||
it("shows no switch note when the save fails", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
renderHost(switchedProject());
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
const unhandled = vi.fn();
|
||||
process.on("unhandledRejection", unhandled);
|
||||
save.mockRejectedValueOnce(new Error("boom"));
|
||||
fireEvent.click(ec2Segment());
|
||||
await waitFor(() => expect(save).toHaveBeenCalled());
|
||||
await new Promise((r) => setTimeout(r, 0));
|
||||
process.off("unhandledRejection", unhandled);
|
||||
expect(screen.queryByText(/Switched to/)).toBeNull();
|
||||
});
|
||||
|
||||
it("drops the switch note when another setting is saved afterwards", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
renderHost(switchedProject());
|
||||
await waitFor(() => expect(ec2Segment()).toBeEnabled());
|
||||
fireEvent.click(ec2Segment());
|
||||
expect(await screen.findByText(/Switched to EC2/)).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("radio", { name: "Fargate" }));
|
||||
await waitFor(() => expect(screen.queryByText(/Switched to EC2/)).toBeNull());
|
||||
});
|
||||
|
||||
it("prices an EC2 project as the user's instance type", async () => {
|
||||
awsEc2Capacity.mockImplementation(async () => M7I);
|
||||
useAppState.setState({
|
||||
appSettings: { hosts: [{ id: "c1", name: "lab" }] } as unknown as AppSettings,
|
||||
});
|
||||
const p = ec2Project();
|
||||
renderHost(p);
|
||||
await waitFor(() => expect(awsCostEstimate).toHaveBeenCalledWith("c1", "eu-west-1", p.aws, "m7i.large"));
|
||||
expect(screen.getByText(/price in this region of your EC2 instance \(m7i.large\)/)).toBeInTheDocument();
|
||||
useAppState.setState({ appSettings: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe("RuntimeSection on EC2", () => {
|
||||
it("enables Docker access and VPN, keeps the sandbox off, and says Docker access is root on the instance", () => {
|
||||
render(<RuntimeSection project={ec2Project({ sandbox_mode_enabled: true })} save={save} disabled={false} />);
|
||||
for (const name of ["Allow container spawning", "VPN support"]) {
|
||||
expect(toggle(name)).toBeEnabled();
|
||||
}
|
||||
// E8, measured live: bubblewrap can't run in an ECS task on EC2 either.
|
||||
expect(toggle("Sandbox mode")).toBeDisabled();
|
||||
expect(toggle("Sandbox mode")).toHaveAttribute("aria-checked", "false");
|
||||
expect(screen.getByText(/root on your own instance/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Off on AWS for now/)).toBeInTheDocument();
|
||||
// The VPN hint speaks of an ECS task on the user's instance, not of
|
||||
// recreating a container or its volumes.
|
||||
const vpn = screen.getByText(/VPN client \(PIA, WireGuard, OpenVPN\)/);
|
||||
expect(vpn).toHaveTextContent(/an ECS task on your own EC2 instance/);
|
||||
expect(vpn).toHaveTextContent(/next start/);
|
||||
expect(vpn).not.toHaveTextContent(/recreates the container|volumes/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { AwsWorkspaceConfig, HostConnection, Project, RepoSource } from "../../../../lib/types";
|
||||
import type {
|
||||
AwsWorkspaceConfig,
|
||||
Ec2Capacity,
|
||||
HostConnection,
|
||||
LaunchType,
|
||||
Project,
|
||||
RepoSource,
|
||||
} from "../../../../lib/types";
|
||||
import * as commands from "../../../../lib/tauri-commands";
|
||||
import { environmentMaxHours } from "../../../../lib/environmentMaxHours";
|
||||
import { useEc2Capacity } from "../../../../lib/ec2Capacity";
|
||||
import { useAppState } from "../../../../store/appState";
|
||||
import {
|
||||
COPY_AT_CONNECT_GATEWAY_NOTE,
|
||||
@@ -16,6 +24,14 @@ import {
|
||||
setupScriptProblem,
|
||||
EFS_HINT,
|
||||
EPHEMERAL_WARNING,
|
||||
EC2_DOCKER_ACCESS_NOTE,
|
||||
EC2_NO_CAPACITY,
|
||||
EC2_NO_SIDECAR,
|
||||
EC2_SCALE_TO_ZERO_NOTE,
|
||||
PERSISTENT_TERMINALS_NOTE,
|
||||
launchTypeChanges,
|
||||
launchTypePatch,
|
||||
type Ec2Limits,
|
||||
FARGATE_CPUS,
|
||||
defaultAwsWorkspace,
|
||||
formatCpu,
|
||||
@@ -43,6 +59,21 @@ interface Props {
|
||||
const DEFAULT_IDLE_MINUTES = 60;
|
||||
const STILL_RUNNING = "The task is still running — stop it first.";
|
||||
|
||||
/** Why EC2 can't be chosen, from what the user's record says (null: it can). */
|
||||
function ec2Unavailable(capacity: Ec2Capacity | null): string | null {
|
||||
if (capacity === null) return "Checking your EC2 capacity in this environment…";
|
||||
switch (capacity.state) {
|
||||
case "ec2":
|
||||
return null;
|
||||
case "none":
|
||||
return `You have no EC2 capacity in this environment. ${EC2_NO_CAPACITY}.`;
|
||||
case "unusable":
|
||||
return `Your EC2 capacity record can't be used (${capacity.reason}). ${EC2_NO_CAPACITY}.`;
|
||||
case "unknown":
|
||||
return "Could not read your EC2 capacity in this environment; EC2 is offered once it can be read.";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Where an AWS project runs and what its Fargate task looks like. Only shown
|
||||
* for AWS projects; a local project's host is chosen when it is added.
|
||||
@@ -55,6 +86,7 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
const hosts = useAppState((s) => s.appSettings?.hosts ?? NO_HOSTS);
|
||||
const connection = hosts.find((h) => h.id === project.host_id);
|
||||
|
||||
const [cpuText, setCpuText] = useState(String(aws.cpu));
|
||||
const [memory, setMemory] = useState(String(aws.memory_mib));
|
||||
const [memoryProblem, setMemoryProblem] = useState<string | null>(null);
|
||||
const [ephemeral, setEphemeral] = useState(String(aws.ephemeral_gib));
|
||||
@@ -66,12 +98,47 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
const [setupProblem, setSetupProblem] = useState<string | null>(null);
|
||||
// The environment's advisory lifetime cap (Q8), from its record.
|
||||
const [maxHours, setMaxHours] = useState<number | null>(null);
|
||||
// What the last launch-type switch changed besides the type (M6).
|
||||
const [switchNote, setSwitchNote] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setCpuText(String(aws.cpu));
|
||||
setMemory(String(aws.memory_mib));
|
||||
setEphemeral(String(aws.ephemeral_gib));
|
||||
setIdle(String(aws.idle_stop_minutes ?? DEFAULT_IDLE_MINUTES));
|
||||
}, [aws.memory_mib, aws.ephemeral_gib, aws.idle_stop_minutes]);
|
||||
}, [aws.cpu, aws.memory_mib, aws.ephemeral_gib, aws.idle_stop_minutes]);
|
||||
|
||||
// Phase 7: the user's own EC2 capacity in this environment (their record),
|
||||
// which decides whether EC2 can be chosen and what it can reserve.
|
||||
const capacity = useEc2Capacity(project);
|
||||
const ec2 = aws.launch_type === "ec2";
|
||||
const compute = capacity?.state === "ec2" ? capacity.compute : null;
|
||||
const limits: Ec2Limits | null =
|
||||
capacity?.state === "ec2" && capacity.available_cpu !== null && capacity.available_memory_mib !== null
|
||||
? { cpu: capacity.available_cpu, memory_mib: capacity.available_memory_mib }
|
||||
: null;
|
||||
const ec2Reason = ec2Unavailable(capacity);
|
||||
|
||||
// Once the instance type is known, check the stored reservations against
|
||||
// it (Rust's fit rule): an instance type changed at onboarding, or a
|
||||
// record that came back after a save, can leave them too big.
|
||||
const checkedFit = useRef<string | null>(null);
|
||||
useEffect(() => {
|
||||
if (!ec2 || !compute) return;
|
||||
const key = `${project.id}:${compute.instance_type}`;
|
||||
if (checkedFit.current === key) return;
|
||||
checkedFit.current = key;
|
||||
let cancelled = false;
|
||||
commands
|
||||
.ec2FitProblem(latest.current.value, compute.instance_type)
|
||||
.then((problem) => {
|
||||
if (!cancelled && problem) setMemoryProblem(`The saved reservations no longer fit: ${problem}`);
|
||||
})
|
||||
.catch(() => {}); // The start checks again.
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [ec2, compute, project.id]);
|
||||
|
||||
useEffect(() => {
|
||||
setSetupScript(aws.setup_script ?? "");
|
||||
@@ -104,7 +171,11 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
if (latest.current.base !== project.aws && latest.current.inFlight === 0) {
|
||||
latest.current = { base: project.aws, value: aws, inFlight: 0 };
|
||||
}
|
||||
const saveAws = async (patch: Partial<AwsWorkspaceConfig>) => {
|
||||
// Bumped by every save: a launch-type note only shows if no later save began.
|
||||
const saveSeq = useRef(0);
|
||||
const saveAws = async (patch: Partial<AwsWorkspaceConfig>, also: Partial<Project> = {}) => {
|
||||
saveSeq.current += 1;
|
||||
setSwitchNote(null);
|
||||
const l = latest.current;
|
||||
const next = { ...l.value, ...patch };
|
||||
l.value = next;
|
||||
@@ -114,7 +185,7 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
if (l.value === next) l.value = l.base ?? defaultAwsWorkspace();
|
||||
};
|
||||
try {
|
||||
const ok = await save({ aws: next });
|
||||
const ok = await save({ ...also, aws: next });
|
||||
if (!ok) refused();
|
||||
return ok;
|
||||
} catch (e) {
|
||||
@@ -141,6 +212,50 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
// Not `disabled` (which also allows Error): on AWS, Error can mean a live task.
|
||||
const stopped = project.status === "stopped";
|
||||
|
||||
// The launch-type switch: `launchTypePatch` keeps each side to what Rust
|
||||
// accepts there (Docker/VPN, the sidecar, Spot, the sizes).
|
||||
const switchLaunchType = (launch_type: LaunchType) => {
|
||||
if (launch_type === latest.current.value.launch_type) return;
|
||||
setMemoryProblem(null);
|
||||
const before = { ...project, aws: latest.current.value };
|
||||
const patch = launchTypePatch(before, launch_type, limits);
|
||||
const note = launchTypeChanges(before, patch);
|
||||
const { aws: next, ...also } = patch;
|
||||
// Shown only once the save went through, and dropped by any later save.
|
||||
const pending = saveAws(next!, also);
|
||||
const seq = saveSeq.current;
|
||||
void pending.then((ok) => {
|
||||
if (ok && saveSeq.current === seq) setSwitchNote(note);
|
||||
});
|
||||
};
|
||||
|
||||
// EC2 reservations: positive whole numbers that fit the user's instance
|
||||
// type — checked by Rust's own fit rule (`ec2_fit_problem`), so this never
|
||||
// disagrees with the start.
|
||||
const commitEc2Sizing = async () => {
|
||||
const cpu = Number(cpuText);
|
||||
const memory_mib = Number(memory);
|
||||
const whole = (n: number) => Number.isInteger(n) && n > 0;
|
||||
if (!whole(cpu) || !whole(memory_mib)) {
|
||||
setMemoryProblem("The CPU and memory reservations must be positive whole numbers");
|
||||
return;
|
||||
}
|
||||
if (compute) {
|
||||
let problem: string | null = null;
|
||||
try {
|
||||
problem = await commands.ec2FitProblem({ ...aws, cpu, memory_mib }, compute.instance_type);
|
||||
} catch {
|
||||
problem = null; // The start checks again.
|
||||
}
|
||||
if (problem) {
|
||||
setMemoryProblem(problem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setMemoryProblem(null);
|
||||
if (cpu !== aws.cpu || memory_mib !== aws.memory_mib) void saveAws({ cpu, memory_mib });
|
||||
};
|
||||
|
||||
const commitMemory = () => {
|
||||
const value = Number(memory);
|
||||
const problem = memoryError(aws.cpu, value);
|
||||
@@ -176,7 +291,11 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
return (
|
||||
<ConfigGroup
|
||||
title="Host"
|
||||
description="This project runs as an AWS Fargate task. Changes apply on its next start."
|
||||
description={
|
||||
ec2
|
||||
? "This project runs on your own EC2 instances, as an ECS task. Changes apply on its next start."
|
||||
: "This project runs as an AWS Fargate task. Changes apply on its next start."
|
||||
}
|
||||
>
|
||||
<dl className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 text-[13px]">
|
||||
<dt className="text-[var(--text-secondary)]">Connection</dt>
|
||||
@@ -199,80 +318,175 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
This environment stops workspaces after {maxHours} h (advisory).
|
||||
</p>
|
||||
)}
|
||||
<p className="text-xs text-[var(--text-secondary)]">{PERSISTENT_TERMINALS_NOTE}</p>
|
||||
|
||||
<div className="flex flex-wrap gap-4">
|
||||
<Field label="CPU">
|
||||
{(id) => (
|
||||
<select
|
||||
id={id}
|
||||
value={aws.cpu}
|
||||
disabled={disabled}
|
||||
className={selectClass}
|
||||
onChange={(e) => {
|
||||
const cpu = Number(e.target.value);
|
||||
const memory_mib = memoryError(cpu, aws.memory_mib)
|
||||
? nearestMemory(cpu, aws.memory_mib)
|
||||
: aws.memory_mib;
|
||||
setMemoryProblem(null);
|
||||
void saveAws({ cpu, memory_mib });
|
||||
}}
|
||||
>
|
||||
{FARGATE_CPUS.filter(
|
||||
// The gateway sidecar needs 1 vCPU / 2 GiB (Rust validates it too).
|
||||
(c) => !aws.gateway_sidecar || c >= GATEWAY_SIDECAR_MIN_CPU,
|
||||
).map((c) => (
|
||||
<option key={c} value={c}>
|
||||
{formatCpu(c)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</Field>
|
||||
<Field
|
||||
label="Memory (MiB)"
|
||||
hint={(() => {
|
||||
const opts = memoryOptions(aws.cpu);
|
||||
return `${formatMemory(opts[0])} to ${formatMemory(opts[opts.length - 1])} with ${formatCpu(aws.cpu)}.`;
|
||||
})()}
|
||||
>
|
||||
{(id) => (
|
||||
<>
|
||||
<div>
|
||||
<span className="block text-[13px] font-medium text-[var(--text-primary)] mb-1">
|
||||
Launch type
|
||||
</span>
|
||||
<SegmentedControl
|
||||
label="Launch type"
|
||||
value={aws.launch_type}
|
||||
disabled={disabled}
|
||||
onChange={switchLaunchType}
|
||||
segments={[
|
||||
{ value: "fargate", label: "Fargate" },
|
||||
{
|
||||
value: "ec2",
|
||||
label: compute ? `EC2 (your instance: ${compute.instance_type})` : "EC2",
|
||||
disabled: !compute && !ec2,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
{ec2Reason && (
|
||||
<p
|
||||
className={`mt-1 text-xs ${ec2 && capacity !== null && capacity.state !== "unknown" ? "text-[var(--warning)]" : "text-[var(--text-secondary)]"}`}
|
||||
>
|
||||
{ec2 && capacity !== null && capacity.state !== "unknown"
|
||||
? `This project can't start on EC2 now: ${ec2Reason} Or switch it to Fargate.`
|
||||
: ec2Reason}
|
||||
</p>
|
||||
)}
|
||||
{switchNote && (
|
||||
<p role="status" className="mt-1 text-xs text-[var(--text-secondary)]">
|
||||
{switchNote}
|
||||
</p>
|
||||
)}
|
||||
{ec2 && (
|
||||
<ul
|
||||
role="note"
|
||||
aria-label="On EC2"
|
||||
className="mt-2 px-2 py-1.5 text-xs text-[var(--text-secondary)] bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-control)] list-disc list-inside space-y-0.5"
|
||||
>
|
||||
<li>{EC2_SCALE_TO_ZERO_NOTE}</li>
|
||||
<li>{EC2_DOCKER_ACCESS_NOTE}</li>
|
||||
<li>{EC2_NO_SIDECAR}.</li>
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{ec2 ? (
|
||||
<div className="flex flex-wrap gap-4">
|
||||
<Field
|
||||
label="CPU reservation (units)"
|
||||
hint={
|
||||
limits && compute
|
||||
? `1024 units = 1 vCPU. Up to ${limits.cpu} on your ${compute.instance_type}.`
|
||||
: "1024 units = 1 vCPU. It must fit your instance type."
|
||||
}
|
||||
>
|
||||
{(id) => (
|
||||
<input
|
||||
id={id}
|
||||
inputMode="numeric"
|
||||
value={cpuText}
|
||||
disabled={disabled}
|
||||
aria-invalid={memoryProblem ? true : undefined}
|
||||
onChange={(e) => setCpuText(e.target.value)}
|
||||
onBlur={() => void commitEc2Sizing()}
|
||||
onKeyDown={(e) => e.key === "Enter" && void commitEc2Sizing()}
|
||||
className={`w-32 ${inputClass}`}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
<Field
|
||||
label="Memory reservation (MiB)"
|
||||
hint={
|
||||
limits && compute
|
||||
? `Up to ${limits.memory_mib} MiB on your ${compute.instance_type} (a tenth is kept for the instance).`
|
||||
: "It must fit your instance type."
|
||||
}
|
||||
>
|
||||
{(id) => (
|
||||
<input
|
||||
id={id}
|
||||
inputMode="numeric"
|
||||
value={memory}
|
||||
disabled={disabled}
|
||||
list={`${id}-options`}
|
||||
aria-invalid={memoryProblem ? true : undefined}
|
||||
onChange={(e) => setMemory(e.target.value)}
|
||||
onBlur={commitMemory}
|
||||
onKeyDown={(e) => e.key === "Enter" && commitMemory()}
|
||||
onBlur={() => void commitEc2Sizing()}
|
||||
onKeyDown={(e) => e.key === "Enter" && void commitEc2Sizing()}
|
||||
className={`w-32 ${inputClass}`}
|
||||
/>
|
||||
<datalist id={`${id}-options`}>
|
||||
{memoryOptions(aws.cpu).map((m) => (
|
||||
<option key={m} value={m} />
|
||||
))}
|
||||
</datalist>
|
||||
</>
|
||||
)}
|
||||
</Field>
|
||||
<div>
|
||||
<span className="block text-[13px] font-medium text-[var(--text-primary)] mb-1">
|
||||
Architecture
|
||||
</span>
|
||||
<SegmentedControl
|
||||
label="Architecture"
|
||||
value={aws.arch}
|
||||
disabled={disabled}
|
||||
onChange={(arch) => void saveAws({ arch })}
|
||||
segments={[
|
||||
{ value: "x86_64", label: "x86_64" },
|
||||
{ value: "arm64", label: "ARM64 (Graviton)" },
|
||||
]}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-wrap gap-4">
|
||||
<Field label="CPU">
|
||||
{(id) => (
|
||||
<select
|
||||
id={id}
|
||||
value={aws.cpu}
|
||||
disabled={disabled}
|
||||
className={selectClass}
|
||||
onChange={(e) => {
|
||||
const cpu = Number(e.target.value);
|
||||
const memory_mib = memoryError(cpu, aws.memory_mib)
|
||||
? nearestMemory(cpu, aws.memory_mib)
|
||||
: aws.memory_mib;
|
||||
setMemoryProblem(null);
|
||||
void saveAws({ cpu, memory_mib });
|
||||
}}
|
||||
>
|
||||
{FARGATE_CPUS.filter(
|
||||
// The gateway sidecar needs 1 vCPU / 2 GiB (Rust validates it too).
|
||||
(c) => !aws.gateway_sidecar || c >= GATEWAY_SIDECAR_MIN_CPU,
|
||||
).map((c) => (
|
||||
<option key={c} value={c}>
|
||||
{formatCpu(c)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</Field>
|
||||
<Field
|
||||
label="Memory (MiB)"
|
||||
hint={(() => {
|
||||
const opts = memoryOptions(aws.cpu);
|
||||
return `${formatMemory(opts[0])} to ${formatMemory(opts[opts.length - 1])} with ${formatCpu(aws.cpu)}.`;
|
||||
})()}
|
||||
>
|
||||
{(id) => (
|
||||
<>
|
||||
<input
|
||||
id={id}
|
||||
inputMode="numeric"
|
||||
value={memory}
|
||||
disabled={disabled}
|
||||
list={`${id}-options`}
|
||||
aria-invalid={memoryProblem ? true : undefined}
|
||||
onChange={(e) => setMemory(e.target.value)}
|
||||
onBlur={commitMemory}
|
||||
onKeyDown={(e) => e.key === "Enter" && commitMemory()}
|
||||
className={`w-32 ${inputClass}`}
|
||||
/>
|
||||
<datalist id={`${id}-options`}>
|
||||
{memoryOptions(aws.cpu).map((m) => (
|
||||
<option key={m} value={m} />
|
||||
))}
|
||||
</datalist>
|
||||
</>
|
||||
)}
|
||||
</Field>
|
||||
<div>
|
||||
<span className="block text-[13px] font-medium text-[var(--text-primary)] mb-1">
|
||||
Architecture
|
||||
</span>
|
||||
<SegmentedControl
|
||||
label="Architecture"
|
||||
value={aws.arch}
|
||||
disabled={disabled}
|
||||
onChange={(arch) => void saveAws({ arch })}
|
||||
segments={[
|
||||
{ value: "x86_64", label: "x86_64" },
|
||||
{ value: "arm64", label: "ARM64 (Graviton)" },
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{memoryProblem && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
{memoryProblem}
|
||||
@@ -328,60 +542,66 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Field
|
||||
label="Ephemeral storage (GiB)"
|
||||
hint={
|
||||
aws.storage === "efs"
|
||||
? "The task's local disk: 21 to 200 GiB. With EFS storage, /workspace is on EFS instead."
|
||||
: "The task's local disk: 21 to 200 GiB. Repositories and builds live here."
|
||||
}
|
||||
>
|
||||
{(id) => (
|
||||
<input
|
||||
id={id}
|
||||
inputMode="numeric"
|
||||
value={ephemeral}
|
||||
disabled={disabled}
|
||||
aria-invalid={ephemeralProblem ? true : undefined}
|
||||
onChange={(e) => setEphemeral(e.target.value)}
|
||||
onBlur={commitEphemeral}
|
||||
className={`w-24 ${inputClass}`}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
{ephemeralProblem && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
{ephemeralProblem}
|
||||
</p>
|
||||
)}
|
||||
{!ec2 && (
|
||||
<>
|
||||
<Field
|
||||
label="Ephemeral storage (GiB)"
|
||||
hint={
|
||||
aws.storage === "efs"
|
||||
? "The task's local disk: 21 to 200 GiB. With EFS storage, /workspace is on EFS instead."
|
||||
: "The task's local disk: 21 to 200 GiB. Repositories and builds live here."
|
||||
}
|
||||
>
|
||||
{(id) => (
|
||||
<input
|
||||
id={id}
|
||||
inputMode="numeric"
|
||||
value={ephemeral}
|
||||
disabled={disabled}
|
||||
aria-invalid={ephemeralProblem ? true : undefined}
|
||||
onChange={(e) => setEphemeral(e.target.value)}
|
||||
onBlur={commitEphemeral}
|
||||
className={`w-24 ${inputClass}`}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
{ephemeralProblem && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
{ephemeralProblem}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<SwitchRow
|
||||
label="Spot"
|
||||
hint={
|
||||
aws.wip_push_on_stop
|
||||
? "Runs on spare capacity at up to 70% off. AWS may reclaim it with two minutes' notice; the work-in-progress push below still runs."
|
||||
: "Runs on spare capacity at up to 70% off. AWS may reclaim it with two minutes' notice, and with the work-in-progress push below off, unpushed work is lost."
|
||||
}
|
||||
control={
|
||||
<Toggle
|
||||
<SwitchRow
|
||||
label="Spot"
|
||||
checked={aws.spot}
|
||||
disabled={disabled}
|
||||
onChange={(spot) => void saveAws({ spot })}
|
||||
hint={
|
||||
aws.wip_push_on_stop
|
||||
? "Runs on spare capacity at up to 70% off. AWS may reclaim it with two minutes' notice; the work-in-progress push below still runs."
|
||||
: "Runs on spare capacity at up to 70% off. AWS may reclaim it with two minutes' notice, and with the work-in-progress push below off, unpushed work is lost."
|
||||
}
|
||||
control={
|
||||
<Toggle
|
||||
label="Spot"
|
||||
checked={aws.spot}
|
||||
disabled={disabled}
|
||||
onChange={(spot) => void saveAws({ spot })}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
<section aria-label="Estimated cost">
|
||||
<h3 className="text-[13px] font-medium text-[var(--text-primary)]">Estimated cost</h3>
|
||||
<p className="mt-0.5 mb-1 text-xs text-[var(--text-secondary)] leading-snug">
|
||||
The on-demand Fargate price in this region for the CPU, memory, architecture and disk
|
||||
above.
|
||||
{ec2
|
||||
? `The on-demand price in this region of your EC2 instance${compute ? ` (${compute.instance_type})` : ""}, whatever this project reserves of it.`
|
||||
: "The on-demand Fargate price in this region for the CPU, memory, architecture and disk above."}
|
||||
</p>
|
||||
<AwsCostEstimate
|
||||
connectionId={connection ? connection.id : null}
|
||||
region={project.aws_placement?.region}
|
||||
config={aws}
|
||||
instanceType={compute?.instance_type ?? null}
|
||||
/>
|
||||
</section>
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import RuntimeSection from "./RuntimeSection";
|
||||
import type { AuthBridgeStatus, Project } from "../../../../lib/types";
|
||||
import { launchTypePatch } from "../../../../lib/awsWorkspace";
|
||||
import { awsProject } from "../../../../test/awsFixtures";
|
||||
|
||||
// The auth-bridge row owns its own IPC — see `AuthBridgeRow.tsx` for why it
|
||||
// does not go through `save`.
|
||||
@@ -181,3 +183,37 @@ describe("RuntimeSection — auth bridge toggle", () => {
|
||||
expect(save).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("RuntimeSection — Docker and VPN on AWS follow the launch type", () => {
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
it("are off and disabled on Fargate, switchable on EC2", () => {
|
||||
const fargate = awsProject();
|
||||
const { unmount } = renderSection(fargate);
|
||||
expect(screen.getByRole("switch", { name: VPN })).toBeDisabled();
|
||||
expect(screen.getByRole("switch", { name: "Allow container spawning" })).toBeDisabled();
|
||||
unmount();
|
||||
renderSection({ ...fargate, aws: { ...fargate.aws!, launch_type: "ec2" } });
|
||||
expect(screen.getByRole("switch", { name: VPN })).toBeEnabled();
|
||||
expect(screen.getByRole("switch", { name: "Allow container spawning" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("saves the flags when toggled on EC2", () => {
|
||||
const p = awsProject({ vpn_support_enabled: false, allow_docker_access: false });
|
||||
renderSection({ ...p, aws: { ...p.aws!, launch_type: "ec2" } });
|
||||
fireEvent.click(screen.getByRole("switch", { name: VPN }));
|
||||
expect(save).toHaveBeenCalledWith({ vpn_support_enabled: true });
|
||||
fireEvent.click(screen.getByRole("switch", { name: "Allow container spawning" }));
|
||||
expect(save).toHaveBeenCalledWith({ allow_docker_access: true });
|
||||
});
|
||||
|
||||
it("launchTypePatch clears Docker and VPN when switching to Fargate", () => {
|
||||
const p = awsProject({ allow_docker_access: true, vpn_support_enabled: true });
|
||||
const toFargate = launchTypePatch(p, "fargate");
|
||||
expect(toFargate).toMatchObject({ allow_docker_access: false, vpn_support_enabled: false });
|
||||
expect(toFargate.aws?.launch_type).toBe("fargate");
|
||||
const toEc2 = launchTypePatch(p, "ec2");
|
||||
expect(toEc2.allow_docker_access).toBeUndefined();
|
||||
expect(toEc2.aws?.launch_type).toBe("ec2");
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,14 @@ import PermissionModeControl, { permissionModePatch } from "../../PermissionMode
|
||||
import ClaudeInstructionsEditor from "../../ClaudeInstructionsEditor";
|
||||
import ClaudeCodeSettingsEditor from "../../ClaudeCodeSettingsEditor";
|
||||
import AuthBridgeRow from "./AuthBridgeRow";
|
||||
import { NOT_ON_AWS, SANDBOX_OFF_ON_AWS, isAwsProject } from "../../../../lib/awsWorkspace";
|
||||
import {
|
||||
EC2_DOCKER_ACCESS_NOTE,
|
||||
EC2_VPN_HINT,
|
||||
NOT_ON_AWS,
|
||||
SANDBOX_OFF_ON_AWS,
|
||||
isAwsProject,
|
||||
sandboxEffective,
|
||||
} from "../../../../lib/awsWorkspace";
|
||||
|
||||
interface Props {
|
||||
project: Project;
|
||||
@@ -21,6 +28,12 @@ export default function RuntimeSection({
|
||||
disabledReason,
|
||||
}: Props) {
|
||||
const aws = isAwsProject(project);
|
||||
// Docker access and VPN need the user's own EC2 instance (not Fargate).
|
||||
const ec2 = aws && project.aws?.launch_type === "ec2";
|
||||
const noPrivileged = aws && !ec2;
|
||||
// No ECS task can run Claude Code's sandbox: not on Fargate (P9), and not
|
||||
// on the user's own EC2 instance either (E8, measured live).
|
||||
const sandboxOff = aws;
|
||||
return (
|
||||
<>
|
||||
<ConfigGroup
|
||||
@@ -37,15 +50,15 @@ export default function RuntimeSection({
|
||||
<SwitchRow
|
||||
label="Sandbox mode"
|
||||
hint={
|
||||
aws
|
||||
? `${SANDBOX_OFF_ON_AWS}: Claude Code's bash sandbox has not been verified on Fargate yet, so an AWS workspace runs with it off whatever this says.`
|
||||
sandboxOff
|
||||
? `${SANDBOX_OFF_ON_AWS}: Claude Code's bash sandbox (bubblewrap) can't create its namespaces in an ECS task, on Fargate or EC2, so an AWS workspace runs with it off whatever this says.`
|
||||
: "Claude Code's bash sandbox (bubblewrap filesystem and network isolation). Triple-C is the source of truth: toggling this overrides any manual /sandbox configuration in the container's settings.json on next start."
|
||||
}
|
||||
control={
|
||||
<Toggle
|
||||
label="Sandbox mode"
|
||||
checked={aws ? false : project.sandbox_mode_enabled}
|
||||
disabled={disabled || aws}
|
||||
checked={sandboxEffective(project)}
|
||||
disabled={disabled || sandboxOff}
|
||||
onChange={(v) => save({ sandbox_mode_enabled: v })}
|
||||
/>
|
||||
}
|
||||
@@ -54,15 +67,17 @@ export default function RuntimeSection({
|
||||
<SwitchRow
|
||||
label="Allow container spawning"
|
||||
hint={
|
||||
aws
|
||||
noPrivileged
|
||||
? NOT_ON_AWS
|
||||
: "Mounts the Docker socket so Claude can build and run Docker containers from inside the sandbox."
|
||||
: ec2
|
||||
? `Mounts the Docker socket so Claude can build and run Docker containers from inside the workspace. ${EC2_DOCKER_ACCESS_NOTE}`
|
||||
: "Mounts the Docker socket so Claude can build and run Docker containers from inside the sandbox."
|
||||
}
|
||||
control={
|
||||
<Toggle
|
||||
label="Allow container spawning"
|
||||
checked={aws ? false : project.allow_docker_access}
|
||||
disabled={disabled || aws}
|
||||
checked={noPrivileged ? false : project.allow_docker_access}
|
||||
disabled={disabled || noPrivileged}
|
||||
onChange={(v) => save({ allow_docker_access: v })}
|
||||
/>
|
||||
}
|
||||
@@ -70,12 +85,12 @@ export default function RuntimeSection({
|
||||
|
||||
<SwitchRow
|
||||
label="VPN support"
|
||||
hint={aws ? NOT_ON_AWS : "Grants NET_ADMIN and the /dev/net/tun device so a VPN client (PIA, WireGuard, OpenVPN) can build a tunnel inside the container. Without it a client installs and runs but its connection hangs until it times out. Anything in the container can then reconfigure the container's own network stack; the host's is untouched. Changing this recreates the container on its next start — the home and .claude volumes are preserved."}
|
||||
hint={noPrivileged ? NOT_ON_AWS : ec2 ? EC2_VPN_HINT : "Grants NET_ADMIN and the /dev/net/tun device so a VPN client (PIA, WireGuard, OpenVPN) can build a tunnel inside the container. Without it a client installs and runs but its connection hangs until it times out. Anything in the container can then reconfigure the container's own network stack; the host's is untouched. Changing this recreates the container on its next start — the home and .claude volumes are preserved."}
|
||||
control={
|
||||
<Toggle
|
||||
label="VPN support"
|
||||
checked={aws ? false : project.vpn_support_enabled}
|
||||
disabled={disabled || aws}
|
||||
checked={noPrivileged ? false : project.vpn_support_enabled}
|
||||
disabled={disabled || noPrivileged}
|
||||
onChange={(v) => save({ vpn_support_enabled: v })}
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dockerFailLabel } from "../../lib/dockerStatus";
|
||||
import { useState } from "react";
|
||||
import { useDocker } from "../../hooks/useDocker";
|
||||
import { useSettings } from "../../hooks/useSettings";
|
||||
@@ -13,7 +14,7 @@ const IMAGE_SOURCE_OPTIONS: { value: ImageSource; label: string; description: st
|
||||
];
|
||||
|
||||
export default function DockerSettings() {
|
||||
const { dockerAvailable, imageExists, checkDocker, checkImage, buildImage, pullImage } =
|
||||
const { dockerAvailable, dockerStatus, imageExists, checkDocker, checkImage, buildImage, pullImage } =
|
||||
useDocker();
|
||||
const { appSettings, saveSettings } = useSettings();
|
||||
const [working, setWorking] = useState(false);
|
||||
@@ -82,7 +83,7 @@ export default function DockerSettings() {
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-[var(--text-secondary)]">Docker Status</span>
|
||||
<span className={dockerAvailable ? "text-[var(--success)]" : "text-[var(--error)]"}>
|
||||
{dockerAvailable === null ? "Checking..." : dockerAvailable ? "Connected" : "Not Available"}
|
||||
{dockerAvailable === null ? "Checking..." : dockerAvailable ? "Connected" : dockerFailLabel(dockerStatus)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -64,10 +64,10 @@ function ErrorLine({
|
||||
}) {
|
||||
return (
|
||||
<div role="alert" className="text-xs text-[var(--error)] leading-snug flex items-center gap-2 flex-wrap">
|
||||
<span>{awsErrorMessage(failure.error)}</span>
|
||||
<span className="min-w-0 break-words">{awsErrorMessage(failure.error)}</span>
|
||||
{failure.error.kind === "sso_expired" && !onSignIn && (
|
||||
<span>
|
||||
Sign in with <code>aws sso login --profile {failure.error.profile}</code> and try again.
|
||||
Sign in with <code className="break-all">aws sso login --profile {failure.error.profile}</code> and try again.
|
||||
</span>
|
||||
)}
|
||||
{failure.error.kind === "sso_expired" && onSignIn && (
|
||||
@@ -124,7 +124,7 @@ function PermissionReportView({ report }: { report: PermissionReport }) {
|
||||
>
|
||||
{badge}
|
||||
</span>
|
||||
<span className="text-xs text-[var(--text-secondary)]">
|
||||
<span className="min-w-0 break-all text-xs text-[var(--text-secondary)]" data-testid="report-identity">
|
||||
{report.identity.arn} · {report.region}
|
||||
</span>
|
||||
</div>
|
||||
@@ -154,8 +154,8 @@ function PermissionReportView({ report }: { report: PermissionReport }) {
|
||||
</summary>
|
||||
<ul className="mt-1 space-y-0.5 pl-4">
|
||||
{t.results.map((r) => (
|
||||
<li key={r.action} className="text-[var(--text-secondary)]">
|
||||
<span className="font-mono">{r.action}</span> — {r.verdict}
|
||||
<li key={r.action} className="text-[var(--text-secondary)] break-words">
|
||||
<span className="font-mono break-all">{r.action}</span> — {r.verdict}
|
||||
{r.note && (
|
||||
<>
|
||||
{" "}
|
||||
@@ -263,7 +263,7 @@ function ConnectionRow({ host }: { host: HostConnection }) {
|
||||
<li
|
||||
ref={rowRef}
|
||||
data-testid={`host-${host.id}`}
|
||||
className="border border-[var(--border-color)] rounded-[var(--radius-control)] p-2.5 space-y-1.5"
|
||||
className="border border-[var(--border-color)] rounded-[var(--radius-control)] p-2.5 space-y-1.5 min-w-0"
|
||||
>
|
||||
{editing ? (
|
||||
<div className="space-y-2">
|
||||
@@ -273,7 +273,7 @@ function ConnectionRow({ host }: { host: HostConnection }) {
|
||||
<input id={id} className={inputClass} list="aws-regions" value={region} onChange={(e) => setRegion(e.target.value)} />
|
||||
)}
|
||||
</Field>
|
||||
<div className="flex gap-2">
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
<Button size="sm" variant="primary" onClick={save} disabled={busy || !name.trim() || !region.trim()}>
|
||||
Save changes
|
||||
</Button>
|
||||
@@ -283,16 +283,16 @@ function ConnectionRow({ host }: { host: HostConnection }) {
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-start justify-between gap-2 flex-wrap">
|
||||
<div className="flex items-start justify-between gap-2 flex-wrap" data-testid={`host-header-${host.id}`}>
|
||||
<div className="min-w-0">
|
||||
<div className="text-[13px] font-medium text-[var(--text-primary)]">{host.name}</div>
|
||||
<div className="text-xs text-[var(--text-secondary)]">
|
||||
<div className="text-[13px] font-medium text-[var(--text-primary)] break-words">{host.name}</div>
|
||||
<div className="text-xs text-[var(--text-secondary)] break-words">
|
||||
<span>{auth.method === "profile" ? `Profile: ${auth.name}` : "Access keys"}</span>
|
||||
{" · "}
|
||||
<span>{host.kind.default_region}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-1.5">
|
||||
<div className="flex gap-1.5 flex-wrap">
|
||||
<Button size="sm" onClick={runCheck} disabled={busy} aria-label={`Check ${host.name}`}>
|
||||
Check
|
||||
</Button>
|
||||
@@ -309,7 +309,7 @@ function ConnectionRow({ host }: { host: HostConnection }) {
|
||||
)}
|
||||
{confirmRemove && (
|
||||
<div role="alertdialog" aria-label={`Remove ${host.name}?`} className="flex items-center gap-2 flex-wrap text-xs">
|
||||
<span>Remove {host.name}? Its stored credentials are deleted.</span>
|
||||
<span className="min-w-0 break-words">Remove {host.name}? Its stored credentials are deleted.</span>
|
||||
<Button data-focus="confirm-remove" size="sm" variant="danger" onClick={remove} disabled={busy} aria-label={`Confirm remove ${host.name}`}>
|
||||
Confirm remove
|
||||
</Button>
|
||||
@@ -501,7 +501,7 @@ function AddConnectionForm() {
|
||||
{busy ? "Adding…" : "Add connection"}
|
||||
</Button>
|
||||
{signedInAs && (
|
||||
<p role="status" className="text-xs text-[var(--success)]">
|
||||
<p role="status" className="text-xs text-[var(--success)] break-all">
|
||||
Signed in as {signedInAs}
|
||||
</p>
|
||||
)}
|
||||
@@ -516,7 +516,7 @@ export default function HostsSettings() {
|
||||
const { appSettings } = useSettings();
|
||||
const hosts = appSettings?.hosts ?? [];
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="space-y-3 min-w-0">
|
||||
<p className="text-xs text-[var(--text-secondary)] leading-snug">
|
||||
Where project environments run. AWS connections check what your credentials can do, and find, deploy and join environments.
|
||||
</p>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import AdminBundleModal from "./AdminBundleModal";
|
||||
import { instanceTable } from "./testkit";
|
||||
|
||||
const invoke = vi.fn();
|
||||
vi.mock("@tauri-apps/api/core", () => ({
|
||||
@@ -11,7 +12,9 @@ describe("AdminBundleModal", () => {
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
it("saves the bundle and shows the folder and the two commands, with Copy", async () => {
|
||||
invoke.mockResolvedValue("/home/me/triple-c-onboard-dev");
|
||||
invoke.mockImplementation(async (cmd: string) =>
|
||||
cmd === "list_ec2_instance_types" ? instanceTable : { folder: "/home/me/triple-c-onboard-dev", warning: null },
|
||||
);
|
||||
const writeText = vi.fn().mockResolvedValue(undefined);
|
||||
Object.assign(navigator, { clipboard: { writeText } });
|
||||
render(
|
||||
@@ -28,11 +31,81 @@ describe("AdminBundleModal", () => {
|
||||
environment: "dev",
|
||||
ownerId: "AIDAEXAMPLE",
|
||||
network: null,
|
||||
ec2: null,
|
||||
});
|
||||
const commands = "tofu init -backend-config=backend.hcl\ntofu apply";
|
||||
expect(screen.getByText((_, el) => el?.tagName === "PRE" && el.textContent === commands)).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Copy commands" }));
|
||||
await waitFor(() => expect(writeText).toHaveBeenCalledWith(commands));
|
||||
expect(invoke.mock.calls.filter((c) => c[0] === "save_aws_admin_bundle")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("an onboarding bundle carries the chosen EC2 capacity", async () => {
|
||||
invoke.mockImplementation(async (cmd: string) =>
|
||||
cmd === "list_ec2_instance_types" ? instanceTable : { folder: "/tmp/onboard", warning: "An ECS cluster named triple-c-dev--u-bob already belongs to another onboarding." },
|
||||
);
|
||||
render(
|
||||
<AdminBundleModal connectionId="h1" region="eu-west-1" kind="user_onboard" environment="dev" ownerId="AIDAEXAMPLE" onClose={vi.fn()} />,
|
||||
);
|
||||
fireEvent.click(screen.getByRole("radio", { name: "My own EC2 instances" }));
|
||||
await waitFor(() => expect((screen.getByLabelText("Instance type") as HTMLSelectElement).value).toBe("m7i.large"));
|
||||
const save = screen.getByRole("button", { name: "Save bundle to folder…" });
|
||||
// The triple-c-user value is required with EC2.
|
||||
expect(save).toBeDisabled();
|
||||
fireEvent.change(screen.getByLabelText("Your triple-c-user value"), { target: { value: " bob " } });
|
||||
fireEvent.change(screen.getByLabelText("Maximum instances"), { target: { value: "4" } });
|
||||
fireEvent.click(save);
|
||||
await waitFor(() =>
|
||||
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", {
|
||||
connectionId: "h1",
|
||||
region: "eu-west-1",
|
||||
kind: "user_onboard",
|
||||
environment: "dev",
|
||||
ownerId: "AIDAEXAMPLE",
|
||||
network: null,
|
||||
ec2: { instance_type: "m7i.large", max_instances: 4, user_tag: "bob" },
|
||||
}),
|
||||
);
|
||||
// The app's partial check of the name (cluster only) warns, never refuses.
|
||||
expect(await screen.findByText(/already belongs to another onboarding/)).toBeInTheDocument();
|
||||
expect(screen.getByText("/tmp/onboard")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps the recorded instance type, read-only, when the list can't load", async () => {
|
||||
invoke.mockImplementation(async (cmd: string) => {
|
||||
if (cmd === "list_ec2_instance_types") throw new Error("boom");
|
||||
return null;
|
||||
});
|
||||
render(
|
||||
<AdminBundleModal
|
||||
connectionId="h1"
|
||||
region="eu-west-1"
|
||||
kind="user_onboard"
|
||||
environment="dev"
|
||||
ownerId="AIDAEXAMPLE"
|
||||
recordedEc2={{
|
||||
instance_type: "r7g.2xlarge",
|
||||
max_instances: 2,
|
||||
user_tag: "alice",
|
||||
cluster_arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/triple-c-dev--u-alice",
|
||||
capacity_provider: "triple-c-dev--u-alice",
|
||||
}}
|
||||
onClose={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
expect(await screen.findByText(/Could not load the instance types/)).toBeInTheDocument();
|
||||
const type = screen.getByLabelText("Instance type") as HTMLSelectElement;
|
||||
expect(type).toBeDisabled();
|
||||
expect(type.value).toBe("r7g.2xlarge");
|
||||
expect(screen.getByText(/keeping r7g.2xlarge/)).toBeInTheDocument();
|
||||
// The kept values can still be saved.
|
||||
expect(screen.getByRole("button", { name: "Save bundle to folder…" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("an environment bundle has no EC2 capacity", () => {
|
||||
invoke.mockResolvedValue(instanceTable);
|
||||
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
|
||||
expect(screen.queryByRole("radio", { name: "My own EC2 instances" })).toBeNull();
|
||||
});
|
||||
|
||||
it("shows nothing new when the folder picker is dismissed", async () => {
|
||||
@@ -44,7 +117,7 @@ describe("AdminBundleModal", () => {
|
||||
});
|
||||
|
||||
it("an environment bundle asks for the name and network", async () => {
|
||||
invoke.mockResolvedValue("/tmp/env");
|
||||
invoke.mockResolvedValue({ folder: "/tmp/env", warning: null });
|
||||
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
|
||||
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "team" } });
|
||||
fireEvent.click(screen.getByRole("radio", { name: "Private + NAT" }));
|
||||
@@ -63,7 +136,7 @@ describe("AdminBundleModal", () => {
|
||||
});
|
||||
|
||||
it("an environment bundle carries the lifetime cap", async () => {
|
||||
invoke.mockResolvedValue("/tmp/env");
|
||||
invoke.mockResolvedValue({ folder: "/tmp/env", warning: null });
|
||||
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
|
||||
fireEvent.change(screen.getByLabelText("Maximum workspace lifetime (hours, 0 = none)"), { target: { value: "24" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
import { useState } from "react";
|
||||
import { saveAwsAdminBundle } from "../../../lib/tauri-commands";
|
||||
import { awsErrorMessage, toAwsError } from "../../../lib/aws-error";
|
||||
import type { AwsError, ModuleKind, NetworkChoice } from "../../../lib/types";
|
||||
import type { AwsError, ModuleKind, NetworkChoice, UserCompute } from "../../../lib/types";
|
||||
import Button from "../../ui/Button";
|
||||
import Field, { inputClass } from "../../ui/Field";
|
||||
import Modal from "../../ui/Modal";
|
||||
import LifetimeCapField, { parseCapHours } from "./LifetimeCapField";
|
||||
import Ec2CapacityFields from "./Ec2CapacityFields";
|
||||
import NetworkFields from "./NetworkFields";
|
||||
import { DEFAULT_NETWORK, environmentNameError, networkFromDraft, type NetworkDraft } from "./environmentFormat";
|
||||
import {
|
||||
DEFAULT_NETWORK,
|
||||
ec2DraftFrom,
|
||||
ec2FromDraft,
|
||||
environmentNameError,
|
||||
networkFromDraft,
|
||||
type Ec2Draft,
|
||||
type NetworkDraft,
|
||||
} from "./environmentFormat";
|
||||
|
||||
const COMMANDS = "tofu init -backend-config=backend.hcl\ntofu apply";
|
||||
|
||||
@@ -29,6 +38,7 @@ export default function AdminBundleModal({
|
||||
environment,
|
||||
ownerId,
|
||||
network,
|
||||
recordedEc2,
|
||||
reason,
|
||||
onSaved,
|
||||
onClose,
|
||||
@@ -40,6 +50,8 @@ export default function AdminBundleModal({
|
||||
environment?: string;
|
||||
ownerId?: string;
|
||||
network?: NetworkChoice;
|
||||
/** An onboarding bundle's starting EC2 capacity: the user's recorded one, if any. */
|
||||
recordedEc2?: UserCompute | null;
|
||||
/** Why the bundle is needed (e.g. the refusal that led here). */
|
||||
reason?: string;
|
||||
/** Called with the folder once a bundle was actually written. */
|
||||
@@ -50,29 +62,39 @@ export default function AdminBundleModal({
|
||||
const [name, setName] = useState("main");
|
||||
const [draft, setDraft] = useState<NetworkDraft>(DEFAULT_NETWORK);
|
||||
const [capText, setCapText] = useState("0");
|
||||
const [ec2Draft, setEc2Draft] = useState<Ec2Draft>(() => ec2DraftFrom(recordedEc2));
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saved, setSaved] = useState<string | null>(null);
|
||||
const [warning, setWarning] = useState<string | null>(null);
|
||||
const [error, setError] = useState<AwsError | null>(null);
|
||||
const [copyNote, setCopyNote] = useState<string | null>(null);
|
||||
|
||||
const nameError = askEnvironment ? environmentNameError(name) : null;
|
||||
const picked = askEnvironment ? networkFromDraft(draft) : null;
|
||||
const capHours = parseCapHours(capText);
|
||||
const ready = !nameError && (!picked || "network" in picked) && (!askEnvironment || capHours !== null);
|
||||
const onboarding = kind === "user_onboard";
|
||||
const ec2 = onboarding ? ec2FromDraft(ec2Draft) : null;
|
||||
const ready =
|
||||
!nameError &&
|
||||
(!picked || "network" in picked) &&
|
||||
(!askEnvironment || capHours !== null) &&
|
||||
(!ec2 || "compute" in ec2);
|
||||
|
||||
const save = async () => {
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
try {
|
||||
const folder = await saveAwsAdminBundle(connectionId, region, kind, {
|
||||
const result = await saveAwsAdminBundle(connectionId, region, kind, {
|
||||
environment: askEnvironment ? name : environment,
|
||||
ownerId,
|
||||
network: picked && "network" in picked ? picked.network : network,
|
||||
...(askEnvironment && capHours !== null ? { maxWorkspaceHours: capHours } : {}),
|
||||
...(ec2 && "compute" in ec2 ? { ec2: ec2.compute } : {}),
|
||||
});
|
||||
if (folder) {
|
||||
setSaved(folder);
|
||||
onSaved?.(folder);
|
||||
if (result) {
|
||||
setSaved(result.folder);
|
||||
setWarning(result.warning);
|
||||
onSaved?.(result.folder);
|
||||
}
|
||||
} catch (e) {
|
||||
setError(toAwsError(e));
|
||||
@@ -116,6 +138,7 @@ export default function AdminBundleModal({
|
||||
<p>
|
||||
Joining {environment} needs your own task role and file system, which an environment admin creates
|
||||
by applying this bundle. Send them the folder; once it's applied, Detect again shows you as joined.
|
||||
With EC2 capacity, its README has the admin first check that your EC2 names aren't already taken.
|
||||
</p>
|
||||
)}
|
||||
{kind === "permissions_boundary" && (
|
||||
@@ -125,6 +148,7 @@ export default function AdminBundleModal({
|
||||
</p>
|
||||
)}
|
||||
<p>The bundle works with OpenTofu or Terraform 1.10 and later. Its README lists every step.</p>
|
||||
{onboarding && <Ec2CapacityFields value={ec2Draft} onChange={setEc2Draft} />}
|
||||
{askEnvironment && (
|
||||
<div className="space-y-3">
|
||||
<Field label="Name" hint={nameError ?? undefined}>
|
||||
@@ -144,11 +168,16 @@ export default function AdminBundleModal({
|
||||
)}
|
||||
{saved && (
|
||||
<div className="space-y-1.5">
|
||||
{warning && (
|
||||
<p role="alert" className="text-[var(--warning)]">
|
||||
{warning}
|
||||
</p>
|
||||
)}
|
||||
<p>
|
||||
Saved to <code className="font-mono text-[var(--text-primary)] break-all">{saved}</code>
|
||||
</p>
|
||||
<p>From that folder, run:</p>
|
||||
<pre className="font-mono text-[var(--text-primary)] bg-[var(--bg-primary)] border border-[var(--border-color)] rounded-[var(--radius-control)] p-2 whitespace-pre-wrap">
|
||||
<pre className="font-mono text-[var(--text-primary)] bg-[var(--bg-primary)] border border-[var(--border-color)] rounded-[var(--radius-control)] p-2 whitespace-pre-wrap break-all max-w-full overflow-x-auto">
|
||||
{COMMANDS}
|
||||
</pre>
|
||||
<Button size="sm" onClick={copy}>
|
||||
|
||||
@@ -144,7 +144,7 @@ describe("DeployEnvironmentModal", () => {
|
||||
it("offers the boundary bundle when the boundary run fails, with its summary as the reason", async () => {
|
||||
invoke.mockImplementation(async (cmd: string) => {
|
||||
if (cmd === "deploy_aws_permissions_boundary") return "b1";
|
||||
if (cmd === "save_aws_admin_bundle") return "/tmp/b";
|
||||
if (cmd === "save_aws_admin_bundle") return { folder: "/tmp/b", warning: null };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
});
|
||||
const props = renderModal({ scan: scan({ boundary: "no" }) });
|
||||
@@ -166,7 +166,7 @@ describe("DeployEnvironmentModal", () => {
|
||||
it("opens the boundary bundle when the boundary command itself is denied", async () => {
|
||||
invoke.mockImplementation(async (cmd: string) => {
|
||||
if (cmd === "deploy_aws_permissions_boundary") throw { kind: "access_denied", action: "iam:CreatePolicy", message: "denied iam:CreatePolicy" };
|
||||
if (cmd === "save_aws_admin_bundle") return "/tmp/b";
|
||||
if (cmd === "save_aws_admin_bundle") return { folder: "/tmp/b", warning: null };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
});
|
||||
renderModal({ scan: scan({ boundary: "no" }) });
|
||||
|
||||
@@ -30,6 +30,7 @@ export default function DeployEnvironmentModal({
|
||||
onClose,
|
||||
onDeploy,
|
||||
onDetect,
|
||||
onBundle,
|
||||
}: {
|
||||
connectionId: string;
|
||||
region: string;
|
||||
@@ -40,6 +41,8 @@ export default function DeployEnvironmentModal({
|
||||
/** Starts the run; resolves to the command's error, or null once started. */
|
||||
onDeploy: (name: string, network: NetworkChoice, maxWorkspaceHours: number) => Promise<AwsError | null>;
|
||||
onDetect: () => void;
|
||||
/** Switch to an environment bundle for an admin (offered when AWS refuses the deploy). */
|
||||
onBundle?: () => void;
|
||||
}) {
|
||||
const [name, setName] = useState("main");
|
||||
const [draft, setDraft] = useState<NetworkDraft>(DEFAULT_NETWORK);
|
||||
@@ -80,7 +83,7 @@ export default function DeployEnvironmentModal({
|
||||
|
||||
const createBoundary = async () => {
|
||||
setBoundaryError(null);
|
||||
const err = await boundary.start(BOUNDARY_STATE_KEY, region, "Permissions boundary", () =>
|
||||
const err = await boundary.start(BOUNDARY_STATE_KEY, region, "boundary", "Permissions boundary", () =>
|
||||
deployAwsPermissionsBoundary(connectionId, region),
|
||||
);
|
||||
if (err?.kind === "access_denied") setBundle({ reason: awsErrorMessage(err) });
|
||||
@@ -125,18 +128,19 @@ export default function DeployEnvironmentModal({
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
<Button
|
||||
size="md"
|
||||
wrap
|
||||
variant="primary"
|
||||
onClick={createBoundary}
|
||||
disabled={!!boundaryRun && !boundaryRun.done}
|
||||
>
|
||||
Create the permissions boundary (needs an account administrator)
|
||||
</Button>
|
||||
<Button size="md" onClick={() => setBundle({})}>
|
||||
<Button size="md" wrap onClick={() => setBundle({})}>
|
||||
Save a bundle for an administrator instead
|
||||
</Button>
|
||||
</div>
|
||||
{boundaryError && (
|
||||
<p role="alert" className="text-[var(--error)]">
|
||||
<p role="alert" className="text-[var(--error)] break-words">
|
||||
{awsErrorMessage(boundaryError)}
|
||||
</p>
|
||||
)}
|
||||
@@ -149,6 +153,7 @@ export default function DeployEnvironmentModal({
|
||||
</p>
|
||||
<Button
|
||||
size="md"
|
||||
wrap
|
||||
onClick={() =>
|
||||
setBundle({
|
||||
reason: boundaryFailed.summary ?? `OpenTofu exited with code ${boundaryFailed.exit_code}`,
|
||||
@@ -179,10 +184,18 @@ export default function DeployEnvironmentModal({
|
||||
First deploy downloads OpenTofu (~30 MB) and the AWS provider (~150 MB).
|
||||
</p>
|
||||
{error && (
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug">
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug break-words">
|
||||
{awsErrorMessage(error)}
|
||||
</p>
|
||||
)}
|
||||
{error?.kind === "access_denied" && onBundle && !repair && (
|
||||
<div className="space-y-1.5 text-xs text-[var(--text-secondary)] leading-snug">
|
||||
<p>An admin can deploy this environment for you from a bundle.</p>
|
||||
<Button size="sm" wrap onClick={onBundle}>
|
||||
Save an environment bundle for an admin…
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { act, renderHook } from "@testing-library/react";
|
||||
import { runKey, useDeployRuns } from "./DeployProgress";
|
||||
import { act, render, renderHook, screen } from "@testing-library/react";
|
||||
import DeployProgressView, { runKey, useDeployRuns } from "./DeployProgress";
|
||||
import type { DeployProgress } from "../../../lib/types";
|
||||
import { progress, tofuEvent } from "./testkit";
|
||||
|
||||
@@ -29,10 +29,10 @@ describe("useDeployRuns", () => {
|
||||
const onDone = vi.fn();
|
||||
const { result } = renderHook(() => useDeployRuns(onDone));
|
||||
await act(async () => {
|
||||
await result.current.start(KEY, "eu-west-1", "Deploy of main", async () => "eu");
|
||||
await result.current.start(KEY, "eu-west-1", "deploy", "Deploy of main", async () => "eu");
|
||||
});
|
||||
await act(async () => {
|
||||
await result.current.start(KEY, "us-east-1", "Deploy of main", async () => "us");
|
||||
await result.current.start(KEY, "us-east-1", "deploy", "Deploy of main", async () => "us");
|
||||
});
|
||||
expect(handlers.size).toBe(2);
|
||||
emit(progress({ op_id: "eu", state_key: KEY, events: [tofuEvent("eu line")] }));
|
||||
@@ -54,8 +54,8 @@ describe("useDeployRuns", () => {
|
||||
it("removes its progress listeners on unmount", async () => {
|
||||
const { result, unmount } = renderHook(() => useDeployRuns());
|
||||
await act(async () => {
|
||||
await result.current.start(KEY, "eu-west-1", "Deploy of main", async () => "a");
|
||||
await result.current.start("boundary/terraform.tfstate", "eu-west-1", "Boundary", async () => "b");
|
||||
await result.current.start(KEY, "eu-west-1", "deploy", "Deploy of main", async () => "a");
|
||||
await result.current.start("boundary/terraform.tfstate", "eu-west-1", "deploy", "Boundary", async () => "b");
|
||||
});
|
||||
expect(handlers.size).toBe(2);
|
||||
unmount();
|
||||
@@ -66,7 +66,7 @@ describe("useDeployRuns", () => {
|
||||
const { result } = renderHook(() => useDeployRuns());
|
||||
let err: unknown;
|
||||
await act(async () => {
|
||||
err = await result.current.start(KEY, "eu-west-1", "Deploy of main", async () => {
|
||||
err = await result.current.start(KEY, "eu-west-1", "deploy", "Deploy of main", async () => {
|
||||
throw { kind: "other", message: "A deployment is already running" };
|
||||
});
|
||||
});
|
||||
@@ -75,3 +75,28 @@ describe("useDeployRuns", () => {
|
||||
expect(result.current.runs).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("DeployProgress layout", () => {
|
||||
it("wraps its header and a long failure summary", () => {
|
||||
render(
|
||||
<DeployProgressView
|
||||
connectionId="h1"
|
||||
run={{
|
||||
stateKey: KEY,
|
||||
region: "eu-west-1",
|
||||
kind: "deploy",
|
||||
label: "Deploy of main",
|
||||
opId: "x",
|
||||
events: [],
|
||||
done: true,
|
||||
cancellable: false,
|
||||
result: { ok: false, exit_code: 1, summary: "a very long summary ".repeat(10) },
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
const header = screen.getByTestId("deploy-progress-header");
|
||||
expect(header.className).toContain("flex-wrap");
|
||||
expect(header.firstElementChild?.className).toContain("min-w-0");
|
||||
expect(screen.getByRole("status").firstElementChild?.className).toContain("whitespace-normal");
|
||||
});
|
||||
});
|
||||
@@ -13,10 +13,18 @@ import Button from "../../ui/Button";
|
||||
import Modal from "../../ui/Modal";
|
||||
import StatusIndicator from "../../ui/StatusIndicator";
|
||||
|
||||
/**
|
||||
* What a run does. Decisions about a run (such as offering an admin bundle
|
||||
* when AWS refuses it) key on this, never on its label: a follow of an
|
||||
* existing environment's deploy is labelled like a new deploy.
|
||||
*/
|
||||
export type RunKind = "deploy" | "follow" | "repair" | "update" | "destroy" | "onboard" | "push" | "boundary";
|
||||
|
||||
/** One OpenTofu run this view is showing: a start, or a follow of one already running. */
|
||||
export interface DeployRun {
|
||||
stateKey: string;
|
||||
region: string;
|
||||
kind: RunKind;
|
||||
label: string;
|
||||
/** Null until the starting command resolves. */
|
||||
opId: string | null;
|
||||
@@ -57,7 +65,13 @@ export function useDeployRuns(
|
||||
}, []);
|
||||
|
||||
const start = useCallback(
|
||||
async (stateKey: string, region: string, label: string, run: () => Promise<string>): Promise<AwsError | null> => {
|
||||
async (
|
||||
stateKey: string,
|
||||
region: string,
|
||||
kind: RunKind,
|
||||
label: string,
|
||||
run: () => Promise<string>,
|
||||
): Promise<AwsError | null> => {
|
||||
const k = runKey(region, stateKey);
|
||||
subs.current.get(k)?.unlisten?.();
|
||||
const sub: Subscription = { unlisten: null };
|
||||
@@ -94,7 +108,7 @@ export function useDeployRuns(
|
||||
|
||||
setRuns((prev) => ({
|
||||
...prev,
|
||||
[k]: { stateKey, region, label, opId: null, events: [], done: false, cancellable: false, result: null },
|
||||
[k]: { stateKey, region, kind, label, opId: null, events: [], done: false, cancellable: false, result: null },
|
||||
}));
|
||||
|
||||
const unlisten = await listen<ProgressPayload>(AWS_DEPLOY_PROGRESS_EVENT, ({ payload }) => {
|
||||
@@ -146,7 +160,7 @@ function EventLine({ e }: { e: TofuEvent }) {
|
||||
return (
|
||||
<li className="text-xs leading-snug">
|
||||
{(e.resource || e.action) && (
|
||||
<span className="mr-1.5">
|
||||
<span className="mr-1.5 break-all">
|
||||
{e.resource && <span className="font-mono text-[var(--text-primary)]">{e.resource}</span>}
|
||||
{e.action && <span className="ml-1 text-[var(--text-secondary)]">{e.action}</span>}
|
||||
</span>
|
||||
@@ -154,10 +168,10 @@ function EventLine({ e }: { e: TofuEvent }) {
|
||||
{isError ? (
|
||||
<>
|
||||
<span className="font-semibold text-[var(--error)]">Error: </span>
|
||||
<pre className="whitespace-pre-wrap break-words font-mono text-[var(--error)]">{e.message}</pre>
|
||||
<pre className="max-w-full overflow-x-auto whitespace-pre-wrap break-words font-mono text-[var(--error)]">{e.message}</pre>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-[var(--text-secondary)]">{e.message}</span>
|
||||
<span className="text-[var(--text-secondary)] break-words">{e.message}</span>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
@@ -205,14 +219,14 @@ export default function DeployProgress({
|
||||
return (
|
||||
<section
|
||||
aria-label={`${run.label} progress`}
|
||||
className="space-y-1.5 border border-[var(--border-color)] rounded-[var(--radius-control)] p-2 bg-[var(--bg-primary)]"
|
||||
className="space-y-1.5 min-w-0 border border-[var(--border-color)] rounded-[var(--radius-control)] p-2 bg-[var(--bg-primary)]"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-2 flex-wrap">
|
||||
<div className="flex items-center gap-2 text-xs">
|
||||
<span className="font-medium text-[var(--text-primary)]">{run.label}</span>
|
||||
<div className="flex items-center justify-between gap-2 flex-wrap" data-testid="deploy-progress-header">
|
||||
<div className="flex items-center gap-2 text-xs flex-wrap min-w-0">
|
||||
<span className="font-medium text-[var(--text-primary)] break-words min-w-0">{run.label}</span>
|
||||
{status && <StatusIndicator tone={status.tone} label={status.label} />}
|
||||
</div>
|
||||
<div className="flex gap-1.5">
|
||||
<div className="flex gap-1.5 flex-wrap">
|
||||
{!run.done && (
|
||||
<Button
|
||||
size="sm"
|
||||
@@ -239,11 +253,12 @@ export default function DeployProgress({
|
||||
</ol>
|
||||
)}
|
||||
{run.done && run.result && (
|
||||
<p role="status" className="text-xs">
|
||||
<p role="status" className="text-xs break-words">
|
||||
{run.result.ok ? (
|
||||
<StatusIndicator tone="ok" label={`Finished${run.result.summary ? `: ${run.result.summary}` : ""}`} />
|
||||
<StatusIndicator wrap tone="ok" label={`Finished${run.result.summary ? `: ${run.result.summary}` : ""}`} />
|
||||
) : (
|
||||
<StatusIndicator
|
||||
wrap
|
||||
tone="error"
|
||||
label={`Failed (exit ${run.result.exit_code})${run.result.summary ? `: ${run.result.summary}` : ""}`}
|
||||
/>
|
||||
@@ -251,7 +266,7 @@ export default function DeployProgress({
|
||||
</p>
|
||||
)}
|
||||
{cancelError && !confirming && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
<p role="alert" className="text-xs text-[var(--error)] break-words">
|
||||
{awsErrorMessage(cancelError)}
|
||||
</p>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { listEc2InstanceTypes } from "../../../lib/tauri-commands";
|
||||
import type { Ec2InstanceTable, Ec2InstanceType } from "../../../lib/types";
|
||||
import Field, { monoInputClass, selectClass } from "../../ui/Field";
|
||||
import SegmentedControl from "../../ui/SegmentedControl";
|
||||
import { EC2_MAX_INSTANCES, userTagError, type Ec2Draft } from "./environmentFormat";
|
||||
|
||||
const label = (t: Ec2InstanceType) => `${t.instance_type} — ${t.vcpu} vCPU, ${t.memory_mib / 1024} GiB`;
|
||||
|
||||
/**
|
||||
* An onboarding's EC2 capacity (phase 7): none, or the user's own instances
|
||||
* of one type from the app's table, up to a maximum, on the cluster named
|
||||
* after their `triple-c-user` sign-in tag. Shared by self-onboarding and the
|
||||
* onboarding bundle.
|
||||
*/
|
||||
export default function Ec2CapacityFields({
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
value: Ec2Draft;
|
||||
onChange: (next: Ec2Draft) => void;
|
||||
}) {
|
||||
const [table, setTable] = useState<Ec2InstanceTable | null>(null);
|
||||
const [loadError, setLoadError] = useState(false);
|
||||
const set = (patch: Partial<Ec2Draft>) => onChange({ ...value, ...patch });
|
||||
|
||||
useEffect(() => {
|
||||
let live = true;
|
||||
listEc2InstanceTypes()
|
||||
.then((t) => live && setTable(t))
|
||||
.catch(() => live && setLoadError(true));
|
||||
return () => {
|
||||
live = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
// The table's default fills an empty choice once the table is known.
|
||||
useEffect(() => {
|
||||
if (table && !value.instanceType) onChange({ ...value, instanceType: table.default_type });
|
||||
}, [table, value, onChange]);
|
||||
|
||||
const tagError = value.enabled && value.userTag ? userTagError(value.userTag.trim()) : null;
|
||||
const x86 = table?.types.filter((t) => t.arch === "x86_64") ?? [];
|
||||
const arm = table?.types.filter((t) => t.arch === "arm64") ?? [];
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="text-[13px] font-medium text-[var(--text-primary)]">EC2 capacity</div>
|
||||
<SegmentedControl<"none" | "ec2">
|
||||
label="EC2 capacity"
|
||||
className="flex-wrap max-w-full"
|
||||
value={value.enabled ? "ec2" : "none"}
|
||||
onChange={(v) => set({ enabled: v === "ec2" })}
|
||||
segments={[
|
||||
{ value: "none", label: "None (Fargate only)" },
|
||||
{ value: "ec2", label: "My own EC2 instances" },
|
||||
]}
|
||||
/>
|
||||
<p className="text-xs text-[var(--text-secondary)] leading-snug">
|
||||
{value.enabled
|
||||
? "Projects set to EC2 run on instances only you use. Instances scale to zero when none of your workspaces run; a start after idle takes 2–3 minutes longer. You pay the instance's hourly price while it runs."
|
||||
: "Every project runs on Fargate. You can add EC2 capacity later by applying again."}
|
||||
</p>
|
||||
{value.enabled && (
|
||||
<div className="space-y-2">
|
||||
<Field label="Instance type">
|
||||
{(id) => (
|
||||
<select
|
||||
id={id}
|
||||
className={`${selectClass} w-full`}
|
||||
value={value.instanceType}
|
||||
disabled={!table}
|
||||
onChange={(e) => set({ instanceType: e.target.value })}
|
||||
>
|
||||
{!table && <option value={value.instanceType}>{value.instanceType || "Loading…"}</option>}
|
||||
{x86.length > 0 && (
|
||||
<optgroup label="x86 (Intel)">
|
||||
{x86.map((t) => (
|
||||
<option key={t.instance_type} value={t.instance_type}>
|
||||
{label(t)}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
)}
|
||||
{arm.length > 0 && (
|
||||
<optgroup label="ARM (Graviton)">
|
||||
{arm.map((t) => (
|
||||
<option key={t.instance_type} value={t.instance_type}>
|
||||
{label(t)}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
)}
|
||||
</select>
|
||||
)}
|
||||
</Field>
|
||||
{loadError && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
Could not load the instance types
|
||||
{value.instanceType ? `; keeping ${value.instanceType}.` : "; detect again to choose one."}
|
||||
</p>
|
||||
)}
|
||||
<Field label="Maximum instances" hint="How many of your EC2 workspaces can run at once (one per instance).">
|
||||
{(id) => (
|
||||
<select
|
||||
id={id}
|
||||
className={`${selectClass} w-full`}
|
||||
value={value.maxInstances}
|
||||
onChange={(e) => set({ maxInstances: Number(e.target.value) })}
|
||||
>
|
||||
{Array.from({ length: EC2_MAX_INSTANCES }, (_, i) => i + 1).map((n) => (
|
||||
<option key={n} value={n}>
|
||||
{n}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</Field>
|
||||
<Field
|
||||
label="Your triple-c-user value"
|
||||
hint={tagError ?? "The value an admin set as your triple-c-user sign-in tag. Your EC2 cluster is named after it."}
|
||||
>
|
||||
{(id) => (
|
||||
<input
|
||||
id={id}
|
||||
className={monoInputClass}
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
value={value.userTag}
|
||||
aria-invalid={!!tagError}
|
||||
onChange={(e) => set({ userTag: e.target.value })}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
<p className="text-xs text-[var(--warning)] leading-snug">
|
||||
Triple-C can't read your sign-in's tags to check this value. If it isn't exactly your triple-c-user tag,
|
||||
EC2 workspaces fail to start with access denied.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import EnvironmentsPanel from "./EnvironmentsPanel";
|
||||
import JoinEnvironmentModal from "./JoinEnvironmentModal";
|
||||
import type { DeployProgress, EnvironmentScan } from "../../../lib/types";
|
||||
import { conn, env, info, progress, scan, tofuEvent } from "./testkit";
|
||||
import { compute, conn, env, info, instanceTable, progress, scan, tofuEvent } from "./testkit";
|
||||
|
||||
const invoke = vi.fn();
|
||||
vi.mock("@tauri-apps/api/core", () => ({
|
||||
@@ -72,7 +73,7 @@ describe("EnvironmentsPanel", () => {
|
||||
});
|
||||
|
||||
it("deploy is secondary when environments exist", async () => {
|
||||
backend(() => scan({ environments: [env({ onboarding: { state: "joined" }, actions: [] })] }));
|
||||
backend(() => scan({ environments: [env({ onboarding: { state: "joined", ec2: null }, actions: [] })] }));
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
const deploy = await screen.findByRole("button", { name: "Deploy a new environment" });
|
||||
@@ -80,7 +81,7 @@ describe("EnvironmentsPanel", () => {
|
||||
expect(within(screen.getByTestId("env-dev")).getByText("Joined")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("offers an admin bundle instead of deploy for a workspace user with nothing found", async () => {
|
||||
it("offers deploy to a workspace user too, with a note and the admin options under a disclosure", async () => {
|
||||
backend(
|
||||
() => scan(),
|
||||
(cmd) => {
|
||||
@@ -90,22 +91,162 @@ describe("EnvironmentsPanel", () => {
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
expect(await screen.findByText(/needs an environment admin/)).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Deploy a new environment" })).toBeNull();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Show the environment-admin policies" }));
|
||||
expect(await screen.findByText("environment-admin.json")).toBeInTheDocument();
|
||||
const deploy = await screen.findByRole("button", { name: "Deploy a new environment" });
|
||||
expect(deploy.className).toContain("--accent-emphasis");
|
||||
expect(screen.getByTestId("deploy-unconfirmed-note")).toHaveTextContent(
|
||||
"Your permission check didn't confirm environment-admin rights; the deploy will stop with AWS's message if a permission is missing.",
|
||||
);
|
||||
const options = screen.getByTestId("not-admin-options");
|
||||
expect(options.tagName).toBe("DETAILS");
|
||||
expect(within(options).getByText(/Not an admin\?/)).toBeInTheDocument();
|
||||
fireEvent.click(within(options).getByRole("button", { name: "Show the environment-admin policies" }));
|
||||
expect(await within(options).findByRole("button", { name: "Copy environment-admin.json" })).toBeInTheDocument();
|
||||
expect(invoke).toHaveBeenCalledWith("aws_policy_document", { tier: "environment_admin" });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Request an environment from an admin…" }));
|
||||
fireEvent.click(within(options).getByRole("button", { name: "Request an environment from an admin…" }));
|
||||
const dialog = screen.getByRole("dialog");
|
||||
expect(within(dialog).getByRole("button", { name: "Save bundle to folder…" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("treats a connection with no permission report as not an admin", async () => {
|
||||
it("opens the deploy form for a workspace user", async () => {
|
||||
backend(() => scan());
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Deploy a new environment" }));
|
||||
expect(screen.getByRole("dialog", { name: "Deploy a new environment" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("an admin sees deploy without the note or the not-an-admin options", async () => {
|
||||
backend(() => scan());
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
expect(await screen.findByRole("button", { name: "Deploy a new environment" })).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("deploy-unconfirmed-note")).toBeNull();
|
||||
expect(screen.queryByTestId("not-admin-options")).toBeNull();
|
||||
});
|
||||
|
||||
it("a connection with no permission report gets deploy with the note", async () => {
|
||||
backend(() => scan());
|
||||
render(<EnvironmentsPanel connection={conn} />);
|
||||
await open();
|
||||
expect(await screen.findByRole("button", { name: "Request an environment from an admin…" })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Deploy a new environment" })).toBeNull();
|
||||
expect(await screen.findByRole("button", { name: "Deploy a new environment" })).toBeInTheDocument();
|
||||
expect(screen.getByTestId("deploy-unconfirmed-note")).toBeInTheDocument();
|
||||
expect(screen.getByText(/Run Check to see what your credentials can do/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows the boundary note to anyone when the account has no boundary", async () => {
|
||||
backend(() => scan({ boundary: "no" }));
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
expect(await screen.findByText(/no Triple-C permissions boundary yet/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("offers a bundle when AWS refuses a deploy run's permission", async () => {
|
||||
backend(
|
||||
() => scan(),
|
||||
(cmd) => {
|
||||
if (cmd === "deploy_aws_environment") return "d1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Deploy a new environment" }));
|
||||
fireEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Deploy" }));
|
||||
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
|
||||
const key = "environments/main/terraform.tfstate";
|
||||
emit(
|
||||
progress({
|
||||
op_id: "d1",
|
||||
state_key: key,
|
||||
events: [tofuEvent("creating IAM Role: AccessDenied: User is not authorized to perform iam:CreateRole", { level: "error" })],
|
||||
}),
|
||||
);
|
||||
emit(progress({ op_id: "d1", state_key: key, done: true, cancellable: false, result: { ok: false, exit_code: 1, summary: null } }));
|
||||
const hint = await screen.findByTestId("denied-hint");
|
||||
expect(hint).toHaveTextContent("AWS refused a permission");
|
||||
fireEvent.click(within(hint).getByRole("button", { name: "Save a bundle for an admin…" }));
|
||||
expect(within(screen.getByRole("dialog")).getByRole("button", { name: "Save bundle to folder…" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("never offers a new-environment bundle for a refused follow of an existing deploy", async () => {
|
||||
const key = "environments/dev/terraform.tfstate";
|
||||
backend(
|
||||
() => scan({ environments: [env({ actions: ["follow"], state: { state: "ready" } })] }),
|
||||
(cmd) => {
|
||||
if (cmd === "follow_aws_deployment") return "f9";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
await waitFor(() => expect(invoke.mock.calls.some((c) => c[0] === "follow_aws_deployment")).toBe(true));
|
||||
await screen.findByRole("region", { name: "Deploy of dev progress" });
|
||||
emit(
|
||||
progress({
|
||||
op_id: "f9",
|
||||
state_key: key,
|
||||
events: [tofuEvent("AccessDenied: User is not authorized to perform ecs:UpdateService", { level: "error" })],
|
||||
}),
|
||||
);
|
||||
emit(progress({ op_id: "f9", state_key: key, done: true, cancellable: false, result: { ok: false, exit_code: 1, summary: null } }));
|
||||
expect(await screen.findByText(/Failed \(exit 1\)/)).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("denied-hint")).toBeNull();
|
||||
});
|
||||
|
||||
it("marks the not-an-admin disclosure open or closed like the Environments toggle", async () => {
|
||||
backend(() => scan());
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
const options = await screen.findByTestId("not-admin-options");
|
||||
const summary = options.querySelector("summary")!;
|
||||
expect(summary).toHaveTextContent("▸");
|
||||
(options as HTMLDetailsElement).open = true;
|
||||
fireEvent(options, new Event("toggle"));
|
||||
await waitFor(() => expect(summary).toHaveTextContent("▾"));
|
||||
});
|
||||
|
||||
it("shows a refused deploy's AWS message and offers the bundle", async () => {
|
||||
backend(
|
||||
() => scan(),
|
||||
(cmd) => {
|
||||
if (cmd === "deploy_aws_environment")
|
||||
throw { kind: "access_denied", action: "s3:CreateBucket", message: "User is not authorized" };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Deploy a new environment" }));
|
||||
const dialog = screen.getByRole("dialog");
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Deploy" }));
|
||||
expect(await within(dialog).findByText("AWS denied s3:CreateBucket: User is not authorized")).toBeInTheDocument();
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Save an environment bundle for an admin…" }));
|
||||
expect(within(screen.getByRole("dialog")).getByRole("button", { name: "Save bundle to folder…" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps the key rows wrapping in a narrow sidebar", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ actions: ["join", "update", "repair"] })] }),
|
||||
(cmd) => {
|
||||
if (cmd === "aws_policy_document") return [{ name: "a.json", json: "{}" }, { name: "b.json", json: "{}" }];
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
const row = await screen.findByTestId("env-row-dev");
|
||||
expect(row.className).toContain("flex-wrap");
|
||||
expect(row.firstElementChild?.className).toContain("min-w-0");
|
||||
const actions = screen.getByTestId("env-actions-dev");
|
||||
expect(actions.className).toContain("flex-wrap");
|
||||
expect(actions.className).toContain("min-w-0");
|
||||
expect(screen.getByTestId("region-row").className).toContain("flex-wrap");
|
||||
expect(screen.getByRole("button", { name: "Deploy a new environment" }).className).toContain("whitespace-normal");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Show the environment-admin policies" }));
|
||||
const grid = await screen.findByTestId("policy-copy-grid");
|
||||
expect(grid.className).toContain("grid");
|
||||
expect(grid.className).toContain("auto-fill");
|
||||
for (const b of within(grid).getAllByRole("button")) expect(b.className).toContain("whitespace-normal");
|
||||
});
|
||||
|
||||
it("an incomplete environment offers repair and destroy, destroy only after typing its name", async () => {
|
||||
@@ -204,9 +345,10 @@ describe("EnvironmentsPanel", () => {
|
||||
backend(
|
||||
() => {
|
||||
detects += 1;
|
||||
return scan({ environments: [env({ onboarding: detects > 1 ? { state: "joined" } : { state: "not_joined" } })] });
|
||||
return scan({ environments: [env({ onboarding: detects > 1 ? { state: "joined", ec2: null } : { state: "not_joined" } })] });
|
||||
},
|
||||
async (cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
if (cmd === "onboard_aws_user") {
|
||||
// The backend can emit before the invoke resolves: an old follower's
|
||||
// line (another op) and this run's first line.
|
||||
@@ -220,9 +362,18 @@ describe("EnvironmentsPanel", () => {
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
// The join dialog asks for EC2 capacity first; none is the default.
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Join" }));
|
||||
expect(await screen.findByText("Planning")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
expect(screen.queryByText("stale line")).toBeNull();
|
||||
expect(invoke).toHaveBeenCalledWith("onboard_aws_user", { connectionId: "h1", region: "eu-west-1", environment: "dev" });
|
||||
expect(invoke).toHaveBeenCalledWith("onboard_aws_user", {
|
||||
connectionId: "h1",
|
||||
region: "eu-west-1",
|
||||
environment: "dev",
|
||||
ec2: { kind: "none" },
|
||||
});
|
||||
expect(screen.getByRole("button", { name: "Cancel deployment" })).toBeDisabled();
|
||||
|
||||
emit(progress({ op_id: "op1", state_key: key, events: [tofuEvent("Creating...", { resource: "aws_iam_role.task", action: "create" })] }));
|
||||
@@ -237,17 +388,58 @@ describe("EnvironmentsPanel", () => {
|
||||
expect(await within(screen.getByTestId("env-dev")).findByText("Joined")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("a workspace user's join opens the onboarding bundle and waits for an admin", async () => {
|
||||
it("a workspace user can join themselves, with the note", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env()] }),
|
||||
(cmd) => {
|
||||
if (cmd === "save_aws_admin_bundle") return "/home/me/onboard-dev";
|
||||
if (cmd === "onboard_aws_user") return "j1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
expect(within(dialog).getByTestId("join-unconfirmed-note")).toBeInTheDocument();
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Join" }));
|
||||
await waitFor(() =>
|
||||
expect(invoke).toHaveBeenCalledWith("onboard_aws_user", expect.objectContaining({ environment: "dev" })),
|
||||
);
|
||||
});
|
||||
|
||||
it("a refused join shows AWS's message and offers the onboarding bundle", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env()] }),
|
||||
(cmd) => {
|
||||
if (cmd === "onboard_aws_user") throw { kind: "access_denied", action: "s3:PutObject", message: "explicit deny" };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Join" }));
|
||||
expect(await within(dialog).findByText("AWS denied s3:PutObject: explicit deny")).toBeInTheDocument();
|
||||
expect(within(dialog).getByText(/AWS refused this with your permissions/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("a workspace user can switch the join to an onboarding bundle and then waits for an admin", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env()] }),
|
||||
(cmd) => {
|
||||
if (cmd === "save_aws_admin_bundle") return { folder: "/home/me/onboard-dev", warning: null };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
fireEvent.click(
|
||||
within(screen.getByRole("dialog", { name: "Join dev" })).getByRole("button", {
|
||||
name: "Save an onboarding bundle for an admin…",
|
||||
}),
|
||||
);
|
||||
const dialog = screen.getByRole("dialog");
|
||||
// Opening the bundle is not a request yet.
|
||||
expect(screen.queryByText("Waiting for an admin to onboard you")).toBeNull();
|
||||
@@ -261,6 +453,7 @@ describe("EnvironmentsPanel", () => {
|
||||
environment: "dev",
|
||||
ownerId: "AIDAEXAMPLEEXAMPLE01",
|
||||
network: null,
|
||||
ec2: null,
|
||||
}),
|
||||
);
|
||||
expect(invoke.mock.calls.some((c) => c[0] === "onboard_aws_user")).toBe(false);
|
||||
@@ -268,6 +461,179 @@ describe("EnvironmentsPanel", () => {
|
||||
expect(within(screen.getByTestId("env-dev")).getByText("Waiting for an admin to onboard you")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("an admin's join has no unconfirmed note", async () => {
|
||||
backend(() => scan({ environments: [env()] }));
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
expect(within(screen.getByRole("dialog", { name: "Join dev" })).queryByTestId("join-unconfirmed-note")).toBeNull();
|
||||
});
|
||||
|
||||
it("an admin joins with EC2 capacity: a type from the table, max instances and their triple-c-user value", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env()] }),
|
||||
(cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
if (cmd === "onboard_aws_user") return "op1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
fireEvent.click(within(dialog).getByRole("radio", { name: "My own EC2 instances" }));
|
||||
const type = within(dialog).getByLabelText("Instance type") as HTMLSelectElement;
|
||||
// The table's default, grouped by architecture.
|
||||
await waitFor(() => expect(type.value).toBe("m7i.large"));
|
||||
const groups = [...type.querySelectorAll("optgroup")].map((g) => g.label);
|
||||
expect(groups).toEqual(["x86 (Intel)", "ARM (Graviton)"]);
|
||||
expect(within(dialog).getByRole("option", { name: "m7g.large — 2 vCPU, 8 GiB" })).toBeInTheDocument();
|
||||
expect(within(dialog).getByText(/admin set as your triple-c-user sign-in tag/)).toBeInTheDocument();
|
||||
expect(within(dialog).getByText(/fail to start with access denied/)).toBeInTheDocument();
|
||||
// The value is required and validated.
|
||||
const joinButton = within(dialog).getByRole("button", { name: "Join" });
|
||||
expect(joinButton).toBeDisabled();
|
||||
const tag = within(dialog).getByLabelText("Your triple-c-user value");
|
||||
fireEvent.change(tag, { target: { value: "al--ice" } });
|
||||
expect(within(dialog).getByText(/single hyphens/)).toBeInTheDocument();
|
||||
expect(joinButton).toBeDisabled();
|
||||
fireEvent.change(tag, { target: { value: "alice" } });
|
||||
fireEvent.change(type, { target: { value: "m7g.large" } });
|
||||
fireEvent.change(within(dialog).getByLabelText("Maximum instances"), { target: { value: "2" } });
|
||||
fireEvent.click(joinButton);
|
||||
await waitFor(() =>
|
||||
expect(invoke).toHaveBeenCalledWith("onboard_aws_user", {
|
||||
connectionId: "h1",
|
||||
region: "eu-west-1",
|
||||
environment: "dev",
|
||||
ec2: { kind: "ec2", instance_type: "m7g.large", max_instances: 2, user_tag: "alice" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("shows a joined user's compute, and a change starts from the recorded values", async () => {
|
||||
backend(
|
||||
() =>
|
||||
scan({
|
||||
environments: [
|
||||
env({ onboarding: { state: "joined", ec2: compute() } }),
|
||||
env({ name: "plain", onboarding: { state: "joined", ec2: null } }),
|
||||
],
|
||||
}),
|
||||
(cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
if (cmd === "onboard_aws_user") return "op1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
expect(await screen.findByTestId("compute-dev")).toHaveTextContent(
|
||||
"EC2 capacity: m7g.xlarge, up to 3 instances (triple-c-user alice)",
|
||||
);
|
||||
expect(screen.getByTestId("compute-plain")).toHaveTextContent("EC2 capacity: none (Fargate only)");
|
||||
// Joined: no Join, but the capacity can be changed (a re-apply).
|
||||
const row = screen.getByTestId("env-dev");
|
||||
expect(within(row).queryByRole("button", { name: "Join dev" })).toBeNull();
|
||||
fireEvent.click(within(row).getByRole("button", { name: "EC2 capacity in dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "EC2 capacity in dev" });
|
||||
expect((within(dialog).getByLabelText("Instance type") as HTMLSelectElement).value).toBe("m7g.xlarge");
|
||||
expect((within(dialog).getByLabelText("Maximum instances") as HTMLSelectElement).value).toBe("3");
|
||||
expect((within(dialog).getByLabelText("Your triple-c-user value") as HTMLInputElement).value).toBe("alice");
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Apply" }));
|
||||
await waitFor(() =>
|
||||
expect(invoke).toHaveBeenCalledWith("onboard_aws_user", {
|
||||
connectionId: "h1",
|
||||
region: "eu-west-1",
|
||||
environment: "dev",
|
||||
ec2: { kind: "ec2", instance_type: "m7g.xlarge", max_instances: 3, user_tag: "alice" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("finishing an incomplete onboarding starts from its record and says when it removes EC2", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ onboarding: { state: "incomplete", lock: null, ec2: compute() } })] }),
|
||||
(cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
expect((within(dialog).getByLabelText("Your triple-c-user value") as HTMLInputElement).value).toBe("alice");
|
||||
expect(within(dialog).queryByText(/This removes your EC2 capacity/)).toBeNull();
|
||||
fireEvent.click(within(dialog).getByRole("radio", { name: "None (Fargate only)" }));
|
||||
expect(within(dialog).getByText("This removes your EC2 capacity (m7g.xlarge) in dev.")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("an incomplete onboarding with no record warns that None removes half-built EC2 capacity", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ onboarding: { state: "incomplete", lock: null, ec2: null } })] }),
|
||||
(cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
expect(within(dialog).getByRole("alert")).toHaveTextContent(
|
||||
/didn't finish, and it may have built part of your EC2 capacity. Choosing None removes anything it built/,
|
||||
);
|
||||
fireEvent.click(within(dialog).getByRole("radio", { name: "My own EC2 instances" }));
|
||||
expect(within(dialog).queryByText(/Choosing None removes/)).toBeNull();
|
||||
});
|
||||
|
||||
it("with no record, the join starts from the EC2 capacity last chosen here", async () => {
|
||||
invoke.mockImplementation(async (cmd: string) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
});
|
||||
render(
|
||||
<JoinEnvironmentModal
|
||||
environment="dev"
|
||||
region="eu-west-1"
|
||||
recorded={null}
|
||||
reapply={false}
|
||||
incomplete
|
||||
lastChoice={{ instance_type: "m7g.xlarge", max_instances: 2, user_tag: "alice" }}
|
||||
onJoin={async () => null}
|
||||
onClose={() => {}}
|
||||
/>,
|
||||
);
|
||||
const dialog = screen.getByRole("dialog", { name: "Join dev" });
|
||||
expect((within(dialog).getByLabelText("Your triple-c-user value") as HTMLInputElement).value).toBe("alice");
|
||||
expect(within(dialog).queryByText(/Choosing None removes/)).toBeNull();
|
||||
});
|
||||
|
||||
it("a joined workspace user's capacity bundle starts from their record", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ onboarding: { state: "joined", ec2: compute({ max_instances: 1 }) } })] }),
|
||||
(cmd) => {
|
||||
if (cmd === "list_ec2_instance_types") return instanceTable;
|
||||
if (cmd === "save_aws_admin_bundle") return { folder: "/home/me/onboard-dev", warning: null };
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
},
|
||||
);
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "EC2 capacity in dev" }));
|
||||
fireEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Save an onboarding bundle for an admin…" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Onboarding bundle" });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Save bundle to folder…" }));
|
||||
await waitFor(() =>
|
||||
expect(invoke).toHaveBeenCalledWith(
|
||||
"save_aws_admin_bundle",
|
||||
expect.objectContaining({ ec2: { instance_type: "m7g.xlarge", max_instances: 1, user_tag: "alice" } }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("follows a deployment running on this machine when opened, and cancels after confirming", async () => {
|
||||
const lock = { who: "me@here", created: "x", operation: "OperationTypeApply", age_secs: 60, local_process: true };
|
||||
backend(
|
||||
@@ -303,7 +669,7 @@ describe("EnvironmentsPanel", () => {
|
||||
|
||||
it("Update pre-fills the environment's recorded cap and sends what is shown", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], info: { ...info("dev"), max_workspace_hours: 24 }, onboarding: { state: "joined" } })] }),
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], info: { ...info("dev"), max_workspace_hours: 24 }, onboarding: { state: "joined", ec2: null } })] }),
|
||||
async (cmd) => {
|
||||
if (cmd === "update_aws_environment") return "u1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
@@ -326,7 +692,7 @@ describe("EnvironmentsPanel", () => {
|
||||
|
||||
it("shows a failed run's errors in full", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], onboarding: { state: "joined" } })] }),
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], onboarding: { state: "joined", ec2: null } })] }),
|
||||
async (cmd) => {
|
||||
if (cmd === "update_aws_environment") return "u1";
|
||||
throw new Error(`unexpected ${cmd}`);
|
||||
@@ -349,6 +715,7 @@ describe("EnvironmentsPanel", () => {
|
||||
render(<EnvironmentsPanel connection={conn} tier="workspace_user" />);
|
||||
await open();
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Join dev" }));
|
||||
fireEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Save an onboarding bundle for an admin…" }));
|
||||
fireEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Close" }));
|
||||
expect(screen.queryByText("Waiting for an admin to onboard you")).toBeNull();
|
||||
expect(within(screen.getByTestId("env-dev")).getByRole("button", { name: "Join dev" })).toHaveTextContent("Join");
|
||||
@@ -383,7 +750,7 @@ describe("EnvironmentsPanel", () => {
|
||||
expect(screen.queryByRole("button", { name: "Destroy dev" })).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: "Deploy a new environment" })).toBeNull();
|
||||
await act(async () =>
|
||||
resolveUs(scan({ region: "us-east-1", environments: [env({ state: { state: "update_available" }, actions: ["update"], onboarding: { state: "joined" } })] })),
|
||||
resolveUs(scan({ region: "us-east-1", environments: [env({ state: { state: "update_available" }, actions: ["update"], onboarding: { state: "joined", ec2: null } })] })),
|
||||
);
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Update dev" }));
|
||||
fireEvent.click(within(screen.getByRole("dialog", { name: "Update dev" })).getByRole("button", { name: "Update" }));
|
||||
@@ -399,7 +766,7 @@ describe("EnvironmentsPanel", () => {
|
||||
invoke.mockImplementation(async (cmd: string) => {
|
||||
if (cmd !== "detect_aws_environments") throw new Error(`unexpected ${cmd}`);
|
||||
calls += 1;
|
||||
const found = scan({ environments: [env({ actions: ["join", "update"], onboarding: { state: "joined" } })] });
|
||||
const found = scan({ environments: [env({ actions: ["join", "update"], onboarding: { state: "joined", ec2: null } })] });
|
||||
if (calls === 1) return found;
|
||||
return new Promise<EnvironmentScan>((r) => (release = () => r(found)));
|
||||
});
|
||||
@@ -414,7 +781,7 @@ describe("EnvironmentsPanel", () => {
|
||||
|
||||
it("a done that arrives before the invoke resolves is shown and re-detects", async () => {
|
||||
backend(
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["update"], onboarding: { state: "joined" } })] }),
|
||||
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["update"], onboarding: { state: "joined", ec2: null } })] }),
|
||||
async (cmd) => {
|
||||
if (cmd === "update_aws_environment") {
|
||||
emit(progress({ op_id: "u1", events: [tofuEvent("Refreshing state")] }));
|
||||
@@ -482,7 +849,7 @@ describe("EnvironmentsPanel", () => {
|
||||
backend(
|
||||
() =>
|
||||
scan({
|
||||
environments: [env({ onboarding: { state: "incomplete", lock }, onboarding_actions: ["unlock"] })],
|
||||
environments: [env({ onboarding: { state: "incomplete", lock, ec2: null }, onboarding_actions: ["unlock"] })],
|
||||
}),
|
||||
(cmd) => {
|
||||
if (cmd === "unlock_aws_state") return undefined;
|
||||
@@ -515,7 +882,7 @@ describe("EnvironmentsPanel", () => {
|
||||
scan({
|
||||
environments: [
|
||||
env({ name: "a", state: { state: "incomplete", lock: null }, actions: ["push_state"], info: null, onboarding: null }),
|
||||
env({ name: "b", onboarding: { state: "incomplete", lock: null }, onboarding_actions: ["push_state"] }),
|
||||
env({ name: "b", onboarding: { state: "incomplete", lock: null, ec2: null }, onboarding_actions: ["push_state"] }),
|
||||
],
|
||||
}),
|
||||
(cmd) => {
|
||||
|
||||
@@ -10,10 +10,13 @@ import {
|
||||
unlockAwsState,
|
||||
updateAwsEnvironment,
|
||||
} from "../../../lib/tauri-commands";
|
||||
import { awsErrorMessage, toAwsError } from "../../../lib/aws-error";
|
||||
import { awsErrorMessage, looksDenied, toAwsError } from "../../../lib/aws-error";
|
||||
import { clearEc2CapacityCache } from "../../../lib/ec2Capacity";
|
||||
import type {
|
||||
AwsError,
|
||||
DetectedEnvironment,
|
||||
Ec2Choice,
|
||||
Ec2Compute,
|
||||
EnvironmentScan,
|
||||
HostConnection,
|
||||
LockInfo,
|
||||
@@ -21,6 +24,7 @@ import type {
|
||||
NetworkChoice,
|
||||
PermissionSummary,
|
||||
PolicyFile,
|
||||
UserCompute,
|
||||
} from "../../../lib/types";
|
||||
import Button from "../../ui/Button";
|
||||
import Field, { inputClass, selectClass } from "../../ui/Field";
|
||||
@@ -29,16 +33,19 @@ import StatusIndicator from "../../ui/StatusIndicator";
|
||||
import AdminBundleModal from "./AdminBundleModal";
|
||||
import UpdateEnvironmentModal from "./UpdateEnvironmentModal";
|
||||
import DeployEnvironmentModal from "./DeployEnvironmentModal";
|
||||
import DeployProgress, { runKey, useDeployRuns, type DeployRun } from "./DeployProgress";
|
||||
import JoinEnvironmentModal from "./JoinEnvironmentModal";
|
||||
import DeployProgress, { runKey, useDeployRuns, type DeployRun, type RunKind } from "./DeployProgress";
|
||||
import PolicyFiles from "./PolicyFiles";
|
||||
import {
|
||||
COMMON_REGIONS,
|
||||
computeLine,
|
||||
environmentStateKey,
|
||||
formatAge,
|
||||
lockLine,
|
||||
onboardingStateKey,
|
||||
stateLock,
|
||||
stateStatus,
|
||||
UNCONFIRMED_NOTE,
|
||||
} from "./environmentFormat";
|
||||
|
||||
/** A typed confirmation: the action runs only once the environment's name is typed. */
|
||||
@@ -101,10 +108,46 @@ function TypedConfirmModal({
|
||||
);
|
||||
}
|
||||
|
||||
type Bundle = { kind: ModuleKind; region: string; environment?: string; ownerId?: string };
|
||||
type Bundle = {
|
||||
kind: ModuleKind;
|
||||
region: string;
|
||||
environment?: string;
|
||||
ownerId?: string;
|
||||
recordedEc2?: UserCompute | null;
|
||||
};
|
||||
/** A self-onboarding (or its re-apply) waiting for its EC2 choice. */
|
||||
type JoinOf = {
|
||||
region: string;
|
||||
name: string;
|
||||
ownerId: string;
|
||||
stateKey: string;
|
||||
recorded: UserCompute | null;
|
||||
reapply: boolean;
|
||||
incomplete: boolean;
|
||||
lastChoice: Ec2Compute | null;
|
||||
};
|
||||
|
||||
/** The caller's recorded EC2 capacity in `e`, if any. */
|
||||
const recordedEc2 = (e: DetectedEnvironment): UserCompute | null =>
|
||||
e.onboarding && (e.onboarding.state === "joined" || e.onboarding.state === "incomplete") ? e.onboarding.ec2 : null;
|
||||
/** A row-level key: the same environment name can exist in every region. */
|
||||
const rowKey = (region: string, name: string) => `${region}:${name}`;
|
||||
|
||||
/**
|
||||
* A failed new deploy or self-onboarding started here that AWS refused a
|
||||
* permission for: the bundle for an admin is the way on. Only those two
|
||||
* kinds: an environment bundle deploys a *new* environment, so a follow,
|
||||
* repair or update of an existing one is never offered it.
|
||||
*/
|
||||
function deniedRun(r: DeployRun): "environment" | "user_onboard" | null {
|
||||
if (r.kind !== "deploy" && r.kind !== "onboard") return null;
|
||||
if (!r.done || !r.result || r.result.ok) return null;
|
||||
const denied =
|
||||
looksDenied(r.result.summary) || r.events.some((e) => e.level === "error" && looksDenied(e.message));
|
||||
if (!denied) return null;
|
||||
return r.kind === "deploy" ? "environment" : "user_onboard";
|
||||
}
|
||||
|
||||
/** The environment a run's state key belongs to, if any. */
|
||||
function runEnvironment(stateKey: string): string | null {
|
||||
const m = /^(?:environments|users)\/([^/]+)\//.exec(stateKey);
|
||||
@@ -113,14 +156,16 @@ function runEnvironment(stateKey: string): string | null {
|
||||
|
||||
/**
|
||||
* The environments a connection can reach in a region (spec §2): detection,
|
||||
* each environment's state and actions, and deploy/join for this caller's tier.
|
||||
* each environment's state and actions, and deploy/join. Deploy and join are
|
||||
* offered to anyone (AWS has the last word and its refusal is shown); an
|
||||
* unconfirmed tier only adds a note and the bundle-for-an-admin alternative.
|
||||
*/
|
||||
export default function EnvironmentsPanel({
|
||||
connection,
|
||||
tier,
|
||||
}: {
|
||||
connection: HostConnection;
|
||||
/** The connection's last permission report; absent means unknown, treated as not an admin. */
|
||||
/** The connection's last permission report; absent means unknown (not confirmed as an admin). */
|
||||
tier?: PermissionSummary;
|
||||
}) {
|
||||
const isAdmin = tier === "environment_admin";
|
||||
@@ -145,7 +190,12 @@ export default function EnvironmentsPanel({
|
||||
onboarding: boolean;
|
||||
} | null>(null);
|
||||
const [bundle, setBundle] = useState<Bundle | null>(null);
|
||||
const [joinOf, setJoinOf] = useState<JoinOf | null>(null);
|
||||
// The EC2 capacity last chosen per environment in this run of the app: an
|
||||
// onboarding that didn't finish has no record to start from.
|
||||
const lastEc2 = useRef(new Map<string, Ec2Compute>());
|
||||
const [copyNote, setCopyNote] = useState<string | null>(null);
|
||||
const [notAdminOpen, setNotAdminOpen] = useState(false);
|
||||
const request = useRef(0);
|
||||
const followed = useRef(new Set<string>());
|
||||
|
||||
@@ -155,6 +205,9 @@ export default function EnvironmentsPanel({
|
||||
setDetectError(null);
|
||||
try {
|
||||
const found = await detectAwsEnvironments(connection.id, region);
|
||||
// A scan reads every user record afresh (a join may have changed the
|
||||
// EC2 capacity): Host settings ask again too.
|
||||
clearEc2CapacityCache();
|
||||
if (id === request.current) setScan(found);
|
||||
} catch (e) {
|
||||
if (id === request.current) {
|
||||
@@ -177,11 +230,18 @@ export default function EnvironmentsPanel({
|
||||
|
||||
/** Start a run in `runRegion` (always the region of the scan the action came from). */
|
||||
const launch = useCallback(
|
||||
async (runRegion: string, envName: string, stateKey: string, label: string, run: () => Promise<string>) => {
|
||||
async (
|
||||
runRegion: string,
|
||||
envName: string,
|
||||
stateKey: string,
|
||||
kind: RunKind,
|
||||
label: string,
|
||||
run: () => Promise<string>,
|
||||
) => {
|
||||
const rk = rowKey(runRegion, envName);
|
||||
setRowErrors(({ [rk]: _gone, ...rest }) => rest);
|
||||
setPending((p) => ({ ...p, [rk]: true }));
|
||||
const err = await start(stateKey, runRegion, label, run);
|
||||
const err = await start(stateKey, runRegion, kind, label, run);
|
||||
setPending(({ [rk]: _gone, ...rest }) => rest);
|
||||
if (err) setRowErrors((r) => ({ ...r, [rk]: err }));
|
||||
return err;
|
||||
@@ -197,7 +257,7 @@ export default function EnvironmentsPanel({
|
||||
const seen = runKey(scan.region, key);
|
||||
if (!runs[seen] && !followed.current.has(seen)) {
|
||||
followed.current.add(seen);
|
||||
void launch(scan.region, name, key, label, () => followAwsDeployment(connection.id, scan.region, key));
|
||||
void launch(scan.region, name, key, "follow", label, () => followAwsDeployment(connection.id, scan.region, key));
|
||||
}
|
||||
};
|
||||
for (const e of scan.environments) {
|
||||
@@ -228,27 +288,42 @@ export default function EnvironmentsPanel({
|
||||
const stale = detecting || !current;
|
||||
|
||||
const deploy = (runRegion: string, name: string, network: NetworkChoice, hours: number) =>
|
||||
launch(runRegion, name, environmentStateKey(name), `Deploy of ${name}`, () =>
|
||||
launch(runRegion, name, environmentStateKey(name), "deploy", `Deploy of ${name}`, () =>
|
||||
deployAwsEnvironment(connection.id, runRegion, name, network, hours),
|
||||
);
|
||||
// Without `hours`, the environment's recorded cap is kept.
|
||||
const repair = (runRegion: string, name: string, network?: NetworkChoice, hours?: number) =>
|
||||
launch(runRegion, name, environmentStateKey(name), `Repair of ${name}`, () =>
|
||||
launch(runRegion, name, environmentStateKey(name), "repair", `Repair of ${name}`, () =>
|
||||
repairAwsEnvironment(connection.id, runRegion, name, network, hours),
|
||||
);
|
||||
const update = (runRegion: string, name: string, hours: number) =>
|
||||
launch(runRegion, name, environmentStateKey(name), `Update of ${name}`, () =>
|
||||
launch(runRegion, name, environmentStateKey(name), "update", `Update of ${name}`, () =>
|
||||
updateAwsEnvironment(connection.id, runRegion, name, hours),
|
||||
);
|
||||
|
||||
// Join (or, once joined, a change of EC2 capacity): anyone chooses the
|
||||
// capacity and applies it; the modal offers a bundle for an admin instead.
|
||||
const join = (found: EnvironmentScan, e: DetectedEnvironment) => {
|
||||
if (isAdmin) {
|
||||
void launch(found.region, e.name, onboardingStateKey(e.name, found.owner_key), `Joining ${e.name}`, () =>
|
||||
onboardAwsUser(connection.id, found.region, e.name),
|
||||
);
|
||||
} else {
|
||||
setBundle({ kind: "user_onboard", region: found.region, environment: e.name, ownerId: found.owner_id });
|
||||
}
|
||||
setJoinOf({
|
||||
region: found.region,
|
||||
name: e.name,
|
||||
ownerId: found.owner_id,
|
||||
stateKey: onboardingStateKey(e.name, found.owner_key),
|
||||
recorded: recordedEc2(e),
|
||||
reapply: e.onboarding?.state === "joined",
|
||||
incomplete: e.onboarding?.state === "incomplete",
|
||||
lastChoice: lastEc2.current.get(rowKey(found.region, e.name)) ?? null,
|
||||
});
|
||||
};
|
||||
const onboardBundle = (region: string, environment: string, ownerId: string, recorded: UserCompute | null) =>
|
||||
setBundle({ kind: "user_onboard", region, environment, ownerId, recordedEc2: recorded });
|
||||
const runJoin = (j: JoinOf, ec2: Ec2Choice) => {
|
||||
const key = rowKey(j.region, j.name);
|
||||
if (ec2.kind === "ec2") {
|
||||
const { kind: _kind, ...c } = ec2;
|
||||
lastEc2.current.set(key, c);
|
||||
} else lastEc2.current.delete(key);
|
||||
return launch(j.region, j.name, j.stateKey, "onboard", `Joining ${j.name}`, () => onboardAwsUser(connection.id, j.region, j.name, ec2));
|
||||
};
|
||||
|
||||
const rowRuns = (name: string) =>
|
||||
@@ -257,14 +332,40 @@ export default function EnvironmentsPanel({
|
||||
const looseRuns = Object.values(runs).filter(
|
||||
(r) => r.region !== current?.region || !shown.has(runEnvironment(r.stateKey) ?? ""),
|
||||
);
|
||||
const progress = (r: DeployRun) => (
|
||||
<DeployProgress
|
||||
key={runKey(r.region, r.stateKey)}
|
||||
run={r}
|
||||
connectionId={connection.id}
|
||||
onDismiss={() => dismiss(runKey(r.region, r.stateKey))}
|
||||
/>
|
||||
);
|
||||
const progress = (r: DeployRun) => {
|
||||
const denied = deniedRun(r);
|
||||
const env = runEnvironment(r.stateKey);
|
||||
const found = current?.region === r.region ? current : null;
|
||||
const target = env && found ? found.environments.find((e) => e.name === env) : undefined;
|
||||
const offer =
|
||||
denied === "environment" ? true : denied === "user_onboard" ? !!(found && target) : false;
|
||||
return (
|
||||
<div key={runKey(r.region, r.stateKey)} className="space-y-1.5 min-w-0">
|
||||
<DeployProgress run={r} connectionId={connection.id} onDismiss={() => dismiss(runKey(r.region, r.stateKey))} />
|
||||
{offer && (
|
||||
<div className="space-y-1.5 text-xs text-[var(--text-secondary)] leading-snug" data-testid="denied-hint">
|
||||
<p className="break-words">
|
||||
AWS refused a permission this run needs (its message is above). This may need an admin:{" "}
|
||||
{denied === "environment"
|
||||
? "An admin can deploy the environment for you from a bundle."
|
||||
: "An admin can onboard you from a bundle."}
|
||||
</p>
|
||||
<Button
|
||||
size="sm"
|
||||
wrap
|
||||
onClick={() =>
|
||||
denied === "environment"
|
||||
? setBundle({ kind: "environment", region: r.region })
|
||||
: found && target && onboardBundle(r.region, target.name, found.owner_id, recordedEc2(target))
|
||||
}
|
||||
>
|
||||
Save a bundle for an admin…
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const envRow = (found: EnvironmentScan, e: DetectedEnvironment) => {
|
||||
const at = found.region;
|
||||
@@ -293,10 +394,10 @@ export default function EnvironmentsPanel({
|
||||
data-testid={`env-${e.name}`}
|
||||
className="border border-[var(--border-color)] rounded-[var(--radius-control)] p-2 space-y-1.5"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2 flex-wrap">
|
||||
<div className="min-w-0 space-y-0.5 text-xs">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="text-[13px] font-medium text-[var(--text-primary)]">{e.name}</span>
|
||||
<div className="flex items-start justify-between gap-2 flex-wrap" data-testid={`env-row-${e.name}`}>
|
||||
<div className="min-w-0 flex-1 basis-40 space-y-0.5 text-xs break-words">
|
||||
<div className="flex items-center gap-2 flex-wrap min-w-0">
|
||||
<span className="text-[13px] font-medium text-[var(--text-primary)] break-all">{e.name}</span>
|
||||
<StatusIndicator tone={status.tone} label={status.label} />
|
||||
{onboarding === "joined" && <StatusIndicator tone="ok" label="Joined" />}
|
||||
{onboarding === "in_progress" && <StatusIndicator tone="busy" label="Onboarding in progress" />}
|
||||
@@ -343,11 +444,19 @@ export default function EnvironmentsPanel({
|
||||
: "Your last onboarding didn't finish; Join again to finish it."}
|
||||
</div>
|
||||
)}
|
||||
{!isAdmin && isWaiting && onboarding !== "joined" && (
|
||||
{(onboarding === "joined" || recordedEc2(e)) && (
|
||||
<div className="text-[var(--text-secondary)]" data-testid={`compute-${e.name}`}>
|
||||
EC2 capacity: {(() => {
|
||||
const c = recordedEc2(e);
|
||||
return c ? computeLine(c) : "none (Fargate only)";
|
||||
})()}
|
||||
</div>
|
||||
)}
|
||||
{isWaiting && onboarding !== "joined" && (
|
||||
<div className="text-[var(--text-secondary)]">Waiting for an admin to onboard you</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-1.5 flex-wrap">
|
||||
<div className="flex gap-1.5 flex-wrap min-w-0 max-w-full" data-testid={`env-actions-${e.name}`}>
|
||||
{has("join") && onboarding !== "joined" && onboarding !== "in_progress" && (
|
||||
<Button
|
||||
size="sm"
|
||||
@@ -356,7 +465,18 @@ export default function EnvironmentsPanel({
|
||||
aria-label={`Join ${e.name}`}
|
||||
onClick={() => join(found, e)}
|
||||
>
|
||||
{!isAdmin && isWaiting ? "Show onboarding bundle" : "Join"}
|
||||
Join
|
||||
</Button>
|
||||
)}
|
||||
{has("join") && onboarding === "joined" && (
|
||||
<Button
|
||||
size="sm"
|
||||
wrap
|
||||
disabled={busy || joinBlocked}
|
||||
aria-label={`EC2 capacity in ${e.name}`}
|
||||
onClick={() => join(found, e)}
|
||||
>
|
||||
Change EC2 capacity
|
||||
</Button>
|
||||
)}
|
||||
{mine("follow") && (
|
||||
@@ -365,7 +485,7 @@ export default function EnvironmentsPanel({
|
||||
disabled={busy}
|
||||
aria-label={`Follow my onboarding to ${e.name}`}
|
||||
onClick={() =>
|
||||
void launch(at, e.name, myKey, `Joining ${e.name}`, () => followAwsDeployment(connection.id, at, myKey))
|
||||
void launch(at, e.name, myKey, "follow", `Joining ${e.name}`, () => followAwsDeployment(connection.id, at, myKey))
|
||||
}
|
||||
>
|
||||
Follow
|
||||
@@ -377,7 +497,7 @@ export default function EnvironmentsPanel({
|
||||
disabled={busy}
|
||||
aria-label={`Push saved state of my onboarding to ${e.name}`}
|
||||
onClick={() =>
|
||||
void launch(at, e.name, myKey, `Push of the saved state of joining ${e.name}`, () =>
|
||||
void launch(at, e.name, myKey, "push", `Push of the saved state of joining ${e.name}`, () =>
|
||||
pushAwsSavedState(connection.id, at, myKey),
|
||||
)
|
||||
}
|
||||
@@ -403,7 +523,7 @@ export default function EnvironmentsPanel({
|
||||
aria-label={`Push saved state of ${e.name}`}
|
||||
onClick={() => {
|
||||
const key = environmentStateKey(e.name);
|
||||
void launch(at, e.name, key, `Push of the saved state of ${e.name}`, () =>
|
||||
void launch(at, e.name, key, "push", `Push of the saved state of ${e.name}`, () =>
|
||||
pushAwsSavedState(connection.id, at, key),
|
||||
);
|
||||
}}
|
||||
@@ -418,7 +538,7 @@ export default function EnvironmentsPanel({
|
||||
aria-label={`Follow ${e.name}`}
|
||||
onClick={() => {
|
||||
const key = environmentStateKey(e.name);
|
||||
void launch(at, e.name, key, `Deploy of ${e.name}`, () => followAwsDeployment(connection.id, at, key));
|
||||
void launch(at, e.name, key, "follow", `Deploy of ${e.name}`, () => followAwsDeployment(connection.id, at, key));
|
||||
}}
|
||||
>
|
||||
Follow
|
||||
@@ -474,7 +594,7 @@ export default function EnvironmentsPanel({
|
||||
</div>
|
||||
</div>
|
||||
{rowError && (
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug">
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug break-words">
|
||||
{awsErrorMessage(rowError)}
|
||||
</p>
|
||||
)}
|
||||
@@ -486,14 +606,14 @@ export default function EnvironmentsPanel({
|
||||
const nothingFound = !!current && current.environments.length === 0;
|
||||
|
||||
return (
|
||||
<div className="pt-1.5 space-y-2">
|
||||
<div className="pt-1.5 space-y-2 min-w-0">
|
||||
<Button size="sm" variant="ghost" aria-expanded={open} onClick={() => setOpen((o) => !o)}>
|
||||
<span aria-hidden="true">{open ? "▾" : "▸"}</span>
|
||||
Environments
|
||||
</Button>
|
||||
{open && (
|
||||
<div className="space-y-2 pl-1">
|
||||
<div className="flex items-end gap-2 flex-wrap">
|
||||
<div className="space-y-2 pl-1 min-w-0">
|
||||
<div className="flex items-end gap-2 flex-wrap min-w-0" data-testid="region-row">
|
||||
<Field label="Region">
|
||||
{(id) => (
|
||||
<select id={id} className={selectClass} value={region} onChange={(e) => setRegion(e.target.value)}>
|
||||
@@ -515,11 +635,11 @@ export default function EnvironmentsPanel({
|
||||
</p>
|
||||
)}
|
||||
{detectError && (
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug">
|
||||
<p role="alert" className="text-xs text-[var(--error)] leading-snug break-words">
|
||||
{awsErrorMessage(detectError)}
|
||||
</p>
|
||||
)}
|
||||
{current && isAdmin && current.boundary === "no" && (
|
||||
{current && current.boundary === "no" && (
|
||||
<p className="text-xs text-[var(--text-secondary)] leading-snug">
|
||||
This account has no Triple-C permissions boundary yet; deploying creates it first.
|
||||
</p>
|
||||
@@ -530,32 +650,54 @@ export default function EnvironmentsPanel({
|
||||
{nothingFound && (
|
||||
<p className="text-xs text-[var(--text-secondary)] leading-snug">
|
||||
No Triple-C environment in {current.region}.
|
||||
{!isAdmin &&
|
||||
" Deploying one needs an environment admin: attach the environment-admin policies to an identity, or send an admin a bundle to deploy."}
|
||||
{!tier && " Run Check to find out whether you can deploy one yourself."}
|
||||
{!tier && " Run Check to see what your credentials can do."}
|
||||
</p>
|
||||
)}
|
||||
{current && isAdmin && (
|
||||
<Button
|
||||
size="md"
|
||||
variant={nothingFound ? "primary" : "secondary"}
|
||||
disabled={stale}
|
||||
onClick={() => setDeployOpen(true)}
|
||||
>
|
||||
Deploy a new environment
|
||||
</Button>
|
||||
)}
|
||||
{nothingFound && !isAdmin && (
|
||||
<div className="space-y-2">
|
||||
<PolicyFiles tier="environment_admin" label="Show the environment-admin policies" onCopy={copy} />
|
||||
<Button size="md" disabled={stale} onClick={() => setBundle({ kind: "environment", region: current.region })}>
|
||||
Request an environment from an admin…
|
||||
{current && (
|
||||
<div className="space-y-1.5 min-w-0" data-testid="deploy-section">
|
||||
<Button
|
||||
size="md"
|
||||
wrap
|
||||
variant={nothingFound ? "primary" : "secondary"}
|
||||
disabled={stale}
|
||||
onClick={() => setDeployOpen(true)}
|
||||
>
|
||||
Deploy a new environment
|
||||
</Button>
|
||||
{copyNote && (
|
||||
<p role="status" className="text-xs text-[var(--text-secondary)]">
|
||||
{copyNote}
|
||||
{!isAdmin && (
|
||||
<p className="text-xs text-[var(--text-secondary)] leading-snug break-words" data-testid="deploy-unconfirmed-note">
|
||||
{UNCONFIRMED_NOTE}
|
||||
</p>
|
||||
)}
|
||||
{!isAdmin && (
|
||||
<details
|
||||
className="text-xs min-w-0"
|
||||
data-testid="not-admin-options"
|
||||
open={notAdminOpen}
|
||||
onToggle={(ev) => setNotAdminOpen(ev.currentTarget.open)}
|
||||
>
|
||||
<summary className="cursor-pointer list-none [&::-webkit-details-marker]:hidden text-[var(--text-primary)] break-words">
|
||||
<span aria-hidden="true" className="mr-1">{notAdminOpen ? "▾" : "▸"}</span>
|
||||
Not an admin? Ask one to deploy it
|
||||
</summary>
|
||||
<div className="mt-1.5 space-y-2 min-w-0">
|
||||
<Button
|
||||
size="sm"
|
||||
wrap
|
||||
disabled={stale}
|
||||
onClick={() => setBundle({ kind: "environment", region: current.region })}
|
||||
>
|
||||
Request an environment from an admin…
|
||||
</Button>
|
||||
<PolicyFiles tier="environment_admin" label="Show the environment-admin policies" onCopy={copy} />
|
||||
{copyNote && (
|
||||
<p role="status" className="text-xs text-[var(--text-secondary)] break-words">
|
||||
{copyNote}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</details>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{looseRuns.map(progress)}
|
||||
@@ -575,6 +717,12 @@ export default function EnvironmentsPanel({
|
||||
repairOf ? repair(current.region, name, network, hours) : deploy(current.region, name, network, hours)
|
||||
}
|
||||
onDetect={() => void detect()}
|
||||
onBundle={() => {
|
||||
const at = current.region;
|
||||
setDeployOpen(false);
|
||||
setRepairOf(null);
|
||||
setBundle({ kind: "environment", region: at });
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{updateOf && (
|
||||
@@ -593,7 +741,7 @@ export default function EnvironmentsPanel({
|
||||
confirmLabel="Destroy"
|
||||
onClose={() => setDestroyOf(null)}
|
||||
onConfirm={() =>
|
||||
launch(destroyOf.region, destroyOf.name, environmentStateKey(destroyOf.name), `Destroy of ${destroyOf.name}`, () =>
|
||||
launch(destroyOf.region, destroyOf.name, environmentStateKey(destroyOf.name), "destroy", `Destroy of ${destroyOf.name}`, () =>
|
||||
destroyIncompleteAwsEnvironment(connection.id, destroyOf.region, destroyOf.name),
|
||||
)
|
||||
}
|
||||
@@ -636,6 +784,24 @@ export default function EnvironmentsPanel({
|
||||
</p>
|
||||
</TypedConfirmModal>
|
||||
)}
|
||||
{joinOf && (
|
||||
<JoinEnvironmentModal
|
||||
environment={joinOf.name}
|
||||
region={joinOf.region}
|
||||
recorded={joinOf.recorded}
|
||||
reapply={joinOf.reapply}
|
||||
incomplete={joinOf.incomplete}
|
||||
lastChoice={joinOf.lastChoice}
|
||||
confirmedAdmin={isAdmin}
|
||||
onBundle={() => {
|
||||
const j = joinOf;
|
||||
setJoinOf(null);
|
||||
onboardBundle(j.region, j.name, j.ownerId, j.recorded);
|
||||
}}
|
||||
onJoin={(ec2) => runJoin(joinOf, ec2)}
|
||||
onClose={() => setJoinOf(null)}
|
||||
/>
|
||||
)}
|
||||
{bundle && (
|
||||
<AdminBundleModal
|
||||
connectionId={connection.id}
|
||||
@@ -643,6 +809,7 @@ export default function EnvironmentsPanel({
|
||||
kind={bundle.kind}
|
||||
environment={bundle.environment}
|
||||
ownerId={bundle.ownerId}
|
||||
recordedEc2={bundle.recordedEc2}
|
||||
onSaved={() => {
|
||||
if (bundle.kind === "user_onboard" && bundle.environment) {
|
||||
const rk = rowKey(bundle.region, bundle.environment);
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import { useState } from "react";
|
||||
import { awsErrorMessage } from "../../../lib/aws-error";
|
||||
import type { AwsError, Ec2Choice, Ec2Compute, UserCompute } from "../../../lib/types";
|
||||
import Button from "../../ui/Button";
|
||||
import Modal from "../../ui/Modal";
|
||||
import Ec2CapacityFields from "./Ec2CapacityFields";
|
||||
import { ec2DraftFrom, ec2FromDraft, UNCONFIRMED_NOTE, type Ec2Draft } from "./environmentFormat";
|
||||
|
||||
/**
|
||||
* Self-onboarding: join an environment, or re-apply an onboarding to change
|
||||
* its EC2 capacity. Starts from the recorded capacity, so a re-apply keeps it
|
||||
* unless changed. Open to anyone: AWS refuses the run when a permission is
|
||||
* missing, and an onboarding bundle for an admin is the alternative.
|
||||
*/
|
||||
export default function JoinEnvironmentModal({
|
||||
environment,
|
||||
region,
|
||||
recorded,
|
||||
reapply,
|
||||
incomplete = false,
|
||||
lastChoice = null,
|
||||
confirmedAdmin = true,
|
||||
onBundle,
|
||||
onJoin,
|
||||
onClose,
|
||||
}: {
|
||||
environment: string;
|
||||
region: string;
|
||||
/** The caller's recorded EC2 capacity, if any. */
|
||||
recorded: UserCompute | null;
|
||||
/** Already joined: this changes the onboarding's EC2 capacity. */
|
||||
reapply: boolean;
|
||||
/** The last onboarding didn't finish (it may have built EC2 capacity
|
||||
* before writing the record). */
|
||||
incomplete?: boolean;
|
||||
/** The EC2 capacity last chosen here in this run of the app, if any:
|
||||
* the starting point when there is no record. */
|
||||
lastChoice?: Ec2Compute | null;
|
||||
/** The permission check confirmed environment-admin rights; otherwise a note says AWS may refuse. */
|
||||
confirmedAdmin?: boolean;
|
||||
/** Switch to an onboarding bundle for an admin instead. */
|
||||
onBundle?: () => void;
|
||||
/** Starts the run; resolves to the command's error, or null once started. */
|
||||
onJoin: (ec2: Ec2Choice) => Promise<AwsError | null>;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const [draft, setDraft] = useState<Ec2Draft>(() => ec2DraftFrom(recorded ?? lastChoice));
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<AwsError | null>(null);
|
||||
const picked = ec2FromDraft(draft);
|
||||
const ready = "compute" in picked && !submitting;
|
||||
const verb = reapply ? "Apply" : "Join";
|
||||
|
||||
const submit = async () => {
|
||||
if (!("compute" in picked)) return;
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
const choice: Ec2Choice = picked.compute ? { kind: "ec2", ...picked.compute } : { kind: "none" };
|
||||
const err = await onJoin(choice);
|
||||
setSubmitting(false);
|
||||
if (err) setError(err);
|
||||
else onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={reapply ? `EC2 capacity in ${environment}` : `Join ${environment}`}
|
||||
description={`In ${region}.`}
|
||||
onClose={onClose}
|
||||
footer={
|
||||
<>
|
||||
<Button size="md" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button size="md" variant="primary" onClick={submit} disabled={!ready}>
|
||||
{submitting ? `${verb}ing…` : verb}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="space-y-3 text-xs text-[var(--text-secondary)] leading-snug">
|
||||
{!reapply && <p>Joining creates your own task role and file system in {environment}.</p>}
|
||||
{!confirmedAdmin && <p data-testid="join-unconfirmed-note">{UNCONFIRMED_NOTE}</p>}
|
||||
{recorded && (
|
||||
<p>Stop your EC2 workspaces before removing EC2 capacity or changing your triple-c-user value: that change can't finish while one runs.</p>
|
||||
)}
|
||||
<Ec2CapacityFields value={draft} onChange={setDraft} />
|
||||
{recorded && !draft.enabled && (
|
||||
<p role="alert" className="text-[var(--warning)]">
|
||||
This removes your EC2 capacity ({recorded.instance_type}) in {environment}.
|
||||
</p>
|
||||
)}
|
||||
{incomplete && !recorded && !draft.enabled && (
|
||||
<p role="alert" className="text-[var(--warning)]">
|
||||
Your last onboarding in {environment} didn't finish, and it may have built part of your EC2 capacity.
|
||||
Choosing None removes anything it built; to keep it, choose your own EC2 instances with the same values.
|
||||
</p>
|
||||
)}
|
||||
{error && (
|
||||
<p role="alert" className="text-[var(--error)] break-words">
|
||||
{awsErrorMessage(error)}
|
||||
</p>
|
||||
)}
|
||||
{onBundle && (
|
||||
<div className="space-y-1.5 pt-1 border-t border-[var(--border-color)]">
|
||||
<p>
|
||||
{error?.kind === "access_denied"
|
||||
? "AWS refused this with your permissions. An admin can onboard you from a bundle instead."
|
||||
: "Can't onboard yourself? An admin can onboard you from a bundle."}
|
||||
</p>
|
||||
<Button size="sm" wrap onClick={onBundle}>
|
||||
Save an onboarding bundle for an admin…
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
@@ -28,6 +28,7 @@ export default function NetworkFields({
|
||||
<div className="text-[13px] font-medium text-[var(--text-primary)]">Network</div>
|
||||
<SegmentedControl<NetworkChoice["mode"]>
|
||||
label="Network"
|
||||
className="flex-wrap max-w-full"
|
||||
value={value.mode}
|
||||
onChange={(mode) => set({ mode })}
|
||||
segments={[
|
||||
|
||||
@@ -28,12 +28,12 @@ export default function PolicyFiles({
|
||||
|
||||
if (!files) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<Button size="sm" onClick={load}>
|
||||
<div className="space-y-1 min-w-0">
|
||||
<Button size="sm" wrap onClick={load}>
|
||||
{label}
|
||||
</Button>
|
||||
{error && (
|
||||
<p role="alert" className="text-xs text-[var(--error)]">
|
||||
<p role="alert" className="text-xs text-[var(--error)] break-words">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
@@ -41,30 +41,38 @@ export default function PolicyFiles({
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="space-y-1 text-xs">
|
||||
<p className="text-[var(--text-secondary)]">
|
||||
<div className="space-y-1.5 text-xs min-w-0">
|
||||
<p className="text-[var(--text-secondary)] leading-snug break-words">
|
||||
{tier === "workspace_user" ? "Workspace user" : "Environment admin"}:{" "}
|
||||
{files.length === 1
|
||||
? "attach this file as a customer managed policy."
|
||||
: `Attach all ${files.length} files, each as its own customer managed policy.`}
|
||||
</p>
|
||||
{/* One copy button per file, in a grid that drops to one column when narrow. */}
|
||||
<div data-testid="policy-copy-grid" className="grid gap-1.5 grid-cols-[repeat(auto-fill,minmax(9rem,1fr))]">
|
||||
{files.map((f) => (
|
||||
<Button
|
||||
key={f.name}
|
||||
size="sm"
|
||||
wrap
|
||||
className="w-full justify-start break-all"
|
||||
aria-label={`Copy ${f.name}`}
|
||||
onClick={() => onCopy(f)}
|
||||
>
|
||||
Copy <span className="font-mono">{f.name}</span>
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
{files.map((f) => (
|
||||
<div key={f.name} className="space-y-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-mono text-[var(--text-primary)]">{f.name}</span>
|
||||
<Button size="sm" aria-label={`Copy ${f.name}`} onClick={() => onCopy(f)}>
|
||||
Copy
|
||||
</Button>
|
||||
</div>
|
||||
<details>
|
||||
<summary className="cursor-pointer text-[var(--text-secondary)]">Show JSON</summary>
|
||||
<pre className="mt-1 max-h-48 overflow-auto whitespace-pre-wrap break-all text-[var(--text-secondary)]">
|
||||
{f.json}
|
||||
</pre>
|
||||
</details>
|
||||
</div>
|
||||
<details key={f.name} className="min-w-0">
|
||||
<summary className="cursor-pointer text-[var(--text-secondary)] break-all">
|
||||
Show JSON of <span className="font-mono">{f.name}</span>
|
||||
</summary>
|
||||
<pre className="mt-1 max-h-48 max-w-full overflow-auto whitespace-pre-wrap break-all text-[var(--text-secondary)]">
|
||||
{f.json}
|
||||
</pre>
|
||||
</details>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Pure helpers shared by the environments UI. The name and id rules mirror
|
||||
// `aws/tofu_modules.rs`; the server re-validates everything.
|
||||
import type { EnvironmentState, LockInfo, NetworkChoice } from "../../../lib/types";
|
||||
import type { Ec2Compute, EnvironmentState, LockInfo, NetworkChoice, UserCompute } from "../../../lib/types";
|
||||
import type { StatusTone } from "../../ui/StatusIndicator";
|
||||
|
||||
export const COMMON_REGIONS = [
|
||||
@@ -97,3 +97,53 @@ export function stateStatus(s: EnvironmentState): { tone: StatusTone; label: str
|
||||
export function stateLock(s: EnvironmentState): LockInfo | null {
|
||||
return s.state === "deploying" || s.state === "incomplete" ? s.lock : null;
|
||||
}
|
||||
|
||||
/** The most instances a user's EC2 capacity may run at once (`EC2_MAX_INSTANCES`). */
|
||||
export const EC2_MAX_INSTANCES = 4;
|
||||
|
||||
/** The EC2 capacity picker's fields. An empty `instanceType` means the table's default. */
|
||||
export interface Ec2Draft {
|
||||
enabled: boolean;
|
||||
instanceType: string;
|
||||
maxInstances: number;
|
||||
userTag: string;
|
||||
}
|
||||
|
||||
/** The picker's start: the recorded capacity (a re-apply keeps it), or none. */
|
||||
export function ec2DraftFrom(
|
||||
recorded: Pick<UserCompute, "instance_type" | "max_instances" | "user_tag"> | null | undefined,
|
||||
): Ec2Draft {
|
||||
return recorded
|
||||
? { enabled: true, instanceType: recorded.instance_type, maxInstances: recorded.max_instances, userTag: recorded.user_tag }
|
||||
: { enabled: false, instanceType: "", maxInstances: 1, userTag: "" };
|
||||
}
|
||||
|
||||
/** `validate_ec2_user_tag`: null when valid, else why not. */
|
||||
export function userTagError(tag: string): string | null {
|
||||
if (!tag) return "Enter your triple-c-user value.";
|
||||
const ok = /^[A-Za-z0-9-]{1,32}$/.test(tag) && !tag.startsWith("-") && !tag.endsWith("-") && !tag.includes("--");
|
||||
return ok ? null : "Use 1 to 32 letters, digits and single hyphens, not starting or ending with a hyphen.";
|
||||
}
|
||||
|
||||
/** The capacity a draft asks for (null = none), or why it can't be used yet. */
|
||||
export function ec2FromDraft(d: Ec2Draft): { compute: Ec2Compute | null } | { error: string } {
|
||||
if (!d.enabled) return { compute: null };
|
||||
if (!d.instanceType) return { error: "Choose an instance type." };
|
||||
if (!Number.isInteger(d.maxInstances) || d.maxInstances < 1 || d.maxInstances > EC2_MAX_INSTANCES) {
|
||||
return { error: `Choose 1 to ${EC2_MAX_INSTANCES} instances.` };
|
||||
}
|
||||
const tag = d.userTag.trim();
|
||||
const tagError = userTagError(tag);
|
||||
if (tagError) return { error: tagError };
|
||||
return { compute: { instance_type: d.instanceType, max_instances: d.maxInstances, user_tag: tag } };
|
||||
}
|
||||
|
||||
/** "m7g.large, up to 3 instances (triple-c-user alice)". */
|
||||
export function computeLine(c: UserCompute): string {
|
||||
const n = c.max_instances === 1 ? "1 instance" : `up to ${c.max_instances} instances`;
|
||||
return `${c.instance_type}, ${n} (triple-c-user ${c.user_tag})`;
|
||||
}
|
||||
|
||||
/** Shown under an action whose permissions the check didn't confirm: AWS has the last word. */
|
||||
export const UNCONFIRMED_NOTE =
|
||||
"Your permission check didn't confirm environment-admin rights; the deploy will stop with AWS's message if a permission is missing.";
|
||||
@@ -2,10 +2,12 @@
|
||||
import type {
|
||||
DeployProgress,
|
||||
DetectedEnvironment,
|
||||
Ec2InstanceTable,
|
||||
EnvironmentInfo,
|
||||
EnvironmentScan,
|
||||
HostConnection,
|
||||
TofuEvent,
|
||||
UserCompute,
|
||||
} from "../../../lib/types";
|
||||
|
||||
export const conn: HostConnection = {
|
||||
@@ -65,3 +67,26 @@ export function tofuEvent(message: string, over: Partial<TofuEvent> = {}): TofuE
|
||||
export function progress(over: Partial<DeployProgress>): DeployProgress {
|
||||
return { op_id: "op1", state_key: "environments/dev/terraform.tfstate", events: [], done: false, cancellable: true, ...over };
|
||||
}
|
||||
|
||||
/** A slice of `list_ec2_instance_types`'s answer: x86 and ARM rows. */
|
||||
export const instanceTable: Ec2InstanceTable = {
|
||||
default_type: "m7i.large",
|
||||
types: [
|
||||
{ instance_type: "m7i.large", arch: "x86_64", vcpu: 2, memory_mib: 8192 },
|
||||
{ instance_type: "m7i.xlarge", arch: "x86_64", vcpu: 4, memory_mib: 16384 },
|
||||
{ instance_type: "m7g.large", arch: "arm64", vcpu: 2, memory_mib: 8192 },
|
||||
{ instance_type: "m7g.xlarge", arch: "arm64", vcpu: 4, memory_mib: 16384 },
|
||||
],
|
||||
};
|
||||
|
||||
export function compute(over: Partial<UserCompute> = {}): UserCompute {
|
||||
const tag = over.user_tag ?? "alice";
|
||||
return {
|
||||
instance_type: "m7g.xlarge",
|
||||
max_instances: 3,
|
||||
user_tag: tag,
|
||||
cluster_arn: `arn:aws:ecs:eu-west-1:123456789012:cluster/triple-c-dev--u-${tag}`,
|
||||
capacity_provider: `triple-c-dev--u-${tag}`,
|
||||
...over,
|
||||
};
|
||||
}
|
||||
Loaded 100 of 149 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user