Removed app structure from the core
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import testAuth from "../auth";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
import { installApp, getApplications, removeApp } from "@/core/application";
|
||||
|
||||
export async function GET(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
|
||||
if (auth) {
|
||||
return NextResponse.json(
|
||||
{ message: auth },
|
||||
{ status: StatusCodes.UNAUTHORIZED }
|
||||
);
|
||||
}
|
||||
|
||||
const applications = await getApplications();
|
||||
|
||||
return NextResponse.json({
|
||||
message: "ok",
|
||||
applications,
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
const { name, repo, nodeId }: { name: string; repo: string; nodeId: string } =
|
||||
await req.json();
|
||||
|
||||
if (auth) {
|
||||
return NextResponse.json(
|
||||
{ message: auth },
|
||||
{ status: StatusCodes.UNAUTHORIZED }
|
||||
);
|
||||
}
|
||||
|
||||
if (!name || !repo || !nodeId) {
|
||||
return NextResponse.json(
|
||||
{ message: "Body missing name, repo, nodeId" },
|
||||
{ status: StatusCodes.BAD_REQUEST }
|
||||
);
|
||||
}
|
||||
|
||||
const res = await installApp(name, repo, nodeId);
|
||||
|
||||
return NextResponse.json({
|
||||
message: "ok",
|
||||
...res,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
const appId = req.nextUrl.searchParams.get("appId");
|
||||
|
||||
if (auth) {
|
||||
return NextResponse.json(
|
||||
{ message: auth },
|
||||
{ status: StatusCodes.UNAUTHORIZED }
|
||||
);
|
||||
}
|
||||
|
||||
if (!appId) {
|
||||
return NextResponse.json(
|
||||
{ message: "Missing param appId" },
|
||||
{ status: StatusCodes.BAD_REQUEST }
|
||||
);
|
||||
}
|
||||
|
||||
await removeApp(appId);
|
||||
|
||||
return NextResponse.json({
|
||||
message: "ok",
|
||||
});
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import testAuth from "../auth";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
import { deployApp, getDeployments } from "@/core/deployment";
|
||||
import { getApplications } from "@/core/application";
|
||||
|
||||
export async function GET(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
@@ -14,9 +13,9 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
|
||||
);
|
||||
}
|
||||
|
||||
const apps = await getApplications();
|
||||
// const apps = await getApplications();
|
||||
|
||||
const applications = await getDeployments(apps);
|
||||
const applications = await getDeployments([]);
|
||||
|
||||
return NextResponse.json({
|
||||
message: "ok",
|
||||
|
||||
Vendored
-41
@@ -1,41 +0,0 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import testAuth from "../auth";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
import { getEnv, setEnv } from "@/core/application";
|
||||
|
||||
export async function GET(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
|
||||
if (auth) {
|
||||
return NextResponse.json(
|
||||
{ message: auth },
|
||||
{ status: StatusCodes.UNAUTHORIZED }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(await getEnv());
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||
const auth = await testAuth(req);
|
||||
|
||||
if (auth) {
|
||||
return NextResponse.json(
|
||||
{ message: auth },
|
||||
{ status: StatusCodes.UNAUTHORIZED }
|
||||
);
|
||||
}
|
||||
|
||||
const { appId, variables } = await req.json();
|
||||
|
||||
if (!appId || !variables) {
|
||||
return NextResponse.json(
|
||||
{ message: "Missing appId, variables in body" },
|
||||
{ status: StatusCodes.BAD_REQUEST }
|
||||
);
|
||||
}
|
||||
|
||||
await setEnv(appId, variables);
|
||||
|
||||
return NextResponse.json({ message: "ok", success: true });
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { DashboardLayout } from "@/components/dashboard/DashboardLayout";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { MoreVertical, Plus, Rocket } from "lucide-react";
|
||||
import InstallApplicationDialog from "@/components/dialogs/InstallApplication";
|
||||
import { getApplications, removeApp } from "@/lib/dockship/application";
|
||||
import RemoveDialog from "@/components/dialogs/Remove";
|
||||
import DeployAppDialog from "@/components/dialogs/DeployApp";
|
||||
import { useAsyncInterval } from "@/hooks/use-async";
|
||||
|
||||
export default function ApplicationsPage() {
|
||||
const { value: applications, setValue: setApplications } = useAsyncInterval(
|
||||
[],
|
||||
getApplications,
|
||||
3000
|
||||
);
|
||||
|
||||
const remove = async (appId: string) => {
|
||||
await removeApp(appId);
|
||||
setApplications((prev) => prev.filter((app) => app.id !== appId));
|
||||
};
|
||||
|
||||
return (
|
||||
<DashboardLayout
|
||||
title="Applications"
|
||||
subtitle="Manage installed applications"
|
||||
>
|
||||
{/* Header */}
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between mb-6">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Badge>{applications.length} apps</Badge>
|
||||
</div>
|
||||
<InstallApplicationDialog>
|
||||
<Button size="sm" className="gap-2 w-full sm:w-auto">
|
||||
<Plus className="w-4 h-4" /> Install Application
|
||||
</Button>
|
||||
</InstallApplicationDialog>
|
||||
</div>
|
||||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
{applications.map((app) => (
|
||||
<Card key={app.id}>
|
||||
<CardHeader>
|
||||
<CardTitle>{app.name}</CardTitle>
|
||||
<CardDescription>{app.repo}</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="flex flex-col gap-2">
|
||||
{/* Deployments */}
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Deployments
|
||||
</span>
|
||||
<span className="font-mono text-sm">
|
||||
{app.deployments?.length ?? 0}
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
<CardFooter className="justify-end">
|
||||
<DeployAppDialog defaultApp={app.id}>
|
||||
<Button variant="ghost" size="icon-sm" aria-label="Deploy">
|
||||
<Rocket className="w-3 h-3" />
|
||||
</Button>
|
||||
</DeployAppDialog>
|
||||
|
||||
<RemoveDialog remove={() => remove(app.id)} />
|
||||
|
||||
<Button variant="ghost" size="icon-sm" aria-label="More options">
|
||||
<MoreVertical className="w-3 h-3" />
|
||||
</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
@@ -15,9 +15,9 @@ import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { getLiveNodes } from "@/lib/dockship/node";
|
||||
import { Combobox } from "../ui/combobox";
|
||||
import { getApplications } from "@/core/application";
|
||||
import { deployApp } from "@/lib/dockship/deploy";
|
||||
import { useAsync } from "@/hooks/use-async";
|
||||
import { Application } from "@/lib/types";
|
||||
|
||||
export default function DeployAppDialog({
|
||||
defaultApp,
|
||||
@@ -27,7 +27,7 @@ export default function DeployAppDialog({
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const { value: nodes } = useAsync([], getLiveNodes);
|
||||
const { value: applications } = useAsync([], getApplications);
|
||||
const { value: applications } = useAsync<Application[]>([], async () => []);
|
||||
const [name, setName] = useState("");
|
||||
const [app, setApp] = useState(defaultApp || "");
|
||||
const [node, setNode] = useState("");
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import Docker from "dockerode";
|
||||
import { randomUUID } from "crypto";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { exec } from "child_process";
|
||||
import util from "util";
|
||||
import { Application, Env, EnvVariable } from "@/lib/types";
|
||||
|
||||
const docker = new Docker();
|
||||
const execAsync = util.promisify(exec);
|
||||
const DATA_DIR = path.join(process.cwd(), "data", "apps");
|
||||
|
||||
export async function getApplications(): Promise<Application[]> {
|
||||
return fs
|
||||
.readdirSync(DATA_DIR)
|
||||
.map((appId) =>
|
||||
JSON.parse(
|
||||
fs.readFileSync(path.join(DATA_DIR, appId, "app.json")).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export async function installApp(name: string, repo: string, nodeId: string) {
|
||||
// Create app ID
|
||||
const appId = `app_${randomUUID()}`;
|
||||
const appDir = path.join(DATA_DIR, appId);
|
||||
const repoDir = path.join(appDir, "repo");
|
||||
|
||||
// Create directories
|
||||
fs.mkdirSync(repoDir, { recursive: true });
|
||||
|
||||
// Write app.json
|
||||
const appConfig: Application = {
|
||||
id: appId,
|
||||
name,
|
||||
repo,
|
||||
nodeId,
|
||||
createdAt: new Date().toISOString(),
|
||||
deployments: [],
|
||||
};
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(appDir, "app.json"),
|
||||
JSON.stringify(appConfig, null, 2)
|
||||
);
|
||||
|
||||
// Write env.json
|
||||
fs.writeFileSync(path.join(appDir, "env.json"), JSON.stringify([], null, 2));
|
||||
|
||||
// Clone repository
|
||||
try {
|
||||
await execAsync(`git clone ${repo} ${repoDir}`);
|
||||
} catch (err) {
|
||||
throw new Error("Failed to clone repository");
|
||||
}
|
||||
|
||||
// Validate Dockerfile
|
||||
const dockerfilePath = path.join(repoDir, "Dockerfile");
|
||||
|
||||
try {
|
||||
fs.accessSync(dockerfilePath);
|
||||
} catch {
|
||||
fs.rmSync(appDir, { recursive: true });
|
||||
throw new Error("Dockerfile not found in repository root");
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
appId,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getEnv(): Promise<Record<string, Env>> {
|
||||
const apps = fs.readdirSync(DATA_DIR);
|
||||
|
||||
return apps.reduce((acc, appId) => {
|
||||
const appPath = path.join(DATA_DIR, appId, "app.json");
|
||||
const name = JSON.parse(fs.readFileSync(appPath).toString()).name;
|
||||
|
||||
const envPath = path.join(DATA_DIR, appId, "env.json");
|
||||
if (fs.existsSync(envPath)) {
|
||||
const envData: Record<string, EnvVariable> = JSON.parse(
|
||||
fs.readFileSync(envPath, "utf-8")
|
||||
);
|
||||
acc[appId] = { variables: envData, id: appId, name };
|
||||
}
|
||||
return acc;
|
||||
}, {} as Record<string, Env>);
|
||||
}
|
||||
|
||||
export async function setEnv(appId: string, variables: EnvVariable[]) {
|
||||
const apps = fs.readdirSync(DATA_DIR);
|
||||
|
||||
apps.forEach((id) => {
|
||||
if (id === appId) {
|
||||
const envPath = path.join(DATA_DIR, appId, "env.json");
|
||||
|
||||
fs.writeFileSync(envPath, JSON.stringify(variables));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function removeApp(appId: string) {
|
||||
const appPath = path.join(DATA_DIR, appId);
|
||||
|
||||
const appMeta: Application = JSON.parse(
|
||||
fs.readFileSync(path.join(appPath, "app.json")).toString()
|
||||
);
|
||||
|
||||
appMeta.deployments.forEach(async (imgId) => {
|
||||
// Remove containers
|
||||
(await docker.listContainers({ all: true }))
|
||||
.filter((con) => con.ImageID === imgId)
|
||||
.forEach((con) => {
|
||||
const container = docker.getContainer(con.Id);
|
||||
container.stop();
|
||||
container.remove();
|
||||
});
|
||||
|
||||
// Remove image
|
||||
const image = docker.getImage(imgId);
|
||||
image.remove();
|
||||
});
|
||||
|
||||
fs.rmSync(appPath, { recursive: true });
|
||||
}
|
||||
Reference in New Issue
Block a user