ecs logic

This commit is contained in:
Ishaan Dey 2024-05-17 23:18:15 -07:00
parent 7981af1451
commit 14ea89d690
5 changed files with 1229 additions and 28 deletions

View File

@ -10,10 +10,9 @@ import {
} from "./types"; } from "./types";
import { import {
DeleteServiceCommand,
DescribeServicesCommand, DescribeServicesCommand,
ECSClient, ECSClient,
StartTaskCommand,
StopTaskCommand,
} from "@aws-sdk/client-ecs"; } from "@aws-sdk/client-ecs";
dotenv.config(); dotenv.config();
@ -178,10 +177,11 @@ export const getProjectSize = async (id: string) => {
return (await res.json()).size; return (await res.json()).size;
}; };
export const stopServer = async (task: string) => { export const stopServer = async (service: string) => {
const command = new StopTaskCommand({ const command = new DeleteServiceCommand({
cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox", cluster: "Sandbox",
task, service,
force: true,
}); });
try { try {

View File

@ -1,12 +1,14 @@
"use server" "use server";
import { revalidatePath } from "next/cache" import { revalidatePath } from "next/cache";
import ecsClient from "./ecs";
import { CreateServiceCommand, StartTaskCommand } from "@aws-sdk/client-ecs";
export async function createSandbox(body: { export async function createSandbox(body: {
type: string type: string;
name: string name: string;
userId: string userId: string;
visibility: string visibility: string;
}) { }) {
const res = await fetch( const res = await fetch(
"https://database.ishaan1013.workers.dev/api/sandbox", "https://database.ishaan1013.workers.dev/api/sandbox",
@ -17,15 +19,15 @@ export async function createSandbox(body: {
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
} }
) );
return await res.text() return await res.text();
} }
export async function updateSandbox(body: { export async function updateSandbox(body: {
id: string id: string;
name?: string name?: string;
visibility?: "public" | "private" visibility?: "public" | "private";
}) { }) {
await fetch("https://database.ishaan1013.workers.dev/api/sandbox", { await fetch("https://database.ishaan1013.workers.dev/api/sandbox", {
method: "POST", method: "POST",
@ -33,17 +35,17 @@ export async function updateSandbox(body: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
}) });
revalidatePath("/dashboard") revalidatePath("/dashboard");
} }
export async function deleteSandbox(id: string) { export async function deleteSandbox(id: string) {
await fetch(`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`, { await fetch(`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`, {
method: "DELETE", method: "DELETE",
}) });
revalidatePath("/dashboard") revalidatePath("/dashboard");
} }
export async function shareSandbox(sandboxId: string, email: string) { export async function shareSandbox(sandboxId: string, email: string) {
@ -56,15 +58,15 @@ export async function shareSandbox(sandboxId: string, email: string) {
}, },
body: JSON.stringify({ sandboxId, email }), body: JSON.stringify({ sandboxId, email }),
} }
) );
const text = await res.text() const text = await res.text();
if (res.status !== 200) { if (res.status !== 200) {
return { success: false, message: text } return { success: false, message: text };
} }
revalidatePath(`/code/${sandboxId}`) revalidatePath(`/code/${sandboxId}`);
return { success: true, message: "Shared successfully." } return { success: true, message: "Shared successfully." };
} }
export async function unshareSandbox(sandboxId: string, userId: string) { export async function unshareSandbox(sandboxId: string, userId: string) {
@ -74,7 +76,37 @@ export async function unshareSandbox(sandboxId: string, userId: string) {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ sandboxId, userId }), body: JSON.stringify({ sandboxId, userId }),
}) });
revalidatePath(`/code/${sandboxId}`) revalidatePath(`/code/${sandboxId}`);
}
export async function startServer(serviceName: string) {
const command = new CreateServiceCommand({
cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox",
serviceName,
taskDefinition: "Sandbox1",
desiredCount: 1,
networkConfiguration: {
awsvpcConfiguration: {
securityGroups: ["sg-07e489fcf3299af52"],
subnets: [
"subnet-06d04f2a6ebb1710c",
"subnet-097c000f157c26a78",
"subnet-00f931ecbabaf87dd",
"subnet-0adcb82d77db9f263",
"subnet-0c6874150d8e63a7c",
"subnet-0b76f9ee3fe20660d",
],
assignPublicIp: "ENABLED",
},
},
});
try {
const response = await ecsClient.send(command);
console.log("started server:", response);
} catch (error) {
console.error("Error starting server:", error);
}
} }

11
frontend/lib/ecs.ts Normal file
View File

@ -0,0 +1,11 @@
import { ECSClient } from "@aws-sdk/client-ecs";
const ecsClient = new ECSClient({
region: "us-east-1",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});
export default ecsClient;

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
}, },
"dependencies": { "dependencies": {
"@atlaskit/pragmatic-drag-and-drop": "^1.1.7", "@atlaskit/pragmatic-drag-and-drop": "^1.1.7",
"@aws-sdk/client-ecs": "^3.577.0",
"@clerk/nextjs": "^4.29.12", "@clerk/nextjs": "^4.29.12",
"@clerk/themes": "^1.7.12", "@clerk/themes": "^1.7.12",
"@hookform/resolvers": "^3.3.4", "@hookform/resolvers": "^3.3.4",