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
50 ArvadosDocsite: string;
51 VocabularyURL: string;
52 FileViewersConfigURL: string;
53 WelcomePageHTML: string;
54 InactivePageHTML: string;
55 SSHHelpPageHTML: string;
56 SSHHelpHostSuffix: string;
75 ForwardSlashNameSubstitution: string;
81 keepWebServiceUrl: string;
89 workbench2Url: string;
90 vocabularyUrl: string;
91 fileViewersConfigUrl: string;
93 clusterConfig: ClusterConfigJSON;
97 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
98 const config = new Config();
99 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
100 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
101 config.uuidPrefix = clusterConfigJSON.ClusterID;
102 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
103 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
104 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
105 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
106 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
107 config.clusterConfig = clusterConfigJSON;
108 config.apiRevision = 0;
109 mapRemoteHosts(clusterConfigJSON, config);
113 const getApiRevision = async (apiUrl: string) => {
115 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
116 return parseInt(dd.revision, 10) || 0;
118 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
123 export const fetchConfig = () => {
125 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
126 .then(response => response.data)
128 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
129 return Promise.resolve(getDefaultConfig());
131 .then(workbenchConfig => {
132 if (workbenchConfig.API_HOST === undefined) {
133 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
135 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
136 const clusterConfigJSON = response.data;
137 const apiRevision = await getApiRevision(clusterConfigJSON.Services.Controller.ExternalURL);
138 const config = { ...buildConfig(clusterConfigJSON), apiRevision };
139 const warnLocalConfig = (varName: string) => console.warn(
140 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
141 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
143 // Check if the workbench config has an entry for vocabulary and file viewer URLs
144 // If so, use these values (even if it is an empty string), but print a console warning.
145 // Otherwise, use the cluster config.
146 let fileViewerConfigUrl;
147 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
148 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
149 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
152 fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
154 config.fileViewersConfigUrl = fileViewerConfigUrl;
157 if (workbenchConfig.VOCABULARY_URL !== undefined) {
158 warnLocalConfig("VOCABULARY_URL");
159 vocabularyUrl = workbenchConfig.VOCABULARY_URL;
162 vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
164 config.vocabularyUrl = vocabularyUrl;
166 return { config, apiHost: workbenchConfig.API_HOST };
171 // Maps remote cluster hosts and removes the default RemoteCluster entry
172 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
173 config.remoteHosts = {};
174 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
175 delete config.remoteHosts["*"];
178 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
182 Controller: { ExternalURL: "" },
183 Workbench1: { ExternalURL: "" },
184 Workbench2: { ExternalURL: "" },
185 Websocket: { ExternalURL: "" },
186 WebDAV: { ExternalURL: "" },
187 WebDAVDownload: { ExternalURL: "" },
188 WebShell: { ExternalURL: "" },
193 FileViewersConfigURL: "",
195 InactivePageHTML: "",
197 SSHHelpHostSuffix: "",
216 ForwardSlashNameSubstitution: "",
221 export const mockConfig = (config: Partial<Config>): Config => ({
223 keepWebServiceUrl: "",
231 fileViewersConfigUrl: "",
233 clusterConfig: mockClusterConfigJSON({}),
238 const getDefaultConfig = (): WorkbenchConfig => {
240 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
241 if (envHost !== undefined) {
242 console.warn(`Using default API host ${envHost}.`);
246 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
250 VOCABULARY_URL: undefined,
251 FILE_VIEWERS_CONFIG_URL: undefined,
255 export const ARVADOS_API_PATH = "arvados/v1";
256 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
257 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
258 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;