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/
This commit is contained in:
Oliver Smith 2019-10-01 09:46:53 +02:00
parent 9984bf9e14
commit cad539b277
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
3 changed files with 82 additions and 74 deletions

View file

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

View file

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