feat: новая функция

This commit is contained in:
2025-11-11 20:11:09 +03:00
parent 1466de718f
commit 44876c0bc2
9 changed files with 173 additions and 133 deletions

View File

@@ -1,4 +1,5 @@
const { exec } = require('child_process');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const log = require('./logger');
@@ -14,10 +15,10 @@ const sshConfigPath = path.join(sshDir, 'config');
const execLocalCommand = async (command) => {
return new Promise((resolve) => {
exec(command, (error, stdout, stderr) => {
resolve({
code: error ? error.code : 0,
stdout: stdout || '',
stderr: stderr || ''
resolve({
code: error ? error.code : 0,
stdout: stdout || '',
stderr: stderr || ''
});
});
});
@@ -28,35 +29,35 @@ const execLocalCommand = async (command) => {
*/
const createSshKeys = async (email) => {
log.info('Создание SSH ключей на хосте...');
return new Promise((resolve) => {
// Сначала исправляем права доступа к SSH конфигу
exec(`mkdir -p "${sshDir}" && chmod 700 "${sshDir}" && chmod 600 "${sshConfigPath}" 2>/dev/null || true`, (configError) => {
if (configError) {
log.warn('Не удалось исправить права доступа к SSH конфигу: ' + configError.message);
await fs.ensureDir(sshDir);
await execLocalCommand(`chmod 700 "${sshDir}" && chmod 600 "${sshConfigPath}" 2>/dev/null || true`);
const privateExists = await fs.pathExists(privateKeyPath);
const publicExists = await fs.pathExists(publicKeyPath);
if (privateExists && publicExists) {
log.info('SSH ключи уже существуют используем текущую пару');
await execLocalCommand(`chmod 600 "${privateKeyPath}" && chmod 644 "${publicKeyPath}"`);
return;
}
await new Promise((resolve) => {
exec(`ssh-keygen -q -t rsa -b 4096 -C "${email}" -f "${privateKeyPath}" -N ""`, (error) => {
if (error) {
log.error('Ошибка создания SSH ключей: ' + error.message);
return resolve();
}
// Создаем SSH ключи
exec(`ssh-keygen -t rsa -b 4096 -C "${email}" -f "${privateKeyPath}" -N ""`, (error, stdout, stderr) => {
if (error) {
log.error('Ошибка создания SSH ключей: ' + error.message);
log.success('SSH ключи успешно созданы на хосте');
exec(`chmod 600 "${privateKeyPath}" && chmod 644 "${publicKeyPath}"`, (permError) => {
if (permError) {
log.warn('Не удалось установить права доступа к SSH ключам: ' + permError.message);
} else {
log.success('SSH ключи успешно созданы на хосте');
// Устанавливаем правильные права доступа к созданным ключам
exec(`chmod 600 "${privateKeyPath}" && chmod 644 "${publicKeyPath}"`, (permError) => {
if (permError) {
log.warn('Не удалось установить права доступа к SSH ключам: ' + permError.message);
} else {
log.success('Права доступа к SSH ключам установлены');
}
resolve();
});
}
if (error) {
resolve();
log.success('Права доступа к SSH ключам установлены');
}
resolve();
});
});
});