def delete(self, jobtype_name, software): """ A ``DELETE`` to this endpoint will delete the requested software requirement from the specified jobtype, creating a new version of the jobtype in the process .. http:delete:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/<int:id> HTTP/1.1 **Request** .. sourcecode:: http DELETE /api/v1/jobtypes/TestJobType/software_requirements/1 HTTP/1.1 Accept: application/json **Response** .. sourcecode:: http HTTP/1.1 204 NO CONTENT :statuscode 204: the software requirement was deleted or didn't exist """ 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 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 for old_req in jobtype_version.software_requirements: if old_req.software.software != software: 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) db.session.add(new_version) db.session.commit() logger.info( "Deleted software requirement %s for jobtype %s, creating " "new version %s", software, jobtype.id, new_version.version, ) return jsonify(None), NO_CONTENT
def delete(self, jobtype_name, software): """ A ``DELETE`` to this endpoint will delete the requested software requirement from the specified jobtype, creating a new version of the jobtype in the process .. http:delete:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/<int:id> HTTP/1.1 **Request** .. sourcecode:: http DELETE /api/v1/jobtypes/TestJobType/software_requirements/1 HTTP/1.1 Accept: application/json **Response** .. sourcecode:: http HTTP/1.1 204 NO CONTENT :statuscode 204: the software requirement was deleted or didn't exist """ 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 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 for old_req in jobtype_version.software_requirements: if old_req.software.software != software: 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) db.session.add(new_version) db.session.commit() logger.info( "Deleted software requirement %s for jobtype %s, creating " "new version %s", software, jobtype.id, new_version.version) return jsonify(None), NO_CONTENT
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)
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)
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)
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)
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 put(self, jobtype_name): """ A ``PUT`` to this endpoint will create a new jobtype under the given URI. If a jobtype already exists under that URI, a new version will be created with the given data. You should only call this by id for updating an existing jobtype or if you have a reserved jobtype id. There is currently no way to reserve a jobtype id. .. http:put:: /api/v1/jobtypes/[<str:name>|<int:id>] HTTP/1.1 **Request** .. sourcecode:: http PUT /api/v1/jobtypes/TestJobType HTTP/1.1 Accept: application/json { "name": "TestJobType", "description": "Jobtype for testing inserts and queries", "code": "\\nfrom pyfarm.jobtypes.core.jobtype import " "JobType\\n\\nclass TestJobType(JobType):\\n" " def get_command(self):\\n" " return \"/usr/bin/touch\"\\n\\n" " def get_arguments(self):\\n" " return [os.path.join(" "self.assignment_data[\"job\"][\"data\"][\"path\"], " "\"%04d\" % self.assignment_data[\"tasks\"]" "[0][\"frame\"])]\\n" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "batch_contiguous": true, "classname": null, "code": "\\nfrom pyfarm.jobtypes.core.jobtype import " "JobType\\n\\nclass TestJobType(JobType):\\n" " def get_command(self):\\n" " return \"/usr/bin/touch\"\\n\\n" " def get_arguments(self):\\n" " return [os.path.join(" "self.assignment_data[\"job\"][\"data\"][\"path\"], " "\"%04d\" % self.assignment_data[\"tasks\"]" "[0][\"frame\"])]\\n", "id": 1, "max_batch": 1, "name": "TestJobType", "description": "Jobtype for testing inserts and queries", "software_requirements": [] } :statuscode 201: a new jobtype was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ if isinstance(jobtype_name, STRING_TYPES): jobtype = JobType.query.filter( JobType.name == jobtype_name).first() else: jobtype = JobType.query.filter_by(id=jobtype_name).first() max_version = None new = False if jobtype else True if jobtype: logger.debug( "jobtype %s will get a new version with data %r on commit", jobtype.name, g.json) max_version, = db.session.query(func.max( JobTypeVersion.version)).filter_by(jobtype=jobtype).first() else: jobtype = JobType() if max_version is not None: version = max_version + 1 else: version = 1 try: jobtype.name = g.json.pop("name") jobtype.description = g.json.pop("description", None) jobtype_version = JobTypeVersion() jobtype_version.jobtype = jobtype jobtype_version.version = version jobtype_version.code = g.json.pop("code") jobtype_version.classname = g.json.pop("classname", None) jobtype_version.batch_contiguous = g.json.pop( "batch_contiguous", None) jobtype_version.no_automatic_start_time =\ g.json.pop("no_automatic_start_time", None) if "max_batch" in g.json and g.json["max_batch"] is None: g.json.pop("max_batch") jobtype_version.max_batch = sql.null() else: jobtype_version.max_batch = g.json.pop("max_batch", None) except KeyError as e: return (jsonify(error="Missing key in input: %r" % e.args), BAD_REQUEST) if "software_requirements" in g.json: try: for r in parse_requirements(g.json["software_requirements"]): r.jobtype_version = jobtype_version db.session.add(r) except (TypeError, ValueError) as e: return jsonify(error=e.args), BAD_REQUEST except ObjectNotFound as e: return jsonify(error=e.args), NOT_FOUND del g.json["software_requirements"] elif not new: # If the user did not specify a list of software requirements and # this jobtype is not new, retain the requirements from the previous # version previous_version = JobTypeVersion.query.filter( JobTypeVersion.jobtype == jobtype, JobTypeVersion.version != version).order_by("version desc").first() if previous_version: for old_req in previous_version.software_requirements: new_req = JobTypeSoftwareRequirement() new_req.jobtype_version = jobtype_version new_req.software_id = old_req.software_id new_req.min_version_id = old_req.min_version_id new_req.max_version_id = old_req.max_version_id db.session.add(new_req) if g.json: return (jsonify(error="Unexpected keys in input: %s" % g.json.keys()), BAD_REQUEST) db.session.add_all([jobtype, jobtype_version]) db.session.commit() jobtype_data = jobtype_version.to_dict( unpack_relationships=["software_requirements"]) jobtype_data.update(jobtype.to_dict(unpack_relationships=False)) del jobtype_data["jobtype_id"] logger.info("%s jobtype %s in put: %r", "created" if new else "updated", jobtype.name, jobtype_data) return jsonify(jobtype_data), CREATED
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 put(self, jobtype_name): """ A ``PUT`` to this endpoint will create a new jobtype under the given URI. If a jobtype already exists under that URI, a new version will be created with the given data. You should only call this by id for updating an existing jobtype or if you have a reserved jobtype id. There is currently no way to reserve a jobtype id. .. http:put:: /api/v1/jobtypes/[<str:name>|<int:id>] HTTP/1.1 **Request** .. sourcecode:: http PUT /api/v1/jobtypes/TestJobType HTTP/1.1 Accept: application/json { "name": "TestJobType", "description": "Jobtype for testing inserts and queries", "code": "\\nfrom pyfarm.jobtypes.core.jobtype import " "JobType\\n\\nclass TestJobType(JobType):\\n" " def get_command(self):\\n" " return \"/usr/bin/touch\"\\n\\n" " def get_arguments(self):\\n" " return [os.path.join(" "self.assignment_data[\"job\"][\"data\"][\"path\"], " "\"%04d\" % self.assignment_data[\"tasks\"]" "[0][\"frame\"])]\\n" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "batch_contiguous": true, "classname": null, "code": "\\nfrom pyfarm.jobtypes.core.jobtype import " "JobType\\n\\nclass TestJobType(JobType):\\n" " def get_command(self):\\n" " return \"/usr/bin/touch\"\\n\\n" " def get_arguments(self):\\n" " return [os.path.join(" "self.assignment_data[\"job\"][\"data\"][\"path\"], " "\"%04d\" % self.assignment_data[\"tasks\"]" "[0][\"frame\"])]\\n", "id": 1, "max_batch": 1, "name": "TestJobType", "description": "Jobtype for testing inserts and queries", "software_requirements": [] } :statuscode 201: a new jobtype was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ if isinstance(jobtype_name, STRING_TYPES): jobtype = JobType.query.filter(JobType.name == jobtype_name).first() else: jobtype = JobType.query.filter_by(id=jobtype_name).first() max_version = None new = False if jobtype else True if jobtype: logger.debug("jobtype %s will get a new version with data %r on commit", jobtype.name, g.json) max_version, = db.session.query(func.max(JobTypeVersion.version)).filter_by(jobtype=jobtype).first() else: jobtype = JobType() if max_version is not None: version = max_version + 1 else: version = 1 try: jobtype.name = g.json.pop("name") jobtype.description = g.json.pop("description", None) jobtype_version = JobTypeVersion() jobtype_version.jobtype = jobtype jobtype_version.version = version jobtype_version.code = g.json.pop("code") jobtype_version.classname = g.json.pop("classname", None) jobtype_version.batch_contiguous = g.json.pop("batch_contiguous", None) jobtype_version.no_automatic_start_time = g.json.pop("no_automatic_start_time", None) if "max_batch" in g.json and g.json["max_batch"] is None: g.json.pop("max_batch") jobtype_version.max_batch = sql.null() else: jobtype_version.max_batch = g.json.pop("max_batch", None) except KeyError as e: return (jsonify(error="Missing key in input: %r" % e.args), BAD_REQUEST) if "software_requirements" in g.json: try: for r in parse_requirements(g.json["software_requirements"]): r.jobtype_version = jobtype_version db.session.add(r) except (TypeError, ValueError) as e: return jsonify(error=e.args), BAD_REQUEST except ObjectNotFound as e: return jsonify(error=e.args), NOT_FOUND del g.json["software_requirements"] elif not new: # If the user did not specify a list of software requirements and # this jobtype is not new, retain the requirements from the previous # version previous_version = ( JobTypeVersion.query.filter(JobTypeVersion.jobtype == jobtype, JobTypeVersion.version != version) .order_by("version desc") .first() ) if previous_version: for old_req in previous_version.software_requirements: new_req = JobTypeSoftwareRequirement() new_req.jobtype_version = jobtype_version new_req.software_id = old_req.software_id new_req.min_version_id = old_req.min_version_id new_req.max_version_id = old_req.max_version_id db.session.add(new_req) if g.json: return (jsonify(error="Unexpected keys in input: %s" % g.json.keys()), BAD_REQUEST) db.session.add_all([jobtype, jobtype_version]) db.session.commit() jobtype_data = jobtype_version.to_dict(unpack_relationships=["software_requirements"]) jobtype_data.update(jobtype.to_dict(unpack_relationships=False)) del jobtype_data["jobtype_id"] logger.info("%s jobtype %s in put: %r", "created" if new else "updated", jobtype.name, jobtype_data) return jsonify(jobtype_data), CREATED
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)
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)
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)