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;
98 keepWebServiceUrl!: string;
99 keepWebInlineServiceUrl!: string;
101 [key: string]: string
105 websocketUrl!: string;
106 workbenchUrl!: string;
107 workbench2Url!: string;
108 vocabularyUrl!: string;
109 fileViewersConfigUrl!: string;
110 loginCluster!: string;
111 clusterConfig!: ClusterConfigJSON;
112 apiRevision!: number;
115 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
116 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
117 const config = new Config();
118 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
119 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
120 config.uuidPrefix = clusterConfigJSON.ClusterID;
121 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
122 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
123 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
124 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
125 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
126 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
127 config.clusterConfig = clusterConfigJSON;
128 config.apiRevision = 0;
129 mapRemoteHosts(clusterConfigJSON, config);
133 const getApiRevision = async (apiUrl: string) => {
135 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
136 return parseInt(dd.revision, 10) || 0;
138 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
143 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
144 const svcs: any = {};
145 Object.keys(config.Services).forEach((s) => {
146 svcs[s] = config.Services[s];
147 if (svcs[s].hasOwnProperty('ExternalURL')) {
148 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
151 return { ...config, Services: svcs };
154 export const fetchConfig = () => {
156 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
157 .then(response => response.data)
159 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
160 return Promise.resolve(getDefaultConfig());
162 .then(workbenchConfig => {
163 if (workbenchConfig.API_HOST === undefined) {
164 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
166 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
167 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
168 const config = { ...buildConfig(response.data), apiRevision };
169 const warnLocalConfig = (varName: string) => console.warn(
170 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
171 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
173 // Check if the workbench config has an entry for vocabulary and file viewer URLs
174 // If so, use these values (even if it is an empty string), but print a console warning.
175 // Otherwise, use the cluster config.
176 let fileViewerConfigUrl;
177 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
178 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
179 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
182 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
184 config.fileViewersConfigUrl = fileViewerConfigUrl;
187 if (workbenchConfig.VOCABULARY_URL !== undefined) {
188 warnLocalConfig("VOCABULARY_URL");
189 vocabularyUrl = workbenchConfig.VOCABULARY_URL;
192 vocabularyUrl = config.clusterConfig.Workbench.VocabularyURL || "/vocabulary-example.json";
194 config.vocabularyUrl = vocabularyUrl;
196 return { config, apiHost: workbenchConfig.API_HOST };
201 // Maps remote cluster hosts and removes the default RemoteCluster entry
202 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
203 config.remoteHosts = {};
204 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
205 delete config.remoteHosts["*"];
208 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
212 Controller: { ExternalURL: "" },
213 Workbench1: { ExternalURL: "" },
214 Workbench2: { ExternalURL: "" },
215 Websocket: { ExternalURL: "" },
216 WebDAV: { ExternalURL: "" },
217 WebDAVDownload: { ExternalURL: "" },
218 WebShell: { ExternalURL: "" },
223 FileViewersConfigURL: "",
225 InactivePageHTML: "",
227 SSHHelpHostSuffix: "",
253 ForwardSlashNameSubstitution: "",
258 export const mockConfig = (config: Partial<Config>): Config => ({
260 keepWebServiceUrl: "",
261 keepWebInlineServiceUrl: "",
269 fileViewersConfigUrl: "",
271 clusterConfig: mockClusterConfigJSON({}),
276 const getDefaultConfig = (): WorkbenchConfig => {
278 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
279 if (envHost !== undefined) {
280 console.warn(`Using default API host ${envHost}.`);
284 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
288 VOCABULARY_URL: undefined,
289 FILE_VIEWERS_CONFIG_URL: undefined,
293 export const ARVADOS_API_PATH = "arvados/v1";
294 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
295 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
296 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;