Switch everything to Python
This commit is contained in:
264
dns-zone-add
264
dns-zone-add
@@ -1,175 +1,133 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env python3
|
||||
|
||||
SCRIPT_PATH=$(realpath -s "${0}")
|
||||
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
||||
SCRIPT=$(basename "$SCRIPT_PATH")
|
||||
import argparse
|
||||
import dns.rdataclass
|
||||
import dns.rdataset
|
||||
import dns.rdatatype
|
||||
import dnsmgr
|
||||
import sys
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $SCRIPT [OPTIONS]... ZONE[@VIEW]
|
||||
from time import sleep
|
||||
|
||||
Add new DNS zones.
|
||||
|
||||
Options:
|
||||
-c, --config path to config file
|
||||
-h, --help print this help message
|
||||
-f, --force add zones 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
|
||||
}
|
||||
def main():
|
||||
preparser = argparse.ArgumentParser(add_help=False)
|
||||
preparser.add_argument('-b', '--batch', action='store_true')
|
||||
preargs, args = preparser.parse_known_args()
|
||||
nargs = None if preargs.batch else '?'
|
||||
|
||||
config_file="/etc/dns-manager/config.sh"
|
||||
config_template=""
|
||||
force=false
|
||||
interactive=false
|
||||
zone=""
|
||||
zone_template=""
|
||||
parser = argparse.ArgumentParser(description='Add DNS zones.')
|
||||
parser.add_argument('-b', '--batch', help='run in batch mode (no user input)', action='store_true')
|
||||
parser.add_argument('-c', '--config', help='path to config file', default=dnsmgr.DEFAULT_CFGFILE)
|
||||
parser.add_argument('-t', '--config-template', help='config file/template (overrides value set in ZONE_TEMPLATES config option)', default=None)
|
||||
parser.add_argument('-z', '--zone-template', help='zone file/template (overrides value set in ZONE_TEMPLATES config option)', default=None)
|
||||
parser.add_argument('zone', metavar='ZONE[@VIEWS]', nargs=nargs, help='DNS zone name and optional list of views (comma separated or asterisk to select all views)', default=None)
|
||||
args = parser.parse_args()
|
||||
|
||||
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
|
||||
try:
|
||||
manager = dnsmgr.DNSManager(cfgfile=args.config)
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(f'config: {e}')
|
||||
sys.exit(100)
|
||||
|
||||
source "$config_file" || exit 2
|
||||
managed_views = sorted(manager.config.zones_config.keys())
|
||||
|
||||
LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib}
|
||||
source "$LIB_DIR"/dns.sh || exit 3
|
||||
source "$LIB_DIR"/output.sh || exit 3
|
||||
try:
|
||||
if args.zone is None:
|
||||
name = dnsmgr.input_name()
|
||||
rows = [[view] for view in managed_views]
|
||||
index = dnsmgr.prettyselect(['View'], rows, prompt='Select view', also_valid=['*'])
|
||||
views = managed_views if index == '*' else [managed_views[index]]
|
||||
else:
|
||||
(name, views) = dnsmgr.name_views_from_text(args.zone)
|
||||
if views is None:
|
||||
if len(managed_views) > 1:
|
||||
raise RuntimeError('multiple managed views configured but none specified')
|
||||
elif managed_views[0] != dnsmgr.NAMED_DEFAULT_VIEW:
|
||||
raise RuntimeError('the default view is not managed')
|
||||
views = managed_views
|
||||
elif views == '*':
|
||||
views = managed_views
|
||||
else:
|
||||
for view in views:
|
||||
if view not in managed_views:
|
||||
raise RuntimeError(f'managed view does not exist -- \'{view}\'')
|
||||
|
||||
set -- "${args[@]}"
|
||||
existing_views = [zone.view for zone in filter(lambda zone: zone.origin == name and zone.view in views, manager.all_zones)]
|
||||
if existing_views:
|
||||
views = 'and '.join(existing_views)
|
||||
raise RuntimeError(f'zone already exists in view {views}')
|
||||
|
||||
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
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(e)
|
||||
sys.exit(150)
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
|
||||
declare -A output
|
||||
if [ "${view}" == "*" ]; then
|
||||
json_array_to_bash views < <(dns_zone_views)
|
||||
else
|
||||
views=("$view")
|
||||
fi
|
||||
if not args.batch:
|
||||
for view in views:
|
||||
origin = name.to_text(omit_final_dot=True)
|
||||
print(f'View: {view}')
|
||||
print(f'\033[32m+ {origin}\033[0m\n')
|
||||
|
||||
for view in "${views[@]}"; do
|
||||
dns_get_base_config "$view" zone_dir conf_dir || exit 13
|
||||
if not dnsmgr.input_yes_no():
|
||||
sys.exit(0)
|
||||
|
||||
zone_conf_file="$conf_dir/$zone.conf"
|
||||
[ -f "$zone_conf_file" ] && echo "ERROR: config file already exists -- '$zone_conf_file'" >&2 && exit 14
|
||||
zones = []
|
||||
for view in views:
|
||||
origin = name.to_text(omit_final_dot=True)
|
||||
if len(views) > 1 or view != dnsmgr.NAMED_DEFAULT_VIEW:
|
||||
origin = f'{origin}@{view}'
|
||||
print(f"Adding zone '{origin}'... ", end='')
|
||||
|
||||
zone_file="$zone_dir/$zone.zone"
|
||||
[ -f "$zone_file" ] && echo "ERROR: zone file already exists -- '$zone_file'" >&2 && exit 15
|
||||
try:
|
||||
zone = manager.add_zone(name, view, args.config_template, args.zone_template)
|
||||
manager.generate_config(view)
|
||||
print('OK')
|
||||
if manager.config.zones_config[view].catalog_zone:
|
||||
zones.append(zone)
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(e)
|
||||
sys.exit(160)
|
||||
|
||||
IFS=":" read -r cfg_zone_template cfg_config_template <<<"${ZONE_TEMPLATES["$view"]}"
|
||||
try:
|
||||
print('Reloading named... ', end='')
|
||||
manager.named_reload()
|
||||
print('OK')
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(e)
|
||||
sys.exit(170)
|
||||
|
||||
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
|
||||
if zones:
|
||||
sleep(2)
|
||||
|
||||
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
|
||||
for zone in zones:
|
||||
catalog_zone_name = manager.config.zones_config[zone.view].catalog_zone
|
||||
try:
|
||||
catalog_zones = manager.get_zones(catalog_zone_name, all_zones=True)
|
||||
except RuntimeError as e:
|
||||
raise RuntimeError(f'catalog zone of view \'{zone.view}\': {e}')
|
||||
|
||||
dns_check_zone_view "$zone@$view" &>/dev/null && echo "ERROR: non-managed zone already exists in DNS -- '$zone@$view'" >&2 && exit 16
|
||||
done
|
||||
origin = zone.origin.to_text(omit_final_dot=True)
|
||||
if len(zones) > 1 or zone.view != dnsmgr.NAMED_DEFAULT_VIEW:
|
||||
origin = f'{origin}@{zone.view}'
|
||||
|
||||
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
|
||||
for catalog_zone in catalog_zones:
|
||||
rdata = dnsmgr.rdata_from_text(dns.rdatatype.PTR, zone.origin.to_text(), catalog_zone.origin)
|
||||
rdataset = dns.rdataset.Rdataset(dns.rdataclass.IN, dns.rdatatype.PTR, ttl=3600)
|
||||
rdataset.add(rdata)
|
||||
rdname = dns.name.from_text(zone.nfz() + '.zones', catalog_zone.origin)
|
||||
catalog_zone_origin = catalog_zone.origin.to_text(omit_final_dot=True)
|
||||
if catalog_zone.view != dnsmgr.NAMED_DEFAULT_VIEW:
|
||||
catalog_zone_origin += f'@{catalog_zone.view}'
|
||||
|
||||
echo -n "Adding zone to config... "
|
||||
for view in "${views[@]}"; do
|
||||
dns_get_base_config "$view" zone_dir conf_dir conf_file || exit 13
|
||||
try:
|
||||
print(f'Adding zone \'{origin}\' to catalog zone \'{catalog_zone_origin}\'... ', end='')
|
||||
manager.add_zone_record(catalog_zone, rdname, rdataset)
|
||||
print('OK')
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(e)
|
||||
|
||||
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
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user