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

Fix webview after changes in upstream. #563

Merged
merged 1 commit into from
Nov 27, 2019
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
1 change: 1 addition & 0 deletions dockerfiles/theia/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ COPY --from=builder /home/theia-dev/theia-source-code/production/plugins /defaul
COPY --chown=theia:root --from=builder /home/theia-dev/theia-source-code/production /home/theia
USER theia
WORKDIR /projects
COPY src/get-webview-route.js ${HOME}/get-webview-route.js
Copy link
Contributor

@benoitf benoitf Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be possible (maybe later) to use a runtime information instead of using environment variable.

Explanation: Here we're using a shell script before running the theia process in the container (by setting environment variable)

I think it would be cleaner if we bring a hook/extension point of theia at runtime by updating the setting on the fly. So no need to use separate script and fix env variable. It's when theia webview will resolve stuff that it will deal with che-theia code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we can do it in runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try, but as for now I would like to merge current PR as it is tested and fixes (for https case) blocker issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is why I said later

COPY src/entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
3 changes: 2 additions & 1 deletion dockerfiles/theia/src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ if [ "${NOCDN}" == "true" ]; then
fi
shopt -u nocasematch

# run che
# run Che Theia
export THEIA_WEBVIEW_EXTERNAL_ENDPOINT=$(node get-webview-route.js)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: we don't have the endpoint as environment variable ?

Copy link
Contributor

@benoitf benoitf Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, che-server is not providing that information to containers through env vars ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't have external route in env vars.

node src-gen/backend/main.js /projects --hostname=0.0.0.0 --port=${THEIA_PORT} &

PID=$!
Expand Down
73 changes: 73 additions & 0 deletions dockerfiles/theia/src/get-webview-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*********************************************************************
* Copyright (c) 2019 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
**********************************************************************/

const fs = require('fs');
const workspaceClient = require('@eclipse-che/workspace-client');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does it find this dependency ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets the dependency from Che Theia node modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the dockerfile we copy script to the /home/theia, where is located node_modules


// Assume that self-signed certificate is located by the following path
const SS_CRT_PATH = '/tmp/che/secret/ca.crt';

// Configure workspace API client
const restAPIConfig = {
baseUrl: process.env.CHE_API
};

const token = process.env.CHE_MACHINE_TOKEN;
if (token) {
restAPIConfig.headers = {};
restAPIConfig.headers['Authorization'] = 'Bearer ' + token;
}

if (fs.existsSync(SS_CRT_PATH)) {
restAPIConfig.ssCrtPath = SS_CRT_PATH;
}

// Create rest API workspace client
const restApiClient = workspaceClient.default.getRestApi(restAPIConfig);

// Search for IDE route
function getIdeRoute() {
return new Promise((resolve, reject) => {
restApiClient.getById(process.env.CHE_WORKSPACE_ID).then(workspace => {
const containers = workspace.runtime.machines;
Object.keys(containers).forEach(containerName => {
const container = containers[containerName];
const servers = container['servers'];
if (servers) {
const ideServerName = Object.keys(servers).find(serverName => servers[serverName].attributes && servers[serverName].attributes['type'] === 'ide');
if (ideServerName) {
resolve(servers[ideServerName].url);
}
}
});
reject('Server with type "ide" not found.');
}).catch(error => {
reject(error);
});
});
}

getIdeRoute().then(ideRoute => {
if (!ideRoute) {
throw new Error('Failed to get ide route.');
}

// Remove trailing slash if any
if (ideRoute.endsWith('/')) {
ideRoute = ideRoute.substring(0, ideRoute.length - 1);
}
// Remove protocol
const webviewDomain = ideRoute.replace(/^https?:\/\//, '');
// Return result to shell by writing into stdout
process.stdout.write(webviewDomain);
}).catch(error => {
console.error('Unable to get IDE route. Webviews might not work. Cause:', error);
// Just exit this script without returning a value to the shell.
});