use -R option to get raw output and extract geni_code
[sface.git] / sface / sfiprocess.py
index 6950554..00f9df3 100644 (file)
@@ -1,6 +1,9 @@
 
 import os
+import json
+import pickle
 import sys
+import tempfile
 import time
 
 from PyQt4.QtCore import *
@@ -12,7 +15,7 @@ def find_executable(exec_name):
     paths = os.getenv("PATH").split(':')
     for p in paths:
         exec_path = os.path.join(p, exec_name)
-        if os.path.exists(exec_path):
+        if os.path.exists(exec_path) and os.access(exec_path,os.X_OK):
             return exec_path
     return None
 
@@ -34,6 +37,8 @@ class SfiProcess(QObject):
         # tracker.
         self.output = ""
 
+        self.do_raw = True   # xxx should check version of sfa
+
         self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
                      self.processStandardOutput)
         self.connect(self.process, SIGNAL("readyReadStandardError()"),
@@ -49,6 +54,15 @@ class SfiProcess(QObject):
         # then to the users.
         self.args << QString('-D')
 
+        if self.do_raw:
+            self.raw_filename = tempfile.mktemp(suffix=".raw")
+            self.args << QString('-R')
+            self.args << QString(self.raw_filename)
+            self.args << QString('--rawformat')
+            self.args << QString('json')
+        else:
+            self.raw_filename = None
+
         for arg in args:
             self.args << QString(arg)
 
@@ -63,7 +77,15 @@ class SfiProcess(QObject):
         output = self.process.readAllStandardOutput()
         self.output = self.output + output
         if config.debug:
-            print output
+            try:
+                print output
+            except IOError, e:
+                if (e.errno == 4):
+                    # XXX why is this happening??
+                    print "*** caught EINTR"
+                else:
+                    raise
+
 
     def processStandardError(self):
         print self.process.readAllStandardError()
@@ -86,6 +108,22 @@ class SfiProcess(QObject):
             elif err == QProcess.UnknownError:
                 print "UnknownError"
 
+        self.raw_data = None
+        if (self.raw_filename != None) and os.path.exists(self.raw_filename):
+            if hasattr(json, "loads"):
+                self.raw_data = json.loads(open(self.raw_filename,"r").read())
+            else:
+                self.raw_data = json.read(open(self.raw_filename,"r").read())
+
+            os.remove(self.raw_filename)
+
+        self.geni_code=None
+        self.geni_output=None
+        if (self.raw_data!=None) and (type(self.raw_data)==dict):
+            code = self.raw_data.get("code",{})
+            self.geni_code = code.get("geni_code",None)
+            self.geni_output = self.raw_data.get("output","")
+
         # extract any faults from the XMLRPC response(s)
         self.xmlrpcreader.responses = []
         self.xmlrpcreader.store(self.output)
@@ -93,6 +131,16 @@ class SfiProcess(QObject):
         self.responses = self.xmlrpcreader.responses
         self.faults = [x for x in self.responses if (x["kind"]=="fault")]
 
+        # if we got a nonzero
+        if (self.geni_code!=None) and (self.geni_code!=0):
+            x = {"kind": "bad_geni_code"}
+            x["faultCode"] = str(self.geni_code)
+            faultString = "Nonzero geni_code: " + str(self.geni_code)
+            if self.geni_output:
+                faultString = faultString + " output: " + str(self.geni_output)
+            x["faultString"] = faultString
+            self.faults.append(x)
+
         self.trace_end()
         self.emit(SIGNAL("finished()"))
 
@@ -119,20 +167,38 @@ class SfiProcess(QObject):
         self.start()
         return filename
 
+    def retrieveResources(self):
+        mgr = config.getSlicemgr()
+        # Write RSpec to file
+        filename = config.getResourcesRSpecFile()
+        try:
+            os.remove(filename)
+        except:
+            pass
+        args = ["-u", config.getUser(), "-a", config.getAuthority(),
+                "-r", config.getRegistry(), "-s", mgr, "resources",
+                "-o", filename]
+
+        self.__init_command(args)
+        self.start()
+        return filename
+
 
     def listRecords(self, hrn, rectype=None, filename=None):
         args = ["-u", config.getUser(), "-a", config.getAuthority(),
-                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", "-F", "xmllist", hrn]
 
-        if filename:
-            # we can't tell whether SFI will create one file or many, so delete
-            # leftovers from last time, then we'll know what we got, after we get it.
-            if os.path.exists(filename):
-                os.remove(filename)
-            if os.path.exists(filename + ".1"):
-                os.remove(filename + ".1")
-            args.append("-o")
-            args.append(filename)
+        if not filename:
+            filename = config.getAuthorityListFile()
+
+        # we can't tell whether SFI will create one file or many, so delete
+        # leftovers from last time, then we'll know what we got, after we get it.
+        if os.path.exists(filename):
+            os.remove(filename)
+        if os.path.exists(filename + ".1"):
+            os.remove(filename + ".1")
+        args.append("-o")
+        args.append(filename)
 
         if rectype:
             args.append("-t")
@@ -156,7 +222,7 @@ class SfiProcess(QObject):
     def getAuthorityRecord(self):
         self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
 
-    def applyRSpec(self, rspec):
+    def applyRSpec(self, rspec, aggAddr=None, aggPort=None, saveObtained=True):
         # that's what we pass, like in what we'd like to get
         requested = config.getSliceRSpecFile() + "_new"
         # that's what we actually receive
@@ -164,9 +230,31 @@ class SfiProcess(QObject):
         obtained = config.getSliceRSpecFile()
         rspec.save(requested)
         args = ["-u", config.getUser(), "-a", config.getAuthority(),
-                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
-                "-o", obtained,
-                config.getSlice(), requested]
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create"]
+
+        if saveObtained:
+            args = args + ["-o", obtained]
+
+        if aggAddr:
+            args = args + ["-a", aggAddr, "-p", str(aggPort)]
+
+        args = args + [config.getSlice(), requested]
+
+        self.__init_command(args)
+        self.start()
+
+    def deleteSlivers(self, slice=None, aggAddr=None, aggPort=None):
+        if not slice:
+            slice = config.getSlice()
+
+        args = ["-u", config.getUser(), "-a", config.getAuthority(),
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "delete"]
+
+        if aggAddr:
+            args = args + ["-a", aggAddr, "-p", str(aggPort)]
+
+        args = args + [slice]
+
         self.__init_command(args)
         self.start()
 
@@ -182,10 +270,42 @@ class SfiProcess(QObject):
         self.__init_command(args)
         self.start()
 
-    def renewSlivers(self, expiration):
+    def removeRecord(self, hrn):
+        args = ["-u", config.getUser(), "-a", config.getAuthority(),
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "remove", hrn]
+        self.__init_command(args)
+        self.start()
+
+    def renewSlivers(self, expiration, slice=None):
+        if not slice:
+            slice = config.getSlice()
+
         args = ["-u", config.getUser(), "-a", config.getAuthority(),
                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
-                config.getSlice(), expiration]
+                slice, expiration]
+        self.__init_command(args)
+        self.start()
+
+    def sliverStatus(self, slice=None, filename=None):
+        if not slice:
+            slice = config.getSlice()
+
+        if not filename:
+            filename = config.fullpath(slice+".sliverstatus")
+
+        args = ["-u", config.getUser(), "-a", config.getAuthority(),
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "status",
+                "-o", filename, "-F", "pickled", slice]
+        self.__init_command(args)
+        self.start()
+
+    def getSliceMgrVersion(self, filename=None):
+        if not filename:
+            filename = config.fullpath("slicemgr.version")
+
+        args = ["-u", config.getUser(), "-a", config.getAuthority(),
+                "-r", config.getRegistry(), "-s", config.getSlicemgr(), "version",
+                "-o", filename, "-F", "pickled",]
         self.__init_command(args)
         self.start()