-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·214 lines (190 loc) · 6.74 KB
/
start.sh
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
#!/bin/bash
usage() {
echo -e "Usage:"
echo -e " --psql-password Sets the PostgreSQL password"
echo -e " (The $PSQLPASS is '$PSQL_PASSWORD')\n"
echo -e " --psql-port Sets the PostgreSQL port"
echo -e " (The $PSQLPO is '$PSQL_PORT')\n"
echo -e " --pgadmin-port Sets the port for PGAdmin 4"
echo -e " (The $PGAPORT is '$PGADMIN_PORT')\n"
echo -e " --pgadmin-user Sets the default login user for PGAdmin 4"
echo -e " (The $PGAUSER is '$PGADMIN_USER')\n"
echo -e " --pgadmin-password Sets the password for the default login for PGAdmin 4"
echo -e " (The $PGAPASS is '$PGADMIN_PASSWORD')\n"
echo -e " -h --help Shows this usage message"
echo -e " -t --test Shows hows docker will be configrued but won't run docker"
}
showtest() {
echo -e "Docker will be configured as follows:"
echo -e " PGCONTAINER_NAME = $PGCONTAINER_NAME"
echo -e " PSQL_PASSWORD = $PSQL_PASSWORD"
echo -e " PSQL_PORT = $PSQL_PORT"
echo -e " PGADMIN_PORT = $PGADMIN_PORT"
echo -e " PGADMIN_USER = $PGADMIN_USER"
echo -e " PGADMIN_PASSWORD = $PGADMIN_PASSWORD"
}
sudowarn() {
echo -e "$red╒═════════════════════════════════════════════════════════╕$normal"
echo -e "$red│$normal $yellow$warn_icon $normal WARNING $red│$normal"
echo -e "$red├─────────────────────────────────────────────────────────┤$normal"
echo -e "$red│$normal You're not part of the docker group so you'll need $red│$normal"
echo -e "$red│$normal sudo access in order to run this script. If you don't $red│$normal"
echo -e "$red│$normal have sudo access please ask an admin to add you to the $red│$normal"
echo -e "$red│$normal the docker group. $red│$normal"
echo -e "$red╘═════════════════════════════════════════════════════════╛$normal\n"
}
# Define colors and styles
normal="\033[0m"
bold="\033[1m"
green="\e[32m"
red="\e[31m"
yellow="\e[93m"
# Define Icons (if in doubt try nerd font... then emoji.. finally nothing)
if [[ "$NERD_FONT" == 'true' ]]; then
file_icon="\ue615"
warn_icon="\uf071 "
docker_icon="\uf308 "
whale_icon="🐋"
elif [[ "$EMOJI" == 'false' ]]; then
file_icon=""
warn_icon="!"
whale_icon=""
docker_icon=""
else
file_icon="📄"
warn_icon="⚠️"
whale_icon="🐋"
docker_icon="🐳"
fi
# Source from config file
SCRIPT=`realpath -s $0`
SCRIPTPATH=`dirname $SCRIPT`
echo "Config file is = $SCRIPTPATH/.config"
if [ -f "$SCRIPTPATH/.config" ]; then
echo -e "$green $file_icon Loading config data from file $SCRIPTPATH/.config... $normal"
source .config
fi
if [ -z $PGCONTAINER_NAME ]; then
PGCONTAINER_NAME=postgres
fi
if [ -z $PSQL_PASSWORD ]; then
PSQL_PASSWORD=docker
PSQLPASS='default'
else
PSQLPASS='current'
fi
if [ -z $PSQL_PORT ]; then
PSQL_PORT=5432
PSQLPO='default'
else
PSQLPO='current'
fi
if [ -z $PGADMIN_PORT ]; then
PGADMIN_PORT=5050
PGAPORT='default'
else
PGAPORT='current'
fi
if [ -z $PGADMIN_USER ]; then
PGAUSER='default'
else
PGAUSER='current'
fi
if [ -z $PGADMIN_PASSWORD ]; then
PGADMIN_PASSWORD=8lScP10eY1L3oeXbBM1E
PGAPASS='default'
else
PGAPASS='current'
fi
TEST=false
while [ "$1" != "" ]; do
case $1 in
--psql-password ) shift
PSQL_PASSWORD=$1
;;
--psql-port ) shift
PSQL_PORT=$1
;;
--pgadmin-port ) shift
PGADMIN_PORT=$1
;;
--pgadmin-user ) shift
PGADMIN_USER=$1
;;
--pgadmin-password ) shift
PGADMIN_PASSWORD=$1
;;
-h | --help ) usage
exit
;;
-t | --test ) TEST=true
;;
--show-warn ) sudowarn;
exit
;;
* ) echo -e "Unknown option $1...\n"
usage
exit 1
esac
shift
done
if [[ $(uname) == "Linux" ]]; then
if groups $(whoami) |grep docker > /dev/null 2>&1; then
DOCKER_CMD="docker"
else
DOCKER_CMD="sudo docker"
sudowarn
fi
else
DOCKER_CMD="docker"
fi
if [ $TEST == "true" ]; then
showtest
exit
fi
docker volume ls | grep pgdata > /dev/null 2>&1
if [[ $? != 0 ]]; then
docker volume create pgdata > /dev/null 2>&1
fi
docker volume ls | grep pga4data > /dev/null 2>&1
if [[ $? != 0 ]]; then
docker volume create pga4data > /dev/null 2>&1
fi
docker network list |grep pgnetwork > /dev/null 2>&1
if [[ $? != 0 ]]; then
docker network create --driver bridge pgnetwork > /dev/null 2>&1
fi
docker ps -f name=$PGCONTAINER_NAME |grep $PGCONTAINER_NAME > /dev/null 2>&1
if [ $? != 0 ]; then
echo -e " $whale_icon Starting PostgreSQL..."
$DOCKER_CMD run --rm --name $PGCONTAINER_NAME \
--network pgnetwork \
--hostname postgres \
-e POSTGRES_PASSWORD=$PSQL_PASSWORD \
-d -p 0.0.0.0:$PSQL_PORT:5432 \
--volume pgdata:/var/lib/postgresql/data \
postgres 1> /dev/null
else
echo -e " $warn_icon Docker is already running PostgreSQL"
fi
docker ps -f name=pgadmin4 |grep pgadmin4 > /dev/null 2>&1
if [ $? != 0 ]; then
echo -e " $whale_icon Starting PGAdmin 4..."
cat << EOF > /tmp/.pgadmin-env.list
PGADMIN_DEFAULT_EMAIL=$PGADMIN_USER
PGADMIN_DEFAULT_PASSWORD=$PGADMIN_PASSWORD
PGADMIN_LISTEN_PORT=$PGADMIN_PORT
EOF
$DOCKER_CMD run --rm --name pgadmin4 \
-d -p 0.0.0.0:$PGADMIN_PORT:$PGADMIN_PORT \
-v pga4data:/var/lib/pgadmin \
--env-file /tmp/.pgadmin-env.list \
--hostname pgadmin4 \
--network pgnetwork \
dpage/pgadmin4 1> /dev/null
rm /tmp/.pgadmin-env.list
else
echo -e " $warn_icon Docker is already running PG Admin 4"
fi
echo -e " $docker_icon PostgreSQL and PGAdmin 4 are now running in Docker!"