def host_check_fillup(host_id): """ Check one host. :param host_id: :return: """ host = host_handler.get_by_id(host_id) if host.get("autofill") == "true": logger.info("Host {}/{}: checking auto-fillup".format( host_handler.get_by_id(host_id).get('name'), host_id)) host_handler.fillup(host_id)
def host_query(host_id): request_debug(r, logger) result = host_handler.schema(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 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 host_check_networks(host_id): """ Check the chain health on the host. :param host_id: :return: """ host = host_handler.get_by_id(host_id) logger.debug("Host {}/{}: checking chains".format(host.name, host_id)) clusters = cluster_handler.list(filter_data={"status": "running"}) for c in clusters: # concurrent health check is safe for multi-chains t = Thread(target=network_check_health, args=(c.get("id"), )) t.start() t.join(timeout=15)
def host_check_chains(host_id): """ Check the chain health on the host. :param host_id: :return: """ logger.debug("Host {}/{}: checking chains".format( host_handler.get_by_id(host_id).get('name'), host_id)) clusters = cluster_handler.list(filter_data={"host_id": host_id, "status": "running"}) for c in clusters: # concurrent health check is safe for multi-chains t = Thread(target=chain_check_health, args=(c.get("id"),)) t.start() t.join(timeout=15)
def host_check(host_id, retries=3, period=3): """ Run check on specific host. Check status and check each chain's health. :param host_id: id of the checked host :param retries: how many retries before thnking it's inactive :param period: retry wait :return: """ for _ in range(retries): if host_handler.refresh_status(host_id): # host is active logger.debug("Host {}/{} is active, start checking".format( host_handler.get_by_id(host_id).get('name'), host_id)) host_check_chains(host_id) time.sleep(period) host_check_fillup(host_id) break time.sleep(period)
def host_info(host_id): logger.debug("/ host_info/{0} method={1}".format(host_id, r.method)) return render_template("host_info.html", item=host_handler.get_by_id(host_id))
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
def host_info(host_id): logger.debug("/ host_info/{0} method={1}".format(host_id, r.method)) return render_template("host_info.html", item=host_handler.get_by_id( host_id))