Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

import tarfile #62

Merged
merged 6 commits into from Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/initialize/src/classes/ProjectInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { detectType } from '../controllers/typeDetectionController';
import { writeCwSettings } from '../controllers/cwSettingsController';
import { importProjectFromGitRepo } from '../controllers/gitController';
import { InitializeResult } from 'initializeTypes';
import { importProjectFromTar } from '../controllers/tarController';

export default class ProjectInitializer {
projectName: string;
Expand Down Expand Up @@ -47,6 +48,16 @@ export default class ProjectInitializer {
return this.initializeExistingProject();
}

async initializeProjectFromTar(
tarFile: string
): Promise<InitializeResult> {
await importProjectFromTar(tarFile,
this.projectName,
this.projectMountDirectory
);
return this.initializeExistingProject();
}

copyFiles() {
this.traverseFileSystem(this.projectMountDirectory);
}
Expand Down
50 changes: 50 additions & 0 deletions src/initialize/src/controllers/tarController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

import fs from 'fs-extra';
import { exec } from 'child_process';
import request from 'request';

export function importProjectFromTar(tarFile: string, name: string, dest: string): Promise<void> {

const fileName = `/tmp/${name}.tar.gz`;
const file = fs.createWriteStream(fileName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an absolute filename, otherwise it will just be created in the current working directory (and I'm not sure exactly where that will be in all cases).

Copy link
Contributor

@hhellyer hhellyer Aug 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use /tmp not /app. /app exists in the initialiser pod but the initialiser code is run directly as an npm when we are in che and /app doesn't exist there.
(Also /tmp is a better location for temporary files.)


return new Promise((resolve, reject) => {
request(tarFile)
.pipe(file)
.on('finish', () => {
exec(`tar -xvf ${fileName} -C ${dest}`, (err: any, stdout: any, stderr: any) => {
try {
fs.removeSync(fileName);
}
catch (err) {
/* Logging error when removing temp dir fails */
console.log(err);
}
if (err) {
return reject(err);
}
resolve();
});
})
.on('error', (err) => {
try {
fs.removeSync(fileName);
}
catch (err) {
console.log(err);
}
reject(err);
});
});

}
4 changes: 3 additions & 1 deletion src/initialize/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function initializeProject(projectName: string, projectDirectory: s
const projectInitializer = new ProjectInitializer(projectName, projectDirectory);

try {
if (isGitCloneRequired(projectName, gitRepository)) {
if (process.env.TAR_FILE) {
initializeResult = await projectInitializer.initializeProjectFromTar(process.env.TAR_FILE);
} else if (isGitCloneRequired(projectName, gitRepository)) {
initializeResult = await projectInitializer.initializeProjectFromGit(gitRepository, gitBranch);
} else {
initializeResult = await projectInitializer.initializeExistingProject();
Expand Down
19 changes: 13 additions & 6 deletions src/pfe/portal/modules/projectInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,19 @@ const createEnvForContainerCreation = (user, projectName, gitInfo) => {
env.push(`CW_EXTENSIONS=${JSON.stringify(cwExtensions)}`);

if (projectName && gitInfo.repo) {
env.push(
`PROJ_NAME=${projectName}`,
`GIT_REPO=${gitInfo.repo}`,
);
if (gitInfo.branch) {
env.push(`GIT_BRANCH=${gitInfo.branch}`);
if (gitInfo.repo.endsWith(".tar.gz")) {
env.push(
`PROJ_NAME=${projectName}`,
`TAR_FILE=${gitInfo.repo}`,
);
} else {
env.push(
`PROJ_NAME=${projectName}`,
`GIT_REPO=${gitInfo.repo}`,
);
if (gitInfo.branch) {
env.push(`GIT_BRANCH=${gitInfo.branch}`);
}
}
}
return env;
Expand Down