2015-08-14 18:11:49 +00:00
|
|
|
Promise = require('bluebird')
|
2015-09-29 17:03:14 +00:00
|
|
|
capitano = Promise.promisifyAll(require('capitano'))
|
2015-08-12 12:17:46 +00:00
|
|
|
_ = require('lodash')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2015-01-21 13:50:19 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2015-07-27 12:08:55 +00:00
|
|
|
form = require('resin-cli-form')
|
2015-08-31 21:39:48 +00:00
|
|
|
events = require('resin-cli-events')
|
2015-08-20 19:54:42 +00:00
|
|
|
rimraf = Promise.promisify(require('rimraf'))
|
|
|
|
patterns = require('../utils/patterns')
|
2015-10-01 17:07:53 +00:00
|
|
|
helpers = require('../utils/helpers')
|
2015-09-29 17:03:14 +00:00
|
|
|
tmp = Promise.promisifyAll(require('tmp'))
|
|
|
|
tmp.setGracefulCleanup()
|
2015-04-20 17:03:19 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
commandOptions = require('./command-options')
|
2014-11-19 17:38:15 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.list =
|
|
|
|
signature: 'devices'
|
|
|
|
description: 'list all devices'
|
|
|
|
help: '''
|
2015-04-27 15:20:53 +00:00
|
|
|
Use this command to list all devices that belong to you.
|
|
|
|
|
|
|
|
You can filter the devices by application by using the `--application` option.
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-04-27 15:20:53 +00:00
|
|
|
$ resin devices
|
2015-03-23 12:17:55 +00:00
|
|
|
$ resin devices --application MyApp
|
2015-04-27 15:20:53 +00:00
|
|
|
$ resin devices --app MyApp
|
|
|
|
$ resin devices -a MyApp
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-04-27 15:20:53 +00:00
|
|
|
options: [ commandOptions.optionalApplication ]
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
2015-10-01 14:39:36 +00:00
|
|
|
primary: true
|
2015-01-16 12:34:59 +00:00
|
|
|
action: (params, options, done) ->
|
2015-08-17 13:49:59 +00:00
|
|
|
Promise.try ->
|
|
|
|
if options.application?
|
|
|
|
return resin.models.device.getAllByApplication(options.application)
|
|
|
|
return resin.models.device.getAll()
|
2015-04-27 15:20:53 +00:00
|
|
|
|
2015-08-17 13:49:59 +00:00
|
|
|
.tap (devices) ->
|
2015-07-29 13:34:31 +00:00
|
|
|
console.log visuals.table.horizontal devices, [
|
2015-01-22 17:06:02 +00:00
|
|
|
'id'
|
|
|
|
'name'
|
2015-01-22 17:20:20 +00:00
|
|
|
'device_type'
|
2015-01-22 17:06:02 +00:00
|
|
|
'is_online'
|
|
|
|
'application_name'
|
|
|
|
'status'
|
|
|
|
'last_seen'
|
2015-01-15 17:10:14 +00:00
|
|
|
]
|
2015-08-17 13:49:59 +00:00
|
|
|
.nodeify(done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
exports.info =
|
2015-07-22 22:06:53 +00:00
|
|
|
signature: 'device <uuid>'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'list a single device'
|
|
|
|
help: '''
|
|
|
|
Use this command to show information about a single device.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-07-22 22:06:53 +00:00
|
|
|
$ resin device 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
2015-10-01 14:39:36 +00:00
|
|
|
primary: true
|
2015-01-16 12:34:59 +00:00
|
|
|
action: (params, options, done) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.models.device.get(params.uuid).then (device) ->
|
2015-07-09 13:56:17 +00:00
|
|
|
|
|
|
|
# TODO: We should outsource this logic and probably
|
|
|
|
# other last_seen edge cases to either Resin CLI Visuals
|
|
|
|
# or have it parsed appropriately in the SDK.
|
|
|
|
device.last_seen ?= 'Not seen'
|
|
|
|
|
2015-07-29 13:34:31 +00:00
|
|
|
console.log visuals.table.vertical device, [
|
|
|
|
"$#{device.name}$"
|
2015-01-22 17:06:02 +00:00
|
|
|
'id'
|
2015-01-22 17:20:20 +00:00
|
|
|
'device_type'
|
2015-01-22 17:06:02 +00:00
|
|
|
'is_online'
|
|
|
|
'ip_address'
|
|
|
|
'application_name'
|
|
|
|
'status'
|
|
|
|
'last_seen'
|
|
|
|
'uuid'
|
|
|
|
'commit'
|
|
|
|
'supervisor_version'
|
|
|
|
'is_web_accessible'
|
|
|
|
'note'
|
2015-01-15 17:10:14 +00:00
|
|
|
]
|
2015-08-31 21:39:48 +00:00
|
|
|
events.send('device.open', device: device.uuid)
|
2015-07-22 22:06:53 +00:00
|
|
|
.nodeify(done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-09-29 18:33:31 +00:00
|
|
|
exports.register =
|
|
|
|
signature: 'device register <application>'
|
|
|
|
description: 'register a device'
|
|
|
|
help: '''
|
|
|
|
Use this command to register a device to an application.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ resin device register MyApp
|
|
|
|
'''
|
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
|
|
|
resin.models.application.get(params.application).then (application) ->
|
2015-10-12 12:34:22 +00:00
|
|
|
|
|
|
|
resin.models.device.generateUUID().then (uuid) ->
|
|
|
|
console.info("Registering to #{application.app_name}: #{uuid}")
|
|
|
|
return resin.models.device.register(application.app_name, uuid)
|
2015-09-29 18:33:31 +00:00
|
|
|
.get('uuid')
|
|
|
|
.nodeify(done)
|
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.remove =
|
2015-07-22 22:06:53 +00:00
|
|
|
signature: 'device rm <uuid>'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'remove a device'
|
|
|
|
help: '''
|
|
|
|
Use this command to remove a device from resin.io.
|
|
|
|
|
|
|
|
Notice this command asks for confirmation interactively.
|
|
|
|
You can avoid this by passing the `--yes` boolean option.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-07-22 22:06:53 +00:00
|
|
|
$ resin device rm 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9
|
|
|
|
$ resin device rm 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 --yes
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
|
|
|
options: [ commandOptions.yes ]
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-08-20 19:54:42 +00:00
|
|
|
patterns.confirm(options.yes, 'Are you sure you want to delete the device?').then ->
|
2015-08-17 13:49:59 +00:00
|
|
|
resin.models.device.remove(params.uuid)
|
2015-08-31 21:39:48 +00:00
|
|
|
.tap ->
|
|
|
|
events.send('device.delete', device: params.uuid)
|
2015-08-17 13:49:59 +00:00
|
|
|
.nodeify(done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
exports.identify =
|
|
|
|
signature: 'device identify <uuid>'
|
|
|
|
description: 'identify a device with a UUID'
|
|
|
|
help: '''
|
|
|
|
Use this command to identify a device.
|
|
|
|
|
|
|
|
In the Raspberry Pi, the ACT led is blinked several times.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin device identify 23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a39849ac0f00db828
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-07-22 22:06:53 +00:00
|
|
|
resin.models.device.identify(params.uuid).nodeify(done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
exports.rename =
|
2015-07-22 22:06:53 +00:00
|
|
|
signature: 'device rename <uuid> [newName]'
|
2015-01-15 17:10:14 +00:00
|
|
|
description: 'rename a resin device'
|
|
|
|
help: '''
|
|
|
|
Use this command to rename a device.
|
|
|
|
|
|
|
|
If you omit the name, you'll get asked for it interactively.
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-07-22 22:06:53 +00:00
|
|
|
$ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 MyPi
|
|
|
|
$ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-08-17 13:49:59 +00:00
|
|
|
Promise.try ->
|
|
|
|
return params.newName if not _.isEmpty(params.newName)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-08-17 13:49:59 +00:00
|
|
|
form.ask
|
|
|
|
message: 'How do you want to name this device?'
|
|
|
|
type: 'input'
|
2015-06-11 16:46:56 +00:00
|
|
|
|
2015-08-17 13:49:59 +00:00
|
|
|
.then(_.partial(resin.models.device.rename, params.uuid))
|
2015-08-31 21:39:48 +00:00
|
|
|
.tap ->
|
|
|
|
events.send('device.rename', device: params.uuid)
|
2015-08-17 13:49:59 +00:00
|
|
|
.nodeify(done)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
exports.init =
|
|
|
|
signature: 'device init'
|
|
|
|
description: 'initialise a device with resin os'
|
|
|
|
help: '''
|
|
|
|
Use this command to download the OS image of a certain application and write it to an SD Card.
|
2015-02-05 13:56:22 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
Notice this command may ask for confirmation interactively.
|
|
|
|
You can avoid this by passing the `--yes` boolean option.
|
2015-03-11 13:05:20 +00:00
|
|
|
|
2015-02-05 13:56:22 +00:00
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-03-23 12:25:45 +00:00
|
|
|
$ resin device init
|
2015-04-20 13:13:15 +00:00
|
|
|
$ resin device init --application MyApp
|
2015-02-04 18:13:28 +00:00
|
|
|
'''
|
|
|
|
options: [
|
2015-03-12 16:03:59 +00:00
|
|
|
commandOptions.optionalApplication
|
2015-08-20 19:54:42 +00:00
|
|
|
commandOptions.yes
|
2015-02-04 18:13:28 +00:00
|
|
|
]
|
|
|
|
permission: 'user'
|
2015-10-01 14:39:36 +00:00
|
|
|
primary: true
|
2015-02-04 18:13:28 +00:00
|
|
|
action: (params, options, done) ->
|
2015-08-20 19:54:42 +00:00
|
|
|
Promise.try ->
|
|
|
|
return options.application if options.application?
|
2015-09-29 15:10:33 +00:00
|
|
|
return patterns.selectApplication()
|
2015-08-20 19:54:42 +00:00
|
|
|
.then(resin.models.application.get)
|
|
|
|
.then (application) ->
|
2015-09-21 11:43:17 +00:00
|
|
|
|
|
|
|
download = ->
|
|
|
|
tmp.tmpNameAsync().then (temporalPath) ->
|
2015-09-30 15:37:27 +00:00
|
|
|
capitano.runAsync("os download #{application.device_type} --output #{temporalPath}")
|
2015-10-12 22:04:52 +00:00
|
|
|
.disposer (temporalPath) ->
|
|
|
|
return rimraf(temporalPath)
|
2015-09-21 11:43:17 +00:00
|
|
|
|
2015-09-30 15:37:27 +00:00
|
|
|
Promise.using download(), (temporalPath) ->
|
2015-09-29 18:33:31 +00:00
|
|
|
capitano.runAsync("device register #{application.app_name}")
|
|
|
|
.then(resin.models.device.get)
|
|
|
|
.tap (device) ->
|
2015-09-29 18:52:34 +00:00
|
|
|
capitano.runAsync("os configure #{temporalPath} #{device.uuid}").then ->
|
2015-10-15 12:48:34 +00:00
|
|
|
helpers.sudo([ 'os', 'initialize', temporalPath, '--type', application.device_type ])
|
2015-09-30 15:37:27 +00:00
|
|
|
.then (device) ->
|
|
|
|
console.log('Done')
|
|
|
|
return device.uuid
|
2015-09-29 18:33:31 +00:00
|
|
|
|
2015-08-20 19:54:42 +00:00
|
|
|
.nodeify(done)
|