Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jose Constela
committed
Feb 24, 2019
0 parents
commit a8e6a54
Showing
12 changed files
with
1,387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
docker: | ||
- image: circleci/node:10.11 | ||
working_directory: ~/repo | ||
steps: | ||
- checkout | ||
- restore_cache: | ||
keys: | ||
- v1-dependencies-{{ checksum "package.json" }} | ||
# fallback to using the latest cache if no exact match is found | ||
- v1-dependencies- | ||
- run: yarn install | ||
- save_cache: | ||
paths: | ||
- node_modules | ||
key: v1-dependencies-{{ checksum "package.json" }} | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2019 Jose Constela <jose@joseconstela.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Tideflow's agent | ||
|
||
> Tideflow.io's agent | ||
[![CircleCI](https://circleci.com/gh/tideflow-io/tfagent.svg?style=svg)](https://circleci.com/gh/tideflow-io/tfagent) [![Greenkeeper badge](https://badges.greenkeeper.io/tideflow-io/tfagent.svg)](https://greenkeeper.io/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const pjson = require('./package.json') | ||
const io = require('socket.io-client') | ||
const colors = require('colors') | ||
const pretty = require('./helpers/pretty') | ||
const runner = require('./runner') | ||
|
||
/** | ||
* @param {Object} cli's parameters parsed via commander's npm package | ||
*/ | ||
module.exports.exec = (program) => { | ||
|
||
let agent = {} | ||
|
||
// Shows welcome messages | ||
console.log(pretty.logo()) | ||
console.log(` || Tideflow.io - agent ${pjson.version}`.blue) | ||
|
||
const URL = process.env.TF_AGENT_LOCAL ? | ||
'http://localhost:1337' : | ||
'https://platform-q.tideflow.io:1337' | ||
|
||
const socket = io(`${URL}?token=${program.token}`) | ||
|
||
// socket.on('connect', function () {}) | ||
|
||
// socket.on('reconnect', function () {}) | ||
|
||
// Execute command | ||
socket.on('tf.command', function (req) { | ||
if (!agent.authenticated) return | ||
runner.cmd(socket, 'tf.command', req) | ||
}) | ||
|
||
// Execute code | ||
socket.on('tf.code', function () { | ||
if (!agent.token) return | ||
}) | ||
|
||
// Authorize agent | ||
socket.on('tf.authz', function (auth) { | ||
agent = Object.assign({authenticated: true}, agent, auth) | ||
console.log(` || Agent ${auth.title} authorized`.green) | ||
}) | ||
|
||
// Show a connection lost message | ||
socket.on('reconnecting', function () { | ||
console.error(` || Connection lost, reconnecting...`.red) | ||
}) | ||
|
||
// Warm the user in case of error | ||
socket.on('error', function (e) { | ||
console.error(` || Error`.red) | ||
console.error(` || ${e}`.red) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Returns a pretty logo for terminal | ||
*/ | ||
module.exports.logo = () => { | ||
return ` | ||
_ _ _ ___ _ _ | ||
| |_|_|_| |___| _| |___ _ _ _ |_|___ | ||
| _| | . | -_| _| | . | | | |_| | . | | ||
|_| |_|___|___|_| |_|___|_____|_|_|___| | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const colors = require('colors') | ||
|
||
/** | ||
* Given an std output (either stdout or stderr) in either array or string | ||
* format, conver it to an array and remove empty lines. | ||
* | ||
* For example, if a command outputs the following: | ||
* | ||
* Command in progress... | ||
* | ||
* Please wait a moment. | ||
* | ||
* The filtered result must look like: | ||
* | ||
* ['Command in progress...', 'Please wait a moment.'] | ||
* | ||
* @param {Array|string} input std to be converted into a filtered array. | ||
* @returns {Array} List of strings from the original input. | ||
*/ | ||
const parseStd = (input) => { | ||
|
||
// | ||
if (!input) return [] | ||
try { | ||
return (Array.isArray(input) ? input : input.toString().split('\n')) | ||
.map(l => l.trim()) | ||
.filter(Boolean) || null | ||
} | ||
catch (ex) { | ||
return [] | ||
} | ||
} | ||
|
||
module.exports.parseStd = parseStd | ||
|
||
/** | ||
* Reports stds outputed by the in-execution command. | ||
* | ||
* @param {Object} socket Socket's io socket that requested the execution | ||
* @param {String} topic Original message's topic | ||
* @param {Object} originalMessage Message that originally requested the execution | ||
* @param {String} stdout Command's stdout output | ||
* @param {String} stderr Command's stderr output | ||
*/ | ||
const progress = (socket, topic, originalMessage, stdout, stderr) => { | ||
console.log(' || STDOUT/ERR. Reporting...'.yellow) | ||
|
||
const res = Object.assign(originalMessage, { | ||
stdout: parseStd(stdout), | ||
stderr: parseStd(stderr) | ||
}) | ||
|
||
// Make sure the agent doesn't make a report to the server, unless there's | ||
// an actual std output to be reported. | ||
if (!res.stdout && !res.stderr) { return } | ||
|
||
socket.emit(`${topic}.progress`, res) | ||
} | ||
|
||
module.exports.progress = progress | ||
|
||
/** | ||
* Reports the final result of an execution. | ||
* | ||
* @param {Object} socket Socket's io socket that requested the execution | ||
* @param {String} topic Original message's topic | ||
* @param {Object} originalMessage Message that originally requested the execution | ||
* @param {Object} res Object containing both stds | ||
*/ | ||
const result = (socket, topic, originalMessage, res) => { | ||
console.log(' || Command executed. Reporting...'.yellow) | ||
|
||
const message = Object.assign(originalMessage, { | ||
stdout: parseStd(res.stdout), | ||
stderr: parseStd(res.stderr) | ||
}) | ||
|
||
socket.emit(`${topic}.res`, message) | ||
} | ||
|
||
module.exports.result = result | ||
|
||
/** | ||
* Reports an exception | ||
* | ||
* @param {Object} socket Socket's io socket that requested the execution | ||
* @param {Object} originalMessage Message that originally requested the execution | ||
* @param {Object} executionResult Catched exception | ||
*/ | ||
const exception = (socket, topic, originalMessage, ex) => { | ||
console.log(' || Command returned error'.red) | ||
|
||
socket.emit(`${topic}.exception`, Object.assign(originalMessage, {ex})) | ||
} | ||
|
||
module.exports.exception = exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const program = require('commander') | ||
const pkg = require('./package.json') | ||
|
||
program | ||
.version(pkg.version, '-v, --version') | ||
.option('-t, --token [token]', 'Authentication token', (v) => { | ||
return v || process.env.TIDEFLOWIO_AGENT_TOKEN | ||
}) | ||
.option('--noupdate', 'Opt-out of update version check') | ||
|
||
// must be before .parse() since | ||
// node's emit() is immediate | ||
|
||
program.on('--help', function(){ | ||
console.log('') | ||
console.log('Examples:') | ||
console.log(' $ tfio-node --help') | ||
console.log(' $ tfio-node -h') | ||
}) | ||
|
||
program.parse(process.argv) | ||
|
||
if (!program.noupdate) { | ||
// Checks for available updates | ||
require('update-notifier')({ | ||
pkg, | ||
updateCheckInterval: 1000 * 60 * 60 * 24 * 2 // 2 days (actively maintained) | ||
}).notify({defer: false}) | ||
} | ||
|
||
if (typeof program.token === 'undefined') { | ||
console.error('No authentication token provided') | ||
process.exit(1) | ||
} | ||
|
||
require('./agent').exec(program) |
Oops, something went wrong.