-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jekyll_wwwconf.sh
188 lines (151 loc) · 6.01 KB
/
jekyll_wwwconf.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
#!/usr/bin/env bash
## Exit if not running with root/level permissions
if [[ "${EUID}" != '0' ]]; then echo "Try: sudo ${0##*/} ${@:---help}"; exit 1; fi
_NGINX_CONF_DIR="${_NGINX_CONF_DIR:-/etc/nginx}"
#
# Set defaults for script variables; these maybe overwritten at run-time
#
_user=''
_group='devs'
_server='nginx'
_repo="${_user}"
_tld='lan'
_interface="$(ls -1 /sys/class/net/ | grep -v 'lo' | head -1)"
_clobber='no'
#
# Set script variables that should not be modified
#
_LESS_OPTS='--quit-at-eof --quit-if-one-screen --chop-long-lines --RAW-CONTROL-CHARS --LONG-PROMPT'
## Find true directory script resides in, true name, and true path
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
__NAME__="${__SOURCE__##*/}"
__PATH__="${__DIR__}/${__NAME__}"
__AUTHOR__='S0AndS0'
__DESCRIPTION__='Writes Web Server configurations for Jekyll and/or Git client projects'
#
# Source useful functions
#
## Provides: 'failure'
source "${__DIR__}/shared_functions/modules/trap-failure/failure.sh"
trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR
## Provides: 'argument_parser <ref_to_allowed_args> <ref_to_user_supplied_args>'
source "${__DIR__}/shared_functions/modules/argument-parser/argument-parser.sh"
## Provides: nginx_rewrite_config <user> <group> tld interface clobber
source "${__DIR__}/shared_functions/web_servers/nginx/nginx_rewrite_config.sh"
## Provides: nginx_write_config <user>:group <repo> tld interface clobber
source "${__DIR__}/shared_functions/web_servers/nginx/nginx_write_config.sh"
## Provides: nginx_remove_config <user>:group <repo> tld clobber
source "${__DIR__}/shared_functions/web_servers/nginx/nginx_remove_config.sh"
## Provides: __license__ <description> <author>
source "${__DIR__}/shared_functions/license.sh"
usage(){
local -n _parsed_argument_list="${1}"
cat <<EOF
${__DESCRIPTION__}
# Options
-u --user=${_user}
Required
Name of user to look under their home for '~/www' directory, for Jekyll
built static files built from ${_repo} for serving via ${_server}
-r --repo=${_repo}
Name of repository to build configs for if '--clobber' is unset
or set to 'yes' or 'append'
-g -d --domain --group=${_group}
Default: 'devs'
Domain/group name that user will become a sub-domain of.
-s --server=${_server}
Default: 'nginx'
Server type, eg 'apache2' or 'nginx' to write and link configuration files
for. Try '${__NAME__} examples'
-i --interface=${_interface}
Interface that web-server is listening on. IPv4 & IPv6 listening addresses
will automatically be parsed and added to the ${_server} configuration or
config. directory if available.
-t --tld --top-level-domain=${_tld}
Default: 'lan'
Top level domain name, eg 'local', 'io' or 'com', etc. Note if left empty or
unset, and if repo contains a period eg 'jekyll-template.local' then the
last word is parsed out as the TLD.
-c --clobber=${_clobber}
Default: 'no'
If 'yes' or 'append' then pre-existing server configuration files will be appended to.
If 'force' then appends regardless of if jekyll-build has been run against --repo
If 'remove' then pre-existing location configuration will be removed for
given repository.
If 'disable' then the symbolic link pointing to given user web server
configurations will be removed, disabling all sites for that user.
If 'delete' then both operations for remove and disable will be preformed
If 'update' then --repo is ignored while all directories
under \${HOME}/www have their configuration blocks updated
If 'rewrite' then process is similar to '--clobber=update', however,
header of configuration file will also be overwritten; useful for
changing interface/listening IP address(es)
-l --license
Shows script or project license then exits
-h --help
Shows values set for above options, print usage, then exits
EOF
if (("${#_parsed_argument_list[@]}")); then
cat <<EOF
Parsed command arguments
$(printf ' %s\n' "${_parsed_argument_list[@]}")
EOF
fi
}
#
# Read & save recognized arguments to variables
#
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
_valid_args=('--help|-h|help:bool'
'--license|-l|license:bool'
'--server|-s:posix'
'--interface|-i:posix'
'--user|-u:posix'
'--group|-g|--domain|-d:posix'
'--tld|-t|--top-level-domain:alpha_numeric'
'--repo|-r:posix'
'--clobber|-c:alpha_numeric')
argument_parser '_args' '_valid_args'
_exit_status="$?"
_repo="${_repo:-${_user}}"
if ((_help)) || ((_exit_status)); then
usage '_assigned_args' | less ${_LESS_OPTS}
exit "${_exit_status:-0}"
elif ((_license)); then
__license__ "${__DESCRIPTION__}" "${__AUTHOR__}"
exit "${_exit_status:-0}"
elif [[ -z "${_repo}" ]] || [[ -z "${_user}" ]]; then
printf 'Please see "--repo" and/or "--user" options\n' >&2
usage '_assigned_args' | less ${_LESS_OPTS}
exit "${_exit_status:-1}"
fi
#
# Do stuff with assigned functions & variables
#
case "${_server,,}" in
'nginx')
case "${_clobber,,}" in
'remove'|'disable'|'delete')
nginx_remove_config "${_user}:${_group}" "${_repo}" "${_tld}" "${_clobber}"
systemctl restart nginx.service
;;
'update'|'rewrite')
nginx_rewrite_config "${_user}" "${_group}" "${_tld}" "${interface}" "${_clobber}"
systemctl reload nginx.service
;;
'yes'|'append'|*)
_sites_available_path="${_NGINX_CONF_DIR}/sites-available/${_user,,}.${_group}.${_tld}"
nginx_write_config "${_user}:${_group}" "${_repo}" "${_tld}" "${_interface}" "${_clobber}"
nginx_enable_config "${_sites_available_path}"
systemctl restart nginx.service
;;
esac
;;
*)
echo "${__NAME__} not currently coded to setup ${_server}"
;;
esac