Merge branch '14813-cluster-config'
authorEric Biagiotti <ebiagiotti@veritasgenetics.com>
Thu, 1 Aug 2019 19:11:51 +0000 (15:11 -0400)
committerEric Biagiotti <ebiagiotti@veritasgenetics.com>
Thu, 1 Aug 2019 19:11:51 +0000 (15:11 -0400)
refs #14813

Arvados-DCO-1.1-Signed-off-by: Eric Biagiotti <ebiagiotti@veritasgenetics.com>

README.md
src/common/config.ts
src/store/auth/auth-action-session.ts
src/store/auth/auth-action.ts

index 425d1787848e5b9dad4e077eed23b2f73aea47ea..e2e14ce47e06f7fc5ddc67b1732419f60d071a07 100644 (file)
--- a/README.md
+++ b/README.md
@@ -46,10 +46,18 @@ Currently this configuration schema is supported:
 }
 ```
 
+#### API_HOST
+
+The Arvados base URL. 
+
+The `REACT_APP_ARVADOS_API_HOST` environment variable can be used to set the default URL if the run time configuration is unreachable.
+
 #### VOCABULARY_URL
 Local path, or any URL that allows cross-origin requests. See 
 [Vocabulary JSON file example](public/vocabulary-example.json).
 
+To use the URL defined in the Arvados cluster configuration, remove the entire `VOCABULARY_URL` entry from the runtime configuration. Found in `/config.json` by default.
+
 ### FILE_VIEWERS_CONFIG_URL
 Local path, or any URL that allows cross-origin requests. See:
 
@@ -57,6 +65,8 @@ Local path, or any URL that allows cross-origin requests. See:
 
 [File viewers config scheme](src/models/file-viewers-config.ts)
 
+To use the URL defined in the Arvados cluster configuration, remove the entire `FILE_VIEWERS_CONFIG_URL` entry from the runtime configuration. Found in `/config.json` by default.
+
 ### Licensing
 
 Arvados is Free Software. See COPYING for information about Arvados Free
index 71b7774c5fa8d765818b7ce611f6e4bfa1805c11..496b0b78891b3657d7119abc0116b2ec1780ed91 100644 (file)
 
 import Axios from "axios";
 
-export const CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
+export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
 
-export interface Config {
-    auth: {};
-    basePath: string;
+interface WorkbenchConfig {
+    API_HOST: string;
+    VOCABULARY_URL?: string;
+    FILE_VIEWERS_CONFIG_URL?: string;
+}
+
+export interface ClusterConfigJSON {
+    ClusterID: string;
+    RemoteClusters: {
+        [key: string]: {
+            ActivateUsers: boolean
+            Host: string
+            Insecure: boolean
+            Proxy: boolean
+            Scheme: string
+        }
+    };
+    Services: {
+        Controller: {
+            ExternalURL: string
+        }
+        Workbench1: {
+            ExternalURL: string
+        }
+        Workbench2: {
+            ExternalURL: string
+        }
+        Websocket: {
+            ExternalURL: string
+        }
+        WebDAV: {
+            ExternalURL: string
+        }
+    };
+    Workbench: {
+        ArvadosDocsite: string;
+        VocabularyURL: string;
+        FileViewersConfigURL: string;
+    };
+}
+
+export class Config {
     baseUrl: string;
-    batchPath: string;
-    blobSignatureTtl: number;
-    crunchLimitLogBytesPerJob: number;
-    crunchLogBytesPerEvent: number;
-    crunchLogPartialLineThrottlePeriod: number;
-    crunchLogSecondsBetweenEvents: number;
-    crunchLogThrottleBytes: number;
-    crunchLogThrottleLines: number;
-    crunchLogThrottlePeriod: number;
-    defaultCollectionReplication: number;
-    defaultTrashLifetime: number;
-    description: string;
-    discoveryVersion: string;
-    dockerImageFormats: string[];
-    documentationLink: string;
-    generatedAt: string;
-    gitUrl: string;
-    id: string;
     keepWebServiceUrl: string;
-    kind: string;
-    maxRequestSize: number;
-    name: string;
-    packageVersion: string;
-    parameters: {};
-    protocol: string;
     remoteHosts: {
         [key: string]: string
     };
-    remoteHostsViaDNS: boolean;
-    resources: {};
-    revision: string;
     rootUrl: string;
-    schemas: {};
-    servicePath: string;
-    sourceVersion: string;
-    source_version: string;
-    title: string;
     uuidPrefix: string;
-    version: string;
     websocketUrl: string;
     workbenchUrl: string;
-    workbench2Url?: string;
+    workbench2Url: string;
     vocabularyUrl: string;
     fileViewersConfigUrl: string;
 }
 
 export const fetchConfig = () => {
     return Axios
-        .get<ConfigJSON>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
+        .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
         .then(response => response.data)
-        .catch(() => Promise.resolve(getDefaultConfig()))
-        .then(config => Axios
-            .get<Config>(getDiscoveryURL(config.API_HOST))
-            .then(response => ({
-                // TODO: After tests delete `|| '/vocabulary-example.json'`
-                // TODO: After tests delete `|| '/file-viewers-example.json'`
-                config: {
-                    ...response.data,
-                    vocabularyUrl: config.VOCABULARY_URL || '/vocabulary-example.json',
-                    fileViewersConfigUrl: config.FILE_VIEWERS_CONFIG_URL || '/file-viewers-example.json'
-                },
-                apiHost: config.API_HOST,
-            })));
+        .catch(() => {
+            console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
+            return Promise.resolve(getDefaultConfig());
+        })
+        .then(workbenchConfig => {
+            if (workbenchConfig.API_HOST === undefined) {
+                throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
+            }
+            return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
+                const config = new Config();
+                const clusterConfigJSON = response.data;
+                const warnLocalConfig = (varName: string) => console.warn(
+                    `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
+remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
+
+                // Check if the workbench config has an entry for vocabulary and file viewer URLs
+                // If so, use these values (even if it is an empty string), but print a console warning.
+                // Otherwise, use the cluster config.
+                let fileViewerConfigUrl;
+                if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
+                    warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
+                    fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
+                }
+                else {
+                    fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
+                }
+                config.fileViewersConfigUrl = fileViewerConfigUrl;
+
+                let vocabularyUrl;
+                if (workbenchConfig.VOCABULARY_URL !== undefined) {
+                    warnLocalConfig("VOCABULARY_URL");
+                    vocabularyUrl = workbenchConfig.VOCABULARY_URL;
+                }
+                else {
+                    vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
+                }
+                config.vocabularyUrl = vocabularyUrl;
 
