Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
420 lines (392 loc) · 12.8 KB
/
Copy pathindex.js
File metadata and controls
420 lines (392 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
module.exports.command = 'local [serial..]'
module.exports.describe = 'Start a complete local development environment.'
module.exports.builder = function(yargs) {
var os = require('os')
return yargs
.env('STF_LOCAL')
.strict()
.option('adb-host', {
describe: 'The ADB server host.'
, type: 'string'
, default: '127.0.0.1'
})
.option('adb-port', {
describe: 'The ADB server port.'
, type: 'number'
, default: 5037
})
.option('allow-remote', {
alias: 'R'
, describe: 'Whether to allow remote devices in STF. Highly ' +
'unrecommended due to almost unbelievable slowness on the ADB side ' +
'and duplicate device issues when used locally while having a ' +
'cable connected at the same time.'
, type: 'boolean'
})
.option('api-port', {
describe: 'The port the api unit should run at.'
, type: 'number'
, default: 7106
})
.option('app-port', {
describe: 'The port the app unit should run at.'
, type: 'number'
, default: 7105
})
.option('auth-options', {
describe: 'JSON array of options to pass to the auth unit.'
, type: 'string'
, default: '[]'
})
.option('auth-port', {
describe: 'The port the auth unit should run at.'
, type: 'number'
, default: 7120
})
.option('auth-secret', {
describe: 'The secret to use for auth JSON Web Tokens. Anyone who ' +
'knows this token can freely enter the system if they want, so keep ' +
'it safe.'
, type: 'string'
, default: 'kute kittykat'
})
.option('auth-type', {
describe: 'The type of auth unit to start.'
, type: 'string'
, choices: ['mock', 'ldap', 'oauth2', 'saml2', 'openid']
, default: 'mock'
})
.option('auth-url', {
alias: 'a'
, describe: 'URL to the auth unit.'
, type: 'string'
})
.option('bind-app-dealer', {
describe: 'The address to bind the app-side ZeroMQ DEALER endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7112'
})
.option('bind-app-pub', {
describe: 'The address to bind the app-side ZeroMQ PUB endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7111'
})
.option('bind-app-pull', {
describe: 'The address to bind the app-side ZeroMQ PULL endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7113'
})
.option('bind-dev-dealer', {
describe: 'The address to bind the device-side ZeroMQ DEALER endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7115'
})
.option('bind-dev-pub', {
describe: 'The address to bind the device-side ZeroMQ PUB endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7114'
})
.option('bind-dev-pull', {
describe: 'The address to bind the device-side ZeroMQ PULL endpoint to.'
, type: 'string'
, default: 'tcp://127.0.0.1:7116'
})
.option('cleanup', {
describe: 'Attempt to reset the device between uses by uninstalling' +
'apps, resetting accounts and clearing caches. Does not do a perfect ' +
'job currently. Negate with --no-cleanup.'
, type: 'boolean'
, default: true
})
.option('group-timeout', {
alias: 't'
, describe: 'Timeout in seconds for automatic release of inactive devices.'
, type: 'number'
, default: 900
})
.option('lock-rotation', {
describe: 'Whether to lock rotation when devices are being used. ' +
'Otherwise changing device orientation may not always work due to ' +
'sensitive sensors quickly or immediately reverting it back to the ' +
'physical orientation.'
, type: 'boolean'
})
.option('mute-master', {
describe: 'Whether to mute master volume.'
, choices: ['always', 'inuse', 'never']
, default: 'never'
, coerce: val => {
if (val === true) {
return 'inuse' // For backwards compatibility.
}
if (val === false) {
return 'never' // For backwards compatibility.
}
return val
}
})
.option('port', {
alias: ['p', 'poorxy-port']
, describe: 'The port STF should run at.'
, type: 'number'
, default: 7100
})
.option('provider', {
describe: 'An easily identifiable name for the UI and/or log output.'
, type: 'string'
, default: os.hostname()
})
.option('provider-max-port', {
describe: 'Highest port number for device workers to use.'
, type: 'number'
, default: 7700
})
.option('provider-min-port', {
describe: 'Lowest port number for device workers to use.'
, type: 'number'
, default: 7400
})
.option('public-ip', {
describe: 'The IP or hostname to use in URLs.'
, type: 'string'
, default: 'localhost'
})
.option('serial', {
describe: 'Only use devices with these serial numbers.'
, type: 'array'
})
.option('storage-options', {
describe: 'JSON array of options to pass to the storage unit.'
, type: 'string'
, default: '[]'
})
.option('storage-plugin-apk-port', {
describe: 'The port the storage-plugin-apk unit should run at.'
, type: 'number'
, default: 7104
})
.option('storage-plugin-image-port', {
describe: 'The port the storage-plugin-image unit should run at.'
, type: 'number'
, default: 7103
})
.option('storage-port', {
describe: 'The port the storage unit should run at.'
, type: 'number'
, default: 7102
})
.option('storage-type', {
describe: 'The type of storage unit to start.'
, type: 'string'
, choices: ['temp', 's3']
, default: 'temp'
})
.option('user-profile-url', {
describe: 'URL to external user profile page'
, type: 'string'
})
.option('vnc-initial-size', {
describe: 'The initial size to use for the experimental VNC server.'
, type: 'string'
, default: '600x800'
, coerce: function(val) {
return val.split('x').map(Number)
}
})
.option('websocket-port', {
describe: 'The port the websocket unit should run at.'
, type: 'number'
, default: 7110
})
.epilog('Each option can be be overwritten with an environment variable ' +
'by converting the option to uppercase, replacing dashes with ' +
'underscores and prefixing it with `STF_LOCAL_` (e.g. ' +
'`STF_LOCAL_ALLOW_REMOTE`).')
}
module.exports.handler = function(argv) {
var util = require('util')
var path = require('path')
var Promise = require('bluebird')
var logger = require('../../util/logger')
var log = logger.createLogger('cli:local')
var procutil = require('../../util/procutil')
// Each forked process waits for signals to stop, and so we run over the
// default limit of 10. So, it's not a leak, but a refactor wouldn't hurt.
process.setMaxListeners(20)
function run() {
var procs = [
// app triproxy
procutil.fork(path.resolve(__dirname, '..'), [
'triproxy', 'app001'
, '--bind-pub', argv.bindAppPub
, '--bind-dealer', argv.bindAppDealer
, '--bind-pull', argv.bindAppPull
])
// device triproxy
, procutil.fork(path.resolve(__dirname, '..'), [
'triproxy', 'dev001'
, '--bind-pub', argv.bindDevPub
, '--bind-dealer', argv.bindDevDealer
, '--bind-pull', argv.bindDevPull
])
// processor one
, procutil.fork(path.resolve(__dirname, '..'), [
'processor', 'proc001'
, '--connect-app-dealer', argv.bindAppDealer
, '--connect-dev-dealer', argv.bindDevDealer
])
// processor two
, procutil.fork(path.resolve(__dirname, '..'), [
'processor', 'proc002'
, '--connect-app-dealer', argv.bindAppDealer
, '--connect-dev-dealer', argv.bindDevDealer
])
// reaper one
, procutil.fork(path.resolve(__dirname, '..'), [
'reaper', 'reaper001'
, '--connect-push', argv.bindDevPull
, '--connect-sub', argv.bindAppPub
])
// provider
, procutil.fork(path.resolve(__dirname, '..'), [
'provider'
, '--name', argv.provider
, '--min-port', argv.providerMinPort
, '--max-port', argv.providerMaxPort
, '--connect-sub', argv.bindDevPub
, '--connect-push', argv.bindDevPull
, '--group-timeout', argv.groupTimeout
, '--public-ip', argv.publicIp
, '--storage-url'
, util.format('http://localhost:%d/', argv.port)
, '--adb-host', argv.adbHost
, '--adb-port', argv.adbPort
, '--vnc-initial-size', argv.vncInitialSize.join('x')
, '--mute-master', argv.muteMaster
]
.concat(argv.allowRemote ? ['--allow-remote'] : [])
.concat(argv.lockRotation ? ['--lock-rotation'] : [])
.concat(!argv.cleanup ? ['--no-cleanup'] : [])
.concat(argv.serial))
// auth
, procutil.fork(path.resolve(__dirname, '..'), [
util.format('auth-%s', argv.authType)
, '--port', argv.authPort
, '--secret', argv.authSecret
, '--app-url', util.format(
'http://%s:%d/'
, argv.publicIp
, argv.port
)
].concat(JSON.parse(argv.authOptions)))
// app
, procutil.fork(path.resolve(__dirname, '..'), [
'app'
, '--port', argv.appPort
, '--secret', argv.authSecret
, '--auth-url', argv.authUrl || util.format(
'http://%s:%d/auth/%s/'
, argv.publicIp
, argv.port
, {
oauth2: 'oauth'
, saml2: 'saml'
}[argv.authType] || argv.authType
)
, '--websocket-url', util.format(
'http://%s:%d/'
, argv.publicIp
, argv.websocketPort
)
].concat((function() {
var extra = []
if (argv.userProfileUrl) {
extra.push('--user-profile-url', argv.userProfileUrl)
}
return extra
})()))
// api
, procutil.fork(path.resolve(__dirname, '..'), [
'api'
, '--port', argv.apiPort
, '--secret', argv.authSecret
, '--connect-push', argv.bindAppPull
, '--connect-sub', argv.bindAppPub
])
// websocket
, procutil.fork(path.resolve(__dirname, '..'), [
'websocket'
, '--port', argv.websocketPort
, '--secret', argv.authSecret
, '--storage-url'
, util.format('http://localhost:%d/', argv.port)
, '--connect-sub', argv.bindAppPub
, '--connect-push', argv.bindAppPull
])
// storage
, procutil.fork(path.resolve(__dirname, '..'), [
util.format('storage-%s', argv.storageType)
, '--port', argv.storagePort
].concat(JSON.parse(argv.storageOptions)))
// image processor
, procutil.fork(path.resolve(__dirname, '..'), [
'storage-plugin-image'
, '--port', argv.storagePluginImagePort
, '--storage-url'
, util.format('http://localhost:%d/', argv.port)
])
// apk processor
, procutil.fork(path.resolve(__dirname, '..'), [
'storage-plugin-apk'
, '--port', argv.storagePluginApkPort
, '--storage-url'
, util.format('http://localhost:%d/', argv.port)
])
// poorxy
, procutil.fork(path.resolve(__dirname, '..'), [
'poorxy'
, '--port', argv.port
, '--app-url'
, util.format('http://localhost:%d/', argv.appPort)
, '--auth-url'
, util.format('http://localhost:%d/', argv.authPort)
, '--api-url'
, util.format('http://localhost:%d/', argv.apiPort)
, '--websocket-url'
, util.format('http://localhost:%d/', argv.websocketPort)
, '--storage-url'
, util.format('http://localhost:%d/', argv.storagePort)
, '--storage-plugin-image-url'
, util.format('http://localhost:%d/', argv.storagePluginImagePort)
, '--storage-plugin-apk-url'
, util.format('http://localhost:%d/', argv.storagePluginApkPort)
])
]
function shutdown() {
log.info('Shutting down all child processes')
procs.forEach(function(proc) {
proc.cancel()
})
return Promise.settle(procs)
}
process.on('SIGINT', function() {
log.info('Received SIGINT, waiting for processes to terminate')
})
process.on('SIGTERM', function() {
log.info('Received SIGTERM, waiting for processes to terminate')
})
return Promise.all(procs)
.then(function() {
process.exit(0)
})
.catch(function(err) {
log.fatal('Child process had an error', err.stack)
return shutdown()
.then(function() {
process.exit(1)
})
})
}
return procutil.fork(path.resolve(__dirname, '..'), ['migrate'])
.done(run)
}