-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathsetup.sh
executable file
·350 lines (298 loc) · 11.5 KB
/
setup.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env sh
# This script sets up dockerized Redash on Debian 12.x, Fedora 38 or later, Ubuntu LTS 20.04 & 22.04, and RHEL (and compatible) 8.x & 9.x
set -eu
REDASH_BASE_PATH=/opt/redash
DONT_START=no
OVERWRITE=no
PREVIEW=no
# Ensure the script is being run as root
ID=$(id -u)
if [ "0$ID" -ne 0 ]
then echo "Please run this script as root"
exit
fi
# Ensure the 'docker' and 'docker-compose' commands are available
# and if not, ensure the script can install them
SKIP_DOCKER_INSTALL=no
if [ -x "$(command -v docker)" ]; then
# The first condition is 'docker-compose (v1)' and the second is 'docker compose (v2)'.
if [ -x "$(command -v docker-compose)" ] || (docker compose 1> /dev/null 2>& 1 && [ $? -eq 0 ]); then
SKIP_DOCKER_INSTALL=yes
fi
elif [ ! -f /etc/os-release ]; then
echo "Unknown Linux distribution. This script presently works only on Debian, Fedora, Ubuntu, and RHEL (and compatible)"
exit
fi
# Parse any user provided parameters
opts="$(getopt -o doph -l dont-start,overwrite,preview,help --name "$0" -- "$@")"
eval set -- "$opts"
while true
do
case "$1" in
-d|--dont-start)
DONT_START=yes
shift
;;
-o|--overwrite)
OVERWRITE=yes
shift
;;
-p|--preview)
PREVIEW=yes
shift
;;
-h|--help)
echo "Redash setup script usage: $0 [-d|--dont-start] [-p|--preview] [-o|--overwrite]"
echo " The --preview (also -p) option uses the Redash 'preview' Docker image instead of the last stable release"
echo " The --overwrite (also -o) option replaces any existing configuration with a fresh new install"
echo " The --dont-start (also -d) option installs Redash, but doesn't automatically start it afterwards"
exit 1
;;
--)
shift
break
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
install_docker_debian() {
echo "** Installing Docker (Debian) **"
export DEBIAN_FRONTEND=noninteractive
apt-get -qqy update
DEBIAN_FRONTEND=noninteractive apt-get -qqy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -yy install apt-transport-https ca-certificates curl software-properties-common pwgen gnupg
# Add Docker GPG signing key
if [ ! -f "/etc/apt/keyrings/docker.gpg" ]; then
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
fi
# Add Docker download repository to apt
cat <<EOF >/etc/apt/sources.list.d/docker.list
deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable
EOF
apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
install_docker_fedora() {
echo "** Installing Docker (Fedora) **"
# Add Docker package repository
dnf -qy install dnf-plugins-core
dnf config-manager --quiet --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
# Install Docker
dnf install -qy docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin pwgen
# Start Docker and enable it for automatic start at boot
systemctl start docker && systemctl enable docker
}
install_docker_rhel() {
echo "** Installing Docker (RHEL and compatible) **"
# Add EPEL package repository
if [ "x$DISTRO" = "xrhel" ]; then
# Genuine RHEL doesn't have the epel-release package in its repos
RHEL_VER=$(. /etc/os-release && echo "$VERSION_ID" | cut -d "." -f1)
if [ "0$RHEL_VER" -eq "9" ]; then
yum install -qy https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
else
yum install -qy https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
fi
yum install -qy yum-utils
else
# RHEL compatible distros do have epel-release available
yum install -qy epel-release yum-utils
fi
yum update -qy
# Add Docker package repository
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum update -qy
# Install Docker
yum install -qy docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin pwgen
# Start Docker and enable it for automatic start at boot
systemctl start docker && systemctl enable docker
}
install_docker_ubuntu() {
echo "** Installing Docker (Ubuntu) **"
export DEBIAN_FRONTEND=noninteractive
apt-get -qqy update
DEBIAN_FRONTEND=noninteractive sudo -E apt-get -qqy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -yy install apt-transport-https ca-certificates curl software-properties-common pwgen gnupg
# Add Docker GPG signing key
if [ ! -f "/etc/apt/keyrings/docker.gpg" ]; then
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
fi
# Add Docker download repository to apt
cat <<EOF >/etc/apt/sources.list.d/docker.list
deb [arch=""$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable
EOF
apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
create_directories() {
echo "** Creating $REDASH_BASE_PATH directory structure for Redash **"
if [ ! -e "$REDASH_BASE_PATH" ]; then
mkdir -p "$REDASH_BASE_PATH"
chown "$USER:" "$REDASH_BASE_PATH"
fi
if [ -e "$REDASH_BASE_PATH"/postgres-data ]; then
# PostgreSQL database directory seems to exist already
if [ "x$OVERWRITE" = "xyes" ]; then
# We've been asked to overwrite the existing database
echo "Shutting down any running Redash instance"
if [ -e "$REDASH_BASE_PATH"/compose.yaml ]; then
docker compose -f "$REDASH_BASE_PATH"/compose.yaml down
fi
echo "Moving old Redash PG database directory out of the way"
mv "${REDASH_BASE_PATH}/postgres-data" "${REDASH_BASE_PATH}/postgres-data-${TIMESTAMP_NOW}"
mkdir "$REDASH_BASE_PATH"/postgres-data
fi
else
mkdir "$REDASH_BASE_PATH"/postgres-data
fi
}
create_env() {
echo "** Creating Redash environment file **"
# Minimum mandatory values (when not just developing)
COOKIE_SECRET=$(pwgen -1s 32)
SECRET_KEY=$(pwgen -1s 32)
PG_PASSWORD=$(pwgen -1s 32)
DATABASE_URL="postgresql://postgres:${PG_PASSWORD}@postgres/postgres"
if [ -e "$REDASH_BASE_PATH"/env ]; then
# There's already an environment file
if [ "x$OVERWRITE" = "xno" ]; then
echo
echo "Environment file already exists, reusing that one + and adding any missing (mandatory) values"
# Add any missing mandatory values
REDASH_COOKIE_SECRET=
REDASH_COOKIE_SECRET=$(. "$REDASH_BASE_PATH"/env && echo "$REDASH_COOKIE_SECRET")
if [ -z "$REDASH_COOKIE_SECRET" ]; then
echo "REDASH_COOKIE_SECRET=$COOKIE_SECRET" >> "$REDASH_BASE_PATH"/env
echo "REDASH_COOKIE_SECRET added to env file"
fi
REDASH_SECRET_KEY=
REDASH_SECRET_KEY=$(. "$REDASH_BASE_PATH"/env && echo "$REDASH_SECRET_KEY")
if [ -z "$REDASH_SECRET_KEY" ]; then
echo "REDASH_SECRET_KEY=$SECRET_KEY" >> "$REDASH_BASE_PATH"/env
echo "REDASH_SECRET_KEY added to env file"
fi
POSTGRES_PASSWORD=
POSTGRES_PASSWORD=$(. "$REDASH_BASE_PATH"/env && echo "$POSTGRES_PASSWORD")
if [ -z "$POSTGRES_PASSWORD" ]; then
POSTGRES_PASSWORD=$PG_PASSWORD
echo "POSTGRES_PASSWORD=$POSTGRES_PASSWORD" >> "$REDASH_BASE_PATH"/env
echo "POSTGRES_PASSWORD added to env file"
fi
REDASH_DATABASE_URL=
REDASH_DATABASE_URL=$(. "$REDASH_BASE_PATH"/env && echo "$REDASH_DATABASE_URL")
if [ -z "$REDASH_DATABASE_URL" ]; then
echo "REDASH_DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres/postgres" >> "$REDASH_BASE_PATH"/env
echo "REDASH_DATABASE_URL added to env file"
fi
echo
return
fi
# Move any existing environment file out of the way
mv -f "${REDASH_BASE_PATH}/env" "${REDASH_BASE_PATH}/env.old-${TIMESTAMP_NOW}"
fi
echo "Generating brand new environment file"
cat <<EOF >"$REDASH_BASE_PATH"/env
PYTHONUNBUFFERED=0
REDASH_LOG_LEVEL=INFO
REDASH_REDIS_URL=redis://redis:6379/0
REDASH_COOKIE_SECRET=$COOKIE_SECRET
REDASH_SECRET_KEY=$SECRET_KEY
POSTGRES_PASSWORD=$PG_PASSWORD
REDASH_DATABASE_URL=$DATABASE_URL
REDASH_ENFORCE_CSRF=true
REDASH_GUNICORN_TIMEOUT=60
EOF
}
setup_compose() {
echo "** Creating Redash Docker compose file **"
cd "$REDASH_BASE_PATH"
GIT_BRANCH="${REDASH_BRANCH:-master}" # Default branch/version to master if not specified in REDASH_BRANCH env var
if [ "x$OVERWRITE" = "xyes" -a -e compose.yaml ]; then
mv -f compose.yaml compose.yaml.old-${TIMESTAMP_NOW}
fi
curl -fsSOL https://raw.githubusercontent.com/getredash/setup/"$GIT_BRANCH"/data/compose.yaml
TAG="10.1.0.b50633"
if [ "x$PREVIEW" = "xyes" ]; then
TAG="preview"
fi
sed -i "s|__TAG__|$TAG|" compose.yaml
export COMPOSE_FILE="$REDASH_BASE_PATH"/compose.yaml
export COMPOSE_PROJECT_NAME=redash
}
create_make_default() {
echo "** Creating redash_make_default.sh script **"
curl -fsSOL https://raw.githubusercontent.com/getredash/setup/"$GIT_BRANCH"/redash_make_default.sh
sed -i "s|__COMPOSE_FILE__|$COMPOSE_FILE|" redash_make_default.sh
sed -i "s|__TARGET_FILE__|$PROFILE|" redash_make_default.sh
chmod +x redash_make_default.sh
}
startup() {
if [ "x$DONT_START" != "xyes" ]; then
echo
echo "*********************"
echo "** Starting Redash **"
echo "*********************"
echo "** Initialising Redash database **"
docker compose run --rm server create_db
echo "** Starting the rest of Redash **"
docker compose up -d
echo
echo "Redash has been installed and is ready for configuring at http://$(hostname -f):5000"
echo
else
echo
echo "*************************************************************"
echo "** As requested, Redash has been installed but NOT started **"
echo "*************************************************************"
echo
fi
}
echo
echo "Redash installation script. :)"
echo
TIMESTAMP_NOW=$(date +'%Y.%m.%d-%H.%M')
# Run the distro specific Docker installation
PROFILE=.profile
if [ "$SKIP_DOCKER_INSTALL" = "yes" ]; then
echo "Docker and Docker Compose are already installed, so skipping that step."
else
DISTRO=$(. /etc/os-release && echo "$ID")
case "$DISTRO" in
debian)
install_docker_debian
;;
fedora)
install_docker_fedora
;;
ubuntu)
install_docker_ubuntu
;;
almalinux|centos|ol|rhel|rocky)
PROFILE=.bashrc
install_docker_rhel
;;
*)
echo "This doesn't seem to be a Debian, Fedora, Ubuntu, nor RHEL (compatible) system, so this script doesn't know how to add Docker to it."
echo
echo "Please contact the Redash project via GitHub and ask about getting support added, or add it yourself and let us know. :)"
echo
exit
;;
esac
fi
# Do the things that aren't distro specific
create_directories
create_env
setup_compose
create_make_default
startup
echo "If you want Redash to be your default Docker Compose project when you login to this server"
echo "in future, then please run $REDASH_BASE_PATH/redash_make_default.sh"
echo
echo "That will set some Docker specific environment variables just for Redash. If you"
echo "already use Docker Compose on this computer for other things, you should probably skip it."