2020-07-30 08:39:05 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-04-27 13:58:54 +00:00
|
|
|
* Copyright 2016-2021 Balena Ltd.
|
2020-07-30 08:39:05 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { flags } from '@oclif/command';
|
|
|
|
import Command from '../command';
|
|
|
|
import { getBalenaSdk } from '../utils/lazy';
|
2021-07-15 13:41:38 +00:00
|
|
|
import * as cf from '../utils/common-flags';
|
2020-07-30 08:39:05 +00:00
|
|
|
import * as compose from '../utils/compose';
|
|
|
|
import type { Application, ApplicationType, BalenaSDK } from 'balena-sdk';
|
2021-04-21 05:26:57 +00:00
|
|
|
import {
|
2021-07-15 13:41:38 +00:00
|
|
|
appToFleetFlagMsg,
|
2021-04-21 05:26:57 +00:00
|
|
|
buildArgDeprecation,
|
|
|
|
dockerignoreHelp,
|
|
|
|
registrySecretsHelp,
|
2021-07-15 13:41:38 +00:00
|
|
|
warnify,
|
2021-04-21 05:26:57 +00:00
|
|
|
} from '../utils/messages';
|
2020-07-30 08:39:05 +00:00
|
|
|
import type { ComposeCliFlags, ComposeOpts } from '../utils/compose-types';
|
2020-10-15 23:26:00 +00:00
|
|
|
import { buildProject, composeCliFlags } from '../utils/compose_ts';
|
|
|
|
import type { BuildOpts, DockerCliFlags } from '../utils/docker';
|
2020-07-30 08:39:05 +00:00
|
|
|
import { dockerCliFlags } from '../utils/docker';
|
2021-07-15 13:41:38 +00:00
|
|
|
import { isV13 } from '../utils/version';
|
2020-07-30 08:39:05 +00:00
|
|
|
|
|
|
|
interface FlagsDef extends ComposeCliFlags, DockerCliFlags {
|
|
|
|
arch?: string;
|
|
|
|
deviceType?: string;
|
|
|
|
application?: string;
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet?: string;
|
2020-07-30 08:39:05 +00:00
|
|
|
source?: string; // Not part of command profile - source param copied here.
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
source?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class BuildCmd extends Command {
|
|
|
|
public static description = `\
|
|
|
|
Build a project locally.
|
|
|
|
|
|
|
|
Use this command to build an image or a complete multicontainer project with
|
|
|
|
the provided docker daemon in your development machine or balena device.
|
|
|
|
(See also the \`balena push\` command for the option of building images in the
|
|
|
|
balenaCloud build servers.)
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
You must specify either a fleet, or the device type and architecture.
|
2020-07-30 08:39:05 +00:00
|
|
|
|
|
|
|
This command will look into the given source directory (or the current working
|
|
|
|
directory if one isn't specified) for a docker-compose.yml file, and if found,
|
|
|
|
each service defined in the compose file will be built. If a compose file isn't
|
|
|
|
found, it will look for a Dockerfile[.template] file (or alternative Dockerfile
|
|
|
|
specified with the \`--dockerfile\` option), and if no dockerfile is found, it
|
|
|
|
will try to generate one.
|
|
|
|
|
|
|
|
${registrySecretsHelp}
|
|
|
|
|
|
|
|
${dockerignoreHelp}
|
|
|
|
`;
|
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena build --fleet myFleet',
|
|
|
|
'$ balena build ./source/ --fleet myorg/myfleet',
|
2020-07-30 08:39:05 +00:00
|
|
|
'$ balena build --deviceType raspberrypi3 --arch armv7hf --emulated',
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena build --docker /var/run/docker.sock --fleet myFleet # Linux, Mac',
|
|
|
|
'$ balena build --docker //./pipe/docker_engine --fleet myFleet # Windows',
|
|
|
|
'$ balena build --dockerHost my.docker.host --dockerPort 2376 --ca ca.pem --key key.pem --cert cert.pem -f myFleet',
|
2020-07-30 08:39:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public static args = [
|
|
|
|
{
|
|
|
|
name: 'source',
|
|
|
|
description: 'path of project source directory',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = 'build [source]';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
arch: flags.string({
|
|
|
|
description: 'the architecture to build for',
|
|
|
|
char: 'A',
|
|
|
|
}),
|
|
|
|
deviceType: flags.string({
|
|
|
|
description: 'the type of device this build is for',
|
|
|
|
char: 'd',
|
|
|
|
}),
|
2021-07-15 13:41:38 +00:00
|
|
|
...(isV13() ? {} : { application: cf.application }),
|
|
|
|
fleet: cf.fleet,
|
2020-07-30 08:39:05 +00:00
|
|
|
...composeCliFlags,
|
|
|
|
...dockerCliFlags,
|
2020-08-21 08:57:14 +00:00
|
|
|
// NOTE: Not supporting -h for help, because of clash with -h in DockerCliFlags
|
|
|
|
// Revisit this in future release.
|
|
|
|
help: flags.help({}),
|
2020-07-30 08:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public static primary = true;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
|
|
|
BuildCmd,
|
|
|
|
);
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
if (options.application && process.stderr.isTTY) {
|
|
|
|
console.error(warnify(appToFleetFlagMsg));
|
|
|
|
}
|
|
|
|
options.application ||= options.fleet;
|
|
|
|
|
2020-07-30 08:39:05 +00:00
|
|
|
await Command.checkLoggedInIf(!!options.application);
|
|
|
|
|
|
|
|
(await import('events')).defaultMaxListeners = 1000;
|
|
|
|
|
|
|
|
const sdk = getBalenaSdk();
|
|
|
|
|
|
|
|
const logger = await Command.getLogger();
|
|
|
|
logger.logDebug('Parsing input...');
|
|
|
|
|
2020-08-14 15:41:56 +00:00
|
|
|
// `build` accepts `source` as a parameter, but compose expects it as an option
|
|
|
|
options.source = params.source;
|
|
|
|
delete params.source;
|
|
|
|
|
2020-07-30 08:39:05 +00:00
|
|
|
await this.validateOptions(options, sdk);
|
|
|
|
|
2021-04-21 05:26:57 +00:00
|
|
|
// Build args are under consideration for removal - warn user
|
|
|
|
if (options.buildArg) {
|
|
|
|
console.log(buildArgDeprecation);
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:39:05 +00:00
|
|
|
const app = await this.getAppAndResolveArch(options);
|
|
|
|
|
|
|
|
const { docker, buildOpts, composeOpts } = await this.prepareBuild(options);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.buildProject(docker, logger, composeOpts, {
|
|
|
|
app,
|
|
|
|
arch: options.arch!,
|
|
|
|
deviceType: options.deviceType!,
|
|
|
|
buildEmulated: options.emulated,
|
|
|
|
buildOpts,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
logger.logError('Build failed.');
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.outputDeferredMessages();
|
|
|
|
logger.logSuccess('Build succeeded!');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async validateOptions(opts: FlagsDef, sdk: BalenaSDK) {
|
|
|
|
// Validate option combinations
|
|
|
|
if (
|
|
|
|
(opts.application == null &&
|
|
|
|
(opts.arch == null || opts.deviceType == null)) ||
|
|
|
|
(opts.application != null &&
|
|
|
|
(opts.arch != null || opts.deviceType != null))
|
|
|
|
) {
|
|
|
|
const { ExpectedError } = await import('../errors');
|
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
'You must specify either a fleet (-f), or the device type (-d) and architecture (-A)',
|
2020-07-30 08:39:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate project directory
|
|
|
|
const { validateProjectDirectory } = await import('../utils/compose_ts');
|
|
|
|
const { dockerfilePath, registrySecrets } = await validateProjectDirectory(
|
|
|
|
sdk,
|
|
|
|
{
|
|
|
|
dockerfilePath: opts.dockerfile,
|
|
|
|
noParentCheck: opts['noparent-check'] || false,
|
|
|
|
projectPath: opts.source || '.',
|
|
|
|
registrySecretsPath: opts['registry-secrets'],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
opts.dockerfile = dockerfilePath;
|
|
|
|
opts['registry-secrets'] = registrySecrets;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async getAppAndResolveArch(opts: FlagsDef) {
|
|
|
|
if (opts.application) {
|
|
|
|
const { getAppWithArch } = await import('../utils/helpers');
|
|
|
|
const app = await getAppWithArch(opts.application);
|
|
|
|
opts.arch = app.arch;
|
2020-07-31 14:35:20 +00:00
|
|
|
opts.deviceType = app.is_for__device_type[0].slug;
|
2020-07-30 08:39:05 +00:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async prepareBuild(options: FlagsDef) {
|
|
|
|
const { getDocker, generateBuildOpts } = await import('../utils/docker');
|
|
|
|
const [docker, buildOpts, composeOpts] = await Promise.all([
|
|
|
|
getDocker(options),
|
|
|
|
generateBuildOpts(options),
|
|
|
|
compose.generateOpts(options),
|
|
|
|
]);
|
|
|
|
return {
|
|
|
|
docker,
|
|
|
|
buildOpts,
|
|
|
|
composeOpts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opts must be an object with the following keys:
|
|
|
|
* app: the app this build is for (optional)
|
|
|
|
* arch: the architecture to build for
|
|
|
|
* deviceType: the device type to build for
|
|
|
|
* buildEmulated
|
|
|
|
* buildOpts: arguments to forward to docker build command
|
|
|
|
*
|
|
|
|
* @param {DockerToolbelt} docker
|
|
|
|
* @param {Logger} logger
|
|
|
|
* @param {ComposeOpts} composeOpts
|
|
|
|
* @param opts
|
|
|
|
*/
|
|
|
|
protected async buildProject(
|
2021-04-27 13:58:54 +00:00
|
|
|
docker: import('dockerode'),
|
2020-07-30 08:39:05 +00:00
|
|
|
logger: import('../utils/logger'),
|
|
|
|
composeOpts: ComposeOpts,
|
|
|
|
opts: {
|
|
|
|
app?: Application;
|
|
|
|
arch: string;
|
|
|
|
deviceType: string;
|
|
|
|
buildEmulated: boolean;
|
2020-10-15 23:26:00 +00:00
|
|
|
buildOpts: BuildOpts;
|
2020-07-30 08:39:05 +00:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
const { loadProject } = await import('../utils/compose_ts');
|
|
|
|
|
2021-09-09 00:22:32 +00:00
|
|
|
const project = await loadProject(
|
|
|
|
logger,
|
|
|
|
composeOpts,
|
|
|
|
undefined,
|
|
|
|
opts.buildOpts.t,
|
|
|
|
);
|
2020-07-30 08:39:05 +00:00
|
|
|
|
|
|
|
const appType = (opts.app?.application_type as ApplicationType[])?.[0];
|
|
|
|
if (
|
|
|
|
appType != null &&
|
|
|
|
project.descriptors.length > 1 &&
|
|
|
|
!appType.supports_multicontainer
|
|
|
|
) {
|
|
|
|
logger.logWarn(
|
2021-07-15 13:41:38 +00:00
|
|
|
'Target fleet does not support multiple containers.\n' +
|
2020-07-30 08:39:05 +00:00
|
|
|
'Continuing with build, but you will not be able to deploy.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-15 23:26:00 +00:00
|
|
|
await buildProject({
|
2020-07-30 08:39:05 +00:00
|
|
|
docker,
|
|
|
|
logger,
|
2020-10-15 23:26:00 +00:00
|
|
|
projectPath: project.path,
|
|
|
|
projectName: project.name,
|
|
|
|
composition: project.composition,
|
|
|
|
arch: opts.arch,
|
|
|
|
deviceType: opts.deviceType,
|
|
|
|
emulated: opts.buildEmulated,
|
|
|
|
buildOpts: opts.buildOpts,
|
|
|
|
inlineLogs: composeOpts.inlineLogs,
|
|
|
|
convertEol: composeOpts.convertEol,
|
|
|
|
dockerfilePath: composeOpts.dockerfilePath,
|
2021-09-08 17:10:22 +00:00
|
|
|
nogitignore: composeOpts.nogitignore, // v13: delete this line
|
2020-10-15 23:26:00 +00:00
|
|
|
multiDockerignore: composeOpts.multiDockerignore,
|
|
|
|
});
|
2020-07-30 08:39:05 +00:00
|
|
|
}
|
|
|
|
}
|