Esempio n. 1
0
    def handle_request(self, request, job_id, parts):
        """ Handle requests related to builds """
        if validators.validate_job_id(job_id) == None:
            self.log.error('Invalid job_id: %r' % job_id)
            return webob.Response(status=400, body=constants.ERROR_JOB_INVALID_ID)

        if len(parts) == 0:
            if request.method == 'GET':
                return self.get_builds(job_id)
            elif request.method == 'POST':
                return self.trigger_build(job_id)
        elif len(parts) == 1:
            if request.method == 'GET':
                return self.get_build_state(job_id, parts[0])
            elif request.method == 'DELETE':
                return self.delete_build(job_id, parts[0])
        elif parts[1] == 'artifacts':
            return self.jobs_builds_artifacts.handle_request(request, job_id, parts[0], parts[2:])
        elif len(parts) == 2:
            if parts[1] == 'state' and request.method == 'GET':
                return self.get_build_state(job_id, parts[0])
            elif parts[1] == 'state' and request.method == 'PUT':
                return self.update_build_state(request, job_id, parts[0])
            elif parts[1] == 'console' and request.method == 'GET':
                return self.get_console_log(job_id, parts[0])
            elif parts[1] == 'console' and request.method == 'POST':
                return self.update_console_log(request, job_id, parts[0])
            elif parts[1] == 'workspace' and request.method == 'GET':
                return self.get_workspace(job_id, parts[0])
            elif parts[1] == 'workspace' and request.method == 'PUT':
                return self.update_workspace(request, job_id, parts[0])
            elif parts[1] == 'workspace' and request.method == 'DELETE':
                return self.delete_workspace(job_id, parts[0])

        return webob.Response(status=400)
Esempio n. 2
0
    def handle_request(self, environ, start_response, method, job_id, build_id, parts):
        """ Handle requests related to build artifacts """
        if validators.validate_job_id(job_id) == None:
            self.log.error('Invalid job_id: %r' % job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)

        if len(parts) == 0:
            if method == 'POST':
                return self.create_or_update_artifact(environ, start_response, job_id, build_id)
            else:
                return response.send_error(start_response, 400)
        elif len(parts) == 1:
            if method == 'GET':
                return self.get_artifact(environ, start_response, job_id, build_id, parts[0])
            elif method == 'PUT':
                return self.create_or_update_artifact(environ, start_response, job_id, build_id, parts[0])
            elif method == 'DELETE':
                return self.delete_artifact(start_response, job_id, build_id, parts[0])
            else:
                return response.send_error(start_response, 400)
        elif len(parts) == 2:
            if method == 'GET':
                return self.get_artifact(environ, start_response, job_id, build_id, parts[0])
            else:
                return response.send_error(start_response, 400)

        return response.send_response(start_response, 400)
Esempio n. 3
0
    def handle_request(self, request, job_id, build_id, parts):
        """ Handle requests related to build artifacts """
        if validators.validate_job_id(job_id) == None:
            self.log.error('Invalid job_id: %r' % job_id)
            return webob.Response(status=400, body=constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return webob.Response(status=400, body=constants.ERROR_BUILD_INVALID_ID)

        if len(parts) == 0:
            if request.method == 'POST':
                return self.create_or_update_artifact(request, job_id, build_id)
        else:
            if validators.validate_artifact_id(parts[0]) != parts[0]:
                return webob.Response(status=400, body=constants.ERROR_ARTIFACT_INVALID_ID)

            if len(parts) == 1:
                if request.method == 'GET':
                    return self.get_artifact(job_id, build_id, parts[0])
                elif request.method == 'PUT':
                    return self.create_or_update_artifact(request, job_id, build_id, parts[0])
                elif request.method == 'DELETE':
                    return self.delete_artifact(job_id, build_id, parts[0])
            elif len(parts) == 2:
                if request.method == 'GET':
                    return self.get_artifact(job_id, build_id, parts[0])

        return webob.Response(status=400)
Esempio n. 4
0
    def update_workspace(self, environ, start_response, job_id, build_id):
        """ Store workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isdir(self._build_dir(job_id, build_id)):
            return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)

        ifh, data_len = request.get_request_data_handle_and_length(environ)

        try:
            ofh = open(self._build_workspace_file(job_id, build_id), 'wb')
        except IOError:
            return response.send_error(start_response, 500, constants.ERROR_BUILD_WRITE_FAILED)

        try:
            while data_len > 0:
                read_len = data_len
                if read_len > 1024*128:
                    read_len = 1024*128
                data = ifh.read(read_len)
                ofh.write(data)
                data_len = data_len - len(data)
        except IOError:
            ofh.close()
            return response.send_error(start_response, 500, constants.ERROR_BUILD_WRITE_FAILED)

        ofh.close()

        return response.send_response(start_response, 204)
Esempio n. 5
0
    def get_workspace(self, environ, start_response, job_id, build_id):
        """ Get workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isfile(self._build_workspace_file(job_id, build_id)):
            return response.send_error(start_response, 404,
                                       constants.ERROR_BUILD_NOT_FOUND)

        try:
            ifh = open(self._build_workspace_file(job_id, build_id))
        except IOError:
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_READ_FAILED)

        file_len = os.path.getsize(self._build_workspace_file(
            job_id, build_id))

        return response.send_response_file(environ, start_response, 200, ifh,
                                           file_len)
