156 lines
3.7 KiB
Python
Executable file
156 lines
3.7 KiB
Python
Executable file
#!/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
|