-
-
-
- Get started by editing{" "}
-
- src/app/page.tsx -- . -
- - Save and see your changes instantly. -
Container Management
+ {error &&{error}
} + {message &&{message}
} + +diff --git a/next.config.ts b/next.config.ts index e9ffa30..a8aa782 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,7 +1,17 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { - /* config options here */ + /* config options here */ + webpack(config, { isServer }) { + // 仅在服务器端处理 .node 文件 + if (isServer) { + config.module.rules.push({ + test: /\.node$/, + use: 'node-loader', // 使用 node-loader 处理 .node 文件 + }); + } + return config; + }, }; export default nextConfig; diff --git a/package.json b/package.json index 829fe25..558a84d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "bun": "^1.1.42", + "cpu-features": "^0.0.10", "dockerode": "^4.0.2", "next": "15.1.3", "react": "^19.0.0", diff --git a/src/actions/createContainers.ts b/src/actions/createContainers.ts index c41a882..916ed8c 100644 --- a/src/actions/createContainers.ts +++ b/src/actions/createContainers.ts @@ -1,67 +1,100 @@ -// app/actions/containerActions.ts +'use server' + import Docker from 'dockerode'; +// 只在服务器端执行的操作 const docker = new Docker({ socketPath: '/var/run/docker.sock' }); -// 批量创建容器 export async function createContainers(count: number, baseName: string, startingPorts: [number, number]) { const [vncStart, sshStart] = startingPorts; const promises = []; + // 只在服务器端加载 cpu-features + if (typeof window === 'undefined') { + try { + const cpuFeatures = require('cpu-features'); // 仅在服务器端加载 + console.log('CPU Features:', cpuFeatures); // 打印所有的 CPU 特性对象 + } catch (error) { + console.error('Error loading cpu-features:', error); + } + } + for (let i = 0; i < count; i++) { const containerName = `${baseName}-${i}`; const vncPort = vncStart + i; const sshPort = sshStart + i; - const container = docker.createContainer({ - Image: 'ubuntu-xfce-vnc-ssh', // 使用你的镜像 - name: containerName, - ExposedPorts: { - '5900/tcp': {}, // VNC 默认端口 - '22/tcp': {}, // SSH/SFTP 默认端口 - }, - HostConfig: { - PortBindings: { - '5900/tcp': [{ HostPort: vncPort.toString() }], - '22/tcp': [{ HostPort: sshPort.toString() }], + try { + const container = await docker.createContainer({ + Image: 'dockerp.com/dcsunset/ubuntu-vnc:latest', // 使用你的镜像 + name: containerName, + ExposedPorts: { + '5900/tcp': {}, // VNC 默认端口 + '22/tcp': {}, // SSH/sSFTP 默认端口 }, - }, - }); + HostConfig: { + PortBindings: { + '5900/tcp': [{ HostPort: vncPort.toString() }], + '22/tcp': [{ HostPort: sshPort.toString() }], + }, + }, + }); - promises.push(container); + promises.push(container); + } catch (error) { + console.error('Error creating container:', error); + throw new Error(`Failed to create container ${containerName}`); + } } return Promise.all(promises); } -// 关闭容器 +// 其他容器操作如 stop、remove 等 export async function stopContainer(containerName: string) { - const container = docker.getContainer(containerName); - await container.stop(); - return { message: `Container ${containerName} stopped successfully.` }; + try { + const container = docker.getContainer(containerName); + await container.stop(); + return { message: `Container ${containerName} stopped successfully.` }; + } catch (error) { + console.error('Error stopping container:', error); + throw new Error(`Failed to stop container ${containerName}`); + } } -// 删除容器 export async function removeContainer(containerName: string) { - const container = docker.getContainer(containerName); - await container.remove(); - return { message: `Container ${containerName} removed successfully.` }; + try { + const container = docker.getContainer(containerName); + await container.remove(); + return { message: `Container ${containerName} removed successfully.` }; + } catch (error) { + console.error('Error removing container:', error); + throw new Error(`Failed to remove container ${containerName}`); + } } -// 获取容器信息 export async function getContainerInfo(containerName: string) { - const container = docker.getContainer(containerName); - const info = await container.inspect(); - return { - containerName: info.Name, - state: info.State.Status, - ports: info.NetworkSettings.Ports, - }; + try { + const container = docker.getContainer(containerName); + const info = await container.inspect(); + return { + containerName: info.Name, + state: info.State.Status, + ports: info.NetworkSettings.Ports, + }; + } catch (error) { + console.error('Error retrieving container info:', error); + throw new Error(`Failed to retrieve info for container ${containerName}`); + } } -// 启动容器 export async function startContainer(containerName: string) { - const container = docker.getContainer(containerName); - await container.start(); - return { message: `Container ${containerName} started successfully.` }; + try { + const container = docker.getContainer(containerName); + await container.start(); + return { message: `Container ${containerName} started successfully.` }; + } catch (error) { + console.error('Error starting container:', error); + throw new Error(`Failed to start container ${containerName}`); + } } diff --git a/src/app/page.tsx b/src/app/page.tsx index 3eee014..396554e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,101 +1,80 @@ -import Image from "next/image"; +'use client' + +import React, { useState } from 'react'; + +// 引入 Server Actions +import { createContainers, stopContainer, removeContainer, getContainerInfo, startContainer } from '@/actions/createContainers'; export default function Home() { - return ( -
- src/app/page.tsx
-
- .
- {error}
} + {message &&{message}
} + +