refined snakeapi_service files
This commit is contained in:
parent
61650658eb
commit
0a143b3dc7
4 changed files with 127 additions and 29 deletions
|
|
@ -5,6 +5,7 @@ RUN pip install --no-cache-dir jupyter flask awscli flask_cors nbconvert nbforma
|
||||||
|
|
||||||
COPY entrypoint.sh .
|
COPY entrypoint.sh .
|
||||||
COPY notebooks ./notebooks
|
COPY notebooks ./notebooks
|
||||||
|
COPY snakeapi_server.py .
|
||||||
|
|
||||||
RUN chmod +x entrypoint.sh
|
RUN chmod +x entrypoint.sh
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,16 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
NOTEBOOK_DIR="notebooks"
|
NOTEBOOK_DIR="notebooks"
|
||||||
mkdir -p ${NOTEBOOK_DIR}
|
mkdir -p "${NOTEBOOK_DIR}"
|
||||||
|
|
||||||
while true; do
|
# fetch latest notebook
|
||||||
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}/"
|
||||||
|
|
||||||
latest_notebook=$(ls -t ${NOTEBOOK_DIR}/*.ipynb | head -1)
|
# convert to Python script for dynamic import
|
||||||
|
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"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -n "$latest_notebook" ]; then
|
# start the Flask server
|
||||||
jupyter nbconvert --to notebook --execute --inplace --ExecutePreprocessor.timeout=0 "$latest_notebook"
|
python snakeapi_server.py
|
||||||
fi
|
|
||||||
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
|
|
|
||||||
44
deployment-service/snakeapi_service/snakeapi_server.py
Normal file
44
deployment-service/snakeapi_service/snakeapi_server.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
# snakeapi_service/snakeapi_server.py
|
||||||
|
import os
|
||||||
|
import importlib.util
|
||||||
|
from flask import Flask, request
|
||||||
|
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)
|
||||||
|
|
||||||
|
handlers = {
|
||||||
|
"info": nb.info,
|
||||||
|
"start": nb.start,
|
||||||
|
"move": nb.move,
|
||||||
|
"end": nb.end
|
||||||
|
}
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
CORS(app)
|
||||||
|
|
||||||
|
@app.route("/", methods=["GET"])
|
||||||
|
def on_info():
|
||||||
|
return handlers["info"]()
|
||||||
|
|
||||||
|
@app.route("/start", methods=["POST"])
|
||||||
|
def on_start():
|
||||||
|
handlers["start"](request.get_json())
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
@app.route("/move", methods=["POST"])
|
||||||
|
def on_move():
|
||||||
|
return handlers["move"](request.get_json())
|
||||||
|
|
||||||
|
@app.route("/end", methods=["POST"])
|
||||||
|
def on_end():
|
||||||
|
handlers["end"](request.get_json())
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
port = int(os.environ.get("PORT", "3006"))
|
||||||
|
app.run(host="0.0.0.0", port=port)
|
||||||
|
|
@ -3,7 +3,6 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const AWS = require('aws-sdk');
|
const AWS = require('aws-sdk');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const tar = require('tar');
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
FLY_ORG,
|
FLY_ORG,
|
||||||
|
|
@ -12,7 +11,8 @@ const {
|
||||||
AWS_SECRET_ACCESS_KEY,
|
AWS_SECRET_ACCESS_KEY,
|
||||||
AWS_ENDPOINT_URL_S3,
|
AWS_ENDPOINT_URL_S3,
|
||||||
AWS_REGION,
|
AWS_REGION,
|
||||||
FLY_ACCESS_TOKEN
|
FLY_ACCESS_TOKEN,
|
||||||
|
IMAGE_REF
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
const s3 = new AWS.S3({
|
const s3 = new AWS.S3({
|
||||||
|
|
@ -62,7 +62,6 @@ app.post('/deploy', async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
|
const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
|
||||||
|
|
||||||
if (!fs.existsSync(notebookFile)) {
|
if (!fs.existsSync(notebookFile)) {
|
||||||
throw new Error(`Notebook file ${notebookName} not found.`);
|
throw new Error(`Notebook file ${notebookName} not found.`);
|
||||||
}
|
}
|
||||||
|
|
@ -70,27 +69,37 @@ app.post('/deploy', async (req, res) => {
|
||||||
const notebookData = fs.readFileSync(notebookFile);
|
const notebookData = fs.readFileSync(notebookFile);
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
|
|
||||||
|
const notebookKey = `${appName}/notebooks/${timestamp}-notebook.ipynb`;
|
||||||
|
|
||||||
await s3.putObject({
|
await s3.putObject({
|
||||||
Bucket: COMMON_BUCKET,
|
Bucket: COMMON_BUCKET,
|
||||||
Key: `${appName}/notebooks/${timestamp}-notebook.ipynb`,
|
Key: notebookKey,
|
||||||
Body: notebookData,
|
Body: notebookData,
|
||||||
ContentType: 'application/json'
|
ContentType: 'application/json'
|
||||||
}).promise();
|
}).promise();
|
||||||
|
|
||||||
const tarFilePath = `/tmp/${appName}.tar.gz`;
|
const machineConfig = {
|
||||||
await tar.c(
|
name: `${appName}-machine`,
|
||||||
{
|
config: {
|
||||||
gzip: true,
|
image: IMAGE_REF,
|
||||||
file: tarFilePath,
|
env: {
|
||||||
cwd: path.join(__dirname, '../snakeapi_service')
|
INSTANCE_PREFIX: appName,
|
||||||
|
NOTEBOOK_KEY: notebookKey,
|
||||||
|
BUCKET_NAME: COMMON_BUCKET,
|
||||||
|
AWS_ACCESS_KEY_ID,
|
||||||
|
AWS_SECRET_ACCESS_KEY,
|
||||||
|
AWS_ENDPOINT_URL_S3,
|
||||||
|
AWS_REGION
|
||||||
},
|
},
|
||||||
['.']
|
services: [{
|
||||||
);
|
ports: [{ port: 3006, handlers: ["http"] }],
|
||||||
|
protocol: "tcp",
|
||||||
|
internal_port: 3006
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const tarData = fs.readFileSync(tarFilePath);
|
await fly.post(`/apps/${appName}/machines`, machineConfig);
|
||||||
await fly.post(`/apps/${appName}/deploys`, tarData, {
|
|
||||||
headers: { 'Content-Type': 'application/gzip' }
|
|
||||||
});
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
status: 'created',
|
status: 'created',
|
||||||
|
|
@ -102,5 +111,50 @@ app.post('/deploy', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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;
|
const port = process.env.PORT || 3006;
|
||||||
app.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`));
|
app.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue