-
Notifications
You must be signed in to change notification settings - Fork 12
/
configure.sh
executable file
·258 lines (235 loc) · 8.33 KB
/
configure.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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
#
# Debian Server Installation Manager
#
# @author Mike Gioia <mike@particlebits.com>
# @name: configure.sh
# @about: Create the configuration files for a new install. This creates a
# new folder in the conf/ directory with all of the skeleton files
# for a new environment.
##
## Set up the base path, flags, and variables
basepath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
profile=''
## 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"
echo ""
echo -e "${yellow}Options:${NC}"
echo -e " ${green}--help -h${NC} Display this help message"
echo ""
echo -e "${yellow}Help:${NC}"
echo -e " ${green}profile${NC} is the full profile path that will be set up in the ${green}/conf${NC} directory. "
echo -e " You can specify a single hostname like ${green}dev_sql_1${NC} or a full path like ${green}dev/db/sql1${NC}. "
echo -e " This is entirely up to you in how you wish to manage profiles."
}
## 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
;;
* )
## Set the profile path
profile=$i
;;
esac
done
}
## Prompt the user to continue with the installation
function promptInstall {
echo -e "\n${blueBgWhiteBold}This script will copy configuration files to ${profile}${NC}"
read -p 'Do you want to continue [y/N]? ' wish
if ! [[ "$wish" == "y" || "$wish" == "Y" ]] ; then
exit 0
fi
}
## Create the profile directory if it doesn't exist
function createProfile {
## Check if profile is a valid string
if ! [[ -n "$profile" ]] ; then
echo -e "\n${redBgWhiteBold}Please enter a profile path or name!${NC}"
exit 1
fi
if ! [[ -d "$basepath/conf/$profile" ]] ; then
echo -e "${green}Creating new profile${NC}"
mkdir -p $basepath/conf/$profile
fi
}
## Create blank authorized keys if none exists
function copyAuthorizedKeys {
if ! [[ -f "$basepath/conf/$profile/authorized_keys" ]] ; then
echo -e " - ${green}Adding${NC} authorized_keys"
cp $basepath/src/authorized_keys $basepath/conf/$profile/authorized_keys
else
echo -e " - ${yellow}Skipping${NC} authorized_keys, file already exists"
fi
}
## Create blank hosts if none exists
function copyHosts {
if ! [[ -f "$basepath/conf/$profile/hosts" ]] ; then
echo -e " - ${green}Adding${NC} hosts file"
cp $basepath/src/hosts $basepath/conf/$profile/hosts
else
echo -e " - ${yellow}Skipping${NC} hosts file, file already exists"
fi
}
## Copy banner if none exists
function copyBanner {
if ! [[ -f "$basepath/conf/$profile/banner" ]] ; then
echo -e " - ${green}Adding${NC} banner"
cp $basepath/src/banner $basepath/conf/$profile/banner
else
echo -e " - ${yellow}Skipping${NC} banner, file already exists"
fi
}
## Create default config file if none exists
function copyConfig {
if ! [[ -f "$basepath/conf/$profile/config" ]] ; then
echo -e " - ${green}Adding${NC} default config"
cp $basepath/src/config $basepath/conf/$profile/config
else
echo -e " - ${yellow}Skipping${NC} config, file already exists"
fi
}
## Create private bash file
function copyPrivateBash {
if ! [[ -f "$basepath/conf/$profile/bash_private" ]] ; then
echo -e " - ${green}Adding${NC} bash_private"
cp $basepath/src/dotfiles/private $basepath/conf/$profile/bash_private
else
echo -e " - ${yellow}Skipping${NC} bash_private, file already exists"
fi
}
## Create firewall
function copyFirewall {
if ! [[ -f "$basepath/conf/$profile/firewall.sh" ]] ; then
echo -e " - ${green}Adding${NC} firewall.sh"
cp $basepath/src/firewall/firewall.sh $basepath/conf/$profile/firewall.sh
else
echo -e " - ${yellow}Skipping${NC} firewall.sh, file already exists"
fi
}
## Create nginx folder and default site config
function createNginx {
if ! [[ -d "$basepath/conf/$profile/nginx" ]] ; then
echo -e " - ${green}Creating${NC} nginx directory"
mkdir $basepath/conf/$profile/nginx
mkdir $basepath/conf/$profile/nginx/sites-available
else
echo -e " - ${yellow}Skipping${NC} nginx, directory already exists"
fi
}
## Set up default nginx config file
function copyNginx {
if ! [[ -f "$basepath/conf/$profile/nginx/example.conf" ]] ; then
echo -e " - ${green}Copying${NC} example nginx config"
cp $basepath/src/nginx/conf/example.conf $basepath/conf/$profile/nginx/sites-available/example.conf
cp $basepath/src/nginx/conf/conf_readme.md $basepath/conf/$profile/nginx/README.md
else
echo -e " - ${yellow}Skipping${NC} nginx example config, files already exists"
fi
}
## Copy over my.cnf
function copyMysql {
if ! [[ -f "$basepath/conf/$profile/my.cnf" ]] ; then
echo -e " - ${green}Copying${NC} MySQL my.cnf"
cp $basepath/src/mysql/my.cnf $basepath/conf/$profile/my.cnf
else
echo -e " - ${yellow}Skipping${NC} my.cnf, file already exists"
fi
}
## Copy over mongodb.conf
function copyMongodb {
if ! [[ -f "$basepath/conf/$profile/mongodb.conf" ]] ; then
echo -e " - ${green}Copying${NC} MongoDB mongodb.conf"
cp $basepath/src/mongodb/mongodb.conf $basepath/conf/$profile/mongodb.conf
else
echo -e " - ${yellow}Skipping${NC} mongodb.conf, file already exists"
fi
}
## Copy over redis_6379.conf
function copyRedis {
if ! [[ -f "$basepath/conf/$profile/redis.conf" ]] ; then
echo -e " - ${green}Copying${NC} Redis redis.conf"
cp $basepath/src/redis/redis.conf $basepath/conf/$profile/redis.conf
else
echo -e " - ${yellow}Skipping${NC} redis.conf, file already exists"
fi
}
## Copy over monitrc
function copyMonit {
if ! [[ -f "$basepath/conf/$profile/monitrc" ]] ; then
echo -e " - ${green}Copying${NC} monitrc"
cp $basepath/src/monit/monitrc $basepath/conf/$profile/monitrc
else
echo -e " - ${yellow}Skipping${NC} monitrc, file already exists"
fi
}
## Copy over sshd_config
function copySshdConfig {
if ! [[ -f "$basepath/conf/$profile/sshd_config" ]] ; then
echo -e " - ${green}Copying${NC} sshd_config"
cp $basepath/src/ssh/sshd_config $basepath/conf/$profile/sshd_config
else
echo -e " - ${yellow}Skipping${NC} sshd_config, file already exists"
fi
}
## Copy over fail2ban config
function copyJailLocal {
if ! [[ -f "$basepath/conf/$profile/jail.local" ]] ; then
echo -e " - ${green}Copying${NC} jail.local"
cp $basepath/src/fail2ban/jail.local $basepath/conf/$profile/jail.local
else
echo -e " - ${yellow}Skipping${NC} jail.local, file already exists"
fi
}
## Run the copy files scripts
function copyFiles {
echo -e "${green}Copying new configuration files${NC}"
copyAuthorizedKeys
copyHosts
copyBanner
copyConfig
copyPrivateBash
copyFirewall
createNginx
copyNginx
copyMysql
copyMongodb
copyRedis
copyMonit
copySshdConfig
copyJailLocal
}
## Finish
function finish {
echo -e "\n${greenBgWhiteBold}Done!${NC}"
echo -e "Default config files generated. Please edit, manage, or remove the files "
echo -e "in $basepath/conf/$profile/!\n"
}
getArgs $@
promptInstall
createProfile
copyFiles
finish
exit 0