2017-03-29 11:03:40 +00:00
// Generated by CoffeeScript 1.12.5
2017-05-10 19:02:52 +00:00
var cacheHighlightStream , generateConnectOpts , parseBuildArgs , tarDirectory ;
2017-03-29 11:03:40 +00:00
exports . appendOptions = function ( opts ) {
return opts . concat ( [
{
signature : 'docker' ,
parameter : 'docker' ,
description : 'Path to a local docker socket' ,
alias : 'P'
} , {
signature : 'dockerHost' ,
parameter : 'dockerHost' ,
description : 'The address of the host containing the docker daemon' ,
alias : 'h'
} , {
signature : 'dockerPort' ,
parameter : 'dockerPort' ,
description : 'The port on which the host docker daemon is listening' ,
alias : 'p'
} , {
signature : 'ca' ,
parameter : 'ca' ,
description : 'Docker host TLS certificate authority file'
} , {
signature : 'cert' ,
parameter : 'cert' ,
description : 'Docker host TLS certificate file'
} , {
signature : 'key' ,
parameter : 'key' ,
description : 'Docker host TLS key file'
2017-04-24 19:05:18 +00:00
} , {
signature : 'tag' ,
parameter : 'tag' ,
description : 'The alias to the generated image' ,
alias : 't'
2017-05-10 19:02:52 +00:00
} , {
signature : 'arg' ,
parameter : 'arg' ,
description : 'Set a build-time variable (eg. "-e \'ARG=value\'"). Can be specified multiple times.' ,
alias : 'e'
2017-04-24 19:05:18 +00:00
} , {
signature : 'nocache' ,
description : "Don't use docker layer caching when building" ,
boolean : true
2017-03-29 11:03:40 +00:00
}
] ) ;
} ;
exports . generateConnectOpts = generateConnectOpts = function ( opts ) {
var connectOpts ;
connectOpts = { } ;
if ( ( opts . docker != null ) && ( opts . dockerHost == null ) ) {
connectOpts . socketPath = opts . docker ;
} else if ( ( opts . dockerHost != null ) && ( opts . docker == null ) ) {
connectOpts . host = opts . dockerHost ;
connectOpts . port = opts . dockerPort || 2376 ;
} else if ( ( opts . docker != null ) && ( opts . dockerHost != null ) ) {
throw new Error ( "Both a local docker socket and docker host have been provided. Don't know how to continue." ) ;
} else {
connectOpts . socketPath = '/var/run/docker.sock' ;
}
if ( ( opts . ca != null ) || ( opts . cert != null ) || ( opts . key != null ) ) {
if ( ! ( ( opts . ca != null ) && ( opts . cert != null ) && ( opts . key != null ) ) ) {
throw new Error ( 'You must provide a CA, certificate and key in order to use TLS' ) ;
}
connectOpts . ca = opts . ca ;
connectOpts . cert = opts . cert ;
connectOpts . key = opts . key ;
}
return connectOpts ;
} ;
exports . tarDirectory = tarDirectory = function ( dir ) {
var Promise , fs , getFiles , klaw , pack , path , streamToPromise , tar ;
Promise = require ( 'bluebird' ) ;
tar = require ( 'tar-stream' ) ;
klaw = require ( 'klaw' ) ;
path = require ( 'path' ) ;
fs = require ( 'mz/fs' ) ;
streamToPromise = require ( 'stream-to-promise' ) ;
getFiles = function ( ) {
return streamToPromise ( klaw ( dir ) ) . filter ( function ( item ) {
return ! item . stats . isDirectory ( ) ;
} ) . map ( function ( item ) {
return item . path ;
} ) ;
} ;
pack = tar . pack ( ) ;
return getFiles ( dir ) . map ( function ( file ) {
var relPath ;
relPath = path . relative ( path . resolve ( dir ) , file ) ;
return Promise . join ( relPath , fs . stat ( file ) , fs . readFile ( file ) , function ( filename , stats , data ) {
return pack . entryAsync ( {
name : filename ,
size : stats . size
} , data ) ;
} ) ;
} ) . then ( function ( ) {
pack . finalize ( ) ;
return pack ;
} ) ;
} ;
2017-05-02 12:27:18 +00:00
cacheHighlightStream = function ( ) {
var EOL , colors , es , extractArrowMessage ;
colors = require ( 'colors/safe' ) ;
es = require ( 'event-stream' ) ;
EOL = require ( 'os' ) . EOL ;
extractArrowMessage = function ( message ) {
var arrowTest , match ;
arrowTest = /^\s*-+>\s*(.+)/i ;
if ( ( match = arrowTest . exec ( message ) ) ) {
return match [ 1 ] ;
} else {
return void 0 ;
}
} ;
return es . mapSync ( function ( data ) {
var msg ;
msg = extractArrowMessage ( data ) ;
if ( ( msg != null ) && msg . toLowerCase ( ) === 'using cache' ) {
data = colors . bgGreen . black ( msg ) ;
}
return data + EOL ;
} ) ;
} ;
2017-05-10 19:02:52 +00:00
parseBuildArgs = function ( args , onError ) {
var _ , obj ;
_ = require ( 'lodash' ) ;
if ( ! _ . isArray ( args ) ) {
args = [ args ] ;
}
obj = { } ;
args . forEach ( function ( str ) {
var pair ;
pair = /^([^\s]+?)=(.*)$/ . exec ( str ) ;
if ( pair != null ) {
return obj [ pair [ 1 ] ] = pair [ 2 ] ;
} else {
return onError ( str ) ;
}
} ) ;
return obj ;
} ;
2017-04-24 12:48:43 +00:00
exports . runBuild = function ( params , options , getBundleInfo , logStreams ) {
2017-04-28 12:44:40 +00:00
var Promise , dockerBuild , doodles , es , logging , logs , resolver ;
2017-03-29 11:03:40 +00:00
Promise = require ( 'bluebird' ) ;
dockerBuild = require ( 'resin-docker-build' ) ;
resolver = require ( 'resin-bundle-resolve' ) ;
2017-04-24 10:06:53 +00:00
es = require ( 'event-stream' ) ;
2017-04-28 12:44:40 +00:00
doodles = require ( 'resin-doodles' ) ;
2017-04-24 12:48:43 +00:00
logging = require ( '../utils/logging' ) ;
2017-04-24 14:55:54 +00:00
if ( params . source == null ) {
params . source = '.' ;
2017-03-29 11:03:40 +00:00
}
2017-04-24 10:06:53 +00:00
logs = '' ;
2017-04-24 14:55:54 +00:00
return tarDirectory ( params . source ) . then ( function ( tarStream ) {
2017-03-29 11:03:40 +00:00
return new Promise ( function ( resolve , reject ) {
var builder , connectOpts , hooks , opts ;
hooks = {
buildSuccess : function ( image ) {
2017-05-02 12:27:18 +00:00
var doodle ;
2017-03-29 11:03:40 +00:00
if ( options . tag != null ) {
console . log ( "Tagging image as " + options . tag ) ;
}
2017-05-02 12:27:18 +00:00
doodle = doodles . getDoodle ( ) ;
2017-04-28 12:44:40 +00:00
console . log ( ) ;
2017-05-02 12:27:18 +00:00
console . log ( doodle ) ;
2017-04-28 12:44:40 +00:00
console . log ( ) ;
2017-04-24 10:06:53 +00:00
return resolve ( {
image : image ,
2017-05-02 12:27:18 +00:00
log : logs + '\n' + doodle + '\n'
2017-04-24 10:06:53 +00:00
} ) ;
2017-03-29 11:03:40 +00:00
} ,
buildFailure : reject ,
buildStream : function ( stream ) {
2017-04-24 10:06:53 +00:00
var throughStream ;
2017-03-29 11:03:40 +00:00
getBundleInfo ( options ) . then ( function ( info ) {
var arch , bundle , deviceType ;
if ( info == null ) {
2017-04-24 12:48:43 +00:00
logging . logWarn ( logStreams , 'Warning: No architecture/device type or application information provided.\n Dockerfile/project pre-processing will not be performed.' ) ;
2017-03-29 11:03:40 +00:00
return tarStream . pipe ( stream ) ;
} else {
arch = info [ 0 ] , deviceType = info [ 1 ] ;
bundle = new resolver . Bundle ( tarStream , deviceType , arch ) ;
return resolver . resolveBundle ( bundle , resolver . getDefaultResolvers ( ) ) . then ( function ( resolved ) {
2017-04-24 12:48:43 +00:00
logging . logInfo ( logStreams , "Building " + resolved . projectType + " project" ) ;
2017-03-29 11:03:40 +00:00
return resolved . tarStream . pipe ( stream ) ;
} ) ;
}
} ) [ "catch" ] ( reject ) ;
2017-04-24 10:06:53 +00:00
throughStream = es . through ( function ( data ) {
logs += data . toString ( ) ;
return this . emit ( 'data' , data ) ;
} ) ;
2017-05-02 12:27:18 +00:00
return stream . pipe ( throughStream ) . pipe ( cacheHighlightStream ( ) ) . pipe ( logStreams . build ) ;
2017-03-29 11:03:40 +00:00
}
} ;
connectOpts = generateConnectOpts ( options ) ;
2017-04-24 12:48:43 +00:00
logging . logDebug ( logStreams , 'Connecting with the following options:' ) ;
logging . logDebug ( logStreams , JSON . stringify ( connectOpts , null , ' ' ) ) ;
2017-03-29 11:03:40 +00:00
builder = new dockerBuild . Builder ( connectOpts ) ;
opts = { } ;
if ( options . tag != null ) {
opts [ 't' ] = options . tag ;
}
if ( options . nocache != null ) {
opts [ 'nocache' ] = true ;
}
2017-05-10 19:02:52 +00:00
if ( options . arg != null ) {
opts [ 'buildargs' ] = parseBuildArgs ( options . arg , function ( arg ) {
return logging . logWarn ( logStreams , "Could not parse variable: '" + arg + "'" ) ;
} ) ;
}
2017-03-29 11:03:40 +00:00
return builder . createBuildStream ( opts , hooks , reject ) ;
} ) ;
} ) ;
} ;
exports . bufferImage = function ( docker , imageId , tmpFile ) {
var Promise , fs , image , stream ;
Promise = require ( 'bluebird' ) ;
fs = require ( 'fs' ) ;
stream = fs . createWriteStream ( tmpFile ) ;
image = docker . getImage ( imageId ) ;
return image . get ( ) . then ( function ( img ) {
return new Promise ( function ( resolve , reject ) {
2017-04-24 10:06:53 +00:00
return img . on ( 'error' , reject ) . on ( 'end' , resolve ) . pipe ( stream ) ;
2017-03-29 11:03:40 +00:00
} ) ;
} ) . then ( function ( ) {
return new Promise ( function ( resolve , reject ) {
return fs . createReadStream ( tmpFile ) . on ( 'open' , function ( ) {
return resolve ( this ) ;
} ) . on ( 'error' , reject ) ;
} ) ;
} ) ;
} ;
exports . getDocker = function ( options ) {
var Docker , Promise , connectOpts ;
Docker = require ( 'dockerode' ) ;
Promise = require ( 'bluebird' ) ;
connectOpts = generateConnectOpts ( options ) ;
connectOpts [ 'Promise' ] = Promise ;
return new Docker ( connectOpts ) ;
} ;
exports . getImageSize = function ( docker , image ) {
return docker . getImage ( image ) . inspectAsync ( ) . get ( 'Size' ) ;
} ;