1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import Axios from "axios";
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
9 interface WorkbenchConfig {
11 VOCABULARY_URL?: string;
12 FILE_VIEWERS_CONFIG_URL?: string;
15 export interface ClusterConfigJSON {
19 ActivateUsers: boolean
27 SupportEmailAddress: string;
53 ArvadosDocsite: string;
54 VocabularyURL: string;
55 FileViewersConfigURL: string;
56 WelcomePageHTML: string;
57 InactivePageHTML: string;
58 SSHHelpPageHTML: string;
59 SSHHelpHostSuffix: string;
81 ForwardSlashNameSubstitution: string;
87 keepWebServiceUrl: string;
95 workbench2Url: string;
96 vocabularyUrl: string;
97 fileViewersConfigUrl: string;
99 clusterConfig: ClusterConfigJSON;
103 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
104 const config = new Config();
105 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
106 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
107 config.uuidPrefix = clusterConfigJSON.ClusterID;
108 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
109 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
110 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
111 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
112 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
113 config.clusterConfig = clusterConfigJSON;
114 config.apiRevision = 0;
115 mapRemoteHosts(clusterConfigJSON, config);
119 const getApiRevision = async (apiUrl: string) => {
121 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
122 return parseInt(dd.revision, 10) || 0;
124 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
129 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
130 const svcs: any = {};
131 Object.keys(config.Services).map((s) => {
132 svcs[s] = config.Services[s];
133 if (svcs[s].hasOwnProperty('ExternalURL')) {
134 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
137 return {...config, Services: svcs};
140 export const fetchConfig = () => {
142 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
143 .then(response => response.data)
145 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
146 return Promise.resolve(getDefaultConfig());
148 .then(workbenchConfig => {
149 if (workbenchConfig.API_HOST === undefined) {
150 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
152 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
153 const clusterConfigJSON = removeTrailingSlashes(response.data);
154 const apiRevision = await getApiRevision(clusterConfigJSON.Services.Controller.ExternalURL);
155 const config = { ...buildConfig(clusterConfigJSON), apiRevision };
156 const warnLocalConfig = (varName: string) => console.warn(
157 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
158 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
160 // Check if the workbench config has an entry for vocabulary and file viewer URLs
161 // If so, use these values (even if it is an empty string), but print a console warning.
162 // Otherwise, use the cluster config.
163 let fileViewerConfigUrl;
164 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
165 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
166 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
169 fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
171 config.fileViewersConfigUrl = fileViewerConfigUrl;
174 if (workbenchConfig.VOCABULARY_URL !== undefined) {
175 warnLocalConfig("VOCABULARY_URL");
176 vocabularyUrl = workbenchConfig.VOCABULARY_URL;
179 vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
181 config.vocabularyUrl = vocabularyUrl;
183 return { config, apiHost: workbenchConfig.API_HOST };
188 // Maps remote cluster hosts and removes the default RemoteCluster entry
189 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
190 config.remoteHosts = {};
191 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
192 delete config.remoteHosts["*"];
195 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
199 Controller: { ExternalURL: "" },
200 Workbench1: { ExternalURL: "" },
201 Workbench2: { ExternalURL: "" },
202 Websocket: { ExternalURL: "" },
203 WebDAV: { ExternalURL: "" },
204 WebDAVDownload: { ExternalURL: "" },
205 WebShell: { ExternalURL: "" },
210 FileViewersConfigURL: "",
212 InactivePageHTML: "",
214 SSHHelpHostSuffix: "",
236 ForwardSlashNameSubstitution: "",
241 export const mockConfig = (config: Partial<Config>): Config => ({
243 keepWebServiceUrl: "",
251 fileViewersConfigUrl: "",
253 clusterConfig: mockClusterConfigJSON({}),
258 const getDefaultConfig = (): WorkbenchConfig => {
260 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
261 if (envHost !== undefined) {
262 console.warn(`Using default API host ${envHost}.`);
266 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
270 VOCABULARY_URL: undefined,
271 FILE_VIEWERS_CONFIG_URL: undefined,
275 export const ARVADOS_API_PATH = "arvados/v1";
276 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
277 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
278 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;