submit assignment now creates an fly machine

This commit is contained in:
JBB0807 2025-05-05 14:50:05 -07:00
parent d326366448
commit 5ce01158ad
8 changed files with 215 additions and 128 deletions

View file

@ -43,15 +43,15 @@ async function convertToAssignment(req) {
const hashPassword = await encryptPassword(req.body.password); const hashPassword = await encryptPassword(req.body.password);
return { return {
campid: campid, campid: parseInt(campid),
programid: programid, programid: parseInt(programid),
studentname: studentname, studentname: studentname,
snakegameid: snakegameid, snakegameid: snakegameid,
originalfile: originalfile, originalfile: originalfile,
editablefile: editablefile, editablefile: editablefile,
assignmenturl: assignmenturl, assignmenturl: assignmenturl,
passwordhash: hashPassword, passwordhash: hashPassword,
instructorid: instructorid, instructorid: parseInt(instructorid),
}; };
} }

View file

@ -1,3 +1,3 @@
#DB_ASSIGNMENT_SERVICE_URL = "http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL = "http://db-assignment-service.internal:3000" DB_ASSIGNMENT_SERVICE_URL = "http://db-assignment-service.internal:3000"
DEPLOY_API_URL="http://localhost:3006"
NODE_PORT = 8080 NODE_PORT = 8080

View file

@ -1,6 +1,11 @@
NODE_ENV=development NODE_ENV=development
#DB_ASSIGNMENT_SERVICE_URL="http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL="http://localhost:3200" DB_ASSIGNMENT_SERVICE_URL="http://localhost:3200"
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42" DEPLOY_API_URL="http://127.0.0.1:3006"
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
AWS_ACCESS_KEY_ID='tid__NSmOVaGknqitaCySppZjqVTgJSdDFnFbWcQllkC_juHwkbQZO'
AWS_ENDPOINT_URL_S3='https://fly.storage.tigris.dev'
AWS_REGION='auto'
AWS_SECRET_ACCESS_KEY='tsec_6Bz1aMbfYQftuq5WfIVEDZkHwskU4MMjVywdtxSP6uxetEBvkSC2VHI9HfTeDgHr4D6kiz'
COMMON_BUCKET='snakeapi-deployment-test-bucket'
NODE_PORT=8082 NODE_PORT=8082

View file

@ -1,6 +1,4 @@
NODE_ENV=development NODE_ENV=test
#DB_ASSIGNMENT_SERVICE_URL="http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL="http://js-assignment-db-service:3200" DB_ASSIGNMENT_SERVICE_URL="http://js-assignment-db-service:3200"
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42" DEPLOY_API_URL="http://localhost:3006/deploy"
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
NODE_PORT=8082 NODE_PORT=8082

View file

@ -1,5 +0,0 @@
#DB_ASSIGNMENT_SERVICE_URL="http://localhost:3000"
DB_ASSIGNMENT_SERVICE_URL="http://js-assignment-db-service:3200"
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42"
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
PORT=8082

View file