+                config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
+                config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
+                config.uuidPrefix = clusterConfigJSON.ClusterID;
+                config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
+                config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
+                config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
+                config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
+                mapRemoteHosts(clusterConfigJSON, config);
+
+                return { config, apiHost: workbenchConfig.API_HOST };
+            });
+        });
+};
+
+// Maps remote cluster hosts and removes the default RemoteCluster entry
+export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
+    config.remoteHosts = {};
+    Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
+    delete config.remoteHosts["*"];
 };
 
 export const mockConfig = (config: Partial<Config>): Config => ({
-    auth: {},
-    basePath: '',
-    baseUrl: '',
-    batchPath: '',
-    blobSignatureTtl: 0,
-    crunchLimitLogBytesPerJob: 0,
-    crunchLogBytesPerEvent: 0,
-    crunchLogPartialLineThrottlePeriod: 0,
-    crunchLogSecondsBetweenEvents: 0,
-    crunchLogThrottleBytes: 0,
-    crunchLogThrottleLines: 0,
-    crunchLogThrottlePeriod: 0,
-    defaultCollectionReplication: 0,
-    defaultTrashLifetime: 0,
-    description: '',
-    discoveryVersion: '',
-    dockerImageFormats: [],
-    documentationLink: '',
-    generatedAt: '',
-    gitUrl: '',
-    id: '',
-    keepWebServiceUrl: '',
-    kind: '',
-    maxRequestSize: 0,
-    name: '',
-    packageVersion: '',
-    parameters: {},
-    protocol: '',
+    baseUrl: "",
+    keepWebServiceUrl: "",
     remoteHosts: {},
-    remoteHostsViaDNS: false,
-    resources: {},
-    revision: '',
-    rootUrl: '',
-    schemas: {},
-    servicePath: '',
-    sourceVersion: '',
-    source_version: '',
-    title: '',
-    uuidPrefix: '',
-    version: '',
-    websocketUrl: '',
-    workbenchUrl: '',
-    vocabularyUrl: '',
-    fileViewersConfigUrl: '',
-    ...config
+    rootUrl: "",
+    uuidPrefix: "",
+    websocketUrl: "",
+    workbenchUrl: "",
+    workbench2Url: "",
+    vocabularyUrl: "",
+    fileViewersConfigUrl: ""
 });
 
