示例#1
0
def remove_jobtype_software_requirement(jobtype_id, software_id):
    with db.session.no_autoflush:
        jobtype = JobType.query.filter_by(id=jobtype_id).first()
        if not jobtype:
            return (render_template(
                        "pyfarm/error.html", error="Jobtype %s not found" %
                        jobtype_id), NOT_FOUND)

        previous_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not previous_version:
            return (render_template(
                "pyfarm/error.html", error="Jobtype %s has no versions" %
                jobtype_id), INTERNAL_SERVER_ERROR)

        new_version = JobTypeVersion(jobtype=jobtype)
        new_version.max_batch = previous_version.max_batch or sql.null()
        new_version.batch_contiguous = previous_version.batch_contiguous
        new_version.no_automatic_start_time =\
            previous_version.no_automatic_start_time
        new_version.classname = previous_version.classname
        new_version.code = previous_version.code
        new_version.version = previous_version.version + 1

        for requirement in previous_version.software_requirements:
            if requirement.software_id != software_id:
                new_requirement = JobTypeSoftwareRequirement()
                new_requirement.jobtype_version = new_version
                new_requirement.software = requirement.software
                new_requirement.min_version = requirement.min_version
                new_requirement.max_version = requirement.max_version
                db.session.add(new_requirement)

        db.session.commit()

    flash("Software requirement has been removed from jobtype %s" %
          jobtype.name)

    return redirect(url_for("single_jobtype_ui", jobtype_id=jobtype.id),
                            SEE_OTHER)
示例#2
0
def remove_jobtype_software_requirement(jobtype_id, software_id):
    with db.session.no_autoflush:
        jobtype = JobType.query.filter_by(id=jobtype_id).first()
        if not jobtype:
            return (render_template("pyfarm/error.html",
                                    error="Jobtype %s not found" % jobtype_id),
                    NOT_FOUND)

        previous_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not previous_version:
            return (render_template("pyfarm/error.html",
                                    error="Jobtype %s has no versions" %
                                    jobtype_id), INTERNAL_SERVER_ERROR)

        new_version = JobTypeVersion(jobtype=jobtype)
        new_version.max_batch = previous_version.max_batch or sql.null()
        new_version.batch_contiguous = previous_version.batch_contiguous
        new_version.no_automatic_start_time =\
            previous_version.no_automatic_start_time
        new_version.classname = previous_version.classname
        new_version.code = previous_version.code
        new_version.version = previous_version.version + 1

        for requirement in previous_version.software_requirements:
            if requirement.software_id != software_id:
                new_requirement = JobTypeSoftwareRequirement()
                new_requirement.jobtype_version = new_version
                new_requirement.software = requirement.software
                new_requirement.min_version = requirement.min_version
                new_requirement.max_version = requirement.max_version
                db.session.add(new_requirement)

        db.session.commit()

    flash("Software requirement has been removed from jobtype %s" %
          jobtype.name)

    return redirect(url_for("single_jobtype_ui", jobtype_id=jobtype.id),
                    SEE_OTHER)
示例#3
0
def jobtype(jobtype_id):
    """
    UI endpoint for a single jobtype. Allows showing and updating the jobtype
    """
    jobtype = JobType.query.filter_by(id=jobtype_id).first()
    if not jobtype:
        return (render_template("pyfarm/error.html",
                                error="Jobtype %s not found" % jobtype_id),
                NOT_FOUND)

    if request.method == "POST":
        with db.session.no_autoflush:
            jobtype.description = request.form["description"]

            new_version = JobTypeVersion(jobtype=jobtype)
            new_version.max_batch = request.form["max_batch"].strip() or\
                sql.null()
            new_version.batch_contiguous =\
                ("batch_contiguous" in request.form and
                 request.form["batch_contiguous"] == "true")
            new_version.no_automatic_start_time =\
                ("no_automatic_start_time" in request.form and
                 request.form["no_automatic_start_time"] == "true")
            new_version.classname = request.form["classname"]
            new_version.code = request.form["code"]

            max_version, = db.session.query(func.max(
                JobTypeVersion.version)).filter_by(jobtype=jobtype).first()
            new_version.version = (max_version or 0) + 1

            previous_version = JobTypeVersion.query.filter_by(
                jobtype=jobtype).order_by(desc(
                    JobTypeVersion.version)).first()
            if previous_version:
                for requirement in previous_version.software_requirements:
                    new_requirement = JobTypeSoftwareRequirement()
                    new_requirement.jobtype_version = new_version
                    new_requirement.software = requirement.software
                    new_requirement.min_version = requirement.min_version
                    new_requirement.max_version = requirement.max_version
                    db.session.add(new_requirement)

            db.session.add(jobtype)
            db.session.add(new_version)
            db.session.commit()

            flash("Jobtype %s updated to version %s" %
                  (jobtype.name, new_version.version))

            return redirect(
                url_for("single_jobtype_ui", jobtype_id=jobtype.id), SEE_OTHER)

    else:
        latest_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not latest_version:
            return (render_template("pyfarm/error.html",
                                    error="Jobtype %s has no versions" %
                                    jobtype_id), INTERNAL_SERVER_ERROR)

        return render_template("pyfarm/user_interface/jobtype.html",
                               jobtype=jobtype,
                               latest_version=latest_version,
                               software_items=Software.query)
