modified index.js and snakeapi

This commit is contained in:
yoshi 2025-05-02 00:12:05 -07:00
parent 5419e3f479
commit e64478b9bc
5 changed files with 86127 additions and 105 deletions

View file

@ -1,15 +1,28 @@
FROM python:3.11-slim FROM python:3.11-slim
WORKDIR /app WORKDIR /app
RUN pip install --no-cache-dir jupyter flask awscli flask_cors nbconvert nbformat # disable Python output buffering
ENV PYTHONUNBUFFERED=1
# wrapper server port
ENV PORT=8000
# notebook server port
ENV NOTEBOOK_PORT=3006
RUN pip install --no-cache-dir \
flask \
flask-cors \
awscli \
jupyter \
nbconvert \
nbformat \
requests
COPY entrypoint.sh . COPY entrypoint.sh .
COPY notebooks ./notebooks
COPY snakeapi_server.py . COPY snakeapi_server.py .
COPY notebooks ./notebooks
RUN chmod +x entrypoint.sh RUN chmod +x entrypoint.sh
ENV PORT=3006 EXPOSE ${PORT} ${NOTEBOOK_PORT}
EXPOSE 3006
CMD ["./entrypoint.sh"] CMD ["./entrypoint.sh"]

View file

@ -1,17 +1,29 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eux
NOTEBOOK_DIR="notebooks" NOTEBOOK_DIR="notebooks"
mkdir -p "${NOTEBOOK_DIR}" mkdir -p "${NOTEBOOK_DIR}"
# fetch latest notebook via prefix sync # sync notebooks from S3
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://$COMMON_BUCKET/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/"
# pick latest .ipynb and normalize name # pick latest notebook
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1) latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -n1)
if [ -n "$latest_ipynb" ]; then cp "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb"
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
# convert notebook to script
jupyter nbconvert --to script "${NOTEBOOK_DIR}/notebook.ipynb" \
--output notebook --output-dir "${NOTEBOOK_DIR}"
# remove IPython magics only
sed -i '/get_ipython()/d' "${NOTEBOOK_DIR}/notebook.py"
sed -i '/^!/d' "${NOTEBOOK_DIR}/notebook.py"
sed -i '/^%/d' "${NOTEBOOK_DIR}/notebook.py"
# remove standalone Flask run calls
sed -i '/app\.run(/d' "${NOTEBOOK_DIR}/notebook.py"
# export path and launch server
export NOTEBOOK_PATH="$(pwd)/${NOTEBOOK_DIR}/notebook.py"
exec python snakeapi_server.py exec python snakeapi_server.py

File diff suppressed because it is too large Load diff

View file

@ -1,64 +1,55 @@
import os import os
import logging import logging
import typing
import importlib.util import importlib.util
from flask import Flask, request, jsonify from flask import Flask, request, jsonify, Response
from flask_cors import CORS from flask_cors import CORS
notebook_path = os.environ.get("NOTEBOOK_PATH") logging.basicConfig(level=logging.INFO)
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_PATH = os.environ.get("NOTEBOOK_PATH")
if not NOTEBOOK_PATH or not os.path.isfile(NOTEBOOK_PATH):
raise RuntimeError(f"NOTEBOOK_PATH not set or file not found: {NOTEBOOK_PATH}")
# Determine .ipynb path
notebook_ipynb_path = NOTEBOOK_PATH[:-3] + ".ipynb"
spec = importlib.util.spec_from_file_location("notebook_module", NOTEBOOK_PATH)
notebook_module = importlib.util.module_from_spec(spec) notebook_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(notebook_module) spec.loader.exec_module(notebook_module)
handlers = { app = Flask(__name__)
"info": notebook_module.info, CORS(app)
"start": notebook_module.start,
"move": notebook_module.move,
"end": notebook_module.end,
}
def run_server(handlers: typing.Dict): @app.route("/health", methods=["GET"])
app = Flask(__name__) def health():
CORS(app) return jsonify({"status": "ok"}), 200
@app.get("/") @app.route("/", methods=["GET"])
def on_info(): def info():
return handlers["info"]() return jsonify(notebook_module.info())
@app.post("/start") @app.route("/start", methods=["POST"])
def on_start(): def start():
game_state = request.get_json() data = request.get_json()
handlers["start"](game_state) return jsonify(notebook_module.start(data))
return "ok"
@app.post("/move") @app.route("/move", methods=["POST"])
def on_move(): def move():
game_state = request.get_json() data = request.get_json()
return handlers["move"](game_state) return jsonify(notebook_module.move(data))
@app.post("/end") @app.route("/end", methods=["POST"])
def on_end(): def end():
end_game = request.get_json() data = request.get_json()
handlers["end"](end_game) return jsonify(notebook_module.end(data))
return "ok"
@app.get("/notebook") @app.route("/notebook", methods=["GET"])
def get_notebook(): def get_notebook():
with open(notebook_path.replace('.py', '.ipynb'), "r", encoding="utf-8") as f: if os.path.isfile(notebook_ipynb_path):
return f.read(), 200, {"Content-Type": "application/json"} with open(notebook_ipynb_path, "r", encoding="utf-8") as f:
return Response(f.read(), mimetype="application/json")
@app.get("/notebook/path") return jsonify({"error": "notebook not found"}), 404
def get_notebook_path():
return jsonify({"path": notebook_path})
host = "0.0.0.0"
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__":
run_server(handlers) port = int(os.environ.get("PORT", 8000))
app.run(host="0.0.0.0", port=port)

