From 1edc0d39c2e8de94bf699bd428f36f3bf53660de Mon Sep 17 00:00:00 2001 From: yoshi Date: Wed, 30 Apr 2025 15:09:22 -0700 Subject: [PATCH] modified index.js --- deployment-service/fly.toml | 3 +- .../snakeapi_service/entrypoint.sh | 11 +- .../snakeapi_service/snakeapi_server.py | 80 +++++++---- deployment-service/src/index.js | 132 +++++++----------- package-lock.json | 6 + 5 files changed, 113 insertions(+), 119 deletions(-) create mode 100644 package-lock.json diff --git a/deployment-service/fly.toml b/deployment-service/fly.toml index d5119fb..09af4c1 100644 --- a/deployment-service/fly.toml +++ b/deployment-service/fly.toml @@ -2,7 +2,7 @@ app = 'deployment-service-test' primary_region = 'sea' [build] -dockerfile = "dockerfile" +dockerfile = "Dockerfile" [env] FLY_ORG="personal" @@ -12,6 +12,7 @@ dockerfile = "dockerfile" AWS_ENDPOINT_URL_S3="https://fly.storage.tigris.dev" AWS_REGION="auto" IMAGE_REF = "registry.fly.io/snakeapi-template:latest" + FLY_API_BASE_URL = "https://api.machines.dev/v1" [http_service] internal_port = 3006 diff --git a/deployment-service/snakeapi_service/entrypoint.sh b/deployment-service/snakeapi_service/entrypoint.sh index cbb5f65..f57164c 100644 --- a/deployment-service/snakeapi_service/entrypoint.sh +++ b/deployment-service/snakeapi_service/entrypoint.sh @@ -2,15 +2,16 @@ NOTEBOOK_DIR="notebooks" mkdir -p "${NOTEBOOK_DIR}" -# fetch latest notebook +# fetch latest notebook via prefix sync aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \ s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/" -# convert to Python script for dynamic import +# pick latest .ipynb and normalize name latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1) if [ -n "$latest_ipynb" ]; then - jupyter nbconvert --to script "$latest_ipynb" --output "${NOTEBOOK_DIR}/notebook.py" + mv "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb" + jupyter nbconvert --to script "${NOTEBOOK_DIR}/notebook.ipynb" --output "${NOTEBOOK_DIR}/notebook.py" + export NOTEBOOK_PATH="$(pwd)/${NOTEBOOK_DIR}/notebook.py" fi -# start the Flask server -python snakeapi_server.py +exec python snakeapi_server.py diff --git a/deployment-service/snakeapi_service/snakeapi_server.py b/deployment-service/snakeapi_service/snakeapi_server.py index c81317a..79d0578 100644 --- a/deployment-service/snakeapi_service/snakeapi_server.py +++ b/deployment-service/snakeapi_service/snakeapi_server.py @@ -1,44 +1,62 @@ -# snakeapi_service/snakeapi_server.py import os +import logging +import typing import importlib.util -from flask import Flask, request +from flask import Flask, request, jsonify from flask_cors import CORS -# load notebook code as module -spec = importlib.util.spec_from_file_location( - "nb_module", os.path.join("notebooks", "notebook.py") -) -nb = importlib.util.module_from_spec(spec) -spec.loader.exec_module(nb) - +# load handlers from converted notebook +notebook_path = os.environ.get("NOTEBOOK_PATH") +if not notebook_path or not os.path.isfile(notebook_path): + raise RuntimeError(f"Notebook module not found: {notebook_path}") +spec = importlib.util.spec_from_file_location("notebook_module", notebook_path) +notebook_module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(notebook_module) handlers = { - "info": nb.info, - "start": nb.start, - "move": nb.move, - "end": nb.end + "info": notebook_module.info, + "start": notebook_module.start, + "move": notebook_module.move, + "end": notebook_module.end, } -app = Flask(__name__) -CORS(app) +def run_server(handlers: typing.Dict): + app = Flask(__name__) + CORS(app) -@app.route("/", methods=["GET"]) -def on_info(): - return handlers["info"]() + @app.get("/") + def on_info(): + return handlers["info"]() -@app.route("/start", methods=["POST"]) -def on_start(): - handlers["start"](request.get_json()) - return "ok" + @app.post("/start") + def on_start(): + game_state = request.get_json() + handlers["start"](game_state) + return "ok" -@app.route("/move", methods=["POST"]) -def on_move(): - return handlers["move"](request.get_json()) + @app.post("/move") + def on_move(): + game_state = request.get_json() + return handlers["move"](game_state) -@app.route("/end", methods=["POST"]) -def on_end(): - handlers["end"](request.get_json()) - return "ok" + @app.post("/end") + def on_end(): + end_game = request.get_json() + handlers["end"](end_game) + return "ok" + + @app.get("/notebook") + def get_notebook(): + with open(notebook_path.replace('.py', '.ipynb'), "r", encoding="utf-8") as f: + return f.read(), 200, {"Content-Type": "application/json"} + + @app.get("/notebook/path") + def get_notebook_path(): + return jsonify({"path": notebook_path}) + + host = "::" + port = int(os.environ.get("PORT", "3006")) + logging.getLogger("werkzeug").setLevel(logging.ERROR) + app.run(host=host, port=port) if __name__ == "__main__": - port = int(os.environ.get("PORT", "3006")) - app.run(host="0.0.0.0", port=port) + run_server(handlers) diff --git a/deployment-service/src/index.js b/deployment-service/src/index.js index 9f53171..910c182 100644 --- a/deployment-service/src/index.js +++ b/deployment-service/src/index.js @@ -1,3 +1,5 @@ +// src/index.js + const express = require('express'); const fs = require('fs'); const path = require('path'); @@ -15,6 +17,19 @@ const { IMAGE_REF } = process.env; +// --- ENV debug --- +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 ---'); + +// S3 client const s3 = new AWS.S3({ endpoint: AWS_ENDPOINT_URL_S3, region: AWS_REGION, @@ -22,6 +37,7 @@ const s3 = new AWS.S3({ s3ForcePathStyle: true }); +// Fly Machines API client function createFlyClient() { return axios.create({ baseURL: 'https://api.machines.dev/v1', @@ -36,55 +52,54 @@ const app = express(); app.use(express.json({ limit: '10mb' })); app.post('/deploy', async (req, res) => { + console.log('Received /deploy:', req.body); const { appName, region = 'sea', notebookName } = req.body; if (!appName || !notebookName) { return res.status(400).json({ error: 'appName and notebookName required' }); } + // resolve path based on SSH-inspected layout + const notebookPath = path.join(__dirname, '../snakeapi_service/notebooks', notebookName); + console.log('Resolved notebookPath:', notebookPath); + console.log('File exists:', fs.existsSync(notebookPath)); + if (!fs.existsSync(notebookPath)) { + console.error('Notebook not found at:', notebookPath); + return res.status(500).json({ error: `Notebook not found: ${notebookPath}` }); + } + try { const fly = createFlyClient(); + console.log('Creating Fly app:', appName); await fly.post('/apps', { app_name: appName, org_slug: FLY_ORG, primary_region: region }); - await fly.post(`/apps/${appName}/secrets`, { - secrets: { - INSTANCE_PREFIX: appName, - BUCKET_NAME: COMMON_BUCKET, - AWS_ACCESS_KEY_ID, - AWS_SECRET_ACCESS_KEY, - AWS_ENDPOINT_URL_S3, - AWS_REGION - } - }); - - const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName); - if (!fs.existsSync(notebookFile)) { - throw new Error(`Notebook file ${notebookName} not found.`); - } - - const notebookData = fs.readFileSync(notebookFile); - const timestamp = Date.now(); - - const notebookKey = `${appName}/notebooks/${timestamp}-notebook.ipynb`; - + console.log('Uploading notebook to S3'); + const data = fs.readFileSync(notebookPath); + const key = `${appName}/notebooks/${Date.now()}-notebook.ipynb`; + console.log('S3 key:', key); await s3.putObject({ Bucket: COMMON_BUCKET, - Key: notebookKey, - Body: notebookData, + Key: key, + Body: data, ContentType: 'application/json' }).promise(); + console.log('Creating machine'); const machineConfig = { name: `${appName}-machine`, + region, + count: 1, + vm_size: 'shared-cpu-1x', + autostart: true, config: { image: IMAGE_REF, env: { INSTANCE_PREFIX: appName, - NOTEBOOK_KEY: notebookKey, + NOTEBOOK_KEY: key, BUCKET_NAME: COMMON_BUCKET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, @@ -92,69 +107,22 @@ app.post('/deploy', async (req, res) => { AWS_REGION }, services: [{ - ports: [{ port: 3006, handlers: ["http"] }], - protocol: "tcp", - internal_port: 3006 + internal_port: 3006, + protocol: 'tcp', + ports: [{ port: 80, handlers: ['http'] }] }] } }; - + console.log('Machine config:', machineConfig); await fly.post(`/apps/${appName}/machines`, machineConfig); - res.json({ - status: 'created', - app: appName, - url: `https://${appName}.fly.dev` - }); - } catch (error) { - res.status(500).json({ error: error.response?.data || error.message }); + console.log('Deployment successful:', appName); + return res.json({ status: 'created', url: `https://${appName}.fly.dev` }); + } catch (err) { + console.error('Deployment error:', err.stack || err); + return res.status(500).json({ error: err.response?.data || err.message }); } }); -app.post('/upload', async (req, res) => { - const { appName, notebookName } = req.body; - if (!appName || !notebookName) { - return res.status(400).json({ error: 'appName and notebookName required' }); - } - - try { - const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName); - if (!fs.existsSync(notebookFile)) { - throw new Error(`Notebook file ${notebookName} not found.`); - } - - const notebookData = fs.readFileSync(notebookFile); - const timestamp = Date.now(); - - const notebookKey = `${appName}/notebooks/${timestamp}-notebook.ipynb`; - - await s3.putObject({ - Bucket: COMMON_BUCKET, - Key: notebookKey, - Body: notebookData, - ContentType: 'application/json' - }).promise(); - - res.json({ status: 'uploaded', notebookKey }); - } catch (error) { - res.status(500).json({ error: error.message }); - } -}); - -app.delete('/delete/:appName', async (req, res) => { - const appName = req.params.appName; - if (!appName) { - return res.status(400).json({ error: 'appName required' }); - } - - try { - const fly = createFlyClient(); - await fly.delete(`/apps/${appName}`); - res.json({ status: 'deleted', app: appName }); - } catch (error) { - res.status(500).json({ error: error.response?.data || error.message }); - } -}); - -const port = process.env.PORT || 3006; -app.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`)); +const PORT = process.env.PORT || 3006; +app.listen(PORT, '0.0.0.0', () => console.log(`Listening on port ${PORT}`)); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..523b444 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "bytecamp-services", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}