Esempio n. 1
0
File: ci.py Progetto: hysds/mozart
    def get(self, job_name=None):
        """Checks if Jenkins job exists"""
        if job_name is None:
            repo = request.args.get('repo')
            branch = request.args.get('branch')
            if repo is None:
                return {
                    'success':
                    False,
                    'message':
                    'repo must be supplied if Jenkins job_name not given'
                }, 400

            app.logger.info("repo: %s" % repo)
            app.logger.info("branch: %s" % branch)
            job_name = get_ci_job_name(repo, branch)
            if job_name is None:
                return {
                    'success': False,
                    'message': "Failed to parse repo owner and name: %s" % repo
                }, 400

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
            return {'success': True, 'message': 'job found: %s' % job_name}
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404
Esempio n. 2
0
File: ci.py Progetto: hysds/mozart
    def delete(self):
        """deletes Jenkins job"""
        repo = request.args.get('repo')
        branch = request.args.get('branch')
        app.logger.info("repo: %s" % repo)
        app.logger.info("branch: %s" % branch)

        if repo is None:
            return {'success': True, 'message': 'repo not supplied'}, 400

        job_name = get_ci_job_name(repo, branch)
        if job_name is None:
            return {
                'success': False,
                'message': "Failed to parse repo owner and name: %s" % repo
            }, 400
        app.logger.info("jenkins job name: %s" % job_name)

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404

        try:
            jenkins_wrapper.delete_job(job_name)
        except JenkinsException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'unable to delete job: %s' % job_name
            }, 500

        return {'success': True, 'message': 'job deleted %s' % job_name}
Esempio n. 3
0
File: ci.py Progetto: hysds/mozart
    def delete(self, job_name=None, build_number=None):
        """Remove Jenkins build"""
        if job_name is None:  # get job_name from request query parameters if not supplied in url
            repo = request.args.get("repo", None)
            branch = request.args.get("branch", None)
            app.logger.info("repo %s" % repo)
            app.logger.info("branch %s" % branch)
            if repo is None:
                return {
                    'success':
                    False,
                    'message':
                    'repo must be supplied if job_name is not given (url parameter)'
                }, 400

            job_name = get_ci_job_name(repo, branch)
            if job_name is None:
                return {
                    'success': False,
                    'message': "Failed to parse repo owner and name: %s" % repo
                }, 400
        app.logger.info("jenkins job name: %s" % job_name)

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404

        job_info = jenkins_wrapper.get_job_info(job_name)

        if build_number is None:
            build_number = request.args.get('build_number')
            if build_number is None:  # get most recent build number if not supplied
                app.logger.info(
                    "build_number not supplied for %s, will look for latest build_number"
                    % job_name)
                last_build = job_info['lastBuild']
                if not last_build:
                    return {
                        'success':
                        False,
                        'message':
                        'job has never been built %s, submit a build with /build (POST)'
                        % job_name
                    }, 404
                build_number = last_build['number']
            else:
                if build_number.isdigit():
                    build_number = int(build_number)
                else:
                    return {
                        'success': False,
                        'message': 'build_number is not an integer'
                    }, 400
        app.logger.info("build number %d" % build_number)

        try:
            build_info = jenkins_wrapper.get_build_info(job_name, build_number)
            if build_info['building'] is True:
                return {
                    'success':
                    False,
                    'message':
                    'job is building: %s (%d), please stop build before removing'
                    % (job_name, build_number)
                }, 400
            """
            python-jenkins delete_build() breaking, so will fallback to using a request in the meantime:
                https://www.mail-archive.com/[email protected]/msg00533.html
            curl -u <user>:<token> -X POST https://<jenkins_url>/job/<job_name>/<build_number>/doDelete
            """
            jenkins_host = app.config.get('JENKINS_HOST', '')
            jenkins_user = app.config.get('JENKINS_USER', '')
            jenkins_token = app.config.get('JENKINS_API_KEY', '')
            if not jenkins_host or not jenkins_user or not jenkins_token:
                return {
                    'success': False,
                    'message': 'interval server error (missing configuration)'
                }, 500

            base_endpoint = os.path.join(jenkins_host, 'job')
            endpoint = '%s/%s/%d/doDelete' % (base_endpoint, job_name,
                                              build_number)
            req = requests.post(endpoint, auth=(jenkins_user, jenkins_token))
            req.raise_for_status()
            return {
                'success': True,
                'message':
                'job build deleted %s (%d)' % (job_name, build_number)
            }
        except (NotFoundException, JenkinsException) as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message':
                'job build not found %s (%d)' % (job_name, build_number)
            }, 404
        except (requests.exceptions.HTTPError, Exception) as e:
            app.logger.error('unable to delete job build %s (%d)' %
                             (job_name, build_number))
            app.logger.error(str(e))
            return {
                'success':
                False,
                'message':
                'unable to delete job build %s (%d)' % (job_name, build_number)
            }, 500
