Add the update-contacts script for updating xvm-contacts.
authorEvan Broder <broder@mit.edu>
Sun, 9 Aug 2009 18:01:25 +0000 (14:01 -0400)
committerEvan Broder <broder@mit.edu>
Sun, 9 Aug 2009 18:01:25 +0000 (14:01 -0400)
svn path=/trunk/scripts/update-contacts/; revision=2377

update-contacts [new file with mode: 0755]

diff --git a/update-contacts b/update-contacts
new file mode 100755 (executable)
index 0000000..03087a2
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+
+"""Update the xvm-contacts list.
+
+This script finds all e-mail addresses currently listed as contacts
+for a VM and updates the xvm-contacts list to match, adding or
+removing as necessary.
+"""
+
+import socket
+import subprocess
+
+from invirt import database
+from invirt import remctl
+
+def getContacts():
+    contacts = set()
+
+    for m in database.Machine.query():
+        if '@' in m.contact:
+            contacts.add(m.contact.lower())
+        else:
+            contacts.add(m.contact.lower() + '@mit.edu')
+
+    return sorted(contacts)
+
+def stripDomain(c):
+    if c.endswith('@mit.edu'):
+        return c[:-8]
+    else:
+        return c
+
+def updateContacts(contacts):
+    p = subprocess.Popen(['blanche', '-f', '-', 'xvm-contacts'],
+                         stdin=subprocess.PIPE,
+                         )
+
+    p.communicate('\n'.join(stripDomain(c) for c in contacts))
+
+def main():
+    database.connect()
+    subprocess.call(['kinit', '-k'])
+
+    updateContacts(getContacts())
+
+if __name__ == '__main__':
+    main()