示例#4
0
def create_jobtype():
    if request.method == "GET":
        return render_template("pyfarm/user_interface/jobtype_create.html",
                               jobtypes=JobType.query,
                               software_items=Software.query)
    else:
        with db.session.no_autoflush:
            jobtype = JobType()
            jobtype.name = request.form["name"]
            jobtype.description = request.form["description"]
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = 1
            jobtype_version.max_batch = request.form["max_batch"].strip() or\
                sql.null()
            jobtype_version.batch_contiguous =\
                ("batch_contiguous" in request.form and
                 request.form["batch_contiguous"] == "true")
            jobtype_version.no_automatic_start_time =\
                ("no_automatic_start_time" in request.form and
                 request.form["no_automatic_start_time"] == "true")
            jobtype_version.classname = request.form["classname"]
            jobtype_version.code = request.form["code"]

            requirements = zip(request.form.getlist("software"),
                               request.form.getlist("min_version"),
                               request.form.getlist("min_version"))

            for requirement_tuple in requirements:
                software = Software.query.filter_by(
                    id=int(requirement_tuple[0])).first()
                if not software:
                    return (render_template("pyfarm/error.html",
                                            error="Software %s not found" %
                                            requirement_tuple[0]), NOT_FOUND)
                requirement = JobTypeSoftwareRequirement()
                requirement.software = software
                requirement.jobtype_version = jobtype_version

                if requirement_tuple[1] != "":
                    minimum_version = SoftwareVersion.query.filter_by(
                        id=int(requirement_tuple[1])).first()
                    if not minimum_version:
                        return (render_template(
                            "pyfarm/error.html",
                            error="Software version %s not "
                            "found" % requirement_tuple[1]), NOT_FOUND)
                    if minimum_version.software != software:
                        return (render_template(
                            "pyfarm/error.html",
                            error="Software version %s "
                            "does not belong to software %s" %
                            (minimum_version.version, software.software)),
                                BAD_REQUEST)
                    requirement.min_version = minimum_version

                if requirement_tuple[2] != "":
                    maximum_version = SoftwareVersion.query.filter_by(
                        id=int(requirement_tuple[2])).first()
                    if not maximum_version:
                        return (render_template(
                            "pyfarm/error.html",
                            error="Software version %s not "
                            "found" % requirement_tuple[2]), NOT_FOUND)
                    if maximum_version.software != software:
                        return (render_template(
                            "pyfarm/error.html",
                            error="Software version %s "
                            "does not belong to software %s" %
                            (maximum_version.version, software.software)),
                                BAD_REQUEST)
                    requirement.max_version = maximum_version

                db.session.add(requirement)

            db.session.add(jobtype)
            db.session.add(jobtype_version)
            db.session.commit()

        flash("Jobtype %s created" % jobtype.name)

        return redirect(url_for('jobtypes_index_ui'), SEE_OTHER)
