Move the remctl code into invirt.remctl
authorEvan Broder <broder@mit.edu>
Tue, 11 Nov 2008 08:50:13 +0000 (03:50 -0500)
committerEvan Broder <broder@mit.edu>
Tue, 11 Nov 2008 08:50:13 +0000 (03:50 -0500)
svn path=/trunk/packages/invirt-base/; revision=1613

debian/changelog
python/invirt/remctl.py [new file with mode: 0644]

index 7082585..314f13b 100644 (file)
@@ -1,8 +1,9 @@
 invirt-base (0.0.9) unstable; urgency=low
 
   * Move useful Python exceptions into invirt.common
+  * Move the remctl code out of the website and into invirt-base
 
- -- Evan Broder <broder@mit.edu>  Tue, 11 Nov 2008 00:32:19 -0500
+ -- Evan Broder <broder@mit.edu>  Tue, 11 Nov 2008 01:14:42 -0500
 
 invirt-base (0.0.8) unstable; urgency=low
 
diff --git a/python/invirt/remctl.py b/python/invirt/remctl.py
new file mode 100644 (file)
index 0000000..8f93984
--- /dev/null
@@ -0,0 +1,40 @@
+"""
+Functions to perform remctls.
+"""
+
+from invirt.common import CodeError
+import subprocess
+
+def kinit():
+    """Kinit with a given username and keytab"""
+    p = subprocess.Popen(['kinit', "-k", "-t", '/etc/invirt/keytab',
+                          'daemon/'+config.web.hostname],
+                         stderr=subprocess.PIPE)
+    e = p.wait()
+    if e:
+        raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
+
+def checkKinit():
+    """If we lack tickets, kinit."""
+    p = subprocess.Popen(['klist', '-s'])
+    if p.wait():
+        kinit()
+
+def remctl(*args, **kws):
+    """Perform a remctl and return the output.
+
+    kinits if necessary, and outputs errors to stderr.
+    """
+    checkKinit()
+    p = subprocess.Popen(['remctl', config.remote.hostname]
+                         + list(args),
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE)
+    v = p.wait()
+    if kws.get('err'):
+        return p.stdout.read(), p.stderr.read()
+    if v:
+        print >> sys.stderr, 'Error', v, 'on remctl', args, ':'
+        print >> sys.stderr, p.stderr.read()
+        raise CodeError('ERROR on remctl')
+    return p.stdout.read()