|
| 1 | +# Copyright (c) 2017-2018 Red Hat, Inc. All rights reserved. This copyrighted |
| 2 | +# material is made available to anyone wishing to use, modify, copy, or |
| 3 | +# redistribute it subject to the terms and conditions of the GNU General |
| 4 | +# Public License v.2 or later. |
| 5 | +# |
| 6 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 7 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 8 | +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 9 | +# details. |
| 10 | +# |
| 11 | +# You should have received a copy of the GNU General Public License |
| 12 | +# along with this program; if not, write to the Free Software Foundation, Inc., |
| 13 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | + |
| 15 | + |
| 16 | +class DirectRun(object): |
| 17 | + """Direct test run""" |
| 18 | + |
| 19 | + def __init__(self, work_dir, work_url, skt_work_dir, |
| 20 | + baserepo, baseref, baseconfig, |
| 21 | + message_id, subject, emails, |
| 22 | + patch_url_list, makeopts) |
| 23 | + """ |
| 24 | + Initialize a direct test run. |
| 25 | +
|
| 26 | + Args: |
| 27 | + dir: Directory to run in. |
| 28 | + url: URL through which the directory is shared. |
| 29 | + skt_dir: Skt work directory to use. |
| 30 | + baserepo: Baseline Git repo URL. |
| 31 | + baseref: Baseline Git reference. |
| 32 | + baseconfig: Kernel configuration URL. |
| 33 | + message_id: Value of the "Message-Id" header of the e-mail |
| 34 | + message representing the patchset, or None if |
| 35 | + unknown. |
| 36 | + subject: Subject of the message representing the patchset, |
| 37 | + or None if unknown. |
| 38 | + emails: Set of e-mail addresses involved with the patchset |
| 39 | + to send notifications to. |
| 40 | + patch_url_list: List of URLs pointing to patches to apply, in the |
| 41 | + order they should be applied in. |
| 42 | + makeopts: String of extra arguments to pass to the build's |
| 43 | + make invocation. |
| 44 | +
|
| 45 | + """ |
| 46 | + self.dir = dir |
| 47 | + self.url = url |
| 48 | + self.skt_dir = skt_dir |
| 49 | + self.baserepo = baserepo |
| 50 | + self.baseref = baseref |
| 51 | + self.baseconfig = baseconfig |
| 52 | + self.message_id = message_id |
| 53 | + self.subject = subject |
| 54 | + self.emails = emails |
| 55 | + self.patch_url_list = patch_url_list |
| 56 | + self.makeopts = makeopts |
| 57 | + |
| 58 | + def start(): |
| 59 | + """ |
| 60 | + Start the run. |
| 61 | + """ |
| 62 | + # Create directory for JUnit results |
| 63 | + self.dir_junit = os.path.join(self.dir, "junit") |
| 64 | + os.mkdir(self.dir_junit) |
| 65 | + # Create directory for build artifacts |
| 66 | + self.dir_build = os.path.join(self.dir, "build") |
| 67 | + os.mkdir(self.dir_build) |
| 68 | + # Format URL for build artifacts |
| 69 | + self.url_build = |
| 70 | + urlparse.urljoin(self.url, os.path.basename(self.dir_build)) |
| 71 | + # Start skt |
| 72 | + self.started = True |
| 73 | + |
| 74 | + def is_started(): |
| 75 | + """ |
| 76 | + Check if the run is started. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + True if the run is started, false otherwise. |
| 80 | + """ |
| 81 | + |
| 82 | + def abort(): |
| 83 | + """ |
| 84 | + Abort a run: stop the run, if started, and mark it aborted. |
| 85 | + """ |
| 86 | + def is_finished(): |
| 87 | + """ |
| 88 | + Check if the run is finished. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + True if the run is finished, false otherwise. |
| 92 | + """ |
| 93 | + |
| 94 | + |
| 95 | +class Direct(object): |
| 96 | + """Direct test run scheduler""" |
| 97 | + |
| 98 | + def __init__(self, skt_dir, public_hostname, port, |
| 99 | + beaker_job_template, parallel_run_num): |
| 100 | + """ |
| 101 | + Initialize a direct test run scheduler. |
| 102 | + """ |
| 103 | + # Scheduler data directory |
| 104 | + self.dir = None |
| 105 | + # HTTP server PID |
| 106 | + self.httpd_pid = None |
| 107 | + # Scheduler public data URL (as shared by the HTTP server) |
| 108 | + self.url = None |
| 109 | + # Skt work directory to use |
| 110 | + self.skt_dir = skt_dir |
| 111 | + # Hostname the published artifacts should be accessible at |
| 112 | + self.public_hostname = public_hostname |
| 113 | + # Port the published artifacts should be accessible at |
| 114 | + self.port = port |
| 115 | + # Path to the Beaker job XML template to supply to skt |
| 116 | + self.beaker_job_template = beaker_job_template |
| 117 | + # Maximum number of parallel runs |
| 118 | + self.parallel_run_num = parallel_run_num |
| 119 | + # Next run ID |
| 120 | + self.run_id = 0 |
| 121 | + # Run ID -> object map |
| 122 | + self.run_map = {} |
| 123 | + |
| 124 | + # Create the scheduler directory |
| 125 | + self.dir = tempfile.mkdtemp(suffix="sktm_runs_") |
| 126 | + # Start an HTTP server for serving artifacts |
| 127 | + self.httpd_pid = os.fork() |
| 128 | + if self.httpd_pid == 0: |
| 129 | + os.chdir(self.dir) |
| 130 | + httpd = BaseHTTPServer.HTTPServer( |
| 131 | + ("", port), |
| 132 | + SimpleHTTPServer.SimpleHTTPRequestHandler) |
| 133 | + # TODO Print port number we're listening on, e.g. |
| 134 | + # print(httpd.socket.getsockname()[1]) |
| 135 | + httpd.serve_forever() |
| 136 | + os._exit(0) |
| 137 | + # TODO Read and update the port in case it was zero |
| 138 | + # TODO That will also ensure the server is functional |
| 139 | + # Format our public URL |
| 140 | + self.url = "http://" + self.public_hostname + ":" + self.port |
| 141 | + |
| 142 | + def __del__(): |
| 143 | + """ |
| 144 | + Cleanup a direct test run scheduler. |
| 145 | + """ |
| 146 | + if self.httpd_pid: |
| 147 | + os.kill(self.httpd_pid) |
| 148 | + if self.dir: |
| 149 | + shutil.rmtree(self.dir) |
| 150 | + |
| 151 | + def get_base_commitdate(self, run_id): |
| 152 | + """ |
| 153 | + Get base commit's committer date of the specified completed run. |
| 154 | + Wait for the run to complete, if it hasn't yet. |
| 155 | +
|
| 156 | + Args: |
| 157 | + run_id: Run ID. |
| 158 | +
|
| 159 | + Return: |
| 160 | + The epoch timestamp string of the committer date. |
| 161 | + """ |
| 162 | + |
| 163 | + def get_base_hash(self, run_id): |
| 164 | + """ |
| 165 | + Get base commit's hash of the specified completed run. |
| 166 | + Wait for the run to complete, if it hasn't yet. |
| 167 | +
|
| 168 | + Args: |
| 169 | + run_id: Run ID. |
| 170 | +
|
| 171 | + Return: |
| 172 | + The base commit's hash string. |
| 173 | + """ |
| 174 | + |
| 175 | + def get_patch_url_list(self, run_id): |
| 176 | + """ |
| 177 | + Get the list of Patchwork patch URLs for the specified completed |
| 178 | + run. Wait for the run to complete, if it hasn't yet. |
| 179 | +
|
| 180 | + Args: |
| 181 | + run_id: Run ID. |
| 182 | +
|
| 183 | + Return: |
| 184 | + The list of Patchwork patch URLs, in the order the patches should |
| 185 | + be applied in. |
| 186 | + """ |
| 187 | + |
| 188 | + def get_result_url(self, run_id): |
| 189 | + """ |
| 190 | + Get the URL of the web representation of the specified run. |
| 191 | +
|
| 192 | + Args: |
| 193 | + run_id: Run ID. |
| 194 | +
|
| 195 | + Result: |
| 196 | + The URL of the run result. |
| 197 | + """ |
| 198 | + |
| 199 | + def get_result(self, run_id): |
| 200 | + """ |
| 201 | + Get result code (sktm.misc.tresult) for the specified run. |
| 202 | + Wait for the run to complete, if it hasn't yet. |
| 203 | +
|
| 204 | + Args: |
| 205 | + run_id: Run ID. |
| 206 | +
|
| 207 | + Result: |
| 208 | + The run result code (sktm.misc.tresult). |
| 209 | + """ |
| 210 | + |
| 211 | + def __update(): |
| 212 | + """ |
| 213 | + Update run status. |
| 214 | + """ |
| 215 | + # Start with the maximum allowed run number |
| 216 | + parallel_run_num = self.parallel_run_num |
| 217 | + # Subtract runs in progress |
| 218 | + for run_id, run in self.run_map.items(): |
| 219 | + if run.is_active(): |
| 220 | + parallel_run_num -= 1 |
| 221 | + # Fill up with new jobs |
| 222 | + while parallel_run_num > 0: |
| 223 | + for run_id, run in self.run_map.items(): |
| 224 | + if not run.is_complete(): |
| 225 | + run.start() |
| 226 | + |
| 227 | + def submit(self, baserepo=None, baseref=None, baseconfig=None, |
| 228 | + message_id=None, subject=None, emails=set(), patch_url_list=[], |
| 229 | + makeopts=None): |
| 230 | + """ |
| 231 | + Submit a run. |
| 232 | +
|
| 233 | + Args: |
| 234 | + baserepo: Baseline Git repo URL. |
| 235 | + baseref: Baseline Git reference to test. |
| 236 | + baseconfig: Kernel configuration URL. |
| 237 | + message_id: Value of the "Message-Id" header of the e-mail |
| 238 | + message representing the patchset, or None if |
| 239 | + unknown. |
| 240 | + subject: Subject of the message representing the patchset, |
| 241 | + or None if unknown. |
| 242 | + emails: Set of e-mail addresses involved with the patchset |
| 243 | + to send notifications to. |
| 244 | + patch_url_list: List of URLs pointing to patches to apply, in the |
| 245 | + order they should be applied in. |
| 246 | + makeopts: String of extra arguments to pass to the build's |
| 247 | + make invocation. |
| 248 | +
|
| 249 | + Returns: |
| 250 | + Submitted run number. |
| 251 | + """ |
| 252 | + # Grab next run ID |
| 253 | + run_id = self.run_id |
| 254 | + # Create a run directory |
| 255 | + run_dir = os.path.join(self.dir, run_id) |
| 256 | + os.mkdir(run_dir) |
| 257 | + # Format a run URL |
| 258 | + run_url = urlparse.urljoin(self.url, run_id) |
| 259 | + # Create a run |
| 260 | + run = DirectRun(dir=run_dir, |
| 261 | + url=run_url, |
| 262 | + skt_dir=self.skt_dir, |
| 263 | + baserepo=baserepo, |
| 264 | + baseref=baseref, |
| 265 | + baseconfig=baseconfig, |
| 266 | + message_id=message_id, |
| 267 | + subject=subject, |
| 268 | + emails=emails, |
| 269 | + patch_url_list=patch_url_list, |
| 270 | + makeopts=makeopts) |
| 271 | + self.job_map[run_id] = run |
| 272 | + # Submitted, move onto next ID |
| 273 | + self.run_id += 1 |
| 274 | + # Update run status. |
| 275 | + self.__update() |
| 276 | + return run_id |
| 277 | + |
| 278 | + def is_run_complete(self, run_id): |
| 279 | + """ |
| 280 | + Check if a run is complete. |
| 281 | +
|
| 282 | + Args: |
| 283 | + run_id: Run ID. |
| 284 | +
|
| 285 | + Return: |
| 286 | + True if the run is complete. |
| 287 | + """ |
0 commit comments