mirror of
https://github.com/caprover/caprover
synced 2026-08-07 08:10:56 +00:00
162 lines
7.8 KiB
JavaScript
Executable File
162 lines
7.8 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const fs = require("fs-extra");
|
|
const path = require("path");
|
|
const child_process_1 = require("child_process");
|
|
const StdOutUtil_1 = require("../utils/StdOutUtil");
|
|
const ProgressBar = require('progress');
|
|
const commandExistsSync = require('command-exists').sync;
|
|
const ValidationsHandler_1 = require("../utils/ValidationsHandler");
|
|
const CliApiManager_1 = require("../api/CliApiManager");
|
|
const SpinnerHelper_1 = require("../utils/SpinnerHelper");
|
|
class DeployHelper {
|
|
constructor(machineToDeploy, appName, branchToPush) {
|
|
this.machineToDeploy = machineToDeploy;
|
|
this.appName = appName;
|
|
this.branchToPush = branchToPush;
|
|
this.lastLineNumberPrinted = -10000; // we want to show all lines to begin with!
|
|
//
|
|
}
|
|
gitArchiveFile(zipFileFullPath) {
|
|
const self = this;
|
|
return new Promise(function (resolve, reject) {
|
|
child_process_1.exec(`git archive --format tar --output "${zipFileFullPath}" ${self.branchToPush}`, (err, stdout, stderr) => {
|
|
if (err) {
|
|
StdOutUtil_1.default.printError(`TAR file failed\n${err}\n`);
|
|
fs.removeSync(zipFileFullPath);
|
|
reject(new Error('TAR file failed'));
|
|
return;
|
|
}
|
|
child_process_1.exec(`git rev-parse ${self.branchToPush}`, (err, stdout, stderr) => {
|
|
const gitHash = (stdout || '').trim();
|
|
if (err || !/^[a-f0-9]{40}$/.test(gitHash)) {
|
|
StdOutUtil_1.default.printError(`Cannot find hash of last commit on this branch: ${self.branchToPush}\n${gitHash}\n${err}\n`);
|
|
reject(new Error('rev-parse failed'));
|
|
return;
|
|
}
|
|
StdOutUtil_1.default.printMessage(`Pushing last commit on ${self.branchToPush}: ${gitHash}`);
|
|
resolve();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
getFileStream(zipFileFullPath) {
|
|
const fileSize = fs.statSync(zipFileFullPath).size;
|
|
const fileStream = fs.createReadStream(zipFileFullPath);
|
|
const barOpts = {
|
|
width: 20,
|
|
total: fileSize,
|
|
clear: false
|
|
};
|
|
const bar = new ProgressBar(' uploading [:bar] :percent (ETA :etas)', barOpts);
|
|
fileStream.on('data', (chunk) => {
|
|
bar.tick(chunk.length);
|
|
});
|
|
fileStream.on('end', () => {
|
|
StdOutUtil_1.default.printMessage('This might take several minutes. PLEASE BE PATIENT...');
|
|
SpinnerHelper_1.default.start('Building your source code...');
|
|
SpinnerHelper_1.default.setColor('yellow');
|
|
});
|
|
return fileStream;
|
|
}
|
|
deployFromGitProject() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const appName = this.appName;
|
|
const branchToPush = this.branchToPush;
|
|
const machineToDeploy = this.machineToDeploy;
|
|
if (!appName || !branchToPush || !machineToDeploy) {
|
|
StdOutUtil_1.default.printError('Default deploy failed. Missing appName or branchToPush or machineToDeploy.', true);
|
|
return;
|
|
}
|
|
if (!commandExistsSync('git')) {
|
|
StdOutUtil_1.default.printError("'git' command not found...");
|
|
StdOutUtil_1.default.printError("Captain needs 'git' to create tar file of your source files...", true);
|
|
return;
|
|
}
|
|
StdOutUtil_1.default.printMessage(`Deploying ${appName} to ${machineToDeploy.name}`);
|
|
yield ValidationsHandler_1.ensureAuthentication(machineToDeploy);
|
|
const zipFileNameToDeploy = 'temporary-captain-to-deploy.tar';
|
|
const zipFileFullPath = path.join(process.cwd(), zipFileNameToDeploy);
|
|
StdOutUtil_1.default.printMessage(`Saving tar file to:\n${zipFileFullPath}\n`);
|
|
// Removes the temporary file created
|
|
if (fs.pathExistsSync(zipFileFullPath))
|
|
fs.removeSync(zipFileFullPath);
|
|
yield this.gitArchiveFile(zipFileFullPath);
|
|
try {
|
|
StdOutUtil_1.default.printMessage(`Uploading the file to ${machineToDeploy.baseUrl}`);
|
|
yield CliApiManager_1.default.get(machineToDeploy).uploadAppData(appName, this.getFileStream(zipFileFullPath));
|
|
StdOutUtil_1.default.printMessage(`Upload done.`);
|
|
fs.removeSync(zipFileFullPath);
|
|
this.startFetchingBuildLogs(machineToDeploy, appName);
|
|
}
|
|
catch (e) {
|
|
if (fs.pathExistsSync(zipFileFullPath))
|
|
fs.removeSync(zipFileFullPath);
|
|
throw e;
|
|
}
|
|
});
|
|
}
|
|
onLogRetrieved(data, machineToDeploy, appName) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
if (data) {
|
|
const lines = data.logs.lines;
|
|
const firstLineNumberOfLogs = data.logs.firstLineNumber;
|
|
let firstLinesToPrint = 0;
|
|
if (firstLineNumberOfLogs > this.lastLineNumberPrinted) {
|
|
if (firstLineNumberOfLogs < 0) {
|
|
// This is the very first fetch, probably firstLineNumberOfLogs is around -50
|
|
firstLinesToPrint = -firstLineNumberOfLogs;
|
|
}
|
|
else {
|
|
StdOutUtil_1.default.printMessage('[[ TRUNCATED ]]');
|
|
}
|
|
}
|
|
else {
|
|
firstLinesToPrint = this.lastLineNumberPrinted - firstLineNumberOfLogs;
|
|
}
|
|
this.lastLineNumberPrinted = firstLineNumberOfLogs + lines.length;
|
|
for (let i = firstLinesToPrint; i < lines.length; i++) {
|
|
StdOutUtil_1.default.printMessage((lines[i] || '').trim());
|
|
}
|
|
}
|
|
if (data && !data.isAppBuilding) {
|
|
if (!data.isBuildFailed) {
|
|
StdOutUtil_1.default.printGreenMessage(`Deployed successfully: ${appName}`);
|
|
StdOutUtil_1.default.printMagentaMessage(`App is available`, true); // TODO add url
|
|
}
|
|
else {
|
|
StdOutUtil_1.default.printError(`\nSomething bad happened. Cannot deploy "${appName}"\n`, true);
|
|
}
|
|
}
|
|
else {
|
|
setTimeout(() => {
|
|
this.startFetchingBuildLogs(machineToDeploy, appName);
|
|
}, 2000);
|
|
}
|
|
});
|
|
}
|
|
startFetchingBuildLogs(machineToDeploy, appName) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const self = this;
|
|
try {
|
|
const data = yield CliApiManager_1.default.get(machineToDeploy).fetchBuildLogs(appName);
|
|
this.onLogRetrieved(data, machineToDeploy, appName);
|
|
}
|
|
catch (error) {
|
|
StdOutUtil_1.default.printError(`\nSomething while retrieving app build logs.. ${error}\n`);
|
|
this.onLogRetrieved(undefined, machineToDeploy, appName);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
exports.default = DeployHelper;
|
|
//# sourceMappingURL=DeployHelper.js.map
|