2017-07-27 11:07:05 +00:00
// Generated by CoffeeScript 1.12.7
2016-01-11 19:58:35 +00:00
/ *
2017-06-14 21:20:15 +00:00
Copyright 2016 - 2017 Resin . io
2016-01-11 19:58:35 +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 .
* /
2017-06-08 19:58:37 +00:00
var _ , commandOptions ;
2016-01-11 19:58:35 +00:00
2017-03-22 09:46:06 +00:00
commandOptions = require ( './command-options' ) ;
2015-09-29 17:03:14 +00:00
2017-06-08 19:58:37 +00:00
_ = require ( 'lodash' ) ;
2017-03-22 09:46:06 +00:00
exports . list = {
signature : 'devices' ,
description : 'list all devices' ,
help : 'Use this command to list all devices that belong to you.\n\nYou can filter the devices by application by using the `--application` option.\n\nExamples:\n\n $ resin devices\n $ resin devices --application MyApp\n $ resin devices --app MyApp\n $ resin devices -a MyApp' ,
options : [ commandOptions . optionalApplication ] ,
permission : 'user' ,
primary : true ,
action : function ( params , options , done ) {
2017-06-08 19:58:37 +00:00
var Promise , resin , visuals ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return resin . models . device . getAllByApplication ( options . application ) ;
}
return resin . models . device . getAll ( ) ;
} ) . tap ( function ( devices ) {
devices = _ . map ( devices , function ( device ) {
device . uuid = device . uuid . slice ( 0 , 7 ) ;
return device ;
} ) ;
2017-04-19 23:27:10 +00:00
return console . log ( visuals . table . horizontal ( devices , [ 'id' , 'uuid' , 'name' , 'device_type' , 'application_name' , 'status' , 'is_online' , 'supervisor_version' , 'os_version' , 'dashboard_url' ] ) ) ;
2017-03-22 09:46:06 +00:00
} ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . info = {
signature : 'device <uuid>' ,
description : 'list a single device' ,
help : 'Use this command to show information about a single device.\n\nExamples:\n\n $ resin device 7cf02a6' ,
permission : 'user' ,
primary : true ,
action : function ( params , options , done ) {
var resin , visuals ;
resin = require ( 'resin-sdk-preconfigured' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
return resin . models . device . get ( params . uuid ) . then ( function ( device ) {
return resin . models . device . getStatus ( device ) . then ( function ( status ) {
device . status = status ;
2017-04-19 23:27:10 +00:00
return console . log ( visuals . table . vertical ( device , [ "$" + device . name + "$" , 'id' , 'device_type' , 'status' , 'is_online' , 'ip_address' , 'application_name' , 'last_seen' , 'uuid' , 'commit' , 'supervisor_version' , 'is_web_accessible' , 'note' , 'os_version' , 'dashboard_url' ] ) ) ;
2017-03-22 09:46:06 +00:00
} ) ;
} ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . supported = {
signature : 'devices supported' ,
description : 'list all supported devices' ,
help : 'Use this command to get the list of all supported devices\n\nExamples:\n\n $ resin devices supported' ,
action : function ( params , options , done ) {
2017-06-12 10:27:15 +00:00
var resin , visuals ;
2017-03-22 09:46:06 +00:00
resin = require ( 'resin-sdk-preconfigured' ) ;
2017-06-12 10:27:15 +00:00
visuals = require ( 'resin-cli-visuals' ) ;
return resin . models . config . getDeviceTypes ( ) . then ( function ( deviceTypes ) {
return console . log ( visuals . table . horizontal ( deviceTypes , [ 'slug' , 'name' ] ) ) ;
2017-03-22 09:46:06 +00:00
} ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . register = {
signature : 'device register <application>' ,
description : 'register a device' ,
help : 'Use this command to register a device to an application.\n\nExamples:\n\n $ resin device register MyApp' ,
permission : 'user' ,
options : [
{
signature : 'uuid' ,
description : 'custom uuid' ,
parameter : 'uuid' ,
alias : 'u'
2016-09-25 23:49:06 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
action : function ( params , options , done ) {
var Promise , resin ;
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . application . get ( params . application ) . then ( function ( application ) {
return Promise [ "try" ] ( function ( ) {
return options . uuid || resin . models . device . generateUniqueKey ( ) ;
} ) . then ( function ( uuid ) {
console . info ( "Registering to " + application . app _name + ": " + uuid ) ;
return resin . models . device . register ( application . app _name , uuid ) ;
} ) ;
} ) . get ( 'uuid' ) . nodeify ( done ) ;
}
} ;
2016-09-25 23:49:06 +00:00
2017-03-22 09:46:06 +00:00
exports . remove = {
signature : 'device rm <uuid>' ,
description : 'remove a device' ,
help : 'Use this command to remove a device from resin.io.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin device rm 7cf02a6\n $ resin device rm 7cf02a6 --yes' ,
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
var patterns , resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
patterns = require ( '../utils/patterns' ) ;
return patterns . confirm ( options . yes , 'Are you sure you want to delete the device?' ) . then ( function ( ) {
return resin . models . device . remove ( params . uuid ) ;
} ) . nodeify ( done ) ;
}
} ;
2015-09-29 18:33:31 +00:00
2017-03-22 09:46:06 +00:00
exports . identify = {
signature : 'device identify <uuid>' ,
description : 'identify a device with a UUID' ,
help : 'Use this command to identify a device.\n\nIn the Raspberry Pi, the ACT led is blinked several times.\n\nExamples:\n\n $ resin device identify 23c73a1' ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . identify ( params . uuid ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . reboot = {
signature : 'device reboot <uuid>' ,
description : 'restart a device' ,
help : 'Use this command to remotely reboot a device\n\nExamples:\n\n $ resin device reboot 23c73a1' ,
options : [ commandOptions . forceUpdateLock ] ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . reboot ( params . uuid , options ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . shutdown = {
signature : 'device shutdown <uuid>' ,
description : 'shutdown a device' ,
help : 'Use this command to remotely shutdown a device\n\nExamples:\n\n $ resin device shutdown 23c73a1' ,
options : [ commandOptions . forceUpdateLock ] ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . shutdown ( params . uuid , options ) . nodeify ( done ) ;
}
} ;
2016-10-26 14:46:20 +00:00
2017-03-22 09:46:06 +00:00
exports . enableDeviceUrl = {
signature : 'device public-url enable <uuid>' ,
description : 'enable public URL for a device' ,
help : 'Use this command to enable public URL for a device\n\nExamples:\n\n $ resin device public-url enable 23c73a1' ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . enableDeviceUrl ( params . uuid ) . nodeify ( done ) ;
}
} ;
2016-03-04 13:38:11 +00:00
2017-03-22 09:46:06 +00:00
exports . disableDeviceUrl = {
signature : 'device public-url disable <uuid>' ,
description : 'disable public URL for a device' ,
help : 'Use this command to disable public URL for a device\n\nExamples:\n\n $ resin device public-url disable 23c73a1' ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . disableDeviceUrl ( params . uuid ) . nodeify ( done ) ;
}
} ;
2016-08-09 13:14:50 +00:00
2017-03-22 09:46:06 +00:00
exports . getDeviceUrl = {
signature : 'device public-url <uuid>' ,
description : 'gets the public URL of a device' ,
help : 'Use this command to get the public URL of a device\n\nExamples:\n\n $ resin device public-url 23c73a1' ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . getDeviceUrl ( params . uuid ) . then ( function ( url ) {
return console . log ( url ) ;
} ) . nodeify ( done ) ;
}
} ;
2016-08-09 13:14:50 +00:00
2017-03-22 09:46:06 +00:00
exports . hasDeviceUrl = {
signature : 'device public-url status <uuid>' ,
description : 'Returns true if public URL is enabled for a device' ,
help : 'Use this command to determine if public URL is enabled for a device\n\nExamples:\n\n $ resin device public-url status 23c73a1' ,
permission : 'user' ,
action : function ( params , options , done ) {
var resin ;
resin = require ( 'resin-sdk-preconfigured' ) ;
return resin . models . device . hasDeviceUrl ( params . uuid ) . then ( function ( hasDeviceUrl ) {
return console . log ( hasDeviceUrl ) ;
} ) . nodeify ( done ) ;
}
} ;
2016-08-09 13:14:50 +00:00
2017-03-22 09:46:06 +00:00
exports . rename = {
signature : 'device rename <uuid> [newName]' ,
description : 'rename a resin device' ,
help : 'Use this command to rename a device.\n\nIf you omit the name, you\'ll get asked for it interactively.\n\nExamples:\n\n $ resin device rename 7cf02a6\n $ resin device rename 7cf02a6 MyPi' ,
permission : 'user' ,
action : function ( params , options , done ) {
2017-06-08 19:58:37 +00:00
var Promise , form , resin ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
form = require ( 'resin-cli-form' ) ;
return Promise [ "try" ] ( function ( ) {
if ( ! _ . isEmpty ( params . newName ) ) {
return params . newName ;
}
return form . ask ( {
message : 'How do you want to name this device?' ,
type : 'input'
} ) ;
} ) . then ( _ . partial ( resin . models . device . rename , params . uuid ) ) . nodeify ( done ) ;
}
} ;
2016-08-09 13:14:50 +00:00
2017-03-22 09:46:06 +00:00
exports . move = {
signature : 'device move <uuid>' ,
description : 'move a device to another application' ,
help : 'Use this command to move a device to another application you own.\n\nIf you omit the application, you\'ll get asked for it interactively.\n\nExamples:\n\n $ resin device move 7cf02a6\n $ resin device move 7cf02a6 --application MyNewApp' ,
permission : 'user' ,
options : [ commandOptions . optionalApplication ] ,
action : function ( params , options , done ) {
2017-06-08 19:58:37 +00:00
var patterns , resin ;
2017-03-22 09:46:06 +00:00
resin = require ( 'resin-sdk-preconfigured' ) ;
patterns = require ( '../utils/patterns' ) ;
return resin . models . device . get ( params . uuid ) . then ( function ( device ) {
return options . application || patterns . selectApplication ( function ( application ) {
return _ . all ( [ application . device _type === device . device _type , device . application _name !== application . app _name ] ) ;
} ) ;
} ) . tap ( function ( application ) {
return resin . models . device . move ( params . uuid , application ) ;
} ) . then ( function ( application ) {
return console . info ( params . uuid + " was moved to " + application ) ;
} ) . nodeify ( done ) ;
}
} ;
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
exports . init = {
signature : 'device init' ,
2017-06-08 14:32:37 +00:00
description : 'initialise a device with resinOS' ,
2017-03-22 09:46:06 +00:00
help : 'Use this command to download the OS image of a certain application and write it to an SD Card.\n\nNotice this command may ask for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin device init\n $ resin device init --application MyApp' ,
options : [
2017-06-14 21:51:56 +00:00
commandOptions . optionalApplication , commandOptions . yes , commandOptions . advancedConfig , _ . assign ( { } , commandOptions . osVersion , {
2017-06-08 19:58:37 +00:00
signature : 'os-version' ,
parameter : 'os-version'
2017-06-14 21:51:56 +00:00
} ) , commandOptions . drive , {
2017-06-12 08:42:08 +00:00
signature : 'config' ,
2017-06-14 21:23:39 +00:00
description : 'path to the config JSON file, see `resin os build-config`' ,
2017-06-12 08:42:08 +00:00
parameter : 'config'
2017-06-09 09:32:28 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
permission : 'user' ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , capitanoRunAsync , helpers , patterns , resin , rimraf , tmp , tmpNameAsync ;
2017-03-22 09:46:06 +00:00
Promise = require ( 'bluebird' ) ;
2017-03-27 09:14:55 +00:00
capitanoRunAsync = Promise . promisify ( require ( 'capitano' ) . run ) ;
2017-03-22 09:46:06 +00:00
rimraf = Promise . promisify ( require ( 'rimraf' ) ) ;
2017-03-27 09:14:55 +00:00
tmp = require ( 'tmp' ) ;
tmpNameAsync = Promise . promisify ( tmp . tmpName ) ;
2017-03-22 09:46:06 +00:00
tmp . setGracefulCleanup ( ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
helpers = require ( '../utils/helpers' ) ;
patterns = require ( '../utils/patterns' ) ;
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return options . application ;
2015-10-20 13:16:56 +00:00
}
2017-03-22 09:46:06 +00:00
return patterns . selectApplication ( ) ;
} ) . then ( resin . models . application . get ) . then ( function ( application ) {
var download ;
download = function ( ) {
2017-03-27 09:14:55 +00:00
return tmpNameAsync ( ) . then ( function ( tempPath ) {
2017-06-08 19:58:37 +00:00
var osVersion ;
osVersion = options [ 'os-version' ] || 'default' ;
return capitanoRunAsync ( "os download " + application . device _type + " --output '" + tempPath + "' --version " + osVersion ) ;
2017-03-24 09:48:14 +00:00
} ) . disposer ( function ( tempPath ) {
return rimraf ( tempPath ) ;
2017-03-22 09:46:06 +00:00
} ) ;
} ;
2017-03-24 09:48:14 +00:00
return Promise . using ( download ( ) , function ( tempPath ) {
2017-03-27 09:14:55 +00:00
return capitanoRunAsync ( "device register " + application . app _name ) . then ( resin . models . device . get ) . tap ( function ( device ) {
2017-03-24 09:48:14 +00:00
var configureCommand ;
configureCommand = "os configure '" + tempPath + "' " + device . uuid ;
2017-06-12 08:42:08 +00:00
if ( options . config ) {
configureCommand += " --config '" + options . config + "'" ;
} else if ( options . advanced ) {
2017-03-24 09:48:14 +00:00
configureCommand += ' --advanced' ;
2017-03-22 09:46:06 +00:00
}
2017-03-27 09:14:55 +00:00
return capitanoRunAsync ( configureCommand ) . then ( function ( ) {
2017-03-24 09:48:14 +00:00
var osInitCommand ;
osInitCommand = "os initialize '" + tempPath + "' --type " + application . device _type ;
2017-06-08 20:55:56 +00:00
if ( options . yes ) {
osInitCommand += ' --yes' ;
}
2017-06-09 09:32:28 +00:00
if ( options . drive ) {
osInitCommand += " --drive " + options . drive ;
}
2017-03-27 09:14:55 +00:00
return capitanoRunAsync ( osInitCommand ) ;
2017-03-22 09:46:06 +00:00
} ) [ "catch" ] ( function ( error ) {
return resin . models . device . remove ( device . uuid ) [ "finally" ] ( function ( ) {
throw error ;
2015-09-29 17:36:29 +00:00
} ) ;
2015-06-04 12:55:32 +00:00
} ) ;
2015-08-20 19:54:42 +00:00
} ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( device ) {
console . log ( 'Done' ) ;
return device . uuid ;
} ) ;
} ) . nodeify ( done ) ;
}
} ;