Skip to content

Commit

Permalink
feat: Create new chectl command -> server:status (#870)
Browse files Browse the repository at this point in the history
* Create new chectl command -> server:status

Signed-off-by: Flavius Lacatusu <[email protected]>

* Add command to e2e

Signed-off-by: Flavius Lacatusu <[email protected]>

* Fix import

Signed-off-by: Flavius Lacatusu <[email protected]>

* Fix Openshift oAuth request

Signed-off-by: Flavius Lacatusu <[email protected]>

* Apply pr related recommendations

Signed-off-by: Flavius Lacatusu <[email protected]>

* Code improvement after reviews

Signed-off-by: Flavius Lacatusu <[email protected]>

* Fixes

Signed-off-by: Flavius Lacatusu <[email protected]>
  • Loading branch information
flacatus authored Sep 29, 2020
1 parent 5c04e92 commit cfd23c5
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -463,6 +464,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
Expand Down
11 changes: 11 additions & 0 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ export class KubeHelper {
}
}

async getPodListByLabel(namespace= '', labelSelector: string): Promise<V1Pod[]> {
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 {
Expand Down
15 changes: 15 additions & 0 deletions src/api/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<string> {
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
}
Expand Down
59 changes: 59 additions & 0 deletions src/commands/server/status.ts
Original file line number Diff line number Diff line change
@@ -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.'
})
}
}
8 changes: 8 additions & 0 deletions test/e2e/minikube.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/minishift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/openshift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cfd23c5

Please sign in to comment.