Skip to content

Commit 6b34ac3

Browse files
authored
Merge pull request #129 from flatrun/feat/runs-runtime-picker
ui(runs): Runtime picker for new runs
2 parents b54bd2b + 796fe18 commit 6b34ac3

8 files changed

Lines changed: 188 additions & 7 deletions

File tree

‎src/components/runs/NewRunModal.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import { mount, flushPromises } from "@vue/test-utils";
33
import NewRunModal from "./NewRunModal.vue";
44
import { runsApi } from "@/services/api";
55
import running from "@/views/fixtures/runs/run-running.json";
6+
import unconfigured from "@/views/fixtures/runs/runtimes-firecracker-unconfigured.json";
7+
import firecrackerOnly from "@/views/fixtures/runs/runtimes-firecracker-only.json";
68

79
vi.mock("@/services/api", () => ({
8-
runsApi: { create: vi.fn() },
10+
runsApi: { create: vi.fn(), runtimes: vi.fn() },
911
}));
1012

1113
const create = runsApi.create as unknown as ReturnType<typeof vi.fn>;
14+
const runtimes = runsApi.runtimes as unknown as ReturnType<typeof vi.fn>;
1215

1316
function mountModal() {
1417
return mount(NewRunModal, {
@@ -33,6 +36,7 @@ describe("NewRunModal", () => {
3336
beforeEach(() => {
3437
vi.clearAllMocks();
3538
create.mockResolvedValue({ data: running });
39+
runtimes.mockResolvedValue({ data: unconfigured });
3640
});
3741

3842
it("sends the run in the shape the agent accepts", async () => {
@@ -53,6 +57,7 @@ describe("NewRunModal", () => {
5357
await submit(wrapper);
5458

5559
expect(create).toHaveBeenCalledWith({
60+
runtime: "docker",
5661
image: "busybox:latest",
5762
command: ["sh", "-c", "echo waiting; sleep 30"],
5863
env: undefined,
@@ -123,4 +128,72 @@ describe("NewRunModal", () => {
123128

124129
expect(wrapper.find(".run-form-error").text()).toBe("timeout must be positive and at most one hour");
125130
});
131+
132+
it("offers only the runtimes this node can run", async () => {
133+
const wrapper = mountModal();
134+
await flushPromises();
135+
136+
const options = wrapper.findAll("option").filter((o) => ["docker", "firecracker"].includes(o.attributes("value")!));
137+
expect(options.map((o) => [o.text(), o.attributes("disabled") !== undefined])).toEqual([
138+
["Docker container", false],
139+
["Firecracker microVM (not set up on this node)", true],
140+
]);
141+
});
142+
143+
it("falls back to Docker when the agent cannot list runtimes", async () => {
144+
runtimes.mockRejectedValue({ response: { status: 404 } });
145+
const wrapper = mountModal();
146+
await flushPromises();
147+
await fill(wrapper, "Image", "busybox:latest");
148+
await submit(wrapper);
149+
150+
expect(create.mock.calls[0][0].runtime).toBe("docker");
151+
});
152+
153+
it("asks for a runtime instead of switching to one the user did not pick", async () => {
154+
runtimes.mockResolvedValue({ data: firecrackerOnly });
155+
const wrapper = mountModal();
156+
await flushPromises();
157+
await fill(wrapper, "Image", "busybox:latest");
158+
await submit(wrapper);
159+
expect(wrapper.find(".run-form-error").text()).toBe("Choose a runtime.");
160+
expect(create).not.toHaveBeenCalled();
161+
162+
await wrapper.findAll("select")[0].setValue("firecracker");
163+
await submit(wrapper);
164+
expect(create.mock.calls[0][0].runtime).toBe("firecracker");
165+
});
166+
167+
it("keeps a chosen Firecracker run when the runtime list cannot be loaded again", async () => {
168+
runtimes.mockResolvedValue({ data: firecrackerOnly });
169+
const wrapper = mountModal();
170+
await flushPromises();
171+
await wrapper.findAll("select")[0].setValue("firecracker");
172+
173+
runtimes.mockRejectedValue({ response: { status: 500 } });
174+
await wrapper.setProps({ visible: false });
175+
await wrapper.setProps({ visible: true });
176+
await flushPromises();
177+
await fill(wrapper, "Image", "busybox:latest");
178+
await submit(wrapper);
179+
180+
expect(create.mock.calls[0][0].runtime).toBe("firecracker");
181+
});
182+
183+
it("asks again when the chosen runtime stops being available", async () => {
184+
runtimes.mockResolvedValue({ data: firecrackerOnly });
185+
const wrapper = mountModal();
186+
await flushPromises();
187+
await wrapper.findAll("select")[0].setValue("firecracker");
188+
189+
runtimes.mockResolvedValue({ data: unconfigured });
190+
await wrapper.setProps({ visible: false });
191+
await wrapper.setProps({ visible: true });
192+
await flushPromises();
193+
await fill(wrapper, "Image", "busybox:latest");
194+
await submit(wrapper);
195+
196+
expect(wrapper.find(".run-form-error").text()).toBe("Choose a runtime.");
197+
expect(create).not.toHaveBeenCalled();
198+
});
126199
});

‎src/components/runs/NewRunModal.vue‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
<BaseInput v-model="image" placeholder="e.g. node:22-alpine" />
66
</BaseField>
77

8+
<BaseField label="Runtime" :hint="runtimeHint(runtime)">
9+
<BaseSelect v-model="runtime">
10+
<option v-if="!runtime" value="" disabled>Choose a runtime</option>
11+
<option v-for="option in runtimes" :key="option.type" :value="option.type" :disabled="!option.available">
12+
{{ runtimeOptionLabel(option) }}
13+
</option>
14+
</BaseSelect>
15+
</BaseField>
16+
817
<BaseField label="Command" hint="Runs with sh -c. Leave empty to use the image's default command.">
918
<BaseTextarea v-model="command" :rows="4" placeholder="e.g. npm ci && npm test" />
1019
</BaseField>
@@ -19,10 +28,10 @@
1928
<option value="egress">Outbound internet</option>
2029
</BaseSelect>
2130
</BaseField>
22-
<BaseField label="CPUs" hint="Empty for no limit.">
31+
<BaseField label="CPUs" :hint="isVm ? 'Rounded up to whole vCPUs. Empty for one.' : 'Empty for no limit.'">
2332
<BaseInput v-model="cpus" type="number" />
2433
</BaseField>
25-
<BaseField label="Memory (MB)" hint="Empty for no limit.">
34+
<BaseField label="Memory (MB)" :hint="isVm ? 'Empty for the node default.' : 'Empty for no limit.'">
2635
<BaseInput v-model="memoryMb" type="number" />
2736
</BaseField>
2837
</div>
@@ -64,19 +73,23 @@
6473
</template>
6574

6675
<script setup lang="ts">
67-
import { ref, watch } from "vue";
76+
import { computed, ref, watch } from "vue";
6877
import BaseModal from "@/components/base/BaseModal.vue";
6978
import BaseField from "@/components/base/BaseField.vue";
7079
import BaseInput from "@/components/base/BaseInput.vue";
7180
import BaseSelect from "@/components/base/BaseSelect.vue";
7281
import BaseTextarea from "@/components/base/BaseTextarea.vue";
7382
import BaseButton from "@/components/base/BaseButton.vue";
7483
import KeyValueRows, { type KeyValueRow } from "./KeyValueRows.vue";
75-
import { runsApi, type CreateRunRequest, type Run } from "@/services/api";
84+
import { runsApi, type CreateRunRequest, type Run, type RunRuntime } from "@/services/api";
85+
import { chosenRuntime, defaultRuntimes, runtimeHint, runtimeOptionLabel } from "@/model/runs";
7686
7787
const props = defineProps<{ visible: boolean }>();
7888
const emit = defineEmits<{ close: []; created: [run: Run] }>();
7989
90+
const runtimes = ref<RunRuntime[]>(defaultRuntimes);
91+
const runtime = ref("docker");
92+
const isVm = computed(() => runtime.value === "firecracker");
8093
const image = ref("");
8194
const command = ref("");
8295
const timeoutMinutes = ref<string | number>(10);
@@ -109,9 +122,22 @@ watch(
109122
(open) => {
110123
error.value = "";
111124
if (!open) secretEnv.value = [];
125+
else loadRuntimes();
112126
},
127+
{ immediate: true },
113128
);
114129
130+
async function loadRuntimes() {
131+
try {
132+
const res = await runsApi.runtimes();
133+
if (!res.data.items.length) return;
134+
runtimes.value = res.data.items;
135+
} catch {
136+
return;
137+
}
138+
runtime.value = chosenRuntime(runtimes.value, runtime.value);
139+
}
140+
115141
function toMap(rows: KeyValueRow[]): Record<string, string> | undefined {
116142
const named = rows.filter((r) => r.key.trim() !== "");
117143
if (!named.length) return undefined;
@@ -127,6 +153,7 @@ function optionalNumber(value: string | number): number | undefined | null {
127153
function buildRequest(): CreateRunRequest | string {
128154
const reference = image.value.trim();
129155
if (!reference) return "Image is required.";
156+
if (!runtime.value) return "Choose a runtime.";
130157
const minutes = Number(timeoutMinutes.value);
131158
if (!Number.isInteger(minutes) || minutes < 1 || minutes > 60) return "Timeout must be a whole number from 1 to 60.";
132159
const paths = artifactPaths.value
@@ -141,6 +168,7 @@ function buildRequest(): CreateRunRequest | string {
141168
if (memory === null) return "Memory must be a positive number of MB, or empty for no limit.";
142169
const script = command.value.trim();
143170
return {
171+
runtime: runtime.value,
144172
image: reference,
145173
command: script ? ["sh", "-c", script] : undefined,
146174
env: toMap(env.value),

‎src/model/runs.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { describe, it, expect } from "vitest";
22
import type { Run } from "@/services/api";
3-
import { commandText, isActive, limitsText, outputEmptyMessage, runDuration, runStatuses, statusTone } from "./runs";
3+
import {
4+
chosenRuntime,
5+
commandText,
6+
isActive,
7+
limitsText,
8+
outputEmptyMessage,
9+
runDuration,
10+
runStatuses,
11+
runtimeOptionLabel,
12+
statusTone,
13+
} from "./runs";
14+
import unconfigured from "@/views/fixtures/runs/runtimes-firecracker-unconfigured.json";
415
import running from "@/views/fixtures/runs/run-running.json";
516
import succeeded from "@/views/fixtures/runs/run-succeeded.json";
617
import failed from "@/views/fixtures/runs/run-failed.json";
@@ -48,4 +59,17 @@ describe("runs model", () => {
4859
expect(outputEmptyMessage(asRun({ status: "running" }))).toMatch(/Waiting for output/);
4960
expect(outputEmptyMessage(asRun({ status: "succeeded" }))).toMatch(/no output/);
5061
});
62+
63+
it("never swaps a chosen runtime for another one", () => {
64+
expect(chosenRuntime(unconfigured.items, "docker")).toBe("docker");
65+
expect(chosenRuntime(unconfigured.items, "firecracker")).toBe("");
66+
expect(chosenRuntime([], "docker")).toBe("");
67+
});
68+
69+
it("marks runtimes the node cannot run", () => {
70+
expect(unconfigured.items.map(runtimeOptionLabel)).toEqual([
71+
"Docker container",
72+
"Firecracker microVM (not set up on this node)",
73+
]);
74+
});
5175
});

‎src/model/runs.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Run, RunStatus } from "@/services/api";
1+
import type { Run, RunRuntime, RunStatus } from "@/services/api";
22
import { duration } from "@/utils/time";
33

44
export const runStatuses: RunStatus[] = [
@@ -66,3 +66,32 @@ export function outputEmptyMessage(run: Run): string {
6666
if (run.status === "running") return "Waiting for output...";
6767
return "This run wrote no output.";
6868
}
69+
70+
const runtimeNames: Record<string, string> = {
71+
docker: "Docker container",
72+
firecracker: "Firecracker microVM",
73+
};
74+
75+
const runtimeHints: Record<string, string> = {
76+
docker: "Shares the host kernel. Use it for code you trust.",
77+
firecracker: "Runs in a microVM with its own kernel. Use it for code you do not trust.",
78+
};
79+
80+
export const defaultRuntimes: RunRuntime[] = [{ type: "docker", available: true }];
81+
82+
export function runtimeLabel(type: string): string {
83+
return runtimeNames[type] ?? type;
84+
}
85+
86+
export function runtimeOptionLabel(runtime: RunRuntime): string {
87+
const name = runtimeLabel(runtime.type);
88+
return runtime.available ? name : `${name} (not set up on this node)`;
89+
}
90+
91+
export function runtimeHint(type: string): string {
92+
return runtimeHints[type] ?? "";
93+
}
94+
95+
export function chosenRuntime(runtimes: RunRuntime[], current: string): string {
96+
return runtimes.some((r) => r.available && r.type === current) ? current : "";
97+
}

‎src/services/api.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,14 @@ export interface Run {
22802280
finished_at?: string;
22812281
}
22822282

2283+
export interface RunRuntime {
2284+
type: string;
2285+
available: boolean;
2286+
reason?: string;
2287+
}
2288+
22832289
export interface CreateRunRequest {
2290+
runtime?: string;
22842291
image: string;
22852292
command?: string[];
22862293
env?: Record<string, string>;
@@ -2296,6 +2303,7 @@ export interface CreateRunRequest {
22962303
export const runsApi = {
22972304
list: (limit?: number) => apiClient.get<{ items: Run[] }>("/runs", { params: limit ? { limit } : undefined }),
22982305
get: (id: string) => apiClient.get<Run>(`/runs/${encodeURIComponent(id)}`),
2306+
runtimes: () => apiClient.get<{ items: RunRuntime[] }>("/runs/runtimes"),
22992307
create: (data: CreateRunRequest) => apiClient.post<Run>("/runs", data),
23002308
cancel: (id: string) => apiClient.delete<Run>(`/runs/${encodeURIComponent(id)}`),
23012309
logs: (id: string) =>

‎src/views/RunsView.vue‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
<dd>{{ runDuration(selected) }}</dd>
120120
<dt>Timeout</dt>
121121
<dd>{{ Math.round(selected.spec.timeout_seconds / 60) }} min</dd>
122+
<dt>Runtime</dt>
123+
<dd>{{ runtimeLabel(selected.runtime) }}</dd>
122124
<dt>Network</dt>
123125
<dd>{{ selected.spec.egress ? "Outbound internet" : "No network" }}</dd>
124126
<dt>Limits</dt>
@@ -199,6 +201,7 @@ import {
199201
commandText,
200202
isActive,
201203
limitsText,
204+
runtimeLabel,
202205
outputEmptyMessage,
203206
runDuration,
204207
runStatuses,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"items": [
3+
{ "type": "docker", "available": false, "reason": "runtime unavailable: docker: not configured on this node" },
4+
{ "type": "firecracker", "available": true }
5+
]
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"items": [
3+
{ "type": "docker", "available": true },
4+
{
5+
"type": "firecracker",
6+
"available": false,
7+
"reason": "runtime unavailable: firecracker: not configured on this node"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)