def ws_results(): """ answers ok if everything is good """ logging.debug("/results") _r = wsCheckParams(["uid", "data"]) if _r != None: return _r uid = int(request.form['uid']) host = lDB.getHostByUid(uid) if host == None: logging.error("probe not known {}".format(uid)) return make_response(jsonify({"answer" : "KO", "reason":"probe not known"}), 404) probename = lDB.getNameForHost(host) fields = lDB.getFieldsForHost(host) bCompressed = False if request.form.__contains__('compressed') and request.form['compressed'] == "yes": bCompressed = True data = [] b64d = b64decode(request.form['data']) try: if bCompressed: s = zlib.decompress(b64d) data = json.loads(s) else: data.append(json.loads(b64d)) except Exception as ex: logging.error("{}".format(" ".join(ex.args))) return make_response(jsonify({"answer" : "KO", "reason":"no json"}), 417) for d in data: if not d.__contains__('date'): logging.error("missing date") return make_response(jsonify({"answer" : "KO", "reason":"missing date"}), 417) d['timestamp'] = datetime.datetime.utcfromtimestamp(d['date']).isoformat() d['probeuid'] = uid d['probename'] = probename for o in outputer: logging.debug("output to {}".format(o)) # check for fields if fields != None: ot = o.getType() if ot in fields: d['data'].update(fields[ot]) o.send(d) return make_response(jsonify({"answer" : "OK"}), 200)
def wsCheckHostUID(uid): """check if we have the host in database""" # global lDB host = lDB.getHostByUid(uid) if host == None: return make_response(jsonify({"answer" : "KO", "reason" : "probe not known"}), 404) return host
def wsCheckHostUID(uid): """check if we have the host in database""" # global lDB host = lDB.getHostByUid(uid) if host == None: return make_response( jsonify({ "answer": "KO", "reason": "probe not known" }), 404) return host
def test_getHost_not(): """ getHostByUid return false on unknown host """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id}) _sHost = lDB.getHostByUid(2) if _sHost != None: assert False, "should return None" lDB.cleanDB()
def test_getHost(): """ getHostByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id}) _sHost = lDB.getHostByUid(1) if _sHost != "test": assert False, "should return name of the host" lDB.cleanDB()
def test_getHost_not(): """ getHostByUid return false on unknown host """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id}) _sHost = lDB.getHostByUid(2) if _sHost != None: assert False, "should return None" lDB.cleanDB()
def test_getHost(): """ getHostByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id}) _sHost = lDB.getHostByUid(1) if _sHost != "test": assert False, "should return name of the host" lDB.cleanDB()
def ws_pushAction(): """ add an action for the uid params : uid, action, module actions : restart """ logging.info("/pushAction") # global lDB _r = wsCheckParams(["uid", "action"]) if _r != None: return _r uid = int(request.form['uid']) action = str(request.form['action']) host = lDB.getHostByUid(uid) if host == None: return make_response( jsonify({ "answer": "KO", "reason": "host not found" }), 404) # global conf r = {"answer": "OK"} # ------------------------- if action == "restart": if request.form.__contains__('module') == False: return make_response( jsonify({ "answer": "KO", "reason": "missing module" }), 400) module = str(request.form['module']) r['action'] = "restart" r['target_uid'] = uid sAction = {"name": "restart"} if module == "all": sAction['args'] = {"module": module} lDB.updateHost(host, {"action": sAction}) if module == "job": if request.form.__contains__('job') == False: return make_response( jsonify({ "answer": "KO", "reason": "missing job" }), 400) job = str(request.form['job']) sAction['args'] = {"module": module, "job": job} lDB.updateHost(host, {"action": sAction}) return make_response(jsonify(r), 200) # ------------------------- if action == "upgrade": sAction = {"name": "upgrade"} sAction['args'] = {"when": "now"} lDB.updateHost(host, {"action": sAction}) return make_response(jsonify(r), 200) # exception r['answer'] = "KO" r['reason'] = "action not found" return make_response(jsonify(r), 400)
def ws_pushAction(): """ add an action for the uid params : uid, action, module actions : restart """ logging.info("/pushAction") # global lDB _r = wsCheckParams(["uid", "action"]) if _r != None: return _r uid = int(request.form['uid']) action = str(request.form['action']) host = lDB.getHostByUid(uid) if host == None: return make_response(jsonify({"answer":"KO", "reason":"host not found"}), 404) # global conf r = { "answer" : "OK" } # ------------------------- if action == "restart": if request.form.__contains__('module') == False: return make_response(jsonify({"answer":"KO", "reason":"missing module"}), 400) module = str(request.form['module']) r['action'] = "restart" r['target_uid'] = uid sAction = { "name" : "restart" } if module == "all": sAction['args'] = { "module" : module } lDB.updateHost(host, {"action" : sAction }) if module == "job": if request.form.__contains__('job') == False: return make_response(jsonify({"answer":"KO", "reason":"missing job"}), 400) job = str(request.form['job']) sAction['args'] = { "module" : module, "job" : job } lDB.updateHost(host, {"action" : sAction }) return make_response(jsonify(r), 200) # ------------------------- if action == "upgrade": sAction = { "name" : "upgrade" } sAction['args'] = { "when" : "now" } lDB.updateHost(host, {"action" : sAction }) return make_response(jsonify(r), 200) # exception r['answer'] = "KO" r['reason'] = "action not found" return make_response(jsonify(r), 400)
def ws_results(): """ answers ok if everything is good """ logging.debug("/results") _r = wsCheckParams(["uid", "data"]) if _r != None: return _r uid = int(request.form['uid']) host = lDB.getHostByUid(uid) if host == None: logging.error("probe not known {}".format(uid)) return make_response( jsonify({ "answer": "KO", "reason": "probe not known" }), 404) probename = lDB.getNameForHost(host) fields = lDB.getFieldsForHost(host) bCompressed = False if request.form.__contains__( 'compressed') and request.form['compressed'] == "yes": bCompressed = True data = [] b64d = b64decode(request.form['data']) try: if bCompressed: s = zlib.decompress(b64d) data = json.loads(s) else: data.append(json.loads(b64d)) except Exception as ex: logging.error("{}".format(" ".join(ex.args))) return make_response(jsonify({ "answer": "KO", "reason": "no json" }), 417) for d in data: if not d.__contains__('date'): logging.error("missing date") return make_response( jsonify({ "answer": "KO", "reason": "missing date" }), 417) d['timestamp'] = datetime.datetime.utcfromtimestamp( d['date']).isoformat() d['probeuid'] = uid d['probename'] = probename for o in outputer: logging.debug("output to {}".format(o)) # check for fields if fields != None: ot = o.getType() if ot in fields: d['data'].update(fields[ot]) o.send(d) return make_response(jsonify({"answer": "OK"}), 200)