Beispiel #1
0
def get_request():
    """
    @return: a random request guarantied to be locked and unique
    """
    session = database.get_session()
    me = Host.by_name(socket.gethostname(), session)
    if me.region is not None:
        # noinspection PyComparisonWithNone,PyPep8
        query = session.query(HashRequest).join(HashRequest.host).join(Host.region).filter(
            and_(HashRequest.locked == False, HashRequest.server != None,
                 Region.id == me.region.id)).order_by(func.random()).with_for_update()
    else:
        # noinspection PyComparisonWithNone,PyPep8
        query = session.query(HashRequest).join(HashRequest.host).filter(
            and_(HashRequest.locked == False, HashRequest.server != None,
                 HashRequest.host == me)).order_by(func.random()).with_for_update()
    try:
        request = query.first()
        if request is None:
            return None
        request.locked = True
        session.commit()
        return request
    except Exception as error:
        session.rollback()
        raise error

    finally:
        session.close()
Beispiel #2
0
def main(args=sys.argv[1:]):
    """
    main :)
    @param args: the args used for the parser, just useful for testing
    """
    parser = argparse.ArgumentParser(description='Configuration tool')
    parser.add_argument('-d', '--database', type=str, metavar='"Connection String"', required=True)
    parser.add_argument('-n', '--name', type=str, metavar='"HOSTNAME"', default=socket.gethostname())
    parser.add_argument('-r', '--region', type=str, metavar='"HOSTNAME"', default=None)
    parser.add_argument('--add', dest='add', type=str, metavar='PATH',
                        help='The directory you want to add to the Database', action='append')
    parser.add_argument('--remove', dest='remove', type=str, metavar='PATH',
                        help='The root you want to remove from the Database', action='append')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--create_schema', action='store_true')
    parser.add_argument('--clear_database', action='store_true')
    args = parser.parse_args(args)

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
        logger.debug('Configurator started with following arguments: %s' % args)
    else:
        logging.basicConfig(level=logging.INFO)

    database = create_engine(args.database, echo=False)

    if args.clear_database:
        Base.metadata.drop_all(database)
        database_logging.Base.metadata.drop_all(database)

    if args.create_schema or args.clear_database:
        Base.metadata.create_all(database)
        database_logging.Base.metadata.create_all(database)

    session = sessionmaker(bind=database)()
    database_logging.DBSession = sessionmaker(bind=database)()

    if args.region is not None:
        try:
            region = Region.by_name(name=args.region.lower(), session=session)
        except NoResultFound:
            region = Region.create_new(name=args.region.lower())
            session.add(region)
    else:
        region = None

    try:
        host = Host.by_name(args.name, session)
    except NoResultFound:
        logger.info('Host not found, creating new One')
        host = Host.create_new(name=args.name, region=region)
        session.add(host)

    if host.region != region:
        host.region = region

    if args.add is not None:
        for root_dir in args.add:
            root_dir = fix_encoding(root_dir)
            try:
                root = host.add_root(root_dir, session)
                session.add(root)
            except AttributeError as error:
                logger.error(error)

    if args.remove is not None:
        for root_dir in args.remove:
            root_dir = fix_encoding(root_dir)
            try:
                host.remove_root(root_dir, session)
            except AttributeError as error:
                logger.error(error)

    try:
        session.commit()
    except OperationalError as error:
        logger.error(error)
        session.rollback()
        return -1
    finally:
        session.close_all()
    return 0