2016-01-11 19:58:35 +00:00
/ *
Copyright 2016 Resin . io
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 .
* /
2015-02-26 15:47:56 +00:00
( function ( ) {
2015-12-07 14:32:24 +00:00
var commandOptions ;
2015-09-29 17:03:14 +00:00
2015-02-26 15:47:56 +00:00
commandOptions = require ( './command-options' ) ;
exports . list = {
signature : 'devices' ,
description : 'list all devices' ,
2015-04-27 15:20:53 +00:00
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 ] ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
2015-10-01 14:39:36 +00:00
primary : true ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2016-01-21 14:26:13 +00:00
var Promise , _ , resin , visuals ;
2015-12-07 14:32:24 +00:00
Promise = require ( 'bluebird' ) ;
2016-01-21 14:26:13 +00:00
_ = require ( 'lodash' ) ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-08-17 13:49:59 +00:00
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return resin . models . device . getAllByApplication ( options . application ) ;
2015-02-26 15:47:56 +00:00
}
2015-08-17 13:49:59 +00:00
return resin . models . device . getAll ( ) ;
} ) . tap ( function ( devices ) {
2016-01-21 14:26:13 +00:00
devices = _ . map ( devices , function ( device ) {
device . uuid = device . uuid . slice ( 0 , 7 ) ;
return device ;
} ) ;
2016-02-10 12:51:00 +00:00
return console . log ( visuals . table . horizontal ( devices , [ 'id' , 'uuid' , 'name' , 'device_type' , 'application_name' , 'status' , 'is_online' ] ) ) ;
2015-08-17 13:49:59 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . info = {
2015-07-22 22:06:53 +00:00
signature : 'device <uuid>' ,
2015-02-26 15:47:56 +00:00
description : 'list a single device' ,
2016-01-21 14:23:40 +00:00
help : 'Use this command to show information about a single device.\n\nExamples:\n\n $ resin device 7cf02a6' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
2015-10-01 14:39:36 +00:00
primary : true ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var resin , visuals ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-07-22 22:06:53 +00:00
return resin . models . device . get ( params . uuid ) . then ( function ( device ) {
2016-01-26 16:16:55 +00:00
return resin . models . device . getStatus ( device ) . then ( function ( status ) {
device . status = status ;
2016-02-12 18:34:16 +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' ] ) ) ;
2015-08-31 21:39:48 +00:00
} ) ;
2015-07-22 22:06:53 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +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.\n\nExamples:\n\n $ resin device register MyApp' ,
permission : 'user' ,
2015-10-19 18:16:47 +00:00
options : [
{
signature : 'uuid' ,
description : 'custom uuid' ,
parameter : 'uuid' ,
alias : 'u'
}
] ,
2015-09-29 18:33:31 +00:00
action : function ( params , options , done ) {
2015-12-07 14:32:24 +00:00
var Promise , resin ;
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk' ) ;
2015-09-29 18:33:31 +00:00
return resin . models . application . get ( params . application ) . then ( function ( application ) {
2015-10-19 18:16:47 +00:00
return Promise [ "try" ] ( function ( ) {
return options . uuid || resin . models . device . generateUUID ( ) ;
} ) . then ( function ( uuid ) {
2015-10-12 12:34:22 +00:00
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-02-26 15:47:56 +00:00
exports . remove = {
2015-07-22 22:06:53 +00:00
signature : 'device rm <uuid>' ,
2015-02-26 15:47:56 +00:00
description : 'remove a device' ,
2016-01-21 14:23:40 +00:00
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' ,
2015-02-26 15:47:56 +00:00
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var patterns , resin ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
patterns = require ( '../utils/patterns' ) ;
2015-08-20 19:54:42 +00:00
return patterns . confirm ( options . yes , 'Are you sure you want to delete the device?' ) . then ( function ( ) {
2015-08-17 13:49:59 +00:00
return resin . models . device . remove ( params . uuid ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . identify = {
signature : 'device identify <uuid>' ,
description : 'identify a device with a UUID' ,
2016-01-21 14:23:40 +00:00
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' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-12-07 14:32:24 +00:00
var resin ;
resin = require ( 'resin-sdk' ) ;
2015-07-22 22:06:53 +00:00
return resin . models . device . identify ( params . uuid ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . rename = {
2015-07-22 22:06:53 +00:00
signature : 'device rename <uuid> [newName]' ,
2015-02-26 15:47:56 +00:00
description : 'rename a resin device' ,
2016-01-21 14:23:40 +00:00
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' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var Promise , _ , form , resin ;
2015-12-07 14:32:24 +00:00
Promise = require ( 'bluebird' ) ;
_ = require ( 'lodash' ) ;
resin = require ( 'resin-sdk' ) ;
form = require ( 'resin-cli-form' ) ;
2015-08-17 13:49:59 +00:00
return Promise [ "try" ] ( function ( ) {
if ( ! _ . isEmpty ( params . newName ) ) {
return params . newName ;
2015-02-26 15:47:56 +00:00
}
2015-08-17 13:49:59 +00:00
return form . ask ( {
message : 'How do you want to name this device?' ,
type : 'input'
} ) ;
2016-02-12 18:34:16 +00:00
} ) . then ( _ . partial ( resin . models . device . rename , params . uuid ) ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
2015-11-11 19:00:02 +00:00
exports . move = {
signature : 'device move <uuid>' ,
description : 'move a device to another application' ,
2016-01-21 14:23:40 +00:00
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' ,
2015-11-11 19:00:02 +00:00
permission : 'user' ,
options : [ commandOptions . optionalApplication ] ,
action : function ( params , options , done ) {
2015-12-07 14:32:24 +00:00
var _ , patterns , resin ;
resin = require ( 'resin-sdk' ) ;
_ = require ( 'lodash' ) ;
patterns = require ( '../utils/patterns' ) ;
2015-11-11 19:00:02 +00:00
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
exports . init = {
2015-08-20 19:54:42 +00:00
signature : 'device init' ,
2015-02-26 15:47:56 +00:00
description : 'initialise a device with resin os' ,
2015-08-20 19:54:42 +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' ,
2015-10-20 13:16:56 +00:00
options : [
commandOptions . optionalApplication , commandOptions . yes , {
signature : 'advanced' ,
description : 'enable advanced configuration' ,
boolean : true ,
alias : 'v'
}
] ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-12-07 14:32:24 +00:00
var Promise , capitano , helpers , patterns , resin , rimraf , tmp ;
Promise = require ( 'bluebird' ) ;
capitano = Promise . promisifyAll ( require ( 'capitano' ) ) ;
rimraf = Promise . promisify ( require ( 'rimraf' ) ) ;
tmp = Promise . promisifyAll ( require ( 'tmp' ) ) ;
tmp . setGracefulCleanup ( ) ;
resin = require ( 'resin-sdk' ) ;
helpers = require ( '../utils/helpers' ) ;
patterns = require ( '../utils/patterns' ) ;
2015-08-20 19:54:42 +00:00
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return 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 ( function ( application ) {
2015-09-21 11:43:17 +00:00
var download ;
download = function ( ) {
return tmp . tmpNameAsync ( ) . then ( function ( temporalPath ) {
2015-09-30 15:37:27 +00:00
return capitano . runAsync ( "os download " + application . device _type + " --output " + temporalPath ) ;
2015-10-12 22:04:52 +00:00
} ) . disposer ( function ( temporalPath ) {
return rimraf ( temporalPath ) ;
} ) ;
2015-09-21 11:43:17 +00:00
} ;
2015-09-30 15:37:27 +00:00
return Promise . using ( download ( ) , function ( temporalPath ) {
2015-09-29 18:33:31 +00:00
return capitano . runAsync ( "device register " + application . app _name ) . then ( resin . models . device . get ) . tap ( function ( device ) {
2015-10-20 13:16:56 +00:00
var configure ;
configure = "os configure " + temporalPath + " " + device . uuid ;
if ( options . advanced ) {
configure += ' --advanced' ;
}
return capitano . runAsync ( configure ) . then ( function ( ) {
2015-12-02 15:15:20 +00:00
var message ;
2016-01-25 13:00:04 +00:00
message = 'Initializing a device requires administrative permissions\ngiven that we need to access raw devices directly.\n' ;
2015-12-02 15:15:20 +00:00
return helpers . sudo ( [ 'os' , 'initialize' , temporalPath , '--type' , application . device _type ] , message ) ;
2016-01-14 13:14:45 +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-09-30 15:37:27 +00:00
} ) . then ( function ( device ) {
console . log ( 'Done' ) ;
return device . uuid ;
2015-08-20 19:54:42 +00:00
} ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
} ) . call ( this ) ;