Esempio n. 6
0
 def get_build_state(self, start_response, job_id, build_id):
     """ Get job state """
     if validators.validate_job_id(job_id) != job_id:
         return response.send_error(start_response, 400,
                                    constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         return response.send_error(start_response, 400,
                                    constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404,
                                    constants.ERROR_JOB_NOT_FOUND)
     build_data = None
     for _ in range(10):
         try:
             build_state = json.load(
                 file(self._build_state_file(job_id, build_id), 'rb'))
             build_data = json.dumps({
                 'job_id': job_id,
                 'build_number': build_id,
                 'state': build_state
             })
             break
         except (OSError, ValueError):
             time.sleep(0.1)
     if not build_data:
         return response.send_error(start_response, 409,
                                    constants.ERROR_BUILD_LOCKED)
     return response.send_response(start_response, 200, build_data)
Esempio n. 7
0
    def handle_request(self, request, parts):
        """ Main dispatcher """
        if len(parts) == 0:
            if request.method == 'GET':
                return self.get_jobs()
        else:
            if validators.validate_job_id(parts[0]) == None:
                self.log.error('Invalid job_id: %r' % parts[0])
                return webob.Response(status=400, body=constants.ERROR_JOB_INVALID_ID)

            if len(parts) == 1:
                if request.method == 'GET':
                    return self.get_job_config(parts[0])
                elif request.method == 'PUT':
                    return self.create_or_update_job(request, parts[0])
                elif request.method == 'DELETE':
                    return self.delete_job(parts[0])
            elif parts[1] == 'builds':
                return self.jobs_builds.handle_request(request, parts[0], parts[2:])
            elif parts[1] == 'tags':
                return self.jobs_tags.handle_request(request, parts[0], parts[2:])
            elif len(parts) == 2 and parts[1] == 'github-webhook' and request.method == 'POST':
                return self.github_webhook_trigger(request, parts[0])

        return webob.Response(status=400)
Esempio n. 8
0
 def get_builds(self, start_response, job_id):
     """ Return all builds for a specific job """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure")
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     result = { 'builds': self._get_build_numbers(job_id) }
     if len(result['builds']) > 0:
         result['last_build_number'] = max(result['builds'])
     return response.send_response(start_response, 200, json.dumps(result))
Esempio n. 9
0
 def get_builds(self, start_response, job_id):
     """ Return all builds for a specific job """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure")
         return response.send_error(start_response, 400,
                                    constants.ERROR_JOB_INVALID_ID)
     result = {'builds': self._get_build_numbers(job_id)}
     if len(result['builds']) > 0:
         result['last_build_number'] = max(result['builds'])
     return response.send_response(start_response, 200, json.dumps(result))
Esempio n. 10
0
File: jobs.py Progetto: Ormod/distci
 def delete_job(self, start_response, job_id):
     """ Delete job """
     if validators.validate_job_id(job_id) == None:
         self.log.debug('Invalid job_id: %r' % job_id)
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if not os.path.isdir(self._job_dir(job_id)):
         return response.send_error(start_response, 404, constants.ERROR_JOB_NOT_FOUND)
     self.log.debug("delete job %s" % job_id)
     try:
         shutil.rmtree(self._job_dir(job_id))
     except OSError:
         return response.send_error(start_response, 404, constants.ERROR_JOB_NOT_FOUND)
     return response.send_response(start_response, 204)
Esempio n. 11
0
File: jobs.py Progetto: Ormod/distci
    def create_or_update_job(self, environ, start_response, job_id_param = None):
        """ Create a new job """
        try:
            job_config = json.loads(request.read_request_data(environ))
        except ValueError:
            self.log.debug('Failed to load job config')
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_PAYLOAD)
        job_id = job_config.get('job_id')
        if job_id == None:
            self.log.debug('Missing job_id')
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_PAYLOAD)
        if job_id_param and job_id_param != job_id:
            self.log.debug('Job ID mismatch: %r vs %r' % (job_id, job_id_param))
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_job_id(job_id) == None:
            self.log.debug('Invalid job_id: %r' % job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if self.zknodes:
            lock = distlocks.ZooKeeperLock(self.zknodes, 'job-lock-%s' % job_id)
            if lock.try_lock() != True:
                lock.close()
                self.log.debug("Job locked '%s'" % job_id)
                return response.send_error(start_response, 400, constants.ERROR_JOB_LOCKED)
        else:
            lock = None

        if job_id_param is None:
            try:
                os.mkdir(self._job_dir(job_id))
            except OSError:
                if lock:
                    lock.unlock()
                    lock.close()
                return response.send_error(start_response, 500, constants.ERROR_JOB_CONFIG_WRITE_FAILED)

        try:
            file(self._job_config_file(job_id), 'wb').write(json.dumps(job_config))
        except IOError:
            self.log.debug('Failed to write job config, job_id %s' % job_id)
            if lock:
                lock.unlock()
                lock.close()
            return response.send_error(start_response, 500, constants.ERROR_JOB_CONFIG_WRITE_FAILED)

        if lock:
            lock.unlock()
            lock.close()

        return response.send_response(start_response, 200 if job_id_param else 201, json.dumps({'job_id':job_id, 'config':job_config}))
Esempio n. 12
0
 def delete_build(self, start_response, job_id, build_id):
     """ Delete a specific build and all related data """
     if validators.validate_job_id(job_id) == None:
         self.log.debug('Invalid job_id: %r' % job_id)
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)
     self.log.debug("delete build %s/%s" % (job_id, build_id))
     try:
         shutil.rmtree(self._build_dir(job_id, build_id))
     except OSError:
         return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)
     return response.send_response(start_response, 204)
