2017-03-29 11:03:40 +00:00
// Generated by CoffeeScript 1.12.5
2017-04-24 19:05:18 +00:00
var Promise , dockerUtils , formatImageName , getBuilderPushEndpoint , getBundleInfo , parseInput , performUpload , pushProgress , uploadToPromise ;
2017-03-29 11:03:40 +00:00
Promise = require ( 'bluebird' ) ;
2017-04-24 19:05:18 +00:00
dockerUtils = require ( '../utils/docker' ) ;
2017-03-29 11:03:40 +00:00
getBuilderPushEndpoint = function ( baseUrl , owner , app ) {
var escApp , escOwner ;
escOwner = encodeURIComponent ( owner ) ;
escApp = encodeURIComponent ( app ) ;
return "https://builder." + baseUrl + "/v1/push?owner=" + escOwner + "&app=" + escApp ;
} ;
formatImageName = function ( image ) {
return image . split ( '/' ) . pop ( ) ;
} ;
parseInput = Promise . method ( function ( params , options ) {
2017-04-24 14:55:54 +00:00
var appName , image , source ;
2017-03-29 11:03:40 +00:00
if ( params . appName == null ) {
throw new Error ( 'Need an application to deploy to!' ) ;
}
appName = params . appName ;
image = void 0 ;
if ( params . image != null ) {
if ( options . build || ( options . source != null ) ) {
throw new Error ( 'Build and source parameters are not applicable when specifying an image' ) ;
}
options . build = false ;
image = params . image ;
} else if ( options . build ) {
2017-04-24 14:55:54 +00:00
source = options . source || '.' ;
2017-03-29 11:03:40 +00:00
} else {
throw new Error ( 'Need either an image or a build flag!' ) ;
}
2017-04-24 14:55:54 +00:00
return [ appName , options . build , source , image ] ;
2017-03-29 11:03:40 +00:00
} ) ;
2017-04-24 12:48:43 +00:00
pushProgress = function ( imageSize , request , logStreams , timeout ) {
var ansiEscapes , logging , progressReporter ;
2017-03-29 11:03:40 +00:00
if ( timeout == null ) {
timeout = 250 ;
}
2017-04-24 12:48:43 +00:00
logging = require ( '../utils/logging' ) ;
ansiEscapes = require ( 'ansi-escapes' ) ;
logging . logInfo ( logStreams , 'Initialising...' ) ;
2017-03-29 11:03:40 +00:00
return progressReporter = setInterval ( function ( ) {
var percent , sent ;
sent = request . req . connection . _bytesDispatched ;
percent = ( sent / imageSize ) * 100 ;
if ( percent >= 100 ) {
clearInterval ( progressReporter ) ;
percent = 100 ;
}
2017-04-24 12:48:43 +00:00
process . stdout . write ( ansiEscapes . cursorUp ( 1 ) ) ;
2017-03-29 11:03:40 +00:00
process . stdout . clearLine ( ) ;
process . stdout . cursorTo ( 0 ) ;
2017-04-24 12:48:43 +00:00
return logging . logInfo ( logStreams , "Uploaded " + ( percent . toFixed ( 1 ) ) + "%" ) ;
2017-03-29 11:03:40 +00:00
} , timeout ) ;
} ;
getBundleInfo = function ( options ) {
var helpers ;
helpers = require ( '../utils/helpers' ) ;
return helpers . getAppInfo ( options . appName ) . then ( function ( app ) {
return [ app . arch , app . device _type ] ;
} ) ;
} ;
2017-04-24 12:48:43 +00:00
performUpload = function ( image , token , username , url , size , appName , logStreams ) {
2017-03-29 11:03:40 +00:00
var post , request ;
request = require ( 'request' ) ;
url = url || process . env . RESINRC _RESIN _URL ;
post = request . post ( {
url : getBuilderPushEndpoint ( url , username , appName ) ,
auth : {
bearer : token
} ,
body : image
} ) ;
2017-04-24 12:48:43 +00:00
return uploadToPromise ( post , size , logStreams ) ;
2017-03-29 11:03:40 +00:00
} ;
2017-04-24 12:48:43 +00:00
uploadToPromise = function ( request , size , logStreams ) {
var logging ;
logging = require ( '../utils/logging' ) ;
2017-03-29 11:03:40 +00:00
return new Promise ( function ( resolve , reject ) {
var handleMessage ;
handleMessage = function ( data ) {
var obj ;
data = data . toString ( ) ;
2017-04-24 12:48:43 +00:00
logging . logDebug ( logStreams , "Received data: " + data ) ;
2017-03-29 11:03:40 +00:00
obj = JSON . parse ( data ) ;
if ( obj . type != null ) {
switch ( obj . type ) {
case 'error' :
return reject ( new Error ( "Remote error: " + obj . error ) ) ;
case 'success' :
return resolve ( obj . image ) ;
case 'status' :
2017-04-24 12:48:43 +00:00
return logging . logInfo ( logStreams , "Remote: " + obj . message ) ;
2017-03-29 11:03:40 +00:00
default :
return reject ( new Error ( "Received unexpected reply from remote: " + data ) ) ;
}
} else {
return reject ( new Error ( "Received unexpected reply from remote: " + data ) ) ;
}
} ;
request . on ( 'error' , reject ) . on ( 'data' , handleMessage ) ;
2017-04-24 12:48:43 +00:00
return pushProgress ( size , request , logStreams ) ;
2017-03-29 11:03:40 +00:00
} ) ;
} ;
module . exports = {
signature : 'deploy <appName> [image]' ,
description : 'Deploy a container to a resin.io application' ,
2017-04-27 13:18:39 +00:00
help : 'Use this command to deploy and optionally build an image to an application.\n\nUsage: deploy <appName> ([image] | --build [--source build-dir])\n\nNote: If building with this command, all options supported by `resin build`\nare also supported with this command.\n\nExamples:\n $ resin deploy myApp --build --source myBuildDir/\n $ resin deploy myApp myApp/myImage' ,
2017-03-29 11:03:40 +00:00
permission : 'user' ,
2017-04-24 19:05:18 +00:00
options : dockerUtils . appendOptions ( [
2017-03-29 11:03:40 +00:00
{
signature : 'build' ,
boolean : true ,
description : 'Build image then deploy' ,
alias : 'b'
} , {
signature : 'source' ,
parameter : 'source' ,
description : 'The source directory to use when building the image' ,
alias : 's'
}
2017-04-24 19:05:18 +00:00
] ) ,
2017-03-29 11:03:40 +00:00
action : function ( params , options , done ) {
2017-04-24 12:48:43 +00:00
var _ , docker , logStreams , logging , resin , tmp , tmpNameAsync ;
2017-03-29 11:03:40 +00:00
_ = require ( 'lodash' ) ;
tmp = require ( 'tmp' ) ;
tmpNameAsync = Promise . promisify ( tmp . tmpName ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
2017-04-24 12:48:43 +00:00
logging = require ( '../utils/logging' ) ;
logStreams = logging . getLogStreams ( ) ;
2017-03-29 11:03:40 +00:00
tmp . setGracefulCleanup ( ) ;
docker = dockerUtils . getDocker ( options ) ;
return parseInput ( params , options ) . then ( function ( arg ) {
2017-04-24 14:55:54 +00:00
var appName , build , imageName , source ;
appName = arg [ 0 ] , build = arg [ 1 ] , source = arg [ 2 ] , imageName = arg [ 3 ] ;
2017-03-29 11:03:40 +00:00
return tmpNameAsync ( ) . then ( function ( tmpPath ) {
options = _ . assign ( { } , options , {
appName : appName
} ) ;
params = _ . assign ( { } , params , {
2017-04-24 14:55:54 +00:00
source : source
2017-03-29 11:03:40 +00:00
} ) ;
return Promise [ "try" ] ( function ( ) {
if ( build ) {
2017-04-24 12:48:43 +00:00
return dockerUtils . runBuild ( params , options , getBundleInfo , logStreams ) ;
2017-03-29 11:03:40 +00:00
} else {
return imageName ;
}
} ) . then ( function ( imageName ) {
2017-04-24 12:48:43 +00:00
return Promise . join ( dockerUtils . bufferImage ( docker , imageName , tmpPath ) , resin . auth . getToken ( ) , resin . auth . whoami ( ) , resin . settings . get ( 'resinUrl' ) , dockerUtils . getImageSize ( docker , imageName ) , params . appName , logStreams , performUpload ) ;
2017-03-29 11:03:40 +00:00
} ) [ "finally" ] ( function ( ) {
return require ( 'fs' ) . unlink ( tmpPath ) ;
} ) ;
} ) ;
} ) . then ( function ( imageName ) {
2017-04-24 12:48:43 +00:00
return logging . logSuccess ( logStreams , "Successfully deployed image: " + ( formatImageName ( imageName ) ) ) ;
2017-03-29 11:03:40 +00:00
} ) . asCallback ( done ) ;
}
} ;