revert to local hosting
This commit is contained in:
@ -1,15 +1,6 @@
|
||||
"use server"
|
||||
|
||||
import { revalidatePath } from "next/cache"
|
||||
import ecsClient, { ec2Client } from "./ecs"
|
||||
import {
|
||||
CreateServiceCommand,
|
||||
DescribeClustersCommand,
|
||||
DescribeServicesCommand,
|
||||
DescribeTasksCommand,
|
||||
ListTasksCommand,
|
||||
} from "@aws-sdk/client-ecs"
|
||||
import { DescribeNetworkInterfacesCommand } from "@aws-sdk/client-ec2"
|
||||
|
||||
export async function createSandbox(body: {
|
||||
type: string
|
||||
@ -87,142 +78,3 @@ export async function unshareSandbox(sandboxId: string, userId: string) {
|
||||
|
||||
revalidatePath(`/code/${sandboxId}`)
|
||||
}
|
||||
|
||||
export async function describeService(serviceName: string) {
|
||||
const command = new DescribeServicesCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
services: [serviceName],
|
||||
})
|
||||
|
||||
return await ecsClient.send(command)
|
||||
}
|
||||
|
||||
export async function getTaskIp(serviceName: string) {
|
||||
const listCommand = new ListTasksCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
serviceName,
|
||||
})
|
||||
|
||||
const listResponse = await ecsClient.send(listCommand)
|
||||
const taskArns = listResponse.taskArns
|
||||
|
||||
const describeCommand = new DescribeTasksCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
tasks: taskArns,
|
||||
})
|
||||
|
||||
const describeResponse = await ecsClient.send(describeCommand)
|
||||
const tasks = describeResponse.tasks
|
||||
const taskAttachment = tasks?.[0].attachments?.[0].details
|
||||
if (!taskAttachment) {
|
||||
throw new Error("Task attachment not found")
|
||||
}
|
||||
|
||||
const eni = taskAttachment.find(
|
||||
(detail) => detail.name === "networkInterfaceId"
|
||||
)?.value
|
||||
if (!eni) {
|
||||
throw new Error("Network interface not found")
|
||||
}
|
||||
|
||||
const describeNetworkInterfacesCommand = new DescribeNetworkInterfacesCommand(
|
||||
{
|
||||
NetworkInterfaceIds: [eni],
|
||||
}
|
||||
)
|
||||
const describeNetworkInterfacesResponse = await ec2Client.send(
|
||||
describeNetworkInterfacesCommand
|
||||
)
|
||||
|
||||
const ip =
|
||||
describeNetworkInterfacesResponse.NetworkInterfaces?.[0].Association
|
||||
?.PublicIp
|
||||
if (!ip) {
|
||||
throw new Error("Public IP not found")
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
|
||||
export async function doesServiceExist(serviceName: string) {
|
||||
const response = await describeService(serviceName)
|
||||
const activeServices = response.services?.filter(
|
||||
(service) => service.status === "ACTIVE"
|
||||
)
|
||||
|
||||
console.log("activeServices: ", activeServices)
|
||||
|
||||
return activeServices?.length === 1
|
||||
}
|
||||
|
||||
async function countServices() {
|
||||
const command = new DescribeClustersCommand({
|
||||
clusters: [process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!],
|
||||
})
|
||||
|
||||
const response = await ecsClient.send(command)
|
||||
return response.clusters?.[0].activeServicesCount!
|
||||
}
|
||||
|
||||
export async function startServer(
|
||||
serviceName: string
|
||||
): Promise<{ success: boolean; message: string }> {
|
||||
const serviceExists = await doesServiceExist(serviceName)
|
||||
if (serviceExists) {
|
||||
return { success: true, message: "" }
|
||||
}
|
||||
|
||||
const activeServices = await countServices()
|
||||
if (activeServices >= 100) {
|
||||
return {
|
||||
success: false,
|
||||
message:
|
||||
"Too many servers are running! Please try again later or contact @ishaandey_ on Twitter/X.",
|
||||
}
|
||||
}
|
||||
|
||||
const command = new CreateServiceCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
serviceName,
|
||||
taskDefinition: "Sandbox1",
|
||||
desiredCount: 1,
|
||||
launchType: "FARGATE",
|
||||
networkConfiguration: {
|
||||
awsvpcConfiguration: {
|
||||
securityGroups: [process.env.AWS_ECS_SECURITY_GROUP!],
|
||||
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.service?.serviceName)
|
||||
|
||||
return { success: true, message: "" }
|
||||
|
||||
// store in workers kv:
|
||||
// {
|
||||
// userId: {
|
||||
// sandboxId,
|
||||
// serviceName,
|
||||
// startedAt,
|
||||
|
||||
// }
|
||||
// }
|
||||
} 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.`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
import { ECSClient } from "@aws-sdk/client-ecs";
|
||||
import { EC2Client } from "@aws-sdk/client-ec2";
|
||||
|
||||
const ecsClient = new ECSClient({
|
||||
region: "us-east-1",
|
||||
credentials: {
|
||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
|
||||
},
|
||||
});
|
||||
|
||||
export const ec2Client = new EC2Client({
|
||||
region: "us-east-1",
|
||||
credentials: {
|
||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
|
||||
},
|
||||
});
|
||||
|
||||
export default ecsClient;
|
@ -2,13 +2,6 @@ import { type ClassValue, clsx } from "clsx"
|
||||
// import { toast } from "sonner"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { Sandbox, TFile, TFolder } from "./types"
|
||||
import { Service } from "@aws-sdk/client-ecs"
|
||||
import {
|
||||
describeService,
|
||||
doesServiceExist,
|
||||
getTaskIp,
|
||||
startServer,
|
||||
} from "./actions"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
@ -68,89 +61,3 @@ export function addNew(
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
export function checkServiceStatus(serviceName: string): Promise<Service> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let tries = 0
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
tries++
|
||||
|
||||
if (tries > 40) {
|
||||
clearInterval(interval)
|
||||
reject(new Error("Timed out."))
|
||||
}
|
||||
|
||||
const response = await describeService(serviceName)
|
||||
const activeServices = response.services?.filter(
|
||||
(service) => service.status === "ACTIVE"
|
||||
)
|
||||
console.log("Checking activeServices status", activeServices)
|
||||
|
||||
if (activeServices?.length === 1) {
|
||||
const service = activeServices?.[0]
|
||||
if (
|
||||
service.runningCount === service.desiredCount &&
|
||||
service.deployments?.length === 1
|
||||
) {
|
||||
if (service.deployments[0].rolloutState === "COMPLETED") {
|
||||
clearInterval(interval)
|
||||
resolve(service)
|
||||
} else if (service.deployments[0].rolloutState === "FAILED") {
|
||||
clearInterval(interval)
|
||||
reject(new Error("Deployment failed."))
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
clearInterval(interval)
|
||||
reject(error)
|
||||
}
|
||||
}, 3000)
|
||||
})
|
||||
}
|
||||
|
||||
export async function setupServer({
|
||||
sandboxId,
|
||||
setIsServiceRunning,
|
||||
setIsDeploymentActive,
|
||||
setTaskIp,
|
||||
setDidFail,
|
||||
toast,
|
||||
}: {
|
||||
sandboxId: string
|
||||
setIsServiceRunning: React.Dispatch<React.SetStateAction<boolean>>
|
||||
setIsDeploymentActive: React.Dispatch<React.SetStateAction<boolean>>
|
||||
setTaskIp: React.Dispatch<React.SetStateAction<string | undefined>>
|
||||
setDidFail: React.Dispatch<React.SetStateAction<boolean>>
|
||||
toast: any
|
||||
}) {
|
||||
const doesExist = await doesServiceExist(sandboxId)
|
||||
|
||||
if (!doesExist) {
|
||||
const response = await startServer(sandboxId)
|
||||
|
||||
if (!response.success) {
|
||||
toast.error(response.message)
|
||||
setDidFail(true)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
setIsServiceRunning(true)
|
||||
|
||||
try {
|
||||
if (!doesExist) {
|
||||
await checkServiceStatus(sandboxId)
|
||||
}
|
||||
|
||||
setIsDeploymentActive(true)
|
||||
|
||||
const taskIp = await getTaskIp(sandboxId)
|
||||
setTaskIp(taskIp)
|
||||
} catch (error) {
|
||||
toast.error("An error occurred while initializing your server.")
|
||||
setDidFail(true)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user