mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:13:31 +02:00
Rename "roles" to "units". Put units in their own folders.
This commit is contained in:
30
lib/units/device/support/adb.js
Normal file
30
lib/units/device/support/adb.js
Normal file
@@ -0,0 +1,30 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var adbkit = require('adbkit')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var promiseutil = require('../../../util/promiseutil')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
var log = logger.createLogger('device:support:adb')
|
||||
var adb = adbkit.createClient({
|
||||
host: options.adbHost
|
||||
, port: options.adbPort
|
||||
})
|
||||
adb.Keycode = adbkit.Keycode
|
||||
|
||||
function ensureBootComplete() {
|
||||
return promiseutil.periodicNotify(
|
||||
adb.waitBootComplete(options.serial)
|
||||
, 1000
|
||||
)
|
||||
.progressed(function() {
|
||||
log.info('Waiting for boot to complete')
|
||||
})
|
||||
.timeout(60000)
|
||||
}
|
||||
|
||||
return ensureBootComplete()
|
||||
.return(adb)
|
||||
})
|
||||
14
lib/units/device/support/channels.js
Normal file
14
lib/units/device/support/channels.js
Normal file
@@ -0,0 +1,14 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var ChannelManager = require('../../../wire/channelmanager')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function() {
|
||||
var log = logger.createLogger('device:support:channels')
|
||||
var channels = new ChannelManager()
|
||||
channels.on('timeout', function(channel) {
|
||||
log.info('Channel "%s" timed out', channel)
|
||||
})
|
||||
return channels
|
||||
})
|
||||
29
lib/units/device/support/http.js
Normal file
29
lib/units/device/support/http.js
Normal file
@@ -0,0 +1,29 @@
|
||||
var http = require('http')
|
||||
var util = require('util')
|
||||
|
||||
var syrup = require('syrup')
|
||||
var express = require('express')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
var log = logger.createLogger('device:support:http')
|
||||
, port = options.ports.pop()
|
||||
, app = express()
|
||||
, server = http.createServer(app)
|
||||
|
||||
app.set('strict routing', true)
|
||||
app.set('case sensitive routing', true)
|
||||
app.set('public url', util.format(
|
||||
'http://%s:%s'
|
||||
, options.publicIp
|
||||
, port
|
||||
))
|
||||
|
||||
server.listen(port)
|
||||
|
||||
log.info('Listening on %s', app.get('public url'))
|
||||
|
||||
return app
|
||||
})
|
||||
17
lib/units/device/support/properties.js
Normal file
17
lib/units/device/support/properties.js
Normal file
@@ -0,0 +1,17 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.dependency(require('./adb'))
|
||||
.define(function(options, adb) {
|
||||
var log = logger.createLogger('device:support:properties')
|
||||
|
||||
function load() {
|
||||
log.info('Loading properties')
|
||||
return adb.getProperties(options.serial)
|
||||
.timeout(10000)
|
||||
}
|
||||
|
||||
return load()
|
||||
})
|
||||
19
lib/units/device/support/push.js
Normal file
19
lib/units/device/support/push.js
Normal file
@@ -0,0 +1,19 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var zmq = require('zmq')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
var log = logger.createLogger('device:support:push')
|
||||
|
||||
// Output
|
||||
var push = zmq.socket('push')
|
||||
options.endpoints.push.forEach(function(endpoint) {
|
||||
log.info('Sending output to %s', endpoint)
|
||||
push.connect(endpoint)
|
||||
})
|
||||
|
||||
return push
|
||||
})
|
||||
19
lib/units/device/support/router.js
Normal file
19
lib/units/device/support/router.js
Normal file
@@ -0,0 +1,19 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var wirerouter = require('../../../wire/router')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.dependency(require('./sub'))
|
||||
.dependency(require('./channels'))
|
||||
.define(function(options, sub, channels) {
|
||||
var router = wirerouter()
|
||||
|
||||
sub.on('message', router.handler())
|
||||
|
||||
// Special case, we're hooking into a message that's not actually routed.
|
||||
router.on({$code: 'message'}, function(channel) {
|
||||
channels.keepalive(channel)
|
||||
})
|
||||
|
||||
return router
|
||||
})
|
||||
53
lib/units/device/support/storage.js
Normal file
53
lib/units/device/support/storage.js
Normal file
@@ -0,0 +1,53 @@
|
||||
var util = require('util')
|
||||
|
||||
var syrup = require('syrup')
|
||||
var Promise = require('bluebird')
|
||||
var request = require('request')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
var log = logger.createLogger('device:support:storage')
|
||||
var plugin = Object.create(null)
|
||||
|
||||
plugin.store = function(type, stream, meta) {
|
||||
var resolver = Promise.defer()
|
||||
|
||||
var req = request.post({
|
||||
url: util.format('%sapi/v1/s/%s', options.storageUrl, type)
|
||||
}
|
||||
, function(err, res, body) {
|
||||
if (err) {
|
||||
log.error('Upload failed', err.stack)
|
||||
resolver.reject(err)
|
||||
}
|
||||
else if (res.statusCode !== 201) {
|
||||
log.error('Upload failed: HTTP %d', res.statusCode)
|
||||
resolver.reject(new Error(util.format(
|
||||
'Upload failed: HTTP %d'
|
||||
, res.statusCode
|
||||
)))
|
||||
}
|
||||
else {
|
||||
try {
|
||||
var result = JSON.parse(body)
|
||||
log.info('Uploaded to %s', result.resources.file.href)
|
||||
resolver.resolve(result.resources.file)
|
||||
}
|
||||
catch (err) {
|
||||
log.error('Invalid JSON in response', err.stack, body)
|
||||
resolver.reject(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
req.form()
|
||||
.append('file', stream, meta)
|
||||
|
||||
return resolver.promise
|
||||
}
|
||||
|
||||
return plugin
|
||||
})
|
||||
26
lib/units/device/support/sub.js
Normal file
26
lib/units/device/support/sub.js
Normal file
@@ -0,0 +1,26 @@
|
||||
var syrup = require('syrup')
|
||||
|
||||
var zmq = require('zmq')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var wireutil = require('../../../wire/util')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
var log = logger.createLogger('device:support:sub')
|
||||
|
||||
// Input
|
||||
var sub = zmq.socket('sub')
|
||||
options.endpoints.sub.forEach(function(endpoint) {
|
||||
log.info('Receiving input from %s', endpoint)
|
||||
sub.connect(endpoint)
|
||||
})
|
||||
|
||||
// Establish always-on channels
|
||||
;[wireutil.global].forEach(function(channel) {
|
||||
log.info('Subscribing to permanent channel "%s"', channel)
|
||||
sub.subscribe(channel)
|
||||
})
|
||||
|
||||
return sub
|
||||
})
|
||||
Reference in New Issue
Block a user