add start/stop/restart feature on containers

This commit is contained in:
Thomas Camlong
2022-06-27 23:38:54 +02:00
parent 72aba9d8cd
commit 035224b02b
3 changed files with 93 additions and 41 deletions

View File

@@ -4,6 +4,7 @@ import {
IconCheck, IconCheck,
IconPlayerPlay, IconPlayerPlay,
IconPlayerStop, IconPlayerStop,
IconRefresh,
IconRotateClockwise, IconRotateClockwise,
IconX, IconX,
} from '@tabler/icons'; } from '@tabler/icons';
@@ -12,10 +13,10 @@ import Dockerode from 'dockerode';
function sendNotification(action: string, containerId: string, containerName: string) { function sendNotification(action: string, containerId: string, containerName: string) {
showNotification({ showNotification({
id: 'load-data', id: containerId,
loading: true, loading: true,
title: `${action}ing container ${containerName}`, title: `${action}ing container ${containerName}`,
message: 'Your password is being checked...', message: undefined,
autoClose: false, autoClose: false,
disallowClose: true, disallowClose: true,
}); });
@@ -23,19 +24,19 @@ function sendNotification(action: string, containerId: string, containerName: st
setTimeout(() => { setTimeout(() => {
if (res.data.success === true) { if (res.data.success === true) {
updateNotification({ updateNotification({
id: 'load-data', id: containerId,
title: 'Container restarted', title: `Container ${containerName} ${action}ed`,
message: 'Your container was successfully restarted', message: `Your container was successfully ${action}ed`,
icon: <IconCheck />, icon: <IconCheck />,
autoClose: 2000, autoClose: 2000,
}); });
} }
if (res.data.success === false) { if (res.data.success === false) {
updateNotification({ updateNotification({
id: 'load-data', id: containerId,
color: 'red', color: 'red',
title: 'There was an error restarting your container.', title: 'There was an error with your container.',
message: 'Your container has encountered issues while restarting.', message: undefined,
icon: <IconX />, icon: <IconX />,
autoClose: 2000, autoClose: 2000,
}); });
@@ -44,39 +45,58 @@ function sendNotification(action: string, containerId: string, containerName: st
}); });
} }
function restart(container: Dockerode.ContainerInfo) {
sendNotification('restart', container.Id, container.Names[0]);
}
function stop(container: Dockerode.ContainerInfo) {
console.log('stoping container', container.Id);
}
function start(container: Dockerode.ContainerInfo) {
console.log('starting container', container.Id);
}
export interface ContainerActionBarProps { export interface ContainerActionBarProps {
selected: Dockerode.ContainerInfo[]; selected: Dockerode.ContainerInfo[];
reload: () => void;
} }
export default function ContainerActionBar(props: ContainerActionBarProps) { export default function ContainerActionBar({ selected, reload }: ContainerActionBarProps) {
const { selected } = props;
return ( return (
<Group> <Group>
<Button <Button
leftIcon={<IconRotateClockwise />} leftIcon={<IconRotateClockwise />}
onClick={() => selected.map((container) => restart(container))} onClick={() =>
variant="filled" Promise.all(
selected.map((container) =>
sendNotification('restart', container.Id, container.Names[0])
)
).then(() => reload())
}
variant="light"
color="orange" color="orange"
radius="md" radius="md"
> >
Restart Restart
</Button> </Button>
<Button leftIcon={<IconPlayerStop />} variant="filled" color="red" radius="md"> <Button
leftIcon={<IconPlayerStop />}
onClick={() =>
Promise.all(
selected.map((container) => sendNotification('stop', container.Id, container.Names[0]))
).then(() => reload())
}
variant="light"
color="red"
radius="md"
>
Stop Stop
</Button> </Button>
<Button leftIcon={<IconPlayerPlay />} variant="filled" color="green" radius="md"> <Button
leftIcon={<IconPlayerPlay />}
onClick={() =>
Promise.all(
selected.map((container) => sendNotification('start', container.Id, container.Names[0]))
).then(() => reload())
}
variant="light"
color="green"
radius="md"
>
Start Start
</Button> </Button>
<Button leftIcon={<IconRefresh />} onClick={() => reload()} variant="light">
Refresh data
</Button>
</Group> </Group>
); );
} }

View File

@@ -33,6 +33,12 @@ export default function DockerDrawer(props: any) {
const [containers, setContainers] = useState<Docker.ContainerInfo[]>([]); const [containers, setContainers] = useState<Docker.ContainerInfo[]>([]);
const { classes, cx } = useStyles(); const { classes, cx } = useStyles();
const [selection, setSelection] = useState<Docker.ContainerInfo[]>([]); const [selection, setSelection] = useState<Docker.ContainerInfo[]>([]);
function reload() {
axios.get('/api/docker/containers').then((res) => {
setContainers(res.data);
});
}
const toggleRow = (container: Docker.ContainerInfo) => const toggleRow = (container: Docker.ContainerInfo) =>
setSelection((current) => setSelection((current) =>
current.includes(container) ? current.filter((c) => c !== container) : [...current, container] current.includes(container) ? current.filter((c) => c !== container) : [...current, container]
@@ -43,9 +49,7 @@ export default function DockerDrawer(props: any) {
); );
useEffect(() => { useEffect(() => {
axios.get('/api/docker/containers').then((res) => { reload();
setContainers(res.data);
});
}, []); }, []);
const rows = containers.map((element) => { const rows = containers.map((element) => {
const selected = selection.includes(element); const selected = selection.includes(element);
@@ -62,15 +66,15 @@ export default function DockerDrawer(props: any) {
<td>{element.Image}</td> <td>{element.Image}</td>
<td> <td>
<Group> <Group>
{element.Ports.slice(-3).map((port) => ( {element.Ports.sort((a, b) => a.PrivatePort - b.PrivatePort)
.slice(-3)
.map((port) => (
<Badge variant="outline"> <Badge variant="outline">
{port.PrivatePort}:{port.PublicPort} {port.PrivatePort}:{port.PublicPort}
</Badge> </Badge>
))} ))}
{element.Ports.length > 3 && ( {element.Ports.length > 3 && (
<Badge variant="filled"> <Badge variant="filled">{element.Ports.length - 3} more</Badge>
{element.Ports.length - 3} more
</Badge>
)} )}
</Group> </Group>
</td> </td>
@@ -85,7 +89,7 @@ export default function DockerDrawer(props: any) {
<> <>
<Drawer opened={opened} onClose={() => setOpened(false)} padding="xl" size="full"> <Drawer opened={opened} onClose={() => setOpened(false)} padding="xl" size="full">
<ScrollArea> <ScrollArea>
<ContainerActionBar selected={selection} /> <ContainerActionBar selected={selection} reload={reload} />
<Table captionSide="bottom" highlightOnHover sx={{ minWidth: 800 }} verticalSpacing="sm"> <Table captionSide="bottom" highlightOnHover sx={{ minWidth: 800 }} verticalSpacing="sm">
<caption>your docker containers</caption> <caption>your docker containers</caption>
<thead> <thead>

View File

@@ -29,10 +29,38 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
}); });
} }
}); });
if (action === 'restart') {
await container.restart(); switch (action) {
return res.status(200).json({ case 'start':
success: true, container.start((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
case 'stop':
container.stop((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
case 'restart':
container.restart((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
default:
res.status(400).json({
message: 'Invalid action',
}); });
} }
return res.status(200).json({ return res.status(200).json({