2025-05-05 14:50:05 -07:00
|
|
|
const express = require("express");
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
const path = require("path");
|
|
|
|
|
const AWS = require("aws-sdk");
|
|
|
|
|
const axios = require("axios");
|
|
|
|
|
require("dotenv").config();
|
2025-04-25 09:21:18 -07:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
FLY_ORG,
|
|
|
|
|
COMMON_BUCKET,
|
|
|
|
|
AWS_ACCESS_KEY_ID,
|
|
|
|
|
AWS_SECRET_ACCESS_KEY,
|
|
|
|
|
AWS_ENDPOINT_URL_S3,
|
2025-04-29 11:47:27 -07:00
|
|
|
AWS_REGION,
|
2025-04-29 15:01:23 -07:00
|
|
|
FLY_ACCESS_TOKEN,
|
2025-05-05 14:50:05 -07:00
|
|
|
IMAGE_REF,
|
2025-04-25 09:21:18 -07:00
|
|
|
} = process.env;
|
|
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Log environment variables for debugging
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("--- ENV START ---");
|
|
|
|
|
console.log("FLY_ORG: ", FLY_ORG);
|
|
|
|
|
console.log("COMMON_BUCKET: ", COMMON_BUCKET);
|
|
|
|
|
console.log(
|
|
|
|
|
"AWS_ACCESS_KEY_ID: ",
|
|
|
|
|
AWS_ACCESS_KEY_ID ? "(found)" : "(NOT SET)"
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY:",
|
|
|
|
|
AWS_SECRET_ACCESS_KEY ? "(found)" : "(NOT SET)"
|
|
|
|
|
);
|
|
|
|
|
console.log("AWS_ENDPOINT_URL_S3: ", AWS_ENDPOINT_URL_S3);
|
|
|
|
|
console.log("AWS_REGION: ", AWS_REGION);
|
|
|
|
|
console.log(
|
|
|
|
|
"FLY_ACCESS_TOKEN: ",
|
|
|
|
|
FLY_ACCESS_TOKEN ? "(found)" : "(NOT SET)"
|
|
|
|
|
);
|
|
|
|
|
console.log("IMAGE_REF: ", IMAGE_REF);
|
|
|
|
|
console.log("--- ENV END ---");
|
2025-04-30 15:09:22 -07:00
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Initialize S3 client
|
2025-04-29 11:47:27 -07:00
|
|
|
const s3 = new AWS.S3({
|
|
|
|
|
endpoint: AWS_ENDPOINT_URL_S3,
|
|
|
|
|
region: AWS_REGION,
|
|
|
|
|
credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
|
2025-05-05 14:50:05 -07:00
|
|
|
s3ForcePathStyle: true,
|
2025-04-29 11:47:27 -07:00
|
|
|
});
|
2025-04-25 09:21:18 -07:00
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Create Fly Machines API client
|
2025-04-29 11:47:27 -07:00
|
|
|
function createFlyClient() {
|
2025-04-25 09:21:18 -07:00
|
|
|
return axios.create({
|
2025-05-05 14:50:05 -07:00
|
|
|
baseURL: "https://api.machines.dev/v1",
|
2025-04-25 09:21:18 -07:00
|
|
|
headers: {
|
2025-04-29 11:47:27 -07:00
|
|
|
Authorization: `Bearer ${FLY_ACCESS_TOKEN}`,
|
2025-05-05 14:50:05 -07:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
2025-04-25 09:21:18 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Create Fly GraphQL client (for IP allocation)
|
2025-04-30 15:29:10 -07:00
|
|
|
const gqlClient = axios.create({
|
2025-05-05 14:50:05 -07:00
|
|
|
baseURL: "https://api.fly.io/graphql",
|
2025-04-30 15:29:10 -07:00
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${FLY_ACCESS_TOKEN}`,
|
2025-05-05 14:50:05 -07:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
2025-04-30 15:29:10 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Define GraphQL mutation for IP allocation
|
2025-04-30 15:29:10 -07:00
|
|
|
const ALLOCATE_IP_MUTATION = `
|
|
|
|
|
mutation AllocateIp($input: AllocateIPAddressInput!) {
|
|
|
|
|
allocateIpAddress(input: $input) {
|
|
|
|
|
ipAddress {
|
|
|
|
|
address
|
|
|
|
|
type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2025-04-29 11:47:27 -07:00
|
|
|
const app = express();
|
2025-05-05 14:50:05 -07:00
|
|
|
app.use(express.json({ limit: "10mb" }));
|
|
|
|
|
|
|
|
|
|
app.post("/deploy", async (req, res) => {
|
|
|
|
|
console.log("Received /deploy:", req.body);
|
|
|
|
|
const {
|
|
|
|
|
appName,
|
|
|
|
|
// , region
|
|
|
|
|
// , notebookName
|
|
|
|
|
} = req.body;
|
|
|
|
|
if (!appName) {
|
|
|
|
|
return res.status(400).json({ error: "appName required" });
|
2025-04-29 12:22:48 -07:00
|
|
|
}
|
2025-04-25 09:21:18 -07:00
|
|
|
|
2025-04-29 11:47:27 -07:00
|
|
|
try {
|
|
|
|
|
const fly = createFlyClient();
|
2025-04-29 12:22:48 -07:00
|
|
|
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Creating Fly app:", appName);
|
|
|
|
|
await fly.post("/apps", {
|
2025-04-29 13:28:25 -07:00
|
|
|
app_name: appName,
|
2025-04-29 12:22:48 -07:00
|
|
|
org_slug: FLY_ORG,
|
2025-05-05 14:50:05 -07:00
|
|
|
primary_region: "sea",
|
2025-04-29 12:22:48 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Creating machine");
|
2025-04-29 15:01:23 -07:00
|
|
|
const machineConfig = {
|
|
|
|
|
name: `${appName}-machine`,
|
2025-05-05 14:50:05 -07:00
|
|
|
region: "sea",
|
2025-04-30 15:09:22 -07:00
|
|
|
count: 1,
|
2025-05-05 14:50:05 -07:00
|
|
|
vm_size: "shared-cpu-1x",
|
2025-04-30 15:09:22 -07:00
|
|
|
autostart: true,
|
2025-04-29 15:01:23 -07:00
|
|
|
config: {
|
|
|
|
|
image: IMAGE_REF,
|
|
|
|
|
env: {
|
|
|
|
|
INSTANCE_PREFIX: appName,
|
2025-05-05 14:50:05 -07:00
|
|
|
// NOTEBOOK_KEY: key,
|
2025-05-02 00:12:05 -07:00
|
|
|
COMMON_BUCKET: COMMON_BUCKET,
|
2025-04-29 15:01:23 -07:00
|
|
|
AWS_ACCESS_KEY_ID,
|
|
|
|
|
AWS_SECRET_ACCESS_KEY,
|
|
|
|
|
AWS_ENDPOINT_URL_S3,
|
2025-05-05 14:50:05 -07:00
|
|
|
AWS_REGION,
|
2025-04-29 15:01:23 -07:00
|
|
|
},
|
2025-05-02 00:12:05 -07:00
|
|
|
http_service: {
|
|
|
|
|
internal_port: 8000,
|
|
|
|
|
force_https: true,
|
2025-05-05 14:50:05 -07:00
|
|
|
auto_stop_machines: "stop",
|
2025-05-02 00:12:05 -07:00
|
|
|
auto_start_machines: true,
|
|
|
|
|
min_machines_running: 0,
|
2025-05-05 14:50:05 -07:00
|
|
|
processes: ["app"],
|
2025-05-02 00:12:05 -07:00
|
|
|
},
|
|
|
|
|
services: [
|
|
|
|
|
{
|
2025-05-05 14:50:05 -07:00
|
|
|
protocol: "tcp",
|
2025-05-02 00:12:05 -07:00
|
|
|
internal_port: 8000,
|
|
|
|
|
ports: [
|
2025-05-05 14:50:05 -07:00
|
|
|
{ port: 443, handlers: ["tls", "http"] },
|
|
|
|
|
{ port: 80, handlers: ["http"] },
|
|
|
|
|
],
|
|
|
|
|
},
|
2025-05-02 00:22:59 -07:00
|
|
|
],
|
|
|
|
|
guest: {
|
|
|
|
|
memory_mb: 512,
|
2025-05-05 14:50:05 -07:00
|
|
|
cpu_kind: "shared",
|
|
|
|
|
cpus: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-04-29 15:01:23 -07:00
|
|
|
};
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Machine config:", JSON.stringify(machineConfig, null, 2));
|
2025-04-29 15:01:23 -07:00
|
|
|
await fly.post(`/apps/${appName}/machines`, machineConfig);
|
2025-04-29 12:22:48 -07:00
|
|
|
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Allocating IPv4 via GraphQL API");
|
|
|
|
|
const v4resp = await gqlClient.post("", {
|
2025-04-30 15:29:10 -07:00
|
|
|
query: ALLOCATE_IP_MUTATION,
|
2025-05-05 14:50:05 -07:00
|
|
|
variables: { input: { appId: appName, type: "v4" } },
|
2025-04-30 15:29:10 -07:00
|
|
|
});
|
|
|
|
|
const ipv4 = v4resp.data.data.allocateIpAddress.ipAddress.address;
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Allocated IPv4:", ipv4);
|
2025-04-30 15:29:10 -07:00
|
|
|
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Allocating IPv6 via GraphQL API");
|
|
|
|
|
const v6resp = await gqlClient.post("", {
|
2025-04-30 15:29:10 -07:00
|
|
|
query: ALLOCATE_IP_MUTATION,
|
2025-05-05 14:50:05 -07:00
|
|
|
variables: { input: { appId: appName, type: "v6" } },
|
2025-04-30 15:29:10 -07:00
|
|
|
});
|
|
|
|
|
const ipv6 = v6resp.data.data.allocateIpAddress.ipAddress.address;
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Allocated IPv6:", ipv6);
|
2025-04-30 15:29:10 -07:00
|
|
|
|
2025-05-07 23:30:37 -07:00
|
|
|
const machines = await fly.get(`/apps/${appName}/machines`);
|
|
|
|
|
const firstPrivateIp = machines.data[0]?.private_ip;
|
|
|
|
|
if (!firstPrivateIp) {
|
|
|
|
|
console.error("No private IP found for the machine:", machines.data);
|
|
|
|
|
return machines.status(500).json({ error: "No private IP found" });
|
|
|
|
|
}
|
|
|
|
|
console.log("First private IP:", firstPrivateIp);
|
|
|
|
|
|
2025-04-30 15:29:10 -07:00
|
|
|
return res.json({
|
2025-05-05 14:50:05 -07:00
|
|
|
status: "created",
|
2025-04-30 15:29:10 -07:00
|
|
|
app: appName,
|
|
|
|
|
ipv4,
|
2025-05-07 23:30:37 -07:00
|
|
|
ipv6 : firstPrivateIp,
|
2025-04-30 15:29:10 -07:00
|
|
|
url_v4: `http://${ipv4}`,
|
2025-05-07 23:30:37 -07:00
|
|
|
url_v6: `http://[${firstPrivateIp}]`,
|
2025-04-30 15:29:10 -07:00
|
|
|
});
|
2025-04-30 15:09:22 -07:00
|
|
|
} catch (err) {
|
2025-05-05 14:50:05 -07:00
|
|
|
console.error(
|
|
|
|
|
"Deployment error:",
|
|
|
|
|
err.response?.data || err.stack || err.message
|
|
|
|
|
);
|
2025-04-30 15:09:22 -07:00
|
|
|
return res.status(500).json({ error: err.response?.data || err.message });
|
2025-04-29 15:01:23 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
// Upload notebook to S3 for an existing app
|
2025-05-05 14:50:05 -07:00
|
|
|
app.post("/:appName/upload", async (req, res) => {
|
2025-05-02 00:12:05 -07:00
|
|
|
const { appName } = req.params;
|
|
|
|
|
const { notebookName, fileContentBase64 } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!notebookName || !fileContentBase64) {
|
2025-05-05 14:50:05 -07:00
|
|
|
return res
|
|
|
|
|
.status(400)
|
|
|
|
|
.json({ error: "notebookName and fileContentBase64 are required" });
|
2025-05-02 00:12:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-05-05 14:50:05 -07:00
|
|
|
const buffer = Buffer.from(fileContentBase64, "base64");
|
2025-05-02 00:12:05 -07:00
|
|
|
const key = `${appName}/notebooks/${notebookName}`;
|
|
|
|
|
|
|
|
|
|
console.log(`Uploading notebook to: s3://${COMMON_BUCKET}/${key}`);
|
2025-05-05 14:50:05 -07:00
|
|
|
await s3
|
|
|
|
|
.putObject({
|
|
|
|
|
Bucket: COMMON_BUCKET,
|
|
|
|
|
Key: key,
|
|
|
|
|
Body: buffer,
|
|
|
|
|
ContentType: "application/json",
|
|
|
|
|
})
|
|
|
|
|
.promise();
|
|
|
|
|
|
|
|
|
|
return res.json({ status: "uploaded", app: appName, key });
|
2025-05-02 00:12:05 -07:00
|
|
|
} catch (err) {
|
2025-05-05 14:50:05 -07:00
|
|
|
console.error("Notebook upload error:", err);
|
2025-05-02 00:12:05 -07:00
|
|
|
return res.status(500).json({ error: err.message });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Delete a Fly app
|
2025-05-05 14:50:05 -07:00
|
|
|
app.post("/:appName/delete", async (req, res) => {
|
2025-05-02 00:12:05 -07:00
|
|
|
const { appName } = req.params;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fly = createFlyClient();
|
2025-05-05 14:50:05 -07:00
|
|
|
console.log("Destroying Fly app:", appName);
|
2025-05-07 11:43:16 -07:00
|
|
|
|
|
|
|
|
//check if the app exists
|
|
|
|
|
console.log("Checking if app exists:", appName);
|
|
|
|
|
const appCheck = await fly.get(`/apps/${appName}`);
|
|
|
|
|
console.log("App check response:", appCheck.status);
|
|
|
|
|
if (appCheck.status !== 200) {
|
|
|
|
|
console.log("App not found:", appName);
|
|
|
|
|
return res.json({ status: "App not found", app: appName });
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 00:12:05 -07:00
|
|
|
await fly.delete(`/apps/${appName}`);
|
|
|
|
|
|
2025-05-05 14:50:05 -07:00
|
|
|
return res.json({ status: "deleted", app: appName });
|
2025-05-02 00:12:05 -07:00
|
|
|
} catch (err) {
|
2025-05-07 11:43:16 -07:00
|
|
|
const errorData = err.response?.data || err.stack || err.message;
|
|
|
|
|
console.error("App deletion error:", errorData);
|
|
|
|
|
return res.status(500).json({ errorData });
|
2025-05-02 00:12:05 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const LISTEN_PORT = process.env.PORT || 3006;
|
2025-05-05 14:50:05 -07:00
|
|
|
app.listen(LISTEN_PORT, "0.0.0.0", () => {
|
2025-05-02 00:12:05 -07:00
|
|
|
console.log(`Deployment service listening on port ${LISTEN_PORT}`);
|
|
|
|
|
});
|
2025-05-07 11:43:16 -07:00
|
|
|
|
|
|
|
|
//deploy fly app based on appname
|
|
|
|
|
app.post("/deploy/:appName", async (req, res) => {
|
|
|
|
|
const { appName } = req.params;
|
|
|
|
|
const { region } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!appName || !region) {
|
|
|
|
|
return res.status(400).json({ error: "appName and region are required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fly = createFlyClient();
|
|
|
|
|
console.log("Creating Fly app:", appName);
|
|
|
|
|
await fly.post("/apps", {
|
|
|
|
|
app_name: appName,
|
|
|
|
|
org_slug: FLY_ORG,
|
|
|
|
|
primary_region: region,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return res.json({ status: "created", app: appName });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("App creation error:", err.response?.data || err.message);
|
|
|
|
|
return res.status(500).json({ error: err.response?.data || err.message });
|
|
|
|
|
}
|
|
|
|
|
});
|