コード例 #1
0
ファイル: util.py プロジェクト: Recras/jenkins-autojobs
class JenkinsControl(object):
    war = pjoin(here, "tmp/jenkins.war")
    cli = pjoin(here, "tmp/jenkins-cli.jar")
    home = pjoin(here, "tmp/jenkins")

    def __init__(self, addr="127.0.0.1:60888", cport="60887"):
        self.addr, self.port = addr.split(":")
        self.url = "http://%s" % addr
        self.py = Jenkins(self.url)

    def start_server(self):
        cmd = pjoin(here, "./bin/start-jenkins.sh 1>/dev/null 2>&1")
        env = {
            "JENKINS_HOME": self.home,
            "JENKINS_PORT": self.port,
            "JENKINS_CPORT": self.cport,
            "JENKINS_ADDR": self.addr,
        }
        check_call(cmd, shell=True, env=env)

    def shutdown_server(self):
        cmd = "echo 0 | nc %s %s" % (self.addr, self.cport)
        check_call(cmd, shell=True)

    def clean_home(self):
        rmtree(self.home)

    def createjob(self, name, configxml_fn):
        configxml = open(configxml_fn).read().encode("utf8")
        self.py.job_create(name, configxml)

    def getjobs(self):
        return {i.name: i for i in self.py.jobs}

    def enabled(self, name):
        return self.py.job(name).info["buildable"]

    def job_etree(self, job):
        res = self.py.job(job).config
        res = etree.fromstring(res)
        return res
コード例 #2
0
ファイル: util.py プロジェクト: sja/jenkins-autojobs
class JenkinsControl(object):
    war = pjoin(here, 'tmp/jenkins.war')
    cli = pjoin(here, 'tmp/jenkins-cli.jar')
    home = pjoin(here, 'tmp/jenkins')

    def __init__(self, addr='127.0.0.1:60888', cport='60887'):
        self.addr, self.port = addr.split(':')
        self.url = 'http://%s' % addr
        self.py = Jenkins(self.url)

    def start_server(self):
        cmd = pjoin(here, './bin/start-jenkins.sh 1>/dev/null 2>&1')
        env = {
            'JENKINS_HOME': self.home,
            'JENKINS_PORT': self.port,
            'JENKINS_CPORT': self.cport,
            'JENKINS_ADDR': self.addr
        }
        check_call(cmd, shell=True, env=env)

    def shutdown_server(self):
        cmd = 'echo 0 | nc %s %s' % (self.addr, self.cport)
        check_call(cmd, shell=True)

    def clean_home(self):
        rmtree(self.home)

    def createjob(self, name, configxml_fn):
        configxml = open(configxml_fn).read().encode('utf8')
        self.py.job_create(name, configxml)

    def getjobs(self):
        return {i.name: i for i in self.py.jobs}

    def enabled(self, name):
        return self.py.job(name).info['buildable']

    def job_etree(self, job):
        res = self.py.job(job).config
        res = etree.fromstring(res)
        return res
コード例 #3
0
ファイル: util.py プロジェクト: ThomasMatern/jenkins-autojobs
class JenkinsControl(object):
    war = pjoin(here, 'tmp/jenkins.war')
    cli = pjoin(here, 'tmp/jenkins-cli.jar')
    home = pjoin(here, 'tmp/jenkins')

    def __init__(self, addr='127.0.0.1:60888', cport='60887'):
        self.addr, self.port = addr.split(':')
        self.url = 'http://%s' % addr
        self.py = Jenkins(self.url)

    def start_server(self):
        cmd = pjoin(here, './bin/start-jenkins.sh 1>/dev/null 2>&1')
        env = {'JENKINS_HOME':  self.home,
               'JENKINS_PORT':  self.port,
               'JENKINS_CPORT': self.cport,
               'JENKINS_ADDR':  self.addr}
        check_call(cmd, shell=True, env=env)

    def shutdown_server(self):
        cmd = 'echo 0 | nc %s %s' % (self.addr, self.cport)
        check_call(cmd, shell=True)

    def clean_home(self):
        rmtree(self.home)

    def createjob(self, name, configxml_fn):
        configxml = open(configxml_fn).read().encode('utf8')
        self.py.job_create(name, configxml)

    def getjobs(self):
        return {i.name: i for i in self.py.jobs}

    def enabled(self, name):
        return self.py.job(name).info['buildable']

    def job_etree(self, job):
        res = self.py.job(job).config
        res = etree.fromstring(res)
        return res
