Exemple #1
0
class NodeApi(TortugaApi):
    """
    High-level API for performing node operations
    """
    def __init__(self):
        super(NodeApi, self).__init__()

        self._nodeManager = NodeManager()

    def createNewNode(self, session: Session, addNodeRequest: dict,
                      dbHardwareProfile: HardwareProfile,
                      dbSoftwareProfile: Optional[SoftwareProfile] = None,
                      validateIp: bool = True, bGenerateIp: bool = True,
                      dns_zone: Optional[str] = None) -> NodeModel:
        try:
            return self._nodeManager.createNewNode(
                session, addNodeRequest, dbHardwareProfile,
                dbSoftwareProfile=dbSoftwareProfile,
                validateIp=validateIp, bGenerateIp=bGenerateIp,
                dns_zone=dns_zone)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error creating new node')

            raise TortugaException(exception=ex)

    def getNodeList(self, session,
                    tags: Optional[Tags] = None) -> TortugaObjectList:
        """
        Get node list..

            Returns:
                list of nodes
            Throws:
                TortugaException
        """
        try:
            return self._nodeManager.getNodeList(session, tags=tags)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error retrieving node list')

            raise TortugaException(exception=ex)

    def getNode(self, session: Session, name: str,
                optionDict: Optional[OptionDict] = None):
        """Get node id by name"""
        try:
            return self._nodeManager.getNode(
                session, name, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node [{}]'.format(name))

            raise TortugaException(exception=ex)

    def getInstallerNode(self, session,
                         optionDict: Optional[OptionDict] = None) \
            -> Node:
        """
        Get installer node
        """

        try:
            return self._nodeManager.getInstallerNode(
                session, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving installer node')

            raise TortugaException(exception=ex)

    def getNodeById(self, session: Session, nodeId: int,
                    optionDict: Optional[OptionDict] = None) -> Node:
        """
        Get a node by id
        """

        try:
            return self._nodeManager.getNodeById(
                session, nodeId, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node by id [{}]'.format(nodeId))

            raise TortugaException(exception=ex)

    def getNodeByIp(self, session: Session, ip: str) -> Node:
        """
        Get a node by ip
        """

        try:
            return self._nodeManager.getNodeByIp(session, ip)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node by ip [{}]'.format(ip))

            raise TortugaException(exception=ex)

    def deleteNode(self, session: Session, nodespec: str, force: bool = False):
        try:
            return self._nodeManager.deleteNode(
                session, nodespec, force=force)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error deleting nodespec [{}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def getProvisioningInfo(self, session: Session, nodeName: str):
        """ Get provisioning information for a node """
        try:
            return self._nodeManager.getProvisioningInfo(session, nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving provisioning info for'
                ' node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def updateNode(self, session: Session, name: str, updateNodeRequest: dict):
        try:
            return self._nodeManager.updateNode(
                session, name, updateNodeRequest)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error updating node [{0}]'.format(name))

            raise TortugaException(exception=ex)

    def updateNodeStatus(self, session: Session, name: str,
                         state: Optional[str] = None,
                         bootFrom: Optional[int] = None):
        try:
            return self._nodeManager.updateNodeStatus(
                session, name, state, bootFrom)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error updating status for node [{}]'.format(name))

            raise TortugaException(exception=ex)

    def transferNodes(self, session: Session,
                      dstSoftwareProfile: str,
                      *,
                      count: Optional[int] = None,
                      srcSoftwareProfile: Optional[str] = None,
                      bForce: Optional[bool] = False,
                      nodespec: Optional[str] = None) -> dict:
        try:
            if nodespec:
                return self._nodeManager.transferNode(
                    session, nodespec, dstSoftwareProfile, bForce=bForce
                )

            if srcSoftwareProfile is None:
                raise TypeError(
                    'transferNodes() missing required keyword-only'
                    ' argument: \'srcSoftwareProfile\'')

            return self._nodeManager.transferNodes(
                session, srcSoftwareProfile, dstSoftwareProfile, count,
                bForce=bForce
            )
        except TortugaException:
            raise
        except Exception as ex:
            if nodespec:
                excmsg = 'Fatal error transferring nodes matching nodespec'
                ' [{}]'.format(nodespec)
            else:
                excmsg = 'Fatal error transferring nodes'

            self.getLogger().exception(excmsg)

            raise TortugaException(exception=ex)

    def idleNode(self, session: Session, nodespec: str):
        try:
            return self._nodeManager.idleNode(session, nodespec)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error idling nodes matching nodespec [{}]'.format(
                    nodespec))

            raise TortugaException(exception=ex)

    def activateNode(self, session: Session, nodeName: str,
                     softwareProfileName: str):
        try:
            return self._nodeManager.activateNode(
                session, nodeName, softwareProfileName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error activating node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def startupNode(self, session: Session, nodespec: str,
                    remainingNodeList: Optional[list] = None,
                    bootMethod: Optional[str] = 'n') -> None:
        try:
            self._nodeManager.startupNode(
                session, nodespec,
                remainingNodeList=remainingNodeList or [],
                bootMethod=bootMethod)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error starting node(s) matching nodespec [{}]'.format(
                    nodespec))

            raise TortugaException(exception=ex)

    def shutdownNode(self, session: Session, nodespec: str,
                     bSoftShutdown: Optional[bool] = False) -> None:
        try:
            self._nodeManager.shutdownNode(
                session, nodespec, bSoftShutdown)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error shutting down node(s) matching'
                ' nodespec [{0}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def rebootNode(self, session: Session, nodespec: str,
                   bSoftReset: Optional[bool] = True,
                   bReinstall: Optional[bool] = False) -> None:
        try:
            self._nodeManager.rebootNode(
                session, nodespec, bSoftReset=bSoftReset,
                bReinstall=bReinstall)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error rebooting node(s) matching'
                ' nodespec [{0}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def addStorageVolume(self, nodeName: str, volume: str,
                         isDirect: Optional[str] = 'DEFAULT'):
        try:
            return self._nodeManager.addStorageVolume(
                nodeName, volume, isDirect)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error adding storage volume to node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def removeStorageVolume(self, nodeName: str, volume: str):
        try:
            return self._nodeManager.removeStorageVolume(nodeName, volume)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error removing storage volume from node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def getStorageVolumeList(self, session: Session, nodeName: str):
        try:
            return self._nodeManager.getStorageVolumes(session, nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving storage volumes for node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def getNodesByNodeState(self, session: Session, state: str) \
            -> TortugaObjectList:
        """
        Get list of nodes in specified state

            Returns:
                list of nodes
            Throws:
                KitNotFound
                TortugaException
        """
        try:
            return self._nodeManager.getNodesByNodeState(session, state)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving nodes in state [{}]'.format(state))

            raise TortugaException(exception=ex)

    def getNodesByNameFilter(self, session: Session, nodespec: str,
                             optionDict: Optional[OptionDict] = None) \
            -> TortugaObjectList:
        try:
            return self._nodeManager.getNodesByNameFilter(
                session, nodespec, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving nodes by nodespec [{}]'.format(
                    nodespec))

            raise TortugaException(exception=ex)

    def getNodesByAddHostSession(self, session: Session,
                                 addHostSession: str,
                                 optionDict: Optional[OptionDict] = None) \
            -> TortugaObjectList:
        try:
            return self._nodeManager.getNodesByAddHostSession(
                session, addHostSession, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving nodes by add host session [{}]'.format(
                    addHostSession))

            raise TortugaException(exception=ex)
