modified index.js

This commit is contained in:
yoshi 2025-04-30 15:09:22 -07:00
parent 52d62894b6
commit 1edc0d39c2
5 changed files with 113 additions and 119 deletions

View file

@ -2,7 +2,7 @@ app = 'deployment-service-test'
primary_region = 'sea' primary_region = 'sea'
[build] [build]
dockerfile = "dockerfile" dockerfile = "Dockerfile"
[env] [env]
FLY_ORG="personal" FLY_ORG="personal"
@ -12,6 +12,7 @@ dockerfile = "dockerfile"
AWS_ENDPOINT_URL_S3="https://fly.storage.tigris.dev" AWS_ENDPOINT_URL_S3="https://fly.storage.tigris.dev"
AWS_REGION="auto" AWS_REGION="auto"
IMAGE_REF = "registry.fly.io/snakeapi-template:latest" IMAGE_REF = "registry.fly.io/snakeapi-template:latest"
FLY_API_BASE_URL = "https://api.machines.dev/v1"
[http_service] [http_service]
internal_port = 3006 internal_port = 3006

View file

@ -2,15 +2,16 @@
NOTEBOOK_DIR="notebooks" NOTEBOOK_DIR="notebooks"
mkdir -p "${NOTEBOOK_DIR}" mkdir -p "${NOTEBOOK_DIR}"
# fetch latest notebook # fetch latest notebook via prefix sync
aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \ aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \
s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/" 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) latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1)
if [ -n "$latest_ipynb" ]; then 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 fi
# start the Flask server exec python snakeapi_server.py
python snakeapi_server.py

View file

@ -1,44 +1,62 @@
# snakeapi_service/snakeapi_server.py
import os import os
import logging
import typing
import importlib.util import importlib.util
from flask import Flask, request from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
# load notebook code as module # load handlers from converted notebook
spec = importlib.util.spec_from_file_location( notebook_path = os.environ.get("NOTEBOOK_PATH")
"nb_module", os.path.join("notebooks", "notebook.py") if not notebook_path or not os.path.isfile(notebook_path):
) raise RuntimeError(f"Notebook module not found: {notebook_path}")
nb = importlib.util.module_from_spec(spec) spec = importlib.util.spec_from_file_location("notebook_module", notebook_path)
spec.loader.exec_module(nb) notebook_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(notebook_module)
handlers = { handlers = {
"info": nb.info, "info": notebook_module.info,
"start": nb.start, "start": notebook_module.start,
"move": nb.move, "move": notebook_module.move,
"end": nb.end "end": notebook_module.end,
} }
app = Flask(__name__) def run_server(handlers: typing.Dict):
CORS(app) app = Flask(__name__)
CORS(app)
@app.route("/", methods=["GET"]) @app.get("/")
def on_info(): def on_info():
return handlers["info"]() return handlers["info"]()
@app.route("/start", methods=["POST"]) @app.post("/start")
def on_start(): def on_start():
handlers["start"](request.get_json()) game_state = request.get_json()
return "ok" handlers["start"](game_state)
return "ok"
@app.route("/move", methods=["POST"]) @app.post("/move")
def on_move(): def on_move():
return handlers["move"](request.get_json()) game_state = request.get_json()
return handlers["move"](game_state)
@app.route("/end", methods=["POST"]) @app.post("/end")
def on_end(): def on_end():
handlers["end"](request.get_json()) end_game = request.get_json()
return "ok" 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__": if __name__ == "__main__":
port = int(os.environ.get("PORT", "3006")) run_server(handlers)
app.run(host="0.0.0.0", port=port)

View file

@ -1,3 +1,5 @@
// src/index.js
const express = require('express'); const express = require('express');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -15,6 +17,19 @@ const {
IMAGE_REF IMAGE_REF
} = process.env; } = 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({ const s3 = new AWS.S3({
endpoint: AWS_ENDPOINT_URL_S3, endpoint: AWS_ENDPOINT_URL_S3,
region: AWS_REGION, region: AWS_REGION,
@ -22,6 +37,7 @@ const s3 = new AWS.S3({
s3ForcePathStyle: true s3ForcePathStyle: true
}); });
// 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',
@ -36,55 +52,54 @@ 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);
const { appName, region = 'sea', notebookName } = req.body; const { appName, region = 'sea', notebookName } = req.body;
if (!appName || !notebookName) { if (!appName || !notebookName) {
return res.status(400).json({ error: 'appName and notebookName required' }); 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 { try {
const fly = createFlyClient(); const fly = createFlyClient();
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: region
}); });
await fly.post(`/apps/${appName}/secrets`, { console.log('Uploading notebook to S3');
secrets: { const data = fs.readFileSync(notebookPath);
INSTANCE_PREFIX: appName, const key = `${appName}/notebooks/${Date.now()}-notebook.ipynb`;
BUCKET_NAME: COMMON_BUCKET, console.log('S3 key:', key);
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`;
await s3.putObject({ await s3.putObject({
Bucket: COMMON_BUCKET, Bucket: COMMON_BUCKET,
Key: notebookKey, Key: key,
Body: notebookData, Body: data,
ContentType: 'application/json' ContentType: 'application/json'
}).promise(); }).promise();
console.log('Creating machine');
const machineConfig = { const machineConfig = {
name: `${appName}-machine`, name: `${appName}-machine`,
region,
count: 1,
vm_size: 'shared-cpu-1x',
autostart: true,
config: { config: {
image: IMAGE_REF, image: IMAGE_REF,
env: { env: {
INSTANCE_PREFIX: appName, INSTANCE_PREFIX: appName,
NOTEBOOK_KEY: notebookKey, NOTEBOOK_KEY: key,
BUCKET_NAME: COMMON_BUCKET, BUCKET_NAME: COMMON_BUCKET,
AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY,
@ -92,69 +107,22 @@ app.post('/deploy', async (req, res) => {
AWS_REGION AWS_REGION
}, },
services: [{ services: [{
ports: [{ port: 3006, handlers: ["http"] }], internal_port: 3006,
protocol: "tcp", protocol: 'tcp',
internal_port: 3006 ports: [{ port: 80, handlers: ['http'] }]
}] }]
} }
}; };
console.log('Machine config:', machineConfig);
await fly.post(`/apps/${appName}/machines`, machineConfig); await fly.post(`/apps/${appName}/machines`, machineConfig);
res.json({ console.log('Deployment successful:', appName);
status: 'created', return res.json({ status: 'created', url: `https://${appName}.fly.dev` });
app: appName, } catch (err) {
url: `https://${appName}.fly.dev` console.error('Deployment error:', err.stack || err);
}); return res.status(500).json({ error: err.response?.data || err.message });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
} }
}); });
app.post('/upload', async (req, res) => { const PORT = process.env.PORT || 3006;
const { appName, notebookName } = req.body; app.listen(PORT, '0.0.0.0', () => console.log(`Listening on port ${PORT}`));
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}`));

6
package-lock.json generated Normal file
View file

@ -0,0 +1,6 @@
{
"name": "bytecamp-services",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}