2015-02-26 15:47:56 +00:00
( function ( ) {
2015-05-05 20:32:44 +00:00
var _ , async , capitano , commandOptions , elevate , mkdirp , npm , os , packageJSON , path , resin , umount , visuals ;
capitano = require ( 'capitano' ) ;
2015-02-26 15:47:56 +00:00
_ = require ( 'lodash-contrib' ) ;
os = require ( 'os' ) ;
async = require ( 'async' ) ;
path = require ( 'path' ) ;
mkdirp = require ( 'mkdirp' ) ;
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-05-02 14:34:11 +00:00
umount = require ( 'umount' ) . umount ;
2015-02-26 15:47:56 +00:00
commandOptions = require ( './command-options' ) ;
2015-03-11 12:49:26 +00:00
npm = require ( '../npm' ) ;
packageJSON = require ( '../../package.json' ) ;
2015-04-06 20:43:31 +00:00
elevate = require ( '../elevate' ) ;
2015-02-26 15:47:56 +00:00
exports . download = {
2015-04-20 13:06:40 +00:00
signature : 'os download <name>' ,
2015-02-26 15:47:56 +00:00
description : 'download device OS' ,
2015-04-20 13:06:40 +00:00
help : 'Use this command to download the device OS configured to a specific network.\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\nAlternatively, you can omit all kind of network configuration options to configure interactively.\n\nYou have to specify an output location with the `--output` option.\n\nExamples:\n\n $ resin os download MyApp --output ~/MyResinOS.zip\n $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip\n $ resin os download MyApp --network wifi --ssid MyNetwork --key secreykey123 --output ~/MyResinOS.zip\n $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip' ,
2015-02-26 15:47:56 +00:00
options : [
commandOptions . network , commandOptions . wifiSsid , commandOptions . wifiKey , {
signature : 'output' ,
parameter : 'output' ,
description : 'output file' ,
2015-03-09 16:38:37 +00:00
alias : 'o' ,
required : 'You need to specify an output file'
2015-02-26 15:47:56 +00:00
}
] ,
permission : 'user' ,
action : function ( params , options , done ) {
2015-04-20 13:06:40 +00:00
return resin . models . application . get ( params . name , function ( error , application ) {
var osParams ;
2015-02-26 15:47:56 +00:00
if ( error != null ) {
return done ( error ) ;
}
2015-04-20 13:06:40 +00:00
osParams = {
network : options . network ,
wifiSsid : options . ssid ,
wifiKey : options . key ,
appId : application . id
} ;
return async . waterfall ( [
function ( callback ) {
if ( osParams . network != null ) {
return callback ( ) ;
}
return visuals . patterns . selectNetworkParameters ( function ( error , parameters ) {
if ( error != null ) {
return callback ( error ) ;
}
_ . extend ( osParams , parameters ) ;
return callback ( ) ;
} ) ;
} , function ( callback ) {
return mkdirp ( path . dirname ( options . output ) , _ . unary ( callback ) ) ;
} , function ( callback ) {
var bar , spinner ;
console . info ( "Destination file: " + options . output + "\n" ) ;
bar = new visuals . widgets . Progress ( 'Downloading Device OS' ) ;
spinner = new visuals . widgets . Spinner ( 'Downloading Device OS (size unknown)' ) ;
return resin . models . os . download ( osParams , options . output , function ( error ) {
spinner . stop ( ) ;
if ( error != null ) {
return callback ( error ) ;
}
} , function ( state ) {
if ( state != null ) {
return bar . update ( state ) ;
} else {
return spinner . start ( ) ;
}
} ) ;
}
] , function ( error ) {
if ( error != null ) {
return done ( error ) ;
}
console . info ( "\nFinished downloading " + options . output ) ;
return done ( null , options . output ) ;
} ) ;
2015-02-26 15:47:56 +00:00
} ) ;
}
} ;
exports . install = {
signature : 'os install <image> [device]' ,
description : 'write an operating system image to a device' ,
2015-05-02 14:34:11 +00:00
help : 'Use this command to write an operating system image to a device.\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 by passing the `--quiet` boolean option.\n\nExamples:\n\n $ resin os install rpi.iso /dev/disk2' ,
2015-02-26 15:47:56 +00:00
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
2015-04-06 20:57:53 +00:00
var bundle ;
bundle = require ( '../devices/raspberry-pi' ) ;
2015-02-26 15:47:56 +00:00
return async . waterfall ( [
function ( callback ) {
2015-03-11 12:49:26 +00:00
return npm . isUpdated ( packageJSON . name , packageJSON . version , callback ) ;
} , function ( isUpdated , callback ) {
if ( isUpdated ) {
return callback ( ) ;
}
console . info ( 'Resin CLI is outdated.\n\nIn order to avoid device compatibility issues, this command\nrequires that you have the Resin CLI updated.\n\nUpdating now...' ) ;
2015-05-05 20:32:44 +00:00
return capitano . run ( 'update' , _ . unary ( callback ) ) ;
2015-03-11 12:49:26 +00:00
} , function ( callback ) {
2015-02-26 15:47:56 +00:00
if ( params . device != null ) {
return callback ( null , params . device ) ;
}
return visuals . patterns . selectDrive ( function ( error , device ) {
if ( error != null ) {
return callback ( error ) ;
}
if ( device == null ) {
return callback ( new Error ( 'No removable devices available' ) ) ;
}
return callback ( null , device ) ;
} ) ;
} , function ( device , callback ) {
var message ;
params . device = device ;
message = "This will completely erase " + params . device + ". Are you sure you want to continue?" ;
return visuals . patterns . confirm ( options . yes , message , callback ) ;
} , function ( confirmed , callback ) {
if ( ! confirmed ) {
return done ( ) ;
}
2015-05-02 14:34:11 +00:00
return umount ( params . device , _ . unary ( callback ) ) ;
} , function ( callback ) {
var bar ;
2015-04-06 20:57:53 +00:00
bar = new visuals . widgets . Progress ( 'Writing Device OS' ) ;
2015-04-17 14:11:02 +00:00
params . progress = _ . bind ( bar . update , bar ) ;
2015-04-06 20:57:53 +00:00
return bundle . write ( params , callback ) ;
2015-02-26 15:47:56 +00:00
}
] , function ( error ) {
2015-04-06 20:43:31 +00:00
var resinWritePath ;
2015-03-03 16:38:45 +00:00
if ( error == null ) {
return done ( ) ;
}
2015-04-06 20:43:31 +00:00
if ( elevate . shouldElevate ( error ) && ! options . fromScript ) {
2015-02-26 15:47:56 +00:00
resinWritePath = "\"" + ( path . join ( _ _dirname , '..' , '..' , 'bin' , 'resin-write' ) ) + "\"" ;
2015-04-06 20:43:31 +00:00
return elevate . run ( "\"" + process . argv [ 0 ] + "\" " + resinWritePath + " \"" + params . image + "\" \"" + params . device + "\"" ) ;
2015-02-26 15:47:56 +00:00
} else {
return done ( error ) ;
}
} ) ;
}
} ;
} ) . call ( this ) ;