示例#5
0
def add_jobtype_software_requirement(jobtype_id):
    with db.session.no_autoflush:
        jobtype = JobType.query.filter_by(id=jobtype_id).first()
        if not jobtype:
            return (render_template("pyfarm/error.html",
                                    error="Jobtype %s not found" % jobtype_id),
                    NOT_FOUND)

        previous_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not previous_version:
            return (render_template("pyfarm/error.html",
                                    error="Jobtype %s has no versions" %
                                    jobtype_id), INTERNAL_SERVER_ERROR)

        new_version = JobTypeVersion(jobtype=jobtype)
        new_version.max_batch = previous_version.max_batch or sql.null()
        new_version.batch_contiguous = previous_version.batch_contiguous
        new_version.no_automatic_start_time =\
            previous_version.no_automatic_start_time
        new_version.classname = previous_version.classname
        new_version.code = previous_version.code
        new_version.version = previous_version.version + 1

        for requirement in previous_version.software_requirements:
            retained_requirement = JobTypeSoftwareRequirement()
            retained_requirement.jobtype_version = new_version
            retained_requirement.software = requirement.software
            retained_requirement.min_version = requirement.min_version
            retained_requirement.max_version = requirement.max_version
            db.session.add(retained_requirement)

        new_requirement = JobTypeSoftwareRequirement()
        new_requirement.jobtype_version = new_version

        new_requirement_software = Software.query.filter_by(
            id=request.form["software"]).first()
        if not new_requirement_software:
            return (render_template("pyfarm/error.html",
                                    error="Software %s not found" %
                                    request.form["software"]), NOT_FOUND)
        new_requirement.software = new_requirement_software

        if request.form["minimum_version"] != "":
            min_version = SoftwareVersion.query.filter_by(
                id=request.form["minimum_version"]).first()
            if not min_version:
                return (render_template(
                    "pyfarm/error.html",
                    error="Software version %s not "
                    "found" % request.form["minimum_version"]), NOT_FOUND)
            if min_version.software != new_requirement_software:
                return (render_template(
                    "pyfarm/error.html",
                    error="Software version %s does "
                    "not belong to software %s" %
                    (min_version.version, new_requirement_software.software)),
                        BAD_REQUEST)
            new_requirement.min_version = min_version

        if request.form["maximum_version"] != "":
            max_version = SoftwareVersion.query.filter_by(
                id=request.form["maximum_version"]).first()
            if not max_version:
                return (render_template(
                    "pyfarm/error.html",
                    error="Software version %s not "
                    "found" % request.form["maximum_version"]), NOT_FOUND)
            if max_version.software != new_requirement_software:
                return (render_template(
                    "pyfarm/error.html",
                    error="Software version %s does "
                    "not belong to software %s" %
                    (max_version.version, new_requirement_software.software)),
                        BAD_REQUEST)
            new_requirement.max_version = max_version

        db.session.add(new_version)
        db.session.add(new_requirement)
        db.session.commit()

    flash("Software requirement has been added to jobtype %s" % jobtype.name)

    return redirect(url_for("single_jobtype_ui", jobtype_id=jobtype.id),
                    SEE_OTHER)
    def post(self, jobtype_name, version=None):
        """
        A ``POST`` to this endpoint will create a new software_requirement for
        the specified jobtype.
        This will transparently create a new jobtype version

        .. http:post:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/ HTTP/1.1

            **Request**

            .. sourcecode:: http

                POST /api/v1/jobtypes/TestJobType/software_requirements/ HTTP/1.1
                Accept: application/json

                {
                    "software": "blender",
                    "min_version": "2.69"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: application/json

                {
                    "jobtype_version": {
                        "id": 8,
                        "jobtype": "TestJobType",
                        "version": 7
                    },
                    "max_version": null,
                    "min_version": {
                        "id": 2,
                        "version": "1.69"
                    },
                    "software": {
                        "id": 2,
                        "software": "blender"
                    }
                }

        :statuscode 201:
            a new software requirement was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)

        :statuscode 405:
            you tried calling this method on a specific version

        :statuscode 409:
            a conflicting software requirement already exists
        """
        if version is not None:
            return (jsonify(
                error="POST not allowed for specific jobtype versions"),
                    METHOD_NOT_ALLOWED)

        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name),
                    NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        if ("software" not in g.json
                or not isinstance(g.json["software"], STRING_TYPES)):
            return (jsonify(error="Software not specified or not a string"),
                    BAD_REQUEST)

        software = Software.query.filter_by(
            software=g.json["software"]).first()
        if not software:
            return jsonify(error="Software not found"), NOT_FOUND

        existing_requirement = JobTypeSoftwareRequirement.query.filter(
            JobTypeSoftwareRequirement.jobtype_version == jobtype_version,
            JobTypeSoftwareRequirement.software == software).first()
        if existing_requirement:
            return jsonify(error="A software requirement for this jobtype "
                           "version and this software exists"), CONFLICT

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        db.session.add(new_version)
        for old_req in jobtype_version.software_requirements:
            new_req = JobTypeSoftwareRequirement()
            for name in JobTypeSoftwareRequirement.types().columns:
                setattr(new_req, name, getattr(old_req, name))
            new_req.jobtype_version = new_version
            db.session.add(new_req)

        min_version = None
        if "min_version" in g.json:
            if not isinstance(g.json["min_version"], STRING_TYPES):
                return jsonify(error="min_version not a string"), BAD_REQUEST
            min_version = SoftwareVersion.query.filter_by(
                version=g.json["min_version"]).first()
            if not min_version:
                return jsonify(error="min_version not found"), NOT_FOUND

        max_version = None
        if "max_version" in g.json:
            if not isinstance(g.json["max_version"], STRING_TYPES):
                return jsonify(error="max_version not a string"), BAD_REQUEST
            max_version = SoftwareVersion.query.filter_by(
                version=g.json["max_version"]).first()
            if not max_version:
                return jsonify(error="max_version not found"), NOT_FOUND

        requirement = JobTypeSoftwareRequirement()
        requirement.jobtype_version = new_version
        requirement.software = software
        requirement.min_version = min_version
        requirement.max_version = max_version

        db.session.add(requirement)
        db.session.commit()
        requirement_data = requirement.to_dict()
        del requirement_data["jobtype_version_id"]
        del requirement_data["software_id"]
        del requirement_data["min_version_id"]
        del requirement_data["max_version_id"]
        logger.info("Created new software requirement for jobtype %s: %r",
                    jobtype.id, requirement_data)

        return jsonify(requirement_data), CREATED
