Exemple #1
0
 def _dhcpd_deleteHost(self, host):
     host = forceObjectClass(host, Host)
     if self._dhcpdOnDepot:
         for depot in self._context.host_getObjects(id=self._depotId):  # pylint: disable=maybe-no-member
             if depot.id != self._depotId:
                 self._getDepotConnection(depot.id).dhcpd_deleteHost(host)  # pylint: disable=maybe-no-member
     self.dhcpd_deleteHost(host)
def testForcingObjectClassFromJSONHasGoodErrorDescription():
    incompleteJson = {
        "clientId": "Nellie*",
        "actionRequest": "setup",
        "productType": "LocalbootProduct",
        "type": "ProductOnClient"
    }

    try:
        forceObjectClass(incompleteJson, ProductOnClient)
        pytest.fail("No error from incomplete json.")
    except ValueError as error:
        assert "Missing required argument(s): 'productId'" in str(error)

    incompleteJson['type'] = "NotValid"
    try:
        forceObjectClass(incompleteJson, ProductOnClient)
        pytest.fail("No error from invalid type.")
    except ValueError as error:
        assert "Invalild object type: NotValid" in str(error)
Exemple #3
0
    def _dhcpd_updateHost(self, host):
        host = forceObjectClass(host, Host)

        if self._dhcpdOnDepot:
            depotId = self._getResponsibleDepotId(host.id)  # pylint: disable=maybe-no-member
            if depotId != self._depotId:
                logger.info(
                    u"Not responsible for client '%s', forwarding request to depot '%s'"
                    % (host.id, depotId))  # pylint: disable=maybe-no-member
                return self._getDepotConnection(depotId).dhcpd_updateHost(host)  # pylint: disable=maybe-no-member
        self.dhcpd_updateHost(host)
def testForcingObjectClassFromProductOnClientJSON():
    json = {
        "clientId": "dolly.janus.vater",
        "actionRequest": "setup",
        "productType": "LocalbootProduct",
        "type": "ProductOnClient",
        "productId": "hoer_auf_deinen_vater"
    }

    poc = forceObjectClass(json, ProductOnClient)

    assert isinstance(poc, ProductOnClient)
Exemple #5
0
    def dhcpd_deleteHost(self, host):
        host = forceObjectClass(host, Host)

        with self._reloadLock:
            try:
                self._dhcpdConfFile.parse()
                hostname = _getHostname(host.id)
                if not self._dhcpdConfFile.getHost(hostname):  # pylint: disable=maybe-no-member
                    return
                self._dhcpdConfFile.deleteHost(hostname)  # pylint: disable=maybe-no-member
                self._dhcpdConfFile.generate()
            except Exception as error:
                logger.logException(error, logLevel=LOG_ERROR)

        self._triggerReload()
def testForceObjectClassFromHash(opsiClient, klass):
    assert isinstance(forceObjectClass(opsiClient.toHash(), klass), klass)
def testForceObjectClassToHostFromJSON(opsiClient, klass):
    assert isinstance(forceObjectClass(opsiClient.toJson(), klass), klass)
Exemple #8
0
    def dhcpd_updateHost(self, host):
        host = forceObjectClass(host, Host)

        if not host.hardwareAddress:  # pylint: disable=maybe-no-member
            logger.warning(
                u"Cannot update dhcpd configuration for client %s: hardware address unknown"
                % host)
            return

        hostname = _getHostname(host.id)  # pylint: disable=maybe-no-member

        ipAddress = host.ipAddress  # pylint: disable=maybe-no-member
        if not ipAddress:
            try:
                logger.info(
                    u"Ip addess of client {0} unknown, trying to get host by name",
                    host)
                ipAddress = socket.gethostbyname(host.id)  # pylint: disable=maybe-no-member
                logger.info(u"Client fqdn resolved to {0!r}", ipAddress)
            except Exception as error:
                logger.debug(u"Failed to get IP by hostname: {0}", error)
                with self._reloadLock:
                    self._dhcpdConfFile.parse()
                    currentHostParams = self._dhcpdConfFile.getHost(hostname)

                if currentHostParams:
                    logger.debug(
                        'Trying to use address for {0} from existing DHCP '
                        'configuration.', hostname)

                    if currentHostParams.get('fixed-address'):
                        ipAddress = currentHostParams['fixed-address']
                    else:
                        raise BackendIOError(
                            u"Cannot update dhcpd configuration for client {0}: ip address unknown and failed to get ip address from DHCP configuration file."
                            .format(host.id))  # pylint: disable=maybe-no-member
                else:
                    raise BackendIOError(
                        u"Cannot update dhcpd configuration for client {0}: ip address unknown and failed to get host by name"
                        .format(host.id))  # pylint: disable=maybe-no-member

        fixedAddress = ipAddress
        if self._fixedAddressFormat == 'FQDN':
            fixedAddress = host.id  # pylint: disable=maybe-no-member

        parameters = forceDict(self._defaultClientParameters)
        if not self._dhcpdOnDepot:
            try:
                depot = self._context.host_getObjects(
                    id=self._getResponsibleDepotId(host.id))[0]  # pylint: disable=maybe-no-member
                if depot.ipAddress:
                    parameters['next-server'] = depot.ipAddress
            except Exception as error:
                logger.error(u"Failed to get depot info: %s" % error)

        with self._reloadLock:
            try:
                self._dhcpdConfFile.parse()
                currentHostParams = self._dhcpdConfFile.getHost(hostname)
                if currentHostParams and (currentHostParams.get('hardware', ' ').split(' ')[1] == host.hardwareAddress) \
                 and (currentHostParams.get('fixed-address') == fixedAddress) \
                 and (currentHostParams.get('next-server') == parameters['next-server']):

                    logger.debug(
                        u"DHCPD config of host '%s' unchanged, no need to update config file"
                        % host)
                    return

                self._dhcpdConfFile.addHost(
                    hostname=hostname,
                    hardwareAddress=host.hardwareAddress,  # pylint: disable=maybe-no-member
                    ipAddress=ipAddress,
                    fixedAddress=fixedAddress,
                    parameters=parameters)
                self._dhcpdConfFile.generate()
            except Exception as error:
                logger.logException(error, logLevel=LOG_ERROR)

        self._triggerReload()