Exemplo n.º 1
0
    def invoke(self,
               securitytoken=None,
               block=False,
               skip_if_running=False,
               invoke_pre_check_delay=3,
               invoke_block_delay=15,
               build_params=None,
               cause=None,
               files=None):
        assert isinstance(invoke_pre_check_delay, (int, float))
        assert isinstance(invoke_block_delay, (int, float))
        assert isinstance(block, bool)
        assert isinstance(skip_if_running, bool)

        # Create a new invocation instance
        invocation = Invocation(self)

        # Either copy the params dict or make a new one.
        build_params = build_params and dict(build_params.items()) or {
        }  # Via POSTed JSON
        params = {}  # Via Get string

        with invocation:
            if len(self.get_params_list()) == 0:
                if self.is_queued():
                    raise WillNotBuild('%s is already queued' % repr(self))

                elif self.is_running():
                    if skip_if_running:
                        log.warn(
                            "Will not request new build because %s is already running",
                            self.name)
                    else:
                        log.warn(
                            "Will re-schedule %s even though it is already running",
                            self.name)
            elif self.has_queued_build(build_params):
                msg = 'A build with these parameters is already queued.'
                raise WillNotBuild(msg)

            log.info("Attempting to start %s on %s", self.name,
                     repr(self.get_jenkins_obj()))

            url = self.get_build_triggerurl()
            # If job has file parameters - it must be triggered
            # using "/build", not by "/buildWithParameters"
            # "/buildWithParameters" will ignore non-file parameters
            if files:
                url = "%s/build" % self.baseurl

            if cause:
                build_params['cause'] = cause

            if securitytoken:
                params['token'] = securitytoken

            build_params['json'] = self.mk_json_from_build_parameters(
                build_params, files)
            data = build_params

            response = self.jenkins.requester.post_and_confirm_status(
                url, data=data, params=params, files=files, valid=[200, 201])
            response = response
            if invoke_pre_check_delay > 0:
                log.info("Waiting for %is to allow Jenkins to catch up",
                         invoke_pre_check_delay)
                sleep(invoke_pre_check_delay)
            if block:
                total_wait = 0

                while self.is_queued():
                    log.info("Waited %is for %s to begin...", total_wait,
                             self.name)
                    sleep(invoke_block_delay)
                    total_wait += invoke_block_delay
                if self.is_running():
                    running_build = self.get_last_build()
                    running_build.block_until_complete(
                        delay=invoke_pre_check_delay)
        return invocation
Exemplo n.º 2
0
    def invoke(self, securitytoken=None, block=False, skip_if_running=False, invoke_pre_check_delay=3, invoke_block_delay=15, build_params=None, cause=None):
        assert isinstance(invoke_pre_check_delay, (int, float))
        assert isinstance(invoke_block_delay, (int, float))
        assert isinstance(block, bool)
        assert isinstance(skip_if_running, bool)

        # Create a new invocation instance
        invocation = Invocation(self)

        # Either copy the params dict or make a new one.
        build_params = build_params and dict(
            build_params.items()) or {}  # Via POSTed JSON
        params = {}  # Via Get string

        with invocation:
            if self.is_queued():
                raise WillNotBuild('%s is already queued' % repr(self))

            elif self.is_running():
                if skip_if_running:
                    log.warn(
                        "Will not request new build because %s is already running", self.name)
                else:
                    log.warn(
                        "Will re-schedule %s even though it is already running", self.name)

            log.info("Attempting to start %s on %s", self.name, repr(
                self.get_jenkins_obj()))

            url = self.get_build_triggerurl()

            if cause:
                build_params['cause'] = cause

            if securitytoken:
                params['token'] = securitytoken

            response = self.jenkins.requester.post_and_confirm_status(
                url,
                data={'json': self.mk_json_from_build_parameters(
                    build_params)},  # See above - build params have to be JSON encoded & posted.
                params=params,
                valid=[200, 201]
            )
            if invoke_pre_check_delay > 0:
                log.info(
                    "Waiting for %is to allow Jenkins to catch up", invoke_pre_check_delay)
                sleep(invoke_pre_check_delay)
            if block:
                total_wait = 0

                while self.is_queued():
                    log.info(
                        "Waited %is for %s to begin...", total_wait, self.name)
                    sleep(invoke_block_delay)
                    total_wait += invoke_block_delay
                if self.is_running():
                    running_build = self.get_last_build()
                    running_build.block_until_complete(
                        delay=invoke_pre_check_delay)
        return invocation