Esempio n. 13
0
 def update_console_log(self, environ, start_response, job_id, build_id):
     """ Append content to the console log """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure, '%s'", job_id)
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)
     try:
         file(self._console_log_file(job_id, build_id), 'ab').write(request.read_request_data(environ))
     except IOError:
         # can be ignored, we just return empty log
         pass
     return response.send_response(start_response, 204)
Esempio n. 14
0
File: jobs.py Progetto: Ormod/distci
 def get_job_config(self, start_response, job_id):
     """ Get config for a specific job """
     if validators.validate_job_id(job_id) != job_id:
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if not os.path.isdir(self._job_dir(job_id)):
         return response.send_error(start_response, 404, constants.ERROR_JOB_NOT_FOUND)
     self.log.debug("get job config %s" % job_id)
     job_data = None
     for _ in range(10):
         try:
             job_data = json.dumps({'job_id': job_id, 'config': self._load_job_config(job_id)})
             break
         except (OSError, ValueError):
             time.sleep(0.1)
     if not job_data:
         return response.send_error(start_response, 409, constants.ERROR_JOB_LOCKED)
     return response.send_response(start_response, 200, job_data)
Esempio n. 15
0
    def delete_workspace(self, start_response, job_id, build_id):
        """ Delete workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isfile(self._build_workspace_file(job_id, build_id)):
            return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)

        try:
            os.unlink(self._build_workspace_file(job_id, build_id))
        except IOError:
            return response.send_error(start_response, 500, constants.ERROR_BUILD_WRITE_FAILED)

        return response.send_response(start_response, 204)
Esempio n. 16
0
 def get_console_log(self, start_response, job_id, build_id):
     """ Return contents of the console log """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure, '%s'", job_id)
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)
     console_log = ''
     try:
         console_log = file(self._console_log_file(job_id, build_id), 'rb').read()
     except IOError:
         # can be ignored, we just return empty log
         pass
     return response.send_response(start_response, 200, console_log, content_type="text/plain")
Esempio n. 17
0
 def get_build_state(self, start_response, job_id, build_id):
     """ Get job state """
     if validators.validate_job_id(job_id) != job_id:
         return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404, constants.ERROR_JOB_NOT_FOUND)
     build_data = None
     for _ in range(10):
         try:
             build_state = json.load(file(self._build_state_file(job_id, build_id), 'rb'))
             build_data = json.dumps({'job_id': job_id, 'build_number': build_id, 'state': build_state})
             break
         except (OSError, ValueError):
             time.sleep(0.1)
     if not build_data:
         return response.send_error(start_response, 409, constants.ERROR_BUILD_LOCKED)
     return response.send_response(start_response, 200, build_data)
Esempio n. 18
0
    def get_workspace(self, environ, start_response, job_id, build_id):
        """ Get workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isfile(self._build_workspace_file(job_id, build_id)):
            return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)

        try:
            ifh = open(self._build_workspace_file(job_id, build_id))
        except IOError:
            return response.send_error(start_response, 500, constants.ERROR_BUILD_READ_FAILED)

        file_len = os.path.getsize(self._build_workspace_file(job_id, build_id))

        return response.send_response_file(environ, start_response, 200, ifh, file_len)
