2018-04-25 14:20:07 +00:00
|
|
|
/*
|
2018-11-07 18:15:05 +00:00
|
|
|
Copyright 2016-2018 Balena Ltd.
|
2018-04-25 14:20:07 +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.
|
|
|
|
*/
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
import { BalenaSDK } from 'balena-sdk';
|
2018-04-25 14:20:07 +00:00
|
|
|
import { CommandDefinition } from 'capitano';
|
|
|
|
import { stripIndent } from 'common-tags';
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2019-02-27 18:01:47 +00:00
|
|
|
import { registrySecretsHelp } from '../utils/messages';
|
2019-04-24 09:06:53 +00:00
|
|
|
import {
|
|
|
|
validateApplicationName,
|
|
|
|
validateIPAddress,
|
|
|
|
} from '../utils/validation';
|
2018-10-16 10:25:37 +00:00
|
|
|
|
|
|
|
enum BuildTarget {
|
|
|
|
Cloud,
|
|
|
|
Device,
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBuildTarget(appOrDevice: string): BuildTarget | null {
|
|
|
|
// First try the application regex from the api
|
2019-04-24 09:06:53 +00:00
|
|
|
if (validateApplicationName(appOrDevice)) {
|
2018-10-16 10:25:37 +00:00
|
|
|
return BuildTarget.Cloud;
|
|
|
|
}
|
|
|
|
|
2019-04-24 09:06:53 +00:00
|
|
|
if (validateIPAddress(appOrDevice)) {
|
2018-10-16 10:25:37 +00:00
|
|
|
return BuildTarget.Device;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-25 14:20:07 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
async function getAppOwner(sdk: BalenaSDK, appName: string) {
|
2019-04-18 13:50:49 +00:00
|
|
|
const { exitWithExpectedError, selectFromList } = await import(
|
|
|
|
'../utils/patterns'
|
|
|
|
);
|
2018-04-25 14:20:07 +00:00
|
|
|
const _ = await import('lodash');
|
|
|
|
|
|
|
|
const applications = await sdk.models.application.getAll({
|
|
|
|
$expand: {
|
|
|
|
user: {
|
|
|
|
$select: ['username'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
$filter: {
|
2018-11-15 09:26:25 +00:00
|
|
|
$eq: [{ $tolower: { $: 'app_name' } }, appName.toLowerCase()],
|
2018-04-25 14:20:07 +00:00
|
|
|
},
|
|
|
|
$select: ['id'],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (applications == null || applications.length === 0) {
|
|
|
|
exitWithExpectedError(
|
|
|
|
stripIndent`
|
|
|
|
No applications found with name: ${appName}.
|
|
|
|
|
|
|
|
This could mean that the application does not exist, or you do
|
|
|
|
not have the permissions required to access it.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (applications.length === 1) {
|
|
|
|
return _.get(applications, '[0].user[0].username');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got more than one application with the same name it means that the
|
|
|
|
// user has access to a collab app with the same name as a personal app. We
|
|
|
|
// present a list to the user which shows the fully qualified application
|
|
|
|
// name (user/appname) and allows them to select
|
|
|
|
const entries = _.map(applications, app => {
|
|
|
|
const username = _.get(app, 'user[0].username');
|
|
|
|
return {
|
|
|
|
name: `${username}/${appName}`,
|
|
|
|
extra: username,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const selected = await selectFromList(
|
|
|
|
`${
|
|
|
|
entries.length
|
|
|
|
} applications found with that name, please select the application you would like to push to`,
|
|
|
|
entries,
|
|
|
|
);
|
|
|
|
|
|
|
|
return selected.extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const push: CommandDefinition<
|
|
|
|
{
|
2018-10-16 10:25:37 +00:00
|
|
|
applicationOrDevice: string;
|
2018-04-25 14:20:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
source: string;
|
2018-06-21 13:29:25 +00:00
|
|
|
emulated: boolean;
|
2019-04-11 11:49:19 +00:00
|
|
|
dockerfile: string; // DeviceDeployOptions.dockerfilePath (alternative Dockerfile)
|
2018-06-21 13:29:25 +00:00
|
|
|
nocache: boolean;
|
2018-11-07 18:15:05 +00:00
|
|
|
'registry-secrets': string;
|
2019-01-14 16:51:57 +00:00
|
|
|
live: boolean;
|
2019-04-24 13:31:30 +00:00
|
|
|
detached: boolean;
|
2018-04-25 14:20:07 +00:00
|
|
|
}
|
|
|
|
> = {
|
2018-10-16 10:25:37 +00:00
|
|
|
signature: 'push <applicationOrDevice>',
|
2019-04-02 17:01:28 +00:00
|
|
|
primary: true,
|
2018-10-16 10:25:37 +00:00
|
|
|
description:
|
2018-10-19 14:38:50 +00:00
|
|
|
'Start a remote build on the balena cloud build servers or a local mode device',
|
2018-04-25 14:20:07 +00:00
|
|
|
help: stripIndent`
|
2019-01-16 19:54:46 +00:00
|
|
|
This command can be used to start a build on the remote balena cloud builders,
|
|
|
|
or a local mode balena device.
|
|
|
|
|
2019-02-27 18:01:47 +00:00
|
|
|
When building on the balenaCloud servers, the given source directory will be
|
|
|
|
sent to the remote server. This can be used as a drop-in replacement for the
|
|
|
|
"git push" deployment method.
|
2018-11-07 18:15:05 +00:00
|
|
|
|
2019-04-23 10:38:13 +00:00
|
|
|
When building on a local mode device, the given source directory will be
|
2019-02-27 18:01:47 +00:00
|
|
|
built on the device, and the resulting containers will be run on the device.
|
|
|
|
Logs will be streamed back from the device as part of the same invocation.
|
|
|
|
The web dashboard can be used to switch a device to local mode:
|
|
|
|
https://www.balena.io/docs/learn/develop/local-mode/
|
2019-04-23 10:38:13 +00:00
|
|
|
Note that local mode requires a supervisor version of at least v7.21.0.
|
|
|
|
|
|
|
|
It is also possible to run a push to a local mode device in live mode.
|
|
|
|
This will watch for changes in the source directory and perform an
|
|
|
|
in-place build in the running containers [BETA].
|
2018-11-07 18:15:05 +00:00
|
|
|
|
2019-02-27 18:01:47 +00:00
|
|
|
${registrySecretsHelp.split('\n').join('\n\t\t')}
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2018-04-25 14:20:07 +00:00
|
|
|
Examples:
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena push myApp
|
|
|
|
$ balena push myApp --source <source directory>
|
|
|
|
$ balena push myApp -s <source directory>
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena push 10.0.0.1
|
|
|
|
$ balena push 10.0.0.1 --source <source directory>
|
|
|
|
$ balena push 10.0.0.1 -s <source directory>
|
2018-04-25 14:20:07 +00:00
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'source',
|
|
|
|
alias: 's',
|
|
|
|
description:
|
2018-10-19 14:38:50 +00:00
|
|
|
'The source that should be sent to the balena builder to be built (defaults to the current directory)',
|
2018-04-25 14:20:07 +00:00
|
|
|
parameter: 'source',
|
|
|
|
},
|
2018-06-21 13:29:25 +00:00
|
|
|
{
|
|
|
|
signature: 'emulated',
|
|
|
|
alias: 'e',
|
|
|
|
description: 'Force an emulated build to occur on the remote builder',
|
|
|
|
boolean: true,
|
|
|
|
},
|
2019-04-11 11:49:19 +00:00
|
|
|
{
|
|
|
|
signature: 'dockerfile',
|
|
|
|
parameter: 'Dockerfile',
|
|
|
|
description:
|
|
|
|
'Alternative Dockerfile name/path, relative to the source folder',
|
|
|
|
},
|
2018-06-21 13:29:25 +00:00
|
|
|
{
|
|
|
|
signature: 'nocache',
|
|
|
|
alias: 'c',
|
|
|
|
description: "Don't use cache when building this project",
|
|
|
|
boolean: true,
|
|
|
|
},
|
2018-11-07 18:15:05 +00:00
|
|
|
{
|
|
|
|
signature: 'registry-secrets',
|
|
|
|
alias: 'R',
|
|
|
|
parameter: 'secrets.yml|.json',
|
|
|
|
description: stripIndent`
|
|
|
|
Path to a local YAML or JSON file containing Docker registry passwords used to pull base images`,
|
|
|
|
},
|
2019-01-14 16:51:57 +00:00
|
|
|
{
|
|
|
|
signature: 'live',
|
|
|
|
alias: 'l',
|
|
|
|
boolean: true,
|
|
|
|
description: stripIndent`
|
2019-04-23 10:38:13 +00:00
|
|
|
Note this feature is in beta.
|
|
|
|
|
2019-04-24 13:31:30 +00:00
|
|
|
Start a live session with the containers pushed to a local mode device.
|
2019-04-23 10:38:13 +00:00
|
|
|
The project source folder is watched for filesystem events, and changes
|
|
|
|
to files and folders are automatically synchronized to the running
|
|
|
|
containers. The synchronisation is only in one direction, from this machine to
|
|
|
|
the device, and changes made on the device itself may be overwritten.
|
|
|
|
This feature requires a device running supervisor version v9.7.0 or greater.`,
|
2019-01-14 16:51:57 +00:00
|
|
|
},
|
2019-04-24 13:31:30 +00:00
|
|
|
{
|
|
|
|
signature: 'detached',
|
|
|
|
alias: 'd',
|
|
|
|
description: `Don't tail application logs when pushing to a local mode device`,
|
|
|
|
boolean: true,
|
|
|
|
},
|
2018-04-25 14:20:07 +00:00
|
|
|
],
|
|
|
|
async action(params, options, done) {
|
2018-10-19 14:38:50 +00:00
|
|
|
const sdk = (await import('balena-sdk')).fromSharedOptions();
|
2018-04-25 14:20:07 +00:00
|
|
|
const Bluebird = await import('bluebird');
|
|
|
|
const remote = await import('../utils/remote-build');
|
2018-10-16 10:25:37 +00:00
|
|
|
const deviceDeploy = await import('../utils/device/deploy');
|
2019-04-18 13:50:49 +00:00
|
|
|
const { exitIfNotLoggedIn, exitWithExpectedError } = await import(
|
|
|
|
'../utils/patterns'
|
|
|
|
);
|
2019-04-11 11:49:19 +00:00
|
|
|
const { validateSpecifiedDockerfile, parseRegistrySecrets } = await import(
|
|
|
|
'../utils/compose_ts'
|
|
|
|
);
|
2019-02-26 13:32:27 +00:00
|
|
|
const { BuildError } = await import('../utils/device/errors');
|
2018-04-25 14:20:07 +00:00
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
const appOrDevice: string | null = params.applicationOrDevice;
|
|
|
|
if (appOrDevice == null) {
|
|
|
|
exitWithExpectedError('You must specify an application or a device');
|
2018-04-25 14:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const source = options.source || '.';
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
console.log(`[debug] Using ${source} as build source`);
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:49:19 +00:00
|
|
|
const dockerfilePath = validateSpecifiedDockerfile(
|
|
|
|
source,
|
|
|
|
options.dockerfile,
|
|
|
|
);
|
|
|
|
|
2018-11-07 18:15:05 +00:00
|
|
|
const registrySecrets = options['registry-secrets']
|
|
|
|
? await parseRegistrySecrets(options['registry-secrets'])
|
|
|
|
: {};
|
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
const buildTarget = getBuildTarget(appOrDevice);
|
|
|
|
switch (buildTarget) {
|
|
|
|
case BuildTarget.Cloud:
|
2019-01-14 16:51:57 +00:00
|
|
|
// Ensure that the live argument has not been passed to a cloud build
|
|
|
|
if (options.live) {
|
|
|
|
exitWithExpectedError(
|
2019-04-24 13:31:30 +00:00
|
|
|
'The --live flag is only valid when pushing to a local mode device',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (options.detached) {
|
|
|
|
exitWithExpectedError(
|
|
|
|
`The --detached flag is only valid when pushing to a local mode device.`,
|
2019-01-14 16:51:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
const app = appOrDevice;
|
2019-03-25 15:16:23 +00:00
|
|
|
await exitIfNotLoggedIn();
|
2019-03-04 15:06:31 +00:00
|
|
|
await Bluebird.join(
|
2018-10-16 10:25:37 +00:00
|
|
|
sdk.auth.getToken(),
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk.settings.get('balenaUrl'),
|
2018-10-16 10:25:37 +00:00
|
|
|
getAppOwner(sdk, app),
|
2019-03-04 15:06:31 +00:00
|
|
|
async (token, baseUrl, owner) => {
|
2018-10-16 10:25:37 +00:00
|
|
|
const opts = {
|
2019-04-11 11:49:19 +00:00
|
|
|
dockerfilePath,
|
2018-10-16 10:25:37 +00:00
|
|
|
emulated: options.emulated,
|
|
|
|
nocache: options.nocache,
|
2018-11-07 18:15:05 +00:00
|
|
|
registrySecrets,
|
2018-10-16 10:25:37 +00:00
|
|
|
};
|
|
|
|
const args = {
|
|
|
|
app,
|
|
|
|
owner,
|
|
|
|
source,
|
|
|
|
auth: token,
|
|
|
|
baseUrl,
|
|
|
|
sdk,
|
|
|
|
opts,
|
|
|
|
};
|
|
|
|
|
2019-03-04 15:06:31 +00:00
|
|
|
return await remote.startRemoteBuild(args);
|
2018-10-16 10:25:37 +00:00
|
|
|
},
|
|
|
|
).nodeify(done);
|
|
|
|
break;
|
|
|
|
case BuildTarget.Device:
|
|
|
|
const device = appOrDevice;
|
|
|
|
// TODO: Support passing a different port
|
2019-03-04 15:06:31 +00:00
|
|
|
await Bluebird.resolve(
|
2018-10-16 10:25:37 +00:00
|
|
|
deviceDeploy.deployToDevice({
|
|
|
|
source,
|
|
|
|
deviceHost: device,
|
2019-04-11 11:49:19 +00:00
|
|
|
dockerfilePath,
|
2018-11-07 18:15:05 +00:00
|
|
|
registrySecrets,
|
2019-03-28 12:20:08 +00:00
|
|
|
nocache: options.nocache || false,
|
2019-01-14 16:51:57 +00:00
|
|
|
live: options.live || false,
|
2019-04-24 13:31:30 +00:00
|
|
|
detached: options.detached || false,
|
2018-10-16 10:25:37 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.catch(BuildError, e => {
|
|
|
|
exitWithExpectedError(e.toString());
|
|
|
|
})
|
|
|
|
.nodeify(done);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exitWithExpectedError(
|
|
|
|
stripIndent`
|
|
|
|
Build target not recognised. Please provide either an application name or device address.
|
|
|
|
|
|
|
|
The only supported device addresses currently are IP addresses.
|
|
|
|
|
|
|
|
If you believe your build target should have been detected, and this is an error, please
|
|
|
|
create an issue.`,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-25 14:20:07 +00:00
|
|
|
},
|
|
|
|
};
|