2019-07-09 14:43:36 +00:00
|
|
|
###*
|
|
|
|
# @license
|
|
|
|
# Copyright 2017-2019 Balena Ltd.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
###
|
|
|
|
|
2017-03-29 11:03:40 +00:00
|
|
|
# Functions to help actions which rely on using docker
|
|
|
|
|
2017-12-11 22:37:56 +00:00
|
|
|
Promise = require('bluebird')
|
2019-01-12 22:27:10 +00:00
|
|
|
_ = require('lodash')
|
2017-05-04 12:00:48 +00:00
|
|
|
|
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.
|
2017-08-04 12:53:31 +00:00
|
|
|
exports.appendConnectionOptions = appendConnectionOptions = (opts) ->
|
2017-03-29 11:03:40 +00:00
|
|
|
opts.concat [
|
|
|
|
{
|
|
|
|
signature: 'docker'
|
|
|
|
parameter: 'docker'
|
2019-02-26 13:32:27 +00:00
|
|
|
description: 'Path to a local docker socket (e.g. /var/run/docker.sock)'
|
2017-03-29 11:03:40 +00:00
|
|
|
alias: 'P'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'dockerHost'
|
|
|
|
parameter: 'dockerHost'
|
2019-02-26 13:32:27 +00:00
|
|
|
description: 'Docker daemon hostname or IP address (dev machine or balena device) '
|
2017-03-29 11:03:40 +00:00
|
|
|
alias: 'h'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'dockerPort'
|
|
|
|
parameter: 'dockerPort'
|
2019-02-26 13:32:27 +00:00
|
|
|
description: 'Docker daemon TCP port number (hint: 2375 for balena devices)'
|
2017-03-29 11:03:40 +00:00
|
|
|
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
|
|
|
},
|
2017-08-04 12:53:31 +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) ->
|
|
|
|
appendConnectionOptions(opts).concat [
|
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-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
|
|
|
]
|
|
|
|
|
2017-12-11 22:37:56 +00:00
|
|
|
generateConnectOpts = (opts) ->
|
2017-08-23 12:14:46 +00:00
|
|
|
buildDockerodeOpts = require('dockerode-options')
|
2017-06-20 10:48:15 +00:00
|
|
|
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.")
|
2017-08-23 12:14:46 +00:00
|
|
|
else if process.env.DOCKER_HOST
|
|
|
|
# If no explicit options are provided, use the env
|
|
|
|
connectOpts = buildDockerodeOpts(process.env.DOCKER_HOST)
|
2017-06-20 10:48:15 +00:00
|
|
|
else
|
2017-08-23 12:14:46 +00:00
|
|
|
# No options anywhere, assume default docker local socket
|
2017-06-20 10:48:15 +00:00
|
|
|
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
|
|
|
|
2017-12-11 22:37:56 +00:00
|
|
|
parseBuildArgs = (args) ->
|
2017-05-10 19:02:52 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
if not _.isArray(args)
|
|
|
|
args = [ args ]
|
2017-05-11 11:20:24 +00:00
|
|
|
buildArgs = {}
|
2017-12-11 22:37:56 +00:00
|
|
|
args.forEach (arg) ->
|
2019-05-29 22:37:13 +00:00
|
|
|
# note: [^] matches any character, including line breaks
|
|
|
|
pair = /^([^\s]+?)=([^]*)$/.exec(arg)
|
2017-05-10 19:02:52 +00:00
|
|
|
if pair?
|
2017-12-11 22:37:56 +00:00
|
|
|
buildArgs[pair[1]] = pair[2] ? ''
|
2017-05-10 19:02:52 +00:00
|
|
|
else
|
2017-12-11 22:37:56 +00:00
|
|
|
throw new Error("Could not parse build argument: '#{arg}'")
|
2017-05-11 11:20:24 +00:00
|
|
|
return buildArgs
|
2017-05-10 19:02:52 +00:00
|
|
|
|
2017-12-11 22:37:56 +00:00
|
|
|
exports.generateBuildOpts = (options) ->
|
|
|
|
opts = {}
|
|
|
|
if options.tag?
|
|
|
|
opts.t = options.tag
|
|
|
|
if options.nocache?
|
|
|
|
opts.nocache = true
|
|
|
|
if options.squash?
|
|
|
|
opts.squash = true
|
|
|
|
if options.buildArg?
|
|
|
|
opts.buildargs = parseBuildArgs(options.buildArg)
|
2019-02-26 13:32:27 +00:00
|
|
|
if not _.isEmpty(options['registry-secrets'])
|
|
|
|
opts.registryconfig = options['registry-secrets']
|
2017-12-11 22:37:56 +00:00
|
|
|
return opts
|
2017-03-29 11:03:40 +00:00
|
|
|
|
|
|
|
exports.getDocker = (options) ->
|
2017-06-20 10:48:15 +00:00
|
|
|
generateConnectOpts(options)
|
2017-12-06 21:16:41 +00:00
|
|
|
.then(createClient)
|
2017-12-11 22:37:56 +00:00
|
|
|
.tap(ensureDockerSeemsAccessible)
|
2017-12-06 21:16:41 +00:00
|
|
|
|
2019-01-12 22:27:10 +00:00
|
|
|
getDockerToolbelt = _.once ->
|
2017-12-06 21:16:41 +00:00
|
|
|
Docker = require('docker-toolbelt')
|
|
|
|
Promise.promisifyAll Docker.prototype, {
|
|
|
|
filter: (name) -> name == 'run'
|
|
|
|
multiArgs: true
|
|
|
|
}
|
|
|
|
Promise.promisifyAll(Docker.prototype)
|
|
|
|
Promise.promisifyAll(new Docker({}).getImage().constructor.prototype)
|
|
|
|
Promise.promisifyAll(new Docker({}).getContainer().constructor.prototype)
|
2019-01-12 22:27:10 +00:00
|
|
|
return Docker
|
2017-12-06 21:16:41 +00:00
|
|
|
|
2019-01-12 22:27:10 +00:00
|
|
|
# docker-toolbelt v3 is not backwards compatible as it removes all *Async
|
|
|
|
# methods that are in wide use in the CLI. The workaround for now is to
|
|
|
|
# manually promisify the client and replace all `new Docker()` calls with
|
|
|
|
# this shared function that returns a promisified client.
|
|
|
|
#
|
|
|
|
# **New code must not use the *Async methods.**
|
|
|
|
#
|
|
|
|
exports.createClient = createClient = (opts) ->
|
|
|
|
Docker = getDockerToolbelt()
|
|
|
|
return new Docker(opts)
|
2017-03-29 11:03:40 +00:00
|
|
|
|
2017-12-11 22:37:56 +00:00
|
|
|
ensureDockerSeemsAccessible = (docker) ->
|
2018-04-17 13:17:48 +00:00
|
|
|
{ exitWithExpectedError } = require('./patterns')
|
2017-12-11 22:37:56 +00:00
|
|
|
docker.ping().catch ->
|
2018-04-17 13:17:48 +00:00
|
|
|
exitWithExpectedError('Docker seems to be unavailable. Is it installed and running?')
|