microservices/deployment-service/src/index.js

147 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-04-25 09:21:18 -07:00
const express = require('express');
2025-04-29 11:47:27 -07:00
const fs = require('fs');
const path = require('path');
2025-04-25 09:21:18 -07:00
const AWS = require('aws-sdk');
2025-04-29 11:47:27 -07:00
const axios = require('axios');
2025-04-29 12:22:48 -07:00
const tar = require('tar');
2025-04-25 09:21:18 -07:00
const {
FLY_ORG,
COMMON_BUCKET,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT_URL_S3,
2025-04-29 11:47:27 -07:00
AWS_REGION,
FLY_ACCESS_TOKEN
2025-04-25 09:21:18 -07:00
} = process.env;
2025-04-29 11:47:27 -07:00
const s3 = new AWS.S3({
endpoint: AWS_ENDPOINT_URL_S3,
region: AWS_REGION,
credentials: new AWS.Credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
s3ForcePathStyle: true
});
2025-04-25 09:21:18 -07:00
2025-04-29 11:47:27 -07:00
function createFlyClient() {
2025-04-25 09:21:18 -07:00
return axios.create({
2025-04-29 11:47:27 -07:00
baseURL: 'https://api.fly.io',
2025-04-25 09:21:18 -07:00
headers: {
2025-04-29 11:47:27 -07:00
Authorization: `Bearer ${FLY_ACCESS_TOKEN}`,
2025-04-25 09:21:18 -07:00
'Content-Type': 'application/json'
}
});
}
2025-04-29 11:47:27 -07:00
const app = express();
2025-04-29 12:22:48 -07:00
app.use(express.json({ limit: '10mb' }));
2025-04-25 09:21:18 -07:00
2025-04-29 11:47:27 -07:00
app.post('/deploy', async (req, res) => {
const { appName, region = 'sea', notebookName } = req.body;
2025-04-29 12:22:48 -07:00
if (!appName || !notebookName) {
return res.status(400).json({ error: 'appName and notebookName required' });
}
2025-04-25 09:21:18 -07:00
2025-04-29 11:47:27 -07:00
try {
const fly = createFlyClient();
2025-04-29 12:22:48 -07:00
await fly.post('/apps', {
name: appName,
org_slug: FLY_ORG,
primary_region: region
});
2025-04-29 11:47:27 -07:00
await fly.post(`/apps/${appName}/secrets`, {
secrets: {
INSTANCE_PREFIX: appName,
BUCKET_NAME: COMMON_BUCKET,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT_URL_S3,
AWS_REGION
}
});
2025-04-25 09:21:18 -07:00
2025-04-29 12:22:48 -07:00
const notebookFile = path.join(__dirname, '../snakeapi_service/notebooks', notebookName);
const notebookData = fs.readFileSync(notebookFile);
2025-04-29 11:47:27 -07:00
await s3.putObject({
Bucket: COMMON_BUCKET,
2025-04-29 12:22:48 -07:00
Key: `${appName}/notebook.ipynb`,
Body: notebookData,
2025-04-29 11:47:27 -07:00
ContentType: 'application/json'
}).promise();
2025-04-25 09:21:18 -07:00
2025-04-29 12:22:48 -07:00
const tarFilePath = `/tmp/${appName}.tar.gz`;
await tar.c(
{
gzip: true,
file: tarFilePath,
cwd: path.join(__dirname, '../snakeapi_service')
},
['.']
);
const tarData = fs.readFileSync(tarFilePath);
await fly.post(`/apps/${appName}/deploys`, tarData, {
headers: { 'Content-Type': 'application/gzip' }
});
res.json({
status: 'created',
app: appName,
url: `https://${appName}.fly.dev`
});
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.post('/upload/:appName', async (req, res) => {
const { appName } = req.params;
const notebookContent = req.body;
if (!notebookContent) {
return res.status(400).json({ error: 'Notebook content required.' });
}
try {
const notebookBuffer = Buffer.from(JSON.stringify(notebookContent));
await s3.putObject({
Bucket: COMMON_BUCKET,
Key: `${appName}/notebook.ipynb`,
Body: notebookBuffer,
ContentType: 'application/json'
}).promise();
const fly = createFlyClient();
const tempNotebookPath = path.join(__dirname, '../snakeapi_service/notebooks/notebook.ipynb');
fs.writeFileSync(tempNotebookPath, notebookBuffer);
const tarFilePath = `/tmp/${appName}-redeploy.tar.gz`;
await tar.c(
{
gzip: true,
file: tarFilePath,
cwd: path.join(__dirname, '../snakeapi_service')
},
['.']
);
const tarData = fs.readFileSync(tarFilePath);
await fly.post(`/apps/${appName}/deploys`, tarData, {
headers: { 'Content-Type': 'application/gzip' }
});
res.json({
status: 'updated',
app: appName,
message: 'Notebook updated and application redeployed.'
});
2025-04-29 11:47:27 -07:00
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
2025-04-25 09:21:18 -07:00
2025-04-29 11:47:27 -07:00
const port = process.env.PORT || 3006;
app.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`));