-interface ConfigJSON {
-    API_HOST: string;
-    VOCABULARY_URL: string;
-    FILE_VIEWERS_CONFIG_URL: string;
-}
-
-const getDefaultConfig = (): ConfigJSON => ({
-    API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "",
-    VOCABULARY_URL: "",
-    FILE_VIEWERS_CONFIG_URL: "",
-});
+const getDefaultConfig = (): WorkbenchConfig => {
+    let apiHost = "";
+    const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
+    if (envHost !== undefined) {
+        console.warn(`Using default API host ${envHost}.`);
+        apiHost = envHost;
+    }
+    else {
+        console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
+    }
+    return {
+        API_HOST: apiHost,
+        VOCABULARY_URL: undefined,
+        FILE_VIEWERS_CONFIG_URL: undefined,
+    };
+};
 
-export const DISCOVERY_URL = 'discovery/v1/apis/arvados/v1/rest';
-export const getDiscoveryURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${DISCOVERY_URL}?nocache=${(new Date()).getTime()}`;
+export const ARVADOS_API_PATH = "arvados/v1";
+export const CLUSTER_CONFIG_URL = "arvados/v1/config";
+export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_URL}?nocache=${(new Date()).getTime()}`;
\ No newline at end of file
index b889e9cf39d7d301a0c5e8e4b8e70859e7d7c3ff..ca2e23269aff5a82578db3731bfb36d846069848 100644 (file)
@@ -9,7 +9,7 @@ import { ServiceRepository } from "~/services/services";
 import Axios from "axios";
 import { getUserFullname, User } from "~/models/user";
 import { authActions } from "~/store/auth/auth-action";
-import { Config, DISCOVERY_URL } from "~/common/config";
+import { Config, ClusterConfigJSON, CLUSTER_CONFIG_URL, ARVADOS_API_PATH } from "~/common/config";
 import { Session, SessionStatus } from "~/models/session";
 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
 import { AuthService, UserDetailsResponse } from "~/services/auth-service/auth-service";
@@ -24,8 +24,8 @@ const getRemoteHostBaseUrl = async (remoteHost: string): Promise<string | null>
     let baseUrl: string | null = null;
 
     try {
-        const resp = await Axios.get<Config>(`${origin}/${DISCOVERY_URL}`);
-        baseUrl = resp.data.baseUrl;
+        const resp = await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_URL}`);
+        baseUrl = `${resp.data.Services.Controller.ExternalURL}/${ARVADOS_API_PATH}`;
     } catch (err) {
         try {
             const resp = await Axios.get<any>(`${origin}/status.json`);
index 1d1ad18cbd8c985218fc7aa433b04056d76d031d..b889adf571113494f32fee49843ebeabe527f5a9 100644 (file)
@@ -10,7 +10,7 @@ import { ServiceRepository } from "~/services/services";
 import { SshKeyResource } from '~/models/ssh-key';
 import { User, UserResource } from "~/models/user";
 import { Session } from "~/models/session";
-import { getDiscoveryURL, Config } from '~/common/config';
+import { getClusterConfigURL, Config, ClusterConfigJSON, mapRemoteHosts } from '~/common/config';
 import { initSessions } from "~/store/auth/auth-action-session";
 import { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
 import { matchTokenRoute, matchFedTokenRoute } from '~/routes/routes';
@@ -88,8 +88,14 @@ const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState,
         });
     }
     Object.keys(config.remoteHosts).map((k) => {
-        Axios.get<Config>(getDiscoveryURL(config.remoteHosts[k]))
-            .then(response => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: response.data })));
+        Axios.get<ClusterConfigJSON>(getClusterConfigURL(config.remoteHosts[k]))
+            .then(response => {
+                const remoteConfig = new Config();
+                remoteConfig.uuidPrefix = response.data.ClusterID;
+                remoteConfig.workbench2Url = response.data.Services.Workbench2.ExternalURL;
+                mapRemoteHosts(response.data, remoteConfig);
+                dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: remoteConfig}));
+            });
     });
 };