def fetch_hashes(dlrn, link, count=1):
    '''Get the commit and distro hashes for a specific promotion link'''
    logger = logging.getLogger('promoter')
    params = dlrnapi_client.PromotionQuery()
    params.promote_name = link
    try:
        api_response = dlrn.api_promotions_get(params)
    except ApiException:
        logger.error('Exception when calling api_promotions_get: %s',
                     ApiException)
        raise
    if len(api_response) == 0:
        return None
    if count <= 1:
        return api_response[0].to_dict()
    else:
        unduplicated_response = []
        for hashes in api_response:
            hashes = hashes.to_dict()
            existing_hashes = [(ex_hashes['commit_hash'],
                                ex_hashes['distro_hash'])
                               for ex_hashes in unduplicated_response]
            if (hashes['commit_hash'],
                    hashes['distro_hash']) not in existing_hashes:
                unduplicated_response.append(hashes)

        return unduplicated_response[:count]
Beispiel #2
0
    def __init__(self, config):
        """
        like all the the other inits around this code, the init will gather
        relevant information for this class and put them into local shortcuts
        :param config: The global promoter config or the reduced dlrnclient
        config
        """
        self.config = config
        # TODO(gcerami): fix credentials gathering
        dlrnapi_client.configuration.password = self.config.dlrnauth_password
        dlrnapi_client.configuration.username = self.config.dlrnauth_username
        api_client = dlrnapi_client.ApiClient(host=self.config.api_url)
        self.api_instance = dlrnapi_client.DefaultApi(api_client=api_client)
        self.last_promotions = {}

        # Variable to detect changes on the hash while we are running a
        # promotion
        self.named_hashes_map = {}

        # This way of preparing parameters and configuration is copied
        # directly from dlrnapi CLI and ansible module
        self.hashes_params = dlrnapi_client.PromotionQuery()
        self.jobs_params = dlrnapi_client.Params2()
        self.jobs_params_aggregate = dlrnapi_client.Params3()
        self.report_params = dlrnapi_client.Params3()
        self.promote_params = dlrnapi_client.Promotion()
        self.log.debug("Promoter DLRN client: API URL: {}, user: {}"
                       "".format(api_client.host,
                                 self.config.dlrnauth_username))
Beispiel #3
0
def get_last_promotion(dlrn, release, name):
    query = dlrnapi_client.PromotionQuery()
    query.promote_name = name
    promotions = dlrn.api_promotions_get(query)
    last_promotion = promotions[0].to_dict()
    last_promotion['release'] = release
    return last_promotion
Beispiel #4
0
def check_dlrn_promoted_hash(stage_info):
    ''' Check that the commit, distro hash has been promoted to
        promotion_target as recorded in DLRN. '''

    dlrn_host = stage_info['dlrn_host']
    promotion_target = stage_info['promotion_target']
    commit_hash = stage_info['promotions']['promotion_candidate'][
        'commit_hash']
    distro_hash = stage_info['promotions']['promotion_candidate'][
        'distro_hash']

    logger = logging.getLogger('TestPromoter')
    api_client = dlrnapi_client.ApiClient(host=dlrn_host)
    dlrn = dlrnapi_client.DefaultApi(api_client=api_client)
    params = dlrnapi_client.PromotionQuery()
    params.commit_hash = commit_hash
    params.distro_hash = distro_hash
    try:
        api_response = dlrn.api_promotions_get(params)
        logger.debug(api_response)
    except dlrnapi_client.rest.ApiException:
        logger.error('Exception when calling api_promotions_get: %s',
                     dlrnapi_client.rest.ApiException)
        raise

    error_message = ("Expected commit hash: {}"
                     " has not been promoted to {}."
                     "".format(commit_hash, promotion_target))
    conditions = [(promotion.promote_name == promotion_target)
                  for promotion in api_response]
    assert any(conditions), error_message
Beispiel #5
0
def check_trigger_condition(dlrn, promotion_name, wait_job_name,
                            launch_job_name):
    """
    Check if launch job has to run looking for the wait job at the latest
    DLRN promotion, the wait job has to be at success state.

    This just look at latest promotion and also don't retrigger if the
    launch_job state is not success.

    This uses to calls to the dlrnapi_client.DefaultApi:
        - api_promotions_get to get the newest  'Promotion'
        - api_repo_status_get to get the jobs from the 'Promotion'

    Parameters
    ----------
    dlrn : dlrnapi_client.DefaultApi
        The dlrn api impl to look for promotions and repo status
    promotion_name: str
        The promotion name to use, tripleo-ci-testing for example
    wait_job_name: str
        Name of the job that have to finish with success state to for
        the trigger condition to be True
    launch_job_name: str
        Name of the job that we want to trigger.

    Returns
    -------
    bool
        It will be True if the launch_job_name has to be triggered false
        otherwise
    """
    logger = logging.getLogger("dlrn-trigger")
    params = dlrnapi_client.PromotionQuery()
    params.promote_name = promotion_name
    api_response = dlrn.api_promotions_get(params)
    last_promotion = api_response[0]
    logger.debug("Selected promotion: {}".format(last_promotion))
    params = dlrnapi_client.Params2()
    params.distro_hash = last_promotion.distro_hash
    params.commit_hash = last_promotion.commit_hash
    api_response = dlrn.api_repo_status_get(params)
    logger.debug("CI status from promotion: {}".format(api_response))
    wait_job = None
    launch_job = None
    # Select the first ocurrence of wait and launch job
    for status in api_response:
        if wait_job is None and status.job_id == wait_job_name:
            wait_job = status
        elif launch_job is None and status.job_id == launch_job_name:
            launch_job = status
    logger.info("Selected wait job build: {}".format(wait_job))
    logger.info("Selected launch job build: {}".format(launch_job))
    # Trigger if job is not already trigger and wait job is fine
    return launch_job is None and wait_job is not None and wait_job.success
