remote_gcc_build.py revision 6aebeb10e517878cb76044e6a1d46f75c2fd93d6
1#!/usr/bin/python 2 3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Script to use remote try-bot build image with local gcc.""" 8 9import argparse 10import glob 11import os 12import re 13import shutil 14import socket 15import sys 16import tempfile 17import time 18 19from utils import command_executer 20from utils import logger 21from utils import manifest_versions 22from utils import misc 23 24BRANCH = "the_actual_branch_used_in_this_script" 25TMP_BRANCH = "tmp_branch" 26SLEEP_TIME = 600 27 28 29def GetPatchNum(output): 30 lines = output.splitlines() 31 line = [l for l in lines if "googlesource" in l][0] 32 patch_num = re.findall(r"\d+", line)[0] 33 if "chrome-internal" in line: 34 patch_num = "*" + patch_num 35 return str(patch_num) 36 37 38def GetPatchString(patch): 39 if patch: 40 return "+".join(patch) 41 return "NO_PATCH" 42 43 44def FindVersionForToolchain(branch, chromeos_root): 45 """Find the version number in artifacts link in the tryserver email.""" 46 # For example: input: toolchain-3701.42.B 47 # output: R26-3701.42.1 48 digits = branch.split("-")[1].split("B")[0] 49 manifest_dir = os.path.join(chromeos_root, "manifest-internal") 50 os.chdir(manifest_dir) 51 major_version = digits.split(".")[0] 52 ce = command_executer.GetCommandExecuter() 53 command = "repo sync . && git branch -a | grep {0}".format(major_version) 54 _, branches, _ = ce.RunCommand(command, return_output=True, 55 print_to_console=False) 56 m = re.search(r"(R\d+)", branches) 57 if not m: 58 logger.GetLogger().LogFatal("Cannot find version for branch {0}" 59 .format(branch)) 60 version = m.group(0)+"-"+digits+"1" 61 return version 62 63 64def FindBuildId(description): 65 """Find the build id of the build at trybot server.""" 66 running_time = 0 67 while True: 68 (result, number) = FindBuildIdFromLog(description) 69 if result >= 0: 70 return (result, number) 71 logger.GetLogger().LogOutput("{0} minutes passed." 72 .format(running_time / 60)) 73 logger.GetLogger().LogOutput("Sleeping {0} seconds.".format(SLEEP_TIME)) 74 time.sleep(SLEEP_TIME) 75 running_time += SLEEP_TIME 76 77 78def FindBuildIdFromLog(description): 79 """Get the build id from build log.""" 80 # returns tuple (result, buildid) 81 # result == 0, buildid > 0, the build was successful and we have a build id 82 # result > 0, buildid > 0, the whole build failed for some reason but we 83 # do have a build id. 84 # result == -1, buildid == -1, we have not found a finished build for this 85 # description yet 86 87 file_dir = os.path.dirname(os.path.realpath(__file__)) 88 commands = ("{0}/utils/buildbot_json.py builds " 89 "http://chromegw/p/tryserver.chromiumos/" 90 .format(file_dir)) 91 ce = command_executer.GetCommandExecuter() 92 _, buildinfo, _ = ce.RunCommand(commands, return_output=True, 93 print_to_console=False) 94 95 my_info = buildinfo.splitlines() 96 current_line = 1 97 running_job = False 98 result = -1 99 100 # result == 0, we have a successful build 101 # result > 0, we have a failed build but build id may be valid 102 # result == -1, we have not found a finished build for this description 103 while current_line < len(my_info): 104 my_dict = {} 105 while True: 106 key = my_info[current_line].split(":")[0].strip() 107 value = my_info[current_line].split(":", 1)[1].strip() 108 my_dict[key] = value 109 current_line += 1 110 if "Build" in key or current_line == len(my_info): 111 break 112 if ("True" not in my_dict["completed"] and 113 str(description) in my_dict["reason"]): 114 running_job = True 115 if ("True" not in my_dict["completed"] or 116 str(description) not in my_dict["reason"]): 117 continue 118 result = int(my_dict["result"]) 119 build_id = int(my_dict["number"]) 120 if result == 0: 121 return (result, build_id) 122 else: 123 # Found a finished failed build. 124 # Keep searching to find a successful one 125 pass 126 127 if result > 0 and not running_job: 128 return (result, build_id) 129 return (-1, -1) 130 131 132def DownloadImage(target, index, dest, version): 133 """Download artifacts from cloud.""" 134 if not os.path.exists(dest): 135 os.makedirs(dest) 136 137 rversion = manifest_versions.RFormatCrosVersion(version) 138# ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/{1}-b{2}" 139# .format(target, rversion, index)) 140 ls_cmd = ("gsutil ls gs://chromeos-image-archive/trybot-{0}/*-b{2}" 141 .format(target, rversion, index)) 142 143 download_cmd = ("$(which gsutil) cp {0} {1}".format("{0}", dest)) 144 ce = command_executer.GetCommandExecuter() 145 146 _, out, _ = ce.RunCommand(ls_cmd, return_output=True, print_to_console=True) 147 lines = out.splitlines() 148 download_files = ["autotest.tar", "chromeos-chrome", 149 "chromiumos_test_image", "debug.tgz", 150 "sysroot_chromeos-base_chromeos-chrome.tar.xz" 151 ] 152 for line in lines: 153 if any([e in line for e in download_files]): 154 cmd = download_cmd.format(line) 155 if ce.RunCommand(cmd): 156 logger.GetLogger().LogFatal("Command {0} failed, existing..." 157 .format(cmd)) 158 159 160def UnpackImage(dest): 161 """Unpack the image, the chroot build dir.""" 162 chrome_tbz2 = glob.glob(dest+"/*.tbz2")[0] 163 commands = ("tar xJf {0}/sysroot_chromeos-base_chromeos-chrome.tar.xz " 164 "-C {0} &&" 165 "tar xjf {1} -C {0} &&" 166 "tar xzf {0}/debug.tgz -C {0}/usr/lib/ &&" 167 "tar xf {0}/autotest.tar -C {0}/usr/local/ &&" 168 "tar xJf {0}/chromiumos_test_image.tar.xz -C {0}" 169 .format(dest, chrome_tbz2)) 170 ce = command_executer.GetCommandExecuter() 171 return ce.RunCommand(commands) 172 173 174def RemoveOldBranch(): 175 """Remove the branch with name BRANCH.""" 176 ce = command_executer.GetCommandExecuter() 177 command = "git rev-parse --abbrev-ref HEAD" 178 _, out, _ = ce.RunCommand(command, return_output=True) 179 if BRANCH in out: 180 command = "git checkout -B {0}".format(TMP_BRANCH) 181 ce.RunCommand(command) 182 command = "git commit -m 'nouse'" 183 ce.RunCommand(command) 184 command = "git branch -D {0}".format(BRANCH) 185 ce.RunCommand(command) 186 187 188def UploadManifest(manifest, chromeos_root, branch="master"): 189 """Copy the manifest to $chromeos_root/manifest-internal and upload.""" 190 chromeos_root = misc.CanonicalizePath(chromeos_root) 191 manifest_dir = os.path.join(chromeos_root, "manifest-internal") 192 os.chdir(manifest_dir) 193 ce = command_executer.GetCommandExecuter() 194 195 RemoveOldBranch() 196 197 if branch != "master": 198 branch = "{0}".format(branch) 199 command = "git checkout -b {0} -t cros-internal/{1}".format(BRANCH, branch) 200 ret = ce.RunCommand(command) 201 if ret: 202 raise Exception("Command {0} failed".format(command)) 203 204 # We remove the default.xml, which is the symbolic link of full.xml. 205 # After that, we copy our xml file to default.xml. 206 # We did this because the full.xml might be updated during the 207 # run of the script. 208 os.remove(os.path.join(manifest_dir, "default.xml")) 209 shutil.copyfile(manifest, os.path.join(manifest_dir, "default.xml")) 210 return UploadPatch(manifest) 211 212 213def GetManifestPatch(manifests, version, chromeos_root, branch="master"): 214 """Return a gerrit patch number given a version of manifest file.""" 215 temp_dir = tempfile.mkdtemp() 216 to_file = os.path.join(temp_dir, "default.xml") 217 manifests.GetManifest(version, to_file) 218 return UploadManifest(to_file, chromeos_root, branch) 219 220 221def UploadPatch(source): 222 """Up load patch to gerrit, return patch number.""" 223 commands = ("git add -A . &&" 224 "git commit -m 'test' -m 'BUG=None' -m 'TEST=None' " 225 "-m 'hostname={0}' -m 'source={1}'" 226 .format(socket.gethostname(), source)) 227 ce = command_executer.GetCommandExecuter() 228 ce.RunCommand(commands) 229 230 commands = ("yes | repo upload . --cbr --no-verify") 231 _, _, err = ce.RunCommand(commands, return_output=True) 232 return GetPatchNum(err) 233 234 235def ReplaceSysroot(chromeos_root, dest_dir, target): 236 """Copy unpacked sysroot and image to chromeos_root.""" 237 ce = command_executer.GetCommandExecuter() 238 # get the board name from "board-release". board may contain "-" 239 board = target.rsplit("-", 1)[0] 240 board_dir = os.path.join(chromeos_root, "chroot", "build", board) 241 command = "sudo rm -rf {0}".format(board_dir) 242 ce.RunCommand(command) 243 244 command = "sudo mv {0} {1}".format(dest_dir, board_dir) 245 ce.RunCommand(command) 246 247 image_dir = os.path.join(chromeos_root, "src", "build", "images", 248 board, "latest") 249 command = "rm -rf {0} && mkdir -p {0}".format(image_dir) 250 ce.RunCommand(command) 251 252 command = "mv {0}/chromiumos_test_image.bin {1}".format(board_dir, image_dir) 253 return ce.RunCommand(command) 254 255 256def GccBranchForToolchain(branch): 257 if branch == "toolchain-3428.65.B": 258 return "release-R25-3428.B" 259 else: 260 return None 261 262 263def GetGccBranch(branch): 264 """Get the remote branch name from branch or version.""" 265 ce = command_executer.GetCommandExecuter() 266 command = "git branch -a | grep {0}".format(branch) 267 _, out, _ = ce.RunCommand(command, return_output=True) 268 if not out: 269 release_num = re.match(r".*(R\d+)-*", branch) 270 if release_num: 271 release_num = release_num.group(0) 272 command = "git branch -a | grep {0}".format(release_num) 273 _, out, _ = ce.RunCommand(command, return_output=True) 274 if not out: 275 GccBranchForToolchain(branch) 276 if not out: 277 out = "remotes/cros/master" 278 new_branch = out.splitlines()[0] 279 return new_branch 280 281 282def UploadGccPatch(chromeos_root, gcc_dir, branch): 283 """Upload local gcc to gerrit and get the CL number.""" 284 ce = command_executer.GetCommandExecuter() 285 gcc_dir = misc.CanonicalizePath(gcc_dir) 286 gcc_path = os.path.join(chromeos_root, "src/third_party/gcc") 287 assert os.path.isdir(gcc_path), ("{0} is not a valid chromeos root" 288 .format(chromeos_root)) 289 assert os.path.isdir(gcc_dir), ("{0} is not a valid dir for gcc" 290 "source".format(gcc_dir)) 291 os.chdir(gcc_path) 292 RemoveOldBranch() 293 if not branch: 294 branch = "master" 295 branch = GetGccBranch(branch) 296 command = ("git checkout -b {0} -t {1} && " 297 "rm -rf *".format(BRANCH, branch)) 298 ce.RunCommand(command, print_to_console=False) 299 300 command = ("rsync -az --exclude='*.svn' --exclude='*.git'" 301 " {0}/ .".format(gcc_dir)) 302 ce.RunCommand(command) 303 return UploadPatch(gcc_dir) 304 305 306def RunRemote(chromeos_root, branch, patches, is_local, 307 target, chrome_version, dest_dir): 308 """The actual running commands.""" 309 ce = command_executer.GetCommandExecuter() 310 311 if is_local: 312 local_flag = "--local -r {0}".format(dest_dir) 313 else: 314 local_flag = "--remote" 315 patch = "" 316 for p in patches: 317 patch += " -g {0}".format(p) 318 cbuildbot_path = os.path.join(chromeos_root, "chromite/buildbot") 319 os.chdir(cbuildbot_path) 320 branch_flag = "" 321 if branch != "master": 322 branch_flag = " -b {0}".format(branch) 323 chrome_version_flag = "" 324 if chrome_version: 325 chrome_version_flag = " --chrome_version={0}".format(chrome_version) 326 description = "{0}_{1}_{2}".format(branch, GetPatchString(patches), target) 327 command = ("yes | ./cbuildbot {0} {1} {2} {3} {4} {5}" 328 " --remote-description={6}" 329 " --chrome_rev=tot" 330 .format(patch, branch_flag, chrome_version, local_flag, 331 chrome_version_flag, target, description)) 332 ce.RunCommand(command) 333 334 return description 335 336 337def Main(argv): 338 """The main function.""" 339 # Common initializations 340 parser = argparse.ArgumentParser() 341 parser.add_argument("-c", "--chromeos_root", required=True, 342 dest="chromeos_root", help="The chromeos_root") 343 parser.add_argument("-g", "--gcc_dir", default="", dest="gcc_dir", 344 help="The gcc dir") 345 parser.add_argument("-t", "--target", required=True, dest="target", 346 help=("The target to be build, the list is at" 347 " $(chromeos_root)/chromite/buildbot/cbuildbot" 348 " --list -all")) 349 parser.add_argument("-l", "--local", action="store_true") 350 parser.add_argument("-d", "--dest_dir", dest="dest_dir", 351 help=("The dir to build the whole chromeos if" 352 " --local is set")) 353 parser.add_argument("--chrome_version", dest="chrome_version", 354 default="", help="The chrome version to use. " 355 "Default it will use the latest one.") 356 parser.add_argument("--chromeos_version", dest="chromeos_version", 357 default="", 358 help=("The chromeos version to use." 359 "(1) A release version in the format: " 360 "'\d+\.\d+\.\d+\.\d+.*'" 361 "(2) 'latest_lkgm' for the latest lkgm version")) 362 parser.add_argument("-r", "--replace_sysroot", action="store_true", 363 help=("Whether or not to replace the build/$board dir" 364 "under the chroot of chromeos_root and copy " 365 "the image to src/build/image/$board/latest." 366 " Default is False")) 367 parser.add_argument("-b", "--branch", dest="branch", default="", 368 help=("The branch to run trybot, default is None")) 369 parser.add_argument("-p", "--patch", dest="patch", default="", 370 help=("The patches to be applied, the patches numbers " 371 "be seperated by ','")) 372 373 script_dir = os.path.dirname(os.path.realpath(__file__)) 374 375 args = parser.parse_args(argv[1:]) 376 target = args.target 377 if args.patch: 378 patch = args.patch.split(",") 379 else: 380 patch = [] 381 chromeos_root = misc.CanonicalizePath(args.chromeos_root) 382 if args.chromeos_version and args.branch: 383 raise Exception("You can not set chromeos_version and branch at the " 384 "same time.") 385 386 manifests = None 387 if args.branch: 388 chromeos_version = "" 389 branch = args.branch 390 else: 391 chromeos_version = args.chromeos_version 392 manifests = manifest_versions.ManifestVersions() 393 if chromeos_version == "latest_lkgm": 394 chromeos_version = manifests.TimeToVersion(time.mktime(time.gmtime())) 395 logger.GetLogger().LogOutput("found version %s for latest LKGM" % ( 396 chromeos_version)) 397 # TODO: this script currently does not handle the case where the version 398 # is not in the "master" branch 399 branch = "master" 400 401 if chromeos_version: 402 manifest_patch = GetManifestPatch(manifests, chromeos_version, 403 chromeos_root) 404 patch.append(manifest_patch) 405 if args.gcc_dir: 406 # TODO: everytime we invoke this script we are getting a different 407 # patch for GCC even if GCC has not changed. The description should 408 # be based on the MD5 of the GCC patch contents. 409 patch.append(UploadGccPatch(chromeos_root, args.gcc_dir, branch)) 410 description = RunRemote(chromeos_root, branch, patch, args.local, 411 target, args.chrome_version, args.dest_dir) 412 if args.local or not args.dest_dir: 413 # TODO: We are not checktng the result of cbuild_bot in here! 414 return 0 415 416 # return value: 417 # 0 => build bot was successful and image was put where requested 418 # 1 => Build bot FAILED but image was put where requested 419 # 2 => Build bot failed or BUild bot was successful but and image was 420 # not generated or could not be put where expected 421 422 os.chdir(script_dir) 423 dest_dir = misc.CanonicalizePath(args.dest_dir) 424 (bot_result, build_id) = FindBuildId(description) 425 if bot_result > 0 and build_id > 0: 426 logger.GetLogger().LogError("Remote trybot failed but image was generated") 427 bot_result = 1 428 elif bot_result > 0: 429 logger.GetLogger().LogError("Remote trybot failed. No image was generated") 430 return 2 431 if "toolchain" in branch: 432 chromeos_version = FindVersionForToolchain(branch, chromeos_root) 433 assert not manifest_versions.IsRFormatCrosVersion(chromeos_version) 434 DownloadImage(target, build_id, dest_dir, chromeos_version) 435 ret = UnpackImage(dest_dir) 436 if ret != 0: 437 return 2 438 # todo: return a more inteligent return value 439 if not args.replace_sysroot: 440 return bot_result 441 442 ret = ReplaceSysroot(chromeos_root, args.dest_dir, target) 443 if ret != 0: 444 return 2 445 446 # got an image and we were successful in placing it where requested 447 return bot_result 448 449if __name__ == "__main__": 450 retval = Main(sys.argv) 451 sys.exit(retval) 452