Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
out
node_modules
.vscode-test/
test/fixtures/workspace/.vscode/
*.vsix
coverage/
toFile
Expand Down
46 changes: 46 additions & 0 deletions build/bundle-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { DownloadUtil } from './download';
import { Archive } from '../src/util/archive';

import hasha = require('hasha');
import mkdirp = require('mkdirp');
import path = require('path');
import fs = require('fs-extra');
import configData = require('../src/tools.json');

async function verifyTools(): Promise<void> {
for (const key in configData) {
for (const OS in configData[key].platform) {
const targetFolder = path.resolve('.', 'out', 'tools', OS);
console.info(`Download tools to '${targetFolder}'`);
await downloadFileAndCreateSha256(
targetFolder,
configData[key].platform[OS].dlFileName,
configData[key].platform[OS].url,
configData[key].platform[OS].sha256sum,
configData[key].platform[OS].cmdFileName,
configData[key].platform[OS].filePrefix);
}
}
}

async function downloadFileAndCreateSha256(targetFolder: string, fileName: string, reqURL: string, sha256sum: string, cmdFileName: string, filePrefix?: string) {
mkdirp.sync(targetFolder);
const currentFile = path.join(targetFolder, fileName);
console.log(`${currentFile} download started from ${reqURL}`);
await DownloadUtil.downloadFile(reqURL, currentFile, (current) => console.log(`${current }%`));
const currentSHA256 = await hasha.fromFile(currentFile, {algorithm: 'sha256'});
if (currentSHA256 === sha256sum) {
console.log( `[INFO] ${currentFile} is downloaded and sha256 is correct`);
} else {
throw Error(`${currentFile} is downloaded and sha256 is not correct`);
}
await Archive.extract(currentFile, targetFolder, cmdFileName, filePrefix);
fs.removeSync(currentFile);
}

verifyTools();
1 change: 1 addition & 0 deletions src/util/download.ts → build/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*-----------------------------------------------------------------------------------------------*/

/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-unused-expressions */
import * as fs from 'fs-extra';

const { promisify } = require('util');
Expand Down
5 changes: 3 additions & 2 deletions build/unit-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ async function main(): Promise<void> {
const extensionTestsPath = path.resolve(__dirname, '../../out/test/unit/');

// Download VS Code, unzip it and run the integration test
console.log(extensionDevelopmentPath, extensionTestsPath);
await runTests({ extensionDevelopmentPath, extensionTestsPath });
await runTests({
extensionDevelopmentPath, extensionTestsPath
});
} catch (err) {
console.error(err);
process.exit(1);
Expand Down
6 changes: 2 additions & 4 deletions build/verify-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { DownloadUtil } from '../src/util/download';

'use strict';
import { DownloadUtil } from './download';

import hasha = require('hasha');
import mkdirp = require('mkdirp');
Expand Down Expand Up @@ -52,4 +50,4 @@ cp.exec('git diff --name-only origin/master -- .', async (error, stdout) => {
} else {
console.log('tools.json is not changed, skipping download verification');
}
});
});
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"main": "./out/src/extension",
"scripts": {
"verify": "node ./out/build/verify-tools.js",
"vscode:prepublish": "npm run build",
"vscode:prepublish": "npm run build && node ./out/build/bundle-tools.js",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"clean": "rm -rf out || rmdir out /s /q || echo 'no out folder, ignoring errors'",
Expand Down Expand Up @@ -1072,6 +1072,12 @@
"type": "boolean",
"default": false,
"description": "Disable check if migration is required for resources created with previous version of the extension and migration request message."
},
"openshiftConnector.searchForToolsInPath": {
"Title": "Search CLI tools in PATH locations before using included binaries",
"type": "boolean",
"default": false,
"description": "Force extension to search for `oc` and `odo` CLI tools in PATH locations before using bundled binaries."
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/oc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Oc {

let toolLocation: string;
if (!message) {
toolLocation = await ToolsConfig.detectOrDownload('oc');
toolLocation = await ToolsConfig.detect('oc');
if (!toolLocation) {
message = 'Cannot run \'oc create\'. OKD CLI client tool cannot be found.';
}
Expand Down
10 changes: 3 additions & 7 deletions src/odo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,19 +891,15 @@ export class OdoImpl implements Odo {

public async executeInTerminal(command: string, cwd: string = process.cwd(), name = 'OpenShift'): Promise<void> {
const cmd = command.split(' ')[0];
const toolLocation = await ToolsConfig.detectOrDownload(cmd);
let toolDirLocation: string;
if (toolLocation) {
toolDirLocation = path.dirname(toolLocation);
}
const toolLocation = await ToolsConfig.detect(cmd);
const terminal: Terminal = WindowUtil.createTerminal(name, cwd);
terminal.sendText(toolDirLocation ? command.replace(cmd, `"${toolLocation}"`).replace(new RegExp(`&& ${cmd}`, 'g'), `&& "${toolLocation}"`) : command, true);
terminal.sendText(toolLocation === cmd ? command : command.replace(cmd, `"${toolLocation}"`).replace(new RegExp(`&& ${cmd}`, 'g'), `&& "${toolLocation}"`), true);
terminal.show();
}

public async execute(command: string, cwd?: string, fail = true): Promise<CliExitData> {
const cmd = command.split(' ')[0];
const toolLocation = await ToolsConfig.detectOrDownload(cmd);
const toolLocation = await ToolsConfig.detect(cmd);
return OdoImpl.cli.execute(
toolLocation ? command.replace(cmd, `"${toolLocation}"`).replace(new RegExp(`&& ${cmd}`, 'g'), `&& "${toolLocation}"`) : command,
cwd ? {cwd} : { }
Expand Down
6 changes: 3 additions & 3 deletions src/openshift/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ export class Component extends OpenShiftItem {
const isNode = tag.annotations.tags.includes('nodejs');
const JAVA_EXT = 'redhat.java';
const JAVA_DEBUG_EXT = 'vscjava.vscode-java-debug';
let result: undefined | PromiseLike<string>;
if (component.compType === ComponentType.LOCAL && (isJava || isNode)) {
const toolLocation = await ToolsConfig.detectOrDownload(`odo`);
let result: undefined | string | PromiseLike<string>;
if (isJava || isNode) {
const toolLocation = await ToolsConfig.detect(`odo`);
if (isJava) {
const jlsIsActive = extensions.getExtension(JAVA_EXT);
const jdIsActive = extensions.getExtension(JAVA_DEBUG_EXT);
Expand Down
13 changes: 8 additions & 5 deletions src/tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"versionRange": "1.0.2",
"versionRangeLabel": "v1.0.2",
"dlFileName": "odo",
"cmdFileName": "odo",
"filePrefix": "",
"platform": {
"win32": {
Expand All @@ -19,12 +18,14 @@
"darwin": {
"url": "https://mirror.openshift.com/pub/openshift-v4/clients/odo/v1.0.2/odo-darwin-amd64.tar.gz",
"sha256sum": "af50916c359b5a383f983c6776f166728497076ff1226e0ef97a3926b447b71e",
"dlFileName": "odo-darwin-amd64.tar.gz"
"dlFileName": "odo-darwin-amd64.tar.gz",
"cmdFileName": "odo"
},
"linux": {
"url": "https://mirror.openshift.com/pub/openshift-v4/clients/odo/v1.0.2/odo-linux-amd64.tar.gz",
"sha256sum": "14640b4fd76c6bd863b5f2a5f871760054cd085da4bc87dc0499eb8a0f9389b3",
"dlFileName": "odo-linux-amd64.tar.gz"
"dlFileName": "odo-linux-amd64.tar.gz",
"cmdFileName": "odo"
}
}
},
Expand All @@ -47,14 +48,16 @@
"darwin": {
"url": "https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-mac.zip",
"sha256sum": "75d58500aec1a2cee9473dfa826c81199669dbc0f49806e31a13626b5e4cfcf0",
"dlFileName": "oc.zip"
"dlFileName": "oc.zip",
"cmdFileName": "oc"
},
"linux": {
"url": "https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz",
"sha256sum": "4b0f07428ba854174c58d2e38287e5402964c9a9355f6c359d1242efd0990da3",
"fileName": "oc.tar.gz",
"dlFileName": "oc.tar.gz",
"filePrefix": "openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit"
"filePrefix": "openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit",
"cmdFileName": "oc"
}
}
}
Expand Down
73 changes: 12 additions & 61 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

'use strict';

import { Platform } from "./util/platform";
import { which } from "shelljs";
import * as vscode from 'vscode';
import * as path from 'path';
import * as fsxt from 'fs-extra';
import * as fs from 'fs';
import { DownloadUtil } from "./util/download";
import { Archive } from "./util/archive";
import { Platform } from "./util/platform";
import { CliChannel } from './cli';

import hasha = require("hasha");
import semver = require('semver');
import configData = require('./tools.json');

Expand All @@ -40,66 +37,20 @@ export class ToolsConfig {
ToolsConfig.tools = ToolsConfig.loadMetadata(configData, Platform.OS);
}

public static async detectOrDownload(cmd: string): Promise<string> {
public static async detect(cmd: string): Promise<string> {

let toolLocation: string = ToolsConfig.tools[cmd].location;

if (toolLocation === undefined) {
const toolCacheLocation = path.resolve(Platform.getUserHomePath(), '.vs-openshift', ToolsConfig.tools[cmd].cmdFileName);
const whichLocation = which(cmd);
const toolLocations: string[] = [whichLocation ? whichLocation.stdout : null, toolCacheLocation];
toolLocation = await ToolsConfig.selectTool(toolLocations, ToolsConfig.tools[cmd].versionRange);

if (toolLocation === undefined) {
// otherwise request permission to download
const toolDlLocation = path.resolve(Platform.getUserHomePath(), '.vs-openshift', ToolsConfig.tools[cmd].dlFileName);
const installRequest = `Download and install v${ToolsConfig.tools[cmd].version}`;
const response = await vscode.window.showInformationMessage(
`Cannot find ${ToolsConfig.tools[cmd].description} ${ToolsConfig.tools[cmd].versionRangeLabel}.`, installRequest, 'Help', 'Cancel');
fsxt.ensureDirSync(path.resolve(Platform.getUserHomePath(), '.vs-openshift'));
if (response === installRequest) {
let action: string;
do {
action = undefined;
await vscode.window.withProgress({
cancellable: true,
location: vscode.ProgressLocation.Notification,
title: `Downloading ${ToolsConfig.tools[cmd].description}`
},
(progress: vscode.Progress<{increment: number; message: string}>) => {
return DownloadUtil.downloadFile(
ToolsConfig.tools[cmd].url,
toolDlLocation,
(dlProgress, increment) => progress.report({ increment, message: `${dlProgress}%`})
);
});
const sha256sum: string = await hasha.fromFile(toolDlLocation, {algorithm: 'sha256'});
if (sha256sum !== ToolsConfig.tools[cmd].sha256sum) {
fsxt.removeSync(toolDlLocation);
action = await vscode.window.showInformationMessage(`Checksum for downloaded ${ToolsConfig.tools[cmd].description} v${ToolsConfig.tools[cmd].version} is not correct.`, 'Download again', 'Cancel');
}

} while (action === 'Download again');

if (action !== 'Cancel') {
if (toolDlLocation.endsWith('.zip') || toolDlLocation.endsWith('.tar.gz')) {
await Archive.extract(toolDlLocation, path.resolve(Platform.getUserHomePath(), '.vs-openshift'), ToolsConfig.tools[cmd].filePrefix);
} else if (toolDlLocation.endsWith('.gz')) {
await Archive.extract(toolDlLocation, toolCacheLocation, ToolsConfig.tools[cmd].filePrefix);
}
fsxt.removeSync(toolDlLocation);
if (Platform.OS !== 'win32') {
fs.chmodSync(toolCacheLocation, 0o765);
}
toolLocation = toolCacheLocation;
}
} else if (response === `Help`) {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(`https://github.com/redhat-developer/vscode-openshift-tools#dependencies`));
}
}
if (toolLocation) {
ToolsConfig.tools[cmd].location = toolLocation;
const toolCacheLocation = path.resolve(__dirname, '..', 'tools', Platform.OS, ToolsConfig.tools[cmd].cmdFileName);
const toolLocations: string[] = [toolCacheLocation];
if (vscode.workspace.getConfiguration("openshiftConnector").get("searchForToolsInPath")) {
const whichLocation = which(cmd);
toolLocations.unshift(whichLocation && whichLocation.stdout);
}

toolLocation = await ToolsConfig.selectTool(toolLocations, ToolsConfig.tools[cmd].versionRange);
if (toolLocation && Platform.OS !== 'win32') fs.chmodSync(toolLocation, 0o765);
}
return toolLocation;
}
Expand Down
23 changes: 17 additions & 6 deletions src/util/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import * as zlib from 'zlib';
import targz = require('targz');
import unzipm = require('unzip-stream');
import pify = require('pify');
import path = require('path');

export class Archive {
static extract(zipFile: string, extractTo: string, prefix?: string): Promise<void> {
static extract(zipFile: string, extractTo: string, cmdFileName: string, prefix?: string): Promise<void> {
return new Promise((resolve, reject) => {
if (zipFile.endsWith('.tar.gz')) {
Archive.untar(zipFile, extractTo, prefix)
Archive.untar(zipFile, extractTo, cmdFileName, prefix)
.then(resolve)
.catch(reject);
} else if (zipFile.endsWith('.gz')) {
Archive.gunzip(zipFile, extractTo)
.then(resolve)
.catch(reject);
} else if (zipFile.endsWith('.zip')) {
Archive.unzip(zipFile, extractTo)
Archive.unzip(zipFile, extractTo, cmdFileName)
.then(resolve)
.catch(reject);
} else {
Expand All @@ -42,7 +43,7 @@ export class Archive {
});
}

static untar(zipFile: string, extractTo: string, prefix: string): Promise<void> {
static untar(zipFile: string, extractTo: string, fileName: string, prefix: string): Promise<void> {
return pify(targz.decompress)({
src: zipFile,
dest: extractTo,
Expand All @@ -53,15 +54,25 @@ export class Archive {
result.name = header.name.substring(prefix.length);
}
return result;
},
ignore: (name: string): boolean => {
return path.basename(name) !== fileName;
}
}
});
}

static unzip(zipFile: string, extractTo: string): Promise<void> {
static unzip(zipFile: string, extractTo: string, fileName: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.createReadStream(zipFile)
.pipe(unzipm.Extract({ path: extractTo }))
.pipe(unzipm.Parse())
.on('entry', (entry) => {
if (path.basename(entry.path) === fileName) {
entry.pipe(fs.createWriteStream(path.join(extractTo, fileName)));
} else {
entry.autodrain();
}
})
.on('error', reject)
.on('close', resolve);
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/oc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ suite('Oc', () => {
warnStub = sandbox.stub(window, 'showWarningMessage');
execStub = sandbox.stub(CliChannel.prototype, 'execute');
quickPickStub = sandbox.stub(window, 'showQuickPick');
detectOrDownloadStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves('path');
detectOrDownloadStub = sandbox.stub(ToolsConfig, 'detect').resolves('path');
});

teardown(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/odo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ suite("odo", () => {

setup(() => {
execStub = sandbox.stub(CliChannel.prototype, 'execute');
toolsStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves();
toolsStub = sandbox.stub(ToolsConfig, 'detect').resolves();
});

test('execute calls the given command in shell', async () => {
Expand Down Expand Up @@ -120,7 +120,7 @@ suite("odo", () => {
dispose: sinon.stub()
};
toolsStub.restore();
toolsStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves(path.join('segment1', 'segment2'));
toolsStub = sandbox.stub(ToolsConfig, 'detect').resolves(path.join('segment1', 'segment2'));
const ctStub = sandbox.stub(WindowUtil, 'createTerminal').returns(termFake);
await odoCli.executeInTerminal('cmd');
expect(termFake.sendText).calledOnce;
Expand Down
Loading