def check_promoted(dlrn, link, hashes):
    ''' check if hashes has ever been promoted to link'''
    logger = logging.getLogger('promoter')
    params = dlrnapi_client.PromotionQuery()
    params.commit_hash = hashes['commit_hash']
    params.distro_hash = hashes['distro_hash']
    try:
        api_response = dlrn.api_promotions_get(params)
    except ApiException:
        logger.error('Exception when calling api_promotions_get: %s',
                     ApiException)
        raise
    return any([(promotion.promote_name == link) for promotion in api_response])
def get_last_promotion(dlrn_client, release, distro, name, component=None):
    query = dlrnapi_client.PromotionQuery()
    query.promote_name = name
    if component:
        query.component = component
    promotions = dlrn_client.api_promotions_get(query)
    if promotions:
        last_promotion = promotions[0].to_dict()
        last_promotion['release'] = release
        last_promotion['distro'] = distro
        # sanitize dict
        if last_promotion['component'] is None:
            last_promotion['component'] = "none"
        return last_promotion
    return
Beispiel #8
0
def get_promotions(api_instance, options):
    params = dlrnapi_client.PromotionQuery()  # PromotionQuery
    if options.commit_hash:
        params.commit_hash = options.commit_hash
    if options.distro_hash:
        params.distro_hash = options.distro_hash
    if options.promote_name:
        params.promote_name = options.promote_name
    if options.offset:
        params.offset = options.offset
    if options.limit:
        params.limit = options.limit

    try:
        api_response = api_instance.api_promotions_get(params)
        return api_response
    except ApiException as e:
        raise e
def check_dlrn_promoted_hash(stage_info=None, **kwargs):
    """
    Check that the the supposed hash has been promoted to
    promotion_target as recorded in DLRN.
    :param stage_info: a dictionary containing parameter of the staging env
    :param kwargs: additional parameter for non-staged executions
    :return: None
    """
    if stage_info is not None:
        # We are checking a stage
        api_url = stage_info['dlrn']['server']['api_url']
        promotion_target = stage_info['dlrn']['promotion_target']
        candidate_commit = \
            stage_info['dlrn']['promotions']['promotion_candidate']
        candidate_hash = DlrnHash(source=candidate_commit)

        api_client = dlrnapi_client.ApiClient(host=api_url)
        dlrn_client = dlrnapi_client.DefaultApi(api_client=api_client)
        params = dlrnapi_client.PromotionQuery()
        params.limit = 1
        params.promote_name = promotion_target
    else:
        # We are checking production server
        # TODO(gcerami) implement this branch ?
        pass

    try:
        api_response = dlrn_client.api_promotions_get(params)
        log.debug(api_response)
    except dlrnapi_client.rest.ApiException:
        log.error('Exception when calling api_promotions_get: %s',
                  dlrnapi_client.rest.ApiException)
        raise

    error_msg = "No promotions for hash {}".format(candidate_hash)
    assert api_response != [], error_msg
    promotion_hash = DlrnHash(source=api_response[0])
    error_message = ("Expected full hash: {}"
                     " has not been promoted to {}."
                     "".format(promotion_hash.full_hash, promotion_target))
    conditions = [(promotion.promote_name == promotion_target)
                  for promotion in api_response]
    assert any(conditions), error_message
Beispiel #10
0
def get_promotions(release, promote_name):

    host = get_endpoint(release)

    api_client = dlrnapi_client.ApiClient(host=host)
    api_instance = dlrnapi_client.DefaultApi(api_client=api_client)
    params = dlrnapi_client.PromotionQuery()

    if promote_name:
        params.promote_name = promote_name

    try:
        api_response = api_instance.api_promotions_get(params)

    except ApiException as e:
        print("Exception when calling DefaultApi->api_promotions_get: %s\n" %
              e)

    return api_response
 def test_str_api_object(self):
     params = dlrnapi_client.PromotionQuery()
     str_params = str_api_object(params)
     self.assertNotIn("\n", str(str_params))