-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions
331 lines (294 loc) · 8.97 KB
/
functions
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
#!/usr/bin/env bash
# These functions have been adapted from
# https://github.com/dokku/dokku/blob/master/plugins/common/functions
has_tty() {
declare desc="return 0 if we have a tty"
if [[ "$(/usr/bin/tty || true)" == "not a tty" ]]; then
return 1
else
return 0
fi
}
ee_log_quiet() {
declare desc="log quiet formatter"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
echo "$*"
fi
}
ee_log_info1() {
declare desc="log info1 formatter"
echo "-----> $*"
}
ee_log_info2() {
declare desc="log info2 formatter"
echo "=====> $*"
}
ee_log_info1_quiet() {
declare desc="log info1 formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
echo "-----> $*"
else
return 0
fi
}
ee_log_info2_quiet() {
declare desc="log info2 formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
echo "=====> $*"
else
return 0
fi
}
ee_col_log_info1() {
declare desc="columnar log info1 formatter"
printf "%-6s %-18s %-25s %-25s %-25s\n" "----->" "$@"
}
ee_col_log_info1_quiet() {
declare desc="columnar log info1 formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
printf "%-6s %-18s %-25s %-25s %-25s\n" "----->" "$@"
else
return 0
fi
}
ee_col_log_info2() {
declare desc="columnar log info2 formatter"
printf "%-6s %-18s %-25s %-25s %-25s\n" "=====>" "$@"
}
ee_col_log_info2_quiet() {
declare desc="columnar log info2 formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
printf "%-6s %-18s %-25s %-25s %-25s\n" "=====>" "$@"
else
return 0
fi
}
ee_col_log_msg() {
declare desc="columnar log formatter"
printf "%-25s %-25s %-25s %-25s\n" "$@"
}
ee_col_log_msg_quiet() {
declare desc="columnar log formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
printf "%-25s %-25s %-25s %-25s\n" "$@"
else
return 0
fi
}
ee_log_verbose_quiet() {
declare desc="log verbose formatter (with quiet option)"
if [[ -z "$EE_QUIET_OUTPUT" ]]; then
echo " $*"
else
return 0
fi
}
ee_log_verbose() {
declare desc="log verbose formatter"
echo " $*"
}
ee_log_warn() {
declare desc="log warning formatter"
echo " ! $*" 1>&2
}
ee_log_fail() {
declare desc="log fail formatter"
echo "$@" 1>&2
exit 1
}
parse_args() {
declare desc="top-level cli arg parser"
local next_index=1
local skip=false
local args=("$@")
for arg in "$@"; do
if [[ "$skip" == "true" ]]; then
next_index=$((next_index + 1))
skip=false
continue
fi
case "$arg" in
--quiet)
export EE_QUIET_OUTPUT=1
;;
--trace)
export EE_TRACE=1
;;
--dry-run)
export EE_DRY_RUN=1
;;
--all)
export EE_SITE_ALL=1
;;
--remote-host)
export REMOTE_HOST=${args[$next_index]}
skip=true
;;
esac
next_index=$((next_index + 1))
done
return 0
}
function check_ssh() {
ee_log_info1 "Checking connection to remote server."
ssh -q -i $SSH_KEY "root@$REMOTE_HOST" exit >/dev/null 2>&1 # No need to show this output
if [ $? -eq 0 ]; then
true
else
if [ ! -f "$SSH_KEY" ]; then
ssh-keygen -t rsa -b 4096 -N '' -C 'ee3_to_ee4_key' -f $SSH_KEY >/dev/null 2>&1 # No need to show this output
else
ee_log_info2 "If you have not done so already, you need to add the following"
cat "${SSH_KEY}.pub"
ee_log_info2 "to \`/root/.ssh/authorized_keys\` on the remote server"
ee_log_fail "Unable to connect to remote server. Please check if \`ssh root@$REMOTE_HOST\` is working."
false
fi
ee_log_info2 "Add the following"
cat "${SSH_KEY}.pub"
ee_log_info2 "to \`/root/.ssh/authorized_keys\` on the remote server"
false
fi
}
function run_remote_command() {
declare desc="run_remote_command COMMAND [HOST:$REMOTE_HOST] [DIR:/root] [USER:root]"
COMMAND="$1"
HOST="${2:-$REMOTE_HOST}"
DIR="${3:-/root}"
USER="${4:-root}"
ssh -i $SSH_KEY $USER@$HOST "source ${REMOTE_TMP_WORK_DIR}install-script; source ${REMOTE_TMP_WORK_DIR}helper-functions; $COMMAND"
}
function setup_docker() {
ee_log_info1 "Installing Docker"
# Check if docker exists. If not start docker installation.
if ! command -v docker >/dev/null 2>&1; then
# Running standard docker installation.
wget --quiet get.docker.com -O docker-setup.sh
sh docker-setup.sh
fi
# Check if docker-compose exists. If not start docker-compose installation.
if ! command -v docker-compose >/dev/null 2>&1; then
ee_log_info1 "Installing Docker-Compose"
curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
fi
}
function setup_php() {
ee_log_info1 "Installing PHP"
if ! command -v php >/dev/null 2>&1; then
# Checking linux distro. Currently only Ubuntu and Debian are supported.
if [ "$EE_LINUX_DISTRO" == "Ubuntu" ]; then
ee_log_info1 "Installing PHP cli"
# Adding software-properties-common for add-apt-repository.
apt-get install -y software-properties-common
# Adding ondrej/php repository for installing php, this works for all ubuntu flavours.
add-apt-repository -y ppa:ondrej/php
apt-get update && apt-get -y upgrade
# Installing php-cli, which is the minimum requirement to run EasyEngine
apt-get -y install php8.3-cli
elif [ "$EE_LINUX_DISTRO" == "Debian" ]; then
ee_log_info1 "Installing PHP cli"
# Nobody should have to change their name to enable a package installation
# https://github.com/oerdnj/deb.sury.org/issues/56#issuecomment-166077158
# That's why we're installing the locales package.
apt-get install apt-transport-https lsb-release ca-certificates locales locales-all -y
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
apt-get update
apt-get install php8.3-cli -y
fi
fi
}
function setup_php_extensions() {
ee_log_info1 "Installing PHP extensions"
# Setting up the three required php extensions for EasyEngine.
if command -v php >/dev/null 2>&1; then
php_extensions=(pcntl curl sqlite3 zip)
if ! command -v gawk >/dev/null 2>&1; then
apt install gawk -y
fi
# Reading the php version.
default_php_version="$(readlink -f /usr/bin/php | gawk -F "php" '{ print $2}')"
ee_log_info1 "Installed PHP : $default_php_version"
ee_log_info1 "Checking if required PHP modules are installed..."
packages=""
for module in "${php_extensions[@]}"; do
if ! php -m | grep $module >/dev/null 2>&1; then
ee_log_info1 "$module not installed. Installing..."
packages+="php$default_php_version-$module "
else
ee_log_info1 "$module is already installed"
fi
apt install -y $packages
done
fi
}
function create_swap() {
ee_log_info2 "Enabling 1GiB swap"
EE_SWAP_FILE="/ee-swapfile"
fallocate -l 1G $EE_SWAP_FILE && \
chmod 600 $EE_SWAP_FILE && \
chown root:root $EE_SWAP_FILE && \
mkswap $EE_SWAP_FILE && \
swapon $EE_SWAP_FILE
}
function check_swap() {
ee_log_info1 "Checking swap"
if free | awk '/^Swap:/ {exit !$2}'; then
:
else
ee_log_info1 "No swap detected"
create_swap
fi
}
function setup_host_dependencies() {
if [ "$EE_LINUX_DISTRO" == "Ubuntu" ] || [ "$EE_LINUX_DISTRO" == "Debian" ]; then
if ! command -v ip >> $LOG_FILE 2>&1; then
echo "Installing iproute2"
apt update && apt install iproute2 -y
fi
fi
}
function check_depdendencies() {
ee_log_info1 "Checking dependencies"
if ! command -v sqlite3 >/dev/null 2>&1; then
ee_log_info2 "Installing sqlite3"
apt install sqlite3 -y
fi
setup_host_dependencies
setup_docker
setup_php
setup_php_extensions
check_swap
}
function download_and_install_easyengine() {
ee_log_info1 "Downloading EasyEngine phar"
# Download EasyEngine phar.
wget -O "$EE4_BINARY" https://raw.githubusercontent.com/EasyEngine/easyengine-builds/master/phar/easyengine.phar
# Make it executable.
chmod +x "$EE4_BINARY"
}
function pull_easyengine_images() {
# Running EE migrations and pulling of images by first `ee` invocation.
ee_log_info1 "Pulling EasyEngine docker images"
"$EE4_BINARY" cli info
}
function add_ssl_renew_cron() {
# noglob is required to ensure that '*' is not expanded to list all files in current directory while printing SSL_RENEW_CRON
set -o noglob
[[ ":$PATH:" != *":/usr/local/bin:"* ]] && PATH="/usr/local/bin:${PATH}"
SSL_RENEW_CRON="PATH=$PATH\n\n0 0 * * * /usr/local/bin/ee site ssl-renew --all >> /opt/easyengine/logs/cron.log 2>&1"
# Check if cron is installed in the system.
which cron && which crontab
if [ $? -eq 0 ]
then
ee_log_info1 "Adding ssl-renew cron"
crontab -l > ee_cron
echo -e $SSL_RENEW_CRON >> ee_cron
crontab ee_cron
rm ee_cron
fi
set +o noglob
}