2017-03-08 18:47:47 +00:00
|
|
|
###
|
2018-10-19 14:38:50 +00:00
|
|
|
Copyright 2017 Balena
|
2017-03-08 18:47:47 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
###
|
|
|
|
|
|
|
|
dockerInfoProperties = [
|
|
|
|
'Containers'
|
|
|
|
'ContainersRunning'
|
|
|
|
'ContainersPaused'
|
|
|
|
'ContainersStopped'
|
|
|
|
'Images'
|
|
|
|
'Driver'
|
|
|
|
'SystemTime'
|
|
|
|
'KernelVersion'
|
|
|
|
'OperatingSystem'
|
|
|
|
'Architecture'
|
|
|
|
]
|
|
|
|
|
|
|
|
dockerVersionProperties = [
|
|
|
|
'Version'
|
|
|
|
'ApiVersion'
|
|
|
|
]
|
|
|
|
|
|
|
|
module.exports =
|
2019-05-16 07:53:24 +00:00
|
|
|
signature: 'scan'
|
2018-10-19 14:38:50 +00:00
|
|
|
description: 'Scan for balenaOS devices in your local network'
|
2017-03-08 18:47:47 +00:00
|
|
|
help: '''
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2019-05-16 07:53:24 +00:00
|
|
|
$ balena scan
|
|
|
|
$ balena scan --timeout 120
|
|
|
|
$ balena scan --verbose
|
2017-03-08 18:47:47 +00:00
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
signature: 'verbose'
|
|
|
|
boolean: true
|
|
|
|
description: 'Display full info'
|
|
|
|
alias: 'v'
|
|
|
|
,
|
|
|
|
signature: 'timeout'
|
|
|
|
parameter: 'timeout'
|
|
|
|
description: 'Scan timeout in seconds'
|
|
|
|
alias: 't'
|
|
|
|
]
|
|
|
|
primary: true
|
2017-03-10 13:54:26 +00:00
|
|
|
root: true
|
2017-03-08 18:47:47 +00:00
|
|
|
action: (params, options, done) ->
|
|
|
|
Promise = require('bluebird')
|
|
|
|
_ = require('lodash')
|
|
|
|
prettyjson = require('prettyjson')
|
2018-10-19 14:38:50 +00:00
|
|
|
{ discover } = require('balena-sync')
|
2017-03-08 18:47:47 +00:00
|
|
|
{ SpinnerPromise } = require('resin-cli-visuals')
|
2019-05-16 07:53:24 +00:00
|
|
|
{ dockerPort, dockerTimeout } = require('./local/common')
|
|
|
|
dockerUtils = require('../utils/docker')
|
|
|
|
{ exitWithExpectedError } = require('../utils/patterns')
|
2017-03-08 18:47:47 +00:00
|
|
|
|
|
|
|
if options.timeout?
|
|
|
|
options.timeout *= 1000
|
|
|
|
|
|
|
|
Promise.try ->
|
|
|
|
new SpinnerPromise
|
2018-10-19 14:38:50 +00:00
|
|
|
promise: discover.discoverLocalBalenaOsDevices(options.timeout)
|
|
|
|
startMessage: 'Scanning for local balenaOS devices..'
|
2017-03-08 18:47:47 +00:00
|
|
|
stopMessage: 'Reporting scan results'
|
2017-03-09 19:52:19 +00:00
|
|
|
.filter ({ address }) ->
|
2017-03-10 15:11:27 +00:00
|
|
|
Promise.try ->
|
2017-12-06 21:16:41 +00:00
|
|
|
docker = dockerUtils.createClient(host: address, port: dockerPort, timeout: dockerTimeout)
|
2017-03-10 15:11:27 +00:00
|
|
|
docker.pingAsync()
|
|
|
|
.return(true)
|
|
|
|
.catchReturn(false)
|
2017-03-08 18:47:47 +00:00
|
|
|
.tap (devices) ->
|
|
|
|
if _.isEmpty(devices)
|
2018-10-19 14:38:50 +00:00
|
|
|
exitWithExpectedError('Could not find any balenaOS devices in the local network')
|
2017-03-08 18:47:47 +00:00
|
|
|
.map ({ host, address }) ->
|
2017-12-06 21:16:41 +00:00
|
|
|
docker = dockerUtils.createClient(host: address, port: dockerPort, timeout: dockerTimeout)
|
2017-03-08 18:47:47 +00:00
|
|
|
Promise.props
|
|
|
|
dockerInfo: docker.infoAsync().catchReturn('Could not get Docker info')
|
|
|
|
dockerVersion: docker.versionAsync().catchReturn('Could not get Docker version')
|
|
|
|
.then ({ dockerInfo, dockerVersion }) ->
|
|
|
|
|
|
|
|
if not options.verbose
|
|
|
|
dockerInfo = _.pick(dockerInfo, dockerInfoProperties) if _.isObject(dockerInfo)
|
|
|
|
dockerVersion = _.pick(dockerVersion, dockerVersionProperties) if _.isObject(dockerVersion)
|
|
|
|
|
|
|
|
return { host, address, dockerInfo, dockerVersion }
|
|
|
|
.then (devicesInfo) ->
|
|
|
|
console.log(prettyjson.render(devicesInfo, noColor: true))
|
|
|
|
.nodeify(done)
|