/* eslint-disable @typescript-eslint/no-base-to-string */
import Editor from "@monaco-editor/react";
import BookIcon from "@mui/icons-material/Book";
import RefreshIcon from "@mui/icons-material/Refresh";
import { Grid, IconButton, InputAdornment, Tooltip } from "@mui/material";
import React from "react";
import { v4 as uuidv4 } from "uuid";
import YAML from "yaml";
import { VMInfo } from "../../api/VMApi";
import { RouterLink } from "../RouterLink";
import { CheckboxInput } from "./CheckboxInput";
import { EditSection } from "./EditSection";
import { SelectInput } from "./SelectInput";
import { TextInput } from "./TextInput";
interface CloudInitProps {
vm: VMInfo;
onChange?: () => void;
editable: boolean;
}
export function CloudInitEditor(p: CloudInitProps): React.ReactElement {
return (
<>
{/* Attach cloud init disk */}
{
p.vm.cloud_init.attach_config = v;
p.onChange?.();
}}
/>
>
);
}
function CloudInitMetadata(p: CloudInitProps): React.ReactElement {
// Regenerate instance id
const reGenerateInstanceId = () => {
p.vm.cloud_init.instance_id = uuidv4();
p.onChange?.();
};
return (
{/* Instance ID */}
{
p.vm.cloud_init.instance_id = v;
p.onChange?.();
}}
endAdornment={
p.editable ? (
) : (
<>>
)
}
/>
{/* Instance hostname */}
{
p.vm.cloud_init.local_hostname = v;
p.onChange?.();
}}
/>
{/* Data source mode */}
{
p.vm.cloud_init.dsmode = v as any;
p.onChange?.();
}}
options={[
{ label: "None", value: undefined },
{ value: "Net" },
{ value: "Local" },
]}
/>
);
}
function CloudInitRawUserData(p: CloudInitProps): React.ReactElement {
return (
}
>
{
p.vm.cloud_init.user_data = v ?? "";
p.onChange?.();
}}
/>
);
}
function CloudInitNetworkConfig(p: CloudInitProps): React.ReactElement {
if (!p.editable && !p.vm.cloud_init.network_configuration) return <>>;
return (
}
>
{
if (v && v !== "") p.vm.cloud_init.network_configuration = v;
else p.vm.cloud_init.network_configuration = undefined;
p.onChange?.();
}}
/>
);
}
function CloudInitUserDataAssistant(p: CloudInitProps): React.ReactElement {
const user_data = React.useMemo(() => {
return YAML.parseDocument(p.vm.cloud_init.user_data);
}, [p.vm.cloud_init.user_data]);
const onChange = () => {
p.vm.cloud_init.user_data = user_data.toString();
if (!p.vm.cloud_init.user_data.startsWith("#cloud-config"))
p.vm.cloud_init.user_data = `#cloud-config\n${p.vm.cloud_init.user_data}`;
p.onChange?.();
};
const SYSTEMD_NOT_SERIAL = `/bin/sh -c "rm -f /etc/default/grub.d/50-cloudimg-settings.cfg && sed -i 's/quiet splash//g' /etc/default/grub && update-grub"`;
return (
{/* /bin/sh -c "rm -f /etc/default/grub.d/50-cloudimg-settings.cfg && update-grub" */}
a.value === SYSTEMD_NOT_SERIAL
)
}
onValueChange={(c) => {
if (!user_data.getIn(["runcmd"])) user_data.addIn(["runcmd"], []);
const runcmd = user_data.getIn(["runcmd"]) as any;
if (c) {
runcmd.addIn([], SYSTEMD_NOT_SERIAL);
} else {
const idx = runcmd.items.findIndex(
(o: any) => o.value === SYSTEMD_NOT_SERIAL
);
runcmd.items.splice(idx, 1);
}
onChange();
}}
/>
);
}
function CloudInitTextInput(p: {
editable: boolean;
name: string;
refUrl: string;
attrPath: Iterable;
yaml: YAML.Document;
onChange?: () => void;
}): React.ReactElement {
return (
{
if (v !== undefined) p.yaml.setIn(p.attrPath, v);
else p.yaml.deleteIn(p.attrPath);
p.onChange?.();
}}
endAdornment={
}
/>
);
}
function CloudInitBooleanInput(p: {
editable: boolean;
name: string;
refUrl: string;
attrPath: Iterable;
yaml: YAML.Document;
onChange?: () => void;
}): React.ReactElement {
return (
{
p.yaml.setIn(p.attrPath, v);
p.onChange?.();
}}
/>
);
}