@ -1,45 +1,98 @@
const intructorRouter = require("express").Router(); const intructorRouter = require("express").Router();
const passport = require("passport"); const passport = require("passport");
const axios = require("axios"); const axios = require("axios");
const multer = require('multer'); const multer = require("multer");
const FormData = require('form-data'); const FormData = require("form-data");
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
// const {
// COMMON_BUCKET,
// AWS_ACCESS_KEY_ID,
// AWS_SECRET_ACCESS_KEY,
// AWS_ENDPOINT_URL_S3,
// AWS_REGION,
// } = process.env;
// Log environment variables for debugging
// console.log('--- ENV START ---');
// 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('--- ENV END ---');
const DB_ASSIGNMENT_SERVICE_URL = const DB_ASSIGNMENT_SERVICE_URL =
process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000"; process.env.DB_ASSIGNMENT_SERVICE_URL || "http://localhost:3000";
const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600";
console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL); console.log("DB_ASSIGNMENT_SERVICE_URL:", DB_ASSIGNMENT_SERVICE_URL);
console.log("DEPLOY_API_URL:", DEPLOY_API_URL);
// 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),
// s3ForcePathStyle: true
// });
// Use memory storage to keep file in RAM // Use memory storage to keep file in RAM
const upload = multer({ storage: multer.memoryStorage() }); const upload = multer({ storage: multer.memoryStorage() });
// This endpoint is for instructors to create a new assignment // This endpoint is for instructors to create a new assignment
intructorRouter.post("/create", intructorRouter.post(
upload.single('file'), "/create",
// passport.authenticate("jwt", { session: false }), upload.single("file"),
// passport.authenticate("jwt", { session: false }),
async (req, res) => { async (req, res) => {
try { try {
const file = req.file; const file = req.file;
const assignmentData = req.body; const assignmentData = req.body;
if (!file) { if (!file) {
return res.status(400).send('No file uploaded.'); return res.status(400).send("No file uploaded.");
} }
await axios.post('https://target-api.com/endpoint', {
filename: file.originalname,
mimetype: file.mimetype,
content: file.buffer.toString('base64')
}, {
headers: {
'Content-Type': 'application/json'
}
});
console.log("Creating a new assignment with data:", req.body); console.log("Creating a new assignment with data:", req.body);
const response = await axios.post(`${DB_ASSIGNMENT_SERVICE_URL}/assignments`, req.body); const response = await axios.post(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments`,
req.body
);
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data); console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
// Upload the file to the S3 bucket
// console.log('Uploading notebook to S3');
// const key = `${assignmentData.appname}/notebooks/${Date.now()}-notebook.ipynb`;
// console.log('S3 key:', key);
// await s3.putObject({
// Bucket: COMMON_BUCKET,
// Key: key,
// Body: file.buffer,
// ContentType: 'application/json'
// }).promise();
// call upload api to upload the file to S3
console.log("Uploading file to:", `${DEPLOY_API_URL}/${assignmentData.appname}/upload`);
const uploadResponse = await axios.post(`${DEPLOY_API_URL}/${assignmentData.appname}/upload`, {
"appName": assignmentData.appname,
"notebookName": file.originalname,
"fileContentBase64": file.buffer.toString('base64'),
});
console.log('Response from DEPLOY_API_URL:', uploadResponse.data);
// Deploy a new Battlesnake API
console.log('Deploying a new Battlesnake API');
console.log("DEPLOY_API_URL:", DEPLOY_API_URL, assignmentData.appname);
const deployResponse = await axios.post(`${DEPLOY_API_URL}/deploy`, {
"appName": assignmentData.appname
});
console.log('Response from DEPLOY_API_URL:', deployResponse.data);
res.status(response.status).json(response.data); res.status(response.status).json(response.data);
} catch (error) { } catch (error) {
console.error("Error creating assignment:", error.message); console.error("Error creating assignment:", error.message);
@ -56,7 +109,7 @@ intructorRouter.get("/details/:id", async (req, res) => {
const response = await axios.get( const response = await axios.get(
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}` `${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`
); );
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data); console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
res.status(response.status).json(response.data); res.status(response.status).json(response.data);
} catch (error) { } catch (error) {

View file

@ -10,6 +10,21 @@ studentRouter.post("/save", (req, res) => {});
studentRouter.post("/deploy", (req, res) => {}); studentRouter.post("/deploy", (req, res) => {});
studentRouter.get("/assignment/:id", (req, res) => {
const assignmentId = req.params.id;
console.log("Fetching details for assignmentId:", assignmentId);
axios
.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${assignmentId}`)
.then((response) => {
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
res.status(response.status).json(response.data);
})
.catch((error) => {
console.error("Error fetching assignment details:", error.message);
res.status(error.response?.status || 500).json({ error: error.message });
});
});
studentRouter.post("/verify", async (req, res) => { studentRouter.post("/verify", async (req, res) => {
try { try {
const assignmentId = req.body.assignmentId; const assignmentId = req.body.assignmentId;

View file

@ -1,8 +1,9 @@
const express = require('express'); const express = require("express");
const fs = require('fs'); const fs = require("fs");
const path = require('path'); const path = require("path");
const AWS = require('aws-sdk'); const AWS = require("aws-sdk");
const axios = require('axios'); const axios = require("axios");
require("dotenv").config();
const { const {
FLY_ORG, FLY_ORG,
@ -12,47 +13,56 @@ const {
AWS_ENDPOINT_URL_S3, AWS_ENDPOINT_URL_S3,
AWS_REGION, AWS_REGION,
FLY_ACCESS_TOKEN, FLY_ACCESS_TOKEN,
IMAGE_REF IMAGE_REF,
} = process.env; } = process.env;
// Log environment variables for debugging // Log environment variables for debugging
console.log('--- ENV START ---'); console.log("--- ENV START ---");
console.log('FLY_ORG: ', FLY_ORG); console.log("FLY_ORG: ", FLY_ORG);
console.log('COMMON_BUCKET: ', COMMON_BUCKET); console.log("COMMON_BUCKET: ", COMMON_BUCKET);
console.log('AWS_ACCESS_KEY_ID: ', AWS_ACCESS_KEY_ID ? '(found)' : '(NOT SET)'); console.log(
console.log('AWS_SECRET_ACCESS_KEY:', AWS_SECRET_ACCESS_KEY ? '(found)' : '(NOT SET)'); "AWS_ACCESS_KEY_ID: ",
console.log('AWS_ENDPOINT_URL_S3: ', AWS_ENDPOINT_URL_S3); AWS_ACCESS_KEY_ID ? "(found)" : "(NOT SET)"
console.log('AWS_REGION: ', AWS_REGION); );
console.log('FLY_ACCESS_TOKEN: ', FLY_ACCESS_TOKEN ? '(found)' : '(NOT SET)'); console.log(
console.log('IMAGE_REF: ', IMAGE_REF); "AWS_SECRET_ACCESS_KEY:",
console.log('--- ENV END ---'); 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 ---");
// Initialize S3 client // Initialize S3 client
const s3 = new AWS.S3({ const s3 = new AWS.S3({
endpoint: AWS_ENDPOINT_URL_S3, endpoint: AWS_ENDPOINT_URL_S3,
region: AWS_REGION, region: AWS_REGION,
credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
s3ForcePathStyle: true s3ForcePathStyle: true,
}); });
// Create Fly Machines API client // Create Fly Machines API client
function createFlyClient() { function createFlyClient() {
return axios.create({ return axios.create({
baseURL: 'https://api.machines.dev/v1', baseURL: "https://api.machines.dev/v1",
headers: { headers: {
Authorization: `Bearer ${FLY_ACCESS_TOKEN}`, Authorization: `Bearer ${FLY_ACCESS_TOKEN}`,
'Content-Type': 'application/json' "Content-Type": "application/json",
} },
}); });
} }
// Create Fly GraphQL client (for IP allocation) // Create Fly GraphQL client (for IP allocation)
const gqlClient = axios.create({ const gqlClient = axios.create({
baseURL: 'https://api.fly.io/graphql', baseURL: "https://api.fly.io/graphql",
headers: { headers: {
Authorization: `Bearer ${FLY_ACCESS_TOKEN}`, Authorization: `Bearer ${FLY_ACCESS_TOKEN}`,
'Content-Type': 'application/json' "Content-Type": "application/json",
} },
}); });
// Define GraphQL mutation for IP allocation // Define GraphQL mutation for IP allocation
@ -68,164 +78,175 @@ mutation AllocateIp($input: AllocateIPAddressInput!) {
`; `;
const app = express(); const app = express();
app.use(express.json({ limit: '10mb' })); app.use(express.json({ limit: "10mb" }));
app.post('/deploy', async (req, res) => { app.post("/deploy", async (req, res) => {
console.log('Received /deploy:', req.body); console.log("Received /deploy:", req.body);
const { appName, region, notebookName } = req.body; const {
if (!appName || !notebookName) { appName,
return res.status(400).json({ error: 'appName and notebookName required' }); // , region
// , notebookName
} = req.body;
if (!appName) {
return res.status(400).json({ error: "appName required" });
} }
const notebookPath = path.join(__dirname, '../snakeapi_service/notebooks', notebookName); // const notebookPath = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
console.log('Resolved notebookPath:', notebookPath); // console.log('Resolved notebookPath:', notebookPath);
if (!fs.existsSync(notebookPath)) { // if (!fs.existsSync(notebookPath)) {
console.error('Notebook not found at:', notebookPath); // console.error('Notebook not found at:', notebookPath);
return res.status(500).json({ error: `Notebook not found: ${notebookPath}` }); // return res.status(500).json({ error: `Notebook not found: ${notebookPath}` });
} // }
try { try {
const fly = createFlyClient(); const fly = createFlyClient();
console.log('Creating Fly app:', appName); console.log("Creating Fly app:", appName);
await fly.post('/apps', { await fly.post("/apps", {
app_name: appName, app_name: appName,
org_slug: FLY_ORG, org_slug: FLY_ORG,
primary_region: region primary_region: "sea",
}); });
console.log('Uploading notebook to S3'); // console.log('Uploading notebook to S3');
const data = fs.readFileSync(notebookPath); // const data = fs.readFileSync(notebookPath);
const key = `${appName}/notebooks/${Date.now()}-notebook.ipynb`; // const key = `${appName}/notebooks/${Date.now()}-notebook.ipynb`;
console.log('S3 key:', key); // console.log('S3 key:', key);
await s3.putObject({ // await s3.putObject({
Bucket: COMMON_BUCKET, // Bucket: COMMON_BUCKET,
Key: key, // Key: key,
Body: data, // Body: data,
ContentType: 'application/json' // ContentType: 'application/json'
}).promise(); // }).promise();
console.log('Creating machine'); console.log("Creating machine");
const machineConfig = { const machineConfig = {
name: `${appName}-machine`, name: `${appName}-machine`,
region: 'sea', region: "sea",
count: 1, count: 1,
vm_size: 'shared-cpu-1x', vm_size: "shared-cpu-1x",
autostart: true, autostart: true,
config: { config: {
image: IMAGE_REF, image: IMAGE_REF,
env: { env: {
INSTANCE_PREFIX: appName, INSTANCE_PREFIX: appName,
NOTEBOOK_KEY: key, // NOTEBOOK_KEY: key,
COMMON_BUCKET: COMMON_BUCKET, COMMON_BUCKET: COMMON_BUCKET,
AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT_URL_S3, AWS_ENDPOINT_URL_S3,
AWS_REGION AWS_REGION,
}, },
http_service: { http_service: {
internal_port: 8000, internal_port: 8000,
force_https: true, force_https: true,
auto_stop_machines: 'stop', auto_stop_machines: "stop",
auto_start_machines: true, auto_start_machines: true,
min_machines_running: 0, min_machines_running: 0,
processes: ['app'] processes: ["app"],
}, },
services: [ services: [
{ {
protocol: 'tcp', protocol: "tcp",
internal_port: 8000, internal_port: 8000,
ports: [ ports: [
{ port: 443, handlers: ['tls', 'http'] }, { port: 443, handlers: ["tls", "http"] },
{ port: 80, handlers: ['http'] } { port: 80, handlers: ["http"] },
] ],
} },
], ],
guest: { guest: {
memory_mb: 512, memory_mb: 512,
cpu_kind: 'shared', cpu_kind: "shared",
cpus: 1 cpus: 1,
} },
} },
}; };
console.log('Machine config:', JSON.stringify(machineConfig, null, 2)); console.log("Machine config:", JSON.stringify(machineConfig, null, 2));
await fly.post(`/apps/${appName}/machines`, machineConfig); await fly.post(`/apps/${appName}/machines`, machineConfig);
console.log('Allocating IPv4 via GraphQL API'); console.log("Allocating IPv4 via GraphQL API");
const v4resp = await gqlClient.post('', { const v4resp = await gqlClient.post("", {
query: ALLOCATE_IP_MUTATION, query: ALLOCATE_IP_MUTATION,
variables: { input: { appId: appName, type: 'v4' } } variables: { input: { appId: appName, type: "v4" } },
}); });
const ipv4 = v4resp.data.data.allocateIpAddress.ipAddress.address; const ipv4 = v4resp.data.data.allocateIpAddress.ipAddress.address;
console.log('Allocated IPv4:', ipv4); console.log("Allocated IPv4:", ipv4);
console.log('Allocating IPv6 via GraphQL API'); console.log("Allocating IPv6 via GraphQL API");
const v6resp = await gqlClient.post('', { const v6resp = await gqlClient.post("", {
query: ALLOCATE_IP_MUTATION, query: ALLOCATE_IP_MUTATION,
variables: { input: { appId: appName, type: 'v6' } } variables: { input: { appId: appName, type: "v6" } },
}); });
const ipv6 = v6resp.data.data.allocateIpAddress.ipAddress.address; const ipv6 = v6resp.data.data.allocateIpAddress.ipAddress.address;
console.log('Allocated IPv6:', ipv6); console.log("Allocated IPv6:", ipv6);
return res.json({ return res.json({
status: 'created', status: "created",
app: appName, app: appName,
ipv4, ipv4,
ipv6, ipv6,
url_v4: `http://${ipv4}`, url_v4: `http://${ipv4}`,
url_v6: `http://[${ipv6}]` url_v6: `http://[${ipv6}]`,
}); });
} catch (err) { } catch (err) {
console.error('Deployment error:', err.response?.data || err.stack || err.message); console.error(
"Deployment error:",
err.response?.data || err.stack || err.message
);
return res.status(500).json({ error: err.response?.data || err.message }); return res.status(500).json({ error: err.response?.data || err.message });
} }
}); });
// Upload notebook to S3 for an existing app // Upload notebook to S3 for an existing app
app.post('/:appName/upload', async (req, res) => { app.post("/:appName/upload", async (req, res) => {
const { appName } = req.params; const { appName } = req.params;
const { notebookName, fileContentBase64 } = req.body; const { notebookName, fileContentBase64 } = req.body;
if (!notebookName || !fileContentBase64) { if (!notebookName || !fileContentBase64) {
return res.status(400).json({ error: 'notebookName and fileContentBase64 are required' }); return res
.status(400)
.json({ error: "notebookName and fileContentBase64 are required" });
} }
try { try {
const buffer = Buffer.from(fileContentBase64, 'base64'); const buffer = Buffer.from(fileContentBase64, "base64");
const key = `${appName}/notebooks/${notebookName}`; const key = `${appName}/notebooks/${notebookName}`;
console.log(`Uploading notebook to: s3://${COMMON_BUCKET}/${key}`); console.log(`Uploading notebook to: s3://${COMMON_BUCKET}/${key}`);
await s3.putObject({ await s3
Bucket: COMMON_BUCKET, .putObject({
Key: key, Bucket: COMMON_BUCKET,
Body: buffer, Key: key,
ContentType: 'application/json' Body: buffer,
}).promise(); ContentType: "application/json",
})
.promise();
return res.json({ status: 'uploaded', app: appName, key }); return res.json({ status: "uploaded", app: appName, key });
} catch (err) { } catch (err) {
console.error('Notebook upload error:', err); console.error("Notebook upload error:", err);
return res.status(500).json({ error: err.message }); return res.status(500).json({ error: err.message });
} }
}); });
// Delete a Fly app // Delete a Fly app
app.post('/:appName/delete', async (req, res) => { app.post("/:appName/delete", async (req, res) => {
const { appName } = req.params; const { appName } = req.params;
try { try {
const fly = createFlyClient(); const fly = createFlyClient();
console.log('Destroying Fly app:', appName); console.log("Destroying Fly app:", appName);
await fly.delete(`/apps/${appName}`); await fly.delete(`/apps/${appName}`);
return res.json({ status: 'deleted', app: appName }); return res.json({ status: "deleted", app: appName });
} catch (err) { } catch (err) {
console.error('App deletion error:', err.response?.data || err.message); console.error("App deletion error:", err.response?.data || err.message);
return res.status(500).json({ error: err.response?.data || err.message }); return res.status(500).json({ error: err.response?.data || err.message });
} }
}); });
const LISTEN_PORT = process.env.PORT || 3006; const LISTEN_PORT = process.env.PORT || 3006;
app.listen(LISTEN_PORT, '0.0.0.0', () => { app.listen(LISTEN_PORT, "0.0.0.0", () => {
console.log(`Deployment service listening on port ${LISTEN_PORT}`); console.log(`Deployment service listening on port ${LISTEN_PORT}`);
}); });