2015-02-26 15:47:56 +00:00
( function ( ) {
2015-06-04 12:55:32 +00:00
var _ , async , capitano , commandOptions , fse , image , inject , manager , path , pine , registerDevice , resin , tmp , vcs , visuals ;
2015-05-18 13:34:40 +00:00
fse = require ( 'fs-extra' ) ;
2015-05-05 20:32:44 +00:00
capitano = require ( 'capitano' ) ;
2015-02-26 15:47:56 +00:00
_ = require ( 'lodash-contrib' ) ;
path = require ( 'path' ) ;
async = require ( 'async' ) ;
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-03-12 16:03:59 +00:00
vcs = require ( 'resin-vcs' ) ;
2015-05-18 13:34:40 +00:00
manager = require ( 'resin-image-manager' ) ;
image = require ( 'resin-image' ) ;
inject = require ( 'resin-config-inject' ) ;
2015-06-04 12:55:32 +00:00
registerDevice = require ( 'resin-register-device' ) ;
pine = require ( 'resin-pine' ) ;
2015-04-20 17:03:19 +00:00
tmp = require ( 'tmp' ) ;
tmp . setGracefulCleanup ( ) ;
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' ,
action : function ( params , options , done ) {
2015-04-27 15:20:53 +00:00
var getFunction ;
if ( options . application != null ) {
getFunction = _ . partial ( resin . models . device . getAllByApplication , options . application ) ;
} else {
getFunction = resin . models . device . getAll ;
}
return getFunction ( function ( error , devices ) {
2015-02-26 15:47:56 +00:00
if ( error != null ) {
return done ( error ) ;
}
console . log ( visuals . widgets . table . horizontal ( devices , [ 'id' , 'name' , 'device_type' , 'is_online' , 'application_name' , 'status' , 'last_seen' ] ) ) ;
2015-05-14 14:43:41 +00:00
return done ( null , devices ) ;
2015-02-26 15:47:56 +00:00
} ) ;
}
} ;
exports . info = {
2015-03-23 12:17:55 +00:00
signature : 'device <name>' ,
2015-02-26 15:47:56 +00:00
description : 'list a single device' ,
2015-03-23 12:17:55 +00:00
help : 'Use this command to show information about a single device.\n\nExamples:\n\n $ resin device MyDevice' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-03-23 12:17:55 +00:00
return resin . models . device . get ( params . name , function ( error , device ) {
2015-02-26 15:47:56 +00:00
if ( error != null ) {
return done ( error ) ;
}
2015-07-09 13:56:17 +00:00
if ( device . last _seen == null ) {
device . last _seen = 'Not seen' ;
}
2015-02-26 15:47:56 +00:00
console . log ( visuals . widgets . table . vertical ( device , [ 'id' , 'name' , 'device_type' , 'is_online' , 'ip_address' , 'application_name' , 'status' , 'last_seen' , 'uuid' , 'commit' , 'supervisor_version' , 'is_web_accessible' , 'note' ] ) ) ;
return done ( ) ;
} ) ;
}
} ;
exports . remove = {
2015-03-23 12:17:55 +00:00
signature : 'device rm <name>' ,
2015-02-26 15:47:56 +00:00
description : 'remove a device' ,
2015-03-23 12:17:55 +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 MyDevice\n $ resin device rm MyDevice --yes' ,
2015-02-26 15:47:56 +00:00
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
return visuals . patterns . remove ( 'device' , options . yes , function ( callback ) {
2015-03-23 12:17:55 +00:00
return resin . models . device . remove ( params . name , callback ) ;
2015-02-26 15:47:56 +00:00
} , done ) ;
}
} ;
exports . identify = {
signature : 'device identify <uuid>' ,
description : 'identify a device with a UUID' ,
2015-03-03 14:14:16 +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 23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a39849ac0f00db828' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
return resin . models . device . identify ( params . uuid , done ) ;
}
} ;
exports . rename = {
2015-03-23 12:17:55 +00:00
signature : 'device rename <name> [newName]' ,
2015-02-26 15:47:56 +00:00
description : 'rename a resin device' ,
2015-03-23 12:17:55 +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 MyDevice MyPi\n $ resin device rename MyDevice' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
return async . waterfall ( [
function ( callback ) {
2015-03-23 12:17:55 +00:00
if ( ! _ . isEmpty ( params . newName ) ) {
return callback ( null , params . newName ) ;
2015-02-26 15:47:56 +00:00
}
2015-06-11 16:46:56 +00:00
return visuals . form . ask ( {
label : 'How do you want to name this device?' ,
name : 'device' ,
type : 'text'
} , callback ) ;
2015-03-23 12:17:55 +00:00
} , function ( newName , callback ) {
return resin . models . device . rename ( params . name , newName , callback ) ;
2015-02-26 15:47:56 +00:00
}
] , done ) ;
}
} ;
exports . supported = {
signature : 'devices supported' ,
description : 'list all supported devices' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to get the list of all supported devices\n\nExamples:\n\n $ resin devices supported' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
return resin . models . device . getSupportedDeviceTypes ( function ( error , devices ) {
if ( error != null ) {
return done ( error ) ;
}
_ . each ( devices , _ . unary ( console . log ) ) ;
return done ( ) ;
} ) ;
}
} ;
2015-05-05 05:08:11 +00:00
exports . await = {
signature : 'device await <name>' ,
description : 'await for a device to become online' ,
help : 'Use this command to await for a device to become online.\n\nThe process will exit when the device becomes online.\n\nNotice that there is no time limit for this command, so it might run forever.\n\nYou can configure the poll interval with the --interval option (defaults to 3000ms).\n\nExamples:\n\n $ resin device await MyDevice\n $ resin device await MyDevice --interval 1000' ,
options : [
{
signature : 'interval' ,
parameter : 'interval' ,
description : 'poll interval' ,
alias : 'i'
}
] ,
permission : 'user' ,
action : function ( params , options , done ) {
var poll ;
if ( options . interval == null ) {
options . interval = 3000 ;
}
poll = function ( ) {
return resin . models . device . isOnline ( params . name , function ( error , isOnline ) {
if ( error != null ) {
return done ( error ) ;
}
if ( isOnline ) {
console . info ( "Device became online: " + params . name ) ;
return done ( ) ;
} else {
console . info ( "Polling device network status: " + params . name ) ;
return setTimeout ( poll , options . interval ) ;
}
} ) ;
} ;
return poll ( ) ;
}
} ;
2015-02-26 15:47:56 +00:00
exports . init = {
signature : 'device init [device]' ,
description : 'initialise a device with resin os' ,
2015-05-18 13:34:40 +00:00
help : 'Use this command to download the OS image of a certain application and write it to an SD Card.\n\nNote that this command requires admin privileges.\n\nIf `device` is omitted, you will be prompted to select a device interactively.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nYou can quiet the progress bar and other logging information by passing the `--quiet` boolean option.\n\nYou need to configure the network type and other settings:\n\nEthernet:\n You can setup the device OS to use ethernet by setting the `--network` option to "ethernet".\n\nWifi:\n You can setup the device OS to use wifi by setting the `--network` option to "wifi".\n If you set "network" to "wifi", you will need to specify the `--ssid` and `--key` option as well.\n\nYou can omit network related options to be asked about them interactively.\n\nExamples:\n\n $ resin device init\n $ resin device init --application MyApp\n $ resin device init --application MyApp --network ethernet\n $ resin device init /dev/disk2 --application MyApp --network wifi --ssid MyNetwork --key secret' ,
2015-03-12 16:03:59 +00:00
options : [ commandOptions . optionalApplication , commandOptions . network , commandOptions . wifiSsid , commandOptions . wifiKey ] ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
2015-05-18 13:34:40 +00:00
root : true ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2015-05-18 13:34:40 +00:00
var networkOptions ;
networkOptions = {
network : options . network ,
wifiSsid : options . ssid ,
wifiKey : options . key
} ;
2015-02-26 15:47:56 +00:00
return async . waterfall ( [
function ( callback ) {
2015-03-12 16:03:59 +00:00
if ( options . application != null ) {
return callback ( null , options . application ) ;
}
2015-07-10 17:04:17 +00:00
return vcs . getApplicationName ( process . cwd ( ) ) . nodeify ( callback ) ;
2015-04-20 13:13:15 +00:00
} , function ( applicationName , callback ) {
2015-05-18 13:34:40 +00:00
options . application = applicationName ;
2015-06-08 16:31:17 +00:00
return resin . models . application . has ( options . application , callback ) ;
} , function ( hasApplication , callback ) {
if ( ! hasApplication ) {
return callback ( new Error ( "Invalid application: " + options . application ) ) ;
}
2015-02-26 15:47:56 +00:00
if ( params . device != null ) {
return callback ( null , params . device ) ;
}
return visuals . patterns . selectDrive ( callback ) ;
} , function ( device , callback ) {
2015-03-23 22:32:18 +00:00
var message ;
2015-02-26 15:47:56 +00:00
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?" ;
return visuals . patterns . confirm ( options . yes , message , callback ) ;
2015-02-26 15:47:56 +00:00
} , function ( confirmed , callback ) {
if ( ! confirmed ) {
return done ( ) ;
}
2015-05-18 13:34:40 +00:00
if ( networkOptions . network != null ) {
return callback ( ) ;
}
return visuals . patterns . selectNetworkParameters ( function ( error , parameters ) {
2015-04-20 17:03:19 +00:00
if ( error != null ) {
return callback ( error ) ;
}
2015-05-18 13:34:40 +00:00
_ . extend ( networkOptions , parameters ) ;
return callback ( ) ;
2015-04-20 17:03:19 +00:00
} ) ;
2015-05-18 13:34:40 +00:00
} , function ( callback ) {
console . info ( "Checking application: " + options . application ) ;
return resin . models . application . get ( options . application , callback ) ;
} , function ( application , callback ) {
2015-06-04 12:55:32 +00:00
return async . parallel ( {
manifest : function ( callback ) {
console . info ( 'Getting device manifest for the application' ) ;
return resin . models . device . getManifestBySlug ( application . device _type , callback ) ;
} ,
config : function ( callback ) {
console . info ( 'Fetching application configuration' ) ;
return resin . models . application . getConfiguration ( options . application , networkOptions , callback ) ;
}
} , callback ) ;
} , function ( results , callback ) {
console . info ( 'Associating the device' ) ;
return registerDevice . register ( pine , results . config , function ( error , device ) {
if ( error != null ) {
return callback ( error ) ;
}
results . config . deviceId = device . id ;
results . config . uuid = device . uuid ;
params . uuid = results . config . uuid ;
return callback ( null , results ) ;
} ) ;
} , function ( results , callback ) {
2015-05-18 13:34:40 +00:00
var bar , spinner ;
2015-06-04 12:55:32 +00:00
console . info ( 'Configuring device operating system image' ) ;
if ( process . env . DEBUG ) {
console . log ( results . config ) ;
}
2015-05-18 13:34:40 +00:00
bar = new visuals . widgets . Progress ( 'Downloading Device OS' ) ;
spinner = new visuals . widgets . Spinner ( 'Downloading Device OS (size unknown)' ) ;
2015-06-04 12:55:32 +00:00
return manager . configure ( results . manifest , results . config , function ( error , imagePath , removeCallback ) {
spinner . stop ( ) ;
return callback ( error , imagePath , removeCallback ) ;
} , function ( state ) {
if ( state != null ) {
return bar . update ( state ) ;
} else {
return spinner . start ( ) ;
2015-04-20 17:03:19 +00:00
}
2015-05-18 13:34:40 +00:00
} ) ;
} , function ( configuredImagePath , removeCallback , callback ) {
var bar ;
console . info ( 'Attempting to write operating system image to drive' ) ;
bar = new visuals . widgets . Progress ( 'Writing Device OS' ) ;
return image . write ( {
device : params . device ,
image : configuredImagePath ,
progress : _ . bind ( bar . update , bar )
} , function ( error ) {
if ( error != null ) {
return callback ( error ) ;
}
return callback ( null , configuredImagePath , removeCallback ) ;
2015-04-20 17:03:19 +00:00
} ) ;
2015-05-18 13:34:40 +00:00
} , function ( temporalImagePath , removeCallback , callback ) {
console . info ( 'Image written successfully' ) ;
return removeCallback ( callback ) ;
2015-06-04 12:55:32 +00:00
} , function ( callback ) {
return resin . models . device . getByUUID ( params . uuid , callback ) ;
} , function ( device , callback ) {
console . info ( "Device created: " + device . name ) ;
return callback ( null , device . name ) ;
2015-02-26 15:47:56 +00:00
}
] , done ) ;
}
} ;
} ) . call ( this ) ;