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;
85 ForwardSlashNameSubstitution: string;
87 responsible_person_uuid?: {
96 keepWebServiceUrl: string;
97 keepWebInlineServiceUrl: string;
103 websocketUrl: string;
104 workbenchUrl: string;
105 workbench2Url: string;
106 vocabularyUrl: string;
107 fileViewersConfigUrl: string;
108 loginCluster: string;
109 clusterConfig: ClusterConfigJSON;
113 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
114 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
115 const config = new Config();
116 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
117 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
118 config.uuidPrefix = clusterConfigJSON.ClusterID;
119 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
120 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
121 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
122 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
123 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
124 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
125 config.clusterConfig = clusterConfigJSON;
126 config.apiRevision = 0;
127 mapRemoteHosts(clusterConfigJSON, config);
131 const getApiRevision = async (apiUrl: string) => {
133 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
134 return parseInt(dd.revision, 10) || 0;
136 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
141 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
142 const svcs: any = {};
143 Object.keys(config.Services).map((s) => {
144 svcs[s] = config.Services[s];
145 if (svcs[s].hasOwnProperty('ExternalURL')) {
146 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
149 return { ...config, Services: svcs };
152 export const fetchConfig = () => {
154 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
155 .then(response => response.data)
157 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
158 return Promise.resolve(getDefaultConfig());
160 .then(workbenchConfig => {
161 if (workbenchConfig.API_HOST === undefined) {
162 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
164 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
165 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
166 const config = { ...buildConfig(response.data), apiRevision };
167 const warnLocalConfig = (varName: string) => console.warn(
168 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
169 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
171 // Check if the workbench config has an entry for vocabulary and file viewer URLs
172 // If so, use these values (even if it is an empty string), but print a console warning.
173 // Otherwise, use the cluster config.
174 let fileViewerConfigUrl;
175 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
176 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
177 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
180 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
182 config.fileViewersConfigUrl = fileViewerConfigUrl;
185 if (workbenchConfig.VOCABULARY_URL !== undefined) {
186 warnLocalConfig("VOCABULARY_URL");
187 vocabularyUrl = workbenchConfig.VOCABULARY_URL;
190 vocabularyUrl = config.clusterConfig.Workbench.VocabularyURL || "/vocabulary-example.json";
192 config.vocabularyUrl = vocabularyUrl;
194 return { config, apiHost: workbenchConfig.API_HOST };
199 // Maps remote cluster hosts and removes the default RemoteCluster entry
200 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
201 config.remoteHosts = {};
202 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
203 delete config.remoteHosts["*"];
206 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
210 Controller: { ExternalURL: "" },
211 Workbench1: { ExternalURL: "" },
212 Workbench2: { ExternalURL: "" },
213 Websocket: { ExternalURL: "" },
214 WebDAV: { ExternalURL: "" },
215 WebDAVDownload: { ExternalURL: "" },
216 WebShell: { ExternalURL: "" },
221 FileViewersConfigURL: "",
223 InactivePageHTML: "",
225 SSHHelpHostSuffix: "",
251 ForwardSlashNameSubstitution: "",
256 export const mockConfig = (config: Partial<Config>): Config => ({
258 keepWebServiceUrl: "",
259 keepWebInlineServiceUrl: "",
267 fileViewersConfigUrl: "",
269 clusterConfig: mockClusterConfigJSON({}),
274 const getDefaultConfig = (): WorkbenchConfig => {
276 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
277 if (envHost !== undefined) {
278 console.warn(`Using default API host ${envHost}.`);
282 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
286 VOCABULARY_URL: undefined,
287 FILE_VIEWERS_CONFIG_URL: undefined,
291 export const ARVADOS_API_PATH = "arvados/v1";
292 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
293 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
294 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;