Esempio n. 19
0
 def update_console_log(self, environ, start_response, job_id, build_id):
     """ Append content to the console log """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure, '%s'", job_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404,
                                    constants.ERROR_BUILD_NOT_FOUND)
     try:
         file(self._console_log_file(job_id, build_id),
              'ab').write(request.read_request_data(environ))
     except IOError:
         # can be ignored, we just return empty log
         pass
     return response.send_response(start_response, 204)
Esempio n. 20
0
 def delete_build(self, start_response, job_id, build_id):
     """ Delete a specific build and all related data """
     if validators.validate_job_id(job_id) == None:
         self.log.debug('Invalid job_id: %r' % job_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404,
                                    constants.ERROR_BUILD_NOT_FOUND)
     self.log.debug("delete build %s/%s" % (job_id, build_id))
     try:
         shutil.rmtree(self._build_dir(job_id, build_id))
     except OSError:
         return response.send_error(start_response, 404,
                                    constants.ERROR_BUILD_NOT_FOUND)
     return response.send_response(start_response, 204)
Esempio n. 21
0
    def delete_workspace(self, start_response, job_id, build_id):
        """ Delete workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isfile(self._build_workspace_file(job_id, build_id)):
            return response.send_error(start_response, 404,
                                       constants.ERROR_BUILD_NOT_FOUND)

        try:
            os.unlink(self._build_workspace_file(job_id, build_id))
        except IOError:
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_WRITE_FAILED)

        return response.send_response(start_response, 204)
Esempio n. 22
0
    def handle_request(self, environ, start_response, method, job_id, build_id,
                       parts):
        """ Handle requests related to build artifacts """
        if validators.validate_job_id(job_id) == None:
            self.log.error('Invalid job_id: %r' % job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_ID)

        if len(parts) == 0:
            if method == 'POST':
                return self.create_or_update_artifact(environ, start_response,
                                                      job_id, build_id)
            else:
                return response.send_error(start_response, 400)
        elif len(parts) == 1:
            if method == 'GET':
                return self.get_artifact(environ, start_response, job_id,
                                         build_id, parts[0])
            elif method == 'PUT':
                return self.create_or_update_artifact(environ, start_response,
                                                      job_id, build_id,
                                                      parts[0])
            elif method == 'DELETE':
                return self.delete_artifact(start_response, job_id, build_id,
                                            parts[0])
            else:
                return response.send_error(start_response, 400)
        elif len(parts) == 2:
            if method == 'GET':
                return self.get_artifact(environ, start_response, job_id,
                                         build_id, parts[0])
            else:
                return response.send_error(start_response, 400)

        return response.send_response(start_response, 400)
Esempio n. 23
0
    def update_workspace(self, environ, start_response, job_id, build_id):
        """ Store workspace archive """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isdir(self._build_dir(job_id, build_id)):
            return response.send_error(start_response, 404,
                                       constants.ERROR_BUILD_NOT_FOUND)

        ifh, data_len = request.get_request_data_handle_and_length(environ)

        try:
            ofh = open(self._build_workspace_file(job_id, build_id), 'wb')
        except IOError:
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_WRITE_FAILED)

        try:
            while data_len > 0:
                read_len = data_len
                if read_len > 1024 * 128:
                    read_len = 1024 * 128
                data = ifh.read(read_len)
                ofh.write(data)
                data_len = data_len - len(data)
        except IOError:
            ofh.close()
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_WRITE_FAILED)

        ofh.close()

        return response.send_response(start_response, 204)
Esempio n. 24
0
    def update_build_state(self, environ, start_response, job_id, build_id):
        """ Update build state """
        try:
            build_state = json.loads(request.read_request_data(environ))
        except ValueError:
            self.log.error('Failed to load build state')
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_PAYLOAD)
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400, constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isdir(self._build_dir(job_id, build_id)):
            return response.send_error(start_response, 404, constants.ERROR_BUILD_NOT_FOUND)

        try:
            file(self._build_state_file(job_id, build_id), 'wb').write(json.dumps(build_state))
        except IOError:
            self.log.debug('Failed to write build state, job_id %s, build %s', job_id, build_id)
            return response.send_error(start_response, 500, constants.ERROR_BUILD_WRITE_FAILED)

        return response.send_response(start_response, 200, json.dumps({'job_id': job_id, 'build_number': int(build_id), 'state': build_state}))
Esempio n. 25
0
    def update_build_state(self, environ, start_response, job_id, build_id):
        """ Update build state """
        try:
            build_state = json.loads(request.read_request_data(environ))
        except ValueError:
            self.log.error('Failed to load build state')
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_PAYLOAD)
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if validators.validate_build_id(build_id) != build_id:
            self.log.error("Build_id validation failure, '%s'", build_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_BUILD_INVALID_ID)
        if not os.path.isdir(self._build_dir(job_id, build_id)):
            return response.send_error(start_response, 404,
                                       constants.ERROR_BUILD_NOT_FOUND)

        try:
            file(self._build_state_file(job_id, build_id),
                 'wb').write(json.dumps(build_state))
        except IOError:
            self.log.debug('Failed to write build state, job_id %s, build %s',
                           job_id, build_id)
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_WRITE_FAILED)

        return response.send_response(
            start_response, 200,
            json.dumps({
                'job_id': job_id,
                'build_number': int(build_id),
                'state': build_state
            }))
Esempio n. 26
0
 def get_console_log(self, start_response, job_id, build_id):
     """ Return contents of the console log """
     if validators.validate_job_id(job_id) == None:
         self.log.error("Job_id validation failure, '%s'", job_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_JOB_INVALID_ID)
     if validators.validate_build_id(build_id) != build_id:
         self.log.error("Build_id validation failure, '%s'", build_id)
         return response.send_error(start_response, 400,
                                    constants.ERROR_BUILD_INVALID_ID)
     if not os.path.isdir(self._build_dir(job_id, build_id)):
         return response.send_error(start_response, 404,
                                    constants.ERROR_BUILD_NOT_FOUND)
     console_log = ''
     try:
         console_log = file(self._console_log_file(job_id, build_id),
                            'rb').read()
     except IOError:
         # can be ignored, we just return empty log
         pass
     return response.send_response(start_response,
                                   200,
                                   console_log,
                                   content_type="text/plain")
Esempio n. 27
0
 def test_03_valid_job_ids(self):
     for job_id in self.valid_job_ids:
         assert validators.validate_job_id(job_id) == job_id, "Validator declined valid job_id %s" % job_id
Esempio n. 28
0
    def trigger_build(self, start_response, job_id):
        """ Trigger a new build """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400, constants.ERROR_JOB_INVALID_ID)
        if self.zknodes:
            lock = distlocks.ZooKeeperLock(self.zknodes, 'job-lock-%s' % job_id)
            if lock.try_lock() != True:
                lock.close()
                self.log.debug("Job locked '%s'" % job_id)
                return response.send_error(start_response, 400, constants.ERROR_JOB_LOCKED)
        else:
            lock = None

        build_ids = self._get_build_numbers(job_id)
        if len(build_ids) > 0:
            new_build_number = str(max(build_ids) + 1)
        else:
            new_build_number = "1"

        try:
            os.mkdir(self._build_dir(job_id, new_build_number))
        except OSError:
            if lock:
                lock.unlock()
                lock.close()
            self.log.error("Build directory creation failed")
            return response.send_error(start_response, 500, constants.ERROR_BUILD_CREATE_FAILED)

        if lock:
            lock.unlock()
            lock.close()

        build_state = { "status": "preparing" }
        try:
            file(self._build_state_file(job_id, new_build_number), 'wb').write(json.dumps(build_state))
        except IOError:
            self.log.debug('Failed to write build state, job_id %s, build %s', job_id, new_build_number)
            return response.send_error(start_response, 500, constants.ERROR_BUILD_WRITE_FAILED)

        if self.config.get('task_frontends'):
            for _ in range(10):
                task_id = self.distci_client.tasks.create()
                if task_id is not None:
                    break
            if task_id is None:
                self.log.error('Failed to create a build task')
                return response.send_error(start_response, 500, constants.ERROR_BUILD_TASK_CREATION_FAILED)

            task_description = { 'capabilities': [ 'build_control_v1' ],
                                 'job_id': job_id,
                                 'build_number': new_build_number,
                                 'status': 'pending',
                                 'id': task_id }

            for _ in range(10):
                task_details = self.distci_client.tasks.update(task_id, task_description)
                if task_details is not None:
                    break
            if task_details is None:
                self.log.error("Build task creation failed")
                return response.send_error(start_response, 500, constants.ERROR_BUILD_TASK_CREATION_FAILED)
        else:
            self.log.warn('No task frontends configured, unable to trigger build control task')

        return response.send_response(start_response, 201, json.dumps({'job_id': job_id, 'build_number': int(new_build_number), 'state': build_state}))
Esempio n. 29
0
    def trigger_build(self, start_response, job_id):
        """ Trigger a new build """
        if validators.validate_job_id(job_id) == None:
            self.log.error("Job_id validation failure, '%s'", job_id)
            return response.send_error(start_response, 400,
                                       constants.ERROR_JOB_INVALID_ID)
        if self.zknodes:
            lock = distlocks.ZooKeeperLock(self.zknodes,
                                           'job-lock-%s' % job_id)
            if lock.try_lock() != True:
                lock.close()
                self.log.debug("Job locked '%s'" % job_id)
                return response.send_error(start_response, 400,
                                           constants.ERROR_JOB_LOCKED)
        else:
            lock = None

        build_ids = self._get_build_numbers(job_id)
        if len(build_ids) > 0:
            new_build_number = str(max(build_ids) + 1)
        else:
            new_build_number = "1"

        try:
            os.mkdir(self._build_dir(job_id, new_build_number))
        except OSError:
            if lock:
                lock.unlock()
                lock.close()
            self.log.error("Build directory creation failed")
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_CREATE_FAILED)

        if lock:
            lock.unlock()
            lock.close()

        build_state = {"status": "preparing"}
        try:
            file(self._build_state_file(job_id, new_build_number),
                 'wb').write(json.dumps(build_state))
        except IOError:
            self.log.debug('Failed to write build state, job_id %s, build %s',
                           job_id, new_build_number)
            return response.send_error(start_response, 500,
                                       constants.ERROR_BUILD_WRITE_FAILED)

        if self.config.get('task_frontends'):
            for _ in range(10):
                task_id = self.distci_client.tasks.create()
                if task_id is not None:
                    break
            if task_id is None:
                self.log.error('Failed to create a build task')
                return response.send_error(
                    start_response, 500,
                    constants.ERROR_BUILD_TASK_CREATION_FAILED)

            task_description = {
                'capabilities': ['build_control_v1'],
                'job_id': job_id,
                'build_number': new_build_number,
                'status': 'pending',
                'id': task_id
            }

            for _ in range(10):
                task_details = self.distci_client.tasks.update(
                    task_id, task_description)
                if task_details is not None:
                    break
            if task_details is None:
                self.log.error("Build task creation failed")
                return response.send_error(
                    start_response, 500,
                    constants.ERROR_BUILD_TASK_CREATION_FAILED)
        else:
            self.log.warn(
                'No task frontends configured, unable to trigger build control task'
            )

        return response.send_response(
            start_response, 201,
            json.dumps({
                'job_id': job_id,
                'build_number': int(new_build_number),
                'state': build_state
            }))
Esempio n. 30
0
 def test_04_invalid_job_ids(self):
     for job_id in self.invalid_job_ids:
         assert validators.validate_job_id(job_id) is None, "Validator accepted invalid job_id %s" % job_id