blob: ce4a1b8109a628dbd9ebea9bd5fe2a34e046a010 [file]
"""Step class for uploading new CIPD packages."""
from __future__ import absolute_import
import logging
import os
from helpers import cipd_upload_utils
from slave import base_step
class CIPDUploadError(Exception):
"""Raised when an error occurs with CIPDUploadStep."""
class CIPDUploadStep(base_step.BaseStep):
"""Uploads a new CIPD package."""
def __init__(self, pkg_def, pkg_vars=None, refs=None, tags=None,
verification_timeout=None, step_name='Upload CIPD Package',
**kwargs):
"""Constructor.
Args:
pkg_def: The path to the pkg_def file relative to the build root.
pkg_vars: A dict of variables to pass to cipd with -pkg-var.
refs: A list of refs to be assigned to the uploaded cipd package.
tags: A dict of tags to be assigned to the uploaded cipd package.
verification_timeout: Time allotted for cipd verification step ('XXmXXs').
step_name: name of the Buildbot step. Used for uniquely identifying the
step and for display in the builder status.
**kwargs: Any additional args to pass to BaseStep.
"""
base_step.BaseStep.__init__(self,
name=step_name,
**kwargs)
self._cipd_upload = self.build_system == 'catabuilder'
if not self._cipd_upload:
self._name += ' DRY RUN'
self._pkg_def_path = pkg_def
self._pkg_vars = pkg_vars
self._refs = refs
self._tags = self._GetDefaultTags()
self._tags.update(tags or {})
self._verification_timeout = verification_timeout
def _GetDefaultTags(self):
"""Returns a dict of default tags to be included on all CIPD uploads."""
tags = {}
tags['system'] = '_'.join([self.build_system, self.env])
if 'buildset' in self._properties:
tags['buildset'] = self.get_property('buildset')
return tags
# TODO(b/144364136) remove support JSON support after migration to GCE
def _GetServiceAccountJson(self):
"""Returns path to service_account_json param for current environment."""
path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
if not path and self._cipd_upload:
path = ':gce'
return path
def run(self):
"""Uploads the designated CIPD package.
Returns:
True iff CIPD package is uploaded successfully.
"""
service_account_json = self._GetServiceAccountJson()
returncode, _, stderr = cipd_upload_utils.UploadCIPDPackage(
self,
self._pkg_def_path,
pkg_vars=self._pkg_vars,
refs=self._refs,
tags=self._tags,
verification_timeout=self._verification_timeout,
service_account_json=service_account_json,
dry_run=not self._cipd_upload)
if returncode == 0:
if self._cipd_upload:
logging.info('Package uploaded successfully: %s', self._pkg_def_path)
return True
message = 'CIPD upload for {} exited with nonzero returncode {}. {}'.format(
self._pkg_def_path, returncode, stderr)
logging.error(message)
self.add_review({'message': message})
return False