2014-12-24 16:49:49 +00:00
|
|
|
_ = require('lodash-contrib')
|
2015-01-29 16:04:48 +00:00
|
|
|
path = require('path')
|
2014-12-19 14:05:54 +00:00
|
|
|
async = require('async')
|
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-03-12 16:03:59 +00:00
|
|
|
vcs = require('resin-vcs')
|
2015-04-20 17:03:19 +00:00
|
|
|
tmp = require('tmp')
|
|
|
|
|
|
|
|
# Cleanup the temporary files even when an uncaught exception occurs
|
|
|
|
tmp.setGracefulCleanup()
|
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
commandOptions = require('./command-options')
|
2015-02-04 18:13:28 +00:00
|
|
|
osAction = require('./os')
|
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'
|
|
|
|
action: (params, options, done) ->
|
2015-04-27 15:20:53 +00:00
|
|
|
|
|
|
|
if options.application?
|
|
|
|
getFunction = _.partial(resin.models.device.getAllByApplication, options.application)
|
|
|
|
else
|
|
|
|
getFunction = resin.models.device.getAll
|
|
|
|
|
|
|
|
getFunction (error, devices) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
return done(error) if error?
|
2015-01-21 13:50:19 +00:00
|
|
|
console.log visuals.widgets.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
|
|
|
]
|
|
|
|
|
|
|
|
return done()
|
|
|
|
|
|
|
|
exports.info =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'device <name>'
|
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-03-23 12:17:55 +00:00
|
|
|
$ resin device MyDevice
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.device.get params.name, (error, device) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
return done(error) if error?
|
2015-01-21 13:50:19 +00:00
|
|
|
console.log visuals.widgets.table.vertical device, [
|
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'
|
|
|
|
'ip_address'
|
|
|
|
'application_name'
|
|
|
|
'status'
|
|
|
|
'last_seen'
|
|
|
|
'uuid'
|
|
|
|
'commit'
|
|
|
|
'supervisor_version'
|
|
|
|
'is_web_accessible'
|
|
|
|
'note'
|
2015-01-15 17:10:14 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
return done()
|
|
|
|
|
|
|
|
exports.remove =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'device rm <name>'
|
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-03-23 12:17:55 +00:00
|
|
|
$ resin device rm MyDevice
|
|
|
|
$ resin device rm MyDevice --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-01-21 13:50:19 +00:00
|
|
|
visuals.patterns.remove 'device', options.yes, (callback) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
resin.models.device.remove(params.name, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
, done
|
|
|
|
|
|
|
|
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-01-15 17:10:14 +00:00
|
|
|
resin.models.device.identify(params.uuid, done)
|
|
|
|
|
|
|
|
exports.rename =
|
2015-03-23 12:17:55 +00:00
|
|
|
signature: 'device rename <name> [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-03-23 12:17:55 +00:00
|
|
|
$ resin device rename MyDevice MyPi
|
|
|
|
$ resin device rename MyDevice
|
2015-01-15 17:10:14 +00:00
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
2015-03-23 12:17:55 +00:00
|
|
|
if not _.isEmpty(params.newName)
|
|
|
|
return callback(null, params.newName)
|
2015-03-09 13:14:39 +00:00
|
|
|
visuals.widgets.ask('How do you want to name this device?', null, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
2015-03-23 12:17:55 +00:00
|
|
|
(newName, callback) ->
|
|
|
|
resin.models.device.rename(params.name, newName, callback)
|
2015-01-15 17:10:14 +00:00
|
|
|
|
|
|
|
], done
|
|
|
|
|
|
|
|
exports.supported =
|
|
|
|
signature: 'devices supported'
|
|
|
|
description: 'list all supported devices'
|
|
|
|
help: '''
|
|
|
|
Use this command to get the list of all supported devices
|
|
|
|
|
|
|
|
Examples:
|
2015-03-03 14:14:16 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
$ resin devices supported
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
2015-02-11 13:53:48 +00:00
|
|
|
action: (params, options, done) ->
|
|
|
|
resin.models.device.getSupportedDeviceTypes (error, devices) ->
|
|
|
|
return done(error) if error?
|
|
|
|
_.each(devices, _.unary(console.log))
|
|
|
|
done()
|
2015-02-04 18:13:28 +00:00
|
|
|
|
|
|
|
exports.init =
|
|
|
|
signature: 'device init [device]'
|
|
|
|
description: 'initialise a device with resin os'
|
|
|
|
help: '''
|
2015-02-05 13:56:22 +00:00
|
|
|
Use this command to download the OS image of a certain application and write it to an SD Card.
|
2015-02-04 18:13:28 +00:00
|
|
|
|
2015-02-05 13:56:22 +00:00
|
|
|
Note that this command requires admin privileges.
|
|
|
|
|
|
|
|
If `device` is omitted, you will be prompted to select a device interactively.
|
|
|
|
|
|
|
|
Notice this command asks for confirmation interactively.
|
|
|
|
You can avoid this by passing the `--yes` boolean option.
|
|
|
|
|
|
|
|
You can quiet the progress bar by passing the `--quiet` boolean option.
|
|
|
|
|
|
|
|
You may have to unmount the device before attempting this operation.
|
|
|
|
|
|
|
|
You need to configure the network type and other settings:
|
|
|
|
|
|
|
|
Ethernet:
|
2015-03-03 14:14:16 +00:00
|
|
|
You can setup the device OS to use ethernet by setting the `--network` option to "ethernet".
|
2015-02-05 13:56:22 +00:00
|
|
|
|
|
|
|
Wifi:
|
2015-03-03 14:14:16 +00:00
|
|
|
You can setup the device OS to use wifi by setting the `--network` option to "wifi".
|
|
|
|
If you set "network" to "wifi", you will need to specify the `--ssid` and `--key` option as well.
|
2015-02-05 13:56:22 +00:00
|
|
|
|
2015-03-11 13:05:20 +00:00
|
|
|
You can omit network related options to be asked about them interactively.
|
|
|
|
|
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
|
|
|
|
$ resin device init --application MyApp --network ethernet
|
|
|
|
$ resin device init /dev/disk2 --application MyApp --network wifi --ssid MyNetwork --key secret
|
2015-02-04 18:13:28 +00:00
|
|
|
'''
|
|
|
|
options: [
|
2015-03-12 16:03:59 +00:00
|
|
|
commandOptions.optionalApplication
|
2015-02-04 18:13:28 +00:00
|
|
|
commandOptions.network
|
|
|
|
commandOptions.wifiSsid
|
|
|
|
commandOptions.wifiKey
|
|
|
|
]
|
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
(callback) ->
|
2015-03-12 16:03:59 +00:00
|
|
|
return callback(null, options.application) if options.application?
|
2015-04-20 15:41:26 +00:00
|
|
|
vcs.getApplicationName(process.cwd(), callback)
|
2015-04-20 13:13:15 +00:00
|
|
|
|
|
|
|
(applicationName, callback) ->
|
|
|
|
params.name = applicationName
|
2015-02-04 18:13:28 +00:00
|
|
|
return callback(null, params.device) if params.device?
|
|
|
|
visuals.patterns.selectDrive(callback)
|
|
|
|
|
|
|
|
(device, callback) ->
|
|
|
|
params.device = device
|
2015-03-23 22:32:18 +00:00
|
|
|
message = "This will completely erase #{params.device}. Are you sure you want to continue?"
|
|
|
|
visuals.patterns.confirm(options.yes, message, callback)
|
2015-02-04 18:13:28 +00:00
|
|
|
|
|
|
|
(confirmed, callback) ->
|
|
|
|
return done() if not confirmed
|
|
|
|
options.yes = confirmed
|
|
|
|
|
2015-04-20 17:03:19 +00:00
|
|
|
tmp.file
|
|
|
|
prefix: 'resin-image-'
|
|
|
|
postfix: '.img'
|
|
|
|
, callback
|
|
|
|
|
|
|
|
(tmpPath, tmpFd, cleanupCallback, callback) ->
|
|
|
|
options.output = tmpPath
|
|
|
|
osAction.download.action params, options, (error, outputFile) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(null, outputFile, cleanupCallback)
|
|
|
|
|
|
|
|
(outputFile, cleanupCallback, callback) ->
|
2015-02-04 18:13:28 +00:00
|
|
|
params.image = outputFile
|
2015-04-20 17:03:19 +00:00
|
|
|
osAction.install.action params, options, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
cleanupCallback()
|
|
|
|
return callback()
|
2015-02-04 18:13:28 +00:00
|
|
|
|
|
|
|
], done)
|