Move the remctl code into invirt.remctl
[invirt/packages/invirt-web.git] / code / controls.py
index 9f5bc48..4576a2e 100644 (file)
@@ -1,11 +1,6 @@
-"""
-Functions to perform remctls.
-"""
-
 import validation
 import validation
-from webcommon import CodeError, InvalidInput
+from invirt.common import CodeError, InvalidInput
 import random
 import random
-import subprocess
 import sys
 import time
 import re
 import sys
 import time
 import re
@@ -14,6 +9,7 @@ import yaml
 
 from invirt.config import structs as config
 from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta
 
 from invirt.config import structs as config
 from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta
+from invirt.remctl import remctl
 
 # ... and stolen from xend/uuid.py
 def randomUUID():
 
 # ... and stolen from xend/uuid.py
 def randomUUID():
@@ -27,40 +23,6 @@ def uuidToString(u):
                      "%02x" * 6]) % tuple(u)
 # end stolen code
 
                      "%02x" * 6]) % tuple(u)
 # end stolen code
 
-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()
-
 def lvcreate(machine, disk):
     """Create a single disk for a machine"""
     remctl('web', 'lvcreate', machine.name,
 def lvcreate(machine, disk):
     """Create a single disk for a machine"""
     remctl('web', 'lvcreate', machine.name,
@@ -144,8 +106,9 @@ def createVm(username, state, owner, contact, name, description, memory, disksiz
     makeDisks(machine)
     if autoinstall:
         lvinstall(machine, autoinstall)
     makeDisks(machine)
     if autoinstall:
         lvinstall(machine, autoinstall)
-    # tell it to boot with cdrom
-    bootMachine(machine, cdrom)
+    else:
+        # tell it to boot with cdrom
+        bootMachine(machine, cdrom)
     return machine
 
 def getList():
     return machine
 
 def getList():