Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
CHE-11443 implement the containers plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 committed Jan 10, 2019
1 parent 5d591ce commit bd899d6
Show file tree
Hide file tree
Showing 11 changed files with 1,679 additions and 2,904 deletions.
7 changes: 7 additions & 0 deletions extensions/eclipse-che-theia-plugin/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@eclipse-che/api@^6.16.1":
version "6.16.1"
resolved "https://registry.yarnpkg.com/@eclipse-che/api/-/api-6.16.1.tgz#5b5e70285cf27d525df3752c938388e36f7925d6"
3 changes: 3 additions & 0 deletions plugins/containers-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib/
node_modules/
*.theia
11 changes: 11 additions & 0 deletions plugins/containers-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# containers-plugin

Containers-tree plugin for Theia.

This plugin provides:

- List of containers with their status, where each container provides:

- List of servers (resource locator) if there is any;

- Ability to open a terminal within a selected container;
54 changes: 54 additions & 0 deletions plugins/containers-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "containers-plugin",
"publisher": "theia",
"keywords": [
"theia-plugin"
],
"version": "0.0.1",
"license": "EPL-2.0",
"files": [
"src"
],
"contributes": {
"viewsContainers": {
"right": [
{
"id": "containers",
"title": "Containers"
}
]
},
"views": {
"containers": [
{
"id": "containers",
"name": "Workspace"
}
]
}
},
"devDependencies": {
"@theia/plugin": "next",
"@theia/plugin-packager": "latest",
"@eclipse-che/plugin": "latest",
"rimraf": "2.6.2",
"typescript-formatter": "7.2.2",
"typescript": "2.9.2"
},
"dependencies": {
},
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "rimraf lib",
"format-code": "tsfmt -r",
"watch": "tsc -watch",
"compile": "tsc",
"build": "yarn run format-code && yarn run compile && theia:plugin pack"
},
"engines": {
"theiaPlugin": "next"
},
"theiaPlugin": {
"backend": "lib/containers-plugin.js"
}
}
25 changes: 25 additions & 0 deletions plugins/containers-plugin/src/containers-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*********************************************************************
* Copyright (c) 2018 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 { ContainersTreeDataProvider } from './containers-tree-data-provider';
import * as theia from '@theia/plugin';

export function start(context: theia.PluginContext) {
const treeDataProvider = new ContainersTreeDataProvider();
const treeDataDisposableFn = theia.Disposable.create(() => {
treeDataProvider.dispose();
});
context.subscriptions.push(treeDataDisposableFn);

theia.window.createTreeView('containers', { treeDataProvider });
}

export function stop() {
}
54 changes: 54 additions & 0 deletions plugins/containers-plugin/src/containers-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*********************************************************************
* Copyright (c) 2018 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 * as che from '@eclipse-che/plugin';

export interface IWorkspaceMachine {
machineName: string;
servers?: {
[serverRef: string]: {
url?: string;
port?: string;
}
};
status?: 'STARTING' | 'RUNNING' | 'STOPPED' | 'FAILED';
}

export class ContainersService {

private runtimeMachines: Array<IWorkspaceMachine> = [];

constructor() {
}

async updateMachines(): Promise<void> {
const workspace = await che.workspace.getCurrentWorkspace();
if (!workspace) {
return Promise.reject('Failed to get workspace configuration');
}

const workspaceMachines = workspace!.runtime
&& workspace!.runtime!.machines
|| workspace!.config!.environments![workspace.config!.defaultEnv!].machines
|| {};

this.runtimeMachines.length = 0;

Object.keys(workspaceMachines).forEach((machineName: string) => {
const machine = <IWorkspaceMachine>workspaceMachines[machineName];
machine.machineName = machineName;
this.runtimeMachines.push(machine);
});
}

get machines(): Array<IWorkspaceMachine> {
return this.runtimeMachines;
}
}
135 changes: 135 additions & 0 deletions plugins/containers-plugin/src/containers-tree-data-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*********************************************************************
* Copyright (c) 2018 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 * as theia from '@theia/plugin';
import { ContainersService, IWorkspaceMachine } from './containers-service';

