229 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-05-26 12:18:09 -07:00
"use server"
2024-05-26 12:18:09 -07:00
import { revalidatePath } from "next/cache"
import ecsClient, { ec2Client } from "./ecs"
2024-05-23 01:35:08 -07:00
import {
CreateServiceCommand,
DescribeClustersCommand,
DescribeServicesCommand,
DescribeTasksCommand,
ListTasksCommand,
2024-05-26 12:18:09 -07:00
} from "@aws-sdk/client-ecs"
import { DescribeNetworkInterfacesCommand } from "@aws-sdk/client-ec2"
export async function createSandbox(body: {
2024-05-26 12:18:09 -07:00
type: string
name: string
userId: string
visibility: string
}) {
const res = await fetch(
"https://database.ishaan1013.workers.dev/api/sandbox",
{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
2024-05-26 12:18:09 -07:00
)
2024-04-27 16:22:35 -04:00
2024-05-26 12:18:09 -07:00
return await res.text()
}
export async function updateSandbox(body: {
2024-05-26 12:18:09 -07:00
id: string
name?: string
visibility?: "public" | "private"
}) {
await fetch("https://database.ishaan1013.workers.dev/api/sandbox", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
2024-05-26 12:18:09 -07:00
})
2024-05-26 12:18:09 -07:00
revalidatePath("/dashboard")
}
export async function deleteSandbox(id: string) {
await fetch(`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`, {
method: "DELETE",
2024-05-26 12:18:09 -07:00
})
2024-05-26 12:18:09 -07:00
revalidatePath("/dashboard")
}
2024-05-01 01:29:16 -04:00
export async function shareSandbox(sandboxId: string, email: string) {
const res = await fetch(
"https://database.ishaan1013.workers.dev/api/sandbox/share",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ sandboxId, email }),
}
2024-05-26 12:18:09 -07:00
)
const text = await res.text()
2024-05-01 01:53:49 -04:00
if (res.status !== 200) {
2024-05-26 12:18:09 -07:00
return { success: false, message: text }
2024-05-01 01:29:16 -04:00
}
2024-05-01 01:53:49 -04:00
2024-05-26 12:18:09 -07:00
revalidatePath(`/code/${sandboxId}`)
return { success: true, message: "Shared successfully." }
2024-05-01 01:29:16 -04:00
}
2024-05-01 02:22:02 -04:00
export async function unshareSandbox(sandboxId: string, userId: string) {
await fetch("https://database.ishaan1013.workers.dev/api/sandbox/share", {
2024-05-01 02:22:02 -04:00
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ sandboxId, userId }),
2024-05-26 12:18:09 -07:00
})
2024-05-01 02:22:02 -04:00
2024-05-26 12:18:09 -07:00
revalidatePath(`/code/${sandboxId}`)
2024-05-17 23:18:15 -07:00
}
2024-05-23 01:35:08 -07:00
export async function describeService(serviceName: string) {
const command = new DescribeServicesCommand({
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
services: [serviceName],
2024-05-26 12:18:09 -07:00
})
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
return await ecsClient.send(command)
2024-05-23 01:35:08 -07:00
}
export async function getTaskIp(serviceName: string) {
const listCommand = new ListTasksCommand({
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
serviceName,
2024-05-26 12:18:09 -07:00
})
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
const listResponse = await ecsClient.send(listCommand)
const taskArns = listResponse.taskArns
2024-05-23 01:35:08 -07:00
const describeCommand = new DescribeTasksCommand({
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
tasks: taskArns,
2024-05-26 12:18:09 -07:00
})
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
const describeResponse = await ecsClient.send(describeCommand)
const tasks = describeResponse.tasks
const taskAttachment = tasks?.[0].attachments?.[0].details
2024-05-23 01:35:08 -07:00
if (!taskAttachment) {
2024-05-26 12:18:09 -07:00
throw new Error("Task attachment not found")
2024-05-23 01:35:08 -07:00
}
const eni = taskAttachment.find(
(detail) => detail.name === "networkInterfaceId"
2024-05-26 12:18:09 -07:00
)?.value
2024-05-23 01:35:08 -07:00
if (!eni) {
2024-05-26 12:18:09 -07:00
throw new Error("Network interface not found")
2024-05-23 01:35:08 -07:00
}
const describeNetworkInterfacesCommand = new DescribeNetworkInterfacesCommand(
{
NetworkInterfaceIds: [eni],
}
2024-05-26 12:18:09 -07:00
)
2024-05-23 01:35:08 -07:00
const describeNetworkInterfacesResponse = await ec2Client.send(
describeNetworkInterfacesCommand
2024-05-26 12:18:09 -07:00
)
2024-05-23 01:35:08 -07:00
const ip =
describeNetworkInterfacesResponse.NetworkInterfaces?.[0].Association
2024-05-26 12:18:09 -07:00
?.PublicIp
2024-05-23 01:35:08 -07:00
if (!ip) {
2024-05-26 12:18:09 -07:00
throw new Error("Public IP not found")
2024-05-23 01:35:08 -07:00
}
2024-05-26 12:18:09 -07:00
return ip
2024-05-23 01:35:08 -07:00
}
2024-05-26 12:18:09 -07:00
export async function doesServiceExist(serviceName: string) {
const response = await describeService(serviceName)
2024-05-23 01:35:08 -07:00
const activeServices = response.services?.filter(
(service) => service.status === "ACTIVE"
2024-05-26 12:18:09 -07:00
)
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
console.log("activeServices: ", activeServices)
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
return activeServices?.length === 1
2024-05-23 01:35:08 -07:00
}
async function countServices() {
const command = new DescribeClustersCommand({
clusters: [process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!],
2024-05-26 12:18:09 -07:00
})
2024-05-23 01:35:08 -07:00
2024-05-26 12:18:09 -07:00
const response = await ecsClient.send(command)
return response.clusters?.[0].activeServicesCount!
2024-05-23 01:35:08 -07:00
}
export async function startServer(
serviceName: string
): Promise<{ success: boolean; message: string }> {
2024-05-26 12:18:09 -07:00
const serviceExists = await doesServiceExist(serviceName)
2024-05-23 01:35:08 -07:00
if (serviceExists) {
2024-05-26 12:18:09 -07:00
return { success: true, message: "" }
2024-05-23 01:35:08 -07:00
}
2024-05-26 12:18:09 -07:00
const activeServices = await countServices()
2024-05-23 01:35:08 -07:00
if (activeServices >= 100) {
return {
success: false,
message:
"Too many servers are running! Please try again later or contact @ishaandey_ on Twitter/X.",
2024-05-26 12:18:09 -07:00
}
2024-05-23 01:35:08 -07:00
}
2024-05-17 23:18:15 -07:00
const command = new CreateServiceCommand({
2024-05-23 01:35:08 -07:00
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
2024-05-17 23:18:15 -07:00
serviceName,
taskDefinition: "Sandbox1",
desiredCount: 1,
2024-05-23 01:35:08 -07:00
launchType: "FARGATE",
2024-05-17 23:18:15 -07:00
networkConfiguration: {
awsvpcConfiguration: {
2024-05-21 00:57:52 -07:00
securityGroups: [process.env.AWS_ECS_SECURITY_GROUP!],
2024-05-17 23:18:15 -07:00
subnets: [
"subnet-06d04f2a6ebb1710c",
"subnet-097c000f157c26a78",
"subnet-00f931ecbabaf87dd",
"subnet-0adcb82d77db9f263",
"subnet-0c6874150d8e63a7c",
"subnet-0b76f9ee3fe20660d",
],
assignPublicIp: "ENABLED",
},
},
2024-05-26 12:18:09 -07:00
})
2024-05-17 23:18:15 -07:00
try {
2024-05-26 12:18:09 -07:00
const response = await ecsClient.send(command)
console.log("started server:", response.service?.serviceName)
2024-05-21 00:57:52 -07:00
2024-05-26 12:18:09 -07:00
return { success: true, message: "" }
2024-05-23 01:35:08 -07:00
2024-05-21 00:57:52 -07:00
// store in workers kv:
// {
// userId: {
// sandboxId,
// serviceName,
// startedAt,
// }
// }
2024-05-23 01:35:08 -07:00
} catch (error: any) {
// console.error("Error starting server:", error.message);
return {
success: false,
message: `Error starting server: ${error.message}. Try again in a minute, or contact @ishaandey_ on Twitter/X if it still doesn't work.`,
2024-05-26 12:18:09 -07:00
}
2024-05-17 23:18:15 -07:00
}
2024-05-01 02:22:02 -04:00
}