2017-03-29 11:03:40 +00:00
|
|
|
# Functions to help actions which rely on using docker
|
|
|
|
|
2017-05-04 12:00:48 +00:00
|
|
|
QEMU_VERSION = 'v2.5.50-resin-execve'
|
|
|
|
QEMU_BIN_NAME = 'qemu-execve'
|
|
|
|
|
2017-03-29 11:03:40 +00:00
|
|
|
# Use this function to seed an action's list of capitano options
|
|
|
|
# with the docker options. Using this interface means that
|
|
|
|
# all functions using docker will expose the same interface
|
|
|
|
#
|
|
|
|
# NOTE: Care MUST be taken when using the function, so as to
|
|
|
|
# not redefine/override options already provided.
|
|
|
|
exports.appendOptions = (opts) ->
|
|
|
|
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
|
|
|
{
|
2017-05-11 11:20:24 +00:00
|
|
|
signature: 'buildArg'
|
2017-05-10 19:02:52 +00:00
|
|
|
parameter: 'arg'
|
2017-05-11 11:20:24 +00:00
|
|
|
description: 'Set a build-time variable (eg. "-B \'ARG=value\'"). Can be specified multiple times.'
|
|
|
|
alias: 'B'
|
2017-05-10 19:02:52 +00:00
|
|
|
},
|
2017-04-24 19:05:18 +00:00
|
|
|
{
|
|
|
|
signature: 'nocache'
|
|
|
|
description: "Don't use docker layer caching when building"
|
|
|
|
boolean: true
|
|
|
|
},
|
2017-05-04 12:00:48 +00:00
|
|
|
{
|
|
|
|
signature: 'emulated'
|
|
|
|
description: 'Run an emulated build using Qemu'
|
|
|
|
boolean: true
|
|
|
|
alias: 'e'
|
2017-06-23 15:20:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'squash'
|
|
|
|
description: 'Squash newly built layers into a single new layer'
|
|
|
|
boolean: true
|
2017-05-04 12:00:48 +00:00
|
|
|
}
|
2017-03-29 11:03:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
exports.generateConnectOpts = generateConnectOpts = (opts) ->
|
2017-06-20 10:48:15 +00:00
|
|
|
Promise = require('bluebird')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
_ = require('lodash')
|
|
|
|
|
|
|
|
Promise.try ->
|
|
|
|
connectOpts = {}
|
|
|
|
# Firsly need to decide between a local docker socket
|
|
|
|
# and a host available over a host:port combo
|
|
|
|
if opts.docker? and not opts.dockerHost?
|
|
|
|
# good, local docker socket
|
|
|
|
connectOpts.socketPath = opts.docker
|
|
|
|
else if opts.dockerHost? and not opts.docker?
|
|
|
|
# Good a host is provided, and local socket isn't
|
|
|
|
connectOpts.host = opts.dockerHost
|
|
|
|
connectOpts.port = opts.dockerPort || 2376
|
|
|
|
else if opts.docker? and opts.dockerHost?
|
|
|
|
# Both provided, no obvious way to continue
|
|
|
|
throw new Error("Both a local docker socket and docker host have been provided. Don't know how to continue.")
|
|
|
|
else
|
|
|
|
# None provided, assume default docker local socket
|
|
|
|
connectOpts.socketPath = '/var/run/docker.sock'
|
|
|
|
|
|
|
|
# Now need to check if the user wants to connect over TLS
|
|
|
|
# to the host
|
|
|
|
|
|
|
|
# If any are set...
|
|
|
|
if (opts.ca? or opts.cert? or opts.key?)
|
|
|
|
# but not all
|
|
|
|
if not (opts.ca? and opts.cert? and opts.key?)
|
|
|
|
throw new Error('You must provide a CA, certificate and key in order to use TLS')
|
|
|
|
|
|
|
|
certBodies = {
|
|
|
|
ca: fs.readFile(opts.ca, 'utf-8')
|
|
|
|
cert: fs.readFile(opts.cert, 'utf-8')
|
|
|
|
key: fs.readFile(opts.key, 'utf-8')
|
|
|
|
}
|
|
|
|
return Promise.props(certBodies)
|
|
|
|
.then (toMerge) ->
|
|
|
|
_.merge(connectOpts, toMerge)
|
|
|
|
|
|
|
|
return connectOpts
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
exports.tarDirectory = tarDirectory = (dir) ->
|
|
|
|
Promise = require('bluebird')
|
|
|
|
tar = require('tar-stream')
|
|
|
|
klaw = require('klaw')
|
|
|
|
path = require('path')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
streamToPromise = require('stream-to-promise')
|
|
|
|
|
|
|
|
getFiles = ->
|
|
|
|
streamToPromise(klaw(dir))
|
|
|
|
.filter((item) -> not item.stats.isDirectory())
|
|
|
|
.map((item) -> item.path)
|
|
|
|
|
|
|
|
pack = tar.pack()
|
|
|
|
getFiles(dir)
|
|
|
|
.map (file) ->
|
|
|
|
relPath = path.relative(path.resolve(dir), file)
|
|
|
|
Promise.join relPath, fs.stat(file), fs.readFile(file),
|
|
|
|
(filename, stats, data) ->
|
2017-05-04 12:00:48 +00:00
|
|
|
pack.entryAsync({ name: filename, size: stats.size, mode: stats.mode }, data)
|
2017-03-29 11:03:40 +00:00
|
|
|
.then ->
|
|
|
|
pack.finalize()
|
|
|
|
return pack
|
|
|
|
|
2017-05-02 12:27:18 +00:00
|
|
|
cacheHighlightStream = ->
|
|
|
|
colors = require('colors/safe')
|
|
|
|
es = require('event-stream')
|
|
|
|
{ EOL } = require('os')
|
|
|
|
|
|
|
|
extractArrowMessage = (message) ->
|
|
|
|
arrowTest = /^\s*-+>\s*(.+)/i
|
|
|
|
if (match = arrowTest.exec(message))
|
|
|
|
match[1]
|
|
|
|
else
|
|
|
|
undefined
|
|
|
|
|
|
|
|
es.mapSync (data) ->
|
|
|
|
msg = extractArrowMessage(data)
|
|
|
|
if msg? and msg.toLowerCase() == 'using cache'
|
|
|
|
data = colors.bgGreen.black(msg)
|
|
|
|
return data + EOL
|
|
|
|
|
2017-05-10 19:02:52 +00:00
|
|
|
parseBuildArgs = (args, onError) ->
|
|
|
|
_ = require('lodash')
|
|
|
|
if not _.isArray(args)
|
|
|
|
args = [ args ]
|
2017-05-11 11:20:24 +00:00
|
|
|
buildArgs = {}
|
2017-05-10 19:02:52 +00:00
|
|
|
args.forEach (str) ->
|
|
|
|
pair = /^([^\s]+?)=(.*)$/.exec(str)
|
|
|
|
if pair?
|
2017-05-11 11:20:24 +00:00
|
|
|
buildArgs[pair[1]] = pair[2]
|
2017-05-10 19:02:52 +00:00
|
|
|
else
|
|
|
|
onError(str)
|
2017-05-11 11:20:24 +00:00
|
|
|
return buildArgs
|
2017-05-10 19:02:52 +00:00
|
|
|
|
2017-03-29 11:03:40 +00:00
|
|
|
# Pass in the command line parameters and options and also
|
|
|
|
# a function which will return the information about the bundle
|
2017-04-24 12:48:43 +00:00
|
|
|
exports.runBuild = (params, options, getBundleInfo, logStreams) ->
|
|
|
|
|
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-05-04 12:00:48 +00:00
|
|
|
transpose = require('docker-qemu-transpose')
|
|
|
|
path = require('path')
|
2017-04-24 10:06:53 +00:00
|
|
|
|
2017-04-24 12:48:43 +00:00
|
|
|
logging = require('../utils/logging')
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
# The default build context is the current directory
|
2017-04-24 14:55:54 +00:00
|
|
|
params.source ?= '.'
|
2017-04-24 10:06:53 +00:00
|
|
|
logs = ''
|
2017-05-04 12:00:48 +00:00
|
|
|
# Only used in emulated builds
|
|
|
|
qemuPath = ''
|
|
|
|
|
|
|
|
Promise.try ->
|
|
|
|
return if not (options.emulated and platformNeedsQemu())
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-05-04 12:00:48 +00:00
|
|
|
hasQemu()
|
|
|
|
.then (present) ->
|
|
|
|
if !present
|
|
|
|
logging.logInfo(logStreams, 'Installing qemu for ARM emulation...')
|
|
|
|
installQemu()
|
|
|
|
.then ->
|
|
|
|
# Copy the qemu binary into the build context
|
|
|
|
copyQemu(params.source)
|
|
|
|
.then (binPath) ->
|
2017-06-14 18:50:24 +00:00
|
|
|
qemuPath = path.relative(params.source, binPath)
|
2017-05-04 12:00:48 +00:00
|
|
|
.then ->
|
|
|
|
# Tar up the directory, ready for the build stream
|
|
|
|
tarDirectory(params.source)
|
2017-03-29 11:03:40 +00:00
|
|
|
.then (tarStream) ->
|
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
hooks =
|
|
|
|
buildSuccess: (image) ->
|
|
|
|
if options.tag?
|
|
|
|
console.log("Tagging image as #{options.tag}")
|
2017-04-28 12:44:40 +00:00
|
|
|
# Show charlie. In the interest of cloud parity,
|
|
|
|
# use console.log, not the standard logging streams
|
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-05-02 12:27:18 +00:00
|
|
|
resolve({ image, log: logs + '\n' + doodle + '\n' } )
|
2017-04-28 12:44:40 +00:00
|
|
|
|
2017-03-29 11:03:40 +00:00
|
|
|
buildFailure: reject
|
|
|
|
buildStream: (stream) ->
|
2017-05-04 12:00:48 +00:00
|
|
|
if options.emulated
|
|
|
|
logging.logInfo(logStreams, 'Running emulated build')
|
|
|
|
|
2017-03-29 11:03:40 +00:00
|
|
|
getBundleInfo(options)
|
|
|
|
.then (info) ->
|
|
|
|
if !info?
|
2017-04-24 12:48:43 +00:00
|
|
|
logging.logWarn logStreams, '''
|
2017-03-29 11:03:40 +00:00
|
|
|
Warning: No architecture/device type or application information provided.
|
|
|
|
Dockerfile/project pre-processing will not be performed.
|
|
|
|
'''
|
2017-05-04 12:00:48 +00:00
|
|
|
return tarStream
|
2017-03-29 11:03:40 +00:00
|
|
|
else
|
|
|
|
[arch, deviceType] = info
|
|
|
|
# Perform type resolution on the project
|
|
|
|
bundle = new resolver.Bundle(tarStream, deviceType, arch)
|
|
|
|
resolver.resolveBundle(bundle, resolver.getDefaultResolvers())
|
|
|
|
.then (resolved) ->
|
2017-04-24 12:48:43 +00:00
|
|
|
logging.logInfo(logStreams, "Building #{resolved.projectType} project")
|
2017-05-04 12:00:48 +00:00
|
|
|
|
|
|
|
return resolved.tarStream
|
|
|
|
.then (buildStream) ->
|
|
|
|
# if we need emulation
|
|
|
|
if options.emulated and platformNeedsQemu()
|
|
|
|
return transpose.transposeTarStream buildStream,
|
|
|
|
hostQemuPath: qemuPath
|
2017-06-15 14:36:44 +00:00
|
|
|
containerQemuPath: "/tmp/#{QEMU_BIN_NAME}"
|
2017-05-04 12:00:48 +00:00
|
|
|
else
|
|
|
|
return buildStream
|
|
|
|
.then (buildStream) ->
|
|
|
|
# Send the resolved tar stream to the docker daemon
|
|
|
|
buildStream.pipe(stream)
|
2017-03-29 11:03:40 +00:00
|
|
|
.catch(reject)
|
|
|
|
|
2017-04-24 10:06:53 +00:00
|
|
|
# And print the output
|
2017-05-04 12:00:48 +00:00
|
|
|
logThroughStream = es.through (data) ->
|
2017-04-24 10:06:53 +00:00
|
|
|
logs += data.toString()
|
|
|
|
this.emit('data', data)
|
|
|
|
|
2017-05-04 12:00:48 +00:00
|
|
|
if options.emulated and platformNeedsQemu()
|
|
|
|
buildThroughStream = transpose.getBuildThroughStream
|
|
|
|
hostQemuPath: qemuPath
|
2017-06-15 14:36:44 +00:00
|
|
|
containerQemuPath: "/tmp/#{QEMU_BIN_NAME}"
|
2017-05-04 12:00:48 +00:00
|
|
|
|
|
|
|
newStream = stream.pipe(buildThroughStream)
|
|
|
|
else
|
|
|
|
newStream = stream
|
|
|
|
|
|
|
|
newStream
|
|
|
|
.pipe(logThroughStream)
|
2017-05-02 12:27:18 +00:00
|
|
|
.pipe(cacheHighlightStream())
|
|
|
|
.pipe(logStreams.build)
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
# Create a builder
|
2017-06-20 10:48:15 +00:00
|
|
|
generateConnectOpts(options)
|
|
|
|
.then (connectOpts) ->
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-06-20 10:48:15 +00:00
|
|
|
# Allow degugging output, hidden behind an env var
|
|
|
|
logging.logDebug(logStreams, 'Connecting with the following options:')
|
|
|
|
logging.logDebug(logStreams, JSON.stringify(connectOpts, null, ' '))
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-06-20 10:48:15 +00:00
|
|
|
builder = new dockerBuild.Builder(connectOpts)
|
|
|
|
opts = {}
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-06-20 10:48:15 +00:00
|
|
|
if options.tag?
|
|
|
|
opts['t'] = options.tag
|
|
|
|
if options.nocache?
|
|
|
|
opts['nocache'] = true
|
|
|
|
if options.buildArg?
|
|
|
|
opts['buildargs'] = parseBuildArgs options.buildArg, (arg) ->
|
|
|
|
logging.logWarn(logStreams, "Could not parse variable: '#{arg}'")
|
2017-06-23 15:20:16 +00:00
|
|
|
if options.squash?
|
|
|
|
opts['squash'] = true
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-06-20 10:48:15 +00:00
|
|
|
builder.createBuildStream(opts, hooks, reject)
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-06-13 17:28:37 +00:00
|
|
|
# Given an image id or tag, export the image to a tar archive,
|
|
|
|
# gzip the result, and buffer it to disk.
|
2017-06-13 17:28:37 +00:00
|
|
|
exports.bufferImage = (docker, imageId, bufferFile) ->
|
|
|
|
Promise = require('bluebird')
|
2017-06-13 17:28:37 +00:00
|
|
|
streamUtils = require('./streams')
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
image = docker.getImage(imageId)
|
2017-06-13 17:28:37 +00:00
|
|
|
imageMetadata = image.inspectAsync()
|
|
|
|
|
2017-06-15 10:34:26 +00:00
|
|
|
Promise.join image.get(), imageMetadata.get('Size'), (imageStream, imageSize) ->
|
2017-06-13 17:28:37 +00:00
|
|
|
streamUtils.buffer(imageStream, bufferFile)
|
|
|
|
.tap (bufferedStream) ->
|
|
|
|
bufferedStream.length = imageSize
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
exports.getDocker = (options) ->
|
|
|
|
Docker = require('dockerode')
|
|
|
|
Promise = require('bluebird')
|
2017-06-20 10:48:15 +00:00
|
|
|
|
|
|
|
generateConnectOpts(options)
|
|
|
|
.then (connectOpts) ->
|
|
|
|
# Use bluebird's promises
|
|
|
|
connectOpts['Promise'] = Promise
|
|
|
|
new Docker(connectOpts)
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-05-04 12:00:48 +00:00
|
|
|
hasQemu = ->
|
|
|
|
fs = require('mz/fs')
|
|
|
|
|
|
|
|
getQemuPath()
|
|
|
|
.then(fs.stat)
|
|
|
|
.return(true)
|
|
|
|
.catchReturn(false)
|
|
|
|
|
|
|
|
getQemuPath = ->
|
|
|
|
resin = require('resin-sdk-preconfigured')
|
|
|
|
path = require('path')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
|
|
|
|
resin.settings.get('binDirectory')
|
|
|
|
.then (binDir) ->
|
|
|
|
# The directory might not be created already,
|
|
|
|
# if not, create it
|
|
|
|
fs.access(binDir)
|
|
|
|
.catch code: 'ENOENT', ->
|
|
|
|
fs.mkdir(binDir)
|
|
|
|
.then ->
|
|
|
|
path.join(binDir, QEMU_BIN_NAME)
|
|
|
|
|
|
|
|
platformNeedsQemu = ->
|
|
|
|
os = require('os')
|
|
|
|
os.platform() == 'linux'
|
|
|
|
|
|
|
|
installQemu = ->
|
|
|
|
request = require('request')
|
|
|
|
fs = require('fs')
|
|
|
|
zlib = require('zlib')
|
|
|
|
|
|
|
|
getQemuPath()
|
|
|
|
.then (qemuPath) ->
|
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
installStream = fs.createWriteStream(qemuPath)
|
|
|
|
qemuUrl = "https://github.com/resin-io/qemu/releases/download/#{QEMU_VERSION}/#{QEMU_BIN_NAME}.gz"
|
|
|
|
request(qemuUrl)
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.pipe(installStream)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('finish', resolve)
|
|
|
|
|
|
|
|
copyQemu = (context) ->
|
|
|
|
path = require('path')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
# Create a hidden directory in the build context, containing qemu
|
|
|
|
binDir = path.join(context, '.resin')
|
|
|
|
binPath = path.join(binDir, QEMU_BIN_NAME)
|
|
|
|
|
|
|
|
fs.access(binDir)
|
|
|
|
.catch code: 'ENOENT', ->
|
|
|
|
fs.mkdir(binDir)
|
|
|
|
.then ->
|
|
|
|
getQemuPath()
|
|
|
|
.then (qemu) ->
|
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
read = fs.createReadStream(qemu)
|
|
|
|
write = fs.createWriteStream(binPath)
|
|
|
|
read
|
|
|
|
.pipe(write)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('finish', resolve)
|
|
|
|
.then ->
|
|
|
|
fs.chmod(binPath, '755')
|
|
|
|
.return(binPath)
|