2015-02-26 11:47:56 -04:00
( function ( ) {
2015-09-29 13:03:14 -04:00
var Promise , _ , capitano , commandOptions , events , form , fs , helpers , init , patterns , resin , rimraf , stepHandler , tmp , umount , vcs , visuals ;
2015-08-14 14:11:49 -04:00
Promise = require ( 'bluebird' ) ;
2015-05-05 17:32:44 -03:00
2015-09-29 13:03:14 -04:00
capitano = Promise . promisifyAll ( require ( 'capitano' ) ) ;
2015-02-26 11:47:56 -04:00
2015-08-12 08:17:46 -04:00
_ = require ( 'lodash' ) ;
2015-02-26 11:47:56 -04:00
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-03-12 12:03:59 -04:00
vcs = require ( 'resin-vcs' ) ;
2015-08-20 15:54:42 -04:00
form = require ( 'resin-cli-form' ) ;
2015-05-18 09:34:40 -04:00
2015-08-20 15:54:42 -04:00
events = require ( 'resin-cli-events' ) ;
2015-06-04 08:55:32 -04:00
2015-08-20 15:54:42 -04:00
init = require ( 'resin-device-init' ) ;
2015-06-04 08:55:32 -04:00
2015-08-20 15:54:42 -04:00
fs = Promise . promisifyAll ( require ( 'fs' ) ) ;
2015-07-23 01:06:53 +03:00
2015-08-20 15:54:42 -04:00
rimraf = Promise . promisify ( require ( 'rimraf' ) ) ;
2015-07-27 15:08:55 +03:00
2015-08-20 15:54:42 -04:00
umount = Promise . promisifyAll ( require ( 'umount' ) ) ;
2015-09-01 00:39:48 +03:00
2015-08-20 15:54:42 -04:00
patterns = require ( '../utils/patterns' ) ;
2015-05-07 12:40:12 -03:00
2015-08-17 09:49:59 -04:00
helpers = require ( '../utils/helpers' ) ;
2015-09-29 13:03:14 -04:00
tmp = Promise . promisifyAll ( require ( 'tmp' ) ) ;
tmp . setGracefulCleanup ( ) ;
2015-02-26 11:47:56 -04:00
commandOptions = require ( './command-options' ) ;
exports . list = {
signature : 'devices' ,
description : 'list all devices' ,
2015-04-27 11:20:53 -04: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 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-17 09:49:59 -04:00
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return resin . models . device . getAllByApplication ( options . application ) ;
2015-02-26 11:47:56 -04:00
}
2015-08-17 09:49:59 -04:00
return resin . models . device . getAll ( ) ;
} ) . tap ( function ( devices ) {
return console . log ( visuals . table . horizontal ( devices , [ 'id' , 'name' , 'device_type' , 'is_online' , 'application_name' , 'status' , 'last_seen' ] ) ) ;
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . info = {
2015-07-23 01:06:53 +03:00
signature : 'device <uuid>' ,
2015-02-26 11:47:56 -04:00
description : 'list a single device' ,
2015-07-23 01:06:53 +03:00
help : 'Use this command to show information about a single device.\n\nExamples:\n\n $ resin device 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-23 01:06:53 +03:00
return resin . models . device . get ( params . uuid ) . then ( function ( device ) {
2015-07-09 09:56:17 -04:00
if ( device . last _seen == null ) {
device . last _seen = 'Not seen' ;
}
2015-09-01 00:39:48 +03:00
console . log ( visuals . table . vertical ( device , [ "$" + device . name + "$" , 'id' , 'device_type' , 'is_online' , 'ip_address' , 'application_name' , 'status' , 'last_seen' , 'uuid' , 'commit' , 'supervisor_version' , 'is_web_accessible' , 'note' ] ) ) ;
return events . send ( 'device.open' , {
device : device . uuid
} ) ;
2015-07-23 01:06:53 +03:00
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
2015-09-29 14:33:31 -04: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' ,
action : function ( params , options , done ) {
return resin . models . application . get ( params . application ) . then ( function ( application ) {
var uuid ;
uuid = resin . models . device . generateUUID ( ) ;
console . info ( "Registering to " + application . app _name + ": " + uuid ) ;
return resin . models . device . register ( application . app _name , uuid ) ;
} ) . get ( 'uuid' ) . nodeify ( done ) ;
}
} ;
2015-02-26 11:47:56 -04:00
exports . remove = {
2015-07-23 01:06:53 +03:00
signature : 'device rm <uuid>' ,
2015-02-26 11:47:56 -04:00
description : 'remove a device' ,
2015-07-23 01:06:53 +03: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 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9\n $ resin device rm 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 --yes' ,
2015-02-26 11:47:56 -04:00
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-20 15:54:42 -04:00
return patterns . confirm ( options . yes , 'Are you sure you want to delete the device?' ) . then ( function ( ) {
2015-08-17 09:49:59 -04:00
return resin . models . device . remove ( params . uuid ) ;
2015-09-01 00:39:48 +03:00
} ) . tap ( function ( ) {
return events . send ( 'device.delete' , {
device : params . uuid
} ) ;
2015-08-17 09:49:59 -04:00
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . identify = {
signature : 'device identify <uuid>' ,
description : 'identify a device with a UUID' ,
2015-03-03 10:14:16 -04: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 23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a39849ac0f00db828' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-23 01:06:53 +03:00
return resin . models . device . identify ( params . uuid ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . rename = {
2015-07-23 01:06:53 +03:00
signature : 'device rename <uuid> [newName]' ,
2015-02-26 11:47:56 -04:00
description : 'rename a resin device' ,
2015-07-23 01:06:53 +03: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 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 MyPi\n $ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-17 09:49:59 -04:00
return Promise [ "try" ] ( function ( ) {
if ( ! _ . isEmpty ( params . newName ) ) {
return params . newName ;
2015-02-26 11:47:56 -04:00
}
2015-08-17 09:49:59 -04:00
return form . ask ( {
message : 'How do you want to name this device?' ,
type : 'input'
} ) ;
2015-09-01 00:39:48 +03:00
} ) . then ( _ . partial ( resin . models . device . rename , params . uuid ) ) . tap ( function ( ) {
return events . send ( 'device.rename' , {
device : params . uuid
} ) ;
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
2015-08-20 15:54:42 -04:00
stepHandler = function ( step ) {
var bar ;
step . on ( 'stdout' , _ . bind ( process . stdout . write , process . stdout ) ) ;
step . on ( 'stderr' , _ . bind ( process . stderr . write , process . stderr ) ) ;
step . on ( 'state' , function ( state ) {
if ( state . operation . command === 'burn' ) {
return ;
}
return console . log ( helpers . stateToString ( state ) ) ;
} ) ;
bar = new visuals . Progress ( 'Writing Device OS' ) ;
step . on ( 'burn' , _ . bind ( bar . update , bar ) ) ;
return new Promise ( function ( resolve , reject ) {
step . on ( 'error' , reject ) ;
return step . on ( 'end' , resolve ) ;
} ) ;
} ;
2015-02-26 11:47:56 -04:00
exports . init = {
2015-08-20 15:54:42 -04:00
signature : 'device init' ,
2015-02-26 11:47:56 -04:00
description : 'initialise a device with resin os' ,
2015-08-20 15:54:42 -04: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 : [ commandOptions . optionalApplication , commandOptions . yes ] ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
2015-05-18 09:34:40 -04:00
root : true ,
2015-02-26 11:47:56 -04:00
action : function ( params , options , done ) {
2015-08-20 15:54:42 -04:00
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return options . application ;
}
return vcs . getApplicationName ( process . cwd ( ) ) ;
} ) . then ( resin . models . application . get ) . then ( function ( application ) {
2015-09-29 13:36:29 -04:00
return tmp . tmpNameAsync ( ) . then ( function ( temporalPath ) {
return capitano . runAsync ( "os download --output " + temporalPath ) ;
} ) . then ( function ( temporalPath ) {
2015-09-29 14:33:31 -04:00
return capitano . runAsync ( "device register " + application . app _name ) . then ( resin . models . device . get ) . tap ( function ( device ) {
2015-09-29 13:36:29 -04:00
console . log ( 'Configuring operating system' ) ;
return capitano . runAsync ( "os configure " + temporalPath + " " + uuid ) . then ( function ( ) {
console . log ( 'Initializing device' ) ;
return resin . models . device . getManifestBySlug ( application . device _type ) . then ( function ( manifest ) {
var ref ;
return form . run ( ( ref = manifest . initialization ) != null ? ref . options : void 0 ) ;
} ) . tap ( function ( answers ) {
var message ;
if ( answers . drive != null ) {
message = "This will erase " + answers . drive + ". Are you sure?" ;
return patterns . confirm ( options . yes , message ) [ "return" ] ( answers . drive ) . then ( umount . umountAsync ) ;
}
} ) . then ( function ( answers ) {
return init . initialize ( temporalPath , device . uuid , answers ) . then ( stepHandler ) [ "return" ] ( answers ) ;
} ) . tap ( function ( answers ) {
2015-08-20 15:54:42 -04:00
if ( answers . drive == null ) {
return ;
}
return umount . umountAsync ( answers . drive ) . tap ( function ( ) {
return console . log ( "You can safely remove " + answers . drive + " now" ) ;
} ) ;
} ) ;
2015-09-29 13:36:29 -04:00
} ) ;
} ) . then ( function ( device ) {
console . log ( 'Done' ) ;
return device . uuid ;
} ) [ "finally" ] ( function ( ) {
return fs . statAsync ( temporalPath ) . then ( function ( stat ) {
if ( stat . isDirectory ( ) ) {
return rimraf ( temporalPath ) ;
}
return fs . unlinkAsync ( temporalPath ) ;
} ) [ "catch" ] ( function ( error ) {
if ( error . code === 'ENOENT' ) {
return ;
}
throw error ;
2015-08-20 15:54:42 -04:00
} ) ;
2015-06-04 08:55:32 -04:00
} ) ;
2015-08-20 15:54:42 -04:00
} ) ;
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
} ) . call ( this ) ;