-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpsql-docker
executable file
·219 lines (161 loc) · 4.47 KB
/
psql-docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
# Launches a Docker psql in interactive mode
# Usage text
usage()
{
cat << EOF
usage: $0 [options] host port user dbname
Launches a Docker psql in interactive mode.
Must be run as superuser with environment:
sudo -E psql-docker
The script mounts the working folder as output folder for the container, mapping
the current folder user to the container's postgres user.
ARGUMENTS:
host Host to connect to. Defaults to "postgis".
port Port to connect to. Defaults to "5432".
user User to connect with. Defaults to "postgres".
dbname DB to connect to. Defaults to "postgres".
OPTIONS:
-h This help.
-n [name] A name. If a container with the same name is present,
it will restart it. If not, it will be created.
If not present, an interactive, --rm container
will be created.
-t [version tag] Launches a certain PostGIS version tag.
If not present, 'latest' will be issued.
-v [folder]:[name] Mounts local folders. Multiple volumes can be issued.
/home/$USER:/home is mounted by default.
-l [container name]:[id] Links to that container assigning the given ID to it.
Multiple links can be issued. If a single container is given
and no ID is specified, "postgis" will be assumed as ID. Volumes
are linked too.
-p [folder] A folder inside the container whose user ID must be mapped to
the postgres user. Defaults to the /output/ folder.
-u uid:gid Assign the given UID and GID to the postgres user. In Mac is not an
issue, but in Linux it's critical.
USAGE PATTERNS:
Most common usage case is:
sudo -E psql-docker -l PG-Container-To-Work-On
sudo -E psql-docker a.server.es 5434 theUser theDb
The first one links to a PostGIS container and opens a psql session on it. The second
connects to an instance running on a server host.
EOF
}
# Variables
NAME=
VERSION=
LINKS=()
VOLUMES=()
FOLDER="/output/"
# Command line parsing
POS=0
while getopts ":hn:t:l:v:p:u:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
n)
NAME=$OPTARG
POS=$((POS+2))
;;
t)
VERSION=$OPTARG
POS=$((POS+2))
;;
v)
VOLUMES+=("$OPTARG")
POS=$((POS+2))
;;
l)
LINKS+=("$OPTARG")
POS=$((POS+2))
;;
p)
FOLDER=$OPTARG
POS=$((POS+2))
;;
u) UGID=$OPTARG
POS=$((POS+2))
;;
?)
usage
exit
;;
esac
done
# Parse arguments
shift $POS
HOST=$1
PORT=$2
USER=$3
DB=$4
if [[ $HOST == "" ]] ; then
HOST="postgis"
fi
if [[ $PORT == "" ]] ; then
PORT="5432"
fi
if [[ $USER == "" ]] ; then
USER="postgres"
fi
if [[ $DB == "" ]] ; then
DB="postgres"
fi
# Check for empty version
if [ -z "$VERSION" ] ; then
VERSION="latest"
fi
# Command parsing
C="docker "
# Check for empty name for issuing an --rm -ti container
if [ -z "$NAME" ] ; then
C+="run --rm -ti "
else
# Check for existing container
DOCKERID=`docker ps -a -q -f name=${NAME}`
if [ $DOCKERID ] ; then
C+="start ${NAME} "
eval $C
exit 0
else
C+="run -ti --name ${NAME} "
fi
fi
# Volumes
if [ "${#VOLUMES[@]}" -gt "0" ] ; then
for ((i=0;i<=((${#VOLUMES[@]}-1));i++))
do
C+="-v ${VOLUMES[i]} "
done;
fi
# Add the current folder as output
C+="-v $(pwd):/output/ "
# Links
if [ "${#LINKS[@]}" -gt "0" ] ; then
for ((i=0;i<=((${#LINKS[@]}-1));i++))
do
# Only the container name is needed to link volumes, strip the alias
IFS=':'; CONNAME=($LINKS[i]); unset IFS;
if ! [[ ${LINKS[i]} == *":"* ]] ; then
C+="--link ${LINKS[i]}:postgis --volumes-from ${LINKS[i]} "
else
C+="--link ${LINKS[i]} --volumes-from ${LINKS[i]} "
fi
done;
fi
# User UID and GID
if [ -z "$UGID" ] ; then
IFS=':' ; R=($UGID)
C+="-e \"CUID=${R[0]}\" -e \"CGID=${R[1]}\" "
unset IFS
fi
# Check if Darwin is the OS, if not, consider UID_FOLDER
if ! [ "$(uname -s)" == "Darwin" ]; then
C+="-e \"UID_FOLDER=${FOLDER}\" "
fi
C+="-e LANG=C.UTF-8 geographica/postgis:${VERSION} "
C+="su postgres -c \"psql -h ${HOST} -p ${PORT} -U ${USER} ${DB}\""
# Launch
eval $C