Esempio n. 1
0
    def get_json(self):
        """
        Get build data for a given job_url.

        :Returns:
            A python dict from the jenkins api response including:
            * builds: a list of dicts, each containing:
                ** building: Boolean of whether it is actively building
                ** timestamp: the time the build started
                ** number: the build id number
                ** actions: a list of 'actions', from which the only
                    item used in this script is 'parameters' which can
                    be used to find the PR number.
        """
        api_url = append_url(self.job_url, '/api/json')

        response = requests.get(
            api_url,
            params={
                "tree": ("builds[building,timestamp,"
                         "number,actions[parameters[*]]]"),
            }
        )

        response.raise_for_status()
        return response.json()
Esempio n. 2
0
    def stop_build(self, build_id):
        """
        Stops a build.

        :Args:
            build_id: id number of build to abort
        """
        build_url = append_url(self.job_url, str(build_id))
        url = append_url(build_url, "/stop")

        response = requests.post(url, auth=self.auth)

        logger.info("Aborting build #{}. Response: {}".format(
            build_id, response.status_code))

        response.raise_for_status()
        return response.ok
Esempio n. 3
0
    def stop_build(self, build_id):
        """
        Stops a build.

        :Args:
            build_id: id number of build to abort
        """
        build_url = append_url(self.job_url, str(build_id))
        url = append_url(build_url, "/stop")

        response = requests.post(url, auth=self.auth)

        logger.info("Aborting build #{}. Response: {}".format(
            build_id, response.status_code))

        response.raise_for_status()
        return response.ok
Esempio n. 4
0
def get_queue_data(jenkins_url):
    """
    Logs the current queue length for the given jenkins instance.

    Args:
        jenkins_url: the url of a jenkins instance
    """
    api_url = append_url(jenkins_url, '/queue/api/json')
    response = requests.get(api_url)
    response.raise_for_status()
    data = response.json()
    length = len(data['items'])
    fields = ['queue_length={}'.format(length)]
    return fields
Esempio n. 5
0
def get_queue_data(jenkins_url):
    """
    Logs the current queue length for the given jenkins instance.

    Args:
        jenkins_url: the url of a jenkins instance
    """
    api_url = append_url(jenkins_url, '/queue/api/json')
    response = requests.get(api_url)
    response.raise_for_status()
    data = response.json()
    length = len(data['items'])
    fields = ['queue_length={}'.format(length)]
    return fields
Esempio n. 6
0
    def update_build_desc(self, build_id, description):
        """
        Updates build description.

        :Args:
            build_id: id number of build to update
            description: the new description
        """
        build_url = append_url(self.job_url, str(build_id))
        url = append_url(build_url, "/submitDescription")

        response = requests.post(
            url,
            auth=self.auth,
            params={
                'description': description,
            },
        )

        logger.info("Updating description for build #{}. Response: {}".format(
            build_id, response.status_code))

        response.raise_for_status()
        return response.ok
Esempio n. 7
0
    def update_build_desc(self, build_id, description):
        """
        Updates build description.

        :Args:
            build_id: id number of build to update
            description: the new description
        """
        build_url = append_url(self.job_url, str(build_id))
        url = append_url(build_url, "/submitDescription")

        response = requests.post(
            url,
            auth=self.auth,
            params={
                'description': description,
            },
        )

        logger.info("Updating description for build #{}. Response: {}".format(
            build_id, response.status_code))

        response.raise_for_status()
        return response.ok
Esempio n. 8
0
def get_computer_data(jenkins_url):
    """
    Logs the current count of workers for the given jenkins instance.
    Logs counts of both busy executors and total executors.

    Args:
        jenkins_url: the url of a jenkins instance
    """
    api_url = append_url(jenkins_url, '/computer/api/json')
    response = requests.get(
        api_url,
        params={
            'tree': (
                "busyExecutors,totalExecutors,"
                "computer[displayName,offline,numExecutors]"
            )
        }
    )
    response.raise_for_status()
    data = response.json()

    fields = [
        'busy_executors_jenkins={}'.format(data['busyExecutors']),
        'total_executors_jenkins={}'.format(data['totalExecutors']),
    ]

    # displayName is made up of two parts -- the description as set in jenkins
    # ami config and the the instance id. We just want just the description
    # of workers that are online.
    worker_counts = defaultdict(int)
    for c in data['computer']:
        if not c['offline']:
            name = c['displayName'].split("(i-")[0]
            worker_counts[name] += int(c['numExecutors'])

    fields.extend([
        '{}_count={}'.format(type.strip(), count)
        for type, count in worker_counts.iteritems()
    ])

    return fields
Esempio n. 9
0
def get_computer_data(jenkins_url):
    """
    Logs the current count of workers for the given jenkins instance.
    Logs counts of both busy executors and total executors.

    Args:
        jenkins_url: the url of a jenkins instance
    """
    api_url = append_url(jenkins_url, '/computer/api/json')
    response = requests.get(api_url,
                            params={
                                'tree':
                                ("busyExecutors,totalExecutors,"
                                 "computer[displayName,offline,numExecutors]")
                            })
    response.raise_for_status()
    data = response.json()

    fields = [
        'busy_executors_jenkins={}'.format(data['busyExecutors']),
        'total_executors_jenkins={}'.format(data['totalExecutors']),
    ]

    # displayName is made up of two parts -- the description as set in jenkins
    # ami config and the the instance id. We just want just the description
    # of workers that are online.
    worker_counts = defaultdict(int)
    for c in data['computer']:
        if not c['offline']:
            name = c['displayName'].split("(i-")[0]
            worker_counts[name] += int(c['numExecutors'])

    fields.extend([
        '{}_count={}'.format(type.strip(), count)
        for type, count in worker_counts.iteritems()
    ])

    return fields
Esempio n. 10
0
    def get_json(self):
        """
        Get build data for a given job_url.

        :Returns:
            A python dict from the jenkins api response including:
            * builds: a list of dicts, each containing:
                ** building: Boolean of whether it is actively building
                ** timestamp: the time the build started
                ** number: the build id number
                ** actions: a list of 'actions', from which the only
                    item used in this script is 'parameters' which can
                    be used to find the PR number.
        """
        api_url = append_url(self.job_url, '/api/json')

        response = requests.get(api_url,
                                params={
                                    "tree": ("builds[building,timestamp,"
                                             "number,actions[parameters[*]]]"),
                                })

        response.raise_for_status()
        return response.json()