From e80247af07d49fc9b337c9dc3f66f4d83055f37c Mon Sep 17 00:00:00 2001 From: Markus Rosenstihl Date: Tue, 22 Nov 2022 13:30:59 +0100 Subject: [PATCH] reorganized AppImage build and upload scripts for GoCD build server; fixed zsync URL --- AppImageBuilder.yml | 2 +- create_AppImage.bash => tools/build.sh | 0 tools/upload_appimage.py | 97 ++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) rename create_AppImage.bash => tools/build.sh (100%) mode change 100644 => 100755 create mode 100755 tools/upload_appimage.py diff --git a/AppImageBuilder.yml b/AppImageBuilder.yml index 45405d5..aefd761 100644 --- a/AppImageBuilder.yml +++ b/AppImageBuilder.yml @@ -80,6 +80,6 @@ AppDir: command: ./AppRun AppImage: - update-information: 'zsync|http://mirror.infra.pkm/nmreval/NMReval-latest.AppImage.zsync' + update-information: 'zsync|http://mirror.infra.pkm/nmreval/NMReval-latest-x86_64.AppImage.zsync' sign-key: None arch: x86_64 diff --git a/create_AppImage.bash b/tools/build.sh old mode 100644 new mode 100755 similarity index 100% rename from create_AppImage.bash rename to tools/build.sh diff --git a/tools/upload_appimage.py b/tools/upload_appimage.py new file mode 100755 index 0000000..95ad471 --- /dev/null +++ b/tools/upload_appimage.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 + +# from: https://github.com/kneufeld/minio-put/blob/master/minio-put.py +import os +import sys +import glob +import argparse +import datetime +import urllib.request +import yaml +import requests + +def upload(key, secret, host, bucket, filename, dest_name=None): + """ + call this function when using this file as a library + date header must be relatively in sync with server or you + get forbidden error + returns True on success + raises on error + """ + base_name = os.path.basename(filename) + + try: + hostname, port = host.split(':') + except ValueError: + hostname = host + + if dest_name: + resource = f"/{bucket}/{dest_name}" + else: + resource = f"/{bucket}/{base_name}" + content_type = "application/octet-stream" + date = tznow().strftime('%a, %d %b %Y %X %z') + _signature = f"PUT\n\n{content_type}\n{date}\n{resource}" + signature = sig_hash(secret, _signature) + + url = f"http://{host}{resource}" + headers = { + 'Host': hostname, + 'Date': date, # eg. Sat, 03 Mar 2018 10:11:16 -0700 + 'Content-Type': content_type, + 'Authorization': f"AWS {key}:{signature}", + } + #import pprint + #pprint.pprint(headers) + #pprint.pprint(url) + filedata = open(filename, 'rb') + #req = urllib.request.Request(url, data=filedata, method='PUT', headers=headers) + #resp = urllib.request.urlopen(req) + req = requests.put(url, data=filedata, headers=headers) + print(req) + return req or True + +# based on: https://gist.github.com/heskyji/5167567b64cb92a910a3 +def sig_hash(secret, sig): + import hashlib + import hmac + import base64 + + # signature=`echo -en ${sig} | openssl sha1 -hmac ${secret} -binary | base64` + + secret = bytes(secret, 'UTF-8') + sig = bytes(sig, 'UTF-8') + + digester = hmac.new(secret, sig, hashlib.sha1) + signature1 = digester.digest() + signature2 = base64.standard_b64encode(signature1) + #signature2 = base64.urlsafe_b64encode(signature1) + + return str(signature2, 'UTF-8') + + +def tznow(): + def utc_to_local(utc_dt): + return utc_dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None) + + ts = datetime.datetime.utcnow() + return utc_to_local(ts) + + +if __name__ == "__main__": + key = os.environ["s3_key"] + secret = os.environ["s3_secret"] + host = "mirror.infra.pkm:9000" + bucket = "nmreval-appimage" + appimage_version = yaml.safe_load(open("AppImageBuilder.yml"))["AppDir"]["app_info"]["version"] + filenames = [f"NMReval-{appimage_version}-x86_64.AppImage", + f"NMReval-{appimage_version}-x86_64.AppImage.zsync", + ] + upload(key, secret, host, bucket, + f"NMReval-{appimage_version}-x86_64.AppImage") + upload(key, secret, host, bucket, + f"NMReval-{appimage_version}-x86_64.AppImage.zsync") + upload(key, secret, host, bucket, + f"NMReval-{appimage_version}-x86_64.AppImage.zsync", + f"NMReval-ldates-x86_64.AppImage.zsync") +