refined snakeapi_service files
This commit is contained in:
parent
61650658eb
commit
0a143b3dc7
4 changed files with 127 additions and 29 deletions
|
|
@ -3,7 +3,6 @@ const fs = require('fs');
|
|||
const path = require('path');
|
||||
const AWS = require('aws-sdk');
|
||||
const axios = require('axios');
|
||||
const tar = require('tar');
|
||||
|
||||
const {
|
||||
FLY_ORG,
|
||||
|
|
@ -12,7 +11,8 @@ const {
|
|||
AWS_SECRET_ACCESS_KEY,
|
||||
AWS_ENDPOINT_URL_S3,
|
||||
AWS_REGION,
|
||||
FLY_ACCESS_TOKEN
|
||||
FLY_ACCESS_TOKEN,
|
||||
IMAGE_REF
|
||||
} = process.env;
|
||||
|
||||
const s3 = new AWS.S3({
|
||||
|
|
@ -62,7 +62,6 @@ app.post('/deploy', async (req, res) => {
|
|||
});
|
||||
|
||||
const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
|
||||
|
||||
if (!fs.existsSync(notebookFile)) {
|
||||
throw new Error(`Notebook file ${notebookName} not found.`);
|
||||
}
|
||||
|
|
@ -70,27 +69,37 @@ app.post('/deploy', async (req, res) => {
|
|||
const notebookData = fs.readFileSync(notebookFile);
|
||||
const timestamp = Date.now();
|
||||
|
||||
const notebookKey = `${appName}/notebooks/${timestamp}-notebook.ipynb`;
|
||||
|
||||
await s3.putObject({
|
||||
Bucket: COMMON_BUCKET,
|
||||
Key: `${appName}/notebooks/${timestamp}-notebook.ipynb`,
|
||||
Key: notebookKey,
|
||||
Body: notebookData,
|
||||
ContentType: 'application/json'
|
||||
}).promise();
|
||||
|
||||
const tarFilePath = `/tmp/${appName}.tar.gz`;
|
||||
await tar.c(
|
||||
{
|
||||
gzip: true,
|
||||
file: tarFilePath,
|
||||
cwd: path.join(__dirname, '../snakeapi_service')
|
||||
},
|
||||
['.']
|
||||
);
|
||||
const machineConfig = {
|
||||
name: `${appName}-machine`,
|
||||
config: {
|
||||
image: IMAGE_REF,
|
||||
env: {
|
||||
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}/deploys`, tarData, {
|
||||
headers: { 'Content-Type': 'application/gzip' }
|
||||
});
|
||||
await fly.post(`/apps/${appName}/machines`, machineConfig);
|
||||
|
||||
res.json({
|
||||
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;
|
||||
app.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue