meshblu-verifier-http
Version:
Verify Meshblu over HTTP
146 lines (127 loc) • 3.62 kB
text/coffeescript
_ = require 'lodash'
async = require 'async'
colors = require 'colors'
dashdash = require 'dashdash'
MeshbluConfig = require 'meshblu-config'
moment = require 'moment'
request = require 'request'
packageJSON = require './package.json'
Verifier = require './src/verifier'
VERIFIER_NAME ='meshblu-verifier-http'
debug = require('debug')("#{VERIFIER_NAME}:command")
OPTIONS = [
{
names: ['help', 'h']
type: 'bool'
help: 'Print this help and exit.'
}
{
names: ['log-expiration', 'e']
type: 'integer'
env: 'LOG_EXPIRATION'
help: 'number of seconds the verification status is good for. (default: 300)'
helpWrap: true
default: 300
}
{
names: ['log-url', 'u']
type: 'string'
env: 'LOG_URL'
help: 'The fully qualified url to post the verifier status to.'
helpArg: 'URL'
}
{
names: ['forever', 'f']
type: 'bool'
env: 'FOREVER'
help: 'The fully qualified url to post the verifier status to. (default: false)'
default: false
}
{
names: ['interval', 'i']
type: 'integer'
env: 'INTERVAL_SECONDS'
help: 'Interval delay in seconds when running in forever mode'
default: 60
}
{
names: ['timeout', 't']
type: 'integer'
env: 'TIMEOUT_SECONDS'
help: 'Time to wait before configuring a test as failed'
default: 30
}
{
names: ['version', 'v']
type: 'bool'
help: 'Print the version and exit.'
}
]
class Command
constructor: ->
process.on 'uncaughtException',
= dashdash.createParser options: OPTIONS
options = ()
debug 'got options', options
{
,
,
,
,
,
} = options
printHelp: =>
options = {includeEnv: true, includeDefault: true}
console.log "usage: #{VERIFIER_NAME} [OPTIONS]\noptions:\n#{@parser.help(options)}"
parseOptions: =>
options = .parse(process.argv)
if options.help
()
process.exit 0
if options.version
console.log packageJSON.version
process.exit 0
if !options.log_url
()
console.error colors.red 'Missing required parameter --log-url, -u, or env: LOG_URL'
process.exit 1
return options
run: =>
(error) =>
return (error) unless
return (error) if error?
async.forever (callback) =>
_.delay , ( * 1000), callback
,
runOnce: (callback) =>
timeoutSeconds = ( * 1000)
debug 'running with timeout', timeoutSeconds
run = async.timeout , timeoutSeconds
run (error) =>
error = new Error 'Timeout Exceeded' if error?.code == 'ETIMEDOUT'
error, callback
_runOnce: (callback) =>
meshbluConfig = new MeshbluConfig().toJSON()
verifier = new Verifier {meshbluConfig}
verifier.verify callback
logResult: (error, callback) =>
debug 'logging results', { error }
request.post , {
json:
success: !error?
expires: moment().add(, 'seconds').utc().format()
error:
message: error?.message
}, callback
print: (error) =>
return console.log "#{VERIFIER_NAME} successful" unless error?
console.log "#{VERIFIER_NAME} error"
console.error error.stack
die: (error) =>
process.exit(0) unless error?
process.exit(1) if error?
printAndDie: (error) =>
error
error
commandWork = new Command()
commandWork.run()