Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve checking command existence #2436

Merged
merged 2 commits into from
Dec 14, 2022
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
4 changes: 2 additions & 2 deletions src/api/che-logs-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 isCommandExists('kubectl') && 'kubectl') || (await isCommandExists('oc') && 'oc')
if (cli) {
const command = 'get events'
const namespaceParam = `-n ${namespace}`
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/platforms/crc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,8 +24,8 @@ export namespace CRCTasks {
export function getPreflightCheckTasks(): Listr.ListrTask<any>[] {
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',
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/platforms/docker-desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,14 +18,15 @@ 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 {
/**
* Returns tasks list which perform preflight platform checks.
*/
export function getPreflightCheckTasks(): Listr.ListrTask<any>[] {
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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/platforms/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[] {
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])),
]
}
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/platforms/microk8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -25,8 +25,8 @@ export namespace MicroK8sTasks {
export function getPeflightCheckTasks(): Listr.ListrTask<any>[] {
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',
Expand Down
7 changes: 3 additions & 4 deletions src/tasks/platforms/minikube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -26,8 +25,8 @@ export namespace MinikubeTasks {
export function getPreflightCheckTasks(): Listr.ListrTask<any>[] {
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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/platforms/openshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
* 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 {
/**
* Returns tasks list which perform preflight platform checks.
*/
export function getPreflightCheckTasks(): Listr.ListrTask<any>[] {
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()),
]
}
Expand Down
21 changes: 21 additions & 0 deletions src/utils/utls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
*/

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'
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')

Expand Down Expand Up @@ -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<boolean> {
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
}