Ejemplo n.º 1
0
    def _run(self):
        """ Authenticate to Jenkins and print our username to STDOUT.
            Useful for checking that our authentication credentials are
            correct. """
        jenkins = util.jenkins_connection()
        # python-jenkins does not have syntactic support for "whoami" (the
        # "/me/api/json" endpoint), so we have to hit it and parse it
        # ourselves.
        # https://review.openstack.org/307896

        whoami_url = posixpath.join(jenkins.url, 'me/api/json')
        try:
            response = jenkins.jenkins_open(Request(whoami_url))
            data = json.loads(response)
        except JenkinsException as err:
            raise SystemExit(err)

        name = data['fullName']  # Our Jenkins instance gets this from LDAP
        try:
            jenkins_version = jenkins.get_version()
        except AttributeError:
            # python-jenkins older than 0.4.1 does not have get_version().
            version_url = jenkins.server
            try:
                response = urlopen(Request(version_url))
                jenkins_version = response.info().getheader('X-Jenkins')
            except (HTTPError, BadStatusLine) as err:
                raise SystemExit(err)
        print('Hello %s from Jenkins %s' % (name, jenkins_version))
Ejemplo n.º 2
0
    def _run(self):
        """ Authenticate to Jenkins and print our username to STDOUT.
            Useful for checking that our authentication credentials are
            correct. """
        jenkins = util.jenkins_connection()
        # python-jenkins does not have syntactic support for "whoami" (the
        # "/me/api/json" endpoint), so we have to hit it and parse it
        # ourselves.
        # https://review.openstack.org/307896

        whoami_url = posixpath.join(jenkins.url, 'me/api/json')
        try:
            response = jenkins.jenkins_open(Request(whoami_url))
            data = json.loads(response)
        except JenkinsException as err:
            raise SystemExit(err)

        name = data['fullName']  # Our Jenkins instance gets this from LDAP
        try:
            jenkins_version = jenkins.get_version()
        except AttributeError:
            # python-jenkins older than 0.4.1 does not have get_version().
            version_url = jenkins.server
            try:
                response = urlopen(Request(version_url))
                if six.PY2:
                    jenkins_version = response.info().getheader('X-Jenkins')
                else:
                    jenkins_version = response.getheader('X-Jenkins')
            except (HTTPError, BadStatusLine) as err:
                raise SystemExit(err)
        print('Hello %s from Jenkins %s' % (name, jenkins_version))
Ejemplo n.º 3
0
    def watch(self, build_number):
        jenkins = util.jenkins_connection()

        build_info = jenkins.get_build_info('build-package', build_number)

        job_url = posixpath.join(jenkins.url, 'job', 'build-package',
                                 str(build_number))
        log.info('Watching %s' % job_url)

        pkg_name = self.pkg_name(build_info)

        start_seconds = build_info['timestamp'] / 1000.0
        # rcm-jenkins is uses the America/New_York timezone:
        jenkins_tz = tz.gettz('America/New_York')
        start = datetime.fromtimestamp(start_seconds, jenkins_tz)
        # If you want to convert to local time:
        # start = start.astimezone(tz.tzlocal())
        log.info('Started %s' % start.strftime("%F %r %z"))

        was_building = build_info['building']
        while build_info['building']:
            try:
                elapsed = datetime.now(jenkins_tz) - start
                # TODO: Xenial has python-humanize (humanize.naturaldelta()
                # here)
                (minutes, seconds) = divmod(elapsed.total_seconds(), 60)
                # Clear the previous line:
                msg = '\r%s building for %02d:%02d' % \
                    (pkg_name, minutes, seconds)
                sys.stdout.write(msg)
                sys.stdout.flush()
                sleep(10)
                build_info = jenkins.get_build_info('build-package',
                                                    build_number)
            except requests.exceptions.ConnectionError as e:
                print('')
                log.error('connection error: %s' % e)
                log.info('Re-try watching with `rhcephpkg watch-build %s`' %
                         build_number)
            except KeyboardInterrupt:
                print('')
                log.info('continue watching with `rhcephpkg watch-build %s`' %
                         build_number)
                raise SystemExit(1)
        if was_building:
            # The above "while" loop will not print a final newline.
            print('')

        end_millis = build_info['timestamp'] + build_info['duration']
        end_seconds = end_millis / 1000.0
        end = datetime.fromtimestamp(end_seconds, jenkins_tz)
        log.info('Ended %s' % end.strftime("%F %r %z"))

        # Show the final build result.
        if build_info['result'] == 'SUCCESS':
            log.info('result is SUCCESS')
        else:
            log.error(build_info['result'])
            raise SystemExit(1)
Ejemplo n.º 4
0
 def _run(self):
     """ Authenticate to Jenkins and print our username to STDOUT.
         Useful for checking that our authentication credentials are
         correct. """
     jenkins = util.jenkins_connection()
     data = jenkins.get_whoami()
     name = data['fullName']  # Our Jenkins instance gets this from LDAP
     jenkins_version = jenkins.get_version()
     print('Hello %s from Jenkins %s' % (name, jenkins_version))
     print('Logged in to %s' % jenkins.url)
