def __init__(self, **kwargs): ConfigDataBackend.__init__(self, **kwargs) self._name = 'opsipxeconfd' self._port = u'/var/run/opsipxeconfd/opsipxeconfd.socket' self._timeout = 10 self._depotId = forceHostId(getfqdn()) self._opsiHostKey = None self._depotConnections = {} self._updateThreads = {} self._updateThreadsLock = threading.Lock() self._parseArguments(kwargs)
def productProperty_updateObject(self, productProperty): if not self._sqlBackendModule: raise Exception(u"SQL backend module disabled") ConfigDataBackend.productProperty_updateObject(self, productProperty) data = self._objectToDatabaseHash(productProperty) where = self._uniqueCondition(productProperty) possibleValues = data['possibleValues'] defaultValues = data['defaultValues'] if possibleValues is None: possibleValues = [] if defaultValues is None: defaultValues = [] del data['possibleValues'] del data['defaultValues'] self._sql.update('PRODUCT_PROPERTY', where, data) if possibleValues is not None: self._sql.delete('PRODUCT_PROPERTY_VALUE', where) for value in possibleValues: try: self._sql.doCommit = False logger.debug2(u'doCommit set to false') valuesExist = self._sql.getRow( u"select * from \"PRODUCT_PROPERTY_VALUE\" where " u"\"propertyId\" = '{0}' AND \"productId\" = '{1}' AND " u"\"productVersion\" = '{2}' AND \"packageVersion\" = '{3}' " u"AND \"value\" = '{4}' AND \"isDefault\" = {5}".format( data['propertyId'], data['productId'], str(data['productVersion']), str(data['packageVersion']), value, str(value in defaultValues) ) ) if not valuesExist: self._sql.doCommit = True logger.debug2(u'doCommit set to true') self._sql.insert('PRODUCT_PROPERTY_VALUE', { 'productId': data['productId'], 'productVersion': data['productVersion'], 'packageVersion': data['packageVersion'], 'propertyId': data['propertyId'], 'value': value, 'isDefault': (value in defaultValues) } ) finally: self._sql.doCommit = True logger.debug2(u'doCommit set to true')
def __init__(self, **kwargs): self._name = 'dhcpd' ConfigDataBackend.__init__(self, **kwargs) self._dhcpdConfigFile = System.Posix.locateDHCPDConfig( u'/etc/dhcp3/dhcpd.conf') self._reloadConfigCommand = '/usr/bin/sudo {command}'.format( command=System.Posix.getDHCPDRestartCommand( default='/etc/init.d/dhcp3-server restart')) self._fixedAddressFormat = u'IP' self._defaultClientParameters = { 'next-server': socket.gethostbyname(getfqdn()), 'filename': u'linux/pxelinux.0' } self._dhcpdOnDepot = False # Parse arguments for (option, value) in kwargs.items(): option = option.lower() if option == 'dhcpdconfigfile': self._dhcpdConfigFile = value elif option == 'reloadconfigcommand': self._reloadConfigCommand = value elif option == 'defaultclientparameters': self._defaultClientParameters = forceDict(value) elif option == 'fixedaddressformat': if value not in (u'IP', u'FQDN'): raise BackendBadValueError( u"Bad value '%s' for fixedAddressFormat, possible values are %s" % (value, u', '.join(('IP', 'FQDN')))) self._fixedAddressFormat = value elif option == 'dhcpdondepot': self._dhcpdOnDepot = forceBool(value) if self._defaultClientParameters.get( 'next-server' ) and self._defaultClientParameters['next-server'].startswith(u'127'): raise BackendBadValueError( u"Refusing to use ip address '%s' as default next-server" % self._defaultClientParameters['next-server']) self._dhcpdConfFile = DHCPDConfFile(self._dhcpdConfigFile) self._reloadEvent = threading.Event() self._reloadEvent.set() self._reloadLock = threading.Lock() self._reloadThread = None self._depotId = forceHostId(getfqdn()) self._opsiHostKey = None self._depotConnections = {}
def productProperty_updateObject(self, productProperty): self._requiresEnabledSQLBackendModule() ConfigDataBackend.productProperty_updateObject(self, productProperty) data = self._objectToDatabaseHash(productProperty) where = self._uniqueCondition(productProperty) possibleValues = data.pop('possibleValues') or [] defaultValues = data.pop('defaultValues') or [] self._sql.update('PRODUCT_PROPERTY', where, data) try: self._sql.delete('PRODUCT_PROPERTY_VALUE', where) except Exception as delError: logger.debug2(u"Failed to delete from PRODUCT_PROPERTY_VALUE: {}", delError) for value in possibleValues: with disableAutoCommit(self._sql): valuesExist = self._sql.getRow( u"select * from PRODUCT_PROPERTY_VALUE where " u"`propertyId` = '{0}' AND `productId` = '{1}' AND " u"`productVersion` = '{2}' AND `packageVersion` = '{3}' " u"AND `value` = '{4}' AND `isDefault` = {5}".format( data['propertyId'], data['productId'], str(data['productVersion']), str(data['packageVersion']), value, str(value in defaultValues) ) ) if not valuesExist: self._sql.autoCommit = True logger.debug2(u'autoCommit set to True') self._sql.insert('PRODUCT_PROPERTY_VALUE', { 'productId': data['productId'], 'productVersion': data['productVersion'], 'packageVersion': data['packageVersion'], 'propertyId': data['propertyId'], 'value': value, 'isDefault': bool(value in defaultValues) } )
def __init__(self, **kwargs): # pylint: disable=super-init-not-called ConfigDataBackend.__init__(self, **kwargs) self._workBackend = None self._masterBackend = None self._snapshotBackend = None self._clientId = None self._depotId = None self._backendChangeListeners = [] for (option, value) in kwargs.items(): option = option.lower() if option == 'workbackend': self._workBackend = value elif option == 'snapshotbackend': self._snapshotBackend = value elif option == 'masterbackend': self._masterBackend = value elif option == 'clientid': self._clientId = forceHostId(value) elif option == 'depotid': self._depotId = forceHostId(value) elif option == 'backendinfo': self._backendInfo = value if not self._workBackend: raise BackendConfigurationError("Work backend undefined") if not self._snapshotBackend: raise BackendConfigurationError("Snapshot backend undefined") if not self._clientId: raise BackendConfigurationError("Client id undefined") if not self._depotId: raise BackendConfigurationError("Depot id undefined") self._workBackend._setContext(self) self._backend = self._workBackend self._createInstanceMethods()
def productProperty_insertObject(self, productProperty): ConfigDataBackend.productProperty_insertObject(self, productProperty) data = self._objectToDatabaseHash(productProperty) possibleValues = data['possibleValues'] defaultValues = data['defaultValues'] if possibleValues is None: possibleValues = [] if defaultValues is None: defaultValues = [] del data['possibleValues'] del data['defaultValues'] where = self._uniqueCondition(productProperty) if self._sql.getRow('select * from "PRODUCT_PROPERTY" where %s' % where): self._sql.update("PRODUCT_PROPERTY", where, data, updateWhereNone = True) else: self._sql.insert("PRODUCT_PROPERTY", data) if possibleValues is not None: (conn, cursor) = self._sql.connect() myTransactionSuccess = False myMaxRetryTransaction = 10 myRetryTransactionCounter = 0 while (not myTransactionSuccess) and (myRetryTransactionCounter < myMaxRetryTransaction): try: myRetryTransactionCounter += 1 # transaction cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE") self._sql.doCommit = False # conn.begin() logger.debug2(u'Start Transaction: delete from ppv %d' % myRetryTransactionCounter) self._sql.delete('PRODUCT_PROPERTY_VALUE', where, conn, cursor) conn.commit() myTransactionSuccess = True except Exception as e: logger.debug(u"Execute error: %s" % e) if e.args[0] == 1213: # 1213: 'Deadlock found when trying to get lock; try restarting transaction' # 1213: May be table locked because of concurrent access - retrying myTransactionSuccess = False if myRetryTransactionCounter >= myMaxRetryTransaction: logger.error(u'Table locked (Code 2013) - giving up after %d retries' % myRetryTransactionCounter) raise else: logger.notice(u'Table locked (Code 2013) - restarting Transaction') time.sleep(0.1) else: logger.error(u'Unknown DB Error: %s' % str(e)) raise logger.debug2(u'End Transaction') self._sql.doCommit = True logger.debug2(u'doCommit set to true') self._sql.close(conn, cursor) (conn, cursor) = self._sql.connect() for value in possibleValues: try: # transform arguments for sql # from uniqueCondition if (value in defaultValues): myPPVdefault = u'"isDefault" = \'true\'' else: myPPVdefault = u'"isDefault" = \'false\'' if type(value) is bool: if value: myPPVvalue = u'"value" = \'true\'' else: myPPVvalue = u'"value" = \'false\'' elif type(value) in (float, long, int): myPPVvalue = u'"value" = %s' % (value) else: myPPVvalue = u"\"value\" = '%s'" % (self._sql.escapeApostrophe(self._sql.escapeBackslash(value))) myPPVselect = ( u"select * from \"PRODUCT_PROPERTY_VALUE\" where " u"\"propertyId\" = '{0}' AND \"productId\" = '{1}' AND " u"\"productVersion\" = '{2}' AND " u"\"packageVersion\" = '{3}' AND {4} AND {5}".format( data['propertyId'], data['productId'], str(data['productVersion']), str(data['packageVersion']), myPPVvalue, myPPVdefault ) ) myTransactionSuccess = False myMaxRetryTransaction = 10 myRetryTransactionCounter = 0 while (not myTransactionSuccess) and (myRetryTransactionCounter < myMaxRetryTransaction): try: myRetryTransactionCounter += 1 # transaction cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE") self._sql.doCommit = False # conn.begin() logger.notice(u'Start Transaction: insert to ppv %d' % myRetryTransactionCounter) if not self._sql.getRow(myPPVselect , conn, cursor): # self._sql.doCommit = True logger.notice(u'doCommit set to true') self._sql.insert('PRODUCT_PROPERTY_VALUE', { 'productId': data['productId'], 'productVersion': data['productVersion'], 'packageVersion': data['packageVersion'], 'propertyId': data['propertyId'], 'value': value, 'isDefault': (value in defaultValues) }, conn, cursor) conn.commit() else: conn.rollback() myTransactionSuccess = True except Exception as e: logger.debug(u"Execute error: %s" % e) if e.args[0] == 1213: # 1213: 'Deadlock found when trying to get lock; try restarting transaction' # 1213: May be table locked because of concurrent access - retrying myTransactionSuccess = False if myRetryTransactionCounter >= myMaxRetryTransaction: logger.error(u'Table locked (Code 2013) - giving up after %d retries' % myRetryTransactionCounter) raise else: logger.notice(u'Table locked (Code 2013) - restarting Transaction') time.sleep(0.1) else: logger.error(u'Unknown DB Error: %s' % str(e)) raise logger.debug2(u'End Transaction') finally: self._sql.doCommit = True logger.debug2(u'doCommit set to true') self._sql.close(conn,cursor) def productProperty_updateObject(self, productProperty): if not self._sqlBackendModule: raise Exception(u"SQL backend module disabled") ConfigDataBackend.productProperty_updateObject(self, productProperty) data = self._objectToDatabaseHash(productProperty) where = self._uniqueCondition(productProperty) possibleValues = data['possibleValues'] defaultValues = data['defaultValues'] if possibleValues is None: possibleValues = [] if defaultValues is None: defaultValues = [] del data['possibleValues'] del data['defaultValues'] self._sql.update('PRODUCT_PROPERTY', where, data) if possibleValues is not None: self._sql.delete('PRODUCT_PROPERTY_VALUE', where) for value in possibleValues: try: self._sql.doCommit = False logger.debug2(u'doCommit set to false') valuesExist = self._sql.getRow( u"select * from \"PRODUCT_PROPERTY_VALUE\" where " u"\"propertyId\" = '{0}' AND \"productId\" = '{1}' AND " u"\"productVersion\" = '{2}' AND \"packageVersion\" = '{3}' " u"AND \"value\" = '{4}' AND \"isDefault\" = {5}".format( data['propertyId'], data['productId'], str(data['productVersion']), str(data['packageVersion']), value, str(value in defaultValues) ) ) if not valuesExist: self._sql.doCommit = True logger.debug2(u'doCommit set to true') self._sql.insert('PRODUCT_PROPERTY_VALUE', { 'productId': data['productId'], 'productVersion': data['productVersion'], 'packageVersion': data['packageVersion'], 'propertyId': data['propertyId'], 'value': value, 'isDefault': (value in defaultValues) } ) finally: self._sql.doCommit = True logger.debug2(u'doCommit set to true')
def productProperty_insertObject(self, productProperty): self._requiresEnabledSQLBackendModule() ConfigDataBackend.productProperty_insertObject(self, productProperty) data = self._objectToDatabaseHash(productProperty) possibleValues = data.pop('possibleValues') or [] defaultValues = data.pop('defaultValues') or [] where = self._uniqueCondition(productProperty) if self._sql.getRow('select * from `PRODUCT_PROPERTY` where %s' % where): self._sql.update('PRODUCT_PROPERTY', where, data, updateWhereNone=True) else: self._sql.insert('PRODUCT_PROPERTY', data) with closingConnectionAndCursor(self._sql) as (conn, cursor): retries = 10 for retry in range(retries): try: # transaction cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE") with disableAutoCommit(self._sql): logger.debug2(u'Start Transaction: delete from ppv #{}', retry) conn.begin() self._sql.delete('PRODUCT_PROPERTY_VALUE', where, conn, cursor) conn.commit() logger.debug2(u'End Transaction') break except Exception as deleteError: logger.debug(u"Execute error: {}", deleteError) if deleteError.args[0] == DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE: logger.debug( u'Table locked (Code {}) - restarting Transaction', DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE ) time.sleep(0.1) else: logger.error(u'Unknown DB Error: {!r}', deleteError) raise else: errorMessage = u'Table locked (Code {}) - giving up after {} retries'.format( DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE, retries ) logger.error(errorMessage) raise BackendUnaccomplishableError(errorMessage) with closingConnectionAndCursor(self._sql) as (conn, cursor): for value in possibleValues: # transform arguments for sql # from uniqueCondition if value in defaultValues: myPPVdefault = u"`isDefault` = 1" else: myPPVdefault = u"`isDefault` = 0" if isinstance(value, bool): if value: myPPVvalue = u"`value` = 1" else: myPPVvalue = u"`value` = 0" elif isinstance(value, (float, long, int)): myPPVvalue = u"`value` = %s" % (value) else: myPPVvalue = u"`value` = '%s'" % (self._sql.escapeApostrophe(self._sql.escapeBackslash(value))) myPPVselect = ( u"select * from PRODUCT_PROPERTY_VALUE where " u"`propertyId` = '{0}' AND `productId` = '{1}' AND " u"`productVersion` = '{2}' AND " u"`packageVersion` = '{3}' AND {4} AND {5}".format( data['propertyId'], data['productId'], str(data['productVersion']), str(data['packageVersion']), myPPVvalue, myPPVdefault ) ) retries = 10 for retry in range(retries): try: # transaction cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE") with disableAutoCommit(self._sql): logger.debug2(u'Start Transaction: insert to ppv #{}', retry) conn.begin() if not self._sql.getRow(myPPVselect, conn, cursor): self._sql.insert('PRODUCT_PROPERTY_VALUE', { 'productId': data['productId'], 'productVersion': data['productVersion'], 'packageVersion': data['packageVersion'], 'propertyId': data['propertyId'], 'value': value, 'isDefault': bool(value in defaultValues) }, conn, cursor) conn.commit() else: conn.rollback() logger.debug2(u'End Transaction') break except Exception as insertError: logger.debug(u"Execute error: {!r}", insertError) if insertError.args[0] == DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE: # 1213: May be table locked because of concurrent access - retrying logger.notice( u'Table locked (Code {}) - restarting Transaction'.format( DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE ) ) time.sleep(0.1) else: logger.error(u'Unknown DB Error: {!r}', insertError) raise else: errorMessage = u'Table locked (Code {}) - giving up after {} retries'.format( DEADLOCK_FOUND_WHEN_TRYING_TO_GET_LOCK_ERROR_CODE, retries ) logger.error(errorMessage) raise BackendUnaccomplishableError(errorMessage)