forked from MPLew-is/apache-config
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apache-config.sh
executable file
·569 lines (429 loc) · 16 KB
/
apache-config.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/bin/sh
#
#Allows for script-based management of Apache modules and other configuration files
#Inspired by "a2enmod" and co. on Debian distributions
#
#Example usage:
# apache-config.sh enable module rewrite
# apache-config.sh install
#Exit immediately if any of the commands in this script fail
set -e
#Check for existence of Apache in the current PATH
if which httpd 1>/dev/null 2>&1
then
#If present, parse the config options to get the path to the config directory
configRoot="$(httpd -V | grep "SERVER_CONFIG_FILE" | awk '{print $2}' | sed -e 's#^SERVER_CONFIG_FILE="##' -e 's#/httpd.conf"$##')"
else
#If not present, show an error message and exit with failure
cat 1>&2 <<-EOF
This script requires Apache ('httpd') to be present in the current PATH
Please install Apache, or link it so it can be found in PATH
EOF
exit 1
fi
#Set a bunch of variables to keep from repeating values, for easy changing if needed
version="0_1"
environmentVariable="VENDOR_MPLEWIS_CONFIG_CONTROLLER"
httpdFile="${configRoot}/httpd.conf"
configPath="other"
configDirectory="${configRoot}/${configPath}"
fileSuffix="conf"
controllerFileName="config-controller.${fileSuffix}"
availablePath="available"
enabledPath="enabled"
module="module"
config="config"
site="site"
cleanup="cleanup"
#Get requested loudness of script
quiet=false
if [ "${1}" = "--quiet" ]
then
quiet=true
shift
fi
#Print a message showing the usage options
printUsageMessage()
{
cat <<-EOF
Check installation: ${0} [--quiet] check
Install: ${0} [--quiet] install
List configuration: ${0} list (${module} | ${config} | ${site} | ${cleanup}) (enabled | available | disabled)
Enable/disable: ${0} [--quiet] (enable | disable) (${module} | ${config} | ${site} | ${cleanup}) {NAME/PATH} [{NAME/PATH}...]
Edit: ${0} [--quiet] edit (${module} | ${config} | ${site} | ${cleanup}) {NAME/PATH}
Help: ${0} help [--verbose]
EOF
}
#Print a message saying Apache needs to be restarted
printApacheRestart()
{
echoStatus "${1} complete. You will need to restart Apache for the changes to take effect."
}
#Print status messages (with no trailing line break) only if output is not silenced
printStatus()
{
if [ "${quiet}" != "true" ]
then
printf "%s" "${1}"
fi
}
#Print status messages (with one trailing line break) only if output is not silenced
echoStatus()
{
if [ "${quiet}" != "true" ]
then
echo "${1}"
fi
}
#Print a help message when requested
command_help()
{
cat <<-EOF
apache-config: allows for script-based management of Apache modules and other configuration files
inspired by "a2enmod" and co. on Debian distributions
EOF
printUsageMessage
cat <<-EOF
All paths listed below are relative to the following path:
${configDirectory}
Example usage:
${0} enable ${module} rewrite
Arguments:
"--quiet": silence all messages about the script's status
Commands:
"help": print this help message
"check": check if the "install" command has been performed
"install": creates the needed files, directories, and configuration directives needed for this script
"list": lists all configuration files of the given type and status (enabled, available, or disabled)
"enable": enables the specified config file type and name
"disable": disables the specified config file type and name
"edit": use the editor specified in VISUAL or EDITOR (falling back to vim or vi if not set) to edit the specified config type and name
Supported types:
"${module}": files for loading and configuring an Apache module
stored in the "${module}s-${availablePath}" directory
"${config}": generic Apache configuration files
stored in the "${config}s-${availablePath}" directory
"${site}": files for configuring a specific host or site
stored in the "${site}s-${availablePath}" directory
"${cleanup}": files for cleaning up after all other files have been included
stored in the "${cleanup}s-${availablePath}" directory
EOF
if [ "${#}" = "0" ]
then
cat <<-EOF
Run "${0} --help --verbose" for more detailed information.
EOF
elif [ "${#}" = "1" ] && [ "${1}" = "--verbose" ]
then
cat <<-EOF
The various types of configuration files will be loaded in the following order:
${module}s, ${config}s, ${site}s
"${module}s" are intended to be Apache module loading and configuration directives, if the defaults must be changed
"${config}s" are intended to be server-wide directives that should be set before any hosts or sites are loaded
"${site}s" are intended to be configuration for hosts, virtual or otherwise
"${cleanup}s" are intended to cleaning up after all other files have been included (such as unsetting macro definitions)
Modules should be stored in (or symlinked into) "${module}s-${availablePath}"
Configs should be stored in (or symlinked into) "${config}s-${availablePath}"
Sites should be stored in (or symlinked into) "${site}s-${availablePath}"
Cleanups should be stored in (or symlinked into) "${cleanup}s-${availablePath}"
If you run an "enable" command with a literal filename, that file will be copied into the appropriate "available" directory and then enabled
When you do this, the last file extension will be replaced with ".${fileSuffix}"
When a command is run for a specific configuration file, the given name is used to search for the specific file to use by appending ".${fileSuffix}"
All enabled configuration will be symlinked into the corresponding "${enabledPath}" directory; for example, when running:
${0} enable ${module} rewrite
the file "${module}s-${availablePath}/rewrite.conf" will be symlinked into "${module}s-${enabledPath}/rewrite.conf"
When disabling a configuration file, the link in "${enabledPath}" will be removed, but the original file will remain in the "${availablePath}" directory
You should never modify the "*-${enabledPath}" directories directly, as anything in that directory can be deleted at any time
EOF
fi
return 0
}
#Install the necessary configuration blocks, files, and directories for the script to run
command_install()
{
#Set variable for the file being output to
controllerFile="${configDirectory}/${controllerFileName}"
#Create the config directory if it doesn't exist
printStatus "Creating directory '${configDirectory}'... "
mkdir -p "${configDirectory}"
echoStatus "done"
#Add a version environment variable to prevent multiple versions from conflicting
#This directive will be replaced every time an installation is performed
printStatus "Adding/updating version environment variable in '${httpdFile}'... "
defineDirective="Define ${environmentVariable}"
if ! checkEnvironmentVariable
then
cat <<-EOF >> "${httpdFile}"
# Do **NOT** delete this line; it is managed by apache-config and will be updated along with the script
${defineDirective}_${version} 'true'
EOF
echoStatus "added"
else
sed -i '' -e "s/${defineDirective}_[0-9]*_[0-9]*/${defineDirective}_${version}/" "${httpdFile}"
echoStatus "updated"
fi
#Add directive that includes the controller conf file to the main httpd.conf
#There doesn't seem to be a better way to do this on a default config file, so an environment variable was added to prevent multiple blocks from executing simultaneously
printStatus "Adding include statement to '${httpdFile}'... "
if ! checkControllerBlock
then
cat <<-EOF >> "${httpdFile}"
<IfDefine ${environmentVariable}_${version}>
<IfDefine !${environmentVariable}>
Define ${environmentVariable} '${version}'
Include ${controllerFile}
</IfDefine>
</IfDefine>
EOF
echoStatus "done"
else
echoStatus "already present"
fi
#Echo a top comment to the controller conf file warning users against editing this file
echo "# Do **NOT** modify this file. It is managed by apache-config, and may be overwritten at any point" > "${controllerFile}"
#Iterate through the config types, create the necessary directories, and include those directories in the controller file
printStatus "Creating and installing configuration directories (if not already present)... "
cat <<-EOF |
${module}
${config}
${site}
${cleanup}
EOF
{
while read -r configType
do
configTypeDirectory="${configDirectory}/${configType}s"
mkdir -p "${configTypeDirectory}-${availablePath}"
mkdir -p "${configTypeDirectory}-${enabledPath}"
echo "IncludeOptional ${configTypeDirectory}-${enabledPath}/*.${fileSuffix}" >> "${controllerFile}"
done
}
echoStatus "done"
#Print Apache restart message
printApacheRestart "Installation"
return 0
}
#Validate the input enable/disable type, exiting on failure
validateType()
{
#Validate the config type against the prefedined types
configType="${1}"
if [ "${1}" != "${module}" ] && [ "${1}" != "${config}" ] && [ "${1}" != "${site}" ] && [ "${1}" != "${cleanup}" ]
then
echo "Unrecognized configuration type '${1}'" 1>&2
printUsageMessage 1>&2
return 3
fi
echo "${configType}"
return 0
}
#Validate that configuration names are provided, or exit if not
validateNames()
{
if [ "${#}" -lt "1" ]
then
echo "No ${module}/${config}/${site}/${cleanup} names were provided" 1>&2
printUsageMessage 1>&2
return 4
fi
return 0
}
#Enable configuration files of the given type and names
command_enable()
{
#Check installation before performing enable command
command_check 1>/dev/null
#Get the configuration type, then shift it off the arguments list to allow for iterating
configType="$(validateType "${1}")"
shift
#Validate that at least one configuration name was given
validateNames "${@}"
#Set a variable storing the path prefix to use, so that "enabled" or "available" can be easily appended
directoryPrefix="${configDirectory}/${configType}s"
#Multiple configuration names can be provided, so shift through each argument and process each
while [ "${#}" -gt "0" ]
do
#Set initial variables
configName="${1}"
fileName="${configName}.${fileSuffix}"
availableDirectory="${directoryPrefix}-${availablePath}"
availableFile="${availableDirectory}/${fileName}"
#Check if the file is actually available, fail if not
if [ ! -f "${availableFile}" ]
then
#If the given input is a file name that exists, copy it into the available directory and enable it from there
if [ -f "${configName}" ]
then
#Get just the file name, and convert any existing file extension to the managed file suffix
fileName="$(basename "${1}")"
configName="${fileName%.*}"
fileName="${configName}.${fileSuffix}"
#Copy the file into the appropriate available directory
printStatus "Copying file '${1}' into '${configType}s-${availablePath}'... "
if cp "${1}" "${availableDirectory}/${fileName}"
then
echoStatus "done"
else
echoStatus "ERROR"
return 33
fi
else
echo "File '${availableFile}' does not exist" 1>&2
return 31
fi
fi
#Get the path in which to store the symlink
enabledFile="${directoryPrefix}-${enabledPath}/${fileName}"
#Check if the file is already enabled, fail if so
if [ -e "${enabledFile}" ]
then
echo "File '${enabledFile}' is already enabled (symlinked into the '${configType}-${enabledPath}' directory)" 1>&2
return 32
fi
#Symlink the config file from the available directory into the enabled directory
printStatus "Enabling ${configType} '${configName}'... "
if ln -s "../${configType}s-${availablePath}/${fileName}" "${enabledFile}"
then
echoStatus "done"
else
echoStatus "ERROR"
fi
shift
done
#Print Apache restart message
printApacheRestart "Enabling"
}
command_disable()
{
#Check installation before performing disable command
command_check 1>/dev/null
#Get the configuration type, then shift it off the arguments list to allow for iterating
configType="$(validateType "${1}")"
shift
#Validate that at least one configuration name was given
validateNames "${@}"
#Set a variable storing the path prefix to use, so that "enabled" or "available" can be easily appended
directoryPrefix="${configDirectory}/${configType}s"
#Multiple config names can be provided, so shift through each argument and process each
while [ "${#}" -gt "0" ]
do
#Set variables for the file to be unlinked
fileName="${1}.${fileSuffix}"
file="${directoryPrefix}-${enabledPath}/${fileName}"
#Check to see if the symlink actually exists in the enabled directory, fail if not
if [ ! -L "${file}" ]
then
echo "File '${file}' does not exist" 1>&2
return 41
fi
#Remove the symlink file pointing at the available directory
printStatus "Disabling ${configType} '${1}'... "
if rm "${directoryPrefix}-${enabledPath}/${fileName}"
then
echoStatus "done"
else
echoStatus "ERROR"
fi
shift
done
#Print Apache restart message
printApacheRestart "Disabling"
}
#Check for the presence of the environment variable definition in httpd.conf
checkEnvironmentVariable()
{
if ! grep --quiet "reqenv('${environmentVariable}_${version}') == 'true'" "${httpdFile}"
then
return 5
fi
}
#Check for the presence of the controller block in httpd.conf
checkControllerBlock()
{
if ! grep --quiet "reqenv('${environmentVariable}_${version}') == 'true'" "${httpdFile}"
then
return 6
fi
}
#Check if the "install" command has been run for this version, failing if not
command_check()
{
#Fail if either of the check functions fail
if checkEnvironmentVariable && checkControllerBlock
then
echoStatus "apache-config not installed"
echoStatus "Please run '${0} install' to install apache-config"
return 51
fi
echoStatus "apache-config is installed"
return 0
}
#List all configuration files of the given status and type
command_list()
{
#Check installation before performing list command
command_check 1>/dev/null
#Get the configuration type, then shift it off the arguments
configType="$(validateType "${1}")"
shift
#Get the requested status, and shift it off the arguments
status="${1}"
shift
#Validate input status, failing if an incorrect status is given
if [ "${status}" != "enabled" ] && [ "${status}" != "available" ] && [ "${status}" != "disabled" ]
then
echoStatus "Unknown status '${status}'" 1>&2
return 61
fi
statusPath="${status}"
if [ "${status}" = "disabled" ]
then
statusPath="available"
fi
directoryPrefix="${configDirectory}/${configType}s"
directory="${configDirectory}/${configType}s-${statusPath}"
find "${directory}" -name "*.${fileSuffix}" | sed -e "s:^${directory}/::g" -e "s:.${fileSuffix}$::g" | \
{
if [ "${status}" = "disabled" ]
then
while read -r fileName
do
if [ ! -L "${directoryPrefix}-enabled/${fileName}.${fileSuffix}" ]
then
echo "${fileName}"
fi
done
else
cat
fi
}
}
command_edit()
{
#Get the configuration type, then shift it off the arguments
configType="$(validateType "${1}")"
name="${2}"
#Get the user's preferred editor from environment variables, ultimately falling back to vim if nothing is defined
editor="${VISUAL:-${EDITOR:-vim}}"
#If the editor fell back to vim, and vim isn't present in PATH, fall back to vi, which is guaranteed to be present by POSIX
if [ "${editor}" = "vim" ] && ! which vim 1>/dev/null
then
editor="vi"
fi
#Use the editor to edit the config file of the input type and name
"${editor}" "${configDirectory}/${configType}s-${availablePath}/${name}.${fileSuffix}"
printApacheRestart "Editing"
}
#Parse requested command, and call the corresponding function with the remaining arguments
if [ "${1}" = "help" ] || [ "${1}" = "install" ] || [ "${1}" = "enable" ] || [ "${1}" = "disable" ] || [ "${1}" = "check" ] || [ "${1}" = "list" ] || [ "${1}" = "edit" ]
then
commandFunction="command_${1}"
shift
"${commandFunction}" "${@}"
#Otherwise, fail with error
else
echo "Unrecognized command '${1}'" 1>&2
printUsageMessage 1>&2
exit 2
fi
exit 0