Skip to content

Commit

Permalink
fix #3967 : replace WSMaster Machine API by Exec Agent calls
Browse files Browse the repository at this point in the history
Change-Id: I67e9d668516bf634c47e72653086e93ef34d5fe9
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf committed Feb 28, 2017
1 parent 7fc0a76 commit f1a8807
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 201 deletions.
5 changes: 0 additions & 5 deletions dockerfiles/lib/dto-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@
<artifactId>che-core-api-git-shared</artifactId>
<version>${che.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-machine-shared</artifactId>
<version>${che.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-project-shared</artifactId>
Expand Down
105 changes: 105 additions & 0 deletions dockerfiles/lib/src/api/exec-agent/exec-agent-service-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2016-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
import {org} from "../../api/dto/che-dto";
import {ProcessTerminatedEventPromiseMessageBusSubscriber} from "./process-terminated-event-promise-subscriber";
import {AuthData} from "../wsmaster/auth/auth-data";
import {Websocket} from "../../spi/websocket/websocket";
import {Workspace} from "../wsmaster/workspace/workspace";
import {CheFileStructWorkspaceCommand} from "../../internal/dir/chefile-struct/che-file-struct";
import {Log} from "../../spi/log/log";
import {JsonRpcBus} from "../../spi/websocket/json-rpc-bus";
import {ProcessAckPromiseMessageBusSubscriber} from "./process-ack-event-promise-subscriber";

/**
* Exec Agent service allowing to start/stop processes
* @author Florent Benoit
*/
export class ExecAgentServiceClientImpl {

/**
* Authentication data
*/
authData : AuthData;

/**
* websocket.
*/
websocket : Websocket;

workspace : Workspace;

constructor(workspace : Workspace, authData : AuthData) {
this.workspace = workspace;
this.authData = authData;
this.websocket = new Websocket();
}

getJsonRpcBus(workspaceDto : org.eclipse.che.api.workspace.shared.dto.WorkspaceDto): Promise<JsonRpcBus> {
var protocol:string;
if (this.authData.isSecured()) {
protocol = 'wss';
} else {
protocol = 'ws';
}

// get links for WS
var link:string = protocol + '://' + this.authData.hostname + ":" + this.authData.port + '/connect';

return this.websocket.getJsonRpcBus(link + '?token=' + this.authData.getToken());
}

/**
* Create a workspace and return a promise with content of WorkspaceDto in case of success
*/
executeCommand(workspaceDto : org.eclipse.che.api.workspace.shared.dto.WorkspaceDto,
machineId:string,
cheFileStructWorkspaceCommand:CheFileStructWorkspaceCommand,
uuid:string,
asynchronous : boolean = true):Promise<boolean> {

let rpcCommand: any =
{
"jsonrpc": "2.0",
"method": "process.start",
"id": uuid,
"params": {
"commandLine": cheFileStructWorkspaceCommand.commandLine,
"name": !cheFileStructWorkspaceCommand.name ? "custom-command" : cheFileStructWorkspaceCommand.name,
"type": !cheFileStructWorkspaceCommand.type ? "custom" : cheFileStructWorkspaceCommand.type
}
};

// get JSON RPC Bus
let processTerminatedEventPromiseMessageBusSubscriber : ProcessTerminatedEventPromiseMessageBusSubscriber;
let userJsonRpcBus : JsonRpcBus;
return this.getJsonRpcBus(workspaceDto).then((jsonRpcBus:JsonRpcBus) => {
userJsonRpcBus = jsonRpcBus;
processTerminatedEventPromiseMessageBusSubscriber = new ProcessTerminatedEventPromiseMessageBusSubscriber(jsonRpcBus);
let processAckPromiseMessageBusSubscriber : ProcessAckPromiseMessageBusSubscriber = new ProcessAckPromiseMessageBusSubscriber(uuid, jsonRpcBus, asynchronous, processTerminatedEventPromiseMessageBusSubscriber);
jsonRpcBus.subscribe(processAckPromiseMessageBusSubscriber);
jsonRpcBus.send(rpcCommand);
return processAckPromiseMessageBusSubscriber.promise;
}).then(() => {
// subscribe to pid event end
if (asynchronous) {
return processTerminatedEventPromiseMessageBusSubscriber.promise;
} else {
// do not wait the end
userJsonRpcBus.close();
return Promise.resolve(true);
}
}).then(() => {
return true;
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2016-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/

import {MessageBusSubscriber} from "../../spi/websocket/messagebus-subscriber";
import {ProcessTerminatedEventPromiseMessageBusSubscriber} from "./process-terminated-event-promise-subscriber";
import {JsonRpcBus} from "../../spi/websocket/json-rpc-bus";
import {ProcesLogOutputMessageBusSubscriber} from "./process-log-output-subscriber";
import {Log} from "../../spi/log/log";
/**
* Handle a promise that will be resolved when process/command is finished.
* If process has error, promise will be rejected
* @author Florent Benoit
*/
export class ProcessAckPromiseMessageBusSubscriber implements MessageBusSubscriber {

resolve : any;
reject : any;
promise: Promise<boolean>;
private id : string;
private processTerminatedEventPromiseMessageBusSubscriber : ProcessTerminatedEventPromiseMessageBusSubscriber;
private asynchronous : boolean;
private jsonRpcBus : JsonRpcBus;

constructor(id: string, jsonRpcBus : JsonRpcBus, asynchronous : boolean, processTerminatedEventPromiseMessageBusSubscriber : ProcessTerminatedEventPromiseMessageBusSubscriber) {
this.id = id;
this.jsonRpcBus = jsonRpcBus;
this.asynchronous = asynchronous;
this.processTerminatedEventPromiseMessageBusSubscriber = processTerminatedEventPromiseMessageBusSubscriber;
this.promise = new Promise<boolean>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}

handleMessage(event: any) {
if (event.id && event.id === this.id) {
if (event.result) {
this.processTerminatedEventPromiseMessageBusSubscriber.setPid(event.result.pid);
// subscribe to websocket
if (this.asynchronous) {
this.jsonRpcBus.subscribe(new ProcesLogOutputMessageBusSubscriber(event.result.pid));
this.jsonRpcBus.subscribe(this.processTerminatedEventPromiseMessageBusSubscriber);
}
this.resolve(true);
} else if (event.error) {
this.reject(event.error);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
* Codenvy, S.A. - initial API and implementation
*/

import {MessageBusSubscriber} from "../../../spi/websocket/messagebus-subscriber";
import {StringUtils} from "../../../utils/string-utils";
import {Log} from "../../../spi/log/log";
import {MessageBusSubscriber} from "../../spi/websocket/messagebus-subscriber";
import {Log} from "../../spi/log/log";
/**
* Class that will display to console all process output messages.
* @author Florent Benoit
*/
export class ProcesLogOutputMessageBusSubscriber implements MessageBusSubscriber {

handleMessage(message: string) {
if (StringUtils.startsWith(message, '[STDOUT] ')) {
console.log(Log.GREEN + message.substr('[STDOUT] '.length) + Log.NC);
} else if (StringUtils.startsWith(message, '[STDERR] ')) {
console.log(Log.RED + message.substr('[STDERR] '.length) + Log.NC);
} else {
console.log(message);
private id : string;


constructor(id : string) {
this.id = id;
}

handleMessage(event: any) {
if (event.params && event.params.pid === this.id) {
if (event.method === "process_stdout") {
console.log(Log.GREEN + event.params.text + Log.NC);
} else if (event.method === "process_stderr") {
console.log(Log.RED + event.params.text + Log.NC);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2016-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/

import {MessageBusSubscriber} from "../../spi/websocket/messagebus-subscriber";
import {Log} from "../../spi/log/log";
import {JsonRpcBus} from "../../spi/websocket/json-rpc-bus";
/**
* Handle a promise that will be resolved when process/command is finished.
* If process has error, promise will be rejected
* @author Florent Benoit
*/
export class ProcessTerminatedEventPromiseMessageBusSubscriber implements MessageBusSubscriber {

resolve : any;
reject : any;
promise: Promise<boolean>;
private pid : number;
private jsonRpcBus;

constructor(jsonRpcBus : JsonRpcBus) {
this.jsonRpcBus = jsonRpcBus;
this.promise = new Promise<boolean>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}

handleMessage(event: any) {
if (this.pid) {
if ('process_died' === event.method && event.params && event.params.pid === this.pid) {
this.jsonRpcBus.close();
this.resolve(true);
}
}
}

setPid(pid : number) {
this.pid = pid;
}

}
104 changes: 0 additions & 104 deletions dockerfiles/lib/src/api/wsmaster/machine/machine-service-client.ts

This file was deleted.

Loading

0 comments on commit f1a8807

Please sign in to comment.