Last active
August 29, 2015 13:56
-
-
Save adgaudio/9200030 to your computer and use it in GitHub Desktop.
StarCluster Plugin: Configure Nodes with Dynamic DNS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A starcluster plugin that registers starcluster nodes in DNS | |
It assumes that nsupdate command can be run from the same machine where you run this plugin. | |
""" | |
from starcluster import clustersetup | |
import subprocess | |
from os.path import join | |
# You should configure these to your needs | |
DNS_ZONE = "example.com" | |
NAMESERVER = "ns.example.com" | |
nsupdate_cmd = """ | |
( | |
cat <<EOF | |
server {mydnsserver_ip} | |
zone {dns_zone}. | |
{update_statement} | |
send | |
EOF | |
) | nsupdate -k /etc/bind/named.conf.local | |
""" | |
add_record = "update add {hostname} 60 A {ip}" | |
delete_record = "update delete {hostname} A" | |
# for example, string above could create: | |
#server ns.example.com | |
#zone example.com | |
#update delete mystarcluster-node001.example.com. A | |
#update add mystarcluster-node001.example.com. 86400 A 10.0.0.0 | |
#show | |
#send | |
def node_in_dns(node, add): | |
"""Register or remove a new/old starcluster node with DNS | |
`node` a starcluster.node.Node instance, received from plugin | |
`add` (bool) whether to add or delete a node's dns record""" | |
ip = node.ssh.execute('hostname --ip-address')[0] | |
hostname = '%s.%s' % (node.alias, DNS_ZONE) | |
if add: | |
update_statement = add_record.format(hostname=hostname, ip=ip) | |
else: | |
update_statement = delete_record.format(hostname=hostname) | |
subprocess.check_call( | |
nsupdate_cmd.format(update_statement=update_statement, | |
dns_zone=DNS_ZONE, nameserver=NAMESERVER), | |
shell=True) | |
class DNSUpdate(clustersetup.ClusterSetup): | |
"""Register starcluster nodes with DNS""" | |
def __init__(self, add): | |
self.add = add | |
def run(self, nodes, master, user, user_shell, volumes): | |
for node in nodes: | |
node_in_dns(node, self.add) | |
node_in_dns(master, self.add) | |
def on_add_node(self, node, nodes, master, user, user_shell, volumes): | |
node_in_dns(node, True) | |
def on_remove_node(self, node, nodes, master, user, user_shell, volumes): | |
node_in_dns(node, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment