diff --git a/.travis.yml b/.travis.yml index 8d144165..0d62b72b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,15 @@ node_js: - "10" services: - - docker + - docker + +before_install: + - COMMIT=`git rev-parse HEAD` && echo "export const gitCommit = \"${COMMIT}\";" >src/server/version.js + - VERSION=`git describe --always --tags` && echo "export const gitVersion =\"${VERSION}\";" >>src/server/version.js install: - - npm install + - npm install script: - - npm run eslint - - npm run test \ No newline at end of file + - npm run eslint + - npm run test \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d4bc0789..214d00b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:16.04 +FROM quay.io/cdis/ubuntu:16.04 ENV DEBIAN_FRONTEND=noninteractive @@ -16,8 +16,8 @@ RUN apt-get update \ COPY . /guppy/ WORKDIR /guppy -RUN COMMIT=`git rev-parse HEAD` && echo "export const guppyCommit = \"${COMMIT}\";" >versions.js -RUN VERSION=`git describe --always --tags` && echo "export const guppyVersion =\"${VERSION}\";" >>versions.js +RUN COMMIT=`git rev-parse HEAD` && echo "export const gitCommit = \"${COMMIT}\";" >src/server/version.js +RUN VERSION=`git describe --always --tags` && echo "export const gitVersion =\"${VERSION}\";" >>src/server/version.js RUN /bin/rm -rf .git RUN /bin/rm -rf node_modules diff --git a/README.md b/README.md index 177dce62..1d630bb4 100644 --- a/README.md +++ b/README.md @@ -107,5 +107,15 @@ npm start > > (E.g., `"tier_access_sensitive_record_exclusion_field": "sensitive"` in the Guppy config tells Guppy to look for a field in the ES index called `sensitive`, and to exclude records in the ES index which have `sensitive: "true"`) -### Download Endpoint: -Guppy has another special endpoint `/download` for just fetching raw data from elasticsearch. please see [here](https://github.com/uc-cdis/guppy/blob/master/doc/download.md) for more details. +### Additional Guppy Endpoints: +Guppy has a special endpoint `/download` for just fetching raw data from elasticsearch. This endpoint can be used to overcome Elastic Search's 10k record limit. Please see [here](https://github.com/uc-cdis/guppy/blob/master/doc/download.md) for details. + +Guppy's `/_status` endpoint yields health check and array field information. This endpoint is publicly accessible and returns output of the form +``` +{"statusCode":200,"warnings":null,"indices":{"":{"aliases":{"alias-name":{}},"arrayFields":[""]}}} +``` + +The `/_version` endpoint yields version and commit information. This endpoint is publicly accessible and returns output of the form +``` +{"version":"","commit":""} +``` \ No newline at end of file diff --git a/src/server/endpoints.js b/src/server/endpoints.js new file mode 100644 index 00000000..994f01fa --- /dev/null +++ b/src/server/endpoints.js @@ -0,0 +1,20 @@ +import esInstance from './es/index'; +import { gitVersion, gitCommit } from './version'; + +export const statusRouter = async (req, res, next) => { + try { + const data = await esInstance.getAllESIndices(); + res.send(data); + } catch (err) { + next(err); + } + return 0; +}; + +export const versionRouter = async (req, res) => { + res.send({ + version: gitVersion, + commit: gitCommit, + }); + return 0; +}; diff --git a/src/server/es/index.js b/src/server/es/index.js index 62dff9fa..a10019d7 100644 --- a/src/server/es/index.js +++ b/src/server/es/index.js @@ -318,6 +318,42 @@ class ES { ); } + /** + * Get all es indices and their alias + */ + getAllESIndices() { + const indicesArray = this.config.indices.map((e) => e.index); + if (this.config.configIndex) { + indicesArray.push(this.config.configIndex); + } + return this.client.indices.getAlias({ + index: indicesArray, + }).then((resp) => { + try { + const indicesMetadata = resp.body; + const indicesWithArrayFields = Object.keys(this.arrayFields); + for (let i = 0; i < indicesWithArrayFields.length; i += 1) { + const indexName = indicesWithArrayFields[i]; + if (!indicesMetadata[indexName]) { + indicesMetadata[indexName] = {}; + } + indicesMetadata[indexName].arrayFields = this.arrayFields[indexName]; + } + return { + statusCode: resp.statusCode, + warnings: resp.warnings, + indices: { + ...indicesMetadata, + }, + }; + } catch (err) { + throw new Error(err); + } + }, (err) => { + throw new Error(err); + }); + } + /** * Check if the field is array */ diff --git a/src/server/server.js b/src/server/server.js index 996883c3..0a708548 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -14,6 +14,7 @@ import headerParser from './utils/headerParser'; import getAuthHelperInstance from './auth/authHelper'; import downloadRouter from './download'; import CodedError from './utils/error'; +import { statusRouter, versionRouter } from './endpoints'; const app = express(); app.use(cors()); @@ -47,10 +48,18 @@ const startServer = () => { }); // simple health check endpoint - app.get('/_status', (req, res) => { - res.send('hello guppy'); + // eslint-disable-next-line no-unused-vars + app.get('/_status', statusRouter, (req, res, err, next) => { + if (err instanceof CodedError) { + res.status(err.code).send(err.msg); + } else { + res.status(500).send(err); + } }); + // eslint-disable-next-line no-unused-vars + app.get('/_version', versionRouter); + // download endpoint for fetching data directly from es app.post('/download', downloadRouter, (err, req, res, next) => { // eslint-disable-line no-unused-vars diff --git a/src/server/version.js b/src/server/version.js new file mode 100644 index 00000000..1505bcb6 --- /dev/null +++ b/src/server/version.js @@ -0,0 +1,2 @@ +export const gitCommit = 'N/A'; +export const gitVersion = 'N/A';