-
Notifications
You must be signed in to change notification settings - Fork 11
/
bootstrap.sh
208 lines (176 loc) · 6.24 KB
/
bootstrap.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
#!/usr/bin/env bash
# programming env: these switches turn some bugs into errors
# set -o errexit -o pipefail -o noclobber -o nounset
# Version: 2.2
# to be run as root, probably as a user-script just after a server is installed
# as root
# if [[ $USER != "root" ]]; then
# echo "This script must be run as root"
# exit 1
# fi
# ref: https://packages.sury.org/php/README.txt
# if [ "$(whoami)" != "root" ]; then
# SUDO=sudo
# else
# SUDO=
# fi
# https://stackoverflow.com/a/52586842/1004587
# also see https://stackoverflow.com/q/3522341/1004587
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
[ is_user_root ] || { echo 'You must be root or user with sudo privilege to run this script. Exiting now.'; exit 1; }
# create some useful directories - create them on demand
mkdir -p ${HOME}/{backups,git,log,scripts,tmp} &> /dev/null
# logging everything
log_file=${HOME}/log/wp-in-a-box.log
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)
echo "Script started on (date & time): $(date +%c)"
# helper function to exit upon non-zero exit code of a command
# usage some_command; check_result $? 'some_command failed'
if ! $(type 'check_result' 2>/dev/null | grep -q 'function') ; then
check_result() {
if [ "$1" -ne 0 ]; then
echo -e "\nError: $2. Exiting!\n"
exit "$1"
fi
}
fi
if ! $(type 'codename' 2>/dev/null | grep -q 'function')
then
codename() {
lsb_release_cli=$(which lsb_release)
local codename=""
if [ ! -z $lsb_release_cli ]; then
codename=$($lsb_release_cli -cs)
else
codename=$(cat /etc/os-release | awk -F = '/VERSION_CODENAME/{print $2}')
fi
echo "$codename"
}
codename=$(codename)
fi
[ ! -f "$HOME/.envrc" ] && touch ~/.envrc
EMAIL=root@localhost
if ! grep -qw $EMAIL /root/.envrc ; then
echo "export EMAIL=$EMAIL" >> /root/.envrc
fi
NAME='root'
if ! grep -qw "$NAME" /root/.envrc ; then
echo "export NAME='$NAME'" >> /root/.envrc
fi
local_wp_in_a_box_repo=/root/git/wp-in-a-box
. /root/.envrc
# take a backup
backup_dir="/root/backups/etc-before-wp-in-a-box-$(date +%F)"
if [ ! -d "$backup_dir" ]; then
printf '%-72s' "Taking initial backup..."
mkdir $backup_dir
cp -a /etc $backup_dir
echo done.
fi
# Ref: https://wiki.debian.org/Multiarch/HOWTO
# https://askubuntu.com/a/1336013/65814
[ ! $(dpkg --get-selections | grep -q i386) ] && dpkg --remove-architecture i386 2>/dev/null
# Fix apt ipv4/6 issue
echo 'Acquire::ForceIPv4 "true";' > /etc/apt/apt.conf.d/1000-force-ipv4-transport
export DEBIAN_FRONTEND=noninteractive
# the following runs when apt cache is older than an hour
printf '%-72s' "Updating apt repos..."
[ -z "$(find /var/cache/apt/pkgcache.bin -mmin -60 2> /dev/null)" ] && apt-get -qq update
echo done.
# Redirection explanation: https://unix.stackexchange.com/a/563563/20241
printf '%-72s' "Installing git..."
if ! dpkg-query -W -f='${Status}' git 2>/dev/null | grep -q "ok installed"; then apt-get -qq install git 1>/dev/null ; fi
echo done.
git config --global --replace-all user.email "$EMAIL"
git config --global --replace-all user.name "$NAME"
printf '%-72s' "Fetching wp-in-a-box repo..."
if [ ! -d $local_wp_in_a_box_repo ] ; then
git clone -q --recursive https://github.com/pothi/wp-in-a-box $local_wp_in_a_box_repo &> /dev/null
else
git -C $local_wp_in_a_box_repo pull -q origin master &> /dev/null
git -C $local_wp_in_a_box_repo pull -q --recurse-submodules &> /dev/null
fi
echo done.
echo
# pre-install steps
case "$codename" in
"focal")
. $local_wp_in_a_box_repo/scripts/pre-install-focal.sh
;;
"bionic")
# . $local_wp_in_a_box_repo/scripts/pre-install-bionic.sh
;;
"stretch")
# . $local_wp_in_a_box_repo/scripts/pre-install-stretch.sh
;;
"xenial")
# . $local_wp_in_a_box_repo/scripts/pre-install-xenial.sh
;;
"buster")
;;
*)
echo "Distro: $codename"
echo 'Warning: Could not figure out the distribution codename. Skipping pre-install steps!'
;;
esac
. $local_wp_in_a_box_repo/scripts/swap.sh
echo
. $local_wp_in_a_box_repo/scripts/base-installation.sh
echo
. $local_wp_in_a_box_repo/scripts/linux-tweaks.sh
echo
. $local_wp_in_a_box_repo/scripts/nginx-installation.sh
echo
. $local_wp_in_a_box_repo/scripts/mysql-installation.sh
echo
. $local_wp_in_a_box_repo/scripts/wp-user-creation.sh
echo
. $local_wp_in_a_box_repo/scripts/php-installation.sh
echo
. $local_wp_in_a_box_repo/scripts/server-admin-creation.sh
echo
# the following can be executed at any order as they are mostly optional
# . $local_wp_in_a_box_repo/scripts/firewall.sh
echo
# optional software, utilities and packages
# . $local_wp_in_a_box_repo/scripts/optional-installation.sh
# post-install steps
case "$codename" in
"focal")
. $local_wp_in_a_box_repo/scripts/post-install-focal.sh
;;
"bionic")
. $local_wp_in_a_box_repo/scripts/post-install-bionic.sh
;;
"stretch")
. $local_wp_in_a_box_repo/scripts/post-install-stretch.sh
;;
"xenial")
. $local_wp_in_a_box_repo/scripts/post-install-xenial.sh
;;
"buster")
. $local_wp_in_a_box_repo/scripts/post-install-buster.sh
;;
*)
echo "Distro: $codename"
echo 'Warning: Could not figure out the distribution codename. Skipping post-install steps!'
;;
esac
# configure some defaults for git and etckeeper
git config --global user.name "root"
git config --global user.email "root@localhost"
git config --global init.defaultBranch main
printf '%-72s' "Installing etckeeper..."
if ! dpkg-query -W -f='${Status}' etckeeper 2> /dev/null | grep -q "ok installed"; then apt-get -qq install etckeeper &> /dev/null; fi
sed -i 's/^GIT_COMMIT_OPTIONS=""$/GIT_COMMIT_OPTIONS="--quiet"/' /etc/etckeeper/etckeeper.conf
echo done.
# logout and then login to see the changes
echo All done.
echo -------------------------------------------------------------------------
echo You may find the login credentials of SFTP/SSH user in /root/.envrc file.
echo -------------------------------------------------------------------------
echo 'You may reboot only once to apply certain updates (ex: kernel updates)!'
echo
echo "Script ended on (date & time): $(date +%c)"
echo