Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a prebuilt image #252

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .resinci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
---
docker:
publish: false
builds:
- path: .
dockerfile: Dockerfile
docker_repo: balena/balena-preload
publish: true
klutchell marked this conversation as resolved.
Show resolved Hide resolved
platforms: [linux/amd64,linux/arm64,linux/arm/7,linux/arm/6]

npm:
platforms:
- name: linux
Expand Down
72 changes: 22 additions & 50 deletions lib/preload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-expect-error
const { version } = require('../package.json');
const _ = require('lodash');
const EventEmitter = require('events');
const dockerProgress = require('docker-progress');
Expand All @@ -21,10 +23,9 @@ const preload = module.exports;
const { R_OK, W_OK } = fs.constants;

const DOCKER_TMPDIR = '/docker_tmpdir';
const DOCKER_IMAGE_TAG = 'balena/balena-preload';
const DOCKER_IMAGE_TAG = `balena/balena-preload:v${version}`;
const DISK_IMAGE_PATH_IN_DOCKER = '/img/balena.img';
const SPLASH_IMAGE_PATH_IN_DOCKER = '/img/balena-logo.png';
const DOCKER_STEP_RE = /Step (\d+)\/(\d+)/;
const CONCURRENT_REQUESTS_TO_REGISTRY = 10;

const GRAPHDRIVER_ERROR =
Expand Down Expand Up @@ -130,15 +131,19 @@ const createContainer = async (
edisonFolder,
) => {
const mounts = [];
const version = await docker.version();
const dockerVersion = await docker.version();
if (os.platform() === 'linux') {
// In some situations, devices created by `losetup -f` in the container only appear on the host and not in the container.
// See https://github.com/balena-io/balena-cli/issues/1008
mounts.push(bindMount('/dev', '/dev', version.ApiVersion));
mounts.push(bindMount('/dev', '/dev', dockerVersion.ApiVersion));
}
if (splashImage) {
mounts.push(
bindMount(splashImage, SPLASH_IMAGE_PATH_IN_DOCKER, version.ApiVersion),
bindMount(
splashImage,
SPLASH_IMAGE_PATH_IN_DOCKER,
dockerVersion.ApiVersion,
),
);
}

Expand All @@ -154,11 +159,11 @@ const createContainer = async (
env.push(`PARTITIONS=${JSON.stringify(partitions)}`);
PARTITION_NAMES.forEach((name) => {
const part = partitions[name];
mounts.push(bindMount(part.file, part.image, version.ApiVersion));
mounts.push(bindMount(part.file, part.image, dockerVersion.ApiVersion));
});
} else {
mounts.push(
bindMount(image, DISK_IMAGE_PATH_IN_DOCKER, version.ApiVersion),
bindMount(image, DISK_IMAGE_PATH_IN_DOCKER, dockerVersion.ApiVersion),
);
}
const containerOptions = {
Expand All @@ -177,7 +182,7 @@ const createContainer = async (
};
// Before api 1.25 bind mounts were going to into HostConfig.Binds
containerOptions.HostConfig[
compareVersions(version.ApiVersion, '1.25') >= 0 ? 'Mounts' : 'Binds'
compareVersions(dockerVersion.ApiVersion, '1.25') >= 0 ? 'Mounts' : 'Binds'
] = mounts;
if (os.platform() === 'linux') {
containerOptions.HostConfig.NetworkMode = 'host';
Expand Down Expand Up @@ -257,52 +262,19 @@ class Preloader extends EventEmitter {
this.stderr.pipe(this.bufferedStderr); // TODO: split stderr and build output ?
}

/**
* Build the preloader docker image
* @returns Promise
*/
async _build() {
const files = ['Dockerfile', 'requirements.txt', 'src/preload.py'];
_build() {
const name = 'Building Docker preloader image.';
this._progress(name, 0);

const tarStream = tarfs.pack(path.resolve(__dirname, '..'), {
entries: files,
});
const build = await this.docker.buildImage(tarStream, {
t: DOCKER_IMAGE_TAG,
});
await new Bluebird((resolve, reject) => {
this.docker.modem.followProgress(
build,
(error, output) => {
// onFinished
if (!error && output && output.length) {
error = output.pop().error;
}
if (error) {
reject(error);
} else {
this._progress(name, 100);
resolve();
}
},
(event) => {
// onProgress
if (event.stream) {
const matches = event.stream.match(DOCKER_STEP_RE);
if (matches) {
this._progress(
name,
(parseInt(matches[1], 10) / (parseInt(matches[2], 10) + 1)) *
100,
);
}
this.stderr.write(event.stream);
}
},
);
const outerDockerProgress = new dockerProgress.DockerProgress({
docker: this.docker,
});
return outerDockerProgress.pull(
`balena/balena-preload:v${version}`,
(e) => {
this._progress(name, e.percentage);
},
);
}

async _fetchDeviceTypes() {
Expand Down