From 90e718edb91cb883223b44cb0c856b8195fe32be Mon Sep 17 00:00:00 2001 From: Neill Cox Date: Mon, 17 Jun 2024 18:54:58 +1000 Subject: [PATCH] Initial commit --- .gitignore | 2 + README.md | 20 ++++++ list-rhel-images.py | 156 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 4 files changed, 180 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 list-rhel-images.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed7652c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/* +token diff --git a/README.md b/README.md new file mode 100644 index 0000000..c5c797f --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# List RHEL Images + +This is a script to list available RHEL images for download. + +Requires an offline token (see https://access.redhat.com/management/api) to generate one. + +./list-rhel-images.py --help +usage: list-rhel-images.py [-h] [-t TOKEN] [--token-file TOKEN_FILE] [-a ARCHITECTURE] [-v VERSION] [-f {json,table}] + +options: + -h, --help show this help message and exit + -t TOKEN, --token TOKEN + The offline token used to generate the bearer token + --token-file TOKEN_FILE + -a ARCHITECTURE, --architecture ARCHITECTURE + Architecture + -v VERSION, --version VERSION + Version + -f {json,table}, --format {json,table} + Output format, one of json or table diff --git a/list-rhel-images.py b/list-rhel-images.py new file mode 100755 index 0000000..13e4495 --- /dev/null +++ b/list-rhel-images.py @@ -0,0 +1,156 @@ +#!/bin/env python3 +""" +A script to list the available RHEL images available to download. + +Requires a token (see https://access.redhat.com/management/api to generate a token) + +By default, this will list all the available images for RHEL 8.4 on the x86_64 architecture in json format. + + + +""" +import argparse +import json +import sys + +from tabulate import tabulate +import requests + + +def parse_arguments(): + """Parse the command line arguments.""" + parser = argparse.ArgumentParser() + + parser.add_argument( + "-a", + "--architecture", + type=str, + default="x86_64", + help="Architecture. Default is x86_64", + ) + + # noinspection PyTypeChecker + parser.add_argument( + "-t", + "--token", + type=str, + help="".join( + [ + "The offline token used to generate the bearer token. " + "Either this or --token-file must be specified.", + ] + ), + ) + + parser.add_argument( + "--token-file", + type=open, + help="The file to read the token from. Either this or --token must be specified.", + ) + + parser.add_argument( + "-v", + "--version", + type=str, + default="8.4", + help="Version. Default is 8.4", + ) + + parser.add_argument( + "-f", + "--format", + choices=["json", "table"], + type=str, + default="json", + help="Output format, one of json or table. Default is json", + ) + + args = parser.parse_args() + + if not (args.token or args.token_file): + sys.stderr.write( + "Either --token-file or --token/-t must be supplied" + ) + sys.exit(1) + + if args.token_file: + args.token = args.token_file.read() + + return args + + +def main(): + """Main function.""" + args = parse_arguments() + req = requests.post( + "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token", + data={ + "grant_type": "refresh_token", + "client_id": "rhsm-api", + "refresh_token": args.token, + }, + timeout=120, + ) + token = req.json()["access_token"] + + req = requests.get( + "https://api.access.redhat.com/management/v1/images/rhel/8.4/x86_64", + headers={"Authorization": "Bearer " + token}, + timeout=120, + ) + + images = req.json()["body"] + + if args.format == "table": + table = [ + [ + "Name", + "Architecture", + "Checksum", + "URI", + "Filename", + "Date", + ] + ] + for i in images: + table.append( + [ + i["imageName"], + i["arch"], + i["checksum"], + i["downloadHref"], + i["filename"], + i["datePublished"], + ] + ) + print(tabulate(table, headers="firstrow")) + elif args.format == "json": + print(json.dumps(images, indent=4)) + + +if __name__ == "__main__": + main() + +# #!/bin/bash +# # set the offline token and checksum parameters +# offline_token="" +# checksum= +# +# # get an access token +# access_token=$(curl +# https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token +# -d grant_type=refresh_token +# -d client_id=rhsm-api +# -d refresh_token=$offline_token | jq -r '.access_token') +# +# # get the filename and download url +# image=$( +# curl +# -H "Authorization: Bearer $access_token" +# "https://api.access.redhat.com/management/v1/images/$checksum/download" +# ) +# filename=$(echo $image | jq -r .body.filename) +# url=$(echo $image | jq -r .body.href) +# +# # download the file +# curl $url -o $filename diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a64e7e1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests~=2.32.3 +tabulate~=0.9.0