interface TreeNodeItem {
id: string;
name: string;
tooltip: string;
iconPath?: string;
parentId?: string;
commandId?: string
}

export class ContainersTreeDataProvider implements theia.TreeDataProvider<TreeNodeItem> {
private ids: string[];
private treeNodeItems: TreeNodeItem[];
private onDidChangeTreeDataEmitter: theia.EventEmitter<undefined>;

readonly onDidChangeTreeData: theia.Event<undefined>;

constructor() {
this.ids = [];
this.treeNodeItems = [];

this.onDidChangeTreeDataEmitter = new theia.EventEmitter<undefined>();
this.onDidChangeTreeData = this.onDidChangeTreeDataEmitter.event;

const containersService = new ContainersService();

this.updateContainersTreeData(containersService).then(() => {
this.onDidChangeTreeDataEmitter.fire();
});
}

private async updateContainersTreeData(containersService: ContainersService): Promise<void> {
this.ids.length = 0;
this.treeNodeItems.length = 0;
await containersService.updateMachines();
containersService.machines.forEach((machine: IWorkspaceMachine) => {
const treeItem: TreeNodeItem = {
id: this.getRandId(),
name: machine.machineName,
tooltip: `workspace container status ${machine.status}`
};
switch (machine.status) {
case 'STARTING':
treeItem.iconPath = 'fa-circle medium-yellow';
break;
case 'RUNNING':
treeItem.iconPath = 'fa-circle medium-green';
break;
case 'FAILED':
treeItem.iconPath = 'fa-circle medium-red';
break;
default:
treeItem.iconPath = 'fa-circle-o';
}
this.treeNodeItems.push(treeItem);
this.treeNodeItems.push({
id: this.getRandId(),
parentId: treeItem.id,
name: 'terminal',
iconPath: 'fa-terminal',
tooltip: `new terminal for ${machine.machineName}`,
commandId: `terminal-for-${machine.machineName}-container:new`
});
const servers = machine.servers;
if (!servers) {
return;
}
Object.keys(servers).forEach((serverName: string) => {
const server = servers[serverName];
if (!server || !server.url) {
return;
}
this.treeNodeItems.push({
id: this.getRandId(),
parentId: treeItem.id,
name: ` [${serverName}](${server.url})`,
tooltip: server.url
});
});
});
}

private getRandId(): string {
let uniqueId = '';
for (let counter = 0; counter < 1000; counter++) {
uniqueId = `${('0000' + (Math.random() * Math.pow(36, 4) << 0).toString(36)).slice(-4)}`;
if (this.ids.findIndex(id => id === uniqueId) === -1) {
break;
}
}
this.ids.push(uniqueId);
return uniqueId;
}

dispose(): void {
this.onDidChangeTreeDataEmitter.dispose();
}

getTreeItem(element: TreeNodeItem): theia.TreeItem {
const treeItem: theia.TreeItem = {
label: element.name,
tooltip: element.tooltip
};
if (element.parentId === undefined) {
treeItem.collapsibleState = theia.TreeItemCollapsibleState.Expanded;
}
if (element.iconPath) {
treeItem.iconPath = element.iconPath
}
if (element.commandId) {
treeItem.command = { id: element.commandId }
}
return treeItem;
}

getChildren(element?: TreeNodeItem): TreeNodeItem[] {
if (element) {
return this.treeNodeItems.filter(item => item.parentId === element.id);
} else {
return this.treeNodeItems.filter(item => item.parentId === undefined);
}
}
}
23 changes: 23 additions & 0 deletions plugins/containers-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"emitDecoratorMetadata": true,
"downlevelIteration": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"lib": [
"es6",
"webworker"
],
"sourceMap": true,
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
]
}
19 changes: 19 additions & 0 deletions plugins/containers-plugin/tsfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"baseIndentSize": 0,
"newLineCharacter": "\n",
"indentSize": 4,
"tabSize": 4,
"indentStyle": 4,
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}

Loading

0 comments on commit bd899d6

Please sign in to comment.