/
install.sh
executable file
·244 lines (229 loc) · 6.98 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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
#
# Debian Server Installation Manager
#
# @author Mike Gioia <mike@particlebits.com>
# @name: install.sh
# @about: Install or update packages, update configuration files and dot
# files.
##
## Set up the base path, flags, and variables
basepath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
updateFlag=''
upgradeFlag=''
norootFlag=''
allFlag=''
scriptArgs=()
profile=''
config=''
## Set up config defaults
username="trunk"
nginxVersion=''
nginxDependencies=''
mongodbVersion=''
mariadbVersion=''
opensslVersion=''
redisVersion=''
redisphpVersion=''
scripts=()
## Source the colors
. $basepath/src/inc/colors
## Help message
function showHelp {
echo -e "${redBold} ____ ${NC}"
echo -e "${redBold} .mgg\$\$\$\$gg. ______ _ _ ${NC}"
echo -e "${redBold} ,g\$\" _ \`\$\$b. | _ \ | | (_) ${NC}"
echo -e "${redBold}\"\$\$ ,gs \`\$\$. | | | |___| |__ _ __ _ _ __ ${NC}"
echo -e "${redBold}\"Y\$. ,\$\" \`\$b. | | | / _ \ '_ \| |/ _\` | '_ \ ${NC}"
echo -e "${redBold}\`\"b. _\$\$,d. | |/ / __/ |_) | | (_| | | | | ${NC}"
echo -e "${redBold} \`Yb. |___/ \___|_.__/|_|\__,_|_| |_| ${NC}"
echo -e "${redBold} \`\"Y._ ${NC}"
echo -e "${redBold} \`'\"\"\" Server Installation ${NC}"
echo ""
echo -e "${yellow}Usage:${NC}"
echo " $0 [options] profile [scripts]"
echo ""
echo -e "${yellow}Options:${NC}"
echo -e " ${green}--help -h${NC} Display this help message"
echo -e " ${green}--update -u${NC} Run apt-get update"
echo -e " ${green}--upgrade -g${NC} Run apt-get upgrade"
echo ""
echo -e "${yellow}Available Scripts:${NC}"
echo -e " ${green}all ${NC} Runs all scripts specified in config"
echo -e " ${green}app ${NC} Sets up your application code"
echo -e " ${green}fail2ban ${NC} Intalls Fail2Ban and config files"
echo -e " ${green}firewall ${NC} Copies firewall script and loads on boot"
echo -e " ${green}mariadb ${NC} Installs MariaDB v10.0"
echo -e " ${green}mongodb ${NC} Compiles and installs MongoDB from source"
echo -e " ${green}monit ${NC} Installs Monit via apt"
echo -e " ${green}mysql ${NC} Installs MySQL via apt"
echo -e " ${green}nginx ${NC} Compiles and installs nginx from source"
echo -e " ${green}openssl ${NC} Compiles and installs OpenSSL from source"
echo -e " ${green}php ${NC} Installs PHP from the DotDeb repository"
echo -e " ${green}profile ${NC} Sets up your bash profile"
echo -e " ${green}redis ${NC} Compiles and installs Redis from source"
echo -e " ${green}user ${NC} Creates shell account and configures environment"
echo -e " ${green}xtrabackup ${NC} Installs Percona XtraBackup via apt"
echo ""
echo -e "Default command is ${green}all${NC} if none is specified."
}
## Read the remaining arguments from the CLI
function getArgs {
## Loop through command parameters
for i
do
case $i in
-\? | -h | --help | help )
showHelp
exit 0
;;
-u | --update )
updateFlag=1
;;
-g | --upgrade )
upgradeFlag=1
;;
--noroot )
norootFlag=1
;;
all )
## Run all scripts
allFlag=1
;;
app | couchdb | fail2ban | firewall | mariadb | mongodb | monit | mysql )
## Add to scripts array
scriptArgs+=$i
;;
nginx | openssl | php | php7 | profile | redis | ssh | user | xtrabackup )
## Add to scripts array
scriptArgs+=$i
;;
* )
## Set the profile path
profile=$i
;;
esac
done
}
## Check if logged in user is root
function checkRoot {
if [[ "$norootFlag" ]] ; then
return
fi
if ! [[ $(id -u) -eq 0 ]] ; then
echo -e "\n${redBgWhiteBold}You are not the root user!${NC}"
exit 1
fi
}
## Check if the profile path is to a valid profile
function checkProfile {
config="$basepath/conf/$profile/config"
if ! [[ -f "$config" ]] ; then
echo -e "\n${redBgWhiteBold}Could not find the profile you entered: ${profile}${NC}"
echo -e -n "Make sure to run ./configure.sh <profile> in the deploy directory "
echo -e "or ./configure --help for more info.${NC}\n"
exit 1
fi
}
## Prompt the user to continue with the installation
function promptInstall {
echo -e "\n${blueBgWhiteBold}This script will update software and configuration files on your server.${NC}"
read -p 'Do you want to continue [y/N]? ' wish
if ! [[ "$wish" == "y" || "$wish" == "Y" ]] ; then
exit 0
fi
}
## Read in the config variables and export vars
function readConfig {
. $config
export basepath
export profile
export username
export nginxVersion
export nginxDependencies
export mongodbVersion
export mariadbVersion
export opensslVersion
export redisVersion
export redisphpVersion
export sites
}
## Update the system if flag set
function update {
if [[ "$updateFlag" ]] ; then
apt-get update
fi
}
## Upgrade the system if flag set
function upgrade {
if [[ "$upgradeFlag" ]] ; then
apt-get upgrade --show-upgraded
fi
}
## Export colors
function exportColors {
export black
export red
export green
export yellow
export blue
export magenta
export cyan
export white
export redBgWhite
export redBgWhiteBold
export blueBgWhite
export blueBgWhiteBold
export greenBgWhite
export greenBgWhiteBold
export blackBold
export redBold
export greenBold
export yellowBold
export blueBold
export magentaBold
export cyanBold
export whiteBold
export NC
}
## Run the scripts
function runScripts {
if ! [[ "${#scriptArgs[@]}" -eq 0 ]] ; then
for script in "${scriptArgs[@]}"
do
if [[ -f "$basepath/conf/$profile/scripts/$script.sh" ]] ; then
$basepath/conf/$profile/scripts/$script.sh
else
$basepath/scripts/$script.sh
fi
done
else
for script in "${scripts[@]}"
do
if [[ -f "$basepath/conf/$profile/scripts/$script.sh" ]] ; then
$basepath/conf/$profile/scripts/$script.sh
else
$basepath/scripts/$script.sh
fi
done
fi
}
## Finish
function finish {
echo -e "\n${greenBgWhiteBold}Done!${NC}"
echo -e "Make sure to restart your server for all changes to take effect!${NC}\n"
}
## Run the program
getArgs $@
checkRoot
checkProfile
promptInstall
readConfig
update
upgrade
exportColors
runScripts
finish
exit 0