-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
INSTALL_dependencies.sh
executable file
·264 lines (217 loc) · 7.48 KB
/
INSTALL_dependencies.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
#!/usr/bin/env bash
#
# Description: One click install for hyperswitch router
#
#
# Global config
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
RUST_MSRV="1.78.0"
_DB_NAME="hyperswitch_db"
_DB_USER="db_user"
_DB_PASS="db_password"
OSTYPE=${OSTYPE:-}
PRE_INSTALL_MSG="Dependency install script.\n
The script assumes 'curl' and build essentials like gcc/clang are already installed.\n
\n
The script will\n
1. Install or update RUST using RUSTUP\n
2. Install Postgresql server and redis server, if found missing\n
3. Install diesel_cli program to setup database\n
4. Setup database and create necessary schema\n
"
POST_INSTALL_MSG="\n
Install was successful.\n
If rust was installed, restart the shell or configure current shell using 'SOURCE $HOME/.cargo/env'\n
"
# Used variables must be initialized
set -o nounset
# utilities
# convert semver to comparable integer
if [[ "$(id -u)" -ne 0 ]]; then
print_info "requires sudo"
SUDO=sudo
else
SUDO=""
fi
ver () {
printf "%03d%03d%03d%03d" "$(echo "$1" | tr '.' ' ')";
}
PROGNAME="$(basename $0)"
print_info () {
echo -e "$PROGNAME: $*"
}
err () {
print_info "*ERROR*:" $*
exit 1
}
need_cmd () {
if ! command -v $1 > /dev/null
then
err "Command \"${1}\" not found. Bailing out"
fi
}
prompt () {
read -p "$*? [y/N] :" ANSWER
case $ANSWER in
[Yy]*) return 1;;
*) return 0;;
esac
}
init_start_postgres () {
if [[ "${OSTYPE}" == "darwin"* ]]; then
initdb -U postgres -D /var/lib/postgres/data
elif command -v su > /dev/null; then
$SUDO su -c "initdb -D /var/lib/postgres/data" postgres
elif command -v sudo > /dev/null; then
sudo -u postgres "initdb -D /var/lib/postgres/data"
else
err "Don't know how to switch to postgres user to run commands"
fi
if command -v brew > /dev/null; then
brew services start postgresql
elif command -v service > /dev/null; then
service postgresql start
elif command -v systemctl > /dev/null; then
$SUDO systemctl start postgresql.service
else
print_info "Unable to start postgres. Please start manually"
fi
}
init_start_redis () {
if command -v brew > /dev/null; then
brew services start redis
elif command -v service > /dev/null; then
service redis-server start
service redis start
elif command -v systemctl > /dev/null; then
$SUDO systemctl start redis.service
$SUDO systemctl start redis-server.service
else
print_info "Unable to start redis. Please start manually"
fi
}
print_info ${PRE_INSTALL_MSG}
if prompt "Would you like to continue"; then
err "Aborted by user"
fi
# verify rust installation and version
if command -v cargo > /dev/null; then
print_info "\"cargo\" found. Verifying rustc version"
need_cmd rustc
RUST_VERSION="$(rustc -V | cut -d " " -f 2)"
_HAVE_VERSION="$(ver ${RUST_VERSION})"
_NEED_VERSION="$(ver ${RUST_MSRV})"
print_info "Found rust version \"${RUST_VERSION}\". MSRV is \"${RUST_MSRV}\""
if [[ ${_HAVE_VERSION} -lt ${_NEED_VERSION} ]] ; then
if command -v rustup > /dev/null; then
print_info "found rustup. Trying to install ${RUST_MSRV} version..."
rustup install "${RUST_MSRV}"
else
print_info "Couldn't find \"rustup\". ***needs upgrade***, but skipping..."
fi
else
print_info "Skipping update"
fi
else
print_info "\"cargo\" command not found..."
if ! prompt "Would you like to install \"rust\" using \"rustup\""
then
print_info "Installing \"rust\" through \"rustup\""
need_cmd curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# add to path
. "$HOME/.cargo/env"
need_cmd rustc
else
print_info "Rust installation was aborted"
err "This application needs \"rust\""
fi
fi
# Dependency checks
print_info "checking for dependencies"
install_dep () {
$INSTALL_CMD $*
}
if [[ ! -x "$(command -v psql)" ]] || [[ ! -x "$(command -v redis-server)" ]] ; then
print_info "Missing dependencies. Trying to install"
# java has an apt which seems to mess up when we look for apt
if command -v apt-get > /dev/null; then
INSTALL_CMD="$SUDO apt-get install -y"
elif command -v yum > /dev/null; then
INSTALL_CMD="$SUDO yum -y install"
elif command -v pacman > /dev/null; then
INSTALL_CMD="$SUDO pacman -S"
elif command -v brew > /dev/null; then
INSTALL_CMD="brew install"
else
err "Unable to identify the package manager"
fi
if ! command -v psql > /dev/null
then
install_dep postgresql
install_dep postgresql-contrib # not needed for macos?
install_dep postgresql-devel # needed for diesel_cli in some linux distributions
install_dep postgresql-libs # needed for diesel_cli in some linux distributions
init_start_postgres # installing libpq messes with initdb creating two copies. better to run it better libpq.
install_dep libpq-dev || install_dep libpq
else
print_info "Postgres found. skipping..."
fi
if ! command -v redis-server > /dev/null
then
# the package names differ. Need better way to identify correct package name
install_dep redis || install_dep redis
init_start_redis
else
print_info "Redis found. skipping..."
fi
else
print_info "No missing dependency."
fi
print_info "Proceeding to setup Database"
# install diesel cli
need_cmd cargo
if ! command -v diesel > /dev/null; then
print_info "diesel_cli not found. Installing..."
cargo install diesel_cli --no-default-features --features "postgres"
else
print_info "diesel_cli found."
fi
# DB setup
need_cmd psql
need_cmd diesel
read -p "Enter name for database [default: ${_DB_NAME}]: " DB_NAME
DB_NAME=${DB_NAME:-$_DB_NAME}
read -p "Enter username for ${DB_NAME} [default: ${_DB_USER}]: " DB_USER
DB_USER=${DB_USER:-$_DB_USER}
read -sp "Enter password for ${DB_USER} [default: ${_DB_PASS}]: " DB_PASS
DB_PASS=${DB_PASS:-$_DB_PASS}
print_info ""
print_info "Creating DB \"${DB_NAME}\" and user \"${DB_USER}\" with provided password"
if [[ "$OSTYPE" == "darwin"* ]]; then
psql -e -U postgres -c "CREATE DATABASE ${DB_NAME};"
psql -e -U postgres -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';"
psql -e -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};"
elif command -v su > /dev/null; then
$SUDO su -c "psql -e -c \"CREATE DATABASE ${DB_NAME};\"" postgres
$SUDO su -c "psql -e -c \"CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';\"" postgres
$SUDO su -c "psql -e -c \"GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};\"" postgres
elif command -v sudo > /dev/null; then
sudo -u postgres psql -e -c "CREATE DATABASE ${DB_NAME};"
sudo -u postgres psql -e -U postgres -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';"
sudo -u postgres psql -e -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};"
else
err "Don't know how to switch to postgres user to run commands"
fi
# run migrations
print_info "Running migrations"
MIGRATION_CMD="diesel migration --database-url=postgres://${DB_USER}:${DB_PASS}@localhost:5432/${DB_NAME} run"
if [[ -d "migrations" ]]; then
$MIGRATION_CMD
else
print_info "\"migrations\" directory not found. Please clone the repo/run the below command in the application directory"
print_info "CMD \"${MIGRATION_CMD}\""
fi
print_info "${POST_INSTALL_MSG}"