-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
Β·305 lines (269 loc) Β· 9.95 KB
/
install.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
#!/usr/bin/env bash
# Copyright (c) 2022 Werbot, Inc.
#
# This is a simple script that can be downloaded and run from
# https://install.werbot.com in order to install the Werbot
# command-line tools and all Werbot components.
#
# Run commands:
# bash <(curl -L -Ss https://install.werbot.com) --domain=domain.com
# bash <(wget -qO- https://install.werbot.com) --domain=domain.com
#
# Parameters:
# --domain - Main domain for work project. For this domain.
# You need to create DNS records CNAME type for subdomain "api" and "app"
# --geolite - Link to update the GeoLite2-Country database
# --cf-email - Cloudflare email
# --cf-api-key - Cloudflare API key
if readlink /proc/$$/exe | grep -q "dash"; then
echo 'This installer needs to be run with "bash", not "sh".'
exit
fi
case "$(uname -s)" in
"Windows")
echo "This script does not support the OS/Distribution on this machine."
echo "If you feel that this is an error contact [email protected]"
exit 1
;;
esac
# Main settings
APP_CDN="https://app.werbot.com"
LICENSE_CDN="https://license.werbot.com"
WERBOT_DIR="/home/werbot"
# Service settings
DOMAIN=""
GEOLITE_DATABASE="https://install.werbot.com/GeoLite2-Country.mmdb"
CLOUDFLARE_EMAIL=""
CLOUDFLARE_API_KEY=""
# Global setting
COLOR_GREY=$(tput setaf 0)
COLOR_RED=$(tput setaf 1)
COLOR_GREEN=$(tput setaf 2)
COLOR_YELLOW=$(tput setaf 3)
COLOR_RESET=$(tput sgr0)
command_exists() {
command -v "$@" >/dev/null 2>&1
}
gen_password() {
tr -cd 'a-zA-Z0-9!#$%&()*+?@[]^_' </dev/urandom |
fold -w 32 |
head -n 1
}
print_answer() {
local COLOR="$COLOR_RESET"
for flag in "$@"; do
case $flag in
grey) COLOR=$COLOR_GREY ;;
green) COLOR=$COLOR_GREEN ;;
yellow) COLOR=$COLOR_YELLOW ;;
red) COLOR=$COLOR_RED ;;
esac
done
echo "${COLOR}$1${COLOR_RESET}" >&2
}
print_header() {
printf "%.45s " "$@ ........................................"
}
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}
get_ip() {
local IP=$(ip addr |
egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' |
egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." |
head -n 1) >/dev/null 2>&1
[ -z ${IP} ] && IP=$(curl -s ${API_CDN}/ip)
echo ${IP}
}
install() {
clear
echo "${COLOR_RED} _ _ "
echo " _ _ _ _____ ____| |__ ___ _| |_ "
echo " | || || | ___ |/ ___) _ \ / _ (_ _)"
echo " | || || | ____| | | |_) ) |_| || |_ "
echo " \_____/|_____)_| |____/ \___/ \__)"
echo "${COLOR_RESET}"
echo " Install Enterprise version"
echo "---------------------------------------------"
# Check options
for flag in "$@"; do
case $flag in
--domain=*)
DOMAIN="${flag#*=}"
shift
;;
--geolite=*)
GEOLITE_DATABASE="${flag#*=}"
shift
;;
--cf-email=*)
CLOUDFLARE_EMAIL="${flag#*=}"
shift
;;
--cf-api-key=*)
CLOUDFLARE_API_KEY="${flag#*=}"
shift
;;
esac
done
echo ""
echo "To begin the installation, you must set up DNS records"
echo "for your domain. Detailed information on how to do"
echo "this is on the page https://werbot.com/doc/install."
echo ""
echo "${COLOR_RED}Currently support only Cloudflare DNS provider."
echo "This is required to issue an SSL certificate."
echo "After installation, you can install your certificate.${COLOR_RESET}"
echo ""
read -p "Have you set up DNS entries for your domain (y/n)? " user_answer
case "$user_answer" in
"y" | "Y") echo "" ;;
*) exit ;;
esac
# Domain parameters
domain_regex='(?=^.{1,254}$)(^(?>(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{2,})$)'
if [[ -z "$(echo $DOMAIN | grep -P $domain_regex)" ]]; then
read -rp "Domain name: " -e -i "$DOMAIN" DOMAIN
if [[ -z "$(echo $DOMAIN | grep -P $domain_regex)" ]]; then
echo "${COLOR_RED}$DOMAIN${COLOR_RESET} - is not validate domain"
exit 1
fi
fi
# Cloudflare email parameters
cf_email_regex='(^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$)'
if [[ -z "$(echo $CLOUDFLARE_EMAIL | grep -P $cf_email_regex)" ]]; then
read -rp "Cloudflare email: " -e -i "${CLOUDFLARE_EMAIL}" CLOUDFLARE_EMAIL
if [[ -z "$(echo $CLOUDFLARE_EMAIL | grep -P $cf_email_regex)" ]]; then
echo "${COLOR_RED}$CLOUDFLARE_EMAIL${COLOR_RESET} - is not validate email"
exit 1
fi
fi
# Cloudflare API key parameters
cf_api_key_regex='(^.{37}$)'
if [[ -z "$(echo $CLOUDFLARE_API_KEY | grep -P $cf_api_key_regex)" ]]; then
read -rp "Cloudflare API key: " -e -i "${CLOUDFLARE_API_KEY}" CLOUDFLARE_API_KEY
if [[ -z "$(echo $CLOUDFLARE_API_KEY | grep -P $cf_api_key_regex)" ]]; then
echo "${COLOR_RED}$CLOUDFLARE_API_KEY${COLOR_RESET} - is not validate API key"
exit 1
fi
fi
# Checking operating system
local OS
print_header "Checking operating system"
OS=$(uname -s)
case "$OS" in
Linux) OS=linux ;;
Darwin) OS=darwin ;;
*) print_answer "NOT SUPPORTED" red && exit 1 ;;
esac
print_answer "SUCCESS" green
# ------------------------------------------------
# Checking CPU architecture
local CPU
print_header "Checking CPU architecture"
CPU=$(uname -m)
case "$CPU" in
x86_64 | x86-64 | x64 | amd64) CPU=amd64 ;;
*) print_answer "NOT SUPPORTED" red && exit 1 ;;
esac
print_answer "SUCCESS" green
# ------------------------------------------------
# Installing jq
print_header "Checking install jq"
command_exists jq || {
print_answer "NEED INSTALLATION" yellow
print_header "Installing jq"
if [ "$OS" = darwin ]; then
brew install jq >/dev/null 2>&1
elif [ "$OS" = linux ]; then
sudo curl -L https://github.com/stedolan/jq/releases/download/$(get_latest_release "stedolan/jq")/jq-linux64 -o /usr/local/bin/jq >/dev/null 2>&1
sudo chmod +x /usr/local/bin/jq
fi
command_exists jq || {
print_answer "ERROR" red && exit 1
}
}
print_answer "SUCCESS" green
# ------------------------------------------------
# Installing docker
print_header "Checking install docker"
command_exists docker || {
print_answer "NEED INSTALLATION" yellow
print_header "Installing docker"
if [ "$OS" = darwin ]; then
brew install docker >/dev/null 2>&1
elif [ "$OS" = linux ]; then
curl -sSf https://get.docker.com | sh >/dev/null 2>&1
fi
command_exists docker || {
print_answer "ERROR" red && exit 1
}
}
print_answer "SUCCESS" green
# ------------------------------------------------
# Installing docker-compose
print_header "Checking install docker-compose"
command_exists docker-compose || {
print_answer "NEED INSTALLATION" yellow
print_header "Installing docker-compose"
if [ "$OS" = darwin ]; then
brew install docker-compose >/dev/null 2>&1
elif [ "$OS" = linux ]; then
sudo curl -L https://github.com/docker/compose/releases/download/$(get_latest_release "docker/compose")/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose >/dev/null 2>&1
sudo chmod +x /usr/local/bin/docker-compose
fi
command_exists docker-compose command_exists || {
print_answer "ERROR" red && exit 1
}
}
print_answer "SUCCESS" green
# ------------------------------------------------
# Create new user
if ! getent passwd werbot >/dev/null; then
print_header "Adding a new werbot user"
sudo useradd -m -d $WERBOT_DIR werbot -s /bin/bash
echo "werbot ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/werbot >/dev/null 2>&1
sudo chmod 0440 /etc/sudoers.d/werbot
sudo usermod -a -G sudo werbot
sudo su - werbot -c "ssh-keygen -q -t ed25519 -N '' -f ~/.ssh/id_ed25519 <<<y"
sudo usermod -aG docker werbot
# newgrp docker
print_answer "SUCCESS" green
fi
# ------------------------------------------------
# Create structure service
print_header "Create structure service"
sudo su - werbot -c "git clone https://github.com/werbot/install.werbot.com.git $WERBOT_DIR/service/tmp" >/dev/null 2>&1
sudo su - werbot -c "mv $WERBOT_DIR/service/tmp/cfg/grafana $WERBOT_DIR/service/grafana" >/dev/null 2>&1
sudo su - werbot -c "mv $WERBOT_DIR/service/tmp/cfg/haproxy $WERBOT_DIR/service/haproxy" >/dev/null 2>&1
sudo su - werbot -c "mv $WERBOT_DIR/service/tmp/cfg/loki $WERBOT_DIR/service/loki" >/dev/null 2>&1
sudo su - werbot -c "mv $WERBOT_DIR/service/tmp/cfg/prometheus $WERBOT_DIR/service/prometheus" >/dev/null 2>&1
sudo su - werbot -c "mv $WERBOT_DIR/service/tmp/cfg/promtail $WERBOT_DIR/service/promtail" >/dev/null 2>&1
sudo su - werbot -c "mkdir -p $WERBOT_DIR/service/{core,postgres,postgres/ca}"
sudo su - werbot -c "cd $WERBOT_DIR/service/postgres/ca/
openssl req -new -text -passout pass:abcd -subj /CN=Werbot -out server.req -keyout privkey.pem
openssl rsa -in privkey.pem -passin pass:abcd -out server.key
openssl req -x509 -in server.req -text -key server.key -out server.crt
chmod 600 server.key
sudo chown 70 server.key" >/dev/null 2>&1
sudo su - werbot -c "ssh-keygen -q -t rsa -b 4096 -N '' -f $WERBOT_DIR/service/core/server_key <<<y
rm $WERBOT_DIR/service/core/server_key.pub
chmod 664 $WERBOT_DIR/service/core/server_key" >/dev/null 2>&1
# TODO: create $WERBOT_DIR/service/.env
sudo su - werbot -c "curl -s -o $WERBOT_DIR/service/core/GeoLite2-Country.mmdb $GEOLITE_DATABASE"
# TODO: update domain $WERBOT_DIR/service/haproxy/config.cfg
# TODO: download $WERBOT_DIR/service/docker-compose.yaml
sudo chown 10001:10001 $WERBOT_DIR/service/core/
sudo rm -rf $WERBOT_DIR/service/tmp
print_answer "SUCCESS" green
# ------------------------------------------------
# Pulling docker containers (~2min)
print_header "Pulling docker containers (~2min)"
# docker-compose -f $WERBOT_DIR/service/docker-compose.yaml pull >/dev/null 2>&1
print_answer "SUCCESS" green
# ------------------------------------------------
echo "My external ip: $(get_ip)"
}
install "$@" || exit 1