summaryrefslogtreecommitdiffstats
path: root/tools/infra-dashboard/utils/jenkinsAdapter.php
blob: c238feb7d47224f49ad365bed8810607bfc92771 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

    function getSlaves() {
        $query="https://build.opnfv.org/ci/computer/api/xml?tree=computer[displayName,offline,idle]";
        $output =  file_get_contents($query);
        $xml = simplexml_load_string($output);
        if ($xml) return $xml;
        else return "";
    }

    function getCiSlaves(){
        $query="https://build.opnfv.org/ci/label/ci-pod/api/xml?xpath=labelAtom/node[nodeName]&wrapper=nodes";
        $output =  file_get_contents($query);
        $xml = simplexml_load_string($output);
        if ($xml) return $xml;
        else return "";
    }

    function getAllBuilds(){
        $query="https://build.opnfv.org/ci/api/xml?tree=jobs[displayName,url,lastBuild[fullDisplayName,building,builtOn,timestamp,result]]";
        $output =  file_get_contents($query);
        $xml = simplexml_load_string($output);
        if ($xml) return $xml;
        else return "";
    }

    $SLAVES = getSlaves();
    $CI_PODS = getCiSlaves();
    $ALL_BUILDS = getAllBuilds();

    function getActiveBuilds() {
        global $ALL_BUILDS;
        //$query="https://build.opnfv.org/ci/api/xml?tree=jobs[displayName,url,lastBuild[fullDisplayName,building,builtOn,timestamp]]&xpath=hudson/job[lastBuild/building=%27true%27]&wrapper=hudson";
        $xml = $ALL_BUILDS->xpath('job[lastBuild/building="true"]');
        if ($xml) return $xml;
        else return "";
    }

    $ACTIVE_BUILDS = getActiveBuilds();

    function slaveExists($slave) {
        global $SLAVES;
        $slave = $SLAVES->xpath('computer[displayName="'.$slave.'"]');
        if ($slave) return true;
        else return false;
    }

    function getSlaveStatus($slave) {
        global $SLAVES;
        $status = "unknown";
        if (!slaveExists($slave)) return $status;
        $slave = $SLAVES->xpath('computer[displayName="'.$slave.'"]');
        $offline = $slave[0]->offline;

        if ($offline == "true") $status = "offline";
        else $status = "online";
        return $status;
    }

    function getSlaveUrl($slave) {
        if (slaveExists($slave)) return "https://build.opnfv.org/ci/computer/".$slave;
        else return "";
    }


    function isCiPod($slave) {
        global $CI_PODS;
        $result = $CI_PODS->xpath('node[nodeName="'.$slave.'"]');
        if ($result) return true;
        else return false;
    }

    function isDevPod($slave) {
        global $CI_PODS;
        if (isCiPod($slave)) return false;
        else if (strpos($slave, 'pod') !== false)  return true;
        else return false;
    }

    function parseJobString($str) {
        $scenario = '';
        $installer = '';
        $branch = '';
        $installers = array("fuel", "joid", "apex", "compass");
        $branches = array("master","arno", "brahmaputra", "colorado");
        $arr = split ('[ -]', $str);
        for($x = 0; $x < count($arr); $x++) {
            if (strcmp($arr[$x],"os") == 0)  //all the scenarios start with 'os'
                $scenario = $arr[$x].'-'.$arr[$x+1].'-'.$arr[$x+2].'-'.$arr[$x+3];
            else if (in_array($arr[$x], $installers))
                $installer = $arr[$x];
            else if (in_array($arr[$x], $branches))
                $branch = $arr[$x];
        }
        $arr2 = explode(' ', $str);
        $jobname = $arr2[0]; //take first word as job name

        return array(
            "jobname"=>$jobname,
            "installer"=>$installer,
            "branch"=>$branch,
            "scenario"=>$scenario
        );
    }


    function getJJob($slave) {
        global $ALL_BUILDS;
        if (!slaveExists($slave)) return "";

        //$builds = $ALL_BUILDS;
        //$xml = $ALL_BUILDS->xpath('job[lastBuild/building="true"][lastBuild/builtOn="'.$slave.'"]');
        $builds = $ALL_BUILDS->xpath('job[lastBuild/builtOn="'.$slave.'"]');
        if (! $builds) { //the slave does not have jobs in building state
            //echo "NO JOBS FOUND";
            return "";
        }
        else {
            //is there any active build?
            $builds = $ALL_BUILDS->xpath('job[lastBuild/building="true"][lastBuild/builtOn="'.$slave.'"]');
            if ($builds) { // there are active builds for this slave
                //print_r($builds);

                $child_job  = simplexml_import_dom($builds[0]);
                foreach ($builds as &$build) {
                    $int1 = intval($build->lastBuild->timestamp);
                    $int2 = intval($child_job->lastBuild->timestamp);
                    if ($int1 > $int2) {
                        $child_job  = simplexml_import_dom($build);
                    }
                }
                $url = strval($child_job->url);
                $fullDisplayName = $child_job->lastBuild->fullDisplayName;
                //echo $fullDisplayName."<br>";

                $params = parseJobString($fullDisplayName);

                $type = 0; // type=0 means the job is running
                $job_params = array(
                    "name"=>$params['jobname'],
                    "url"=>$url,
                    "scenario"=>$params['scenario'],
                    "installer"=>$params['installer'],
                    "branch"=>$params['branch'],
                    "type"=>$type
                );
                //print_r($job_params);
                return $job_params;

            }
            else { // there are NO active builds for this slave, we take the latest build
                //echo "NO Active builds";
                $builds = $ALL_BUILDS->xpath('job[lastBuild/building="false"][lastBuild/builtOn="'.$slave.'"]');
                $last_job  = simplexml_import_dom($builds[0]);
                //print_r($last_job);
                foreach ($builds as &$build) {
                    $int1 = intval($build->lastBuild->timestamp);
                    $int2 = intval($last_job->lastBuild->timestamp);
                    if ($int1 > $int2) {
                        $last_job  = simplexml_import_dom($build);
                    }
                }
                $url = strval($last_job->url);
                $result = strval($last_job->lastBuild->result);
                $fullDisplayName = $last_job->lastBuild->fullDisplayName;

                $params = parseJobString($fullDisplayName);

                $type = 3;
                if ($result == "SUCCESS") $type = 1; // type=1 means it's the last job and it succeded
                if ($result == "FAILURE") $type = 2; // type=2 means it's the last job and it failed
                if ($result == "UNSTABLE") $type = 3; // type=3 means it's the last job is unstable

                $job_params = array(
                    "name"=>$params['jobname'],
                    "url"=>$url,
                    "scenario"=>$params['scenario'],
                    "installer"=>$params['installer'],
                    "branch"=>$params['branch'],
                    "type"=>$type
                );

                return $job_params;
                //print_r($job_params);
            }

        }
    }

    /*
    $slave = "lf-pod2";
    $job_params = getJJob($slave);

    $status = getSlaveStatus($slave);
    echo "Status slave ".$slave.": ".$status."<br>";
    echo "Job: ".$job_params['name']."<br>";
    echo "URL: ".$job_params['url']."<br>";
    echo "Scenario: ".$job_params['scenario']."<br>";
    echo "Installer: ".$job_params['installer']."<br>";
    echo "Branch: ".$job_params['branch']."<br>";
    echo "Type: ".$job_params['type']."<br>";
    */

?>