Skip to content
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

Use subcommands for lock,release,check #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pgagne
Copy link

@pgagne pgagne commented Aug 8, 2022

Part of #19

made use of argparse.subparser to implement subcommands. Also various related changed detailed below.

So locking a resource aws-resource-1 with signoff ptest1 and priortity 3 now looks like this :

rlock  -s <url> -t <token> lock aws-resource-1 ptest1 3

my hope is in the future we can get rid of requiring -s and -t with some sort of config file.

Details

Arguments that are required by certain things (signoff, search string, etc) are now positional args
This makes it easy to determine if they've been specified and error out if not. Eliminating common
user issues. Server args --server-url form, but that should change if we start using a config file or something.

Added defaults for --interval and --attempts based on the ones in ResourceLocker class

Added short form for most common params (-s, -t, etc)

Here is what --help looks like:

$ rlock --help
usage: rlock [-h] -s SERVER_URL -t TOKEN [--resume-on-connection-error] [-i INTERVAL] {release,check,lock} ...

positional arguments:
  {release,check,lock}
    release             Release a resource
    check               check a resource
    lock                Lock a resource

options:
  -h, --help            show this help message and exit
  -s SERVER_URL, --server-url SERVER_URL
                        The URL of the Resource Locker Server
  -t TOKEN, --token TOKEN
                        Token of the user that creates API calls
  --resume-on-connection-error
                        Use this argument in case you don't want to break queue execution in the middle of waiting for queue status being FINISHED
  -i INTERVAL, --interval INTERVAL
                        How many seconds to wait between each call (default 15)

$ rlock lock --help
usage: rlock lock [-h] [-a ATTEMPTS] [-l LINK] SEARCH SIGN_OFF PRIORITY

positional arguments:
  SEARCH                Specify the label or the name of the lockable resource
  SIGN_OFF              locking or releasing a resource requires signoff
  PRIORITY              Specify the level of priority the resource should be locked

options:
  -h, --help            show this help message and exit
  -a ATTEMPTS, --attempts ATTEMPTS
                        How many times to create an API call that will check for a free resource (default 120)
  -l LINK, --link LINK  Specify the link of the CI/CD pipeline that locks the resource
$ rlock check --help
usage: rlock check [-h] SEARCH

positional arguments:
  SEARCH      Specify the label or the name of the lockable resource

options:
  -h, --help  show this help message and exit
$ rlock release --help
usage: rlock release [-h] SIGN_OFF

positional arguments:
  SIGN_OFF    locking or releasing a resource requires signoff

options:
  -h, --help  show this help message and exit

@jimdevops19 let me know what you think

Part of red-hat-storage#19

made use of [argparse.subparser](https://docs.python.org/3/library/argparse.html#sub-commands) to implement subcommands. ie:

`rlock lock [args]` instead of `rlock --lock --arg1 --arg2`

Arguments that are required by certain things (signoff, search string, etc) are now positional args
This makes it easy to determine if they've been specified and error out if not. Eliminating common
user issues. Server args `--server-url` form, but that should change if we start using a config file or something.

Added defaults for `--interval` and `--attempts` based on the ones in `ResourceLocker` class

Added short form for most common params (`-s`, `-t`, etc)
@pgagne
Copy link
Author

pgagne commented Aug 9, 2022

@LNST-project Please review as well if you can.

@jtluka
Copy link

jtluka commented Aug 10, 2022

Could you add some additional description of what SIGN_OFF actually means? Is it an arbitrary string that is used in release after locking a resource? The current description that it is required is obvious, since it's mandatory parameter.

@pgagne
Copy link
Author

pgagne commented Aug 10, 2022

Could you add some additional description of what SIGN_OFF actually means? Is it an arbitrary string that is used in release after locking a resource? The current description that it is required is obvious, since it's mandatory parameter.

Yeah i kinda just copied what was there before. I tryed to come up with one but they where all too long.

@jimdevops19 Do you have a short phrase to describe what the signoff is.

Copy link

@olichtne olichtne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just providing some comments from a quick look...

"--resume-on-connection-error", help="Use this argument in case you don't want to break queue execution"
" in the middle of waiting for queue status being FINISHED", action="store_true"

# Parent of all sub commands that take a signoff string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is doubled and both times in the incorrect place, i think you wanted to say that the parser is the parent of all subcommands, but the comment is located at the search_parser and at the signoff_parser instead.


Returns:
object: Parsed arguments - returned from parser.parse_args()
"""
server_args_parser = ArgumentParser(add_help=False)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think these arguments since they're shared by all of the sub-commands and relate to the generic configuration of the server, they should just be added to the parser instead of sharing them via parents to all the individual sub-parsers.

I think that results in the difference of:

./my_cli --server-opt  ACTION --action-opt

vs.

./my_cli ACTION --server-opt --action-opt

but i'm not sure... i haven't really used this pattern before. Based on the linked stackoverflow questions I think the parents way of sharing options is meant for sub-commands where some share the options and some don't, these options however look like they're always optional for all sub-commands...

though i guess that's personal preference...

Copy link
Author

@pgagne pgagne Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I went back and forth a few times on this, I think i ran into an issue adding them to parser and decided to switch back to the way it is now. It might be possible I just didn't have a chance to fully debug it and wanted to get something out.

I my opinion though things like --server and --token really should be coming from some sort of config file so you dont have to type them everytime. We still want them to be options but I think it gets kinda unwieldy having them be part of the command line all the time.

parser.add_argument(
# TODO These really should be config file things,
server_args_parser.add_argument(
"-s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit concerned about having one letter shortcut for possibly two reasons (maybe su could be a better option):

1 ) Maybe we might want to save the -s for shortening the signoff which is already a mandatory argument

2 ) Maybe some other arguments in the future that will be single-worded and start with an s as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would -u for url be better?

Also I think it might be okay to not shorten it, but i do think we should add support for some sort of config file so you dont need to enter it (and the token, etc) all the time. then we could just have it be --server

I was curious does your team commonly deal with multiple resource locker instances in production?

@pgagne pgagne marked this pull request as ready for review August 18, 2022 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants