From 561a284fc9c13144b548bda77e281e0a847a2efb Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Tue, 21 May 2024 00:57:52 -0700 Subject: [PATCH] more container orchestration logic --- backend/server/dockerfile | 2 + frontend/.env.example | 15 ++++++++ frontend/components/editor/index.tsx | 4 +- frontend/lib/actions.ts | 16 ++++++-- frontend/lib/utils.ts | 55 +++++++++++++++------------- 5 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 frontend/.env.example diff --git a/backend/server/dockerfile b/backend/server/dockerfile index 93219f9..645f4d4 100644 --- a/backend/server/dockerfile +++ b/backend/server/dockerfile @@ -20,6 +20,8 @@ RUN useradd -m myuser RUN mkdir projects && chown -R myuser:myuser projects USER myuser +# user namespace mapping + EXPOSE 3000 EXPOSE 4000 diff --git a/frontend/.env.example b/frontend/.env.example new file mode 100644 index 0000000..908ff5a --- /dev/null +++ b/frontend/.env.example @@ -0,0 +1,15 @@ +NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= +CLERK_SECRET_KEY= +NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_KEY= +LIVEBLOCKS_SECRET_KEY= + +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= + +NEXT_PUBLIC_AWS_ECS_CLUSTER= +AWS_ECS_SECURITY_GROUP= + +NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in +NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up +NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard +NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/dashboard \ No newline at end of file diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index d4e2f2a..c344f9a 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -34,8 +34,8 @@ export default function Editor({ console.log("startServer"); }, []); - if (!isServerRunning || didFail) - return ; + // if (!isServerRunning || didFail) + // return ; return ; } diff --git a/frontend/lib/actions.ts b/frontend/lib/actions.ts index e00bea0..9fef49c 100644 --- a/frontend/lib/actions.ts +++ b/frontend/lib/actions.ts @@ -83,13 +83,13 @@ export async function unshareSandbox(sandboxId: string, userId: string) { export async function startServer(serviceName: string) { const command = new CreateServiceCommand({ - cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox", + cluster: process.env.AWS_ECS_CLUSTER!, serviceName, taskDefinition: "Sandbox1", desiredCount: 1, networkConfiguration: { awsvpcConfiguration: { - securityGroups: ["sg-07e489fcf3299af52"], + securityGroups: [process.env.AWS_ECS_SECURITY_GROUP!], subnets: [ "subnet-06d04f2a6ebb1710c", "subnet-097c000f157c26a78", @@ -105,7 +105,17 @@ export async function startServer(serviceName: string) { try { const response = await ecsClient.send(command); - console.log("started server:", response); + console.log("started server:", response.service?.serviceName); + + // store in workers kv: + // { + // userId: { + // sandboxId, + // serviceName, + // startedAt, + + // } + // } } catch (error) { console.error("Error starting server:", error); } diff --git a/frontend/lib/utils.ts b/frontend/lib/utils.ts index d2259e0..126e8b5 100644 --- a/frontend/lib/utils.ts +++ b/frontend/lib/utils.ts @@ -94,31 +94,34 @@ export function addNew( // } // } -// const checkServiceStatus = (serviceName: string) => { -// return new Promise((resolve, reject) => { +const checkServiceStatus = (serviceName: string) => { + return new Promise((resolve, reject) => { + const command = new DescribeServicesCommand({ + cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER, + services: [serviceName], + }); -// const command = new DescribeServicesCommand({ -// cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox", -// services: [serviceName] -// }) + const interval = setInterval(async () => { + try { + const response = await ecsClient.send(command); + console.log("Checking service status", response); -// const interval = setInterval(async () => { -// try { - -// const response = await ecsClient.send(command) - -// if (response.services && response.services.length > 0) { -// const service = response.services?.[0]; -// if (service.runningCount === service.desiredCount && service.deployments.length === 1 && service.deployments[0].rolloutState === 'COMPLETED') { -// clearInterval(interval); -// resolve(service); -// } -// } - -// } catch (error) { -// clearInterval(interval); -// reject(error); -// } -// }, 5000); // Check every 5 seconds -// }); -// }; + if (response.services && response.services.length > 0) { + const service = response.services?.[0]; + if ( + service.runningCount === service.desiredCount && + service.deployments && + service.deployments.length === 1 && + service.deployments[0].rolloutState === "COMPLETED" + ) { + clearInterval(interval); + resolve(service); + } + } + } catch (error) { + clearInterval(interval); + reject(error); + } + }, 5000); + }); +};