Skip to content

Commit

Permalink
Add PG vars to generate-vectortiles and import-sql (#119)
Browse files Browse the repository at this point in the history
* Use standard PG* env vars in addition to the legacy POSTGRES_* ones
  • Loading branch information
nyurik committed Nov 25, 2019
1 parent a9f04b8 commit 6d9ed08
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
16 changes: 11 additions & 5 deletions bin/import-sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ set -o pipefail
set -o nounset
shopt -s nullglob

# For backward compatibility, allow both PG* and POSTGRES_* forms,
# with the non-standard POSTGRES_* form taking precedence.
# An error will be raised if neither form is given, except for the PGPORT
export PGHOST="${POSTGRES_HOST:-${PGHOST?}}"
export PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}"
export PGUSER="${POSTGRES_USER:-${PGUSER?}}"
export PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}"
export PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}"


function exec_psql_file() {
local file_name="$1"
# Allws additional parameters to be passed to psql
Expand All @@ -14,12 +24,8 @@ function exec_psql_file() {
echo "Importing $file_name (md5 $(md5sum < "$file_name") $(wc -l < "$file_name") lines) into Postgres..."

# shellcheck disable=SC2154
PGPASSWORD="$POSTGRES_PASSWORD" psql \
psql \
-v ON_ERROR_STOP="1" \
--host="$POSTGRES_HOST" \
${POSTGRES_PORT:+ --port="$POSTGRES_PORT"} \
--dbname="$POSTGRES_DB" \
--username="$POSTGRES_USER" \
-c '\timing on' \
-f "$file_name" \
"${psql_opts[@]}"
Expand Down
15 changes: 10 additions & 5 deletions docker/generate-vectortiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ You need to provide the database credentials and run `generate-vectortiles`.
docker run --rm \
-v $(pwd)/project.tm2source:/tm2source \
-v $(pwd):/export \
-e POSTGRES_USER="osm" \
-e POSTGRES_PASSWORD="osm" \
-e POSTGRES_HOST="127.0.0.1" \
-e POSTGRES_DB="osm" \
-e POSTGRES_PORT="5432" \
-e PGHOST="127.0.0.1" \
-e PGDATABASE="openmaptiles" \
-e PGUSER="openmaptiles" \
-e PGPASSWORD="openmaptiles" \
openmaptiles/generate-vectortiles
```

Optional environment variables:
* `PGPORT` (defaults to `5432`)

Legacy env variables are still supported, but they are not recommended:
`POSTGRES_HOST`,`POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_PORT`
19 changes: 14 additions & 5 deletions docker/generate-vectortiles/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set -o pipefail
readonly DEST_PROJECT_DIR="/tmp/project"
readonly DEST_PROJECT_FILE="${DEST_PROJECT_DIR%%/}/data.yml"

# For backward compatibility, allow both PG* and POSTGRES_* forms,
# with the non-standard POSTGRES_* form taking precedence.
# An error will be raised if neither form is given, except for the PGPORT
PGHOST="${POSTGRES_HOST:-${PGHOST?}}"
PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}"
PGUSER="${POSTGRES_USER:-${PGUSER?}}"
PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}"
PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}"

# project config will be copied to new folder because we
# modify the source configuration
function copy_source_project() {
Expand All @@ -19,11 +28,11 @@ function cleanup_dest_project() {

# replace database connection with postgis container connection
function replace_db_connection() {
local replace_expr_1="s|host: .*|host: \"$POSTGRES_HOST\"|g"
local replace_expr_2="s|port: .*|port: \"$POSTGRES_PORT\"|g"
local replace_expr_3="s|dbname: .*|dbname: \"$POSTGRES_DB\"|g"
local replace_expr_4="s|user: .*|user: \"$POSTGRES_USER\"|g"
local replace_expr_5="s|password: .*|password: \"$POSTGRES_PASSWORD\"|g"
local replace_expr_1="s|host: .*|host: \"$PGHOST\"|g"
local replace_expr_2="s|port: .*|port: \"$PGPORT\"|g"
local replace_expr_3="s|dbname: .*|dbname: \"$PGDATABASE\"|g"
local replace_expr_4="s|user: .*|user: \"$PGUSER\"|g"
local replace_expr_5="s|password: .*|password: \"$PGPASSWORD\"|g"

sed -i "$replace_expr_1" "$DEST_PROJECT_FILE"
sed -i "$replace_expr_2" "$DEST_PROJECT_FILE"
Expand Down
2 changes: 1 addition & 1 deletion docker/import-sql/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# !! OBSOLETE !!

This image is obsolete, and should not be used. Use `openmaptiles-tools` image instead. `import_sql.sh` was copied to `/bin`.
This image is obsolete, and should not be used. Use `openmaptiles-tools` image instead. `import_sql.sh` was copied to `/bin` and should be used as `import-sql` command.

## Import SQL files into PostgreSQL [![Docker Automated buil](https://img.shields.io/docker/automated/openmaptiles/import-sql.svg)](https://hub.docker.com/r/openmaptiles/import-sql/)

Expand Down

0 comments on commit 6d9ed08

Please sign in to comment.