-
Notifications
You must be signed in to change notification settings - Fork 62
GPII-3138: Move functionality of gpii-dataloader repo into universal #692
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d5562d5
GPII-3138: Moved parts of gpii-dataloder code into universal
klown 9e5346c
GPII-3138: Import deleteAndLoadSnapsets.sh and DataLoader.md
stepanstipl 5c28281
GPII-3138: Add dataLoader dependencies to docker image (jq and curl)
stepanstipl 5f3921a
GPII-3138: Modify convertPrefs.js to expect directories without the f…
stepanstipl 044aa10
GPII-3138: Update data loader script to reflect new location in unive…
stepanstipl d59bf09
GPII-3138: Update directory checks to match containerised environment
stepanstipl 54d75f7
GPII-3138: Minor formatting changes
stepanstipl 68a961b
GPII-3138: Add DB connectivity check to dataloader
stepanstipl fb3d435
GPII-3138: Update vagrantCloudBasedContainers.sh to wor with omported…
stepanstipl 26995c2
GPII-3138: Update DataLoader docs to reflect current state
stepanstipl d44b584
GPII-3138: Fix linting errors on DataLoader.md
stepanstipl 07ace4c
GPII-3138: Revert unintended change in DataLoader readme
stepanstipl eb92c4e
GPII-3138: Improved check for proper usage of convertPrefs.js
klown 2d8abe9
Merge pull request #3 from stepanstipl/dataloader-import-klown
klown f28c79a
GPII-3138: Fixed merge issues.
klown 9c42fbe
GPII-3138: Fix typo in DataLoader.md
stepanstipl ee8417b
GPII-3138: Add GPII prefix to dataloader env variables
stepanstipl cbe889b
Merge pull request #4 from stepanstipl/dataloader-import2
klown 28fc635
GPII-3138: Added to description of dataloader script.
klown 6794a0f
GPII-3138: Added "GPII_" prefix to environment variables
klown af1660b
GPII-3138: Add GPII prefix to dataloader env variables
stepanstipl a8598eb
GPII-3138: Add GPII prefix to dataloader APP_DIR variable
stepanstipl 9c2167b
Merge pull request #5 from stepanstipl/dataloader-import3
klown 7eebe6e
GPII-3138: Merged upstream master GPII branch.
klown 7362413
GPII-3138: Improved dataloader README
klown dd3ea15
GPII-3138: Add uniqie ID to pouchManager temp test dir
stepanstipl 8847cd3
Merge pull request #6 from stepanstipl/dataloader-import3
klown 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,67 @@ | ||
| # 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 exists, | ||
| - Updates the database with respect to its `design/views` document, as required, | ||
| - Deletes the snapsets and keys, if any, from the database, | ||
| - Loads the latest snapsets and keys created created above into the database. | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| - `GPII_COUCHDB_URL`: URL of the CouchDB database. (required) | ||
| - `GPII_CLEAR_INDEX`: If set to `true`, the database at $GPII_COUCHDB_URL will be deleted and recreated. (optional) | ||
| - `GPII_STATIC_DATA_DIR`: The directory where the static data to be loaded into CouchDB resides. (optional) | ||
| - `GPII_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 GPII_COUCHDB_URL=http://couchdb:5984/gpii \ | ||
| -e GPII_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 `GPII_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 GPII_STATIC_DATA_DIR=/static_data \ | ||
| -v /home/vagrant/sync/universal/build/dbData:/build_data -e GPII_BUILD_DATA_DIR=/build_data \ | ||
| -e GPII_COUCHDB_URL=http://couchdb:5984/gpii \ | ||
| -e GPII_CLEAR_INDEX=true vagrant-universal scripts/deleteAndLoadSnapsets.sh | ||
| ``` | ||
|
|
||
| The second version does not set `GPII_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 GPII_STATIC_DATA_DIR=/static_data \ | ||
| -v /home/vagrant/sync/universal/build/dbData:/build_data -e GPII_BUILD_DATA_DIR=/build_data \ | ||
| -e GPII_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 | ||
| GPII_APP_DIR=${GPII_APP_DIR:-"/app"} | ||
|
|
||
| GPII_STATIC_DATA_DIR=${GPII_STATIC_DATA_DIR:-"${GPII_APP_DIR}/testData/dbData"} | ||
| GPII_PREFERENCES_DATA_DIR=${GPII_PREFERENCES_DATA_DIR:-"${GPII_APP_DIR}/testData/preferences"} | ||
| GPII_BUILD_DATA_DIR=${GPII_BUILD_DATA_DIR:-'/tmp/build/dbData'} | ||
|
|
||
| DATALOADER_JS="${GPII_APP_DIR}/scripts/deleteAndLoadSnapsets.js" | ||
| CONVERT_JS="${GPII_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 "${GPII_COUCHDB_URL}/_design/views/" | jq -r '.views | keys[]'); do | ||
| curl -fsS "${GPII_COUCHDB_URL}/_design/views/_view/${view}" >/dev/null | ||
| done | ||
|
|
||
| log "Finished warming indices..." | ||
| } | ||
|
|
||
| # Verify variables | ||
| if [ -z "${GPII_COUCHDB_URL}" ]; then | ||
| echo "GPII_COUCHDB_URL environment variable must be defined" | ||
| exit 1 | ||
| fi | ||
|
|
||
| GPII_COUCHDB_URL_SANITIZED=$(echo "${GPII_COUCHDB_URL}" | sed -e 's,\(://\)[^/]*\(@\),\1<SENSITIVE>\2,g') | ||
|
|
||
| log 'Starting' | ||
| log "CouchDB: ${GPII_COUCHDB_URL_SANITIZED}" | ||
| log "Clear index: ${GPII_CLEAR_INDEX}" | ||
| log "Static: ${GPII_STATIC_DATA_DIR}" | ||
| log "Build: ${GPII_BUILD_DATA_DIR}" | ||
| log "Working directory: $(pwd)" | ||
|
|
||
| # Check we can connect to CouchDB | ||
| GPII_COUCHDB_URL_ROOT=$(echo "${GPII_COUCHDB_URL}" | sed 's/[^\/]*$//g') | ||
| RET_CODE=$(curl --write-out '%{http_code}' --silent --output /dev/null "${GPII_COUCHDB_URL_ROOT}/_up") | ||
| if [ "$RET_CODE" != '200' ]; then | ||
| log "[ERROR] Failed to connect to CouchDB: ${GPII_COUCHDB_URL_SANITIZED}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Create build dir if it does not exist | ||
| if [ ! -d "${GPII_BUILD_DATA_DIR}" ]; then | ||
| mkdir -p "${GPII_BUILD_DATA_DIR}" | ||
| fi | ||
|
|
||
| # Convert preferences json5 to GPII keys and preferences safes | ||
| if [ -d "${GPII_PREFERENCES_DATA_DIR}" ]; then | ||
| node "${CONVERT_JS}" "${GPII_PREFERENCES_DATA_DIR}" "${GPII_BUILD_DATA_DIR}" snapset | ||
| if [ "$?" != '0' ]; then | ||
| log "[ERROR] ${CONVERT_JS} failed (exit code: $?)" | ||
| exit 1 | ||
| fi | ||
| else | ||
| log "GPII_PREFERENCES_DATA_DIR ($GPII_PREFERENCES_DATA_DIR) does not exist, nothing to convert" | ||
| fi | ||
|
|
||
| # Initialize (possibly clear) data base | ||
| if [ "${GPII_CLEAR_INDEX}" == 'true' ]; then | ||
| log "Deleting database at ${GPII_COUCHDB_URL_SANITIZED}" | ||
| if ! curl -fsS -X DELETE "${GPII_COUCHDB_URL}"; then | ||
| log "Error deleting database" | ||
| fi | ||
| fi | ||
|
|
||
| log "Creating database at ${GPII_COUCHDB_URL_SANITIZED}" | ||
| if ! curl -fsS -X PUT "${GPII_COUCHDB_URL}"; then | ||
| log "Database already exists at ${GPII_COUCHDB_URL_SANITIZED}" | ||
| fi | ||
|
|
||
| # Submit data | ||
| node "${DATALOADER_JS}" "${GPII_COUCHDB_URL}" "${GPII_STATIC_DATA_DIR}" "${GPII_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.
Puzzled that the option here is to delete the entire database rather than merely all of the documents of type "snapset"
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.
There is a production test (
test:vagrantProduction) that executesvagrantCloudBasedContainers.shwithout its--no-rebuildflag. When first run locally, or when run by CI, there is no database to delete, so deleting the database is irrelevant at that point.But, if a developer runs the test a second time, they can at their option start from scratch with an empty database, or use the
--no-rebuildflag and modify the database in situ, which is closer to a production environment.The question is whether the first option -- start from scratch -- is useful.
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.
OK, I think this is just a documentation issue, since if I understand correctly, the action of steps 5 and 6 is actually the one of https://github.com/GPII/universal/blob/master/scripts/deleteAndLoadSnapsets.js - so we should just clarify the wording in the readme here to explain that steps 5 and 6 don't simply drop "snapsets and keys" but in particular only those keys which are associated with snapsets. I think it would be helpful for the comment here to explicitly link to or reproduce the comment at the head of the script https://github.com/GPII/universal/blob/master/scripts/deleteAndLoadSnapsets.js#L11 so that, for example, anyone invoking this script will do so in confidence that it will not delete user data (unless they enable GPII_CLEAR_INDEX, which should be supplied with a clear warning)
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.
OK, I've added to the README -- take a look.