Menü schliessen
Created: December 11th 2016
Last updated: May 1st 2020
Categories: Linux,  Miscellaneous Online Services
Author: Marcus Fleuti

HowTo: Setting up Virtualmin with BuddyNS secondary DNS service

Tags:  bind,  buddyNS,  curl,  DNS,  jq,  named,  script,  secondary DNS,  Virtualmin
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Description

This HowTo explains how the BuddyNS secondary DNS services can be easily implemented with Virtualmin domain management

Howto

1. Install the UNIX tool JQ. On Debian/Ubuntu you can do this easily like this:

aptitude install jq

2. If not yet installed - install the CURL command line tool like this (i.e. Debian / Ubuntu):

aptitude install curl

3. Create a script called virtualmin_post_changes_commands with the following content:

#! /bin/sh
MASTER_NS=YOUR.IP.ADDRESS.HERE
TOKEN="Enter Your BuddyNS Token Here. Go to BuddyNS account settings to generate one"
### Please check your settings in SYSTEM SETTINGS => SERVER TEMPLATES => [YOUR TEMPLATE] => BIND DNS domain => Add sub-domain DNS records to parent domain? => NO
### Reason: After a lot of testing we found out that Virtualmin does not handle it well adding sub-server domain settings to parent domain automatically
### Alternative solution: do NOT enable the DNS functionality on subdomains and enter subdomain values yourself on primary domain upon creating sub-server creation.
if [ $VIRTUALSERVER_ACTION = "CREATE_DOMAIN" ] && [ $VIRTUALSERVER_DNS = "1" ]; then
 echo "<strong>Updating buddyNS (secondary nameserver) settings ...</strong><br/>"
 echo "Creating domain on buddyNS and issuing synchronisation ...<br/>"
 ### Delete zone first (in case it already exists for some reason) ###
 echo "... Checking if zone <strong>$VIRTUALSERVER_DOM</strong> already exists on buddyNS. If so, remove it in order to recreate and sync the zone information properly...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XDELETE https://www.buddyns.com/api/v2/zone/$VIRTUALSERVER_DOM |jq ".[]"
 ### (Re)create zone on buddyNS ###
 echo "<br/>... Creating new zone <strong>$VIRTUALSERVER_DOM</strong> immediately ...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XPOST -F "name=$VIRTUALSERVER_DOM" -F "master=$MASTER_NS" https://www.buddyns.com/api/v2/zone/ |jq ".[]"
 ### To be sure that all data is being synced: force zone sync immediately ###
 echo "<br/>... Forcing buddyNS to sync the zone <strong>$VIRTUALSERVER_DOM</strong> with our MASTER nameserver $MASTER_NS ...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XGET https://www.buddyns.com/api/v2/sync/$VIRTUALSERVER_DOM |jq ".[]"
 echo "<br/>... done<br/><br/>"
fi
if [ $VIRTUALSERVER_ACTION = "MODIFY_DOMAIN" ] && [ $VIRTUALSERVER_DNS = "1" ]; then
 echo "<strong>Updating buddyNS (secondary nameserver) settings ...</strong><br/>"
 echo "Modifying domain parameters on buddyNS ...<br/>"
 ### We have to do basically the same as when we create a new domain. Since we remove the old domain in the pre changes commands and now we recreate the domain on buddyNS again ###
 echo "... Checking if zone <strong>$VIRTUALSERVER_DOM</strong> already exists on buddyNS. If so, remove it in order to recreate and sync the zone information properly...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XDELETE https://www.buddyns.com/api/v2/zone/$VIRTUALSERVER_DOM |jq ".[]"
 ### (Re)create zone on buddyNS ONLY if DNS option is enabled (it could be that the DNS options has just been disabled - in that case the zone shall remain deleted on buddyNS ###
 echo "<br/>... Creating new zone <strong>$VIRTUALSERVER_DOM</strong> immediately ...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XPOST -F "name=$VIRTUALSERVER_DOM" -F "master=$MASTER_NS" https://www.buddyns.com/api/v2/zone/ |jq ".[]"
 ### To be sure that all data is being synced: force zone sync immediately ###
 echo "<br/>... Forcing buddyNS to sync the zone <strong>$VIRTUALSERVER_DOM</strong> with our MASTER nameserver $MASTER_NS ...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XGET https://www.buddyns.com/api/v2/sync/$VIRTUALSERVER_DOM |jq ".[]"
 echo "<br/>... done<br/><br/>"
fi
## In any case try to also remove it from buddyNS to be sure that there is no residual unnecessary data
if [ $VIRTUALSERVER_ACTION = "DELETE_DOMAIN" ]; then
 echo "<strong>Updating buddyNS (secondary nameserver) settings ...</strong><br/>"
 echo "Deleting the zone <strong>$VIRTUALSERVER_DOM</strong> from buddyNS ...<br/>"
 curl -ss -H "Authorization: Token ${TOKEN}" -XDELETE https://www.buddyns.com/api/v2/zone/$VIRTUALSERVER_DOM |jq ".[]"
 echo "<br/>Deleting Letsencrypt certificate(s)..."
 /home/srvdata/remove_ssl_cert $VIRTUALSERVER_DOM
 echo "<br/>... done<br/><br/>"
fi

4. Create a script called virtualmin_pre_changes_commands with the following content:

#! /bin/sh
MASTER_NS=IP.OF.YOUR-MASTER.DNS
TOKEN="Enter Your BuddyNS Token Here. Go to BuddyNS account settings to generate one"
if [ "$VIRTUALSERVER_ACTION" = "MODIFY_DOMAIN" ]; then
        echo "<strong>Updating buddyNS (secondary nameserver) settings ...</strong><br/>"
        ### To be on the safe side we generally remove the zone from buddyNS. Because at this stage we do not know exactly what the user selected. The domain will be recreated and resynced within the POST script if required ###
        echo "... Delete existing zone <strong>$VIRTUALSERVER_DOM</strong> on buddyNS ...<br/>"
        curl -ss -H "Authorization: Token ${TOKEN}" -XDELETE https://www.buddyns.com/api/v2/zone/$VIRTUALSERVER_DOM |jq ".[]"
        echo "<br/>... done<br/><br/>"
fi

5. Add the 2 scripts to the Virtualmin domain configuration:

  • Go to System Settings -> Virtualmin configuration
  • Add the above scripts to the system configuration as seen on the following screenshot:virtulamin_buddyns_script_configuration_automatic_dns_update_secondary_dns_service

Conclusion

Test the configuration by adding a new domain and check the messages on the screen. Logon to www.buddyns.com to see if the new domain has been created and synced properly.
Let us know in the comments if something is not working out.

Known issues and possible solutions

The script does not work properly when creating sub-domains (sub-servers)

Make sure that you set this option:
Please check your settings in SYSTEM SETTINGS => SERVER TEMPLATES => [YOUR TEMPLATE] => BIND DNS domain => Add sub-domain DNS records to parent domain? => NO
This might look like this: