@pradyumn-el/pollycli
Version:
pollycli lets users access the functionalities of Polly over a command line interface
172 lines (151 loc) • 6.57 kB
JavaScript
const axios = require('axios');
const fs = require('fs');
const pollymsg = require("./message");
import {
dockerBuild,
dockerBuildStatus,
getAllDockerRepos,
getAllDockerCommits,
getWorkSpaceId,
createADockerRepo
} from './docker';
const pollyEnv = require('./env.json');
const git = require('simple-git');
const rmdir = require('rimraf');
const path = require('path');
import { organizationDetails } from './organization';
export async function generateNotebookTemplate(templateType, notebookName) {
const organizationInfo = await organizationDetails();
const orgSlug = organizationInfo.slug;
if (fs.existsSync(path.resolve(`./${notebookName}`))) {
pollymsg.pollyError('Folder with name exists locally. Not able to create component');
}
let gitBranches = await axios.get('https://api.github.com/repos/ElucidataInc/polly-notebook-templates/branches');
const allowedTemplate = []
for (const eachBranches of gitBranches.data) {
if (eachBranches.name != 'master') {
allowedTemplate.push(eachBranches.name)
}
}
if (allowedTemplate.indexOf(templateType) < 0) {
pollymsg.pollyError('We not support this type. If you would like to add this please do get in touch with out customer success team');
}
try {
await git().silent(true)
.clone('https://github.com/ElucidataInc/polly-notebook-templates.git', notebookName, ['--single-branch', '--branch', templateType]);
} catch (error) {
console.log(error)
pollymsg.pollyError('Not able to get the template');
}
await (async () => {
return new Promise((resolve, reject) => {
rmdir(`${path.resolve(".")}/${notebookName}/.git`, function (error) {
if (error) {
resolve(false);
} else {
resolve(true);
}
});
});
})();
const completeDockerName = `${pollyEnv.dockerDomain}/${orgSlug}/notebooks-${notebookName}`
const createStatus = await createADockerRepo(`notebooks-${notebookName}`, `This is a notebook docker for ${completeDockerName}`, false, true);
if (createStatus) {
fs.mkdirSync(path.resolve(`./${notebookName}/.polly/`));
fs.writeFileSync(path.resolve(`./${notebookName}/.polly/config.json`), JSON.stringify(
{
"pollyNotebooks": {
"dockerName": completeDockerName,
"environmentName": notebookName
}
}, null, ' '));
pollymsg.pollyMessage(`Notebook template ${notebookName} generated!! Happy coding`);
} else {
if (fs.existsSync(`${path.resolve(".")}/${notebookName}`)) {
await (async () => {
return new Promise((resolve, reject) => {
rmdir(`${path.resolve(".")}/${notebookName}`, function (error) {
if (error) {
resolve(false);
} else {
resolve(true);
}
});
});
})();
}
pollymsg.pollyError("Not able to create a notebook docker environment please contact the customer success team");
}
}
export async function notebookDockerBuild(localComponentPath = '.') {
const organizationInfo = await organizationDetails();
const orgSlug = organizationInfo.slug;
const orgId = organizationInfo.org_id;
const configFilePath = `${path.resolve(localComponentPath)}/.polly/config.json`;
if (!fs.existsSync(configFilePath)) {
pollymsg.pollyError("Seems to be not building from a polly notebooks code directory");
}
const dockerNameWithOutTag = JSON.parse(fs.readFileSync(configFilePath)).pollyNotebooks.dockerName;
const dockerName = dockerNameWithOutTag.split(`${pollyEnv.dockerDomain}/${orgSlug}/`)[1];
let foundDocker = false;
const allDockerImages = await getAllDockerRepos(orgId);
let dockerCommits = [];
for (const dockers of allDockerImages) {
if (`${pollyEnv.dockerDomain}/${dockers.attributes.resource_name}` == `${pollyEnv.dockerDomain}/${orgSlug}/${dockerName}`) {
foundDocker = true;
dockerCommits = await getAllDockerCommits(orgId, dockers.attributes.resource_id, 'public' == dockers.attributes.resource_access, true);
break;
}
}
if (!foundDocker) {
pollymsg.pollyError("Seems like new component creation was not successful or the component is deleted");
}
let dockerTag = "V1";
if (dockerCommits.length != 0) {
const dockerTagAppender = "V";
let allTags = [];
for (const dockerCommit of dockerCommits) {
allTags.push(parseInt(dockerCommit.attributes.tag_name.split(dockerTagAppender)[1]));
}
dockerTag = `V${Math.max(...allTags) + 1}`;
}
let localPath = `${path.resolve(localComponentPath)}`;
if (!localPath.endsWith('/')) {
localPath = `${localPath}/`;
}
const dockerFullNameWithTag = `${dockerNameWithOutTag}:${dockerTag}`
const dockerFilePath = `${localPath}Dockerfile`
try {
await dockerBuild(dockerFullNameWithTag, dockerFilePath);
} catch (error) {
pollymsg.pollyError("Not able to start the build process for the Polly notebooks")
}
}
export async function notebookDockerBuildStatus(buildId) {
await dockerBuildStatus(buildId);
}
function checkIfLineExistOrIsCommented(fileData, lineRegex, message) {
lineRegex = lineRegex.split(" ").filter(Boolean);
lineRegex = lineRegex.join("[ ]*");
const re = new RegExp(lineRegex);
const recom = new RegExp('[ ]*#[#]*[ ]*' + lineRegex);
if (fileData.match(re) == null) {
pollymsg.pollyError(message);
} else {
if (fileData.match(recom) != null) {
pollymsg.pollyError(message);
}
}
}
export async function notebookTestScript(nn = 'test-notebook') {
const pt = pollyEnv.baseApi.split('api.').join("")
const pe = pollystore.get('pollyUser').pollyemail;
const wi = await getWorkSpaceId('polly-notebook-test');
const pi = pollystore.get(`${pe}`).pollyIdToken;
const pr = pollystore.get(`${pe}`).pollyrefreshToken;
const pik = `CognitoIdentityServiceProvider.${pollystore.get(`${pe}`).pollyAud}.${pollystore.get(`${pe}`).pollySub}.idToken`
const prk = `CognitoIdentityServiceProvider.${pollystore.get(`${pe}`).pollyAud}.${pollystore.get(`${pe}`).pollySub}.refreshToken`
const finalDockerCommand = `docker run -it -p 4000:8888 -e POLLY_FUNC=open_notebook -e POLLY_TYPE=${pt} -e POLLY_NOTEBOOK_NAME=${nn} -e POLLY_USER=${pe} -e POLLY_PROJECT_ID=${wi} -e POLLY_ID_TOKEN=${pi} -e POLLY_REFRESH_TOKEN=${pr} -e POLLY_ID_TOKEN_KEY=${pik} -e POLLY_REFRESH_TOKEN_KEY=${prk} -e POLLY_NB_KERNEL=sos -e POLLY_NB_ID="" <replace-with-docker-to-test>`
console.log(finalDockerCommand);
pollymsg.pollyMessage("Replace the docker name with the docker that need to be tested");
}