summaryrefslogtreecommitdiffstats
path: root/utils/agl-publish.py
blob: 2128c53c4141d302819000d84b7a5520b40867f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3

import argparse
import requests
from urllib.parse import urljoin
import os
import json
import urllib

parser = argparse.ArgumentParser(description="AGL create bmeta", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--arch', action='store', help="arch of the build", required=True)
parser.add_argument('--branch', action='store', help="branch of the build", required=True)
parser.add_argument('--commit', action='store', help="commit of the build", required=True)
parser.add_argument('--build_version', action='store', help="build_version of the build", required=True)
parser.add_argument('--kernel_version', action='store', help="kernel_version of the build", default=None)
parser.add_argument('--machine', action='store', help="machine of the build", required=True)
parser.add_argument('--api', action='store', help="URL to kernelci backend API", required=True)
args = parser.parse_args()

KCI_PUSH_TOKEN = "AGL_KCIAPI_TOKEN"
KCI_UPLOAD_TOKEN = "AGL_KCIAPI_TOKEN"

if args.api == 'http://kernelci.dev.baylibre.com:8081/':
    KCI_PUSH_TOKEN = "BAY_KCIAPI_TOKEN"
    KCI_UPLOAD_TOKEN = "BAY_KCIAPI_TOKEN"

if not KCI_PUSH_TOKEN in os.environ:
    print("Missing env variable %s" % KCI_PUSH_TOKEN)
    ptoken=None
else:
    ptoken=os.environ[KCI_PUSH_TOKEN]
if not KCI_UPLOAD_TOKEN in os.environ:
    print("Missing env variable %s" % KCI_UPLOAD_TOKEN)
    utoken=None
else:
    utoken=os.environ[KCI_UPLOAD_TOKEN]

bmeta = {}
bmeta["arch"] = args.arch
bmeta["build_log"] = "build.log"
bmeta["build_environment"] = "AGL-yocto"
bmeta["defconfig"] = "defconfig"
bmeta["defconfig_full"] = "defconfig+CONFIG_AGL=y"

bmeta["git_branch"] = args.branch
bmeta["git_commit"] = args.commit
if args.kernel_version:
    bmeta["git_describe"] = args.kernel_version
    bmeta["git_describe_v"] = args.kernel_version
else:
    bmeta["git_describe"] = args.branch
    bmeta["git_describe_v"] = args.branch
bmeta["job"] = "AGL-yocto"

fsr = "AGL-yocto/%s/%s/%s/" % (args.branch, args.build_version, args.machine)
bmeta["file_server_resource"] = fsr

print(bmeta)

DIR_META="agl-build-meta"
# create directory with artifacts to be uploaded (bmeta.json)
if not os.path.exists(DIR_META):
    os.mkdir(DIR_META)

# write bmeta in it
with open(os.path.join(DIR_META, 'bmeta.json'), 'w') as json_file:
    json.dump(bmeta, json_file, indent=4, sort_keys=True)

def upload():
    path = bmeta["file_server_resource"]

    data = {
        'path': path,
    }
    artifacts = {}
    for root, _, files in os.walk(DIR_META):
        for f in files:
            px = os.path.relpath(root, DIR_META)
            artifacts[os.path.join(px, f)] = open(os.path.join(root, f), "rb")

    files = {
        'file{}'.format(i): (name, fobj)
        for i, (name, fobj) in enumerate(artifacts.items())
    }
    url = urljoin(args.api, 'upload')
    print("DATA====================")
    print(data)
    if utoken is None:
        print("No upload token, skipping upload")
        return 0
    headers = {
        'Authorization': utoken,
    }
    resp = requests.post(url, headers=headers, data=data, files=files)
    resp.raise_for_status()
    print(resp.reason)
    print(resp.text)


def publish_kernel():
    data = {k: bmeta[v] for k, v in {
        'file_server_resource': 'file_server_resource',
        'job': 'job',
        'git_branch': 'git_branch',
        'arch': 'arch',
        'kernel': 'git_describe',
        'build_environment': 'build_environment',
        'defconfig': 'defconfig',
        'defconfig_full': 'defconfig_full',
    }.items()}
    print("DATA====================")
    print(data)
    if ptoken is None:
        print("No publish token, skipping publish")
        return 0
    headers = {
        'Authorization': ptoken,
        'Content-Type': 'application/json',
    }
    url = urllib.parse.urljoin(args.api, '/build')
    data_json = json.dumps(data)
    resp = requests.post(url, headers=headers, data=data_json)
    resp.raise_for_status()
    print(resp.reason)
    print(resp.text)

print("============== UPLOAD")
upload()
print("============== PUBLISH")
publish_kernel()