diff --git a/.gitignore b/.gitignore index 03a4a6e7bf646c45e891dcd096e26b806848f059..fd5f989717b28ceac3ad2b651f5952c2bc818986 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -config/default.json \ No newline at end of file +config/default.json +config/id_rsa.pub \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 71a5485479613fdf85c29ab91f595aacf7f462af..65b4f1bc9be2aee6f8c416013d5ba43273d06bf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] - +- setup ssh directory script +- add id_rsa.pub script ## [1.0.0] - 2021-03-24 ### Added diff --git a/lib/copy-ssh-key.js b/lib/copy-ssh-key.js new file mode 100644 index 0000000000000000000000000000000000000000..88394d4760f4de3e680d0cfb61594a8761b374ae --- /dev/null +++ b/lib/copy-ssh-key.js @@ -0,0 +1,59 @@ +const { + to, + handleStderr, + handleStdout, + getClientSSH +} = require('./helper'); + +const path = require('path'); +const logger = require('./logger'); + + +const sshCopyId = async (host, username, password) => { + logger.info(`START SIMPLE SSH COPY ID ${host}\n`); + + const [err, client] = await getClientSSH(host, username, password); + + if(err){ + logger.info("Connection failed", err); + return + } + + const basename = "id_rsa.pub" + const filepath = path.join(__dirname, '..', 'config', basename) + const remoteDir = `/home/${username}/.ssh/jdc/${basename}` + + logger.info(`copy pub key from ${filepath} to ${remoteDir}`) + + const [errFile, _] = await to(client.putFile(filepath, remoteDir)) + + if(errFile){ + logger.error(`Error when copy ${basename}`) + logger.error(errFile) + return; + } + + + const cmds = [ + `cat ${remoteDir}`, + `cat ${remoteDir} > /home/${username}/.ssh/authorized_keys`, + `cat /home/${username}/.ssh/authorized_keys`, + `rm ${remoteDir}` + ] + + const options = {onStdout: handleStdout(host), onStderr: handleStderr(host)}; + for (let i = 0; i < cmds.length; i++) { + const cmd = cmds[i]; + logger.info(`${host} => run cmd: ${cmd}`) + const [errCmd, res] = await to(client.exec(cmd, [], options)); + + if(errCmd){ + logger.error(`${host} something wrong's happend :(}`, errCmd); + process.exit(1); + } + } + + logger.info(`\nSUCCESS SIMPLE SSH COPY ID ${host}`); +} + +module.exports = sshCopyId; \ No newline at end of file diff --git a/lib/healthcheck.js b/lib/healthcheck.js index cac7282f0546b3b72dc48d42900171f37f7dd320..51b65bdcefaae5151f03725d189c3784a6b0f70e 100644 --- a/lib/healthcheck.js +++ b/lib/healthcheck.js @@ -10,19 +10,22 @@ const logger = require('./logger'); const healthcheck = async (host, username, password) => { - - logger.info(`START HEALTHCHECK SERVER ${host}\n`); - + console.log(); + logger.info(`${host} => START HEALTHCHECK SERVER`); const [err, client] = await getClientSSH(host, username, password); if(err){ - logger.info("Connection failed", err); + console.log(); + logger.error("******************************************************"); + logger.error("Connection failed", err); + logger.error("******************************************************"); + console.log(); return } const cmds = [ "echo Access ok !", - "docker ps" + //"docker ps" ] const options = {onStdout: handleStdout(host), onStderr: handleStderr(host)}; @@ -32,12 +35,17 @@ const healthcheck = async (host, username, password) => { const [errCmd, res] = await to(client.exec(cmd, [], options)); if(errCmd){ - logger.error(`${host} something wrong's happend :(}`, errCmd); + console.log(); + logger.error("******************************************************"); + logger.error(`${host} something wrong happened !}`, errCmd); + logger.error("******************************************************"); + console.log(); process.exit(1); } } - logger.info(`\nSUCCESS HEALTHCHECK SERVER ${host}`); + logger.info(`${host} => SUCCESS HEALTHCHECK SERVER`); + console.log(); } module.exports = healthcheck; \ No newline at end of file diff --git a/lib/setup-ssh-directory.js b/lib/setup-ssh-directory.js new file mode 100644 index 0000000000000000000000000000000000000000..cabd296dad4d4f6dda77c79bb9a6ce1c5d61b29f --- /dev/null +++ b/lib/setup-ssh-directory.js @@ -0,0 +1,43 @@ +const { + to, + handleStderr, + handleStdout, + getClientSSH +} = require('./helper'); + + +const logger = require('./logger'); + + +const sshDirectory = async (host, username, password) => { + logger.info(`START SETUP SSH DIRECTORY ${host}\n`); + + const [err, client] = await getClientSSH(host, username, password); + + if(err){ + logger.info("Connection failed", err); + return + } + + const cmds = [ + "mkdir -p ~/.ssh", + "mkdir -p ~/.ssh/jdc", + "cd ~/.ssh && touch authorized_keys", + ] + + const options = {onStdout: handleStdout(host), onStderr: handleStderr(host)}; + for (let i = 0; i < cmds.length; i++) { + const cmd = cmds[i]; + logger.info(`${host} => run cmd: ${cmd}`) + const [errCmd, res] = await to(client.exec(cmd, [], options)); + + if(errCmd){ + logger.error(`${host} something wrong's happend :(}`, errCmd); + process.exit(1); + } + } + + logger.info(`\nSUCCESS SETUP SSH DIRECTORY ${host}`); +} + +module.exports = sshDirectory; \ No newline at end of file diff --git a/main.js b/main.js index 0b279fe470415451e7566c0409eeff6ac5c71678..a17435d11cbe1e65182e863fa13e41ab3b31840c 100644 --- a/main.js +++ b/main.js @@ -1,12 +1,6 @@ const { - to, generateFilePayload, generateLogin, - runCMD, - handleStderr, - handleStdout, - getClientSSH, - registerFactory } = require('./lib/helper'); const config = require("config") @@ -23,6 +17,8 @@ const setupServer = require('./lib/setup-server'); const setupSonarqube = require('./lib/setup-sonarqube'); const passwordUpdate = require('./lib/password-update'); const healthcheck = require('./lib/healthcheck'); +const setupSSHDirectory = require('./lib/setup-ssh-directory'); +const copySSHId = require('./lib/copy-ssh-key'); const filesToUpload = [ generateFilePayload("install-docker.sh", DIR_BASH, DIR_REMOTE_BASH), @@ -41,25 +37,35 @@ const logins = loginConfig.map(login => generateLogin(login.host, login.username const main = async options => { - for (let i = 0; i < logins.length; i++) { - const login = logins[i]; - await setupServer(login.host, login.username, login.password, filesToUpload); - } + // for (let i = 0; i < logins.length; i++) { + // const login = logins[i]; + // await setupSSHDirectory(login.host, login.username, login.password); + // } for (let i = 0; i < logins.length; i++) { const login = logins[i]; - await passwordUpdate(login.host, login.username, login.password, login.newPassword); + await copySSHId(login.host, login.username, login.password); } - for (let i = 0; i < logins.length; i++) { - const login = logins[i]; - await healthcheck(login.host, login.username, login.password); - } + // for (let i = 0; i < logins.length; i++) { + // const login = logins[i]; + // await setupServer(login.host, login.username, login.password, filesToUpload); + // } - for (let i = 0; i < logins.length; i++) { - const login = logins[i]; - await setupSonarqube(login.host, login.username, login.password); - } + // for (let i = 0; i < logins.length; i++) { + // const login = logins[i]; + // await passwordUpdate(login.host, login.username, login.password, login.newPassword); + // } + + // for (let i = 0; i < logins.length; i++) { + // const login = logins[i]; + // await healthcheck(login.host, login.username, login.password); + // } + + // for (let i = 0; i < logins.length; i++) { + // const login = logins[i]; + // await setupSonarqube(login.host, login.username, login.password); + // } }