Add dns-zone-add and dns-zone-delete
This commit is contained in:
175
dns-zone-add
Executable file
175
dns-zone-add
Executable file
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
SCRIPT_PATH=$(realpath -s "${0}")
|
||||
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
||||
SCRIPT=$(basename "$SCRIPT_PATH")
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $SCRIPT [OPTIONS]... ZONE[@VIEW]
|
||||
|
||||
Add new DNS zones.
|
||||
|
||||
Options:
|
||||
-c, --config path to config file
|
||||
-h, --help print this help message
|
||||
-f, --force add zone without confirmation prompt
|
||||
-i, --interactive interactively ask for missing arguments
|
||||
-t, --config-template config file/template (overrides value set in ZONE_TEMPLATES config option)
|
||||
-z, --zone-template zone file/template (overrides value set in ZONE_TEMPLATES config option)
|
||||
EOF
|
||||
exit
|
||||
}
|
||||
|
||||
config_file="/etc/dns-manager/config.sh"
|
||||
config_template=""
|
||||
force=false
|
||||
interactive=false
|
||||
zone=""
|
||||
zone_template=""
|
||||
|
||||
declare -a args=()
|
||||
while [ -n "$1" ]; do
|
||||
opt=$1
|
||||
shift
|
||||
case "$opt" in
|
||||
-c|--config)
|
||||
config_file=$1
|
||||
if ! shift; then
|
||||
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
;;
|
||||
-f|--force)
|
||||
force=true
|
||||
;;
|
||||
-i|--interactive)
|
||||
interactive=true
|
||||
;;
|
||||
-t|--config-template)
|
||||
config_template=$1
|
||||
if ! shift; then
|
||||
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
-z|--zone-template)
|
||||
zone_template=$1
|
||||
if ! shift; then
|
||||
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
-*)
|
||||
echo "$SCRIPT: invalid option -- '$opt'" >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
args+=("$opt")
|
||||
if (( ${#args[@]} > 1 )); then
|
||||
echo "$SCRIPT: invalid argument -- '$opt'" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
source "$config_file" || exit 2
|
||||
|
||||
LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib}
|
||||
source "$LIB_DIR"/dns.sh || exit 3
|
||||
source "$LIB_DIR"/output.sh || exit 3
|
||||
|
||||
set -- "${args[@]}"
|
||||
|
||||
zone=$1
|
||||
if shift; then
|
||||
dns_check_zone_name_view "$zone" zone view || exit 10
|
||||
elif $interactive; then
|
||||
dns_read_zone_view zone view || exit 11
|
||||
else
|
||||
echo "$SCRIPT: missing argument -- ZONE[@VIEW]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
declare -A output
|
||||
if [ "${view}" == "*" ]; then
|
||||
json_array_to_bash views < <(dns_zone_views)
|
||||
else
|
||||
views=("$view")
|
||||
fi
|
||||
|
||||
for view in "${views[@]}"; do
|
||||
dns_get_base_config "$view" zone_dir conf_dir || exit 13
|
||||
|
||||
zone_conf_file="$conf_dir/$zone.conf"
|
||||
[ -f "$zone_conf_file" ] && echo "ERROR: config file already exists -- '$zone_conf_file'" >&2 && exit 14
|
||||
|
||||
zone_file="$zone_dir/$zone.zone"
|
||||
[ -f "$zone_file" ] && echo "ERROR: zone file already exists -- '$zone_file'" >&2 && exit 15
|
||||
|
||||
IFS=":" read -r cfg_zone_template cfg_config_template <<<"${ZONE_TEMPLATES["$view"]}"
|
||||
|
||||
conf_template=${config_template:-${cfg_config_template}}
|
||||
[ -z "$conf_template" ] && echo "ERROR: config template not configured nor specified by '-t' option" >&2 && exit 16
|
||||
! [ -f "$conf_template" ] && echo "ERROR: zone config template: no such file -- '$conf_template'" >&2 && exit 17
|
||||
|
||||
zone_template=${zone_template:-${cfg_zone_template}}
|
||||
[ -z "$zone_template" ] && echo "ERROR: zone template not configured nor specified by '-z' option" >&2 && exit 18
|
||||
! [ -f "$zone_template" ] && echo "ERROR: zone template: no such file -- '$zone_template'" >&2 && exit 19
|
||||
|
||||
dns_check_zone_view "$zone@$view" &>/dev/null && echo "ERROR: non-managed zone already exists in DNS -- '$zone@$view'" >&2 && exit 16
|
||||
done
|
||||
|
||||
if ! $force; then
|
||||
for view in "${views[@]}"; do
|
||||
echo "View: $view"
|
||||
echo -e "\e[32m+ $TAB$zone\e[0m"
|
||||
done
|
||||
echo
|
||||
! yes_no "Proceed?" && echo -e "Aborted" && exit
|
||||
echo
|
||||
fi
|
||||
|
||||
echo -n "Adding zone to config... "
|
||||
for view in "${views[@]}"; do
|
||||
dns_get_base_config "$view" zone_dir conf_dir conf_file || exit 13
|
||||
|
||||
zone_conf_file="$conf_dir/$zone.conf"
|
||||
zone_file="$zone_dir/$zone.zone"
|
||||
|
||||
IFS=":" read -r cfg_zone_template cfg_config_template <<<"${ZONE_TEMPLATES["$view"]}"
|
||||
conf_template=${config_template:-${cfg_config_template}}
|
||||
zone_template=${zone_template:-${cfg_zone_template}}
|
||||
|
||||
! sed "s#%ZONE%#$zone#g;s#%ZONE_FILE%#$zone_file#g" "$conf_template" >"$zone_conf_file" && echo "ERROR: unable to write to config file -- '$zone_conf_file'" >&2 && exit 20
|
||||
! sed "s#%ZONE%#$zone#g" "$zone_template" >"$zone_file" && echo "ERROR: unable to write to zone file -- '$zone_file'" >&2 && exit 21
|
||||
! chown named:named "$zone_file" && echo "ERROR: unable to set ownership of zone file to 'named:named' -- '$zone_file'" >&2 && exit 22
|
||||
|
||||
tmp=$(mktemp)
|
||||
cat >"$tmp" <<EOF
|
||||
/*
|
||||
* This file was generated by DNS-Manager.
|
||||
* DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN!
|
||||
*/
|
||||
EOF
|
||||
while IFS=$NEWLINE read -r file; do
|
||||
if ! cat "$file" >>"$tmp"; then
|
||||
echo "ERROR: unable to write to temp file -- '$tmp'" >&2
|
||||
rm "$tmp"
|
||||
exit 23
|
||||
fi
|
||||
done < <(find "$conf_dir" -maxdepth 1 -type f -name '*.conf')
|
||||
if ! cat "$tmp" > "$conf_file"; then
|
||||
echo "ERROR: unable to write config file -- '$conf_file'" >&2
|
||||
rm "$tmp"
|
||||
exit 24
|
||||
fi
|
||||
rm "$tmp"
|
||||
done
|
||||
echo "Ok"
|
||||
|
||||
dns_reload_config || exit 25
|
||||
Reference in New Issue
Block a user