forked from ArkEcosystem/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
285 lines (224 loc) · 8.17 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
#!/usr/bin/env bash
set -e
# Typography
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
lila=$(tput setaf 4)
pink=$(tput setaf 5)
blue=$(tput setaf 6)
white=$(tput setaf 7)
black=$(tput setaf 8)
bold=$(tput bold)
reset=$(tput sgr0)
heading ()
{
echo " ${lila}==>${reset}${bold} $1${reset}"
}
success ()
{
echo " ${green}==>${reset}${bold} $1${reset}"
}
info ()
{
echo " ${blue}==>${reset}${bold} $1${reset}"
}
warning ()
{
echo " ${yellow}==>${reset}${bold} $1${reset}"
}
error ()
{
echo " ${red}==>${reset}${bold} $1${reset}"
}
# Detect pkg type
DEB=$(which apt-get || :)
RPM=$(which yum || :)
# Detect SystemV / SystemD
SYS=$([[ -L "/sbin/init" ]] && echo 'SystemD' || echo 'SystemV')
# Detect Debian/Ubuntu derivative
DEB_ID=$( (grep DISTRIB_CODENAME /etc/upstream-release/lsb-release || grep DISTRIB_CODENAME /etc/lsb-release) 2>/dev/null | cut -d'=' -f2 )
if [[ ! -z $DEB ]]; then
success "Running install for Debian derivative"
elif [[ ! -z $RPM ]]; then
success "Running install for RedHat derivative"
else
heading "Unsupported system"
exit 1;
fi
if [[ $(locale -a | grep ^en_US.UTF-8) ]] || [[ $(locale -a | grep ^en_US.utf8) ]]; then
if ! $(grep -E "(en_US.UTF-8)" "$HOME/.bashrc"); then
# Setting the bashrc locale
echo "export LC_ALL=en_US.UTF-8" >> "$HOME/.bashrc"
echo "export LANG=en_US.UTF-8" >> "$HOME/.bashrc"
echo "export LANGUAGE=en_US.UTF-8" >> "$HOME/.bashrc"
# Setting the current shell locale
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
fi
else
# Install en_US.UTF-8 Locale
if [[ ! -z $DEB ]]; then
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8
elif [[ ! -z $RPM ]]; then
sudo localedef -c -i en_US -f UTF-8 en_US.UTF-8
fi
# Setting the current shell locale
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
# Setting the bashrc locale
echo "export LC_ALL=en_US.UTF-8" >> "$HOME/.bashrc"
echo "export LANG=en_US.UTF-8" >> "$HOME/.bashrc"
echo "export LANGUAGE=en_US.UTF-8" >> "$HOME/.bashrc"
fi
heading "Installing system dependencies..."
if [[ ! -z $DEB ]]; then
sudo apt-get update
sudo apt-get install -y git curl apt-transport-https update-notifier
elif [[ ! -z $RPM ]]; then
sudo yum update -y
sudo yum install git curl epel-release -y
fi
success "Installed system dependencies!"
heading "Installing node.js & npm..."
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
sudo rm -rf ~/{.npm,.forever,.node*,.cache,.nvm}
if [[ ! -z $DEB ]]; then
sudo wget --quiet -O - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
(echo "deb https://deb.nodesource.com/node_12.x ${DEB_ID} main" | sudo tee /etc/apt/sources.list.d/nodesource.list)
sudo apt-get update
sudo apt-get install nodejs -y
elif [[ ! -z $RPM ]]; then
sudo yum install gcc-c++ make -y
curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash - > /dev/null 2>&1
fi
success "Installed node.js & npm!"
heading "Installing Yarn..."
if [[ ! -z $DEB ]]; then
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
(echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list)
sudo apt-get update
sudo apt-get install -y yarn
elif [[ ! -z $RPM ]]; then
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn -y
fi
success "Installed Yarn!"
heading "Installing PM2..."
sudo yarn global add pm2
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 500M
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:retain 7
success "Installed PM2!"
heading "Installing program dependencies..."
if [[ ! -z $DEB ]]; then
sudo apt-get install build-essential libcairo2-dev pkg-config libtool autoconf automake python libpq-dev jq libjemalloc-dev -y
elif [[ ! -z $RPM ]]; then
sudo yum groupinstall "Development Tools" -y -q
sudo yum install postgresql-devel jq jemalloc-devel -y -q
fi
success "Installed program dependencies!"
heading "Installing PostgreSQL..."
if [[ ! -z $DEB ]]; then
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib -y
elif [[ ! -z $RPM ]]; then
sudo yum install postgresql-server postgresql-contrib -y
if [[ "$SYS" == "SystemV" ]]; then
sudo service postgresql initdb
sudo service postgresql start
else
sudo postgresql-setup initdb
sudo systemctl start postgresql
fi
fi
success "Installed PostgreSQL!"
heading "Installing NTP..."
sudo timedatectl set-ntp off > /dev/null 2>&1 || true # disable the default systemd timesyncd service
if [[ ! -z $DEB ]]; then
sudo apt-get install ntp -yyq
elif [[ ! -z $RPM ]]; then
sudo yum install ntp -y -q
fi
if [ -z "$(sudo service ntp status |grep running)" ] ; then
sudo ntpd -gq
fi
success "Installed NTP!"
heading "Installing system updates..."
if [[ ! -z $DEB ]]; then
sudo apt-get update
sudo apt-get upgrade -yqq
sudo apt-get dist-upgrade -yq
sudo apt-get autoremove -yyq
sudo apt-get autoclean -yq
elif [[ ! -z $RPM ]]; then
sudo yum update
sudo yum clean
fi
success "Installed system updates!"
heading "Installing ARK Core..."
while ! yarn global add @arkecosystem/core ; do
read -p "Installing ARK Core failed, do you want to retry? [y/N]: " choice
if [[ ! "$choice" =~ ^(yes|y|Y) ]] ; then
exit 1
fi
done
echo 'export PATH=$(yarn global bin):$PATH' >> ~/.bashrc
export PATH=$(yarn global bin):$PATH
ark config:publish
success "Installed ARK Core!"
readNonempty() {
prompt=${1}
answer=""
while [ -z "${answer}" ] ; do
read -p "${prompt}" answer
done
echo "${answer}"
}
# setup postgres username, password and database
read -p "Would you like to configure the database? [y/N]: " choice
if [[ "$choice" =~ ^(yes|y|Y) ]]; then
choice=""
while [[ ! "$choice" =~ ^(yes|y|Y) ]] ; do
databaseUsername=$(readNonempty "Enter the database username: ")
databasePassword=$(readNonempty "Enter the database password: ")
databaseName=$(readNonempty "Enter the database name: ")
echo "database username: ${databaseUsername}"
echo "database password: ${databasePassword}"
echo "database name: ${databaseName}"
read -p "Proceed? [y/N]: " choice
done
ark env:set CORE_DB_USERNAME "${databaseUsername}"
ark env:set CORE_DB_PASSWORD "${databasePassword}"
ark env:set CORE_DB_DATABASE "${databaseName}"
userExists=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_user WHERE usename = '${databaseUsername}'")
databaseExists=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname = '${databaseName}'")
if [[ $userExists == 1 ]]; then
read -p "The database user ${databaseUsername} already exists, do you want to recreate it? [y/N]: " choice
if [[ "$choice" =~ ^(yes|y|Y) ]]; then
if [[ $databaseExists == 1 ]]; then
sudo -i -u postgres psql -c "ALTER DATABASE ${databaseName} OWNER TO postgres;"
fi
sudo -i -u postgres psql -c "DROP USER ${databaseUsername}"
sudo -i -u postgres psql -c "CREATE USER ${databaseUsername} WITH PASSWORD '${databasePassword}' CREATEDB;"
fi
else
sudo -i -u postgres psql -c "CREATE USER ${databaseUsername} WITH PASSWORD '${databasePassword}' CREATEDB;"
fi
if [[ $databaseExists == 1 ]]; then
read -p "The database ${databaseName} already exists, do you want to overwrite it? [y/N]: " choice
if [[ "$choice" =~ ^(yes|y|Y) ]]; then
sudo -i -u postgres psql -c "DROP DATABASE ${databaseName};"
sudo -i -u postgres psql -c "CREATE DATABASE ${databaseName} WITH OWNER ${databaseUsername};"
elif [[ "$choice" =~ ^(no|n|N) ]]; then
sudo -i -u postgres psql -c "ALTER DATABASE ${databaseName} OWNER TO ${databaseUsername};"
fi
else
sudo -i -u postgres psql -c "CREATE DATABASE ${databaseName} WITH OWNER ${databaseUsername};"
fi
fi
exec "$BASH"