2020-04-27 13:10:03 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-23 10:29:03 +00:00
|
|
|
import * as Bluebird from 'bluebird';
|
2020-04-27 13:10:03 +00:00
|
|
|
import { getVisuals } from './lazy';
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const getBuilderPushEndpoint = function (baseUrl, owner, app) {
|
2020-04-27 13:10:03 +00:00
|
|
|
const querystring = require('querystring');
|
|
|
|
const args = querystring.stringify({ owner, app });
|
|
|
|
return `https://builder.${baseUrl}/v1/push?${args}`;
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const getBuilderLogPushEndpoint = function (baseUrl, buildId, owner, app) {
|
2020-04-27 13:10:03 +00:00
|
|
|
const querystring = require('querystring');
|
|
|
|
const args = querystring.stringify({ owner, app, buildId });
|
|
|
|
return `https://builder.${baseUrl}/v1/pushLogs?${args}`;
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const bufferImage = function (docker, imageId, bufferFile) {
|
2020-04-27 13:10:03 +00:00
|
|
|
const streamUtils = require('./streams');
|
|
|
|
|
|
|
|
const image = docker.getImage(imageId);
|
|
|
|
const imageMetadata = image.inspect();
|
|
|
|
|
2020-06-23 10:29:03 +00:00
|
|
|
return Bluebird.join(
|
2020-04-27 13:10:03 +00:00
|
|
|
image.get(),
|
|
|
|
imageMetadata.get('Size'),
|
|
|
|
(imageStream, imageSize) =>
|
2020-06-15 22:53:07 +00:00
|
|
|
streamUtils.buffer(imageStream, bufferFile).tap((bufferedStream) => {
|
2020-04-27 13:10:03 +00:00
|
|
|
// @ts-ignore adding an extra property
|
|
|
|
bufferedStream.length = imageSize;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const showPushProgress = function (message) {
|
2020-04-27 13:10:03 +00:00
|
|
|
const visuals = getVisuals();
|
|
|
|
const progressBar = new visuals.Progress(message);
|
|
|
|
progressBar.update({ percentage: 0 });
|
|
|
|
return progressBar;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uploadToPromise = (uploadRequest, logger) =>
|
2020-06-23 10:29:03 +00:00
|
|
|
new Bluebird(function (resolve, reject) {
|
2020-06-15 22:53:07 +00:00
|
|
|
const handleMessage = function (data) {
|
2020-04-27 13:10:03 +00:00
|
|
|
let obj;
|
|
|
|
data = data.toString();
|
|
|
|
logger.logDebug(`Received data: ${data}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
logger.logError('Error parsing reply from remote side');
|
|
|
|
reject(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (obj.type) {
|
|
|
|
case 'error':
|
|
|
|
reject(new Error(`Remote error: ${obj.error}`));
|
|
|
|
break;
|
|
|
|
case 'success':
|
|
|
|
resolve(obj);
|
|
|
|
break;
|
|
|
|
case 'status':
|
|
|
|
logger.logInfo(obj.message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reject(new Error(`Received unexpected reply from remote: ${data}`));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uploadRequest.on('error', reject).on('data', handleMessage);
|
|
|
|
});
|
|
|
|
|
2020-04-27 14:10:31 +00:00
|
|
|
/**
|
2020-06-23 10:29:03 +00:00
|
|
|
* @returns {Bluebird<{ buildId: number }>}
|
2020-04-27 14:10:31 +00:00
|
|
|
*/
|
2020-06-15 22:53:07 +00:00
|
|
|
const uploadImage = function (
|
2020-04-27 13:10:03 +00:00
|
|
|
imageStream,
|
|
|
|
token,
|
|
|
|
username,
|
|
|
|
url,
|
|
|
|
appName,
|
|
|
|
logger,
|
|
|
|
) {
|
|
|
|
const request = require('request');
|
|
|
|
const progressStream = require('progress-stream');
|
|
|
|
const zlib = require('zlib');
|
|
|
|
|
|
|
|
// Need to strip off the newline
|
|
|
|
const progressMessage = logger
|
|
|
|
.formatMessage('info', 'Uploading')
|
|
|
|
.slice(0, -1);
|
|
|
|
const progressBar = showPushProgress(progressMessage);
|
|
|
|
const streamWithProgress = imageStream.pipe(
|
|
|
|
progressStream(
|
|
|
|
{
|
|
|
|
time: 500,
|
|
|
|
length: imageStream.length,
|
|
|
|
},
|
|
|
|
({ percentage, eta }) =>
|
|
|
|
progressBar.update({
|
|
|
|
percentage: Math.min(percentage, 100),
|
|
|
|
eta,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const uploadRequest = request.post({
|
|
|
|
url: getBuilderPushEndpoint(url, username, appName),
|
|
|
|
headers: {
|
|
|
|
'Content-Encoding': 'gzip',
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
bearer: token,
|
|
|
|
},
|
|
|
|
body: streamWithProgress.pipe(
|
|
|
|
zlib.createGzip({
|
|
|
|
level: 6,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
return uploadToPromise(uploadRequest, logger);
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const uploadLogs = function (logs, token, url, buildId, username, appName) {
|
2020-04-27 13:10:03 +00:00
|
|
|
const request = require('request');
|
|
|
|
return request.post({
|
|
|
|
json: true,
|
|
|
|
url: getBuilderLogPushEndpoint(url, buildId, username, appName),
|
|
|
|
auth: {
|
|
|
|
bearer: token,
|
|
|
|
},
|
|
|
|
body: Buffer.from(logs),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
opts must be a hash with the following keys:
|
|
|
|
|
|
|
|
- appName: the name of the app to deploy to
|
|
|
|
- imageName: the name of the image to deploy
|
|
|
|
- buildLogs: a string with build output
|
|
|
|
- shouldUploadLogs
|
|
|
|
*/
|
2020-06-15 22:53:07 +00:00
|
|
|
export const deployLegacy = function (
|
2020-04-27 13:10:03 +00:00
|
|
|
docker,
|
|
|
|
logger,
|
|
|
|
token,
|
|
|
|
username,
|
|
|
|
url,
|
|
|
|
opts,
|
|
|
|
) {
|
|
|
|
const tmp = require('tmp');
|
2020-06-23 10:29:03 +00:00
|
|
|
const tmpNameAsync = Bluebird.promisify(tmp.tmpName);
|
2020-04-27 13:10:03 +00:00
|
|
|
|
|
|
|
// Ensure the tmp files gets deleted
|
|
|
|
tmp.setGracefulCleanup();
|
|
|
|
|
|
|
|
const { appName, imageName, buildLogs, shouldUploadLogs } = opts;
|
|
|
|
const logs = buildLogs;
|
|
|
|
|
|
|
|
return tmpNameAsync()
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (bufferFile) {
|
2020-04-27 13:10:03 +00:00
|
|
|
logger.logInfo('Initializing deploy...');
|
|
|
|
return bufferImage(docker, imageName, bufferFile)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((stream) =>
|
2020-04-27 13:10:03 +00:00
|
|
|
uploadImage(stream, token, username, url, appName, logger),
|
|
|
|
)
|
|
|
|
.finally(() =>
|
|
|
|
// If the file was never written to (for instance because an error
|
|
|
|
// has occured before any data was written) this call will throw an
|
|
|
|
// ugly error, just suppress it
|
2020-06-23 10:29:03 +00:00
|
|
|
Bluebird.try(() => require('mz/fs').unlink(bufferFile)).catchReturn(
|
2020-04-27 13:10:03 +00:00
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.tap(function ({ buildId }) {
|
2020-04-27 13:10:03 +00:00
|
|
|
if (!shouldUploadLogs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.logInfo('Uploading logs...');
|
2020-06-23 10:29:03 +00:00
|
|
|
return Bluebird.join(
|
2020-04-27 13:10:03 +00:00
|
|
|
logs,
|
|
|
|
token,
|
|
|
|
url,
|
|
|
|
buildId,
|
|
|
|
username,
|
|
|
|
appName,
|
|
|
|
uploadLogs,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.get('buildId');
|
|
|
|
};
|