-
Notifications
You must be signed in to change notification settings - Fork 62
Dataloader move to universal from gpii-ops/gpii-dataloader #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6cb062
GPII-3138: Import deleteAndLoadSnapsets.sh and DataLoader.md
stepanstipl 59a4eb4
GPII-3138: Add dataLoader dependencies to docker image (jq and curl)
stepanstipl 9370d18
GPII-3138: Modify convertPrefs.js to expect directories without the f…
stepanstipl 0808a24
GPII-3138: Update data loader script to reflect new location in unive…
stepanstipl 2c6c50d
GPII-3138: Update directory checks to match containerised environment
stepanstipl 776b880
GPII-3138: Minor formatting changes
stepanstipl d588bf2
GPII-3138: Add DB connectivity check to dataloader
stepanstipl 6e6da16
GPII-3138: Update vagrantCloudBasedContainers.sh to wor with omported…
stepanstipl e55d8c1
GPII-3138: Update DataLoader docs to reflect current state
stepanstipl ab32b8b
GPII-3138: Fix linting errors on DataLoader.md
stepanstipl bbfabf2
GPII-3138: Revert unintended change in DataLoader readme
stepanstipl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # CouchDB Data Loader | ||
|
|
||
| (`scripts/deleteAndLoadSnapsets.sh`) | ||
|
|
||
| This script is used to setup CouchDB database and is executed as a Kubernetes batch Job every time new version of the | ||
| universal image is deployed to the cluster (also when cluster is initially created). | ||
|
|
||
| It does following: | ||
|
|
||
| - Converts the preferences in universal into `snapset` Prefs Safes and GPII Keys, | ||
| - Optionally deletes existing database, | ||
| - Creates a CouchDB database if none exits, | ||
| - Updates the database with respect to its `design/views` document, as required, | ||
| - Loads the latest snapsets created into the database. | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| - `COUCHDB_URL`: URL of the CouchDB database. (required) | ||
| - `CLEAR_INDEX`: If set to `true`, the database at $COUCHDB_URL will be deleted and recreated. (optional) | ||
| - `STATIC_DATA_DIR`: The directory where the static data to be loaded into CouchDB resides. (optional) | ||
| - `BUILD_DATA_DIR`: The directory where the data built from the conversion step resides. (optional) | ||
|
|
||
| The use of environment variables for data directories is useful if you want to mount the database data using a Docker | ||
| volume and point the data loader at it. | ||
|
|
||
| Note that since [the docker doesn't support the environment variable type of | ||
| array](https://github.com/moby/moby/issues/20169), two separate environment variables are used for inputting data | ||
| directories instead of one array that holds these directories. | ||
|
|
||
| ## Running | ||
|
|
||
| Example using containers: | ||
|
|
||
| ```bash | ||
| $ docker run -d -p 5984:5984 --name couchdb couchdb | ||
| $ docker run --rm --link couchdb -e COUCHDB_URL=http://couchdb:5984/gpii \ | ||
| -e CLEAR_INDEX=true vagrant-universal scripts/deleteAndLoadSnapsets.sh | ||
| $ docker run -d -p 8081:8081 --name preferences --link couchdb \ | ||
| -e NODE_ENV=gpii.config.preferencesServer.standalone.production \ | ||
| -e PREFERENCESSERVER_LISTEN_PORT=8081 -e DATASOURCE_HOSTNAME=http://couchdb \ | ||
| -e DATASOURCE_PORT=5984 vagrant-universal | ||
| ``` | ||
|
|
||
| Below are two versions of loading couchdb data from a different location (e.g. | ||
| /home/vagrant/sync/universal/testData/dbData for static data directory and /home/vagrant/sync/universal/build/dbData for | ||
| build data directory). The first version has the optional `CLEAR_INDEX` set to true to erase and reset the database | ||
| prior to other database changes: | ||
|
|
||
| ```bash | ||
| $ docker run --name dataloader --link couchdb \ | ||
| -v /home/vagrant/sync/universal/testData/dbData:/static_data -e STATIC_DATA_DIR=/static_data \ | ||
| -v /home/vagrant/sync/universal/build/dbData:/build_data -e BUILD_DATA_DIR=/build_data \ | ||
| -e COUCHDB_URL=http://couchdb:5984/gpii \ | ||
| -e CLEAR_INDEX=true vagrant-universal scripts/deleteAndLoadSnapsets.sh | ||
| ``` | ||
|
|
||
| The second version does not set `CLEAR_INDEX` such that any existing database is left intact prior to subsequent changes | ||
| to it (e.g., deleting the snapsets): | ||
|
|
||
| ```bash | ||
| $ docker run --name dataloader --link couchdb \ | ||
| -v /home/vagrant/sync/universal/testData/dbData:/static_data -e STATIC_DATA_DIR=/static_data \ | ||
| -v /home/vagrant/sync/universal/build/dbData:/build_data -e BUILD_DATA_DIR=/build_data \ | ||
| -e COUCHDB_URL=http://couchdb:5984/gpii \ | ||
| vagrant-universal scripts/deleteAndLoadSnapsets.sh | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/bin/sh | ||
| APP_DIR=${APP_DIR:-"/app"} | ||
|
|
||
| STATIC_DATA_DIR=${STATIC_DATA_DIR:-"${APP_DIR}/testData/dbData"} | ||
| PREFERENCES_DATA_DIR=${PREFERENCES_DATA_DIR:-"${APP_DIR}/testData/preferences"} | ||
| BUILD_DATA_DIR=${BUILD_DATA_DIR:-'/tmp/build/dbData'} | ||
|
|
||
| DATALOADER_JS="${APP_DIR}/scripts/deleteAndLoadSnapsets.js" | ||
| CONVERT_JS="${APP_DIR}/scripts/convertPrefs.js" | ||
|
|
||
| log() { | ||
| echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | ||
| } | ||
|
|
||
| warm_indices(){ | ||
| log "Warming indices..." | ||
|
|
||
| for view in $(curl -s "${COUCHDB_URL}/_design/views/" | jq -r '.views | keys[]'); do | ||
| curl -fsS "${COUCHDB_URL}/_design/views/_view/${view}" >/dev/null | ||
| done | ||
|
|
||
| log "Finished warming indices..." | ||
| } | ||
|
|
||
| # Verify variables | ||
| if [ -z "${COUCHDB_URL}" ]; then | ||
| echo "COUCHDB_URL environment variable must be defined" | ||
| exit 1 | ||
| fi | ||
|
|
||
| COUCHDB_URL_SANITIZED=$(echo "${COUCHDB_URL}" | sed -e 's,\(://\)[^/]*\(@\),\1<SENSITIVE>\2,g') | ||
|
|
||
| log 'Starting' | ||
| log "CouchDB: ${COUCHDB_URL_SANITIZED}" | ||
| log "Clear index: ${CLEAR_INDEX}" | ||
| log "Static: ${STATIC_DATA_DIR}" | ||
| log "Build: ${BUILD_DATA_DIR}" | ||
| log "Working directory: $(pwd)" | ||
|
|
||
| # Check we can connect to CouchDB | ||
| COUCHDB_URL_ROOT=$(echo "${COUCHDB_URL}" | sed 's/[^\/]*$//g') | ||
| RET_CODE=$(curl --write-out '%{http_code}' --silent --output /dev/null "${COUCHDB_URL_ROOT}/_up") | ||
| if [ "$RET_CODE" != '200' ]; then | ||
| log "[ERROR] Failed to connect to CouchDB: ${COUCHDB_URL_SANITIZED}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Create build dir if it does not exist | ||
| if [ ! -d "${BUILD_DATA_DIR}" ]; then | ||
| mkdir -p "${BUILD_DATA_DIR}" | ||
| fi | ||
|
|
||
| # Convert preferences json5 to GPII keys and preferences safes | ||
| if [ -d "${PREFERENCES_DATA_DIR}" ]; then | ||
| node "${CONVERT_JS}" "${PREFERENCES_DATA_DIR}" "${BUILD_DATA_DIR}" snapset | ||
| if [ "$?" != '0' ]; then | ||
| log "[ERROR] ${CONVERT_JS} failed (exit code: $?)" | ||
| exit 1 | ||
| fi | ||
| else | ||
| log "PREFERENCES_DATA_DIR ($PREFERENCES_DATA_DIR) does not exist, nothing to convert" | ||
| fi | ||
|
|
||
| # Initialize (possibly clear) data base | ||
| if [ "${CLEAR_INDEX}" == 'true' ]; then | ||
| log "Deleting database at ${COUCHDB_URL_SANITIZED}" | ||
| if ! curl -fsS -X DELETE "${COUCHDB_URL}"; then | ||
| log "Error deleting database" | ||
| fi | ||
| fi | ||
|
|
||
| log "Creating database at ${COUCHDB_URL_SANITIZED}" | ||
| if ! curl -fsS -X PUT "${COUCHDB_URL}"; then | ||
| log "Database already exists at ${COUCHDB_URL_SANITIZED}" | ||
| fi | ||
|
|
||
| # Submit data | ||
| node "${DATALOADER_JS}" "${COUCHDB_URL}" "${STATIC_DATA_DIR}" "${BUILD_DATA_DIR}" | ||
| err=$? | ||
| if [ "${err}" != '0' ]; then | ||
| log "${DATALOADER_JS} failed with ${err}, exiting" | ||
| exit "${err}" | ||
| fi | ||
|
|
||
| # Warm Data | ||
| warm_indices |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old text was correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this, not sure how it got in and I didn't notice, reverted back to original text.