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 =
8 process.env.REACT_APP_ARVADOS_CONFIG_URL || '/config.json';
10 interface WorkbenchConfig {
12 VOCABULARY_URL?: string;
13 FILE_VIEWERS_CONFIG_URL?: string;
16 export interface ClusterConfigJSON {
18 UnfreezeProjectRequiresAdmin: boolean
19 MaxItemsPerResponse: number
23 ReserveExtraRAM: number;
30 DriverVersion: string;
31 HardwareCapability: string;
33 IncludedScratch: number;
43 ActivateUsers: boolean
51 SupportEmailAddress: string;
64 DisableSharingURLsUI: boolean;
65 ArvadosDocsite: string;
66 FileViewersConfigURL: string;
67 WelcomePageHTML: string;
68 InactivePageHTML: string;
69 SSHHelpPageHTML: string;
70 SSHHelpHostSuffix: string;
88 DisableSharingURLsUI: boolean;
89 ArvadosDocsite: string;
90 FileViewersConfigURL: string;
91 WelcomePageHTML: string;
92 InactivePageHTML: string;
93 SSHHelpPageHTML: string;
94 SSHHelpHostSuffix: string;
98 UserProfileFormFields: {};
99 UserProfileFormMessage: string;
102 LoginCluster: string;
123 ForwardSlashNameSubstitution: string;
124 ManagedProperties?: {
131 TrustAllContent: boolean;
136 [key: string]: boolean;
141 AnonymousUserToken: string;
145 export class Config {
147 keepWebServiceUrl!: string;
148 keepWebInlineServiceUrl!: string;
150 [key: string]: string;
154 websocketUrl!: string;
155 workbenchUrl!: string;
156 workbench2Url!: string;
157 vocabularyUrl!: string;
158 fileViewersConfigUrl!: string;
159 loginCluster!: string;
160 clusterConfig!: ClusterConfigJSON;
161 apiRevision!: number;
164 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
165 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
166 const config = new Config();
167 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
168 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
169 config.uuidPrefix = clusterConfigJSON.ClusterID;
170 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
171 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
172 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
173 config.keepWebServiceUrl =
174 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
175 config.keepWebInlineServiceUrl =
176 clusterConfigJSON.Services.WebDAV.ExternalURL;
177 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
178 config.clusterConfig = clusterConfigJSON;
179 config.apiRevision = 0;
180 mapRemoteHosts(clusterConfigJSON, config);
184 export const getStorageClasses = (config: Config): string[] => {
185 const classes: Set<string> = new Set(['default']);
186 const volumes = config.clusterConfig.Volumes;
187 Object.keys(volumes).forEach((v) => {
188 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
189 if (volumes[v].StorageClasses[sc]) {
194 return Array.from(classes);
197 const getApiRevision = async (apiUrl: string) => {
199 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
200 return parseInt(dd.revision, 10) || 0;
203 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
209 const removeTrailingSlashes = (
210 config: ClusterConfigJSON
211 ): ClusterConfigJSON => {
212 const svcs: any = {};
213 Object.keys(config.Services).forEach((s) => {
214 svcs[s] = config.Services[s];
215 if (svcs[s].hasOwnProperty('ExternalURL')) {
216 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
219 return { ...config, Services: svcs };
222 export const fetchConfig = () => {
223 return Axios.get<WorkbenchConfig>(
224 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
226 .then((response) => response.data)
229 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
231 return Promise.resolve(getDefaultConfig());
233 .then((workbenchConfig) => {
234 if (workbenchConfig.API_HOST === undefined) {
236 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
239 return Axios.get<ClusterConfigJSON>(
240 getClusterConfigURL(workbenchConfig.API_HOST)
241 ).then(async (response) => {
242 const apiRevision = await getApiRevision(
243 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
245 const config = { ...buildConfig(response.data), apiRevision };
246 const warnLocalConfig = (varName: string) =>
248 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
249 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
252 // Check if the workbench config has an entry for vocabulary and file viewer URLs
253 // If so, use these values (even if it is an empty string), but print a console warning.
254 // Otherwise, use the cluster config.
255 let fileViewerConfigUrl;
256 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
257 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
258 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
260 fileViewerConfigUrl =
261 config.clusterConfig.Workbench.FileViewersConfigURL ||
262 '/file-viewers-example.json';
264 config.fileViewersConfigUrl = fileViewerConfigUrl;
266 if (workbenchConfig.VOCABULARY_URL !== undefined) {
268 `A value for VOCABULARY_URL was found in ${WORKBENCH_CONFIG_URL}. It will be ignored as the cluster already provides its own endpoint, you can safely remove it.`
271 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
273 return { config, apiHost: workbenchConfig.API_HOST };
278 // Maps remote cluster hosts and removes the default RemoteCluster entry
279 export const mapRemoteHosts = (
280 clusterConfigJSON: ClusterConfigJSON,
283 config.remoteHosts = {};
284 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
285 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
287 delete config.remoteHosts['*'];
290 export const mockClusterConfigJSON = (
291 config: Partial<ClusterConfigJSON>
292 ): ClusterConfigJSON => ({
294 UnfreezeProjectRequiresAdmin: false,
295 MaxItemsPerResponse: 1000,
299 ReserveExtraRAM: 576716800,
303 Controller: { ExternalURL: '' },
304 Workbench1: { ExternalURL: '' },
305 Workbench2: { ExternalURL: '' },
306 Websocket: { ExternalURL: '' },
307 WebDAV: { ExternalURL: '' },
308 WebDAVDownload: { ExternalURL: '' },
309 WebShell: { ExternalURL: '' },
311 DisableSharingURLsUI: false,
313 FileViewersConfigURL: "",
315 InactivePageHTML: "",
317 SSHHelpHostSuffix: "",
323 DisableSharingURLsUI: false,
325 FileViewersConfigURL: '',
327 InactivePageHTML: '',
329 SSHHelpHostSuffix: '',
333 UserProfileFormFields: {},
334 UserProfileFormMessage: '',
358 ForwardSlashNameSubstitution: '',
359 TrustAllContent: false,
363 AnonymousUserToken: ""
368 export const mockConfig = (config: Partial<Config>): Config => ({
370 keepWebServiceUrl: '',
371 keepWebInlineServiceUrl: '',
379 fileViewersConfigUrl: '',
381 clusterConfig: mockClusterConfigJSON({}),
386 const getDefaultConfig = (): WorkbenchConfig => {
388 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
389 if (envHost !== undefined) {
390 console.warn(`Using default API host ${envHost}.`);
394 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
399 VOCABULARY_URL: undefined,
400 FILE_VIEWERS_CONFIG_URL: undefined,
404 export const ARVADOS_API_PATH = 'arvados/v1';
405 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
406 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
407 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
408 export const getClusterConfigURL = (apiHost: string) =>
409 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
410 export const getVocabularyURL = (apiHost: string) =>
411 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;