UI: init update machine

This commit is contained in:
Johannes Kirschbauer
2024-08-15 15:04:17 +02:00
parent cacd49ff54
commit 02662867fb
4 changed files with 102 additions and 47 deletions

View File

@@ -16,8 +16,91 @@ interface MachineListItemProps {
export const MachineListItem = (props: MachineListItemProps) => {
const { name, info, nixOnly } = props;
const [deploying, setDeploying] = createSignal<boolean>(false);
// Bootstrapping
const [installing, setInstalling] = createSignal<boolean>(false);
// Later only updates
const [updating, setUpdating] = createSignal<boolean>(false);
const navigate = useNavigate();
const handleInstall = async () => {
if (!info?.deploy.targetHost || installing()) {
return;
}
const active_clan = activeURI();
if (!active_clan) {
toast.error("No active clan selected");
return;
}
if (!info?.deploy.targetHost) {
toast.error(
"Machine does not have a target host. Specify where the machine should be deployed.",
);
return;
}
setInstalling(true);
await toast.promise(
callApi("install_machine", {
opts: {
machine: name,
flake: {
loc: active_clan,
},
no_reboot: true,
target_host: info?.deploy.targetHost,
debug: true,
nix_options: [],
},
password: null,
}),
{
loading: "Installing...",
success: "Installed",
error: "Failed to install",
},
);
setInstalling(false);
};
const handleUpdate = async () => {
if (!info?.deploy.targetHost || installing()) {
return;
}
const active_clan = activeURI();
if (!active_clan) {
toast.error("No active clan selected");
return;
}
if (!info?.deploy.targetHost) {
toast.error(
"Machine does not have a target host. Specify where the machine should be deployed.",
);
return;
}
setUpdating(true);
await toast.promise(
callApi("update_machines", {
base_path: active_clan,
machines: [
{
name: name,
deploy: {
targetHost: info?.deploy.targetHost,
},
},
],
}),
{
loading: "Updating...",
success: "Updated",
error: "Failed to update",
},
);
setUpdating(false);
};
return (
<li>
<div class="card card-side m-2 bg-base-200">
@@ -73,51 +156,25 @@ export const MachineListItem = (props: MachineListItemProps) => {
</li>
<li
classList={{
disabled: !info?.deploy.targetHost || deploying(),
}}
onClick={async (e) => {
if (!info?.deploy.targetHost || deploying()) {
return;
}
const active_clan = activeURI();
if (!active_clan) {
toast.error("No active clan selected");
return;
}
if (!info?.deploy.targetHost) {
toast.error(
"Machine does not have a target host. Specify where the machine should be deployed.",
);
return;
}
setDeploying(true);
await toast.promise(
callApi("install_machine", {
opts: {
machine: name,
flake: {
loc: active_clan,
},
no_reboot: true,
target_host: info?.deploy.targetHost,
debug: true,
nix_options: [],
},
password: null,
}),
{
loading: "Deploying...",
success: "Deployed",
error: "Failed to deploy",
},
);
setDeploying(false);
disabled: !info?.deploy.targetHost || installing(),
}}
onClick={handleInstall}
>
<a>
<Show when={info?.deploy.targetHost} fallback={"Deploy"}>
{(d) => `Deploy to ${d()}`}
{(d) => `Install to ${d()}`}
</Show>
</a>
</li>
<li
classList={{
disabled: !info?.deploy.targetHost || updating(),
}}
onClick={handleUpdate}
>
<a>
<Show when={info?.deploy.targetHost} fallback={"Deploy"}>
{(d) => `Update (${d()})`}
</Show>
</a>
</li>