Exemple #2
0
class NodeApi(TortugaApi):
    """
    High-level API for performing node operations
    """
    def __init__(self):
        super(NodeApi, self).__init__()

        self._nodeManager = NodeManager()

    def createNewNode(self,
                      session: Session,
                      addNodeRequest: dict,
                      dbHardwareProfile: HardwareProfiles,
                      dbSoftwareProfile: Optional[SoftwareProfiles] = None,
                      validateIp: bool = True,
                      bGenerateIp: bool = True,
                      dns_zone: Optional[str] = None) -> Nodes:
        try:
            return self._nodeManager.createNewNode(
                session,
                addNodeRequest,
                dbHardwareProfile,
                dbSoftwareProfile=dbSoftwareProfile,
                validateIp=validateIp,
                bGenerateIp=bGenerateIp,
                dns_zone=dns_zone)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error creating new node')

            raise TortugaException(exception=ex)

    def getNodeList(self, tags: Optional[dict] = None):
        """
        Get node list..

            Returns:
                list of nodes
            Throws:
                TortugaException
        """
        try:
            return self._nodeManager.getNodeList(tags=tags)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error retrieving node list')

            raise TortugaException(exception=ex)

    def getNode(self, name: str, optionDict: Optional[dict] = None):
        """Get node id by name"""
        try:
            return self._nodeManager.getNode(name, optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node [{}]'.format(name))

            raise TortugaException(exception=ex)

    def getInstallerNode(self, optionDict: Optional[dict] = None):
        """Get installer node"""
        try:
            return self._nodeManager.getInstallerNode(optionDict=optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error retrieving installer node')

            raise TortugaException(exception=ex)

    def getNodeById(self, nodeId: int, optionDict: Optional[dict] = None):
        """Get a node by id"""
        try:
            return self._nodeManager.getNodeById(nodeId, optionDict)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node by id [{}]'.format(nodeId))

            raise TortugaException(exception=ex)

    def getNodeByIp(self, ip: str):
        """Get a node by ip"""
        try:
            return self._nodeManager.getNodeByIp(ip)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving node by ip [{}]'.format(ip))

            raise TortugaException(exception=ex)

    def deleteNode(self, nodespec: str):
        try:
            return self._nodeManager.deleteNode(nodespec)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error deleting nodespec [{}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def getMyNode(self):
        """ get a node entry of the current node """
        try:
            return self._nodeManager.getInstallerNode()
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error retrieving current node')

            raise TortugaException(exception=ex)

    def getKickstartFile(self,
                         node,
                         hardwareprofile=None,
                         softwareprofile=None):
        """ Get the kickstart file for a given node """

        try:
            return self._nodeManager.getKickstartFile(
                node,
                hardwareprofile=hardwareprofile,
                softwareprofile=softwareprofile)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving kickstart for node [{}]'.format(
                    node.name))

            raise TortugaException(exception=ex)

    def getProvisioningInfo(self, nodeName: str):
        """ Get provisioning information for a node """
        try:
            return self._nodeManager.getProvisioningInfo(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving provisioning info for'
                ' node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def updateNode(self, name: str, updateNodeRequest):
        try:
            return self._nodeManager.updateNode(name, updateNodeRequest)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error updating node [{0}]'.format(name))

            raise TortugaException(exception=ex)

    def updateNodeStatus(self,
                         name: str,
                         state: Optional[str] = None,
                         bootFrom: Optional[int] = None):
        try:
            return self._nodeManager.updateNodeStatus(name, state, bootFrom)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error updating status for node [{}]'.format(name))

            raise TortugaException(exception=ex)

    def _updateNodeStatus(self,
                          name: str,
                          state: Optional[str] = None,
                          bootFrom: Optional[int] = None) -> NoReturn:
        try:
            # pylint: disable=protected-access
            self._nodeManager._updateNodeStatus(name, state, bootFrom)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error updating status for node [{}]'.format(name))

            raise TortugaException(exception=ex)

    def transferNode(self,
                     nodespec: str,
                     softwareProfileName: str,
                     bForce: Optional[bool] = False):
        try:
            return self._nodeManager.transferNode(nodespec,
                                                  softwareProfileName,
                                                  bForce=bForce)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error transferring nodes matching nodespec'
                ' [{}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def transferNodes(self,
                      srcSoftwareProfile: str,
                      dstSoftwareProfile: str,
                      count: int,
                      bForce: Optional[bool] = False):
        try:
            return self._nodeManager.transferNodes(srcSoftwareProfile,
                                                   dstSoftwareProfile,
                                                   count,
                                                   bForce=bForce)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error transferring nodes')

            raise TortugaException(exception=ex)

    def idleNode(self, nodespec: str):
        try:
            return self._nodeManager.idleNode(nodespec)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error idling nodes matching nodespec [{}]'.format(
                    nodespec))

            raise TortugaException(exception=ex)

    def activateNode(self, nodeName: str, softwareProfileName: str):
        try:
            return self._nodeManager.activateNode(nodeName,
                                                  softwareProfileName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error activating node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def startupNode(self,
                    nodespec: str,
                    remainingNodeList: Optional[list] = None,
                    bootMethod: Optional[str] = 'n'):
        try:
            self._nodeManager.startupNode(nodespec,
                                          remainingNodeList=remainingNodeList
                                          or [],
                                          bootMethod=bootMethod)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error starting node(s) matching nodespec [{}]'.format(
                    nodespec))

            raise TortugaException(exception=ex)

    def shutdownNode(self,
                     nodespec: str,
                     bSoftShutdown: Optional[bool] = False):
        try:
            self._nodeManager.shutdownNode(nodespec, bSoftShutdown)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error shutting down node(s) matching'
                ' nodespec [{0}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def rebootNode(self,
                   nodespec: str,
                   bSoftReset: Optional[bool] = True,
                   bReinstall: Optional[bool] = False):
        try:
            self._nodeManager.rebootNode(nodespec,
                                         bSoftReset=bSoftReset,
                                         bReinstall=bReinstall)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('Fatal error rebooting node(s) matching'
                                       ' nodespec [{0}]'.format(nodespec))

            raise TortugaException(exception=ex)

    def checkpointNode(self, nodeName: str):
        try:
            self._nodeManager.checkpointNode(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error checkpointing node [{0}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def revertNodeToCheckpoint(self, nodeName: str):
        try:
            self._nodeManager.revertNodeToCheckpoint(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error reverting node [{}] to checkpoint'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def migrateNode(self, nodeName: str, remainingNodeList: list,
                    liveMigrate: bool):
        try:
            self._nodeManager.migrateNode(nodeName, remainingNodeList,
                                          liveMigrate)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error migrating node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def evacuateChildren(self, nodeName: str):
        try:
            self._nodeManager.evacuateChildren(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error evacuating child node [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def getChildrenList(self, nodeName: str):
        try:
            return self._nodeManager.getChildrenList(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving child node list')

            raise TortugaException(exception=ex)

    def setParentNode(self, nodeName: str, parentNodeName: str) -> NoReturn:
        try:
            self._nodeManager.setParentNode(nodeName, parentNodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error setting parent node of [{}]'.format(nodeName))

            raise TortugaException(exception=ex)

    def addStorageVolume(self,
                         nodeName: str,
                         volume: str,
                         isDirect: Optional[str] = 'DEFAULT'):
        try:
            if isDirect == 'DEFAULT':
                return self._nodeManager.addStorageVolume(nodeName, volume)

            return self._nodeManager.addStorageVolume(nodeName, volume,
                                                      isDirect)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error adding storage volume to node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def removeStorageVolume(self, nodeName: str, volume: str):
        try:
            return self._nodeManager.removeStorageVolume(nodeName, volume)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error removing storage volume from node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def getStorageVolumeList(self, nodeName: str):
        try:
            return self._nodeManager.getStorageVolumes(nodeName)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving storage volumes for node [{}]'.format(
                    nodeName))

            raise TortugaException(exception=ex)

    def getNodesByNodeState(self, state: str):
        """
        Get list of nodes in specified state

            Returns:
                list of nodes
            Throws:
                KitNotFound
                TortugaException
        """
        try:
            return self._nodeManager.getNodesByNodeState(state)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception(
                'Fatal error retrieving nodes in state [{}]'.format(state))

            raise TortugaException(exception=ex)