From 4b5c74c74493fe2728daccf9087ee8f988427310 Mon Sep 17 00:00:00 2001 From: yoshi Date: Thu, 8 May 2025 00:34:17 -0700 Subject: [PATCH] added deploy button --- assignment-service/routes/StudentRouter.js | 23 ++++++++++++++++++---- deployment-service/fly.toml | 3 --- deployment-service/src/index.js | 22 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/assignment-service/routes/StudentRouter.js b/assignment-service/routes/StudentRouter.js index 9a2e703..15fbb26 100644 --- a/assignment-service/routes/StudentRouter.js +++ b/assignment-service/routes/StudentRouter.js @@ -7,16 +7,17 @@ const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL; const DEPLOY_API_URL = process.env.DEPLOY_API_URL || "http://localhost:3600"; studentRouter.post("/save", async (req, res) => { - //get the app name and code and save the latest jupiter file in s3 bucket - const { appName ,code } = req.body; + //get the app name and code and save the latest jupyter file in s3 bucket + const { appName, code } = req.body; - //convert the code to jupyter file format const notebook = { cells: [ { cell_type: "code", execution_count: null, - metadata: {}, + metadata: { + language: "python" + }, outputs: [], source: code.split('\n').map(line => line + '\n') } @@ -123,4 +124,18 @@ studentRouter.post("/verify", async (req, res) => { } }); +// post restart from deployment service /appname/restart endpoint +studentRouter.post("/restart", async (req, res) => { + const { appName } = req.body; + console.log("Received request to restart app:", appName); + try { + const response = await axios.post(`${DEPLOY_API_URL}/${appName}/restart`); + console.log("Restart response:", response.data); + res.status(response.status).json(response.data); + } catch (error) { + console.error("Error restarting app:", error.message); + res.status(error.response?.status || 500).json({ error: error.message }); + } +}); + module.exports = studentRouter; diff --git a/deployment-service/fly.toml b/deployment-service/fly.toml index 6bd8fd1..6f3da2a 100644 --- a/deployment-service/fly.toml +++ b/deployment-service/fly.toml @@ -14,9 +14,6 @@ primary_region = 'sea' IMAGE_REF="registry.fly.io/snake-api-template:latest" FLY_API_BASE_URL = "https://api.machines.dev/v1" -[services] - internal_only = true - [http_service] internal_port = 3006 force_https = true diff --git a/deployment-service/src/index.js b/deployment-service/src/index.js index 1467d13..55f61ef 100644 --- a/deployment-service/src/index.js +++ b/deployment-service/src/index.js @@ -230,6 +230,28 @@ app.post("/:appName/upload", async (req, res) => { } }); +// restart a Fly app +app.post("/:appName/restart", async (req, res) => { + const { appName } = req.params; + try { + const fly = createFlyClient(); + const { data: machines } = await fly.get(`/apps/${appName}/machines`); + if (!machines || !Array.isArray(machines) || machines.length === 0) { + return res.status(404).json({ error: "No machines found for this app" }); + } + const results = await Promise.all( + machines.map(machine => + fly.post(`/apps/${appName}/machines/${machine.id}/restart`) + ) + ); + res.json({ status: "restarted", app: appName, count: results.length }); + } catch (err) { + console.error("Restart error:", err.response?.data || err.message); + res.status(500).json({ error: err.response?.data || err.message }); + } +}); + + // Delete a Fly app app.post("/:appName/delete", async (req, res) => { const { appName } = req.params;