Esempio n. 4
0
File: ci.py Progetto: hysds/mozart
    def get(self, job_name=None, build_number=None):
        """Get current status of Jenkins build"""
        if job_name is None:  # get job_name from request query parameters if not supplied in url
            repo = request.args.get("repo", None)
            branch = request.args.get("branch", None)
            app.logger.info("repo %s" % repo)
            app.logger.info("branch %s" % branch)
            if repo is None:
                return {
                    'success':
                    False,
                    'message':
                    'repo must be supplied if job_name is not given (url parameter)'
                }, 400

            job_name = get_ci_job_name(repo, branch)
            if job_name is None:
                return {
                    'success': False,
                    'message': "Failed to parse repo owner and name: %s" % repo
                }, 400
        app.logger.info("jenkins job name: %s" % job_name)

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404

        if build_number is None:
            build_number = request.args.get('build_number')
            if build_number is None:  # get most recent build number if not supplied
                app.logger.info(
                    "build_number not supplied for %s, will look for latest build_number"
                    % job_name)
                job_info = jenkins_wrapper.get_job_info(job_name)
                last_build = job_info['lastBuild']
                if not last_build:
                    return {
                        'success':
                        False,
                        'message':
                        'no builds listed for %s, submit a build' % job_name
                    }, 404
                build_number = last_build['number']
            else:
                if build_number.isdigit():
                    build_number = int(build_number)
                else:
                    return {
                        'success': False,
                        'message': 'build_number is not an integer'
                    }, 400
        app.logger.info("build number %d" % build_number)

        build_info = jenkins_wrapper.get_build_info(job_name, build_number)

        return {
            'job_name': job_name,
            'build_number': build_number,
            'result': build_info.get('result', None),
            'timestamp': build_info.get('timestamp', None),
            'url': build_info.get('url', None),
            'duration': build_info.get('duration', None),
            'building': build_info.get('building', None),
        }
Esempio n. 5
0
File: ci.py Progetto: hysds/mozart
    def delete(self, job_name=None):
        """stop Jenkins build"""
        if job_name is None:
            repo = request.args.get('repo')
            branch = request.args.get('branch')
            if repo is None:
                return {
                    'success':
                    False,
                    'message':
                    'repo must be supplied if Jenkins job_name not given'
                }, 400

            app.logger.info("repo: %s" % repo)
            app.logger.info("branch: %s" % branch)
            job_name = get_ci_job_name(repo, branch)
            if job_name is None:
                return {
                    'success': False,
                    'message': "Failed to parse repo owner and name: %s" % repo
                }, 400
        app.logger.info("jenkins job name: %s" % job_name)

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404

        job_info = jenkins_wrapper.get_job_info(job_name)

        if job_info['inQueue'] is True:
            queue_id = job_info['queueItem']['id']
            jenkins_wrapper.cancel_queue(queue_id)
            return {
                'success': True,
                'message':
                'job %s removed from queue %d' % (job_name, queue_id)
            }

        # check if job is running
        if job_info['lastBuild']:
            build_number = job_info['lastBuild']['number']
            build_info = jenkins_wrapper.get_build_info(job_name, build_number)
            if build_info['building'] is True:
                jenkins_wrapper.stop_build(job_name, build_number)
                return {
                    'success': True,
                    'message': 'job build stopped: %s' % job_name
                }
            else:  # job is currently not building (or already finished building)
                return {
                    'success': True,
                    'message': 'job is currently not building: %s' % job_name
                }

        return {
            'success': False,
            'message': 'job has no previous and current builds'
        }, 403
Esempio n. 6
0
File: ci.py Progetto: hysds/mozart
    def post(self, job_name=None):
        """start Jenkins build"""
        request_data = request.json or request.form

        if job_name is None:
            repo = request_data.get('repo')
            branch = request_data.get('branch')
            if repo is None:
                return {
                    'success':
                    False,
                    'message':
                    'repo must be supplied if Jenkins job_name not given'
                }, 400

            app.logger.info("repo: %s" % repo)
            app.logger.info("branch: %s" % branch)
            job_name = get_ci_job_name(repo, branch)
            if job_name is None:
                return {
                    'success': False,
                    'message': "Failed to parse repo owner and name: %s" % repo
                }, 400
        app.logger.info("jenkins job name: %s" % job_name)
        input_params = request_data.get('parameters', {})
        app.logger.info("parameters: %s" % input_params)

        try:
            job_found = jenkins_wrapper.job_exists(job_name)
            if not job_found:
                raise NotFoundException
        except NotFoundException as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'jenkins job not found: %s' % job_name
            }, 404

        job_info = jenkins_wrapper.get_job_info(job_name)
        next_build_number = job_info['nextBuildNumber']

        if job_info['inQueue'] is True:  # check if in queue
            return {
                'success':
                True,
                'message':
                'job is currently in queue: %s (%d)' %
                (job_name, next_build_number)
            }, 203

        # check if job is running
        if job_info['lastBuild'] is not None:
            build_number = job_info['lastBuild']['number']
            build_info = jenkins_wrapper.get_build_info(job_name, build_number)
            if build_info['building'] is True:
                return {
                    'success':
                    True,
                    'message':
                    'job is already building: %s (%d)' %
                    (job_name, build_number)
                }, 203

        default_params = {}
        for prop in job_info['property']:
            if prop['_class'] == 'hudson.model.ParametersDefinitionProperty':
                params = prop['parameterDefinitions']
                for param in params:
                    param_name = param['name']
                    default_params[param_name] = param[
                        'defaultParameterValue']['value']

        input_params = {
            **default_params,
            **input_params
        }  # combining parameters
        try:
            queue = jenkins_wrapper.build_job(job_name,
                                              parameters=input_params)
            app.logger.info('job build submitted %s' % job_name)
            return {
                'success':
                True,
                'message':
                'job successfully submitted to build %s (%d)' %
                (job_name, next_build_number),
                'queue':
                queue,
                'build_number':
                next_build_number
            }
        except (requests.exceptions.HTTPError, Exception) as e:
            app.logger.error(str(e))
            return {
                'success': False,
                'message': 'job failed to submit build %s' % job_name
            }, 400