-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jekyll_dnsconf.sh
173 lines (135 loc) · 4.96 KB
/
jekyll_dnsconf.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
#!/usr/bin/env bash
## Exit if not running with root/level permissions
if [[ "${EUID}" != '0' ]]; then echo "Try: sudo ${0##*/} ${@:---help}"; exit 1; fi
#
# Set defaults for script variables; these maybe overwritten at run-time
#
_server='unbound'
_user='jek'
_group='devs'
_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 DNS configurations for Jekyll and/or Git clients'
#
# Source useful functions
#
## Provides: 'falure' <line-number> <command> exit-code
## 'examples_dns' args
source "${__DIR__}/shared_functions/modules/trap-failure/failure.sh"
trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR
## Provides: remove_unbound_config <domain> <tld> interface clobber
source "${__DIR__}/shared_functions/domain_name_servers/unbound/unbound_remove_config.sh"
## Provides: unbound_write_config <domain> <tld> interface clobber
source "${__DIR__}/shared_functions/domain_name_servers/unbound/unbound_write_config.sh"
## Provides: 'argument_parser <ref_to_allowed_args> <ref_to_user_supplied_args>'
source "${__DIR__}/shared_functions/modules/argument-parser/argument-parser.sh"
## Provides: '__license__ <description> <author>'
source "${__DIR__}/shared_functions/license.sh"
usage(){
local -n _parsed_argument_list="${1}"
cat <<EOF
${__DESCRIPTION__}
Note, this script is only needed when a new user is added.
# Options
-s --server=${_server}
Server type, eg 'apache2' or 'nginx' to write and link configuration files
for. Try '${__NAME__} examples'
-u --user=${_user}
Optional username to prepend as a subdomain for group, eg...
http://${_user:-bill}.${_group}.${_tld}
http://ted.${_group}.${_tld}
-g -d --group --domain=${_group}
The domain/group name that git client usernames are used as sub-domains for
serving via web-server, eg...
http://${_group}.${_tld}
-i --interface=${_interface}
Interface that domain name server is listening on. IPv4 & IPv6 listening
addresses will automatically be parsed and added to the ${_server}
configuration or config. directory if available.
Note interface may also be set to 'all' if clobber is set to 'remove' to force
removal of all configuration blocks that match specific urls, eg
${_user}.${_group}.${_tld} or ${_group}.${_tld}
-t --tld --top-level-domain=${_tld}
Top level domain name, eg 'local', 'io' or 'com', etc.
Defaults to 'lan' if not set.
-c --clobber=${_clobber}
If 'yes' then pre-existing server configuration group file will be appended
with new configuration blocks.
If 'remove' then pre-existing server configuration blocks will be removed
from configuration file.
If 'delete' then server configuration group file will be removed.
Default is 'no'
-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'
'--clobber|-c:alpha_numeric')
argument_parser '_args' '_valid_args'
_exit_status="$?"
_group="${_group,,}"
(("${#_user}")) && {
_user="${_user,,}"
_domain="${_user}.${_group}"
} || {
_domain="${_group}"
}
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}
fi
#
# Do stuff with assigned functions & variables
#
case "${_server,,}" in
'unbound')
case "${_clobber,,}" in
'remove'|'delete')
unbound_remove_config "${_domain}" "${_tld:-lan}" "${_interface}" "${_clobber}"
# systemctl restart unbound.service
;;
*)
unbound_write_config "${_domain}" "${_tld:-lan}" "${_interface}" "${_clobber}"
# systemctl restart unbound.service
;;
esac
;;
*)
echo "${__NAME__} not currently coded to setup ${_server}"
;;
esac