def main(): """Parses arguments and launch service. """ parser = argparse.ArgumentParser( description="Resource monitor and service starter for CMS.") parser.add_argument("-a", "--autorestart", metavar="CONTEST_ID", help="restart automatically services on its machine", nargs="?", type=int, const=-1) parser.add_argument("shard", type=int, nargs="?", default=-1) args = parser.parse_args() # If the shard is -1 (i.e., unspecified) we find it basing on the # local IP addresses if args.shard == -1: addrs = find_local_addresses() args.shard = get_shard_from_addresses("ResourceService", addrs) if args.autorestart is not None: if args.autorestart == -1: ResourceService(args.shard, contest_id=ask_for_contest()).run() else: if is_contest_id(args.autorestart): ResourceService(args.shard, contest_id=args.autorestart).run() else: import sys print >> sys.stderr, "There is no contest " \ "with the specified id. Please try again." sys.exit(1) else: ResourceService(args.shard).run()
def default_argument_parser(description, cls, ask_contest=None): """Default argument parser for services - in two versions: needing a contest_id, or not. description (string): description of the service. cls (class): service's class. ask_contest (function): None if the service does not require a contest, otherwise a function that returns a contest_id (after asking the admins?) return (object): an instance of a service. """ parser = ArgumentParser(description=description) parser.add_argument("shard", nargs="?", type=int, default=-1) # We need to allow using the switch "-c" also for services that do # not need the contest_id because RS needs to be able to restart # everything without knowing which is which. contest_id_help = "id of the contest to automatically load" if ask_contest is None: contest_id_help += " (ignored)" parser.add_argument("-c", "--contest-id", help=contest_id_help, nargs="?", type=int) args = parser.parse_args() # If the shard is -1 (i.e., unspecified) we find it basing on the # local IP addresses if args.shard == -1: addrs = find_local_addresses() args.shard = get_shard_from_addresses(cls.__name__, addrs) if ask_contest is not None: if args.contest_id is not None: # Test if there is a contest with the given contest id. from cms.db.SQLAlchemyAll import Contest, SessionGen with SessionGen(commit=False) as session: contest = session.query(Contest).\ filter_by(id=args.contest_id).first() if contest is None: print >> sys.stderr, "There is no contest " \ "with the specified id. Please try again." sys.exit(1) return cls(args.shard, args.contest_id) else: return cls(args.shard, ask_contest()) else: return cls(args.shard)
def default_argument_parser(description, cls, ask_contest=None): """Default argument parser for services - in two versions: needing a contest_id, or not. description (string): description of the service. cls (class): service's class. ask_contest (function): None if the service does not require a contest, otherwise a function that returns a contest_id (after asking the admins?) return (object): an instance of a service. """ parser = ArgumentParser(description=description) parser.add_argument("shard", nargs="?", type=int, default=-1) # We need to allow using the switch "-c" also for services that do # not need the contest_id because RS needs to be able to restart # everything without knowing which is which. contest_id_help = "id of the contest to automatically load" if ask_contest is None: contest_id_help += " (ignored)" parser.add_argument("-c", "--contest-id", help=contest_id_help, nargs="?", type=int) args = parser.parse_args() # If the shard is -1 (i.e., unspecified) we find it basing on the # local IP addresses if args.shard == -1: addrs = find_local_addresses() args.shard = get_shard_from_addresses(cls.__name__, addrs) if ask_contest is not None: if args.contest_id is not None: return cls(args.shard, args.contest_id) else: return cls(args.shard, ask_contest()) else: return cls(args.shard)