def mangle(from_server, to_server, commit): db = Factory.get('Database')() et = Factory.get('EmailTarget')(db) db.cl_init(change_program='update_email_target_server') # Yes yes yes, it is quite pretty es = EmailServer(db) es.clear() es.find_by_name(from_server) from_server_id = es.entity_id es.clear() es.find_by_name(to_server) to_server_id = es.entity_id for row in et.list_email_server_targets(): if row.has_key('server_id') and row['server_id'] == from_server_id: et.clear() et.find(row['target_id']) old_sid = et.email_server_id et.email_server_id = to_server_id et.write_db() print('Moved %d from %d to %d' % \ (et.entity_id, old_sid, to_server_id)) if commit: db.commit() print 'Committed all changes' else: db.rollback() print 'Rolled back all changes'
def process_servers(server_type, except_re): es = EmailServer(db) eq = EmailQuota(db) existing_servers = {} # This lists all hosts, so we need to filter on server_type later. for row in es.list_traits(co.trait_email_server_weight): existing_servers[int(row['entity_id'])] = True assigned = {} for row in es.list_email_server_ext(server_type=server_type): # logger.debug("Processing %r" % row.dict()) if except_re and except_re.match(row['name']): logger.debug("Skipping server named '%s'" % row['name']) continue srv = int(row['server_id']) assigned[srv] = eq.get_quota_stats_by_server(srv)['total_quota'] or 0 logger.debug("%s has assigned quota %d" % (row['name'], assigned[srv])) max_weight = max(assigned.values()) * 110 / 100 for srv in assigned: es.clear() es.find(srv) # We add 1 to handle the case with only one server weight = max_weight - assigned[srv] + 1 logger.debug("Assigning weight %d to server ID %d" % (weight, srv)) es.populate_trait(co.trait_email_server_weight, numval=weight) es.write_db() if srv in existing_servers: del existing_servers[srv] for obsolete in existing_servers: es.clear() es.find(obsolete) if server_type and es.email_server_type != server_type: continue logger.info("Deleting old weight trait for %s" % es.name) es.delete_trait(co.trait_email_server_weight)