Initial commit

This commit is contained in:
Neill Cox 2024-06-17 18:54:58 +10:00
commit 90e718edb9
4 changed files with 180 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea/*
token

20
README.md Normal file
View file

@ -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

156
list-rhel-images.py Executable file
View file

@ -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="<offline_token>"
# checksum=<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

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
requests~=2.32.3
tabulate~=0.9.0