From cad539b2778861b33226f82ae69c21062bf3827a Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Tue, 1 Oct 2019 09:46:53 +0200 Subject: [PATCH] bpo: update api submit code for bpo rewrite (!654) Rename .sr.ht to .build.postmarketos.org, because the submit script is talking to the bpo server and may not even be using sourcehut if running with the local job service. Update the script to work with the new API. See the build.postmarketos.org git repository for details: https://gitlab.com/postmarketOS/build.postmarketos.org/ --- .../install_pmbootstrap.sh | 0 .build.postmarketos.org/submit.py | 82 +++++++++++++++++++ .sr.ht/submit.py | 74 ----------------- 3 files changed, 82 insertions(+), 74 deletions(-) rename {.sr.ht => .build.postmarketos.org}/install_pmbootstrap.sh (100%) create mode 100755 .build.postmarketos.org/submit.py delete mode 100755 .sr.ht/submit.py diff --git a/.sr.ht/install_pmbootstrap.sh b/.build.postmarketos.org/install_pmbootstrap.sh similarity index 100% rename from .sr.ht/install_pmbootstrap.sh rename to .build.postmarketos.org/install_pmbootstrap.sh diff --git a/.build.postmarketos.org/submit.py b/.build.postmarketos.org/submit.py new file mode 100755 index 000000000..0b364d529 --- /dev/null +++ b/.build.postmarketos.org/submit.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# Copyright 2019 Martijn Braam +# Copyright 2019 Oliver Smith +# SPDX-License-Identifier: GPL-3.0-or-later + +import json +import os +import requests + +# Require environment vars +for key in ["BPO_API_ENDPOINT", + "BPO_API_HOST", + "BPO_ARCH", + "BPO_BRANCH", + "BPO_TOKEN_FILE", + "BPO_PAYLOAD_FILES", # one file per line + "BPO_PAYLOAD_IS_JSON", # set to "1" to enable + "BPO_PKGNAME", + "BPO_PUSH_ID", + "BPO_VERSION", # $pkgver-r$pkgrel + ]: + if key not in os.environ: + print("ERROR: missing environment variable: " + key) + exit(1) + +# Parse and check files +files = os.environ["BPO_PAYLOAD_FILES"].split("\n") +for path in files: + if not os.path.exists(path): + print("ERROR: file not found: " + path) + exit(1) + +# Load token +with open(os.path.expanduser(os.environ["BPO_TOKEN_FILE"]), + encoding="utf-8") as handle: + token = handle.read().strip() + +# Load other env vars +url = (os.environ["BPO_API_HOST"] + "/api/job-callback/" + + os.environ["BPO_API_ENDPOINT"]) +is_json = (os.environ["BPO_PAYLOAD_IS_JSON"] == "1") + +# Prepare HTTP headers +headers = {"X-BPO-Arch": os.environ["BPO_ARCH"], + "X-BPO-Branch": os.environ["BPO_BRANCH"], + "X-BPO-Push-Id": os.environ["BPO_PUSH_ID"], + "X-BPO-Token": token, + "X-BPO-Pkgname": os.environ["BPO_PKGNAME"], + "X-BPO-Version": os.environ["BPO_VERSION"]} + +# Submit JSON +if is_json: + if len(files) > 1: + print("ERROR: json mode doesn't support multiple input files") + exit(1) + + # Send contents of file as HTTP POST with json payload + with open(files[0], encoding="utf-8") as handle: + data = handle.read() + data = json.loads(data) + + print("Sending JSON to: " + url) + response = requests.post(url, json=data, headers=headers) +else: # Submit blobs + blobs = [] + for path in files: + print("Appending: " + path) + filename = os.path.basename(path) + # Send contents of file as HTTP POST with multipart/formdata payload + blobs.append(("file[]", (filename, + open(path, "rb"), + "application/octet-stream"))) + + print("Uploading to: " + url) + response = requests.post(url, files=blobs, headers=headers) + +if response.status_code > 399: + print("Error occurred:") + print(response.content.decode()) + exit(1) +else: + print(response.content.decode()) diff --git a/.sr.ht/submit.py b/.sr.ht/submit.py deleted file mode 100755 index 5a152c986..000000000 --- a/.sr.ht/submit.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python3 -import os -import argparse -import json - -import requests - -commit_default = os.environ['COMMIT'] if 'COMMIT' in os.environ else None - -parser = argparse.ArgumentParser(description='tool to send data to build.postmarketos.org') -parser.add_argument('--token', default='~/.pmos_token', help='secret token to authenticate to build.postmarketos.org') -parser.add_argument('--host', default='https://build.postmarketos.org', help='base url for the submit request') -parser.add_argument('--commit', default=commit_default, help='value for the X-Commit header') -parser.add_argument('--arch', default='x86_64', help='value for the X-Arch header') -parser.add_argument('--id', help='id of the job in the queue, value for the X-Id header') -parser.add_argument('--json', action='store_true', help='datafile is a json file, do extra sanity checks') -parser.add_argument('--verbose', '-v', action='store_true', help='show more debug info') -parser.add_argument('endpoint', help='endpoint name on the API') -parser.add_argument('datafile', help='file containing the data to be submitted', nargs='+') -args = parser.parse_args() - -if args.commit is None: - print('You need to either add COMMIT to the environment or specify --commit') - exit(1) - -if args.verbose: - print('Environment:') - print(os.environ) - -with open(os.path.expanduser(args.token), encoding="utf-8") as handle: - secret = handle.read().strip() - -url = '{}/api/{}'.format(args.host, args.endpoint) - -if args.json: - if len(args.datafile) > 1: - print("json mode doesn't support multiple input files") - exit(1) - - # Send contents of file as HTTP POST with json payload - with open(args.datafile[0], encoding="utf-8") as handle: - data = handle.read() - data = json.loads(data) - - print('Sending json data to {}'.format(url)) - response = requests.post(url, json=data, headers={ - 'X-Secret': secret, - 'X-Commit': args.commit, - 'X-Arch': args.arch - }) -else: - files = [] - - for file in args.datafile: - filename = os.path.basename(file) - # Send contents of file as HTTP POST with multipart/formdata payload - files.append(('file[]', (filename, open(file, 'rb'), 'application/octet-stream'))) - - print('Uploading {} to {}'.format(filename, url)) - - response = requests.post(url, files=files, headers={ - 'X-Secret': secret, - 'X-Commit': args.commit, - 'X-Arch': args.arch, - 'X-Id': args.id - }) - -if response.status_code > 399: - print('Error occurred:') - print(response.content.decode()) - exit(1) - -else: - print(response.content.decode())