Ejemplo n.º 5
0
    def watch(self, build_number):
        jenkins = util.jenkins_connection()

        build_info = jenkins.get_build_info('build-package', build_number)

        job_url = posixpath.join(jenkins.url, 'job', 'build-package',
                                 str(build_number))
        log.info('Watching %s' % job_url)

        pkg_name = self.pkg_name(build_info)

        start_seconds = build_info['timestamp'] / 1000.0
        # rcm-jenkins is uses the America/New_York timezone:
        jenkins_tz = tz.gettz('America/New_York')
        start = datetime.fromtimestamp(start_seconds, jenkins_tz)
        # If you want to convert to local time:
        # start = start.astimezone(tz.tzlocal())
        log.info('Started %s' % start.strftime("%F %r %z"))

        while build_info['building']:
            elapsed = datetime.now(jenkins_tz) - start
            # TODO: Xenial has python-humanize (humanize.naturaldelta() here)
            # Backport the python-humanize package for Trusty? Or drop Trusty?
            (minutes, seconds) = divmod(elapsed.total_seconds(), 60)
            # Clear the previous line:
            msg = '\r%s building for %02d:%02d' % (pkg_name, minutes, seconds)
            sys.stdout.write(msg)
            sys.stdout.flush()
            sleep(10)
            build_info = jenkins.get_build_info('build-package', build_number)

        end_millis = build_info['timestamp'] + build_info['duration']
        end_seconds = end_millis / 1000.0
        end = datetime.fromtimestamp(end_seconds, jenkins_tz)
        log.info('Ended %s' % end.strftime("%F %r %z"))

        # Show the final build result.
        if build_info['result'] == 'SUCCESS':
            log.info('result is SUCCESS')
        else:
            log.error(build_info['result'])
            raise SystemExit(1)
Ejemplo n.º 6
0
    def _run(self):
        """ Build a package in Jenkins. """
        pkg_name = util.package_name()
        branch_name = util.current_branch()
        jenkins = util.jenkins_connection()

        if branch_name.startswith('patch-queue/'):
            log.error('%s a patch-queue branch' % branch_name)
            msg = 'You can switch to the debian branch with "gbp pq switch"'
            raise SystemExit(msg)

        log.info('building %s branch %s at %s', pkg_name, branch_name,
                 posixpath.join(jenkins.url, 'job', 'build-package'))
        job_params = {'PKG_NAME': pkg_name, 'BRANCH': branch_name}

        if self._has_broken_build_job():
            jenkins.build_job = types.MethodType(_build_job_fixed, jenkins)

        jenkins.build_job('build-package', parameters=job_params,
                          token=jenkins.password)
Ejemplo n.º 7
0
    def _run(self):
        """ Build a package in Jenkins. """
        pkg_name = util.package_name()
        branch_name = util.current_branch()
        jenkins = util.jenkins_connection()

        if branch_name.startswith("patch-queue/"):
            log.error("%s a patch-queue branch" % branch_name)
            msg = 'You can switch to the debian branch with "gbp pq switch"'
            raise SystemExit(msg)

        log.info(
            "building %s branch %s at %s", pkg_name, branch_name, posixpath.join(jenkins.url, "job", "build-package")
        )
        job_params = {"PKG_NAME": pkg_name, "BRANCH": branch_name}

        if self._has_broken_build_job():
            jenkins.build_job = types.MethodType(_build_job_fixed, jenkins)

        jenkins.build_job("build-package", parameters=job_params, token=jenkins.password)
Ejemplo n.º 8
0
    def _run(self):
        """ Build a package in Jenkins. """
        pkg_name = util.package_name()
        branch_name = util.current_branch()
        jenkins = util.jenkins_connection()

        if branch_name.startswith('patch-queue/'):
            log.error('%s is a patch-queue branch' % branch_name)
            msg = 'You can switch to the debian branch with "gbp pq switch"'
            raise SystemExit(msg)

        log.info('building %s branch %s at %s', pkg_name, branch_name,
                 posixpath.join(jenkins.url, 'job', 'build-package'))
        job_params = {'PKG_NAME': pkg_name, 'BRANCH': branch_name}

        queue_number = jenkins.build_job('build-package',
                                         parameters=job_params,
                                         token=jenkins.password)

        # Job is now queued, not yet running.
        log.info('Waiting for build queue #%d' % queue_number)
        log.info('This may be safely interrupted...')
        queue_item = jenkins.get_queue_item(queue_number)
        while 'executable' not in queue_item:
            try:
                log.info('queue state: %s' % queue_item['why'])
                sleep(2)
                queue_item = jenkins.get_queue_item(queue_number)
            except KeyboardInterrupt:
                # We have no build_number, so just print a general message with
                # a basic URL for the user to check.
                print('')
                print('Build is queued for starting at %s' % jenkins.url)
                raise SystemExit(1)

        # Job is now running.
        build_number = queue_item['executable']['number']
        # Pass the rest over to the "watch-build" command.
        watcher = WatchBuild(['watch'])
        watcher.watch(build_number)