diff --git a/.resinci.yml b/.resinci.yml index d13898a..2b398dc 100644 --- a/.resinci.yml +++ b/.resinci.yml @@ -1,6 +1,12 @@ --- docker: - publish: false + builds: + - path: . + dockerfile: Dockerfile + docker_repo: balena/balena-preload + publish: true + platforms: [linux/amd64,linux/arm64,linux/arm/7,linux/arm/6] + npm: platforms: - name: linux diff --git a/lib/preload.js b/lib/preload.js index d0b5252..d46c5ff 100644 --- a/lib/preload.js +++ b/lib/preload.js @@ -1,3 +1,5 @@ +// @ts-expect-error +const { version } = require('../package.json'); const _ = require('lodash'); const EventEmitter = require('events'); const dockerProgress = require('docker-progress'); @@ -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 = @@ -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, + ), ); } @@ -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 = { @@ -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'; @@ -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() {