api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # Copyright 2014 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Recipe module to ensure a checkout is consistant on a bot."""
  5. from recipe_engine import recipe_api
  6. class BotUpdateApi(recipe_api.RecipeApi):
  7. def __init__(self, issue, patchset, repository, gerrit_ref, rietveld,
  8. revision, parent_got_revision, deps_revision_overrides,
  9. fail_patch, *args, **kwargs):
  10. self._issue = issue
  11. self._patchset = patchset
  12. self._repository = repository
  13. self._gerrit_ref = gerrit_ref
  14. self._rietveld = rietveld
  15. self._revision = revision
  16. self._parent_got_revision = parent_got_revision
  17. self._deps_revision_overrides = deps_revision_overrides
  18. self._fail_patch = fail_patch
  19. self._last_returned_properties = {}
  20. super(BotUpdateApi, self).__init__(*args, **kwargs)
  21. def __call__(self, name, cmd, **kwargs):
  22. """Wrapper for easy calling of bot_update."""
  23. assert isinstance(cmd, (list, tuple))
  24. bot_update_path = self.resource('bot_update.py')
  25. kwargs.setdefault('infra_step', True)
  26. kwargs.setdefault('env', {})
  27. kwargs['env'].setdefault('PATH', '%(PATH)s')
  28. kwargs['env']['PATH'] = self.m.path.pathsep.join([
  29. kwargs['env']['PATH'], str(self._module.PACKAGE_REPO_ROOT)])
  30. return self.m.python(name, bot_update_path, cmd, **kwargs)
  31. @property
  32. def last_returned_properties(self):
  33. return self._last_returned_properties
  34. # DO NOT USE.
  35. # The below method will be removed after there are no more callers of
  36. # tryserver.maybe_apply_issue (skbug.com/5588).
  37. def apply_gerrit_ref(self, root, gerrit_no_reset=False,
  38. gerrit_no_rebase_patch_ref=False, **kwargs):
  39. apply_gerrit_path = self.resource('apply_gerrit.py')
  40. kwargs.setdefault('infra_step', True)
  41. kwargs.setdefault('env', {}).setdefault('PATH', '%(PATH)s')
  42. kwargs['env']['PATH'] = self.m.path.pathsep.join([
  43. kwargs['env']['PATH'], str(self._module.PACKAGE_REPO_ROOT)])
  44. cmd = [
  45. '--gerrit_repo', self._repository,
  46. '--gerrit_ref', self._gerrit_ref or '',
  47. '--root', str(root),
  48. ]
  49. if gerrit_no_reset:
  50. cmd.append('--gerrit_no_reset')
  51. if gerrit_no_rebase_patch_ref:
  52. cmd.append('--gerrit_no_rebase_patch_ref')
  53. return self.m.python('apply_gerrit', apply_gerrit_path, cmd, **kwargs)
  54. def ensure_checkout(self, gclient_config=None, suffix=None,
  55. patch=True, update_presentation=True,
  56. patch_root=None, no_shallow=False,
  57. with_branch_heads=False, refs=None,
  58. patch_oauth2=False, oauth2_json=False,
  59. use_site_config_creds=True,
  60. output_manifest=True, clobber=False,
  61. root_solution_revision=None, rietveld=None, issue=None,
  62. patchset=None, gerrit_no_reset=False,
  63. gerrit_no_rebase_patch_ref=False, **kwargs):
  64. """
  65. Args:
  66. use_site_config_creds: If the oauth2 credentials are in the buildbot
  67. site_config. See crbug.com/624212 for more information.
  68. gclient_config: The gclient configuration to use when running bot_update.
  69. If omitted, the current gclient configuration is used.
  70. rietveld: The rietveld server to use. If omitted, will infer from
  71. the 'rietveld' property.
  72. issue: The rietveld issue number to use. If omitted, will infer from
  73. the 'issue' property.
  74. patchset: The rietveld issue patchset to use. If omitted, will infer from
  75. the 'patchset' property.
  76. """
  77. refs = refs or []
  78. # We can re-use the gclient spec from the gclient module, since all the
  79. # data bot_update needs is already configured into the gclient spec.
  80. cfg = gclient_config or self.m.gclient.c
  81. assert cfg is not None, (
  82. 'missing gclient_config or forgot api.gclient.set_config(...) before?')
  83. # Only one of these should exist.
  84. assert not (oauth2_json and patch_oauth2)
  85. # Construct our bot_update command. This basically be inclusive of
  86. # everything required for bot_update to know:
  87. root = patch_root
  88. if root is None:
  89. root = self.m.gclient.calculate_patch_root(
  90. self.m.properties.get('patch_project'), cfg)
  91. if patch:
  92. issue = issue or self._issue
  93. patchset = patchset or self._patchset
  94. gerrit_repo = self._repository
  95. gerrit_ref = self._gerrit_ref
  96. else:
  97. # The trybot recipe sometimes wants to de-apply the patch. In which case
  98. # we pretend the issue/patchset never existed.
  99. issue = patchset = email_file = key_file = None
  100. gerrit_repo = gerrit_ref = None
  101. # Issue and patchset must come together.
  102. if issue:
  103. assert patchset
  104. if patchset:
  105. assert issue
  106. # The gerrit_ref and gerrit_repo must be together or not at all. If one is
  107. # missing, clear both of them.
  108. if not gerrit_ref or not gerrit_repo:
  109. gerrit_repo = gerrit_ref = None
  110. assert (gerrit_ref != None) == (gerrit_repo != None)
  111. # Point to the oauth2 auth files if specified.
  112. # These paths are where the bots put their credential files.
  113. oauth2_json_file = email_file = key_file = None
  114. if oauth2_json:
  115. if self.m.platform.is_win:
  116. oauth2_json_file = 'C:\\creds\\refresh_tokens\\rietveld.json'
  117. else:
  118. oauth2_json_file = '/creds/refresh_tokens/rietveld.json'
  119. elif patch_oauth2:
  120. # TODO(martiniss): remove this hack :(. crbug.com/624212
  121. if use_site_config_creds:
  122. email_file = self.m.path['build'].join(
  123. 'site_config', '.rietveld_client_email')
  124. key_file = self.m.path['build'].join(
  125. 'site_config', '.rietveld_secret_key')
  126. else: #pragma: no cover
  127. #TODO(martiniss): make this use path.join, so it works on windows
  128. email_file = '/creds/rietveld/client_email'
  129. key_file = '/creds/rietveld/secret_key'
  130. # Allow patch_project's revision if necessary.
  131. # This is important for projects which are checked out as DEPS of the
  132. # gclient solution.
  133. self.m.gclient.set_patch_project_revision(
  134. self.m.properties.get('patch_project'), cfg)
  135. rev_map = cfg.got_revision_mapping.as_jsonish()
  136. flags = [
  137. # What do we want to check out (spec/root/rev/rev_map).
  138. ['--spec', self.m.gclient.config_to_pythonish(cfg)],
  139. ['--patch_root', root],
  140. ['--revision_mapping_file', self.m.json.input(rev_map)],
  141. ['--git-cache-dir', cfg.cache_dir],
  142. # How to find the patch, if any (issue/patchset).
  143. ['--issue', issue],
  144. ['--patchset', patchset],
  145. ['--rietveld_server', rietveld or self._rietveld],
  146. ['--gerrit_repo', gerrit_repo],
  147. ['--gerrit_ref', gerrit_ref],
  148. ['--apply_issue_email_file', email_file],
  149. ['--apply_issue_key_file', key_file],
  150. ['--apply_issue_oauth2_file', oauth2_json_file],
  151. # Hookups to JSON output back into recipes.
  152. ['--output_json', self.m.json.output()],]
  153. # Collect all fixed revisions to simulate them in the json output.
  154. # Fixed revision are the explicit input revisions of bot_update.py, i.e.
  155. # every command line parameter "--revision name@value".
  156. fixed_revisions = {}
  157. revisions = {}
  158. for solution in cfg.solutions:
  159. if solution.revision:
  160. revisions[solution.name] = solution.revision
  161. elif solution == cfg.solutions[0]:
  162. revisions[solution.name] = (
  163. self._parent_got_revision or
  164. self._revision or
  165. 'HEAD')
  166. if self.m.gclient.c and self.m.gclient.c.revisions:
  167. revisions.update(self.m.gclient.c.revisions)
  168. if cfg.solutions and root_solution_revision:
  169. revisions[cfg.solutions[0].name] = root_solution_revision
  170. # Allow for overrides required to bisect into rolls.
  171. revisions.update(self._deps_revision_overrides)
  172. for name, revision in sorted(revisions.items()):
  173. fixed_revision = self.m.gclient.resolve_revision(revision)
  174. if fixed_revision:
  175. fixed_revisions[name] = fixed_revision
  176. flags.append(['--revision', '%s@%s' % (name, fixed_revision)])
  177. # Add extra fetch refspecs.
  178. for ref in refs:
  179. flags.append(['--refs', ref])
  180. # Filter out flags that are None.
  181. cmd = [item for flag_set in flags
  182. for item in flag_set if flag_set[1] is not None]
  183. if clobber:
  184. cmd.append('--clobber')
  185. if no_shallow:
  186. cmd.append('--no_shallow')
  187. if output_manifest:
  188. cmd.append('--output_manifest')
  189. if with_branch_heads or cfg.with_branch_heads:
  190. cmd.append('--with_branch_heads')
  191. if gerrit_no_reset:
  192. cmd.append('--gerrit_no_reset')
  193. if gerrit_no_rebase_patch_ref:
  194. cmd.append('--gerrit_no_rebase_patch_ref')
  195. # Inject Json output for testing.
  196. first_sln = cfg.solutions[0].name
  197. step_test_data = lambda: self.test_api.output_json(
  198. root, first_sln, rev_map, self._fail_patch,
  199. output_manifest=output_manifest, fixed_revisions=fixed_revisions)
  200. # Add suffixes to the step name, if specified.
  201. name = 'bot_update'
  202. if not patch:
  203. name += ' (without patch)'
  204. if suffix:
  205. name += ' - %s' % suffix
  206. # Ah hah! Now that everything is in place, lets run bot_update!
  207. step_result = None
  208. try:
  209. # 87 and 88 are the 'patch failure' codes for patch download and patch
  210. # apply, respectively. We don't actually use the error codes, and instead
  211. # rely on emitted json to determine cause of failure.
  212. step_result = self(name, cmd, step_test_data=step_test_data,
  213. ok_ret=(0, 87, 88), **kwargs)
  214. except self.m.step.StepFailure as f:
  215. step_result = f.result
  216. raise
  217. finally:
  218. if step_result:
  219. self._last_returned_properties = step_result.json.output.get(
  220. 'properties', {})
  221. if update_presentation:
  222. # Set properties such as got_revision.
  223. for prop_name, prop_value in (
  224. self.last_returned_properties.iteritems()):
  225. step_result.presentation.properties[prop_name] = prop_value
  226. # Add helpful step description in the step UI.
  227. if 'step_text' in step_result.json.output:
  228. step_text = step_result.json.output['step_text']
  229. step_result.presentation.step_text = step_text
  230. # Add log line output.
  231. if 'log_lines' in step_result.json.output:
  232. for log_name, log_lines in step_result.json.output['log_lines']:
  233. step_result.presentation.logs[log_name] = log_lines.splitlines()
  234. # Set the "checkout" path for the main solution.
  235. # This is used by the Chromium module to figure out where to look for
  236. # the checkout.
  237. # If there is a patch failure, emit another step that said things
  238. # failed.
  239. if step_result.json.output.get('patch_failure'):
  240. return_code = step_result.json.output.get('patch_apply_return_code')
  241. if return_code == 3:
  242. # This is download failure, hence an infra failure.
  243. # Sadly, python.failing_step doesn't support kwargs.
  244. self.m.python.inline(
  245. 'Patch failure - Try Rebasing',
  246. ('import sys;'
  247. 'print "Patch download failed. See bot_update step for'
  248. ' details";sys.exit(1)'),
  249. infra_step=True,
  250. step_test_data=lambda: self.m.raw_io.test_api.output(
  251. 'Patch download failed. See bot_update step for details',
  252. retcode=1)
  253. )
  254. else:
  255. # This is actual patch failure.
  256. self.m.tryserver.set_patch_failure_tryjob_result()
  257. self.m.python.failing_step(
  258. 'Patch failure', 'Check the bot_update step for details')
  259. # bot_update actually just sets root to be the folder name of the
  260. # first solution.
  261. if step_result.json.output['did_run']:
  262. co_root = step_result.json.output['root']
  263. cwd = kwargs.get('cwd', self.m.path['slave_build'])
  264. if 'checkout' not in self.m.path:
  265. self.m.path['checkout'] = cwd.join(*co_root.split(self.m.path.sep))
  266. return step_result