Skip to content
Snippets Groups Projects

Various improvements

Merged Ben Morrice requested to merge v2 into master
1 file
+ 46
19
Compare changes
  • Side-by-side
  • Inline
@@ -82,17 +82,35 @@ def do_execute(cmd, tdir="/tmp", return_output=False, return_error=False):
def get_changelog(token, checksum):
"""get the changelog for a rpm, which requires downloading it"""
url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download"
result = call_api(token, url, stream=True)
# We don't usually retry stuff from api calls as the script runs
# frequently, however - in this case a failure results in a blank
# changelog which is not very useful
attempts = 0
max_attempts = 3
rpm_downloaded = False
temp_file = generate_temp_file("checksum")
# TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth
with open(temp_file, "wb") as f:
try:
for chunk in result.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
except:
f.close()
url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download"
while attempts < max_attempts:
attempts += 1
result = call_api(token, url, stream=True)
if result.status_code == 200:
# TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth
with open(temp_file, "wb") as f:
try:
for chunk in result.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
rpm_downloaded = True
except:
print(f"Saving changelog appeared to have failed. Retrying {attempts}/{max_attempts}", flush=True)
f.close()
if rpm_downloaded:
break
else:
print(f"API call failed, Retrying {attempts}/{max_attempts}", flush=True)
if not rpm_downloaded:
print(f"Changelog failed to download, exiting", flush=True)
sys.exit(1)
# TODO: maybe have some better logic instead of the head command
changelog = do_execute(f"rpm -qp {temp_file} --changelog | head -20", return_output=True)
# Even though we are running through a container, let's clean up after ourselves
@@ -115,7 +133,11 @@ def get_token(ot):
if result.status_code != 200:
print("unable to auth, exiting")
sys.exit(1)
token = json.loads(result.content)["access_token"]
try:
token = json.loads(result.content)["access_token"]
except json.decoder.JSONDecodeError:
print("unable to auth, exiting")
sys.exit(1)
return token
@@ -136,16 +158,25 @@ def get_upstream(token, feed):
packages = []
while True:
result = call_api(token, url, params={"limit": 100, "offset": offset})
j = json.loads(result.content)
try:
j = json.loads(result.content)
except json.decoder.JSONDecodeError:
# TODO: fix this?
print("Encountered an error retrieving upstream packages, exiting loop", flush=True)
return []
offset = j["pagination"]["offset"]
count = j["pagination"]["count"]
for p in j["body"]:
package = f"{p['name']}-{p['version']}-{p['release']}"
checksum = p["checksum"]
packages.append(f"{package},{checksum}")
package_arch = p["arch"]
if package_arch == arch:
packages.append(f"{package},{checksum}")
if count == 100:
offset += count
print(f"{feed}: incrementing offset to {offset}", flush=True)
else:
print(f"{feed}: up-to-date", flush=True)
break
return packages
@@ -223,7 +254,7 @@ def format_release(p, f, token):
response = requests.post(mattermost_integration_url, json=payload)
print(
f" -- Sent notification to Mattermost, response code was {response.status_code}", flush=True
f" -- Sent notification to Mattermost for package {newpackage}, response code was {response.status_code}", flush=True
)
print(f" -- Payload sent: {payload}", flush=True)
@@ -242,11 +273,7 @@ if __name__ == "__main__":
print(f"{len(differences)} new package(s) detected", flush=True)
for p in differences:
format_release(p, feed, token)
# In case there was an API fault
# don't overwrite the local packages if upstream is less than local
if upstream_packages > local_packages:
set_local(upstream_packages, feed)
set_local(upstream_packages, feed)
logfile.close()
if os.stat(logfilename).st_size == 0:
os.remove(logfilename)
Loading