-
-
Notifications
You must be signed in to change notification settings - Fork 275
feat: rename LG_TOKEN to LG_RESERVATION #1932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,22 @@ def __str__(self): | |
| return f"{self.message}:\n{errors_combined}" | ||
|
|
||
|
|
||
| def _get_reservation_id_from_env(): | ||
| reservation_id = os.environ.get("LG_RESERVATION") | ||
| legacy_reservation_id = os.environ.get("LG_TOKEN") | ||
| if reservation_id: | ||
| if legacy_reservation_id and legacy_reservation_id != reservation_id: | ||
| print( | ||
| "warning: LG_RESERVATION and deprecated LG_TOKEN are set to different values; using LG_RESERVATION", | ||
| file=sys.stderr, | ||
| ) | ||
| return reservation_id | ||
|
|
||
| if legacy_reservation_id: | ||
| print("warning: LG_TOKEN is deprecated; use LG_RESERVATION instead", file=sys.stderr) | ||
| return legacy_reservation_id | ||
|
|
||
|
|
||
| @attr.s(eq=False) | ||
| class ClientSession: | ||
| """The ClientSession encapsulates all the actions a Client can invoke on | ||
|
|
@@ -440,19 +456,19 @@ def _match_places(self, pattern): | |
| """ | ||
| result = set() | ||
|
|
||
| # reservation token lookup | ||
| token = None | ||
| # reservation id lookup | ||
| reservation_id = None | ||
| if pattern.startswith("+"): | ||
| token = pattern[1:] | ||
| if not token: | ||
| token = os.environ.get("LG_TOKEN", None) | ||
| if not token: | ||
| reservation_id = pattern[1:] | ||
| if not reservation_id: | ||
| reservation_id = _get_reservation_id_from_env() | ||
| if not reservation_id: | ||
| return [] | ||
| for name, place in self.places.items(): | ||
| if place.reservation == token: | ||
| if place.reservation == reservation_id: | ||
| result.add(name) | ||
| if not result: | ||
| raise UserError(f"reservation token {token} matches nothing") | ||
| raise UserError(f"reservation id {reservation_id} matches nothing") | ||
| return list(result) | ||
|
|
||
| # name and alias lookup | ||
|
|
@@ -1574,28 +1590,30 @@ async def create_reservation(self): | |
|
|
||
| res = Reservation.from_pb2(response.reservation) | ||
| if self.args.shell: | ||
| print(f"export LG_TOKEN={res.token}") | ||
| print(f"export LG_RESERVATION={res.id}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to export the old
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. Say I: Use eval Some old script/command is run to set Use labgrid-client
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could warn about that in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea! updated |
||
| # TODO: remove the deprecated LG_TOKEN export after one release. | ||
| print(f"export LG_TOKEN={res.id}") | ||
| else: | ||
| print(f"Reservation '{res.token}':") | ||
| print(f"Reservation '{res.id}':") | ||
| res.show(level=1) | ||
| if self.args.wait: | ||
| if not self.args.shell: | ||
| print("Waiting for allocation...") | ||
| await self._wait_reservation(res.token, verbose=False) | ||
| await self._wait_reservation(res.id, verbose=False) | ||
|
|
||
| async def cancel_reservation(self): | ||
| token: str = self.args.token | ||
| reservation_id: str = self.args.reservation_id | ||
|
|
||
| request = labgrid_coordinator_pb2.CancelReservationRequest(token=token) | ||
| request = labgrid_coordinator_pb2.CancelReservationRequest(token=reservation_id) | ||
|
|
||
| try: | ||
| await self.stub.CancelReservation(request) | ||
| except grpc.aio.AioRpcError as e: | ||
| raise ServerError(e.details()) | ||
|
|
||
| async def _wait_reservation(self, token: str, verbose=True): | ||
| async def _wait_reservation(self, reservation_id: str, verbose=True): | ||
| while True: | ||
| request = labgrid_coordinator_pb2.PollReservationRequest(token=token) | ||
| request = labgrid_coordinator_pb2.PollReservationRequest(token=reservation_id) | ||
|
|
||
| try: | ||
| response: labgrid_coordinator_pb2.PollReservationResponse = await self.stub.PollReservation(request) | ||
|
|
@@ -1611,8 +1629,8 @@ async def _wait_reservation(self, token: str, verbose=True): | |
| break | ||
|
|
||
| async def wait_reservation(self): | ||
| token = self.args.token | ||
| await self._wait_reservation(token) | ||
| reservation_id = self.args.reservation_id | ||
| await self._wait_reservation(reservation_id) | ||
|
|
||
| async def print_reservations(self): | ||
| request = labgrid_coordinator_pb2.GetReservationsRequest() | ||
|
|
@@ -1624,7 +1642,7 @@ async def print_reservations(self): | |
| raise ServerError(e.details()) | ||
|
|
||
| for res in sorted(reservations, key=lambda x: (-x.prio, x.created)): | ||
| print(f"Reservation '{res.token}':") | ||
| print(f"Reservation '{res.id}':") | ||
| res.show(level=1) | ||
|
|
||
| async def export(self, place, target): | ||
|
|
@@ -2212,11 +2230,23 @@ def get_parser(auto_doc_mode=False) -> "argparse.ArgumentParser | AutoProgramArg | |
| subparser.set_defaults(func=ClientSession.create_reservation) | ||
|
|
||
| subparser = subparsers.add_parser("cancel-reservation", help="cancel a reservation") | ||
| subparser.add_argument("token", type=str, nargs="?") | ||
| subparser.add_argument( | ||
| "reservation_id", | ||
| type=str, | ||
| nargs="?", | ||
| help="the reservation id (previously called token)", | ||
| metavar="reservation-id", | ||
| ) | ||
| subparser.set_defaults(func=ClientSession.cancel_reservation) | ||
|
|
||
| subparser = subparsers.add_parser("wait", help="wait for a reservation to be allocated") | ||
| subparser.add_argument("token", type=str, nargs="?") | ||
| subparser.add_argument( | ||
| "reservation_id", | ||
| type=str, | ||
| nargs="?", | ||
| help="the reservation id (previously called token)", | ||
| metavar="reservation-id", | ||
| ) | ||
| subparser.set_defaults(func=ClientSession.wait_reservation) | ||
|
|
||
| subparser = subparsers.add_parser("reservations", help="list current reservations") | ||
|
|
@@ -2259,7 +2289,6 @@ def main(): | |
| state = os.environ.get("STATE", None) | ||
| state = os.environ.get("LG_STATE", state) | ||
| initial_state = os.environ.get("LG_INITIAL_STATE", None) | ||
| token = os.environ.get("LG_TOKEN", None) | ||
|
|
||
| parser = get_parser() | ||
|
|
||
|
|
@@ -2283,11 +2312,12 @@ def main(): | |
| if args.initial_state is None: | ||
| args.initial_state = initial_state | ||
|
|
||
| if args.command in ["cancel-reservation", "wait"] and args.token is None: | ||
| if token: | ||
| args.token = token | ||
| if args.command in ["cancel-reservation", "wait"] and args.reservation_id is None: | ||
| reservation_id = _get_reservation_id_from_env() | ||
| if reservation_id: | ||
| args.reservation_id = reservation_id | ||
| else: | ||
| print("Please provide a token", file=sys.stderr) | ||
| print("Please provide a reservation id (previously called token)", file=sys.stderr) | ||
| exit(1) | ||
|
|
||
| if args.verbose: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section should export why we are forcing people to change to
LG_RESERVATIONin all workflows/scripts/CLI usage. IMO this is needed becauseLG_TOKENimplies a security property (which is not present here) since the meaning ofTOKENhas changed over the last few years. Additional explanation or links are welcome as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated