modified index.js and snakeapi
This commit is contained in:
parent
5419e3f479
commit
e64478b9bc
5 changed files with 86127 additions and 105 deletions
|
|
@ -1,15 +1,28 @@
|
|||
FROM python:3.11-slim
|
||||
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 notebooks ./notebooks
|
||||
COPY snakeapi_server.py .
|
||||
COPY notebooks ./notebooks
|
||||
|
||||
RUN chmod +x entrypoint.sh
|
||||
|
||||
ENV PORT=3006
|
||||
EXPOSE 3006
|
||||
EXPOSE ${PORT} ${NOTEBOOK_PORT}
|
||||
|
||||
CMD ["./entrypoint.sh"]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,29 @@
|
|||
#!/usr/bin/env bash
|
||||
set -eux
|
||||
|
||||
NOTEBOOK_DIR="notebooks"
|
||||
mkdir -p "${NOTEBOOK_DIR}"
|
||||
|
||||
# fetch latest notebook via prefix sync
|
||||
# sync notebooks from S3
|
||||
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
|
||||
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1)
|
||||
if [ -n "$latest_ipynb" ]; then
|
||||
mv "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb"
|
||||
jupyter nbconvert --to script "${NOTEBOOK_DIR}/notebook.ipynb" --output "${NOTEBOOK_DIR}/notebook.py"
|
||||
# pick latest notebook
|
||||
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -n1)
|
||||
cp "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb"
|
||||
|
||||
# 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"
|
||||
fi
|
||||
|
||||
exec python snakeapi_server.py
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,64 +1,55 @@
|
|||
import os
|
||||
import logging
|
||||
import typing
|
||||
import importlib.util
|
||||
from flask import Flask, request, jsonify
|
||||
from flask import Flask, request, jsonify, Response
|
||||
from flask_cors import CORS
|
||||
|
||||
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}")
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
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)
|
||||
spec.loader.exec_module(notebook_module)
|
||||
|
||||
handlers = {
|
||||
"info": notebook_module.info,
|
||||
"start": notebook_module.start,
|
||||
"move": notebook_module.move,
|
||||
"end": notebook_module.end,
|
||||
}
|
||||
|
||||
def run_server(handlers: typing.Dict):
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
@app.get("/")
|
||||
def on_info():
|
||||
return handlers["info"]()
|
||||
@app.route("/health", methods=["GET"])
|
||||
def health():
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
@app.post("/start")
|
||||
def on_start():
|
||||
game_state = request.get_json()
|
||||
handlers["start"](game_state)
|
||||
return "ok"
|
||||
@app.route("/", methods=["GET"])
|
||||
def info():
|
||||
return jsonify(notebook_module.info())
|
||||
|
||||
@app.post("/move")
|
||||
def on_move():
|
||||
game_state = request.get_json()
|
||||
return handlers["move"](game_state)
|
||||
@app.route("/start", methods=["POST"])
|
||||
def start():
|
||||
data = request.get_json()
|
||||
return jsonify(notebook_module.start(data))
|
||||
|
||||
@app.post("/end")
|
||||
def on_end():
|
||||
end_game = request.get_json()
|
||||
handlers["end"](end_game)
|
||||
return "ok"
|
||||
@app.route("/move", methods=["POST"])
|
||||
def move():
|
||||
data = request.get_json()
|
||||
return jsonify(notebook_module.move(data))
|
||||
|
||||
@app.get("/notebook")
|
||||
@app.route("/end", methods=["POST"])
|
||||
def end():
|
||||
data = request.get_json()
|
||||
return jsonify(notebook_module.end(data))
|
||||
|
||||
@app.route("/notebook", methods=["GET"])
|
||||
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 = "0.0.0.0"
|
||||
port = int(os.environ.get("PORT", "3006"))
|
||||
logging.getLogger("werkzeug").setLevel(logging.ERROR)
|
||||
|
||||
app.run(host=host, port=port)
|
||||
if os.path.isfile(notebook_ipynb_path):
|
||||
with open(notebook_ipynb_path, "r", encoding="utf-8") as f:
|
||||
return Response(f.read(), mimetype="application/json")
|
||||
return jsonify({"error": "notebook not found"}), 404
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_server(handlers)
|
||||
port = int(os.environ.get("PORT", 8000))
|
||||
app.run(host="0.0.0.0", port=port)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
// src/index.js
|
||||
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
|
@ -17,7 +15,7 @@ const {
|
|||
IMAGE_REF
|
||||
} = process.env;
|
||||
|
||||
// --- ENV debug ---
|
||||
// Log environment variables for debugging
|
||||
console.log('--- ENV START ---');
|
||||
console.log('FLY_ORG: ', FLY_ORG);
|
||||
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('--- ENV END ---');
|
||||
|
||||
// S3 client
|
||||
// Initialize S3 client
|
||||
const s3 = new AWS.S3({
|
||||
endpoint: AWS_ENDPOINT_URL_S3,
|
||||
region: AWS_REGION,
|
||||
|
|
@ -37,7 +35,7 @@ const s3 = new AWS.S3({
|
|||
s3ForcePathStyle: true
|
||||
});
|
||||
|
||||
// Fly Machines API client
|
||||
// Create Fly Machines API client
|
||||
function createFlyClient() {
|
||||
return axios.create({
|
||||
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({
|
||||
baseURL: 'https://api.fly.io/graphql',
|
||||
headers: {
|
||||
|
|
@ -57,7 +55,7 @@ const gqlClient = axios.create({
|
|||
}
|
||||
});
|
||||
|
||||
// GraphQL ミューテーション定義
|
||||
// Define GraphQL mutation for IP allocation
|
||||
const ALLOCATE_IP_MUTATION = `
|
||||
mutation AllocateIp($input: AllocateIPAddressInput!) {
|
||||
allocateIpAddress(input: $input) {
|
||||
|
|
@ -79,10 +77,8 @@ app.post('/deploy', async (req, res) => {
|
|||
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}` });
|
||||
|
|
@ -112,7 +108,7 @@ app.post('/deploy', async (req, res) => {
|
|||
console.log('Creating machine');
|
||||
const machineConfig = {
|
||||
name: `${appName}-machine`,
|
||||
region,
|
||||
region: region,
|
||||
count: 1,
|
||||
vm_size: 'shared-cpu-1x',
|
||||
autostart: true,
|
||||
|
|
@ -121,53 +117,51 @@ app.post('/deploy', async (req, res) => {
|
|||
env: {
|
||||
INSTANCE_PREFIX: appName,
|
||||
NOTEBOOK_KEY: key,
|
||||
BUCKET_NAME: COMMON_BUCKET,
|
||||
COMMON_BUCKET: COMMON_BUCKET,
|
||||
AWS_ACCESS_KEY_ID,
|
||||
AWS_SECRET_ACCESS_KEY,
|
||||
AWS_ENDPOINT_URL_S3,
|
||||
AWS_REGION
|
||||
},
|
||||
services: [{
|
||||
internal_port: 3006,
|
||||
http_service: {
|
||||
internal_port: 8000,
|
||||
force_https: true,
|
||||
auto_stop_machines: 'stop',
|
||||
auto_start_machines: true,
|
||||
min_machines_running: 0,
|
||||
processes: ['app']
|
||||
},
|
||||
services: [
|
||||
{
|
||||
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);
|
||||
|
||||
// ── ここから IP 割り当て ──
|
||||
|
||||
// GraphQL で IPv4 を割り当て
|
||||
console.log('Allocating IPv4 via GraphQL API');
|
||||
const v4resp = await gqlClient.post('', {
|
||||
query: ALLOCATE_IP_MUTATION,
|
||||
variables: {
|
||||
input: {
|
||||
appId: appName,
|
||||
type: 'v4'
|
||||
}
|
||||
}
|
||||
variables: { input: { appId: appName, type: 'v4' } }
|
||||
});
|
||||
const ipv4 = v4resp.data.data.allocateIpAddress.ipAddress.address;
|
||||
console.log('Allocated IPv4:', ipv4);
|
||||
|
||||
// GraphQL で IPv6 を割り当て
|
||||
console.log('Allocating IPv6 via GraphQL API');
|
||||
const v6resp = await gqlClient.post('', {
|
||||
query: ALLOCATE_IP_MUTATION,
|
||||
variables: {
|
||||
input: {
|
||||
appId: appName,
|
||||
type: 'v6'
|
||||
}
|
||||
}
|
||||
variables: { input: { appId: appName, type: 'v6' } }
|
||||
});
|
||||
const ipv6 = v6resp.data.data.allocateIpAddress.ipAddress.address;
|
||||
console.log('Allocated IPv6:', ipv6);
|
||||
|
||||
console.log('Deployment successful:', appName);
|
||||
return res.json({
|
||||
status: 'created',
|
||||
app: appName,
|
||||
|
|
@ -182,5 +176,51 @@ app.post('/deploy', async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3006;
|
||||
app.listen(PORT, '0.0.0.0', () => console.log(`Listening on port ${PORT}`));
|
||||
// Upload notebook to S3 for an existing app
|
||||
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}`);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue