def ws_myjobs(): """ provide job list to probe asking for """ logging.debug("/myjobs") # global lDB if request.method == 'POST': _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return host conf.reload() jobs = lDB.getJobsForHost(host) return make_response(jsonify({"answer": "OK", "jobs": jobs}), 200) return make_response( jsonify({ "answer": "KO", "reason": "bad method used" }), 400)
def ws_myconfig(): """ provide main configuration for the probe """ logging.info("/myConfig") # global lDB _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return host config = lDB.getConfigForHost(host) r = { 'probename': config['probename'], 'hostname': config['hostname'], 'firmware': config['firmware'] } return make_response(jsonify({"answer": "OK", "config": r}), 200)
def ws_myconfig(): """ provide main configuration for the probe """ logging.info("/myConfig") # global lDB _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return host config = lDB.getConfigForHost(host) r = { 'probename' : config['probename'], 'hostname' : config['hostname'], 'firmware' : config['firmware'] } return make_response(jsonify({"answer" : "OK", "config" : r}), 200)
def ws_myjobs(): """ provide job list to probe asking for """ logging.debug("/myjobs") # global lDB if request.method == 'POST': _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return host conf.reload() jobs = lDB.getJobsForHost(host) return make_response(jsonify({"answer" : "OK", "jobs" : jobs}), 200) return make_response(jsonify({"answer" : "KO", "reason" : "bad method used"}), 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)
def ws_upgrade(): """ upgrade web service, sends back to PI the appropriate version """ logging.debug("/upgrade") # global lDB if request.method == 'POST': _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return make_response(jsonify({"answer" : "KO", "reason" : "probe not found"}), 404) sVersion = lDB.getHostVersionByUid(uid) if sVersion == None: logging.warning("probe with no version") return make_response(jsonify({"answer" : "KO", "reason" : "no version provided"}), 400) # iProbeVersion = versionToInt(sVersion) root = os.path.join(os.getcwd(), "static") nextVersion = defNextVersion(sVersion, conf.getFWVersion(conf.getFWVersionForHost(host))) # iNextVersion = versionToInt(nextVersion) fileName = "netprobe_{}_all.deb".format(nextVersion) # checks wether the file exists if not os.path.isfile("{}/{}".format(root, fileName)): logging.error("firmware file does not exists {}/{}".format(root, fileName)) return make_response(jsonify({"answer" : "KO", "reason" : "firmware file not found", "file": "{}".format(fileName)}), 404) logging.info("current version {} for {}, next version {}, file {}".format(sVersion, host, nextVersion, fileName)) # return send_from_directory(root, fileName) if sVersion == nextVersion: logging.info("no need for upgrade") return make_response(jsonify({"answer" : "OK", "reason" : "no need for upgrade"}), 201) return send_from_directory(root, fileName)
def ws_ping(): """ answers ok if everything is good """ logging.debug("/ping") # global lDB # global conf r = { "answer" : "OK" } if request.method == 'POST': _r = wsCheckParams(["uid", "hostId"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode) and not isinstance(host, str): return host if host != request.form['hostId']: logging.error("bad probe {} {}".format(host, request.form['hostId'])) return make_response(jsonify({"answer" : "bad probe matching id and hostid"}), 400) lDB.updateHost(host, {"last" : time.time()}) a = lDB.getAction(host) if a != None: r['action'] = a return make_response(jsonify(r), 200) r = { "answer" : "KO" } return make_response(jsonify(r), 500)
def ws_ping(): """ answers ok if everything is good """ logging.debug("/ping") # global lDB # global conf r = {"answer": "OK"} if request.method == 'POST': _r = wsCheckParams(["uid", "hostId"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode) and not isinstance(host, str): return host if host != request.form['hostId']: logging.error("bad probe {} {}".format(host, request.form['hostId'])) return make_response( jsonify({"answer": "bad probe matching id and hostid"}), 400) lDB.updateHost(host, {"last": time.time()}) a = lDB.getAction(host) if a != None: r['action'] = a return make_response(jsonify(r), 200) r = {"answer": "KO"} return make_response(jsonify(r), 500)
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_upgrade(): """ upgrade web service, sends back to PI the appropriate version """ logging.debug("/upgrade") # global lDB if request.method == 'POST': _r = wsCheckParams(["uid"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode): return make_response( jsonify({ "answer": "KO", "reason": "probe not found" }), 404) sVersion = lDB.getHostVersionByUid(uid) if sVersion == None: logging.warning("probe with no version") return make_response( jsonify({ "answer": "KO", "reason": "no version provided" }), 400) # iProbeVersion = versionToInt(sVersion) root = os.path.join(os.getcwd(), "static") nextVersion = defNextVersion( sVersion, conf.getFWVersion(conf.getFWVersionForHost(host))) # iNextVersion = versionToInt(nextVersion) fileName = "netprobe_{}_all.deb".format(nextVersion) # checks wether the file exists if not os.path.isfile("{}/{}".format(root, fileName)): logging.error("firmware file does not exists {}/{}".format( root, fileName)) return make_response( jsonify({ "answer": "KO", "reason": "firmware file not found", "file": "{}".format(fileName) }), 404) logging.info( "current version {} for {}, next version {}, file {}".format( sVersion, host, nextVersion, fileName)) # return send_from_directory(root, fileName) if sVersion == nextVersion: logging.info("no need for upgrade") return make_response( jsonify({ "answer": "OK", "reason": "no need for upgrade" }), 201) return send_from_directory(root, fileName)
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)