예제 #1
0
def getClientConf():
    try:
        cfg = ClientConf()
        #make sure there is configured server
        cfg.getClientSecurePort()
        cfg.getClientHost()
    except (NoConfError, NoServerError):
        raise ClientError("No servers."\
            " Use cpcc add-server to add one.")
    except cpc.util.conf.conf_base.ConfError as e:
        raise ClientError(e)
예제 #2
0
def getClientConf():
    try:
        cfg = ClientConf()
        #make sure there is configured server
        cfg.getClientSecurePort()
        cfg.getClientHost()
    except (NoConfError, NoServerError):
        raise ClientError("No servers."\
            " Use cpcc add-server to add one.")
    except cpc.util.conf.conf_base.ConfError as e:
        raise ClientError(e)     
예제 #3
0
class ClientMessage(ClientBase):
    '''Client request class. Has methods for specific requests.
        Messages that end users should be able to call should be defined here
    '''

    def __init__(self,host=None,port=None,conf=None, use_secure_server_port=False):
        '''
        @input String host : the hostname
        @input String port : the port to connect to
        @input Conf conf
        '''

        self.host = host
        self.port = port
        self.conf = conf
        self.require_certificate_authentication = use_secure_server_port
        if self.conf is None:
            #FIXME THIS IS WRONG AND TOOK HOURS TO FIND OUT WHY
            #APPERANTLY WE CANNOT INITIATE A CLIENTCONF WITHOUT SPECYFYING A
            # BUNDLE OF SOME SORT AS IS DONE IN cpcc
            self.conf = ClientConf()
        if self.host is None:
            self.host = self.conf.getClientHost()
        if self.port is None:
            if use_secure_server_port:
                self.port = self.conf.getServerSecurePort()
            else:
                self.port = self.conf.getClientSecurePort()

    #NOTE should not be able to perform with a post or put request 
    def stopRequest(self): 
        cmdstring="stop"
        fields = []
        input = Input('cmd', cmdstring)   
        fields.append(input)
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        #response= self.putRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def loginRequest(self, user, password):
        cmdstring="login"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        fields.append(Input('password', password))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def addUser(self, user, password):
        cmdstring="add-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        fields.append(Input('password', password))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def deleteUser(self, user):
        cmdstring="delete-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def promoteUser(self, user):
        cmdstring="promote-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def demoteUser(self, user):
        cmdstring="demote-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def grantAccess(self, user):
        cmdstring="grant-access"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('name', user))
        msg = ServerRequest.prepareRequest(fields,[])
        response = self.postRequest(msg)
        return response

    def pingServer(self,serverId):
        cmdstring='ping'
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        headers = dict()
        if serverId!= None:
            headers['server-id'] = serverId
        msg = ServerRequest.prepareRequest(fields,[],headers)
        response= self.putRequest(msg)
        return response

    def serverInfo(self):
        cmdstring='server-info'
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields,[])
        response= self.postRequest(msg)
        return response


    def networkTopology(self):
        cmdstring="network-topology-client"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))

        msg = ServerRequest.prepareRequest(fields,[])

        response = self.postRequest(msg)
        return response

    #port is what port the request should be sent to not what port to communicate with later on
    def addNode(self,host,client_secure_port):
        #sends an add node request to the server
        cmdstring = "connnect-server"
        fields = []

        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))

        input2 = Input('host',host)
        input3 = Input('client_secure_port',client_secure_port)

        fields.append(input)
        fields.append(input2)
        fields.append(input3)
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    #shows all the nodes connected to the current server
    def listServers(self):
        cmdString = "list-servers"
        fields  = []
        input = Input('cmd',cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields,[])
        return self.postRequest(msg)


    def revokeNode(self,serverId):
        cmdString = "revoke-node"
        fields  = []
        input = Input('cmd',cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(Input('serverId',serverId))
        msg = ServerRequest.prepareRequest(fields,[])
        return self.postRequest(msg)


    #accepts a connect request for a node
    def grantNodeConnection(self,serverId):
        cmdString = "grant-node-connection"
        input = Input('cmd',cmdString)
        fields = []
        fields.append(input)    
        fields.append(Input('version', "1"))
        fields.append(Input('serverId',serverId))

        msg = ServerRequest.prepareRequest(fields,[])
        return self.postRequest(msg)
        
    def grantAllNodeConnections(self):
        cmdString = "grant-all-node-connections"
        input = Input('cmd',cmdString)
        fields = []
        fields.append(input)    
        fields.append(Input('version', "1"))
          
        msg = ServerRequest.prepareRequest(fields,[])
        
        return self.postRequest(msg)  
     
    def changeNodePriority(self,node,priority,port):
        cmdString = "change-node-priority"
        id = "%s:%s"%(node,port)
        input = Input('cmd',cmdString)
        input2 = Input("nodeId",id)
        input3 = Input("priority",priority)
        fields = []
        fields.append(input)
        fields.append(input2)
        fields.append(input3)
        fields.append(Input('version', "1"))
        
        msg = ServerRequest.prepareRequest(fields,[])
        
        return self.postRequest(msg)  

    def listRequest(self, name): 
        """An assortment of list commands: for queues, running commands, and 
           heartbeat items."""
        cmdstring='list'   
        type = name     
        fields = []
        input = Input('cmd', cmdstring)
        typeInput = Input('type',name)
        fields.append(input)     
        fields.append(Input('version', "1"))
        fields.append(typeInput)
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def statusRequest(self, project):
        """Fetches an aggregated general information about the server and
           and its projects. The argument project is optional"""
        cmdstring="status"
        fields = []
        fields.append(Input('cmd', cmdstring))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def readConfRequest(self):
        """Tell the server to re-read its configuration."""
        cmdstring="readconf"
        fields = []
        input = Input('cmd', cmdstring)   
        fields.append(input)
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def saveStateRequest(self):
        """Tell the server to save its state now."""
        cmdstring="save-state"
        fields = []
        input = Input('cmd', cmdstring)   
        fields.append(input)
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def commandFailedRequest(self, cmdID, cputime):
        cmdstring='command-finished-forward'
        fields = []
        input = Input('cmd', cmdstring)
        cmdIdInput = Input('cmd_id', cmdID)
        fields.append(cmdIdInput)
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(Input('project_server', ''))
        fields.append(Input('used_cpu_time', cputime))
        #if jobTarFileobj is not None:
        #    jobTarFileobj.seek(0)
        #    files = [FileInput('rundata','cmd.tar.gz',jobTarFileobj)]
        #else:
        files=[]
        headers = dict()
        response= self.postRequest(ServerRequest.prepareRequest(fields, files,
                                                               headers))
        return response


      
    # dataflow application requests
    def projectsRequest(self):
        """List all projects"""
        cmdstring="projects"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectStartRequest(self, name):
        """Start a new empty project """
        cmdstring="project-start"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('name',name))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectDeleteRequest(self, project, deleteDir):
        """Delete a project and optionally also its directory """
        cmdstring="project-delete"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('project',project))
        if deleteDir:
            fields.append(Input('delete-dir',1))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectGetDefaultRequest(self):
        """Get the default project project name"""
        cmdstring="project-get-default"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSetDefaultRequest(self, name):
        """Set the default project project name"""
        cmdstring="project-set-default"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('name',name))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectListRequest(self, project, item):
        """List all items in a project"""
        cmdstring="project-list"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectInfoRequest(self, project, item):
        """List descriptions of project items"""
        cmdstring="project-info"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectDebugRequest(self, project, item):
        """Get debug info for project items"""
        cmdstring="project-debug"
        fields = []
        fields.append(Input('cmd', cmdstring))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectLogRequest(self, project, item):
        """List all items in a project"""
        cmdstring="project-log"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectGraphRequest(self, project, item):
        """Graph item's network in a project"""
        cmdstring="project-graph"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectAddInstanceRequest(self, project,  func, name):
        """Add an instance to the top-level active network"""
        cmdstring="project-add-instance"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('name', name))
        fields.append(Input('function', func))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectConnectRequest(self, project, src, dst):
        """Add a connection to the top-level active network"""
        cmdstring="project-connect"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('source', src))
        fields.append(Input('destination', dst))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectActivateRequest(self, project, item):
        """List all projects"""
        cmdstring="project-activate"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectHoldRequest(self, project, item):
        """List all projects"""
        cmdstring="project-deactivate"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectRerunRequest(self, project, item, clearError):
        """Force a rerun and optionally clear an error."""
        cmdstring="project-rerun"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('item', item))
        if clearError:
            fields.append(Input('clear-error', 1))
            fields.append(Input('recursive', 1))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response


    def projectUploadRequest(self, project, file):
        """Upload project file"""
        cmdstring="project-upload"
        fields = []
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        files = [FileInput('upload','project.xml',open(file,'r'))]  
        response= self.postRequest(ServerRequest.prepareRequest(fields, files))
        return response

    #list modules available on the server
    def listModules(self):
        cmdString = "list-modules"
        fields  = []
        input = Input('cmd',cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields,[])
        return self.postRequest(msg)

    def projectImportRequest(self, project, module):
        """Import a module in a network"""
        cmdstring="project-import"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('module', module))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response


    def projectGetRequest(self, project, item,getFile=False):
        """Get a data item from a project."""
        cmdstring="project-get"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        if getFile:
            fields.append(Input('getFile',True))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSetRequest(self, project, item, value, filename):
        """Get a data item from a project."""
        cmdstring="project-set"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        if value is not None:
            fields.append(Input('value', value))
            files = []
        if filename is not None:
            fields.append(Input('filename', os.path.basename(filename)))
            files = [FileInput('upload','upload.dat',open(filename,'r'))]  
        response= self.postRequest(ServerRequest.prepareRequest(fields, files))
        return response

    def projectTransactRequest(self, project):
        """Start a series of previously scheduled set&connect requests, 
           to commit atomically."""
        cmdstring="project-transact"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectCommitRequest(self, project):
        """Commit a series of previously scheduled set&connect requests, 
           atomically."""
        cmdstring="project-commit"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response
    def projectRollbackRequest(self, project):
        """Cancel a series of previously scheduled set&connect requests."""
        cmdstring="project-rollback"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSaveRequest(self, project):
        """Get a data item from a project."""
        cmdstring="project-save"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        #if project is not None:
        fields.append(Input('project', project))
        response= self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectRestoreRequest(self, projectBundle,projectName):
        """Get a data item from a project."""
        cmdstring="project-load"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        #if project is not None:

        filename = os.path.basename(projectBundle)
        fields.append(Input('project', projectName))
        files = [FileInput('projectFile',filename,open(projectBundle,'r'))]

        response= self.postRequest(ServerRequest.prepareRequest(fields, files))
        return response
