diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 9abe74310..0bfe77881 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -32,7 +32,7 @@ jobs: - name: Install and start minishift OCP 3.11 cluster run: | brew cask install minishift - export MINISHIFT_GITHUB_API_TOKEN=${{ secrets.GITHUBTOKEN }} + export MINISHIFT_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} minishift start --memory=5500 --vm-driver=virtualbox - name: Generate minishift certificates run: | diff --git a/README.md b/README.md index 899d21b44..d275c1a53 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ USAGE * [`chectl server:delete`](#chectl-serverdelete) * [`chectl server:logs`](#chectl-serverlogs) * [`chectl server:start`](#chectl-serverstart) +* [`chectl server:status`](#chectl-serverstatus) * [`chectl server:stop`](#chectl-serverstop) * [`chectl server:update`](#chectl-serverupdate) * [`chectl update [CHANNEL]`](#chectl-update-channel) @@ -446,6 +447,23 @@ OPTIONS _See code: [src/commands/server/start.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/start.ts)_ +## `chectl server:status` + +status Eclipse Che server + +``` +USAGE + $ chectl server:status + +OPTIONS + -h, --help show CLI help + + -n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to be + deployed +``` + +_See code: [src/commands/server/status.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/status.ts)_ + ## `chectl server:stop` stop Eclipse Che server diff --git a/src/api/kube.ts b/src/api/kube.ts index d653f17c7..57e62c316 100644 --- a/src/api/kube.ts +++ b/src/api/kube.ts @@ -350,6 +350,17 @@ export class KubeHelper { } } + async getPodListByLabel(namespace= '', labelSelector: string): Promise { + const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(CoreV1Api) + try { + const { body: podList } = await k8sCoreApi.listNamespacedPod(namespace, undefined, undefined, undefined, undefined, labelSelector) + + return podList.items + } catch (e) { + throw this.wrapK8sClientError(e) + } + } + async deleteClusterRole(name = '') { const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(RbacAuthorizationV1Api) try { diff --git a/src/api/version.ts b/src/api/version.ts index 46ed9ac90..c1f23165e 100644 --- a/src/api/version.ts +++ b/src/api/version.ts @@ -11,12 +11,16 @@ import execa = require('execa') import Listr = require('listr') +import { getClusterClientCommand } from '../util' + import { KubeHelper } from './kube' export namespace VersionHelper { export const MINIMAL_OPENSHIFT_VERSION = '3.11' export const MINIMAL_K8S_VERSION = '1.9' export const MINIMAL_HELM_VERSION = '2.15' + export const CHE_POD_MANIFEST_FILE = '/home/user/eclipse-che/tomcat/webapps/ROOT/META-INF/MANIFEST.MF' + export const CHE_PREFFIX_VERSION = 'Implementation-Version: ' export function getOpenShiftCheckVersionTask(flags: any): Listr.ListrTask { return { @@ -132,6 +136,17 @@ export namespace VersionHelper { return stdout.split('\n').filter(value => value.startsWith(versionPrefix)).map(value => value.substring(versionPrefix.length))[0] } + export async function getCheVersionFromPod(namespace: string, podName: string): Promise { + const command = getClusterClientCommand() + const args = ['exec', podName, '--namespace', namespace, 'cat', CHE_POD_MANIFEST_FILE] + try { + const { stdout } = await execa(command, args, { timeout: 60000 }) + return stdout.split('\n').filter(value => value.startsWith(CHE_PREFFIX_VERSION)).map(value => value.substring(CHE_PREFFIX_VERSION.length))[0] + } catch (error) { + throw new Error(`E_COMMAND_FAILED: ${error}`) + } + } + function removeVPrefix(version: string): string { return version.startsWith('v') ? version.substring(1) : version } diff --git a/src/commands/server/status.ts b/src/commands/server/status.ts new file mode 100644 index 000000000..15a0eab86 --- /dev/null +++ b/src/commands/server/status.ts @@ -0,0 +1,59 @@ +/********************************************************************* + * Copyright (c) 2020 Red Hat, Inc. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + **********************************************************************/ + +import { Command, flags } from '@oclif/command' +import { cli } from 'cli-ux' +import * as notifier from 'node-notifier' + +import { CheHelper } from '../../api/che' +import { KubeHelper } from '../../api/kube' +import { VersionHelper } from '../../api/version' +import { cheNamespace } from '../../common-flags' +import { CheTasks } from '../../tasks/che' +export default class List extends Command { + // Implementation-Version it is a property from Manifest.ml inside of che server pod which indicate Eclipse Che build version. + static description = 'status Eclipse Che server' + + static flags = { + help: flags.help({ char: 'h' }), + chenamespace: cheNamespace, + } + + async run() { + const { flags } = this.parse(List) + const kube = new KubeHelper(flags) + const che = new CheHelper(flags) + const cheTask = new CheTasks(flags) + + let openshiftOauth = 'No' + let cheVersion = 'UNKNOWN' + + const cr = await kube.getCheCluster(flags.chenamespace) + if (cr && cr.spec && cr.spec.auth && cr.spec.auth.openShiftoAuth) { + openshiftOauth = 'Yes' + } + + const chePodList = await kube.getPodListByLabel(flags.chenamespace, cheTask.cheSelector) + const [chePodName] = chePodList.map(pod => pod.metadata && pod.metadata.name) + + if (chePodName) { + cheVersion = await VersionHelper.getCheVersionFromPod(flags.chenamespace, chePodName) + } + + cli.log(`Eclipse Che Verion : ${cheVersion}`) + cli.log(`Eclipse Che Url : ${await che.cheURL(flags.chenamespace)}`) + cli.log(`OpenShift OAuth enabled: ${openshiftOauth}\n`) + + notifier.notify({ + title: 'chectl', + message: 'Command server:status has completed successfully.' + }) + } +} diff --git a/test/e2e/minikube.test.ts b/test/e2e/minikube.test.ts index 8dcec1b84..f27db9d57 100644 --- a/test/e2e/minikube.test.ts +++ b/test/e2e/minikube.test.ts @@ -108,6 +108,14 @@ describe('Workspace creation, list, start, inject, delete. Support stop and dele .it('List workspaces') }) + describe('Server Status', () => { + test + .stdout({ print: true }) + .stderr({ print: true }) + .command(['server:status']) + .it('Get Che Server status') + }) + describe('Stop Workspace', () => { it('Stop a workspace using execa library', async () => { const workspaceId = await helper.getWorkspaceId(PLATFORM) diff --git a/test/e2e/minishift.test.ts b/test/e2e/minishift.test.ts index 8661b7291..85801b5f0 100644 --- a/test/e2e/minishift.test.ts +++ b/test/e2e/minishift.test.ts @@ -114,6 +114,14 @@ describe('Workspace creation, list, start, inject, delete. Support stop and dele .it('List workspaces') }) + describe('Server Status', () => { + test + .stdout({ print: true }) + .stderr({ print: true }) + .command(['server:status']) + .it('Get Che Server status') + }) + describe('Stop Workspace', () => { it('Stop a workspace using execa library', async () => { const workspaceId = await helper.getWorkspaceId(PLATFORM) diff --git a/test/e2e/openshift.test.ts b/test/e2e/openshift.test.ts index 4b74d10ae..0b4aaf871 100644 --- a/test/e2e/openshift.test.ts +++ b/test/e2e/openshift.test.ts @@ -113,6 +113,14 @@ describe('Workspace creation, list, start, inject, delete. Support stop and dele .it('List workspaces') }) + describe('Server Status', () => { + test + .stdout({ print: true }) + .stderr({ print: true }) + .command(['server:status']) + .it('Get Che Server status') + }) + describe('Stop Workspace', () => { it('Stop a workspace using execa library', async () => { const workspaceId = await helper.getWorkspaceId(PLATFORM)