Merge branch 'master' into 15672-subprocess-list-v2
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 interface WorkbenchConfig {
10     API_HOST: string;
11     VOCABULARY_URL?: string;
12     FILE_VIEWERS_CONFIG_URL?: string;
13 }
14
15 export interface ClusterConfigJSON {
16     ClusterID: string;
17     RemoteClusters: {
18         [key: string]: {
19             ActivateUsers: boolean
20             Host: string
21             Insecure: boolean
22             Proxy: boolean
23             Scheme: string
24         }
25     };
26     Services: {
27         Controller: {
28             ExternalURL: string
29         }
30         Workbench1: {
31             ExternalURL: string
32         }
33         Workbench2: {
34             ExternalURL: string
35         }
36         Websocket: {
37             ExternalURL: string
38         }
39         WebDAV: {
40             ExternalURL: string
41         },
42         WebDAVDownload: {
43             ExternalURL: string
44         }
45     };
46     Workbench: {
47         ArvadosDocsite: string;
48         VocabularyURL: string;
49         FileViewersConfigURL: string;
50         WelcomePageHTML: string;
51         InactivePageHTML: string;
52         SiteName: string;
53     };
54     Login: {
55         LoginCluster: string;
56     };
57 }
58
59 export class Config {
60     baseUrl: string;
61     keepWebServiceUrl: string;
62     remoteHosts: {
63         [key: string]: string
64     };
65     rootUrl: string;
66     uuidPrefix: string;
67     websocketUrl: string;
68     workbenchUrl: string;
69     workbench2Url: string;
70     vocabularyUrl: string;
71     fileViewersConfigUrl: string;
72     loginCluster: string;
73     clusterConfig: ClusterConfigJSON;
74 }
75
76 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
77     const config = new Config();
78     config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
79     config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
80     config.uuidPrefix = clusterConfigJSON.ClusterID;
81     config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
82     config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
83     config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
84     config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
85     config.loginCluster = clusterConfigJSON.Login.LoginCluster;
86     config.clusterConfig = clusterConfigJSON;
87     mapRemoteHosts(clusterConfigJSON, config);
88     return config;
89 };
90
91 export const fetchConfig = () => {
92     return Axios
93         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
94         .then(response => response.data)
95         .catch(() => {
96             console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
97             return Promise.resolve(getDefaultConfig());
98         })
99         .then(workbenchConfig => {
100             if (workbenchConfig.API_HOST === undefined) {
101                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
102             }
103             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
104                 const clusterConfigJSON = response.data;
105                 const config = buildConfig(clusterConfigJSON);
106                 const warnLocalConfig = (varName: string) => console.warn(
107                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
108 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
109
110                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
111                 // If so, use these values (even if it is an empty string), but print a console warning.
112                 // Otherwise, use the cluster config.
113                 let fileViewerConfigUrl;
114                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
115                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
116                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
117                 }
118                 else {
119                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
120                 }
121                 config.fileViewersConfigUrl = fileViewerConfigUrl;
122
123                 let vocabularyUrl;
124                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
125                     warnLocalConfig("VOCABULARY_URL");
126                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
127                 }
128                 else {
129                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
130                 }
131                 config.vocabularyUrl = vocabularyUrl;
132
133                 return { config, apiHost: workbenchConfig.API_HOST };
134             });
135         });
136 };
137
138 // Maps remote cluster hosts and removes the default RemoteCluster entry
139 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
140     config.remoteHosts = {};
141     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
142     delete config.remoteHosts["*"];
143 };
144
145 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
146     ClusterID: "",
147     RemoteClusters: {},
148     Services: {
149         Controller: { ExternalURL: "" },
150         Workbench1: { ExternalURL: "" },
151         Workbench2: { ExternalURL: "" },
152         Websocket: { ExternalURL: "" },
153         WebDAV: { ExternalURL: "" },
154         WebDAVDownload: { ExternalURL: "" },
155     },
156     Workbench: {
157         ArvadosDocsite: "",
158         VocabularyURL: "",
159         FileViewersConfigURL: "",
160         WelcomePageHTML: "",
161         InactivePageHTML: "",
162         SiteName: "",
163     },
164     Login: {
165         LoginCluster: "",
166     },
167     ...config
168 });
169
170 export const mockConfig = (config: Partial<Config>): Config => ({
171     baseUrl: "",
172     keepWebServiceUrl: "",
173     remoteHosts: {},
174     rootUrl: "",
175     uuidPrefix: "",
176     websocketUrl: "",
177     workbenchUrl: "",
178     workbench2Url: "",
179     vocabularyUrl: "",
180     fileViewersConfigUrl: "",
181     loginCluster: "",
182     clusterConfig: mockClusterConfigJSON({}),
183     ...config
184 });
185
186 const getDefaultConfig = (): WorkbenchConfig => {
187     let apiHost = "";
188     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
189     if (envHost !== undefined) {
190         console.warn(`Using default API host ${envHost}.`);
191         apiHost = envHost;
192     }
193     else {
194         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
195     }
196     return {
197         API_HOST: apiHost,
198         VOCABULARY_URL: undefined,
199         FILE_VIEWERS_CONFIG_URL: undefined,
200     };
201 };
202
203 export const ARVADOS_API_PATH = "arvados/v1";
204 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
205 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
206 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;