detached build initial commit

This commit is contained in:
Kasra Bigdeli
2018-01-06 17:46:13 -08:00
parent d0fa23265f
commit cfbb386987
5 changed files with 317 additions and 57 deletions
+104 -8
View File
@@ -131,7 +131,6 @@ function savePropForDirectory(propType, propValue) {
function getDefaultMachine() {
let machine = getPropForDirectory(MACHINE_TO_DEPLOY);
console.log(machine);
if (machine) {
return machine.name;
}
@@ -227,15 +226,15 @@ if (!program.default || defaultInvalid) {
function deployTo(machineToDeploy, branchToPush, appName) {
if (!commandExistsSync('git')) {
console.log(chalk.red('"git" command not found...'));
console.log(chalk.red('Captain needs "git" to create zip file of your source files...'));
console.log(chalk.red('Captain needs "git" to create tar file of your source files...'));
console.log(' ');
process.exit(1);
}
let zipFileNameToDeploy = 'temporary-captain-to-deploy.zip';
let zipFileNameToDeploy = 'temporary-captain-to-deploy.tar';
let zipFileFullPath = path.join(process.cwd(), zipFileNameToDeploy);
console.log('Saving zip file to:');
console.log('Saving tar file to:');
console.log(zipFileFullPath);
console.log(' ');
@@ -298,7 +297,7 @@ function sendFileToCaptain(machineToDeploy, zipFileFullPath, appName, gitHash, b
let options = {
url: machineToDeploy.baseUrl + '/api/v1/user/appData/' + appName,
url: machineToDeploy.baseUrl + '/api/v1/user/appData/' + appName + '/?detached=1',
headers: {
'x-namespace': 'captain',
'x-captain-auth': machineToDeploy.authToken
@@ -337,7 +336,7 @@ function sendFileToCaptain(machineToDeploy, zipFileFullPath, appName, gitHash, b
return;
}
if (data.status !== 100) {
if (data.status !== 100 && data.status !== 101) {
throw new Error(JSON.stringify(data, null, 2));
}
@@ -345,8 +344,14 @@ function sendFileToCaptain(machineToDeploy, zipFileFullPath, appName, gitHash, b
savePropForDirectory(BRANCH_TO_PUSH, branchToPush);
savePropForDirectory(MACHINE_TO_DEPLOY, machineToDeploy);
console.log(chalk.green('Deployed successful: ') + appName);
console.log(' ');
if (data.status === 100) {
console.log(chalk.green('Deployed successful: ') + appName);
console.log(' ');
} else if (data.status === 101) {
console.log(chalk.green('Building started: ') + appName);
console.log(' ');
startFetchingBuildLogs(machineToDeploy, appName);
}
return;
}
@@ -384,6 +389,97 @@ function sendFileToCaptain(machineToDeploy, zipFileFullPath, appName, gitHash, b
}
var lastLineNumberPrinted = -10000; // we want to show all lines to begin with!
function startFetchingBuildLogs(machineToDeploy, appName) {
let options = {
url: machineToDeploy.baseUrl + '/api/v1/user/appData/' + appName,
headers: {
'x-namespace': 'captain',
'x-captain-auth': machineToDeploy.authToken
},
method: 'GET'
};
function onLogRetrieved(data) {
if (data) {
var lines = data.logs.lines;
var firstLineNumberOfLogs = data.logs.firstLineNumber;
var firstLinesToPrint = 0;
if (firstLineNumberOfLogs > lastLineNumberPrinted) {
if (firstLineNumberOfLogs < 0) {
// This is the very first fetch, probably firstLineNumberOfLogs is around -50
firstLinesToPrint = -firstLineNumberOfLogs;
} else {
console.log('[[ TRUNCATED ]]');
}
} else {
firstLinesToPrint = lastLineNumberPrinted - firstLineNumberOfLogs;
}
lastLineNumberPrinted = firstLineNumberOfLogs + lines.length;
for (var i = firstLinesToPrint; i < lines.length; i++) {
console.log((lines[i] || '').trim());
}
}
if (data && !data.isAppBuilding) {
console.log(' ');
if (!data.isBuildFailed) {
console.log(chalk.green('Deployed successful: ') + appName);
console.log(chalk.magenta('App is available at ') + (machineToDeploy.baseUrl.replace('//captain.', '//' + appName + '.')));
} else {
console.error(chalk.red('\nSomething bad happened. Cannot deploy "' + appName + '"\n'));
}
console.log(' ');
return;
}
setTimeout(function () {
startFetchingBuildLogs(machineToDeploy, appName);
}, 2000);
}
function callback(error, response, body) {
try {
if (!error && response.statusCode === 200) {
let data = JSON.parse(body);
if (data.status !== 100) {
throw new Error(JSON.stringify(data, null, 2));
}
onLogRetrieved(data.data);
return;
}
if (error) {
throw new Error(error)
}
throw new Error(response ? JSON.stringify(response, null, 2) : 'Response NULL');
} catch (error) {
console.error(chalk.red('\nSomething while retrieving app build logs.. "' + error + '"\n'));
onLogRetrieved(null);
}
}
request(options, callback);
}
function requestLogin(serverName, serverAddress, loginCallback) {
console.log('Your auth token is not valid anymore. Try to login again.');