Add the update-contacts script for updating xvm-contacts.
[invirt/scripts/update-contacts.git] / update-contacts
1 #!/usr/bin/python
2
3 """Update the xvm-contacts list.
4
5 This script finds all e-mail addresses currently listed as contacts
6 for a VM and updates the xvm-contacts list to match, adding or
7 removing as necessary.
8 """
9
10 import socket
11 import subprocess
12
13 from invirt import database
14 from invirt import remctl
15
16 def getContacts():
17     contacts = set()
18
19     for m in database.Machine.query():
20         if '@' in m.contact:
21             contacts.add(m.contact.lower())
22         else:
23             contacts.add(m.contact.lower() + '@mit.edu')
24
25     return sorted(contacts)
26
27 def stripDomain(c):
28     if c.endswith('@mit.edu'):
29         return c[:-8]
30     else:
31         return c
32
33 def updateContacts(contacts):
34     p = subprocess.Popen(['blanche', '-f', '-', 'xvm-contacts'],
35                          stdin=subprocess.PIPE,
36                          )
37
38     p.communicate('\n'.join(stripDomain(c) for c in contacts))
39
40 def main():
41     database.connect()
42     subprocess.call(['kinit', '-k'])
43
44     updateContacts(getContacts())
45
46 if __name__ == '__main__':
47     main()