View file

@ -1,5 +1,3 @@
// 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');
@ -17,7 +15,7 @@ const {
IMAGE_REF IMAGE_REF
} = process.env; } = process.env;
// --- ENV debug --- // 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);
@ -29,7 +27,7 @@ console.log('FLY_ACCESS_TOKEN: ', FLY_ACCESS_TOKEN ? '(found)' : '(NOT SET)'
console.log('IMAGE_REF: ', IMAGE_REF); console.log('IMAGE_REF: ', IMAGE_REF);
console.log('--- ENV END ---'); console.log('--- ENV END ---');
// 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,
@ -37,7 +35,7 @@ const s3 = new AWS.S3({
s3ForcePathStyle: true s3ForcePathStyle: true
}); });
// 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',
@ -48,7 +46,7 @@ function createFlyClient() {
}); });
} }
// 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: {
@ -57,7 +55,7 @@ const gqlClient = axios.create({
} }
}); });
// GraphQL ミューテーション定義 // Define GraphQL mutation for IP allocation
const ALLOCATE_IP_MUTATION = ` const ALLOCATE_IP_MUTATION = `
mutation AllocateIp($input: AllocateIPAddressInput!) { mutation AllocateIp($input: AllocateIPAddressInput!) {
allocateIpAddress(input: $input) { allocateIpAddress(input: $input) {
@ -79,10 +77,8 @@ app.post('/deploy', async (req, res) => {
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); const notebookPath = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
console.log('Resolved notebookPath:', notebookPath); console.log('Resolved notebookPath:', notebookPath);
console.log('File exists:', fs.existsSync(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}` });
@ -112,7 +108,7 @@ app.post('/deploy', async (req, res) => {
console.log('Creating machine'); console.log('Creating machine');
const machineConfig = { const machineConfig = {
name: `${appName}-machine`, name: `${appName}-machine`,
region, region: region,
count: 1, count: 1,
vm_size: 'shared-cpu-1x', vm_size: 'shared-cpu-1x',
autostart: true, autostart: true,
@ -121,53 +117,51 @@ app.post('/deploy', async (req, res) => {
env: { env: {
INSTANCE_PREFIX: appName, INSTANCE_PREFIX: appName,
NOTEBOOK_KEY: key, NOTEBOOK_KEY: key,
BUCKET_NAME: 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
}, },
services: [{ http_service: {
internal_port: 3006, internal_port: 8000,
force_https: true,
auto_stop_machines: 'stop',
auto_start_machines: true,
min_machines_running: 0,
processes: ['app']
},
services: [
{
protocol: 'tcp', protocol: 'tcp',
ports: [{ port: 80, handlers: ['http'] }] internal_port: 8000,
}] ports: [
{ port: 443, handlers: ['tls', 'http'] },
{ port: 80, handlers: ['http'] }
]
}
]
} }
}; };
console.log('Machine config:', machineConfig); console.log('Machine config:', JSON.stringify(machineConfig, null, 2));
await fly.post(`/apps/${appName}/machines`, machineConfig); await fly.post(`/apps/${appName}/machines`, machineConfig);
// ── ここから IP 割り当て ──
// GraphQL で IPv4 を割り当て
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: { variables: { input: { appId: appName, type: 'v4' } }
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);
// GraphQL で IPv6 を割り当て
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: { variables: { input: { appId: appName, type: 'v6' } }
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);
console.log('Deployment successful:', appName);
return res.json({ return res.json({
status: 'created', status: 'created',
app: appName, app: appName,
@ -182,5 +176,51 @@ app.post('/deploy', async (req, res) => {
} }
}); });
const PORT = process.env.PORT || 3006; // Upload notebook to S3 for an existing app
app.listen(PORT, '0.0.0.0', () => console.log(`Listening on port ${PORT}`)); app.post('/:appName/upload', async (req, res) => {
const { appName } = req.params;
const { notebookName, fileContentBase64 } = req.body;
if (!notebookName || !fileContentBase64) {
return res.status(400).json({ error: 'notebookName and fileContentBase64 are required' });
}
try {
const buffer = Buffer.from(fileContentBase64, 'base64');
const key = `${appName}/notebooks/${notebookName}`;
console.log(`Uploading notebook to: s3://${COMMON_BUCKET}/${key}`);
await s3.putObject({
Bucket: COMMON_BUCKET,
Key: key,
Body: buffer,
ContentType: 'application/json'
}).promise();
return res.json({ status: 'uploaded', app: appName, key });
} catch (err) {
console.error('Notebook upload error:', err);
return res.status(500).json({ error: err.message });
}
});
// Delete a Fly app
app.post('/:appName/delete', async (req, res) => {
const { appName } = req.params;
try {
const fly = createFlyClient();
console.log('Destroying Fly app:', appName);
await fly.delete(`/apps/${appName}`);
return res.json({ status: 'deleted', app: appName });
} catch (err) {
console.error('App deletion error:', err.response?.data || err.message);
return res.status(500).json({ error: err.response?.data || err.message });
}
});
const LISTEN_PORT = process.env.PORT || 3006;
app.listen(LISTEN_PORT, '0.0.0.0', () => {
console.log(`Deployment service listening on port ${LISTEN_PORT}`);
});