Skip to content
Snippets Groups Projects
validate.py 4.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lukas Köhler's avatar
    Lukas Köhler committed
    import os
    import time
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    from urllib import parse, request
    
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    import urllib3
    from jenkins import Jenkins
    from jenkins import JenkinsException
    
    DEBUG = False
    
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    PIPELINE_CONFIG_URL = 'https://code.fbi.h-da.de/SS18-REP-PAD2/Config/raw/master/pipelines/PipelineConfig.xml'
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    PIPELINE_REPO = 'https://code.fbi.h-da.de/SS18-REP-PAD2/Config.git'
    PIPELINE_BASE = 'pipelines'
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    JENKINSFILE_NAME = 'jenkinsfile.groovy'
    
    JENKINS_URL = 'https://jenkins-open-submit.apps.ocp.fbi.h-da.de'
    JENKINS_USERNAME = 'api'
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    JENKINS_SECRET = os.environ['JENKINS_API_TOKEN']
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    
    # Parameter that will be filled when debug is off
    USERNAME = 'istlukoeh'
    COURSE = 'PAD1'
    ASSIGNMENT = 'Praktikum 1'
    COURSE_AND_ASSIGNMENT = COURSE + '-' + ASSIGNMENT
    JOB_NAME = COURSE + '-' + ASSIGNMENT + '-' + USERNAME
    FOLDER = '/tmp/1_3wrqhvuj/'
    
    
    def validate(job):
        global FOLDER
        my_init(job)
    
        if not DEBUG:
    
            for fname in os.listdir('.'):
                if fname.endswith('Makefile'):
                    job.run_make(mandatory=True)
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    
        server = Jenkins(JENKINS_URL, JENKINS_USERNAME, JENKINS_SECRET)
        create_pipeline(server)
    
    
    Lukas Köhler's avatar
    Lukas Köhler committed
        paramFolder = FOLDER.split('/')[-1]
        if paramFolder == "":
            paramFolder = FOLDER.split('/')[-2]
    
    Lukas Köhler's avatar
    Lukas Köhler committed
        if paramFolder == "":
            paramFolder = FOLDER.split('/')[-3]
    
    Lukas Köhler's avatar
    Lukas Köhler committed
        queue_number = server.build_job(JOB_NAME, {'folder': '/opensubmit/' + paramFolder})
    
    Lukas Köhler's avatar
    Lukas Köhler committed
        queue_item = server.get_queue_item(queue_number)
    
        while 'executable' not in queue_item or not queue_item['executable']:
            print('Waiting for queue item . . .')
            time.sleep(2)
            queue_item = server.get_queue_item(queue_number)
    
        while 'number' not in queue_item['executable'] or not queue_item['executable']['number']:
            print('Waiting for queue item . . .')
            time.sleep(2)
            queue_item = server.get_queue_item(queue_number)
    
        build_number = queue_item['executable']['number']
    
        build_output = server.get_build_console_output(JOB_NAME, build_number)
        while 'Finished:' not in build_output:
            print('Waiting for complete pipeline output . . .')
            time.sleep(2)
            build_output = server.get_build_console_output(JOB_NAME, build_number)
    
        print('Finished!')
    
        job_url = JENKINS_URL + '/job/' + parse.quote(JOB_NAME) + '/' + str(build_number)
        result = '<a href="' + job_url + '" target="_blank">Jenkins results</a>'
        success = 'Finished: SUCCESS' in build_output
    
        if success:
            print('Failed!')
        else:
            print('Success')
    
        print(result)
    
        if not DEBUG:
            if success:
                job.send_pass_result(result)
            else:
                job.send_fail_result(result)
    
    
    def my_init(job):
        global USERNAME
        global COURSE
        global ASSIGNMENT
        global FOLDER
        global COURSE_AND_ASSIGNMENT
        global JOB_NAME
        if not DEBUG:
            USERNAME = job.submitter_student_id
            COURSE = job.course
            ASSIGNMENT = job.assignment
            FOLDER = job.working_dir
            COURSE_AND_ASSIGNMENT = COURSE + '-' + ASSIGNMENT
            JOB_NAME = COURSE + '-' + ASSIGNMENT + '-' + USERNAME
    
    
    Lukas Köhler's avatar
    Lukas Köhler committed
            fileNameList = [name for name in os.listdir(FOLDER) if
                            '__pycache__' not in name and '.py' not in name and '.zip' not in name]
            while len(fileNameList) == 1 and os.path.isdir(os.path.join(FOLDER, fileNameList[0])):
                FOLDER = os.path.join(FOLDER, fileNameList[0])
    
    Lukas Köhler's avatar
    Lukas Köhler committed
    
            job.working_dir = FOLDER
    
    Lukas Köhler's avatar
    Lukas Köhler committed
            os.system('scp -i /ssh/key -r ' + FOLDER + ' jenkins-ssh@jenkins-ssh:/opensubmit')
            os.system('chmod -R 777 ' + FOLDER)
    
        os.environ['PYTHONHTTPSVERIFY'] = '0'
        urllib3.disable_warnings()
    
    
    def create_pipeline(jenkins_server):
        # data = request.urlopen(
        #    PIPELINE_BASE_URL + parse.quote(COURSE) + '/' + parse.quote(ASSIGNMENT) + '.xml')
        data = request.urlopen(PIPELINE_CONFIG_URL)
        xml_file = data.read().decode("utf-8")
        xml_file = xml_file.replace('{USERNAME}', USERNAME)
        xml_file = xml_file.replace('{GIT_REPO}', PIPELINE_REPO)
        xml_file = xml_file.replace('{GIT_JENKINSFILE_PATH}',
                                    PIPELINE_BASE + '/' + COURSE + '/' + ASSIGNMENT + '/' + JENKINSFILE_NAME)
    
        try:
            jenkins_server.create_job(JOB_NAME, xml_file)
        except JenkinsException:
            print('Pipeline already exists')
    
    
    if DEBUG:
        validate(None)