コード例 #4
0
ファイル: jenkins_script.py プロジェクト: jkunle/paul
class JenkinsTrigger(object):
    """
    A class to trigger builds on Jenkins and print the results.

    :param token: The token we need to trigger the build. If you do not have this token, ask in IRC.
    """
    def __init__(self, token):
        """
        Create the JenkinsTrigger instance.
        """
        self.token = token
        self.repo_name = get_repo_name()
        self.jenkins_instance = Jenkins(JENKINS_URL)

    def trigger_build(self):
        """
        Ask our jenkins server to build the "Branch-01-Pull" job.
        """
        bzr = Popen(('bzr', 'whoami'), stdout=PIPE, stderr=PIPE)
        raw_output, error = bzr.communicate()
        # We just want the name (not the email).
        name = ' '.join(raw_output.decode().split()[:-1])
        cause = 'Build triggered by %s (%s)' % (name, self.repo_name)
        self.jenkins_instance.job(OpenLPJobs.Branch_Pull).build(
            {
                'BRANCH_NAME': self.repo_name,
                'cause': cause
            }, token=self.token)

    def print_output(self):
        """
        Print the status information of the build triggered.
        """
        print('Add this to your merge proposal:')
        print('--------------------------------')
        bzr = Popen(('bzr', 'revno'), stdout=PIPE, stderr=PIPE)
        raw_output, error = bzr.communicate()
        revno = raw_output.decode().strip()
        print('%s (revision %s)' % (get_repo_name(), revno))

        for job in OpenLPJobs.Jobs:
            if not self.__print_build_info(job):
                print('Stopping after failure')
                break

    def open_browser(self):
        """
        Opens the browser.
        """
        url = self.jenkins_instance.job(OpenLPJobs.Branch_Pull).info['url']
        # Open the url
        Popen(('xdg-open', url), stderr=PIPE)

    def __print_build_info(self, job_name):
        """
        This helper method prints the job information of the given ``job_name``

        :param job_name: The name of the job we want the information from. For example *Branch-01-Pull*. Use the class
         variables from the :class:`OpenLPJobs` class.
        """
        is_success = False
        job = self.jenkins_instance.job(job_name)
        while job.info['inQueue']:
            time.sleep(1)
        build = job.last_build
        build.wait()
        if build.info['result'] == 'SUCCESS':
            # Make 'SUCCESS' green.
            result_string = '%s%s%s' % (Colour.GREEN_START,
                                        build.info['result'], Colour.GREEN_END)
            is_success = True
        else:
            # Make 'FAILURE' red.
            result_string = '%s%s%s' % (Colour.RED_START, build.info['result'],
                                        Colour.RED_END)
        url = build.info['url']
        print('[%s] %s' % (result_string, url))
        return is_success
コード例 #5
0
ファイル: jenkins_script.py プロジェクト: imkernel/openlp
class JenkinsTrigger(object):
    """
    A class to trigger builds on Jenkins and print the results.

    :param token: The token we need to trigger the build. If you do not have this token, ask in IRC.
    """

    def __init__(self, token):
        """
        Create the JenkinsTrigger instance.
        """
        self.token = token
        self.repo_name = get_repo_name()
        self.jenkins_instance = Jenkins(JENKINS_URL)

    def trigger_build(self):
        """
        Ask our jenkins server to build the "Branch-01-Pull" job.
        """
        bzr = Popen(('bzr', 'whoami'), stdout=PIPE, stderr=PIPE)
        raw_output, error = bzr.communicate()
        # We just want the name (not the email).
        name = ' '.join(raw_output.decode().split()[:-1])
        cause = 'Build triggered by %s (%s)' % (name, self.repo_name)
        self.jenkins_instance.job(OpenLPJobs.Branch_Pull).build({'BRANCH_NAME': self.repo_name, 'cause': cause},
                                                                token=self.token)

    def print_output(self):
        """
        Print the status information of the build triggered.
        """
        print('Add this to your merge proposal:')
        print('--------------------------------')
        bzr = Popen(('bzr', 'revno'), stdout=PIPE, stderr=PIPE)
        raw_output, error = bzr.communicate()
        revno = raw_output.decode().strip()
        print('%s (revision %s)' % (get_repo_name(), revno))

        for job in OpenLPJobs.Jobs:
            if not self.__print_build_info(job):
                print('Stopping after failure')
                break

    def open_browser(self):
        """
        Opens the browser.
        """
        url = self.jenkins_instance.job(OpenLPJobs.Branch_Pull).info['url']
        # Open the url
        Popen(('xdg-open', url), stderr=PIPE)

    def __print_build_info(self, job_name):
        """
        This helper method prints the job information of the given ``job_name``

        :param job_name: The name of the job we want the information from. For example *Branch-01-Pull*. Use the class
         variables from the :class:`OpenLPJobs` class.
        """
        is_success = False
        job = self.jenkins_instance.job(job_name)
        while job.info['inQueue']:
            time.sleep(1)
        build = job.last_build
        build.wait()
        if build.info['result'] == 'SUCCESS':
            # Make 'SUCCESS' green.
            result_string = '%s%s%s' % (Colour.GREEN_START, build.info['result'], Colour.GREEN_END)
            is_success = True
        else:
            # Make 'FAILURE' red.
            result_string = '%s%s%s' % (Colour.RED_START, build.info['result'], Colour.RED_END)
        url = build.info['url']
        print('[%s] %s' % (result_string, url))
        return is_success