microservices/deployment-service/src/index.js

129 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-04-30 15:09:22 -07:00
// src/index.js
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-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,
2025-04-29 15:01:23 -07:00
FLY_ACCESS_TOKEN,
IMAGE_REF
2025-04-25 09:21:18 -07:00
} = process.env;
2025-04-30 15:09:22 -07:00
// --- ENV debug ---
console.log('--- ENV START ---');
console.log('FLY_ORG: ', FLY_ORG);
console.log('COMMON_BUCKET: ', COMMON_BUCKET);
console.log('AWS_ACCESS_KEY_ID: ', AWS_ACCESS_KEY_ID ? '(found)' : '(NOT SET)');
console.log('AWS_SECRET_ACCESS_KEY:', AWS_SECRET_ACCESS_KEY ? '(found)' : '(NOT SET)');
console.log('AWS_ENDPOINT_URL_S3: ', AWS_ENDPOINT_URL_S3);
console.log('AWS_REGION: ', AWS_REGION);
console.log('FLY_ACCESS_TOKEN: ', FLY_ACCESS_TOKEN ? '(found)' : '(NOT SET)');
console.log('IMAGE_REF: ', IMAGE_REF);
console.log('--- ENV END ---');
// S3 client
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-30 15:09:22 -07:00
// Fly Machines API client
2025-04-29 11:47:27 -07:00
function createFlyClient() {
2025-04-25 09:21:18 -07:00
return axios.create({
2025-04-29 13:28:25 -07:00
baseURL: 'https://api.machines.dev/v1',
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) => {
2025-04-30 15:09:22 -07:00
console.log('Received /deploy:', req.body);
2025-04-29 11:47:27 -07:00
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-30 15:09:22 -07:00
// 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}` });
}
2025-04-29 11:47:27 -07:00
try {
const fly = createFlyClient();
2025-04-29 12:22:48 -07:00
2025-04-30 15:09:22 -07:00
console.log('Creating Fly app:', appName);
2025-04-29 12:22:48 -07:00
await fly.post('/apps', {
2025-04-29 13:28:25 -07:00
app_name: appName,
2025-04-29 12:22:48 -07:00
org_slug: FLY_ORG,
primary_region: region
});
2025-04-30 15:09:22 -07:00
console.log('Uploading notebook to S3');
const data = fs.readFileSync(notebookPath);
const key = `${appName}/notebooks/${Date.now()}-notebook.ipynb`;
console.log('S3 key:', key);
2025-04-29 11:47:27 -07:00
await s3.putObject({
Bucket: COMMON_BUCKET,
2025-04-30 15:09:22 -07:00
Key: key,
Body: data,
2025-04-29 11:47:27 -07:00
ContentType: 'application/json'
}).promise();
2025-04-25 09:21:18 -07:00
2025-04-30 15:09:22 -07:00
console.log('Creating machine');
2025-04-29 15:01:23 -07:00
const machineConfig = {
name: `${appName}-machine`,
2025-04-30 15:09:22 -07:00
region,
count: 1,
vm_size: 'shared-cpu-1x',
autostart: true,
2025-04-29 15:01:23 -07:00
config: {
image: IMAGE_REF,
env: {
INSTANCE_PREFIX: appName,
2025-04-30 15:09:22 -07:00
NOTEBOOK_KEY: key,
2025-04-29 15:01:23 -07:00
BUCKET_NAME: COMMON_BUCKET,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT_URL_S3,
AWS_REGION
},
services: [{
2025-04-30 15:09:22 -07:00
internal_port: 3006,
protocol: 'tcp',
ports: [{ port: 80, handlers: ['http'] }]
2025-04-29 15:01:23 -07:00
}]
}
};
2025-04-30 15:09:22 -07:00
console.log('Machine config:', machineConfig);
2025-04-29 15:01:23 -07:00
await fly.post(`/apps/${appName}/machines`, machineConfig);
2025-04-29 12:22:48 -07:00
2025-04-30 15:09:22 -07:00
console.log('Deployment successful:', appName);
return res.json({ status: 'created', url: `https://${appName}.fly.dev` });
} catch (err) {
console.error('Deployment error:', err.stack || err);
return res.status(500).json({ error: err.response?.data || err.message });
2025-04-29 15:01:23 -07:00
}
});
2025-04-30 15:09:22 -07:00
const PORT = process.env.PORT || 3006;
app.listen(PORT, '0.0.0.0', () => console.log(`Listening on port ${PORT}`));