def parse_requirements(requirements):
    """
    Takes a list dicts specifying a software and optional min- and max-versions
    and returns a list of :class:`JobRequirement` objects.

    Raises TypeError if the input was not as expected or ObjectNotFound if a
    referenced software of or version was not found.

    :param list requirements:
        A list of of dicts specifying a software and optionally min_version
        and/or max_version.

    :raises TypeError:
        Raised if ``requirements`` is not a list or if an entry in
        ``requirements`` is not a dictionary.

    :raises ValueError:
        Raised if there's a problem with the content of at least one of the
        requirement dictionaries.

    :raises ObjectNotFound:
        Raised if the referenced software or version was not found
    """
    if not isinstance(requirements, list):
        raise TypeError("software_requirements must be a list")

    out = []
    for entry in requirements:
        if not isinstance(entry, dict):
            raise TypeError("Every software_requirement must be a dict")

        requirement = JobTypeSoftwareRequirement()
        software_name = entry.pop("software", None)
        if software_name is None:
            raise ValueError(
                "Software requirement does not specify a software.")
        software = Software.query.filter_by(software=software_name).first()
        if not software:
            raise ObjectNotFound("Software %s not found" % software_name)
        requirement.software = software

        min_version_str = entry.pop("min_version", None)
        if min_version_str is not None:
            min_version = SoftwareVersion.query.filter(
                SoftwareVersion.software == software,
                SoftwareVersion.version == min_version_str).first()
            if not min_version:
                raise ObjectNotFound("Version %s of software %s not found" %
                                     (software_name, min_version_str))
            requirement.min_version = min_version

        max_version_str = entry.pop("max_version", None)
        if max_version_str is not None:
            max_version = SoftwareVersion.query.filter(
                SoftwareVersion.software == software,
                SoftwareVersion.version == max_version_str).first()
            if not max_version:
                raise ObjectNotFound("Version %s of software %s not found" %
                                     (software_name, max_version_str))
            requirement.max_version = max_version

        if entry:
            raise ValueError("Unexpected keys in software requirement: %r" %
                             entry.keys())

        out.append(requirement)
    return out
