Files
DLE/webssh-agent/utils/userUtils.js
2025-11-11 20:11:09 +03:00

48 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { execSshCommand } = require('./sshUtils');
const log = require('./logger');
/**
* Создание пользователя с SSH ключами
*/
const createUserWithSshKeys = async (username, publicKey, options) => {
log.info(`Создание пользователя ${username}...`);
// Создание пользователя
await execSshCommand(`useradd -m -s /bin/bash ${username} || true`, options);
await execSshCommand(`usermod -aG sudo ${username}`, options);
// Настройка SSH ключей для пользователя
await execSshCommand(`mkdir -p /home/${username}/.ssh`, options);
await execSshCommand(`echo "${publicKey}" > /home/${username}/.ssh/authorized_keys`, options);
await execSshCommand(`chown -R ${username}:${username} /home/${username}/.ssh`, options);
await execSshCommand(`chmod 700 /home/${username}/.ssh`, options);
await execSshCommand(`chmod 600 /home/${username}/.ssh/authorized_keys`, options);
log.success(`Пользователь ${username} создан с SSH ключами`);
};
/**
* Создание всех необходимых пользователей
*/
const createAllUsers = async (ubuntuUser, dockerUser, publicKey, options) => {
// Создание пользователя Ubuntu
await createUserWithSshKeys(ubuntuUser, publicKey, options);
// Создание пользователя Docker
await createUserWithSshKeys(dockerUser, publicKey, options);
// Добавление пользователя Docker в группу docker
await execSshCommand(`usermod -aG docker ${dockerUser}`, options);
// Создание директории для приложения
await execSshCommand(`mkdir -p /home/${dockerUser}/dapp`, options);
await execSshCommand(`chown ${dockerUser}:${dockerUser} /home/${dockerUser}/dapp`, options);
log.success('Все пользователи созданы');
};
module.exports = {
createUserWithSshKeys,
createAllUsers
};