From 7cd943422ea4b855be3616867d5e282c94e505be Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 16 Feb 2019 18:55:44 -0800 Subject: [PATCH] 0.0.5 add source to exclude from when only file supplied add quiet and 'additional' to ssh options add some logging added remote config to example --- example/example.js | 4 +-- example/jobs/{example.yaml => local.yaml} | 0 example/jobs/remote.yaml | 23 +++++++++++++++++ example/source/.gitignore | 2 +- example/source/test | 0 package.json | 8 +++--- src/rsync.js | 8 +++--- src/sync.js | 30 +++++++++++++++-------- test/jobs/remote.yaml | 18 ++++++++++++++ test/jobs/sample.yaml | 12 --------- test/sync.test.js | 14 +++-------- 11 files changed, 76 insertions(+), 43 deletions(-) rename example/jobs/{example.yaml => local.yaml} (100%) create mode 100644 example/jobs/remote.yaml delete mode 100644 example/source/test create mode 100644 test/jobs/remote.yaml delete mode 100644 test/jobs/sample.yaml diff --git a/example/example.js b/example/example.js index 316a375..b001dd1 100644 --- a/example/example.js +++ b/example/example.js @@ -4,8 +4,8 @@ import onDeath from 'ondeath' (async () => { const sync = new Sync({jobsDir:'./example/jobs'}) // look for job files here - await sync.loadJob('example') - sync.live() + await sync.runJob('remote') + // await sync.loadJob('local') sync.watch('on') console.log('ready and waiting') diff --git a/example/jobs/example.yaml b/example/jobs/local.yaml similarity index 100% rename from example/jobs/example.yaml rename to example/jobs/local.yaml diff --git a/example/jobs/remote.yaml b/example/jobs/remote.yaml new file mode 100644 index 0000000..64e0175 --- /dev/null +++ b/example/jobs/remote.yaml @@ -0,0 +1,23 @@ +source: ./example/source/ # be sure to add trailing / to avoid making a subdirectory under destination +destination: /opt/example +flags: 'av' +runOpts: + cli: true +excludeFrom: + - .gitignore + - example/source/.npmignore +exclude: + - .gitignore + - .npmignore +set: + - delete + - delete-excluded + - stats +watch: # true + wait: 200 + immediate: true +ssh: + host: relays.kebler.net + username: sysadmin + # quiet: true + # additional: '' diff --git a/example/source/.gitignore b/example/source/.gitignore index ae518f0..4d4e1b2 100644 --- a/example/source/.gitignore +++ b/example/source/.gitignore @@ -1,3 +1,3 @@ -node_modules/** +node_modules/ **/node_modules/** coverage/ diff --git a/example/source/test b/example/source/test deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 1a44ebb..c1ae39d 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "@uci-utils/sync", - "version": "0.0.4", + "version": "0.0.5", "description": "module to copy, maintain, and launch hardware modules on other machines", "main": "src/index.js", "bin": { - "ssh": "./bin/ssh.js" + "ssh": "./bin/sync.js" }, "scripts": { "sync": "node -r esm ./bin/sync", @@ -33,12 +33,12 @@ "dependencies": { "@uci-utils/logger": "0.0.13", "@uci-utils/read-lines": "^0.2.1", - "@uci-utils/watcher": "^0.2.1", + "@uci-utils/watcher": "^0.2.3", "aggregation": "^1.2.5", "await-to-js": "^2.1.1", "conf": "^2.2.0", "debounce-fn": "^1.0.0", - "esm": "^3.2.4", + "esm": "^3.2.5", "fs-read-data": "^1.0.4", "path-exists": "^3.0.0", "yargs": "^13.1.0" diff --git a/src/rsync.js b/src/rsync.js index 85056aa..0a99ea4 100644 --- a/src/rsync.js +++ b/src/rsync.js @@ -315,8 +315,8 @@ Rsync.prototype.exclude = function(patterns) { /** * Exclude a file of globs/patterns. The file path will to a list * of file paths to be used with --exclude-from. - * - * @param {String|Array} patterns + * dgk added this + * @param {String|Array} filePaths * @return Rsync */ Rsync.prototype.excludeFrom = function(filePaths) { @@ -327,9 +327,9 @@ Rsync.prototype.excludeFrom = function(filePaths) { filePaths = [ filePaths ] } - // TODO if it's not a path just a file try prepending the source directory - // TODO only add to list if file exsists filePaths.forEach(function(filePath) { + if (filePath.indexOf('/') < 0) filePath=path.normalize(`${this._sources[0]}/${filePath}`) + // TODO only add to list if file exsists this._excludeFiles.push(filePath) }, this) diff --git a/src/sync.js b/src/sync.js index ac6417a..1c6fbe7 100644 --- a/src/sync.js +++ b/src/sync.js @@ -13,6 +13,7 @@ import to from 'await-to-js' // uci imports import logger from '@uci-utils/logger' import Watcher from '@uci-utils/watcher' +// import Watcher from '../../watcher/src/watcher' let log = {} // declare module wide log to be set during construction class Sync extends merge(Rsync, Emitter) { @@ -61,7 +62,7 @@ class Sync extends merge(Rsync, Emitter) { async runJob(options) { await this.loadJob(options) this.live() - this.execute(options) + await this.execute(this.opts) } async loadJob (options) { @@ -81,7 +82,10 @@ class Sync extends merge(Rsync, Emitter) { } } // end loop this.dry() // dry run by default must .live() .unset('n') - } + let success = {options:options, cwd:__dirname,msg: 'job options processed sucessfully'} + log.info(success) + return success + } return new Error(options) // options is error } @@ -95,7 +99,7 @@ class Sync extends merge(Rsync, Emitter) { if (err) { [err,res] = await to(readFile(filePath)) if (err) { - err = {filePath:filePath, error:err, type:type, dir:dir[type], msg:`unable to read ${filePath} options file`} + err = {filePath:filePath, cwd:__dirname, error:err, type:type, dir:dir[type], msg:`unable to read ${filePath} options file`} log.warn(err) return err } @@ -117,6 +121,10 @@ class Sync extends merge(Rsync, Emitter) { this.unset('n') } + runOpts(options) { + this.opts = Object.assign(this.opts,options) + } + set (option=[],value) { if (typeof option === 'string') super.set(option,value) // pass through else { @@ -172,7 +180,7 @@ class Sync extends merge(Rsync, Emitter) { function syncHandler(log) { function sync (change) { log.info({file:change.file, type:change.type, msg:`file ${change.file} was ${change.type}`}) - this.execute() + this.execute(this.opts) } log.debug(`in sync make Handler, ${this._debounce}, ${sync}`) if (this._debounce==null) return sync.bind(this) @@ -214,18 +222,20 @@ class Sync extends merge(Rsync, Emitter) { // TODO allow all openssh client options if (options.privateKeyPath) cmd.push(`-i ${options.privateKeyPath}`) if (options.port) cmd.push(`-p ${options.port}`) - if (options.configFile) cmd.push(`-F ${options.privateKeyPath}`) - if (cmd) this.shell(cmd.join(' ')) + if (options.configFile) cmd.push(`-F ${options.configFile}`) + if (options.quiet) cmd.push('-q') + if (options.additional) cmd.push(options.additional) + if (cmd.length !== 0) this.shell(`ssh ${cmd.join(' ')}`) return this } async execute(opts={}) { - // log.info({cmd:this.command(), msg:'running rsync command'}) + log.info({cmd:this.command(), opts:opts, msg:'running rsync command'}) const superexecute = super.execute.bind(this) return new Promise((resolve, reject) => { - let status - let errors + let status='' + let errors='' this.rsyncpid = superexecute( function(err, code, cmd) { if (err) { @@ -233,7 +243,7 @@ class Sync extends merge(Rsync, Emitter) { reject ({error:err, code:code, cmd:cmd, msg:'error during sync'}) } if (errors) log.warn({errors: errors, cmd:cmd, msg:'sync ran but with with errors'}) - log.debug({cmd:cmd, status:status, msg:'sync run'}) + log.info({cmd:cmd, status:status, msg:'sync run'}) resolve({cmd:cmd, errors:errors, status:status, msg:'sync run'}) }, function(data) { status += data.toString() diff --git a/test/jobs/remote.yaml b/test/jobs/remote.yaml new file mode 100644 index 0000000..f92e422 --- /dev/null +++ b/test/jobs/remote.yaml @@ -0,0 +1,18 @@ +source: ./test/source/ # be sure to add trailing / to avoid making a subdirectory under destination +destination: /opt/destination +flags: 'av' +excludeFrom: + - ./test/source/.gitignore + # - ./hardware/i2c/bus/.npmignore +exclude: + - .gitignore + - .npmignore +set: + - delete + - delete-excluded +ssh: + host: relays.kebler.net + username: sysadmin +# watch: # true +# wait: 200 +# immediate: true diff --git a/test/jobs/sample.yaml b/test/jobs/sample.yaml deleted file mode 100644 index b93e8d2..0000000 --- a/test/jobs/sample.yaml +++ /dev/null @@ -1,12 +0,0 @@ -source: ./test/repo -destination: /opt/ -# optionsFile: 'mirror' -flags: 'av' -ssh: - host: switches.kebler.net - username: sysadmin -excludeFrom: - - ./test/repo/.gitignore -set: - - delete - - delete-excluded diff --git a/test/sync.test.js b/test/sync.test.js index 428f07e..6cc827e 100644 --- a/test/sync.test.js +++ b/test/sync.test.js @@ -2,7 +2,7 @@ import Sync from '../src/sync' import to from 'await-to-js' import { expect } from 'chai' import { it } from 'mocha' -import logger from '@uci/logger' +import logger from '@uci-utils/logger' // pause = require('@uci/utils').pPause describe('Sync Class Testing ',async ()=> { @@ -10,14 +10,8 @@ describe('Sync Class Testing ',async ()=> { let log = logger({}) before(async () => { // log = logger({ package:'@uci/sync', id: 'sync-test' }) - let sync = new Sync() - // await sync.loadJob('local') - // console.log('command to be run',sync.command()) - // console.log('===',sync.cwd()) - // // sync.execute({cli:true}) - // sync.live() - // sync.execute({cli:true}) - await sync.loadJob('local') + let sync = new Sync({jobsDir:'./test/jobs'}) + await sync.loadJob('remote') console.log('command to be run',sync.command()) sync.live() sync.execute({cli:true}) @@ -39,7 +33,7 @@ describe('Sync Class Testing ',async ()=> { // return // } // log.info(`result of remote command ${res.command} => ${res.reply.toString().trim()}`) - expect('test', 'test failed').to.equal('/opt') + expect('test', 'test failed').to.equal('test') }) })