summaryrefslogtreecommitdiff
path: root/post-build-lava.py
blob: b25a9c2df9e02b2ba9cfd22d9916dd6cfe68b595 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python

import os
import sys
import json
import xmlrpclib
import urllib2
import re

def obfuscate_credentials(s):
    return re.sub(r"([^ ]:).+?(@)", r"\1xxx\2", s)

def main():
    """Script entry point: return some JSON based on calling args.
    We should be called from Jenkins and expect the following to be defined:
    $HWPACK_BUILD_NUMBER $HWPACK_JOB_NAME HWPACK_FILE_NAME $DEVICE_TYPE
    """

    # CI base URL
    ci_base_url = "https://ci.linaro.org/jenkins/job/"
    # Snapshots base URL
    snapshots_url = "http://snapshots.linaro.org"

    # Name of the hardware pack project
    hwpack_job_name = os.environ.get("HWPACK_JOB_NAME")
    # The hardware pack build number
    hwpack_build_number = os.environ.get("HWPACK_BUILD_NUMBER")
    # Hardware pack file name
    hwpack_file_name = os.environ.get("HWPACK_FILE_NAME", "Undefined")
    if hwpack_file_name == "Undefined":
        sys.exit("Hardware pack is not defined.")
    # Device type
    device_type = os.environ.get("DEVICE_TYPE", "Undefined")
    if device_type == "Undefined":
        sys.exit("Device type is not defined.")

    # Distribution, architecture and hardware pack type
    ret_split = hwpack_job_name.split("-",2)
    (distribution, architecture, hwpack_type) = ret_split[0], ret_split[1], ret_split[2]
    # Rootfs type, default is nano-lava
    rootfs_type = os.getenv("ROOTFS_TYPE", "nano-lava")

    # Bundle stream name
    bundle_stream_name = os.environ.get("BUNDLE_STREAM_NAME", "/private/team/linaro/developers-and-community-builds/")
    # LAVA user
    lava_user = os.environ.get("LAVA_USER")
    if lava_user == None:
        f = open('/var/run/lava/lava-user')
        lava_user = f.read().strip()
        f.close()
    # LAVA token
    lava_token = os.environ.get("LAVA_TOKEN")
    if lava_token == None:
        f = open('/var/run/lava/lava-token')
        lava_token = f.read().strip()
        f.close()
    # LAVA server URL
    lava_server = os.environ.get("LAVA_SERVER", "validation.linaro.org/lava-server/RPC2/")
    # LAVA server base URL
    lava_server_root = lava_server.rstrip("/")
    if lava_server_root.endswith("/RPC2"):
        lava_server_root = lava_server_root[:-len("/RPC2")]

    ci_url = "%s%s-%s-%s%s" % (ci_base_url, distribution, architecture, rootfs_type, "/lastSuccessfulBuild/buildNumber")
    request = urllib2.Request(ci_url)
    try:
        response = urllib2.urlopen(request)
    except urllib2.URLError, e:
        if hasattr(e, 'reason'):
            print 'Failed to reach %s.' % ci_url
            print 'Reason: ', e.reason
        elif hasattr(e, 'code'):
            print 'ci.linaro.org could not fulfill the request.'
            print 'Error code: ', e.code
        sys.exit("Failed to get last successful rootfs build number.")

    rootfs_build_number = eval(response.read())

    ci_url = "%s%s-%s-%s%s" % (ci_base_url, distribution, architecture, rootfs_type, "/lastSuccessfulBuild/buildTimestamp?format=yyyyMMdd")
    request = urllib2.Request(ci_url)
    try:
        response = urllib2.urlopen(request)
    except urllib2.URLError, e:
        if hasattr(e, 'reason'):
            print 'Failed to reach %s.' % ci_url
            print 'Reason: ', e.reason
        elif hasattr(e, 'code'):
            print 'ci.linaro.org could not fulfill the request.'
            print 'Error code: ', e.code
        sys.exit("Failed to get last successful rootfs build timestamp.")

    rootfs_build_timestamp = eval(response.read())

    rootfs_file_name = "linaro-%s-%s-%s-%s.tar.gz" % (distribution, rootfs_type, rootfs_build_timestamp, rootfs_build_number)

    # Convert CI URLs to snapshots URLs
    hwpack_url = "%s/%s/%s/%s/%s/%s" % (snapshots_url, distribution, "hwpacks", hwpack_type, hwpack_build_number, hwpack_file_name)
    rootfs_url = "%s/%s/%s/%s/%s/%s" % (snapshots_url, distribution, "images", rootfs_type, rootfs_build_number, rootfs_file_name)

    actions = [{
        "command": "deploy_linaro_image",
        "parameters": {
                "hwpack": "%s" % hwpack_url,
                "rootfs": "%s" % rootfs_url
        },
        "metadata": {
                "hwpack.type": "%s" % hwpack_type,
                "hwpack.build": "%s" % hwpack_build_number,
                "rootfs.type": "%s" % rootfs_type,
                "rootfs.build": "%s" % rootfs_build_number,
                "distribution": "%s" % distribution
        }
    },
    {
        "command": "boot_linaro_image"
    },
    {
        "command": "lava_test_run",
        "parameters": {
            "test_name": "pwrmgmt"
        }
    },
    {
        "command": "lava_test_run",
        "parameters": {
            "test_name": "gatortests"
        }
    },
    {
        "command": "lava_test_run",
        "parameters": {
            "test_name": "perf"
        }
    },
    {
        "command": "lava_test_run",
        "parameters": {
            "test_name": "wifi-enablement"
        }
    },
    {
        "command": "submit_results",
        "parameters": {
            "stream": bundle_stream_name,
            "server": "%s%s" % ("http://", lava_server)
        }
    }]

    config = json.dumps({"timeout": 18000,
                         "actions": actions,
                         "job_name": "%s%s/%s/" % (ci_base_url, hwpack_job_name, hwpack_build_number),
                         "device_type": device_type,
                        }, indent=2)

    print config

    skip_lava = os.environ.get("SKIP_LAVA")
    if skip_lava == None:
        try:
            server = xmlrpclib.ServerProxy("https://{lava_user:>s}:{lava_token:>s}@{lava_server:>s}".format(lava_user=lava_user, lava_token=lava_token, lava_server=lava_server))
            lava_job_id = server.scheduler.submit_job(config)
        except xmlrpclib.ProtocolError, e:
            print "Error making a LAVA request:", obfuscate_credentials(str(e))
            sys.exit(1)

        print "LAVA Job Id: %s, URL: http://%s/scheduler/job/%s" % (lava_job_id, lava_server_root, lava_job_id)
        json.dump({'lava_url': "http://" + lava_server_root,
                   'job_id': lava_job_id},
                   open('lava-job-info', 'w'))
    else:
        print "LAVA job submission skipped."

if __name__ == "__main__":
        main()