From cc686279c97767aa4809ca6babffc6244ffe7b46 Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Tue, 30 Mar 2021 12:11:39 +0200 Subject: [PATCH 1/6] copy-ssh-key --- lib/copy-ssh-key.js | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/copy-ssh-key.js diff --git a/lib/copy-ssh-key.js b/lib/copy-ssh-key.js new file mode 100644 index 0000000..88394d4 --- /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 -- GitLab From d68b93e6e100070395f674759e357c0702f4b06c Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Tue, 30 Mar 2021 12:11:47 +0200 Subject: [PATCH 2/6] setup ssh directory --- lib/setup-ssh-directory.js | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/setup-ssh-directory.js diff --git a/lib/setup-ssh-directory.js b/lib/setup-ssh-directory.js new file mode 100644 index 0000000..cabd296 --- /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 -- GitLab From ee77dadf838b3ef65ae019a28aed0a7a955e9955 Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Tue, 30 Mar 2021 12:12:11 +0200 Subject: [PATCH 3/6] update ignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 03a4a6e..fd5f989 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 -- GitLab From 841a9d4cd211f6e880128def6fbd8ccd96d2d73b Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Tue, 30 Mar 2021 12:12:16 +0200 Subject: [PATCH 4/6] test main --- main.js | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/main.js b/main.js index 0b279fe..a17435d 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); + // } } -- GitLab From 05ca78c5e8d158d02771d5f544a0f5488bf868e4 Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Tue, 30 Mar 2021 12:19:46 +0200 Subject: [PATCH 5/6] update changelog.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71a5485..65b4f1b 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 -- GitLab From 2c55c29a7807d268ae8ebd75af01dbe015859d06 Mon Sep 17 00:00:00 2001 From: Johnny Da Costa Date: Wed, 23 Feb 2022 08:39:24 +0100 Subject: [PATCH 6/6] ui fix --- lib/healthcheck.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/healthcheck.js b/lib/healthcheck.js index cac7282..51b65bd 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 -- GitLab