示例#8
0
    def post(self, jobtype_name, version=None):
        """
        A ``POST`` to this endpoint will create a new software_requirement for
        the specified jobtype.
        This will transparently create a new jobtype version

        .. http:post:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/ HTTP/1.1

            **Request**

            .. sourcecode:: http

                POST /api/v1/jobtypes/TestJobType/software_requirements/ HTTP/1.1
                Accept: application/json

                {
                    "software": "blender",
                    "min_version": "2.69"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: application/json

                {
                    "jobtype_version": {
                        "id": 8,
                        "jobtype": "TestJobType",
                        "version": 7
                    },
                    "max_version": null,
                    "min_version": {
                        "id": 2,
                        "version": "1.69"
                    },
                    "software": {
                        "id": 2,
                        "software": "blender"
                    }
                }

        :statuscode 201:
            a new software requirement was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)

        :statuscode 405:
            you tried calling this method on a specific version

        :statuscode 409:
            a conflicting software requirement already exists
        """
        if version is not None:
            return (jsonify(error="POST not allowed for specific jobtype versions"), METHOD_NOT_ALLOWED)

        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name), NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        if "software" not in g.json or not isinstance(g.json["software"], STRING_TYPES):
            return (jsonify(error="Software not specified or not a string"), BAD_REQUEST)

        software = Software.query.filter_by(software=g.json["software"]).first()
        if not software:
            return jsonify(error="Software not found"), NOT_FOUND

        existing_requirement = JobTypeSoftwareRequirement.query.filter(
            JobTypeSoftwareRequirement.jobtype_version == jobtype_version,
            JobTypeSoftwareRequirement.software == software,
        ).first()
        if existing_requirement:
            return (
                jsonify(error="A software requirement for this jobtype " "version and this software exists"),
                CONFLICT,
            )

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        db.session.add(new_version)
        for old_req in jobtype_version.software_requirements:
            new_req = JobTypeSoftwareRequirement()
            for name in JobTypeSoftwareRequirement.types().columns:
                setattr(new_req, name, getattr(old_req, name))
            new_req.jobtype_version = new_version
            db.session.add(new_req)

        min_version = None
        if "min_version" in g.json:
            if not isinstance(g.json["min_version"], STRING_TYPES):
                return jsonify(error="min_version not a string"), BAD_REQUEST
            min_version = SoftwareVersion.query.filter_by(version=g.json["min_version"]).first()
            if not min_version:
                return jsonify(error="min_version not found"), NOT_FOUND

        max_version = None
        if "max_version" in g.json:
            if not isinstance(g.json["max_version"], STRING_TYPES):
                return jsonify(error="max_version not a string"), BAD_REQUEST
            max_version = SoftwareVersion.query.filter_by(version=g.json["max_version"]).first()
            if not max_version:
                return jsonify(error="max_version not found"), NOT_FOUND

        requirement = JobTypeSoftwareRequirement()
        requirement.jobtype_version = new_version
        requirement.software = software
        requirement.min_version = min_version
        requirement.max_version = max_version

        db.session.add(requirement)
        db.session.commit()
        requirement_data = requirement.to_dict()
        del requirement_data["jobtype_version_id"]
        del requirement_data["software_id"]
        del requirement_data["min_version_id"]
        del requirement_data["max_version_id"]
        logger.info("Created new software requirement for jobtype %s: %r", jobtype.id, requirement_data)

        return jsonify(requirement_data), CREATED
示例#9
0
def parse_requirements(requirements):
    """
    Takes a list dicts specifying a software and optional min- and max-versions
    and returns a list of :class:`JobRequirement` objects.

    Raises TypeError if the input was not as expected or ObjectNotFound if a
    referenced software of or version was not found.

    :param list requirements:
        A list of of dicts specifying a software and optionally min_version
        and/or max_version.

    :raises TypeError:
        Raised if ``requirements`` is not a list or if an entry in
        ``requirements`` is not a dictionary.

    :raises ValueError:
        Raised if there's a problem with the content of at least one of the
        requirement dictionaries.

    :raises ObjectNotFound:
        Raised if the referenced software or version was not found
    """
    if not isinstance(requirements, list):
        raise TypeError("software_requirements must be a list")

    out = []
    for entry in requirements:
        if not isinstance(entry, dict):
            raise TypeError("Every software_requirement must be a dict")

        requirement = JobTypeSoftwareRequirement()
        software_name = entry.pop("software", None)
        if software_name is None:
            raise ValueError("Software requirement does not specify a software.")
        software = Software.query.filter_by(software=software_name).first()
        if not software:
            raise ObjectNotFound("Software %s not found" % software_name)
        requirement.software = software

        min_version_str = entry.pop("min_version", None)
        if min_version_str is not None:
            min_version = SoftwareVersion.query.filter(
                SoftwareVersion.software == software, SoftwareVersion.version == min_version_str
            ).first()
            if not min_version:
                raise ObjectNotFound("Version %s of software %s not found" % (software_name, min_version_str))
            requirement.min_version = min_version

        max_version_str = entry.pop("max_version", None)
        if max_version_str is not None:
            max_version = SoftwareVersion.query.filter(
                SoftwareVersion.software == software, SoftwareVersion.version == max_version_str
            ).first()
            if not max_version:
                raise ObjectNotFound("Version %s of software %s not found" % (software_name, max_version_str))
            requirement.max_version = max_version

        if entry:
            raise ValueError("Unexpected keys in software requirement: %r" % entry.keys())

        out.append(requirement)
    return out
