Example #1
0
def work(interval):
    """
    This is where the work is done
    @param interval: The interval between two calls if there are no request
    """
    logger.debug('Worker Running!')
    loop = asyncio.get_event_loop()
    session = database.get_session()
    request = get_request()
    if request is None:
        loop.call_later(interval, work, interval)
        logger.debug('Waiting for Request, check again in %s seconds.' % interval)
        return False
    try:
        logger.info('Calculating Hash for: %s' % request)
        request = session.merge(request)
        fhash = calculate_hash(request.server.ip, request.server.port, request.server.id)

        file = File(name=fix_encoding(request.name), folder=request.folder, mtime=request.mtime,
                    size=request.size,
                    host=request.host, hash=fhash)
        session.add(file)
        session.delete(request.server)
        session.delete(request)
        session.commit()
    except Exception as e:
        logger.exception(e)
        session.rollback()

    loop.call_soon(work, interval)
    return True
Example #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