dns-config still had occurrences of file()
[myplc.git] / bin / dns-config
1 #!/usr/bin/env /usr/bin/plcsh
2 #
3 # Writes IP addresses and hostnames of PlanetLab nodes to
4 # /etc/plc_hosts. Useful for dnsmasq, specify "addn-hosts
5 # /etc/plc_hosts" in /etc/dnsmasq.conf.
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # pylint: disable=c0111
11
12 import sys
13 import os
14 from plc_config import PLCConfiguration
15
16 def writepid(prog):
17     """
18     Check PID file. Exit if already running. Update PID file.
19     """
20
21     try:
22         with open("/var/run/%s.pid" % prog, "r") as pidfile:
23             pid = pidfile.readline().strip()
24         if os.path.isdir("/proc/" + pid):
25             print("Error: Another copy of %s is still running (%s)" % (prog, pid))
26             sys.exit(1)
27     except IOError:
28         pass
29
30     with open("/var/run/%s.pid" % prog, "w") as pidfile:
31         pidfile.write(str(os.getpid()))
32
33 def removepid(prog):
34     os.unlink("/var/run/%s.pid" % prog)
35
36 def main():
37     writepid("dns-config")
38
39     cfg = PLCConfiguration()
40     cfg.load()
41     variables = cfg.variables()
42
43     (_, variablelist) = variables['plc_dns']
44     plc_dns = dict(list(zip(list(variablelist.keys()),
45                             [variable['value']
46                              for variable in list(variablelist.values())])))
47
48     if plc_dns['enabled'] != "true":
49         return 0
50
51     # Get the primary IP address for each node
52     hosts = {}
53
54     nodes = {}
55     for node in GetNodes():
56         nodes[node['node_id']] = node
57
58     interface_ids = set()
59     for node in list(nodes.values()):
60         interface_ids.update(node['interface_ids'])
61
62     for interface in GetInterfaces(list(interface_ids)):
63         if not interface['ip']:
64             continue
65
66         if interface['hostname']:
67             hostname = interface['hostname']
68         else:
69             hostname = nodes[interface['node_id']]['hostname']
70
71         if interface['ip'] in hosts:
72             if hostname not in hosts[interface['ip']]:
73                 hosts[interface['ip']].append(hostname)
74         else:
75             hosts[interface['ip']] = [hostname]
76
77     # Write /etc/plc_hosts
78     plc_hosts = open("/etc/plc_hosts", "w")
79     plc_hosts.write("# DO NOT EDIT; File is written and removed by automatic scripts\n")
80     for ip, hostnames in hosts.items():
81         plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n")
82     plc_hosts.close()
83
84     # From the default dnsmasq.conf configuration file:
85     #
86     # The [domain-needed and bogus-priv] options make you a better
87     # netizen, since they tell dnsmasq to filter out queries which
88     # the public DNS cannot answer, and which load the servers
89     # (especially the root servers) uneccessarily.
90     #
91     with open("/etc/dnsmasq.conf", "w") as writer:
92         writer.write("""
93 domain-needed
94 bogus-priv
95 addn-hosts=/etc/plc_hosts
96 resolv-file=/etc/resolv.conf
97 """.lstrip())
98
99     # Reload dnsmasq
100     os.system("killall -q -HUP dnsmasq")
101
102     removepid("dns-config")
103
104 if __name__ == '__main__':
105     main()