def cluster_release_dep(): """ Return status. """ request_debug(r, logger) user_id = request_get(r, "user_id") cluster_id = request_get(r, "cluster_id") if not user_id and not cluster_id: error_msg = "cluster_release without id" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.args) else: result = None if cluster_id: result = cluster_handler.release_cluster(cluster_id=cluster_id) elif user_id: result = cluster_handler.release_cluster_for_user(user_id=user_id) if not result: error_msg = "cluster_release failed user_id={} cluster_id={}". \ format(user_id, cluster_id) logger.warning(error_msg) data = { "user_id": user_id, "cluster_id": cluster_id, } return make_fail_resp(error=error_msg, data=data) else: return make_ok_resp()
def cluster_release_dep(): """ Return status. """ request_debug(r, logger) user_id = request_get(r, "user_id") cluster_id = request_get(r, "cluster_id") if not user_id and not cluster_id: error_msg = "cluster_release without id" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.args) else: result = None if cluster_id: result = cluster_handler.release_cluster(cluster_id=cluster_id) elif user_id: result = cluster_handler.release_cluster_for_user(user_id=user_id) if not result: error_msg = "cluster_release failed user_id={} cluster_id={}". \ format(user_id, cluster_id) logger.warning(error_msg) data = { "user_id": user_id, "cluster_id": cluster_id, } return make_fail_response(error=error_msg, data=data) else: return make_ok_response()
def cluster_release(): """ Return status. """ request_debug(r, logger) user_id = request_get(r, "user_id") cluster_id = request_get(r, "cluster_id") if not user_id and not cluster_id: logger.warning("cluster_release without id") response_fail["error"] = "No id in release" response_fail["data"] = r.args return make_response(jsonify(response_fail), CODE_BAD_REQUEST) else: result = None if cluster_id: result = cluster_handler.release_cluster(cluster_id=cluster_id) elif user_id: result = cluster_handler.release_cluster_for_user(user_id=user_id) if not result: logger.warning( "cluster_release failed user_id={} cluster_id={}".format( user_id, cluster_id)) response_fail["error"] = "release fail" response_fail["data"] = { "user_id": user_id, "cluster_id": cluster_id, } return jsonify(response_fail), CODE_BAD_REQUEST else: return jsonify(response_ok), CODE_OK
def cluster_apply(): """ Return a Cluster json body. """ request_debug(r, logger) def make_response_invalid(msg=""): response_fail["error"] = msg or "Invalid request data" response_fail["data"] = request_json_body(r) return jsonify(response_fail), CODE_BAD_REQUEST user_id = request_get(r, "user_id") if not user_id: logger.warning("cluster_apply without user_id") return make_response_invalid("cluster_apply without user_id") allow_multiple, condition = request_get(r, "allow_multiple"), {} consensus_plugin = request_get(r, "consensus_plugin") consensus_mode = request_get(r, "consensus_mode") cluster_size = int(request_get(r, "size") or -1) if consensus_plugin: if consensus_plugin not in CONSENSUS_PLUGINS: logger.warning("Invalid consensus_plugin") return make_response_invalid("Invalid consensus_plugin") else: condition["consensus_plugin"] = consensus_plugin if consensus_mode: if consensus_mode not in CONSENSUS_MODES: logger.warning("Invalid consensus_mode") return make_response_invalid("Invalid consensus_mode") else: condition["consensus_mode"] = consensus_mode if cluster_size >= 0: if cluster_size not in CLUSTER_SIZES: logger.warning("Invalid cluster_size") return make_response_invalid("Invalid cluster_size") else: condition["size"] = cluster_size logger.debug("condition={}".format(condition)) c = cluster_handler.apply_cluster(user_id=user_id, condition=condition, allow_multiple=allow_multiple) if not c: logger.warning("cluster_apply failed") return make_response_invalid("No available res for {}".format(user_id)) else: response_ok["data"] = c return jsonify(response_ok), CODE_OK
def cluster_apply_dep(): """ Return a Cluster json body. """ request_debug(r, logger) user_id = request_get(r, "user_id") if not user_id: error_msg = "cluster_apply without user_id" logger.warning(error_msg) return make_fail_resp(error=error_msg) allow_multiple, condition = request_get(r, "allow_multiple"), {} consensus_plugin = request_get(r, "consensus_plugin") consensus_mode = request_get(r, "consensus_mode") cluster_size = int(request_get(r, "size") or -1) if consensus_plugin: if consensus_plugin not in CONSENSUS_PLUGINS: error_msg = "Invalid consensus_plugin" logger.warning(error_msg) return make_fail_resp(error=error_msg) else: condition["consensus_plugin"] = consensus_plugin if consensus_mode: if consensus_mode not in CONSENSUS_MODES: error_msg = "Invalid consensus_mode" logger.warning(error_msg) return make_fail_resp(error=error_msg) else: condition["consensus_mode"] = consensus_mode if cluster_size >= 0: if cluster_size not in NETWORK_SIZE_FABRIC_PRE_V1: error_msg = "Invalid cluster_size" logger.warning(error_msg) return make_fail_resp(error=error_msg) else: condition["size"] = cluster_size logger.debug("condition={}".format(condition)) c = cluster_handler.apply_cluster(user_id=user_id, condition=condition, allow_multiple=allow_multiple) if not c: error_msg = "No available res for {}".format(user_id) logger.warning(error_msg) return make_fail_resp(error=error_msg) else: return make_ok_resp(data=c)
def cluster_apply_dep(): """ Return a Cluster json body. """ request_debug(r, logger) user_id = request_get(r, "user_id") if not user_id: error_msg = "cluster_apply without user_id" logger.warning(error_msg) return make_fail_response(error=error_msg) allow_multiple, condition = request_get(r, "allow_multiple"), {} consensus_plugin = request_get(r, "consensus_plugin") consensus_mode = request_get(r, "consensus_mode") cluster_size = int(request_get(r, "size") or -1) if consensus_plugin: if consensus_plugin not in CONSENSUS_PLUGINS: error_msg = "Invalid consensus_plugin" logger.warning(error_msg) return make_fail_response(error=error_msg) else: condition["consensus_plugin"] = consensus_plugin if consensus_mode: if consensus_mode not in CONSENSUS_MODES: error_msg = "Invalid consensus_mode" logger.warning(error_msg) return make_fail_response(error=error_msg) else: condition["consensus_mode"] = consensus_mode if cluster_size >= 0: if cluster_size not in CLUSTER_SIZES: error_msg = "Invalid cluster_size" logger.warning(error_msg) return make_fail_response(error=error_msg) else: condition["size"] = cluster_size logger.debug("condition={}".format(condition)) c = cluster_handler.apply_cluster(user_id=user_id, condition=condition, allow_multiple=allow_multiple) if not c: error_msg = "No available res for {}".format(user_id) logger.warning(error_msg) return make_fail_response(error=error_msg) else: return make_ok_response(data=c)
def cluster_actions(): """Issue some operations on the cluster. Valid operations include: apply, release, start, stop, restart e.g., apply a cluster for user: GET /cluster_op?action=apply&user_id=xxx release a cluster: GET /cluster_op?action=release&cluster_id=xxx start a cluster: GET /cluster_op?action=start&cluster_id=xxx stop a cluster: GET /cluster_op?action=stop&cluster_id=xxx restart a cluster: GET /cluster_op?action=restart&cluster_id=xxx Return a json obj. """ request_debug(r, logger) action = request_get(r, "action") logger.info("cluster_op with action={}".format(action)) if action == "apply": return cluster_apply(r) elif action == "release": return cluster_release(r) elif action == "start": return cluster_start(r) elif action == "stop": return cluster_stop(r) elif action == "restart": return cluster_restart(r) else: return make_fail_response(error="Unknown action type")
def cluster_actions(): """Issue some operations on the cluster. Valid operations include: apply, release, start, stop, restart e.g., apply a cluster for user: GET /cluster_op?action=apply&user_id=xxx release a cluster: GET /cluster_op?action=release&cluster_id=xxx start a cluster: GET /cluster_op?action=start&cluster_id=xxx stop a cluster: GET /cluster_op?action=stop&cluster_id=xxx restart a cluster: GET /cluster_op?action=restart&cluster_id=xxx Return a json obj. """ request_debug(r, logger) action = request_get(r, "action") logger.info("cluster_op with action={}".format(action)) if action == "apply": return cluster_apply(r) elif action == "release": return cluster_release(r) elif action == "start": return cluster_start(r) elif action == "stop": return cluster_stop(r) elif action == "restart": return cluster_restart(r) else: return make_fail_resp(error="Unknown action type")
def cluster_apply(r): """Apply a cluster. Return a Cluster json body. """ request_debug(r, logger) user_id = request_get(r, "user_id") if not user_id: logger.warning("cluster_apply without user_id") return make_fail_response("cluster_apply without user_id") allow_multiple, condition = request_get(r, "allow_multiple"), {} consensus_plugin = request_get(r, "consensus_plugin") consensus_mode = request_get(r, "consensus_mode") cluster_size = int(request_get(r, "size") or -1) if consensus_plugin: if consensus_plugin not in CONSENSUS_PLUGINS: logger.warning("Invalid consensus_plugin") return make_fail_response("Invalid consensus_plugin") else: condition["consensus_plugin"] = consensus_plugin if consensus_mode: if consensus_mode not in CONSENSUS_MODES: logger.warning("Invalid consensus_mode") return make_fail_response("Invalid consensus_mode") else: condition["consensus_mode"] = consensus_mode if cluster_size >= 0: if cluster_size not in CLUSTER_SIZES: logger.warning("Invalid cluster_size") return make_fail_response("Invalid cluster_size") else: condition["size"] = cluster_size logger.debug("condition={}".format(condition)) c = cluster_handler.apply_cluster(user_id=user_id, condition=condition, allow_multiple=allow_multiple) if not c: logger.warning("cluster_apply failed") return make_fail_response("No available res for {}".format(user_id)) else: return make_ok_response(data=c)
def cluster_apply(r): """Apply a cluster. Return a Cluster json body. """ request_debug(r, logger) user_id = request_get(r, "user_id") if not user_id: logger.warning("cluster_apply without user_id") return make_fail_resp("cluster_apply without user_id") allow_multiple, condition = request_get(r, "allow_multiple"), {} consensus_plugin = request_get(r, "consensus_plugin") consensus_mode = request_get(r, "consensus_mode") cluster_size = int(request_get(r, "size") or -1) if consensus_plugin: if consensus_plugin not in CONSENSUS_PLUGINS_FABRIC_V1: logger.warning("Invalid consensus_plugin") return make_fail_resp("Invalid consensus_plugin") else: condition["consensus_plugin"] = consensus_plugin if consensus_mode: if consensus_mode not in CONSENSUS_MODES: logger.warning("Invalid consensus_mode") return make_fail_resp("Invalid consensus_mode") else: condition["consensus_mode"] = consensus_mode if cluster_size >= 0: if cluster_size not in NETWORK_SIZE_FABRIC_PRE_V1: logger.warning("Invalid cluster_size") return make_fail_resp("Invalid cluster_size") else: condition["size"] = cluster_size logger.debug("condition={}".format(condition)) c = cluster_handler.apply_cluster(user_id=user_id, condition=condition, allow_multiple=allow_multiple) if not c: logger.warning("cluster_apply failed") return make_fail_resp("No available res for {}".format(user_id)) else: return make_ok_resp(data=c)
def cluster_release(r): """Release a cluster which should be in used status currently. :param r: :return: """ cluster_id = request_get(r, "cluster_id") if not cluster_id: logger.warning("No cluster_id is given") return make_fail_resp("No cluster_id is given") release_cluster.delay(cluster_id) return make_ok_resp()
def cluster_stop(r): """Stop a cluster which should be in running status currently. :param r: :return: """ cluster_id = request_get(r, "cluster_id") if not cluster_id: logger.warning("No cluster_id is given") return make_fail_response("No cluster_id is given") if cluster_handler.stop(cluster_id): return make_ok_response() return make_fail_response("cluster stop failed")
def cluster_restart(r): """Start a cluster which should be in stopped status currently. :param r: :return: """ cluster_id = request_get(r, "cluster_id") if not cluster_id: logger.warning("No cluster_id is given") return make_fail_resp("No cluster_id is given") if cluster_handler.restart(cluster_id): return make_ok_resp() return make_fail_resp("cluster restart failed")
def cluster_restart(r): """Start a cluster which should be in stopped status currently. :param r: :return: """ cluster_id = request_get(r, "cluster_id") if not cluster_id: logger.warning("No cluster_id is given") return make_fail_response("No cluster_id is given") if cluster_handler.restart(cluster_id): return make_ok_response() return make_fail_response("cluster restart failed")
def cluster_stop(r): """Stop a cluster which should be in running status currently. :param r: :return: """ cluster_id = request_get(r, "cluster_id") if not cluster_id: logger.warning("No cluster_id is given") return make_fail_resp("No cluster_id is given") if cluster_handler.stop(cluster_id): return make_ok_resp() return make_fail_resp("cluster stop failed")
def cluster_list(): """ Return list of the clusters. """ request_debug(r, logger) user_id = request_get(r, "user_id") logger.warning("user_id={}".format(user_id)) if not user_id: logger.warning("cluster_list without user_id") response_fail["error"] = "No user_id is given" response_fail["data"] = r.args return jsonify(response_fail), CODE_BAD_REQUEST result = cluster_handler.list(filter_data={'user_id': user_id}) response_ok["data"] = result return jsonify(response_ok), CODE_OK
def cluster_info(): """ Return information of the cluster. """ request_debug(r, logger) cluster_id = request_get(r, "cluster_id") logger.warning("cluster_id={}".format(cluster_id)) if not cluster_id: response_fail["error"] = "cluster_get without cluster_id" logger.warning(response_fail["error"]) response_fail["data"] = r.args return jsonify(response_fail), CODE_BAD_REQUEST result = cluster_handler.get_by_id(cluster_id) response_ok["data"] = result return jsonify(response_ok), CODE_OK
def host_api(): request_debug(r, logger) if r.method == 'GET': if "id" not in r.args and "id" not in r.form: logger.warn("host get without enough data") response_fail["error"] = "host GET without enough data" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST else: host_id = request_get(r, "id") result = host_handler.get_by_id(host_id) if result: return jsonify(result), CODE_OK else: logger.warn("host not found with id=" + host_id) response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST elif r.method == 'POST': name, daemon_url, capacity, log_type, log_server, log_level = \ r.form['name'], r.form['daemon_url'], r.form['capacity'], \ r.form['log_type'], r.form['log_server'], r.form['log_level'] if "autofill" in r.form and r.form["autofill"] == "on": autofill = "true" else: autofill = "false" if "schedulable" in r.form and r.form["schedulable"] == "on": schedulable = "true" else: schedulable = "false" logger.debug("name={}, daemon_url={}, capacity={}" "fillup={}, schedulable={}, log={}/{}".format( name, daemon_url, capacity, autofill, schedulable, log_type, log_server)) if not name or not daemon_url or not capacity or not log_type: logger.warn("host post without enough data") response_fail["error"] = "host POST without enough data" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST else: result = host_handler.create(name=name, daemon_url=daemon_url, capacity=int(capacity), autofill=autofill, schedulable=schedulable, log_level=log_level, log_type=log_type, log_server=log_server) if result: logger.debug("host creation successfully") return jsonify(response_ok), CODE_CREATED else: logger.debug("host creation failed") response_fail["error"] = "Failed to create host {}".format( r.form["name"]) return jsonify(response_fail), CODE_BAD_REQUEST elif r.method == 'PUT': if "id" not in r.form: logger.warn("host put without enough data") response_fail["error"] = "host PUT without enough data" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST else: id, d = r.form["id"], {} for k in r.form: if k != "id": d[k] = r.form.get(k) result = host_handler.update(id, d) if result: logger.debug("host PUT successfully") return jsonify(response_ok), CODE_OK else: logger.debug("host PUT failed") response_fail["error"] = "Failed to update host {}".format( result.get("name")) return jsonify(response_fail), CODE_BAD_REQUEST elif r.method == 'DELETE': if "id" not in r.form or not r.form["id"]: logger.warn("host operation post without enough data") response_fail["error"] = "host delete without enough data" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST else: logger.debug("host delete with id={0}".format(r.form["id"])) if host_handler.delete(id=r.form["id"]): return jsonify(response_ok), CODE_OK else: response_fail["error"] = "Failed to delete host {}".format( r.form["id"]) return jsonify(response_fail), CODE_BAD_REQUEST else: response_fail["error"] = "unknown operation method" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST