From 8ff63fe3fbb8d034768076d96d4b6c4176178d68 Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Wed, 14 Dec 2022 09:55:14 +0200 Subject: [PATCH 1/2] fix: Improve checking command existance Signed-off-by: Anatolii Bazko --- src/api/che-logs-reader.ts | 2 +- src/tasks/platforms/crc.ts | 6 +++--- src/tasks/platforms/docker-desktop.ts | 4 ++-- src/tasks/platforms/k8s.ts | 4 ++-- src/tasks/platforms/microk8s.ts | 6 +++--- src/tasks/platforms/minikube.ts | 7 +++---- src/tasks/platforms/openshift.ts | 4 ++-- src/utils/utls.ts | 21 +++++++++++++++++++++ 8 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/api/che-logs-reader.ts b/src/api/che-logs-reader.ts index c124aaefc..eeb8ea776 100644 --- a/src/api/che-logs-reader.ts +++ b/src/api/che-logs-reader.ts @@ -61,7 +61,7 @@ export class CheLogsReader { const fileName = path.resolve(directory, namespace, 'events.txt') fs.ensureFileSync(fileName) - const cli = (commandExists.sync('kubectl') && 'kubectl') || (commandExists.sync('oc') && 'oc') + const cli = (await commandExists('kubectl') && 'kubectl') || (await commandExists('oc') && 'oc') if (cli) { const command = 'get events' const namespaceParam = `-n ${namespace}` diff --git a/src/tasks/platforms/crc.ts b/src/tasks/platforms/crc.ts index 243bd67ec..3137300f8 100644 --- a/src/tasks/platforms/crc.ts +++ b/src/tasks/platforms/crc.ts @@ -10,12 +10,12 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as execa from 'execa' import * as Listr from 'listr' import {CheCtlContext} from '../../context' import {CommonTasks} from '../common-tasks' +import {isCommandExists} from '../../utils/utls' /** * Helper for Code Ready Container @@ -24,8 +24,8 @@ export namespace CRCTasks { export function getPreflightCheckTasks(): Listr.ListrTask[] { const flags = CheCtlContext.getFlags() return [ - CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => commandExists.sync('oc')), - CommonTasks.getVerifyCommand('Verify if OpenShift Local is installed', 'OpenShift Local not found', () => commandExists.sync('crc')), + CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => isCommandExists('oc')), + CommonTasks.getVerifyCommand('Verify if OpenShift Local is installed', 'OpenShift Local not found', () => isCommandExists('crc')), CommonTasks.getVerifyCommand('Verify if OpenShift Local is running', 'OpenShift Local not ready', () => isCRCRunning()), { title: 'Retrieving OpenShift Local IP and domain for routes URLs', diff --git a/src/tasks/platforms/docker-desktop.ts b/src/tasks/platforms/docker-desktop.ts index 63b39e926..e0636ecaf 100644 --- a/src/tasks/platforms/docker-desktop.ts +++ b/src/tasks/platforms/docker-desktop.ts @@ -10,7 +10,6 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as execa from 'execa' import * as Listr from 'listr' import * as os from 'os' @@ -19,6 +18,7 @@ import {CheCtlContext} from '../../context' import {DOMAIN_FLAG} from '../../flags' import {KubeClient} from '../../api/kube-client' import {CommonTasks} from '../common-tasks' +import {isCommandExists} from '../../utils/utls' export namespace DockerDesktopTasks { /** @@ -26,7 +26,7 @@ export namespace DockerDesktopTasks { */ export function getPreflightCheckTasks(): Listr.ListrTask[] { return [ - CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => commandExists.sync('oc')), + CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => isCommandExists('oc')), { title: 'Verify if kubectl context is Docker Desktop', task: async (_ctx: any, task: any) => { diff --git a/src/tasks/platforms/k8s.ts b/src/tasks/platforms/k8s.ts index 752eb898b..2b2c2b1d9 100644 --- a/src/tasks/platforms/k8s.ts +++ b/src/tasks/platforms/k8s.ts @@ -10,18 +10,18 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as Listr from 'listr' import {CheCtlContext} from '../../context' import {DOMAIN_FLAG} from '../../flags' import {CommonTasks} from '../common-tasks' +import {isCommandExists} from '../../utils/utls' export namespace K8sTasks { export function getPeflightCheckTasks(): Listr.ListrTask[] { const flags = CheCtlContext.getFlags() return [ - CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => commandExists.sync('kubectl')), + CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => isCommandExists('kubectl')), CommonTasks.getVerifyCommand('Verify domain is set', `--${DOMAIN_FLAG} flag needs to be defined`, () => Boolean(flags[DOMAIN_FLAG])), ] } diff --git a/src/tasks/platforms/microk8s.ts b/src/tasks/platforms/microk8s.ts index bb725ccba..aedf520f3 100644 --- a/src/tasks/platforms/microk8s.ts +++ b/src/tasks/platforms/microk8s.ts @@ -10,13 +10,13 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as execa from 'execa' import * as Listr from 'listr' import {CheCtlContext} from '../../context' import {DOMAIN_FLAG} from '../../flags' import {CommonTasks} from '../common-tasks' +import {isCommandExists} from '../../utils/utls' export namespace MicroK8sTasks { /** @@ -25,8 +25,8 @@ export namespace MicroK8sTasks { export function getPeflightCheckTasks(): Listr.ListrTask[] { const flags = CheCtlContext.getFlags() return [ - CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => commandExists.sync('kubectl')), - CommonTasks.getVerifyCommand('Verify if microk8s is installed', 'MicroK8s not found', () => commandExists.sync('microk8s.status')), + CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => isCommandExists('kubectl')), + CommonTasks.getVerifyCommand('Verify if microk8s is installed', 'MicroK8s not found', () => isCommandExists('microk8s.status')), CommonTasks.getVerifyCommand('Verify if microk8s is running', 'MicroK8s is not running.', () => isMicroK8sRunning()), { title: 'Verify if microk8s ingress addon is enabled', diff --git a/src/tasks/platforms/minikube.ts b/src/tasks/platforms/minikube.ts index 51fee2b0d..bbfba9e5c 100644 --- a/src/tasks/platforms/minikube.ts +++ b/src/tasks/platforms/minikube.ts @@ -10,12 +10,11 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as execa from 'execa' import * as Listr from 'listr' import {CheCtlContext, OIDCContext} from '../../context' import { KubeClient } from '../../api/kube-client' -import { sleep } from '../../utils/utls' +import {isCommandExists, sleep} from '../../utils/utls' import {DOMAIN_FLAG} from '../../flags' import {CommonTasks} from '../common-tasks' @@ -26,8 +25,8 @@ export namespace MinikubeTasks { export function getPreflightCheckTasks(): Listr.ListrTask[] { const flags = CheCtlContext.getFlags() return [ - CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => commandExists.sync('kubectl')), - CommonTasks.getVerifyCommand('Verify if minikube is installed', 'minikube not found', () => commandExists.sync('minikube')), + CommonTasks.getVerifyCommand('Verify if kubectl is installed', 'kubectl not found', () => isCommandExists('kubectl')), + CommonTasks.getVerifyCommand('Verify if minikube is installed', 'minikube not found', () => isCommandExists('minikube')), { title: 'Verify if minikube is running', task: async (_ctx: any, task: any) => { diff --git a/src/tasks/platforms/openshift.ts b/src/tasks/platforms/openshift.ts index 749a12672..df798aaf8 100644 --- a/src/tasks/platforms/openshift.ts +++ b/src/tasks/platforms/openshift.ts @@ -10,11 +10,11 @@ * Red Hat, Inc. - initial API and implementation */ -import * as commandExists from 'command-exists' import * as Listr from 'listr' import {OpenShift} from '../../utils/openshift' import {CommonTasks} from '../common-tasks' +import {isCommandExists} from '../../utils/utls' export namespace OpenshiftTasks { /** @@ -22,7 +22,7 @@ export namespace OpenshiftTasks { */ export function getPreflightCheckTasks(): Listr.ListrTask[] { return [ - CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => commandExists.sync('oc')), + CommonTasks.getVerifyCommand('Verify if oc is installed', 'oc not found', () => isCommandExists('oc')), CommonTasks.getVerifyCommand('Verify if openshift is running', 'PLATFORM_NOT_READY: \'oc status\' command failed. Please login with \'oc login\' command and try again.', () => OpenShift.isOpenShiftRunning()), ] } diff --git a/src/utils/utls.ts b/src/utils/utls.ts index eba61635d..462d091a1 100644 --- a/src/utils/utls.ts +++ b/src/utils/utls.ts @@ -11,6 +11,7 @@ */ import * as fs from 'fs-extra' +import * as os from 'os' import * as yaml from 'js-yaml' import * as path from 'path' import {CheCtlContext} from '../context' @@ -18,6 +19,8 @@ import * as Listr from 'listr' import {LISTR_RENDERER_FLAG} from '../flags' import {EclipseChe} from '../tasks/installers/eclipse-che/eclipse-che' import {CHE} from '../constants' +import * as commandExists from 'command-exists' +import execa = require('execa') const pkjson = require('../../package.json') @@ -137,3 +140,21 @@ export function isPartOfEclipseChe(resource: any): boolean { export function isCheFlavor(): boolean { return EclipseChe.CHE_FLAVOR === CHE } + +export async function isCommandExists(commandName: string): Promise { + if (commandExists.sync(commandName)) { + return true + } + + // commandExists.sync fails if not executable command exists in the same directory. + // Double check without accessing local file. + // The check above there is for backward compatability. + + const whereCommand = os.platform() === 'win32' ? 'where' : 'whereis' + try { + await execa(whereCommand, [commandName]) + return true + } catch {} + + return false +} From 6c6a52ebc245f19fe82d90ae02404ff97e1acdf5 Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Wed, 14 Dec 2022 11:04:49 +0200 Subject: [PATCH 2/2] fix Signed-off-by: Anatolii Bazko --- src/api/che-logs-reader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/che-logs-reader.ts b/src/api/che-logs-reader.ts index eeb8ea776..9b81dd405 100644 --- a/src/api/che-logs-reader.ts +++ b/src/api/che-logs-reader.ts @@ -12,10 +12,10 @@ import { V1Pod, Watch } from '@kubernetes/client-node' import * as cp from 'child_process' -import * as commandExists from 'command-exists' import * as fs from 'fs-extra' import * as path from 'path' import { KubeClient } from './kube-client' +import {isCommandExists} from '../utils/utls' export class CheLogsReader { private kubeHelper: KubeClient @@ -61,7 +61,7 @@ export class CheLogsReader { const fileName = path.resolve(directory, namespace, 'events.txt') fs.ensureFileSync(fileName) - const cli = (await commandExists('kubectl') && 'kubectl') || (await commandExists('oc') && 'oc') + const cli = (await isCommandExists('kubectl') && 'kubectl') || (await isCommandExists('oc') && 'oc') if (cli) { const command = 'get events' const namespaceParam = `-n ${namespace}`