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_delete(): """Delete a cluster DELETE /cluster { id: xxx col_name: active } :return: response obj """ logger.info("/cluster action=" + r.method) request_data = r.get_json(force=True, silent=True) if r.form: cluster_id = r.form["id"] col_name = r.form["col_name"] else: cluster_id = request_data.get("id") col_name = request_data.get("col_name") request_debug(r, logger) if not cluster_id or not col_name: error_msg = "cluster operation post without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: logger.debug("cluster delete with id={0}, col_name={1}".format( cluster_id, col_name)) delete_cluster.delay(cluster_id, col_name) return make_ok_resp()
def clusters_show(): request_debug(r, logger) show_type = r.args.get("type", "active") col_filter = dict((key, r.args.get(key)) for key in r.args if key != "col_name" and key != "page" and key != "type") if show_type != "released": col_name = r.args.get("col_name", "active") else: col_name = r.args.get("col_name", "released") if show_type == "inused": col_filter["user_id"] = {"$ne": ""} clusters = list(cluster_handler.list(filter_data=col_filter, col_name=col_name)) if show_type == "active": clusters.sort(key=lambda x: str(x["create_ts"]), reverse=True) elif show_type == "inused": clusters.sort(key=lambda x: str(x["apply_ts"]), reverse=True) else: clusters.sort(key=lambda x: str(x["release_ts"]), reverse=True) total_items = len(clusters) hosts = list(host_handler.list()) hosts_avail = list(filter(lambda e: e["status"] == "active" and len( e["clusters"]) < e["capacity"], hosts)) return render_template("clusters.html", type=show_type, col_name=col_name, items_count=total_items, items=clusters, hosts_available=hosts_avail, network_type=NETWORK_TYPES, consensus_plugins=CONSENSUS_PLUGINS_FABRIC_V1, consensus_modes=CONSENSUS_MODES, cluster_sizes=NETWORK_SIZE_FABRIC_V1)
def hosts_list(): logger.info("/hosts_list method=" + r.method) request_debug(r, logger) col_filter = dict((key, r.args.get(key)) for key in r.args) items = list(host_handler.list(filter_data=col_filter)) return make_ok_resp(data=items)
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_delete(): """Delete a cluster DELETE /cluster { id: xxx col_name: active } :return: response obj """ logger.info("/cluster action=" + r.method) request_debug(r, logger) if not r.form["id"] or not r.form["col_name"]: error_msg = "cluster operation post without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) else: logger.debug("cluster delete with id={0}, col_name={1}".format( r.form["id"], r.form["col_name"])) if r.form["col_name"] == "active": result = cluster_handler.delete(id=r.form["id"]) else: result = cluster_handler.delete_released(id=r.form["id"]) if result: return make_ok_response() else: error_msg = "Failed to delete cluster {}".format(r.form["id"]) logger.warning(error_msg) return make_fail_response(error=error_msg)
def health(): request_debug(r, logger) result = { 'health': 'OK', 'version': version } return jsonify(result), CODE_OK
def delete_user(user_id): request_debug(r, logger) try: user = UserModel.objects.get(id=user_id) except Exception: pass else: user.delete() return make_ok_resp(data={})
def host_query(host_id): request_debug(r, logger) result = host_handler.get_by_id(host_id) logger.debug(result) if result: return make_ok_resp(data=result) else: error_msg = "host not found with id=" + host_id logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form)
def cluster_create(): """Create a cluster on a host POST /cluster { name: xxx, host_id: xxx, network_type=fabric-0.6, consensus_plugin: pbft, consensus_mode: batch, size: 4, } :return: response object """ logger.info("/cluster action=" + r.method) request_debug(r, logger) if not r.form["name"] or not r.form["host_id"] or \ not r.form["network_type"]: error_msg = "cluster post without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) name, host_id, network_type, size = \ r.form['name'], r.form['host_id'],\ r.form['network_type'], int(r.form['size']) if network_type == NETWORK_TYPE_FABRIC_PRE_V1: # TODO: deprecated soon config = FabricPreNetworkConfig( consensus_plugin=r.form['consensus_plugin'], consensus_mode=r.form['consensus_mode'], size=size) elif network_type == NETWORK_TYPE_FABRIC_V1: config = FabricV1NetworkConfig( consensus_plugin=r.form['consensus_plugin'], size=size) else: error_msg = "Unknown network_type={}".format(network_type) logger.warning(error_msg) return make_fail_resp() if not config.validate(): return make_fail_resp(error="config not validated", data=config.get_data()) if cluster_handler.create(name=name, host_id=host_id, config=config): logger.debug("cluster POST successfully") return make_ok_resp(code=CODE_CREATED) else: logger.debug("cluster creation failed using handlder") return make_fail_resp(error="Failed to create cluster {}". format(name))
def get(): request_debug(r, logger) res = r.args.get('res') if res == 'host': result = stat_handler.hosts() elif res == 'cluster': result = stat_handler.clusters() else: result = { 'example': '/api/stat?res=host' } return jsonify(result), 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_FABRIC_V1: 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_create(): """Create a cluster on a host POST /cluster { name: xxx, host_id: xxx, network_type=fabric-0.6, consensus_plugin: pbft, consensus_mode: batch, size: 4, } :return: response object """ logger.info("/cluster action=" + r.method) request_debug(r, logger) if not r.form["name"] or not r.form["host_id"] or \ not r.form["network_type"]: error_msg = "cluster post without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) name, host_id, network_type, size = \ r.form['name'], r.form['host_id'],\ r.form['network_type'], int(r.form['size']) if network_type == NETWORK_TYPE_FABRIC_PRE_V1: # TODO: deprecated soon config = FabricPreNetworkConfig( consensus_plugin=r.form['consensus_plugin'], consensus_mode=r.form['consensus_mode'], size=size) elif network_type == NETWORK_TYPE_FABRIC_V1: config = FabricV1NetworkConfig( consensus_plugin=r.form['consensus_plugin'], size=size) else: error_msg = "Unknown network_type={}".format(network_type) logger.warning(error_msg) return make_fail_resp() if not config.validate(): return make_fail_resp(error="config not validated", data=config.get_data()) if cluster_handler.create(name=name, host_id=host_id, config=config): logger.debug("cluster POST successfully") return make_ok_resp(code=CODE_CREATED) else: logger.debug("cluster creation failed using handlder") return make_fail_resp(error="Failed to create cluster {}".format(name))
def get(self): request_debug(r, logger) user = utils._get_user() user = user.dbUser user_id = str(user.id) args = cluster_list_parser.parse_args() page = args['pageNo'] per_page = args['pageSize'] result = cluster_handler.list(page_no=page, page_size=per_page, user=user) # result = cluster_handler.list(page_no=page, page_size=per_page, user_id=user_id) return make_ok_resp(data=result)
def host_actions(): logger.info("/host_op, method=" + r.method) request_debug(r, logger) if r.content_type.startswith("application/json"): body = dict(r.get_json(force=True, silent=True)) else: body = r.form host_id, action = body['id'], body['action'] if not host_id or not action: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body) else: if action == "fillup": if host_handler.fillup(host_id): logger.debug("fillup successfully") return make_ok_resp() else: error_msg = "Failed to fillup the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body) elif action == "clean": if host_handler.clean(host_id): logger.debug("clean successfully") return make_ok_resp() else: error_msg = "Failed to clean the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body) elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") try: host_model = HostModel.Query.get(id=host_id) clusters = ClusterModel.Query.\ filter(host=host_model.as_pointer) for cluster_item in clusters: cluster_item.delete() except Exception: pass return make_ok_resp() else: error_msg = "Failed to reset the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body) error_msg = "unknown host action={}".format(action) logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body)
def host_delete(): request_debug(r, logger) if "id" not in r.form or not r.form["id"]: error_msg = "host delete without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) else: logger.debug("host delete with id={0}".format(r.form["id"])) if host_handler.delete(id=r.form["id"]): return make_ok_response() else: error_msg = "Failed to delete host {}".format(r.form["id"]) logger.warning(error_msg) return make_fail_response(error=error_msg)
def get(): request_debug(r, logger) res = r.args.get('res') if res == 'hosts': result = stat_handler.hosts() elif res == 'clusters': result = stat_handler.clusters() else: result = { 'example': '_stat?res=hosts' } logger.debug(result) return jsonify(result), 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_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_create(): """Create a cluster on a host POST /cluster { name: xxx, host_id: xxx, consensus_plugin: pbft, consensus_mode: batch, size: 4, } :return: response object """ logger.info("/cluster action=" + r.method) request_debug(r, logger) if not r.form["name"] or not r.form["host_id"] or not \ r.form["consensus_plugin"] or not r.form["size"]: error_msg = "cluster post without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) else: name, host_id, consensus_plugin, consensus_mode, size = \ r.form['name'], r.form['host_id'], r.form['consensus_plugin'],\ r.form['consensus_mode'] or '', int(r.form[ "size"]) if consensus_plugin not in CONSENSUS_PLUGINS: logger.debug( "Unknown consensus_plugin={}".format(consensus_plugin)) return make_fail_response() if consensus_plugin != CONSENSUS_PLUGINS[0] and consensus_mode \ not in CONSENSUS_MODES: logger.debug("Invalid consensus, plugin={}, mode={}".format( consensus_plugin, consensus_mode)) return make_fail_response() if size not in CLUSTER_SIZES: logger.debug("Unknown cluster size={}".format(size)) return make_fail_response() if cluster_handler.create(name=name, host_id=host_id, consensus_plugin=consensus_plugin, consensus_mode=consensus_mode, size=size): logger.debug("cluster POST successfully") return make_ok_response(code=CODE_CREATED) else: logger.debug("cluster creation failed") return make_fail_response( error="Failed to create cluster {}".format(name))
def cluster_create(): """Create a cluster on a host POST /cluster { name: xxx, host_id: xxx, consensus_plugin: pbft, consensus_mode: batch, size: 4, } :return: response object """ logger.info("/cluster action=" + r.method) request_debug(r, logger) if not r.form["name"] or not r.form["host_id"] or not \ r.form["consensus_plugin"] or not r.form["size"]: error_msg = "cluster post without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) else: name, host_id, consensus_plugin, consensus_mode, size = \ r.form['name'], r.form['host_id'], r.form['consensus_plugin'],\ r.form['consensus_mode'] or '', int(r.form[ "size"]) if consensus_plugin not in CONSENSUS_PLUGINS: logger.debug("Unknown consensus_plugin={}".format( consensus_plugin)) return make_fail_response() if consensus_plugin != CONSENSUS_PLUGINS[0] and consensus_mode \ not in CONSENSUS_MODES: logger.debug("Invalid consensus, plugin={}, mode={}".format( consensus_plugin, consensus_mode)) return make_fail_response() if size not in CLUSTER_SIZES: logger.debug("Unknown cluster size={}".format(size)) return make_fail_response() if cluster_handler.create(name=name, host_id=host_id, consensus_plugin=consensus_plugin, consensus_mode=consensus_mode, size=size): logger.debug("cluster POST successfully") return make_ok_response(code=CODE_CREATED) else: logger.debug("cluster creation failed") return make_fail_response(error="Failed to create cluster {}". format(name))
def hosts_show(): logger.info("/hosts method=" + r.method) request_debug(r, logger) col_filter = dict((key, r.args.get(key)) for key in r.args) items = list(host_handler.list(filter_data=col_filter)) items.sort(key=lambda x: str(x["name"]), reverse=True) logger.debug(items) return render_template("hosts.html", items_count=len(items), items=items, host_types=WORKER_TYPES, log_types=CLUSTER_LOG_TYPES, log_levels=CLUSTER_LOG_LEVEL, )
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_query(cluster_id): """Query a json obj of a cluster GET /cluster/xxxx Return a json obj of the cluster. """ request_debug(r, logger) result = cluster_handler.get_by_id(cluster_id) if result: return make_ok_resp(data=result) else: error_msg = "cluster not found with id=" + cluster_id logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form, code=CODE_NOT_FOUND)
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 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 get(self): logger.info("/hosts_list method=" + r.method) request_debug(r, logger) args = host_list_parser.parse_args() page = args['pageNo'] per_page = args['pageSize'] offset = (page - 1) * per_page host_count = HostModel.objects.all().count() hosts = HostModel.objects.skip(offset).limit(per_page).order_by( '-timestamp') hosts_list = [] id = (page * per_page) - (per_page - 1) for host in hosts: host_info = { 'id': id, "host_id": str(host.id), # 'users': host.users, "host_name": host.name, "worker_api": host.worker_api, 'host_type': host.host_type, 'capacity': host.capacity, # 'create_ts': host.create_ts.strftime("%Y-%m-%d %H:%M:%S"), 'create_ts': (host.create_ts + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"), "active": host.status, 'clusters': host.clusters } hosts_list.append(host_info) id += 1 result = { "hosts": hosts_list, "totalCount": host_count, "pageSize": per_page, "pageNo": page } return make_ok_resp(data=result)
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 host_actions(): logger.info("/host_op, method=" + r.method) request_debug(r, logger) host_id, action = r.form['id'], r.form['action'] if not host_id or not action: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: if action == "fillup": if host_handler.fillup(host_id): logger.debug("fillup successfully") return make_ok_resp() else: error_msg = "Failed to fillup the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) elif action == "clean": if host_handler.clean(host_id): logger.debug("clean successfully") return make_ok_resp() else: error_msg = "Failed to clean the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") try: host_model = HostModel.objects.get(id=host_id) clusters = ClusterModel.objects(host=host_model) for cluster_item in clusters: cluster_item.delete() except Exception: pass return make_ok_resp() else: error_msg = "Failed to reset the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) error_msg = "unknown host action={}".format(action) logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form)
def show(): request_debug(r, logger) hosts = list(host_handler.list(filter_data={})) hosts.sort(key=lambda x: x["name"], reverse=False) hosts_active = list(filter(lambda e: e["status"] == "active", hosts)) hosts_inactive = list(filter(lambda e: e["status"] != "active", hosts)) hosts_free = list( filter(lambda e: len(e["clusters"]) < e["capacity"], hosts_active)) hosts_available = hosts_free clusters_active = len(list(cluster_handler.list(col_name="active"))) clusters_released = len(list(cluster_handler.list(col_name="released"))) clusters_free = len( list( cluster_handler.list(filter_data={"user_id": ""}, col_name="active"))) clusters_inuse = clusters_active - clusters_free clusters_temp = len( list( cluster_handler.list(filter_data={"user_id": "/^__/"}, col_name="active"))) user_id, username, is_admin = \ str(current_user.id), current_user.username, current_user.isAdmin return render_template("index.html", hosts=hosts, hosts_free=hosts_free, hosts_active=hosts_active, hosts_inactive=hosts_inactive, hosts_available=hosts_available, clusters_active=clusters_active, clusters_released=clusters_released, clusters_free=clusters_free, clusters_inuse=clusters_inuse, clusters_temp=clusters_temp, cluster_sizes=NETWORK_SIZE_FABRIC_PRE_V1, network_type=NETWORK_TYPES, consensus_plugins=CONSENSUS_PLUGINS_FABRIC_V1, consensus_modes=CONSENSUS_MODES, host_types=WORKER_TYPES, log_types=CLUSTER_LOG_TYPES, log_levels=CLUSTER_LOG_LEVEL, username=username, user_id=user_id, is_admin=is_admin)
def register(): request_debug(r, logger) if not r.form["username"] or not r.form["password"]: error_msg = "register without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) username, password = r.form["username"], r.form["password"] salt = app.config.get("SALT", b"") password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode())) try: user = User(username, password) user.save() return make_ok_resp(code=CODE_CREATED) except Exception as exc: logger.info("exc %s", exc) return make_fail_resp(error="register failed")
def cluster_list(): """List clusters with the filter e.g., GET /clusters?consensus_plugin=pbft Return objs of the clusters. """ request_debug(r, logger) f = {} if r.method == 'GET': f.update(r.args.to_dict()) elif r.method == 'POST': f.update(request_json_body(r)) logger.info(f) result = cluster_handler.list(filter_data=f) logger.error(result) return make_ok_resp(data=result)
def cluster_list(): """List clusters with the filter e.g., GET /clusters?consensus_plugin=pbft Return objs of the clusters. """ request_debug(r, logger) f = {} if r.method == 'GET': f.update(r.args.to_dict()) elif r.method == 'POST': f.update(request_json_body(r)) logger.info(f) result = cluster_handler.list(filter_data=f) logger.error(result) return make_ok_response(data=result)
def host_delete(): request_debug(r, logger) request_data = r.get_json(force=True, silent=True) if "id" in r.form: host_id = r.form["id"] elif "id" in request_data: host_id = request_data.get("id") else: error_msg = "host delete without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) logger.debug("host delete with id={0}".format(host_id)) if host_handler.delete(id=host_id): return make_ok_resp() else: error_msg = "Failed to delete host {}".format(host_id) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def host_create(): request_debug(r, logger) name, worker_api, capacity, log_type, log_server, log_level, host_type = \ r.form['name'], r.form['worker_api'], r.form['capacity'], \ r.form['log_type'], r.form['log_server'], r.form['log_level'], \ r.form['host_type'] if 'host_type' in r.form else None 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={}, worker_api={}, capacity={}" "fillup={}, schedulable={}, log={}/{}".format( name, worker_api, capacity, autofill, schedulable, log_type, log_server)) if not name or not worker_api or not capacity or not log_type: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: host_type = host_type if host_type else detect_daemon_type(worker_api) result = host_handler.create(name=name, worker_api=worker_api, capacity=int(capacity), autofill=autofill, schedulable=schedulable, log_level=log_level, log_type=log_type, log_server=log_server, host_type=host_type) if result: logger.debug("host creation successfully") return make_ok_resp(code=CODE_CREATED) else: error_msg = "Failed to create host {}".format(r.form["name"]) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def host_update(): request_debug(r, logger) if "id" not in r.form: error_msg = "host PUT without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) 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 make_ok_response() else: error_msg = "Failed to update host {}".format(result.get("name")) logger.warning(error_msg) return make_fail_response(error=error_msg)
def show(): request_debug(r, logger) hosts = list(host_handler.list(filter_data={})) hosts.sort(key=lambda x: x["name"], reverse=False) hosts_active = list(filter(lambda e: e["status"] == "active", hosts)) hosts_inactive = list(filter(lambda e: e["status"] != "active", hosts)) hosts_free = list( filter(lambda e: len(e["clusters"]) < e["capacity"], hosts_active)) hosts_available = hosts_free clusters_active = len(list(cluster_handler.list(col_name="active"))) clusters_released = len(list(cluster_handler.list(col_name="released"))) clusters_free = len( list( cluster_handler.list(filter_data={"user_id": ""}, col_name="active"))) clusters_inuse = clusters_active - clusters_free clusters_temp = len( list( cluster_handler.list(filter_data={"user_id": "/^__/"}, col_name="active"))) return render_template( "index.html", hosts=hosts, hosts_free=hosts_free, hosts_active=hosts_active, hosts_inactive=hosts_inactive, hosts_available=hosts_available, clusters_active=clusters_active, clusters_released=clusters_released, clusters_free=clusters_free, clusters_inuse=clusters_inuse, clusters_temp=clusters_temp, cluster_sizes=CLUSTER_SIZES, fabric_version=FABRIC_VERSION, consensus_plugins=CONSENSUS_PLUGINS, consensus_modes=CONSENSUS_MODES, host_types=HOST_TYPES, log_types=CLUSTER_LOG_TYPES, log_levels=CLUSTER_LOG_LEVEL, )
def organization_update(organization_id): request_debug(r, logger) if r.content_type.startswith("application/json"): body = dict(r.get_json(force=True, silent=True)) else: body = r.form id = organization_id peerNum = body["peerNum"] result = org_handler().update(id, peerNum) if result: logger.debug("organization PUT successfully") return make_ok_my_resp(resource='organization', result=result) else: error_msg = "Failed to update organization {}".format( result.get("name")) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def host_update(): request_debug(r, logger) if "id" not in r.form: error_msg = "host PUT without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) 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 make_ok_resp() else: error_msg = "Failed to update host {}".format(result.get("name")) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def update_user(user_id): request_debug(r, logger) if not r.form["username"] or not r.form["password"] \ or not r.form["role"]: error_msg = "create user without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) username, role = r.form["username"], int(r.form["role"]) active = r.form["active"] active = active == "true" try: UserModel.objects(id=user_id).update(set__username=username, set__active=active, set__role=role, upsert=True) except Exception as exc: error_msg = exc.message logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) return make_ok_resp(data={})
def blockchain_network_create_yarml_for_neworgs(blockchain_network_id): request_debug(r, logger) if r.content_type.startswith("application/json"): body = dict(r.get_json(force=True, silent=True))['blockchain_network'] else: error_msg = "request header content-type is not supported, use application/json" raise UnsupportedMediaType(msg=error_msg) peer_orgs = body.get('peer_orgs', None) if peer_orgs is None: raise BadRequest(msg="peer_orgs must be provided in request body") network_handler = BlockchainNetworkHandler() try: network = network_handler.createyamlforneworgs( id=blockchain_network_id, peer_orgs=peer_orgs) return make_ok_gaea_resp(resource='blockchain_network', result=network) except Exception as e: msg = "blockchain_network add org failed {}".format(e) logger.error(msg) raise InternalServerError(msg=msg)
def list_user(): request_debug(r, logger) f = {} f.update(r.args.to_dict()) page = int(f.get("pageNo", 1)) per_page = int(f.get("pageSize", 10)) sort_columns = f.get("sortColumns", "") sort_columns = sort_columns.split(" ") sort_str = '' if len(sort_columns) > 1: sort_type = sort_columns[1] sort_field = sort_columns[0] if sort_type == "desc": sort_str = "-%s" % sort_field else: sort_str = sort_field offset = (page - 1) * per_page user_count = UserModel.objects.all().count() users = UserModel.objects.skip(offset).limit(per_page).order_by(sort_str) users = [{ "id": str(user.id), "name": user.username, "isAdmin": user.isAdmin, "role": user.role, "active": user.active, "timestamp": time.mktime(user.timestamp.timetuple()) } for user in users] result = { "users": { "result": users, "totalCount": user_count, "pageSize": per_page, "pageNo": page }, } return make_ok_resp(data=result)
def host_action(): logger.info("/host_action, method=" + r.method) request_debug(r, logger) host_id, action = r.form['id'], r.form['action'] if not host_id or not action: 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: if action == "fillup": if host_handler.fillup(host_id): logger.debug("fillup successfully") return jsonify(response_ok), CODE_OK else: response_fail["data"] = r.form response_fail["error"] = "Failed to fillup the host." return jsonify(response_fail), CODE_BAD_REQUEST elif action == "clean": if host_handler.clean(host_id): logger.debug("clean successfully") return jsonify(response_ok), CODE_OK else: response_fail["data"] = r.form response_fail["error"] = "Failed to clean the host." return jsonify(response_fail), CODE_BAD_REQUEST elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") return jsonify(response_ok), CODE_OK else: response_fail["data"] = r.form response_fail["error"] = "Failed to reset the host." return jsonify(response_fail), CODE_BAD_REQUEST logger.warn("unknown host action={}".format(action)) response_fail["error"] = "unknown operation method" response_fail["data"] = r.form return jsonify(response_fail), CODE_BAD_REQUEST
def host_create(): request_debug(r, logger) 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: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) 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 make_ok_response(), CODE_CREATED else: error_msg = "Failed to create host {}".format(r.form["name"]) logger.warning(error_msg) return make_fail_response(error=error_msg)
def clusters_show(): request_debug(r, logger) show_type = r.args.get("type", "active") col_filter = dict((key, r.args.get(key)) for key in r.args if key != "col_name" and key != "page" and key != "type") if show_type != "released": col_name = r.args.get("col_name", "active") else: col_name = r.args.get("col_name", "released") if show_type == "inused": col_filter["user_id"] = {"$ne": ""} clusters = list( cluster_handler.list(filter_data=col_filter, col_name=col_name)) if show_type == "active": clusters.sort(key=lambda x: str(x["create_ts"]), reverse=True) elif show_type == "inused": clusters.sort(key=lambda x: str(x["apply_ts"]), reverse=True) else: clusters.sort(key=lambda x: str(x["release_ts"]), reverse=True) total_items = len(clusters) hosts = list(host_handler.list()) hosts_avail = list( filter( lambda e: e["status"] == "active" and len(e["clusters"]) < e[ "capacity"], hosts)) # TODO: need to use consensus_type as the combination of plugin+mode return render_template("clusters.html", type=show_type, col_name=col_name, items_count=total_items, items=clusters, hosts_available=hosts_avail, network_type=NETWORK_TYPES, consensus_plugins=CONSENSUS_PLUGINS_FABRIC_V1, consensus_modes=CONSENSUS_MODES_FABRIC_V1, cluster_sizes=NETWORK_SIZE_FABRIC_V1)
def create_user(): request_debug(r, logger) if not r.form["username"] or not r.form["password"] \ or not r.form["role"]: error_msg = "create user without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) username, password = r.form["username"], r.form["password"] role, active = int(r.form["role"]), r.form["active"] active = active == "true" salt = app.config.get("SALT", b"") password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode())) try: user = User(username, password, is_admin=role == ADMIN, role=role, active=active) user.save() return make_ok_resp(code=CODE_CREATED) except Exception as exc: logger.info("exc %s", exc) return make_fail_resp(error="create user failed")
def show(): request_debug(r, logger) hosts = list(host_handler.list(filter_data={})) hosts.sort(key=lambda x: x["name"], reverse=False) hosts_active = list(filter(lambda e: e["status"] == "active", hosts)) hosts_inactive = list(filter(lambda e: e["status"] != "active", hosts)) hosts_free = list(filter( lambda e: len(e["clusters"]) < e["capacity"], hosts_active)) hosts_available = hosts_free clusters_active = len(list(cluster_handler.list(col_name="active"))) clusters_released = len(list(cluster_handler.list(col_name="released"))) clusters_free = len(list(cluster_handler.list(filter_data={"user_id": ""}, col_name="active"))) clusters_inuse = clusters_active - clusters_free clusters_temp = len(list(cluster_handler.list(filter_data={ "user_id": "/^__/"}, col_name="active"))) username, is_admin = current_user.username, current_user.isAdmin return render_template("index.html", hosts=hosts, hosts_free=hosts_free, hosts_active=hosts_active, hosts_inactive=hosts_inactive, hosts_available=hosts_available, clusters_active=clusters_active, clusters_released=clusters_released, clusters_free=clusters_free, clusters_inuse=clusters_inuse, clusters_temp=clusters_temp, cluster_sizes=NETWORK_SIZE_FABRIC_PRE_V1, network_type=NETWORK_TYPES, consensus_plugins=CONSENSUS_PLUGINS_FABRIC_V1, consensus_modes=CONSENSUS_MODES, host_types=WORKER_TYPES, log_types=CLUSTER_LOG_TYPES, log_levels=CLUSTER_LOG_LEVEL, username=username, is_admin=is_admin )
def host_actions(): logger.info("/host_op, method=" + r.method) request_debug(r, logger) host_id, action = r.form['id'], r.form['action'] if not host_id or not action: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: if action == "fillup": if host_handler.fillup(host_id): logger.debug("fillup successfully") return make_ok_resp() else: error_msg = "Failed to fillup the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) elif action == "clean": if host_handler.clean(host_id): logger.debug("clean successfully") return make_ok_resp() else: error_msg = "Failed to clean the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") return make_ok_resp() else: error_msg = "Failed to reset the host." logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) error_msg = "unknown host action={}".format(action) logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form)
def host_create(): request_debug(r, logger) name, worker_api, capacity, log_type, log_server, log_level = \ r.form['name'], r.form['worker_api'], 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={}, worker_api={}, capacity={}" "fillup={}, schedulable={}, log={}/{}". format(name, worker_api, capacity, autofill, schedulable, log_type, log_server)) if not name or not worker_api or not capacity or not log_type: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: result = host_handler.create(name=name, worker_api=worker_api, 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 make_ok_resp(code=CODE_CREATED) else: error_msg = "Failed to create host {}".format(r.form["name"]) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def host_actions(): logger.info("/host_op, method=" + r.method) request_debug(r, logger) host_id, action = r.form['id'], r.form['action'] if not host_id or not action: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) else: if action == "fillup": if host_handler.fillup(host_id): logger.debug("fillup successfully") return make_ok_response() else: error_msg = "Failed to fillup the host." logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) elif action == "clean": if host_handler.clean(host_id): logger.debug("clean successfully") return make_ok_response() else: error_msg = "Failed to clean the host." logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") return make_ok_response() else: error_msg = "Failed to reset the host." logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form) error_msg = "unknown host action={}".format(action) logger.warning(error_msg) return make_fail_response(error=error_msg, data=r.form)
def host_update(): request_debug(r, logger) if r.content_type.startswith("application/json"): body = dict(r.get_json(force=True, silent=True)) else: body = r.form if "id" not in body: error_msg = "host PUT without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=body) else: id, d = body["id"], {} for k in body: if k != "id": d[k] = body.get(k) result = host_handler.update(id, d) if result: logger.debug("host PUT successfully") return make_ok_resp() else: error_msg = "Failed to update host {}".format(result.get("name")) logger.warning(error_msg) return make_fail_resp(error=error_msg)
def login(): request_debug(r, logger) return render_template("login.html")
def host_create(): request_debug(r, logger) name, worker_api, capacity, log_type, log_server, log_level, host_type = \ r.form['name'], r.form['worker_api'], r.form['capacity'], \ r.form['log_type'], r.form['log_server'], r.form['log_level'], \ r.form['host_type'] if 'host_type' in r.form else None 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" if host_type == "vsphere": vcaddress = r.form['vc_address'] if vcaddress.find(":") == -1: address = vcaddress port = "443" else: address = vcaddress.split(':')[0] port = vcaddress.split(':')[1] logger.debug("address={}, port={}".format(address, port)) vmname = "cello-vsphere-" + str(uuid.uuid1()) vsphere_param = { 'vc': { 'address': address, 'port': port, 'username': r.form['vc_user'], 'password': r.form['vc_password'], 'network': r.form['vc_network'], 'vc_datastore': r.form['datastore'], 'vc_datacenter': r.form['datacenter'], 'vc_cluster': r.form['cluster'], 'template': r.form['vm_template']}, 'vm': { 'vmname': vmname, 'ip': r.form['vm_ip'], 'gateway': r.form['vm_gateway'], 'netmask': r.form['vm_netmask'], 'dns': r.form['vm_dns'], 'vcpus': int(r.form['vm_cpus']), 'memory': int(r.form['vm_memory'])}} logger.debug("name={}, capacity={}," "fillup={}, schedulable={}, log={}/{}, vsphere_param={}". format(name, capacity, autofill, schedulable, log_type, log_server, vsphere_param)) vsphere_must_have_params = { 'Name': name, 'Capacity': capacity, 'LoggingType': log_type, 'VCAddress': address, 'VCUser': r.form['vc_user'], 'VCPassword': r.form['vc_password'], 'VCNetwork': r.form['vc_network'], 'Datastore': r.form['datastore'], 'Datacenter': r.form['datacenter'], 'Cluster': r.form['cluster'], 'VMIp': r.form['vm_ip'], 'VMGateway': r.form['vm_gateway'], 'VMNetmask': r.form['vm_netmask']} for key in vsphere_must_have_params: if vsphere_must_have_params[key] == '': error_msg = "host POST without {} data".format(key) logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) result = host_handler.create(name=name, worker_api=worker_api, capacity=int(capacity), autofill=autofill, schedulable=schedulable, log_level=log_level, log_type=log_type, log_server=log_server, host_type=host_type, params=vsphere_param) else: logger.debug("name={}, worker_api={}, capacity={}" "fillup={}, schedulable={}, log={}/{}". format(name, worker_api, capacity, autofill, schedulable, log_type, log_server)) if not name or not worker_api or not capacity or not log_type: error_msg = "host POST without enough data" logger.warning(error_msg) return make_fail_resp(error=error_msg, data=r.form) else: host_type = host_type if host_type \ else detect_daemon_type(worker_api) result = host_handler.create(name=name, worker_api=worker_api, capacity=int(capacity), autofill=autofill, schedulable=schedulable, log_level=log_level, log_type=log_type, log_server=log_server, host_type=host_type) logger.debug("result.msg={}".format(result.get('msg'))) if (host_type == "vsphere") and ('msg' in result): vsphere_errmsg = result.get('msg') error_msg = "Failed to create vsphere host {}".format(vsphere_errmsg) logger.warning(error_msg) return make_fail_resp(error=error_msg) elif result: logger.debug("host creation successfully") return make_ok_resp(code=CODE_CREATED) else: error_msg = "Failed to create host {}".format(r.form["name"]) logger.warning(error_msg) return make_fail_resp(error=error_msg)