예제 #4
0
class ClientMessage(ClientBase):
    '''Client request class. Has methods for specific requests.
        Messages that end users should be able to call should be defined here
    '''
    def __init__(self,
                 host=None,
                 port=None,
                 conf=None,
                 use_secure_server_port=False):
        '''
        @input String host : the hostname
        @input String port : the port to connect to
        @input Conf conf
        '''

        self.host = host
        self.port = port
        self.conf = conf
        self.require_certificate_authentication = use_secure_server_port
        if self.conf is None:
            #FIXME THIS IS WRONG AND TOOK HOURS TO FIND OUT WHY
            #APPERANTLY WE CANNOT INITIATE A CLIENTCONF WITHOUT SPECYFYING A
            # BUNDLE OF SOME SORT AS IS DONE IN cpcc
            self.conf = ClientConf()
        if self.host is None:
            self.host = self.conf.getClientHost()
        if self.port is None:
            if use_secure_server_port:
                self.port = self.conf.getServerSecurePort()
            else:
                self.port = self.conf.getClientSecurePort()

    #NOTE should not be able to perform with a post or put request
    def stopRequest(self):
        cmdstring = "stop"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        #response= self.putRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def loginRequest(self, user, password):
        cmdstring = "login"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        fields.append(Input('password', password))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def addUser(self, user, password):
        cmdstring = "add-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        fields.append(Input('password', password))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def deleteUser(self, user):
        cmdstring = "delete-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def promoteUser(self, user):
        cmdstring = "promote-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def demoteUser(self, user):
        cmdstring = "demote-user"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)

        fields.append(Input('user', user))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def grantAccess(self, user):
        cmdstring = "grant-access"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('name', user))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def pingServer(self, serverId):
        cmdstring = 'ping'
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        headers = dict()
        if serverId != None:
            headers['server-id'] = serverId
        msg = ServerRequest.prepareRequest(fields, [], headers)
        response = self.putRequest(msg)
        return response

    def serverInfo(self):
        cmdstring = 'server-info'
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    def networkTopology(self):
        cmdstring = "network-topology-client"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))

        msg = ServerRequest.prepareRequest(fields, [])

        response = self.postRequest(msg)
        return response

    #port is what port the request should be sent to not what port to communicate with later on
    def addNode(self, host, client_secure_port):
        #sends an add node request to the server
        cmdstring = "connnect-server"
        fields = []

        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))

        input2 = Input('host', host)
        input3 = Input('client_secure_port', client_secure_port)

        fields.append(input)
        fields.append(input2)
        fields.append(input3)
        msg = ServerRequest.prepareRequest(fields, [])
        response = self.postRequest(msg)
        return response

    #shows all the nodes connected to the current server
    def listServers(self):
        cmdString = "list-servers"
        fields = []
        input = Input('cmd', cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields, [])
        return self.postRequest(msg)

    def revokeNode(self, serverId):
        cmdString = "revoke-node"
        fields = []
        input = Input('cmd', cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(Input('serverId', serverId))
        msg = ServerRequest.prepareRequest(fields, [])
        return self.postRequest(msg)

    #accepts a connect request for a node
    def grantNodeConnection(self, serverId):
        cmdString = "grant-node-connection"
        input = Input('cmd', cmdString)
        fields = []
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(Input('serverId', serverId))

        msg = ServerRequest.prepareRequest(fields, [])
        return self.postRequest(msg)

    def grantAllNodeConnections(self):
        cmdString = "grant-all-node-connections"
        input = Input('cmd', cmdString)
        fields = []
        fields.append(input)
        fields.append(Input('version', "1"))

        msg = ServerRequest.prepareRequest(fields, [])

        return self.postRequest(msg)

    def changeNodePriority(self, node, priority, port):
        cmdString = "change-node-priority"
        id = "%s:%s" % (node, port)
        input = Input('cmd', cmdString)
        input2 = Input("nodeId", id)
        input3 = Input("priority", priority)
        fields = []
        fields.append(input)
        fields.append(input2)
        fields.append(input3)
        fields.append(Input('version', "1"))

        msg = ServerRequest.prepareRequest(fields, [])

        return self.postRequest(msg)

    def listRequest(self, name):
        """An assortment of list commands: for queues, running commands, and 
           heartbeat items."""
        cmdstring = 'list'
        type = name
        fields = []
        input = Input('cmd', cmdstring)
        typeInput = Input('type', name)
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(typeInput)
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def statusRequest(self, project):
        """Fetches an aggregated general information about the server and
           and its projects. The argument project is optional"""
        cmdstring = "status"
        fields = []
        fields.append(Input('cmd', cmdstring))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def readConfRequest(self):
        """Tell the server to re-read its configuration."""
        cmdstring = "readconf"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def saveStateRequest(self):
        """Tell the server to save its state now."""
        cmdstring = "save-state"
        fields = []
        input = Input('cmd', cmdstring)
        fields.append(input)
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def commandFailedRequest(self, cmdID, cputime):
        cmdstring = 'command-finished-forward'
        fields = []
        input = Input('cmd', cmdstring)
        cmdIdInput = Input('cmd_id', cmdID)
        fields.append(cmdIdInput)
        fields.append(input)
        fields.append(Input('version', "1"))
        fields.append(Input('project_server', ''))
        fields.append(Input('used_cpu_time', cputime))
        #if jobTarFileobj is not None:
        #    jobTarFileobj.seek(0)
        #    files = [FileInput('rundata','cmd.tar.gz',jobTarFileobj)]
        #else:
        files = []
        headers = dict()
        response = self.postRequest(
            ServerRequest.prepareRequest(fields, files, headers))
        return response

    # dataflow application requests
    def projectsRequest(self):
        """List all projects"""
        cmdstring = "projects"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectStartRequest(self, name):
        """Start a new empty project """
        cmdstring = "project-start"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('name', name))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectDeleteRequest(self, project, deleteDir):
        """Delete a project and optionally also its directory """
        cmdstring = "project-delete"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('project', project))
        if deleteDir:
            fields.append(Input('delete-dir', 1))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectGetDefaultRequest(self):
        """Get the default project project name"""
        cmdstring = "project-get-default"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSetDefaultRequest(self, name):
        """Set the default project project name"""
        cmdstring = "project-set-default"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('name', name))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectListRequest(self, project, item):
        """List all items in a project"""
        cmdstring = "project-list"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectInfoRequest(self, project, item):
        """List descriptions of project items"""
        cmdstring = "project-info"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectDebugRequest(self, project, item):
        """Get debug info for project items"""
        cmdstring = "project-debug"
        fields = []
        fields.append(Input('cmd', cmdstring))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectLogRequest(self, project, item):
        """List all items in a project"""
        cmdstring = "project-log"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectGraphRequest(self, project, item):
        """Graph item's network in a project"""
        cmdstring = "project-graph"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectAddInstanceRequest(self, project, func, name):
        """Add an instance to the top-level active network"""
        cmdstring = "project-add-instance"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('name', name))
        fields.append(Input('function', func))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectConnectRequest(self, project, src, dst):
        """Add a connection to the top-level active network"""
        cmdstring = "project-connect"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('source', src))
        fields.append(Input('destination', dst))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectActivateRequest(self, project, item):
        """List all projects"""
        cmdstring = "project-activate"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectHoldRequest(self, project, item):
        """List all projects"""
        cmdstring = "project-deactivate"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectRerunRequest(self, project, item, clearError):
        """Force a rerun and optionally clear an error."""
        cmdstring = "project-rerun"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        fields.append(Input('item', item))
        if clearError:
            fields.append(Input('clear-error', 1))
            fields.append(Input('recursive', 1))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectUploadRequest(self, project, file):
        """Upload project file"""
        cmdstring = "project-upload"
        fields = []
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        files = [FileInput('upload', 'project.xml', open(file, 'r'))]
        response = self.postRequest(ServerRequest.prepareRequest(
            fields, files))
        return response

    #list modules available on the server
    def listModules(self):
        cmdString = "list-modules"
        fields = []
        input = Input('cmd', cmdString)
        fields.append(input)
        fields.append(Input('version', "1"))
        msg = ServerRequest.prepareRequest(fields, [])
        return self.postRequest(msg)

    def projectImportRequest(self, project, module):
        """Import a module in a network"""
        cmdstring = "project-import"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('module', module))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectGetRequest(self, project, item, getFile=False):
        """Get a data item from a project."""
        cmdstring = "project-get"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        if getFile:
            fields.append(Input('getFile', True))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSetRequest(self, project, item, value, filename):
        """Get a data item from a project."""
        cmdstring = "project-set"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        if project is not None:
            fields.append(Input('project', project))
        fields.append(Input('item', item))
        if value is not None:
            fields.append(Input('value', value))
            files = []
        if filename is not None:
            fields.append(Input('filename', os.path.basename(filename)))
            files = [FileInput('upload', 'upload.dat', open(filename, 'r'))]
        response = self.postRequest(ServerRequest.prepareRequest(
            fields, files))
        return response

    def projectTransactRequest(self, project):
        """Start a series of previously scheduled set&connect requests, 
           to commit atomically."""
        cmdstring = "project-transact"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectCommitRequest(self, project):
        """Commit a series of previously scheduled set&connect requests, 
           atomically."""
        cmdstring = "project-commit"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectRollbackRequest(self, project):
        """Cancel a series of previously scheduled set&connect requests."""
        cmdstring = "project-rollback"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectSaveRequest(self, project):
        """Get a data item from a project."""
        cmdstring = "project-save"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        #if project is not None:
        fields.append(Input('project', project))
        response = self.postRequest(ServerRequest.prepareRequest(fields, []))
        return response

    def projectRestoreRequest(self, projectBundle, projectName):
        """Get a data item from a project."""
        cmdstring = "project-load"
        fields = []
        fields.append(Input('cmd', cmdstring))
        fields.append(Input('version', "1"))
        #if project is not None:

        filename = os.path.basename(projectBundle)
        fields.append(Input('project', projectName))
        files = [FileInput('projectFile', filename, open(projectBundle, 'r'))]

        response = self.postRequest(ServerRequest.prepareRequest(
            fields, files))
        return response