示例#10
0
def jobtype(jobtype_id):
    """
    UI endpoint for a single jobtype. Allows showing and updating the jobtype
    """
    jobtype = JobType.query.filter_by(id=jobtype_id).first()
    if not jobtype:
        return (render_template(
                    "pyfarm/error.html", error="Jobtype %s not found" %
                    jobtype_id), NOT_FOUND)

    if request.method == "POST":
        with db.session.no_autoflush:
            jobtype.description = request.form["description"]

            new_version = JobTypeVersion(jobtype=jobtype)
            new_version.max_batch = request.form["max_batch"].strip() or\
                sql.null()
            new_version.batch_contiguous =\
                ("batch_contiguous" in request.form and
                 request.form["batch_contiguous"] == "true")
            new_version.no_automatic_start_time =\
                ("no_automatic_start_time" in request.form and
                 request.form["no_automatic_start_time"] == "true")
            new_version.classname = request.form["classname"]
            new_version.code = request.form["code"]

            max_version, = db.session.query(func.max(
                    JobTypeVersion.version)).filter_by(jobtype=jobtype).first()
            new_version.version = (max_version or 0) + 1

            previous_version = JobTypeVersion.query.filter_by(
                jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
            if previous_version:
                for requirement in previous_version.software_requirements:
                    new_requirement = JobTypeSoftwareRequirement()
                    new_requirement.jobtype_version = new_version
                    new_requirement.software = requirement.software
                    new_requirement.min_version = requirement.min_version
                    new_requirement.max_version = requirement.max_version
                    db.session.add(new_requirement)

            db.session.add(jobtype)
            db.session.add(new_version)
            db.session.commit()

            flash("Jobtype %s updated to version %s" %
                (jobtype.name, new_version.version))

            return redirect(url_for("single_jobtype_ui", jobtype_id=jobtype.id),
                            SEE_OTHER)

    else:
        latest_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not latest_version:
            return (render_template(
                        "pyfarm/error.html", error="Jobtype %s has no versions" %
                        jobtype_id), INTERNAL_SERVER_ERROR)


        return render_template("pyfarm/user_interface/jobtype.html",
                            jobtype=jobtype, latest_version=latest_version,
                            software_items=Software.query)
示例#11
0
def create_jobtype():
    if request.method == "GET":
        return render_template("pyfarm/user_interface/jobtype_create.html",
                               jobtypes=JobType.query,
                               software_items=Software.query)
    else:
        with db.session.no_autoflush:
            jobtype = JobType()
            jobtype.name = request.form["name"]
            jobtype.description = request.form["description"]
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = 1
            jobtype_version.max_batch = request.form["max_batch"].strip() or\
                sql.null()
            jobtype_version.batch_contiguous =\
                ("batch_contiguous" in request.form and
                 request.form["batch_contiguous"] == "true")
            jobtype_version.no_automatic_start_time =\
                ("no_automatic_start_time" in request.form and
                 request.form["no_automatic_start_time"] == "true")
            jobtype_version.classname = request.form["classname"]
            jobtype_version.code = request.form["code"]

            requirements = zip(request.form.getlist("software"),
                            request.form.getlist("min_version"),
                            request.form.getlist("min_version"))

            for requirement_tuple in requirements:
                software = Software.query.filter_by(
                    id=int(requirement_tuple[0])).first()
                if not software:
                    return (render_template(
                        "pyfarm/error.html", error="Software %s not found" %
                        requirement_tuple[0]), NOT_FOUND)
                requirement = JobTypeSoftwareRequirement()
                requirement.software = software
                requirement.jobtype_version = jobtype_version

                if requirement_tuple[1] != "":
                    minimum_version = SoftwareVersion.query.filter_by(
                        id=int(requirement_tuple[1])).first()
                    if not minimum_version:
                        return (render_template(
                            "pyfarm/error.html", error="Software version %s not "
                            "found" % requirement_tuple[1]), NOT_FOUND)
                    if minimum_version.software != software:
                        return (render_template(
                            "pyfarm/error.html", error="Software version %s "
                            "does not belong to software %s" %
                            (minimum_version.version, software.software)),
                            BAD_REQUEST)
                    requirement.min_version = minimum_version

                if requirement_tuple[2] != "":
                    maximum_version = SoftwareVersion.query.filter_by(
                        id=int(requirement_tuple[2])).first()
                    if not maximum_version:
                        return (render_template(
                            "pyfarm/error.html", error="Software version %s not "
                            "found" % requirement_tuple[2]), NOT_FOUND)
                    if maximum_version.software != software:
                        return (render_template(
                            "pyfarm/error.html", error="Software version %s "
                            "does not belong to software %s" %
                            (maximum_version.version, software.software)),
                            BAD_REQUEST)
                    requirement.max_version = maximum_version

                db.session.add(requirement)

            db.session.add(jobtype)
            db.session.add(jobtype_version)
            db.session.commit()

        flash("Jobtype %s created" % jobtype.name)

        return redirect(url_for('jobtypes_index_ui'), SEE_OTHER)
示例#12
0
def add_jobtype_software_requirement(jobtype_id):
    with db.session.no_autoflush:
        jobtype = JobType.query.filter_by(id=jobtype_id).first()
        if not jobtype:
            return (render_template(
                        "pyfarm/error.html", error="Jobtype %s not found" %
                        jobtype_id), NOT_FOUND)

        previous_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by(desc(JobTypeVersion.version)).first()
        if not previous_version:
            return (render_template(
                "pyfarm/error.html", error="Jobtype %s has no versions" %
                jobtype_id), INTERNAL_SERVER_ERROR)

        new_version = JobTypeVersion(jobtype=jobtype)
        new_version.max_batch = previous_version.max_batch or sql.null()
        new_version.batch_contiguous = previous_version.batch_contiguous
        new_version.no_automatic_start_time =\
            previous_version.no_automatic_start_time
        new_version.classname = previous_version.classname
        new_version.code = previous_version.code
        new_version.version = previous_version.version + 1

        for requirement in previous_version.software_requirements:
            retained_requirement = JobTypeSoftwareRequirement()
            retained_requirement.jobtype_version = new_version
            retained_requirement.software = requirement.software
            retained_requirement.min_version = requirement.min_version
            retained_requirement.max_version = requirement.max_version
            db.session.add(retained_requirement)

        new_requirement = JobTypeSoftwareRequirement()
        new_requirement.jobtype_version = new_version

        new_requirement_software = Software.query.filter_by(
            id=request.form["software"]).first()
        if not new_requirement_software:
            return (render_template(
                        "pyfarm/error.html", error="Software %s not found" %
                        request.form["software"]), NOT_FOUND)
        new_requirement.software = new_requirement_software

        if request.form["minimum_version"] != "":
            min_version = SoftwareVersion.query.filter_by(
                id=request.form["minimum_version"]).first()
            if not min_version:
                return (render_template(
                        "pyfarm/error.html", error="Software version %s not "
                        "found" %  request.form["minimum_version"]), NOT_FOUND)
            if min_version.software != new_requirement_software:
                return (render_template(
                        "pyfarm/error.html", error="Software version %s does "
                        "not belong to software %s" %
                        (min_version.version,
                         new_requirement_software.software)), BAD_REQUEST)
            new_requirement.min_version = min_version

        if request.form["maximum_version"] != "":
            max_version = SoftwareVersion.query.filter_by(
                id=request.form["maximum_version"]).first()
            if not max_version:
                return (render_template(
                        "pyfarm/error.html", error="Software version %s not "
                        "found" %  request.form["maximum_version"]), NOT_FOUND)
            if max_version.software != new_requirement_software:
                return (render_template(
                        "pyfarm/error.html", error="Software version %s does "
                        "not belong to software %s" %
                        (max_version.version,
                         new_requirement_software.software)), BAD_REQUEST)
            new_requirement.max_version = max_version

        db.session.add(new_version)
        db.session.add(new_requirement)
        db.session.commit()

    flash("Software requirement has been added to jobtype %s" %
          jobtype.name)

    return redirect(url_for("single_jobtype_ui", jobtype_id=jobtype.id),
                            SEE_OTHER)