91 lines
3.2 KiB
JavaScript
91 lines
3.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.dockerService = void 0;
|
|
const child_process_1 = require("child_process");
|
|
const util_1 = require("util");
|
|
const http_1 = require("http");
|
|
const logger_1 = require("../utils/logger");
|
|
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
const DOCKER_SOCKET = '/var/run/docker.sock';
|
|
/**
|
|
* Make a request to the Docker Engine API over Unix socket.
|
|
*/
|
|
function dockerRequest(method, path, body) {
|
|
return new Promise((resolve, reject) => {
|
|
const options = {
|
|
socketPath: DOCKER_SOCKET,
|
|
path,
|
|
method,
|
|
headers: body
|
|
? { 'Content-Type': 'application/json' }
|
|
: undefined,
|
|
};
|
|
const req = (0, http_1.request)(options, (res) => {
|
|
const chunks = [];
|
|
res.on('data', (chunk) => chunks.push(chunk));
|
|
res.on('end', () => {
|
|
resolve({
|
|
statusCode: res.statusCode || 0,
|
|
body: Buffer.concat(chunks).toString(),
|
|
});
|
|
});
|
|
});
|
|
req.on('error', reject);
|
|
if (body) {
|
|
req.write(JSON.stringify(body));
|
|
}
|
|
req.end();
|
|
});
|
|
}
|
|
/**
|
|
* Check if a container is running via Docker API.
|
|
*/
|
|
async function getContainerStatus(containerName) {
|
|
try {
|
|
const response = await dockerRequest('GET', `/containers/${containerName}/json`);
|
|
if (response.statusCode === 404) {
|
|
return { running: false, status: 'not_found', error: 'Container not found' };
|
|
}
|
|
if (response.statusCode !== 200) {
|
|
return { running: false, status: 'error', error: response.body };
|
|
}
|
|
const data = JSON.parse(response.body);
|
|
return {
|
|
running: data.State?.Running === true,
|
|
status: data.State?.Status || 'unknown',
|
|
exitCode: data.State?.ExitCode,
|
|
};
|
|
}
|
|
catch (err) {
|
|
logger_1.logger.error(`Failed to check container status: ${containerName}`, err);
|
|
return { running: false, status: 'error', error: err.message };
|
|
}
|
|
}
|
|
/**
|
|
* Restart/recreate a container using docker compose.
|
|
* This ensures environment variables are picked up.
|
|
*/
|
|
async function restartContainer(containerName) {
|
|
try {
|
|
// Use docker compose to recreate the container with updated env vars
|
|
// -d = detached mode, service name is 'newt' in docker-compose.yml
|
|
const { stdout, stderr } = await execAsync('docker compose up -d newt', {
|
|
cwd: '/app', // Assuming docker-compose.yml is in /app
|
|
timeout: 30000, // 30 second timeout
|
|
});
|
|
const output = stdout + stderr;
|
|
logger_1.logger.info(`Container restart output: ${output}`);
|
|
return { success: true, output };
|
|
}
|
|
catch (err) {
|
|
const error = err;
|
|
const output = (error.stdout || '') + (error.stderr || '') + error.message;
|
|
logger_1.logger.error(`Failed to restart container: ${containerName}`, err);
|
|
return { success: false, output };
|
|
}
|
|
}
|
|
exports.dockerService = {
|
|
getContainerStatus,
|
|
restartContainer,
|
|
};
|
|
//# sourceMappingURL=docker.service.js.map
|