From 7554048339af5982add12ab1b55b19b24cfb6aa3 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 13 Dec 2020 12:48:48 +0200 Subject: [PATCH] ssh rsync deployment --- package.json | 4 ++-- src/squeebot.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index cee702f..a8837ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@squeebot/cli", - "version": "3.0.1", + "version": "3.0.2", "description": "Squeebot v3 runtime, environments and configuration", "main": "dist/squeebot.js", "bin": { @@ -29,7 +29,7 @@ "typescript": "^4.0.5" }, "dependencies": { - "@squeebot/core": "^3.0.1", + "@squeebot/core": "^3.1.0", "fs-extra": "^9.0.1", "node-watch": "^0.7.0", "tar": "^6.0.5", diff --git a/src/squeebot.ts b/src/squeebot.ts index 2f9783c..79200b2 100644 --- a/src/squeebot.ts +++ b/src/squeebot.ts @@ -44,9 +44,11 @@ function execute(cmd: any[], loc: string): Promise { return new Promise((resolve, reject) => { const cproc = child_process.spawn(cmd[0], cmd.slice(1), { cwd: loc, - stdio: 'pipe', }); + cproc.stdout.pipe(process.stdout); + cproc.stderr.pipe(process.stderr); + cproc.on('exit', (code: number) => { if (code !== 0) { return reject(new Error('Build failed!')); @@ -135,6 +137,43 @@ async function newRepository( console.log('\nDone! Your repository "%s" lives at:', name, location); } +async function sshDeploy( + outDir: string, + sshconf: any): Promise { + + const key = sshconf.key; + const host = sshconf.host; + const username = sshconf.user; + const port = sshconf.port || 22; + const target = sshconf.target; + const args = sshconf.args || '-rltgoDzvO'; + + if (!key || !host || !username || !target) { + console.error('SSH deployment requires more arguments.'); + return; + } + + console.log('Deploying to %s@%s:%d%s', username, host, port, target); + + const fileList = (await fs.readdir(outDir)).filter((val) => { + return val === 'repository.json' || val.match('.tgz$'); + }).map((x) => path.join(outDir, x)); + + const pargs = [ + 'rsync', + args, + '-e', + `ssh -i ${key} -p ${port}`, + ...fileList, + `${username}@${host}:${target}`, + ]; + + console.log(pargs.join(' ')); + await execute(pargs, outDir); + + console.log('Done!'); +} + async function deploy( name: string, location: string, @@ -182,6 +221,13 @@ async function deploy( console.log('Done!'); } + + if (deployment in meta) { + const dpl = meta[deployment]; + if (dpl.ssh) { + await sshDeploy(outDir, dpl); + } + } } async function buildRepository(