Feature: Add external database volume and external database connection#155
Feature: Add external database volume and external database connection#155nshandra wants to merge 4 commits intodevelopmentfrom
Conversation
|
@nshandra Sorry, I had this pending. Can you merge with development? (re-add me when done) |
|
@tokland done. |
| @@ -300,6 +318,9 @@ def run_docker_compose( | |||
| ("ROOT_PATH", ROOT_PATH), | |||
| ("PSQL_ENABLE_QUERY_LOGS", "") if not enable_postgres_queries_logging else None, | |||
| ("GLOWROOT_PORT", get_port_glowroot(glowroot_port)) | |||
There was a problem hiding this comment.
Missing trailing comma, the code does not run as-is.
| "--external-db-volume", | ||
| metavar="DIRECTORY", | ||
| help="Directory for external database volume", | ||
| ) |
There was a problem hiding this comment.
This option worked for me. Only problem is that the final permissions of the folder are off:
$ ls -al | grep volume
drwx------ 19 70 70 4.0K Apr 14 10:21 volumeHowever, that's something d2-docker has been ignoring all along - we probably should have user: "${UID}:${GID}" or similar in our docker-compose to avoid this kind of problems. Not sure it's the moment to try that (it may have unintended consequences), but I'd be a step in the good direction.
| type=str, | ||
| metavar="postgresql://user:pass@host:port/dbname", | ||
| help="Use external PostgreSQL database" | ||
| ) |
| "--load-dump-from-data", | ||
| action="store_true", | ||
| help="Load database dump from data container (only with --external-db-url)", | ||
| ) |
There was a problem hiding this comment.
It worked for me, only that I had to explicitly create the database with psql for it to work. Is that the intended workflow?
| else | ||
| echo "Normal mode: $path" | ||
| $psql_cmd < "$path" | ||
| $psql_cmd <"$path" |
There was a problem hiding this comment.
Formatting is being applied to code outside of the PR. It would be fine if the formatting were incorrect, but we don't have a defined prettifier, so there is no "incorrect" for now. Would the best approach be a .vscode/settings.json file where we set up the formatter and its options?
| ("GLOWROOT_PORT", get_port_glowroot(glowroot_port)) | ||
| ("EXTERNAL_DB_VOLUME", external_db_volume) if external_db_volume else None, | ||
| ("EXTERNAL_DB_URL", external_db_url) if external_db_url else None, | ||
| ("LOAD_DUMP_FROM_DATA", "yes" if load_dump_from_data else "no") if external_db_url else "no", |
There was a problem hiding this comment.
The grouping/parentheses look off, right?
| logger.info("Validating external database connection...") | ||
| try: | ||
| psql_cmd = ["psql", "-d", db_url, "-c", "SELECT now();", "&> /dev/null"] | ||
| run(psql_cmd, capture_output=False) |
There was a problem hiding this comment.
it seems it worked, but I am curious about "&> /dev/null", since the default for run is shell=False.
|
|
||
| if args.external_db_url: | ||
| utils.validate_external_db_connection(args.external_db_url) | ||
|
|
There was a problem hiding this comment.
Should we add a check to raise error if both options are passed, as they are mutually exclusive?
Also, a standalone --load-dump-from-data is not correct workflow.
| psql_cmd="$psql_base_cmd -v ON_ERROR_STOP=0" | ||
| psql_strict_cmd="$psql_base_cmd -v ON_ERROR_STOP=1" | ||
| pgrestore_cmd="pg_restore -h db -U dhis -d dhis2" | ||
| pgrestore_cmd="pg_restore ${db_url:-"-h db -U dhis dhis2"}" |
There was a problem hiding this comment.
Before we had -d dhis2 but now it's directly dhis2. Is that correct?
| if parsed.hostname == "localhost": | ||
| hostname = "host.docker.internal" | ||
| else: | ||
| hostname = parsed.hostname |
There was a problem hiding this comment.
[minor] we can use hostname = ..
📌 References
📝 Implementation
Add parameters to d2-docker start to allow either:
Testing
External Persistent Volume Case
This case will store the DB volume at the specified absolute path.
External DB Case
This case needs a psql DB runnig in the machine hosting the dockers or in a accessible network.
This DB has to have the correct settings:
/etc/postgresql/{ver}/main/postgresql.confSet
listen_addressesto:'*'(easiest for testing)'localhost,172.17.0.1'(docker and DB in same system)'localhost,<reachable_ip>'/etc/postgresql/{ver}/main/pg_hba.confAdd an entry to allow the connection. Example:
Running d2-cloner:
#1ck80y8