Skip to content

Commit ec2ae79

Browse files
committed
Begin v2 upgrade
1 parent 8b06a90 commit ec2ae79

File tree

11 files changed

+584
-145
lines changed

11 files changed

+584
-145
lines changed

DEVELOPMENT.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Development guide
2+
3+
* Any additional echos must be to stderr so the output can be piped directly into another command
4+
5+
```
6+
# Start a postgres server
7+
docker run -d --name myapp-db -e POSTGRES_PASSWORD=password -e POSTGRES_USER=myapp -e POSTGRES_DB=myapp postgres:9.6
8+
9+
# Connect with psql
10+
docker run --rm -it panubo/postgres-toolbox:latest psql -h 172.18.0.2 -W password -U myapp -d myapp
11+
```

Dockerfile

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,73 @@
1-
FROM alpine:latest
1+
FROM alpine:3.10
22

3-
MAINTAINER Andrew Cutler <[email protected]>
3+
# Install some tools
4+
# python is required for gsutil
5+
RUN set -x \
6+
&& apk add --update bash findutils postgresql-client gzip bzip2 lz4 xz unzip zip coreutils python3 rsync \
7+
&& rm -rf /var/cache/apk/* \
8+
&& ln -s /usr/bin/python3 /usr/bin/python \
9+
;
410

5-
RUN apk add --update bash findutils gzip postgresql-client && \
6-
rm -rf /var/cache/apk/*
11+
# Install Panubo bash-container
12+
RUN set -x \
13+
&& BASHCONTAINER_VERSION=0.6.0 \
14+
&& if [ -n "$(readlink /usr/bin/wget)" ]; then \
15+
fetchDeps="${fetchDeps} wget"; \
16+
fi \
17+
&& if ! command -v gpg > /dev/null; then \
18+
fetchDeps="${fetchDeps} gnupg"; \
19+
fi \
20+
&& apk add --no-cache ca-certificates bash curl coreutils ${fetchDeps} \
21+
&& cd /tmp \
22+
&& wget -nv https://github.com/panubo/bash-container/releases/download/v${BASHCONTAINER_VERSION}/panubo-functions.tar.gz \
23+
&& wget -nv https://github.com/panubo/bash-container/releases/download/v${BASHCONTAINER_VERSION}/panubo-functions.tar.gz.asc \
24+
&& GPG_KEYS="E51A4070A3FFBD68C65DDB9D8BECEF8DFFCC60DD" \
25+
&& ( gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$GPG_KEYS" \
26+
|| gpg --keyserver hkp://ipv4.pool.sks-keyservers.net --recv-keys "$GPG_KEYS" \
27+
|| gpg --keyserver hkp://pgp.mit.edu:80 --recv-keys "$GPG_KEYS" ) \
28+
&& gpg --batch --verify panubo-functions.tar.gz.asc panubo-functions.tar.gz \
29+
&& tar -C / -zxf panubo-functions.tar.gz \
30+
&& rm -rf /tmp/* \
31+
&& apk del ${fetchDeps} \
32+
;
733

8-
COPY commands /commands
34+
ENV \
35+
GSUTIL_VERSION=4.46 \
36+
GSUTIL_CHECKSUM=bb319cc49d74cf12b692748d498abc63e6098750fb6e36cff71eebf71ec895e0 \
37+
CLOUDSDK_GSUTIL_PYTHON=python3 \
38+
CLOUDSDK_PYTHON=python3
939

10-
ENTRYPOINT ["/commands/entry.sh"]
40+
# Install gsutil
41+
RUN set -x \
42+
&& mkdir -p /opt \
43+
&& curl -o /tmp/gsutil_${GSUTIL_VERSION}.tar.gz "https://storage.googleapis.com/pub/gsutil_${GSUTIL_VERSION}.tar.gz" \
44+
&& echo "${GSUTIL_CHECKSUM} gsutil_${GSUTIL_VERSION}.tar.gz" > /tmp/SHA256SUM \
45+
&& ( cd /tmp; sha256sum -c SHA256SUM; ) \
46+
&& tar -C /opt -zxf /tmp/gsutil_${GSUTIL_VERSION}.tar.gz \
47+
&& ln -s /opt/gsutil/gsutil /usr/local/bin/gsutil \
48+
&& rm -f /tmp/* \
49+
&& find /opt ! -group 0 -exec chgrp -h 0 {} \; \
50+
;
1151

12-
CMD ["default"]
52+
ENV \
53+
PYTHONIOENCODING=UTF-8 \
54+
PYTHONUNBUFFERED=0 \
55+
PAGER=more \
56+
AWS_CLI_VERSION=1.16.286 \
57+
AWS_CLI_CHECKSUM=7e99ea733b3d97b1fa178fab08b5d7802d0647ad514c14221513c03ce920ce83
58+
59+
RUN set -x \
60+
&& apk add --no-cache ca-certificates wget \
61+
&& cd /tmp \
62+
&& wget -nv https://s3.amazonaws.com/aws-cli/awscli-bundle-${AWS_CLI_VERSION}.zip -O /tmp/awscli-bundle-${AWS_CLI_VERSION}.zip \
63+
&& echo "${AWS_CLI_CHECKSUM} awscli-bundle-${AWS_CLI_VERSION}.zip" > /tmp/SHA256SUM \
64+
&& sha256sum -c SHA256SUM \
65+
&& unzip awscli-bundle-${AWS_CLI_VERSION}.zip \
66+
&& /tmp/awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws \
67+
&& apk del wget \
68+
&& rm -rf /tmp/* \
69+
;
70+
71+
COPY commands /usr/local/bin/
72+
73+
CMD ["usage"]

commands/common.sh

Lines changed: 185 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,191 @@
1-
#!/usr/bin/env bash
2-
3-
HOST=${DATABASE_HOST-${POSTGRES_PORT_5432_TCP_ADDR-localhost}}
4-
PORT=${DATABASE_PORT-${POSTGRES_PORT_5432_TCP_PORT-5432}}
5-
USER=${DATABASE_USER-${POSTGRES_ENV_POSTGRES_USER-postgres}}
6-
DATABASE=${DATABASE_NAME-${POSTGRES_ENV_POSTGRES_DATABASE-postgres}}
7-
PASS="${DATABASE_PASS-${POSTGRES_ENV_POSTGRES_PASSWORD}}"
8-
export PGPASSWORD="${PASS}"
9-
PGCONN="--username=${USER} --host=${HOST} --port=${PORT}"
10-
PSQL="psql ${PGCONN} --dbname=${DATABASE}"
11-
PGDUMP="pg_dump ${PGCONN}"
12-
GZIP="gzip --fast"
13-
14-
15-
function wait_postgres {
16-
# Wait for PostgreSQL to be available
17-
TIMEOUT=${3:-30}
18-
echo -n "Waiting to connect to PostgreSQL at ${1-$HOST}:${2-$PORT}"
19-
for (( i=0;; i++ )); do
20-
if [ ${i} -eq ${TIMEOUT} ]; then
21-
echo " timeout!"
22-
exit 99
1+
# shellcheck disable=SC2034 shell=bash
2+
3+
# The docker.io/postgres image uses the following env vars
4+
# POSTGRES_PASSWORD
5+
# POSTGRES_USER
6+
# POSTGRES_DB
7+
8+
# Postgres also supports environment variables https://www.postgresql.org/docs/current/libpq-envars.html
9+
# PGPASSWORD
10+
# PGUSER
11+
# PGDATABASE
12+
# PGHOST
13+
# PGPORT
14+
# PGPASSFILE
15+
16+
# Original env vars
17+
# HOST=${DATABASE_HOST-${POSTGRES_PORT_5432_TCP_ADDR-localhost}}
18+
# PORT=${DATABASE_PORT-${POSTGRES_PORT_5432_TCP_PORT-5432}}
19+
# USER=${DATABASE_USER-${POSTGRES_ENV_POSTGRES_USER-postgres}}
20+
# DATABASE=${DATABASE_NAME-${POSTGRES_ENV_POSTGRES_DATABASE-postgres}}
21+
# PASS="${DATABASE_PASS-${POSTGRES_ENV_POSTGRES_PASSWORD}}"
22+
# export PGPASSWORD="${PASS}"
23+
# PGCONN="--username=${USER} --host=${HOST} --port=${PORT}"
24+
# PSQL="psql ${PGCONN} --dbname=${DATABASE}"
25+
# PGDUMP="pg_dump ${PGCONN}"
26+
# GZIP="gzip --fast"
27+
28+
# shellcheck disable=SC1091
29+
. /panubo-functions.sh
30+
31+
global_options() {
32+
echoerr "Global Options: (where possible these options match psql options)"
33+
echoerr " -h|--host host to connect to"
34+
echoerr " -p|--port post to connect to"
35+
echoerr " -U|--username user to connect with"
36+
echoerr " -d|--dbname database to connect to"
37+
echoerr " -W|--password password to connection to (not recommended. Use password-file)"
38+
echoerr " --password-file password file to read password from"
39+
}
40+
41+
parse_options() {
42+
# Function parses the following options from both the command line and environment variables
43+
# Command line options take precedence and if neither is set commands should fall back to
44+
# default postgres environment variables.
45+
46+
# Connection options
47+
# h|host
48+
# p|port
49+
# U|user
50+
# d|dbname
51+
52+
# Password options
53+
# W|password
54+
# password-file
55+
56+
# Create User DB options
57+
# no-create-database
58+
# no-revoke-public-create
59+
60+
# Drop User DB options
61+
# drop-database
62+
63+
# We don't want to pass these back to the caller
64+
local password
65+
local password_file
66+
67+
# Pull in environment variables prefixed with DATABASE_
68+
for item in host port username dbname password password_file no_create_database no_revoke_public_create drop_database; do
69+
local varname
70+
varname="DATABASE_${item^^}"
71+
if [[ -n "${!varname:-}" ]]; then
72+
eval ${item}="${!varname}"
73+
fi
74+
done
75+
76+
# Options and long options
77+
local options="h:p:U:d:W:"
78+
local longopts="host:,port:,username:,dbname:,password:,password-file:,no-create-database,no-revoke-public-create,drop-database,format:,compression:,skip-globals"
79+
local parsed
80+
81+
# Parse with getopt (not getopts)
82+
! parsed=$(getopt --quiet --options=${options} --longoptions=${longopts} --name "${0}" -- "${@}")
83+
eval set -- "${parsed}"
84+
while true; do
85+
case "${1}" in
86+
-h|--host)
87+
host="${2}"
88+
shift 2
89+
;;
90+
-p|--port)
91+
port="${2}"
92+
shift 2
93+
;;
94+
-U|--username)
95+
user="${2}"
96+
shift 2
97+
;;
98+
-d|--database)
99+
database="${2}"
100+
shift 2
101+
;;
102+
-W|--password)
103+
password="${2}"
104+
shift 2
105+
;;
106+
--password-file)
107+
password_file="${2}"
108+
shift 2
109+
;;
110+
--no-create-database)
111+
no_create_database="true"
112+
shift
113+
;;
114+
--no-revoke-public-create)
115+
no_revoke_public_create="true"
116+
shift
117+
;;
118+
--drop-database)
119+
drop_database="true"
120+
shift
121+
;;
122+
--format)
123+
format="${2}"
124+
shift 2
125+
;;
126+
--compression)
127+
compression="${2}"
128+
shift 2
129+
;;
130+
--skip-globals)
131+
skip_globals="true"
132+
shift
133+
;;
134+
--)
135+
shift
136+
break
137+
;;
138+
*)
139+
echo "Unrecognised option"
140+
exit 3
141+
;;
142+
esac
143+
done
144+
145+
# Set remaining command line arguments into an array
146+
args=( "$@" )
147+
148+
# Setup connection string
149+
connection=()
150+
for item in host port user dbname; do
151+
if [[ -n "${!item:-}" ]]; then
152+
connection+=("--${item}" "${!item}")
23153
fi
24-
sleep 1
25-
(exec 3<>/dev/tcp/${1-$HOST}/${2-$PORT}) &>/dev/null && break
26-
echo -n "."
27154
done
28-
echo " connected."
29-
exec 3>&-
30-
exec 3<&-
155+
156+
# Read in the password file if set
157+
if [[ -n "${password_file:-}" ]]; then
158+
# Read password file if set on the command line or DATABASE_PASSWORD_FILE
159+
password="$(cat "${password_file}")"
160+
fi
161+
162+
# If the password was set write it to .pgpass (or save a temporary file and set PGPASSFILE)
163+
if [[ -n "${password}" ]]; then
164+
local old_umask
165+
old_umask="$(umask)"
166+
umask 0077
167+
PGPASSFILE="$(mktemp)"
168+
export PGPASSFILE
169+
echo "*:*:*:*:${password}" > "${PGPASSFILE}"
170+
umask "${old_umask}"
171+
echoerr ">>> Password written to ${PGPASSFILE}"
172+
fi
31173
}
32174

175+
echoerr() { echo "$@" 1>&2; }
176+
177+
genpasswd() {
178+
# Ambiguous characters have been been excluded
179+
CHARS="abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
180+
181+
export LC_CTYPE=C # Quiet tr warnings
182+
local length
183+
length="${1:-16}"
184+
set +o pipefail
185+
strings < /dev/urandom | tr -dc "${CHARS}" | head -c "${length}" | xargs
186+
set -o pipefail
187+
}
33188

34-
function genpasswd() {
35-
export LC_CTYPE=C # Quiet tr warnings
36-
local l=$1
37-
[ "$l" == "" ] && l=16
38-
set +o pipefail
39-
strings < /dev/urandom | tr -dc A-Za-z0-9_ | head -c ${l}
40-
set -o pipefail
189+
wait_postgres() {
190+
wait_tcp "${host:-${PGHOST:-127.0.0.1}}" "${port:-${PGPORT:-5432}}"
41191
}

0 commit comments

Comments
 (0)