Ejemplo n.º 1
0
    def post(self):
        self.set_header("Content-Type", "application/json")
        try:
            data = tornado.escape.json_decode(self.request.body)
            print(data)
        except Exception as e:
            elog(e)
            print(e)
            self.write(json.dumps({"err_code": -3}))
            self.finish()

        results = dataCenter.parse_setting(data)
        # print("parsing result", results)

        error_data, codes_to_execute = results
        print("error_data", error_data)
        print("codes_to_execute", codes_to_execute)

        self.write(json.dumps(error_data))

        dataFeeder.run_command(codes_to_execute)

        dataCenter.save()

        self.finish()
Ejemplo n.º 2
0
    def post(self):
        # pass

        try:
            data = tornado.escape.json_decode(self.request.body)
        except Exception as e:
            elog(e)
            self.write(json.dumps({"error": "unable to parse"}))
            self.finish()
        host = data.get("host")
        if not host:
            self.write(json.dumps({"error": "host not found"}))
            self.finish()
        if not valid(host):
            self.write(json.dumps({"error": "host not valid"}))
            self.finish()

        try:
            dataCenter.hosts.remove(host)
        except Exception as e:
            elogger.Exception(e)
        print("dataCenter.hosts:===>", dataCenter.hosts)
        dataCenter.save_host()

        self.write(json.dumps({"message": "successfully removed %s as remote platform" % host}))
        self.finish()
Ejemplo n.º 3
0
def set_network_config(kwargs):
    '''接收一个dict, 如{'netmask': '255.255.255.0', 'gateway': '192.168.0.1', 'address': '192.168.0.199'}。将其写入 / etc / network / interfaces'''
    old_config = get_network_config()
    try:
        old_ip, old_netmask, old_gateway, old_dns = old_config
    except Exception as e:
        old_ip, old_netmask, old_gateway, old_dns = "", "", "", ""
        rlog("failed to get old data")

    address = kwargs.get('address')
    netmask = kwargs.get('netmask')
    gateway = kwargs.get('gateway')
    dns = kwargs.get('dns')

    if not (is_valid(netmask) and is_valid(gateway) and is_valid(address) and is_valid(dns)):
        elog("invalid config")
        print("invalid config")
        # 检测三个数据都不为空。

    to_write = network_temp.generate(**kwargs)

    with open(setting.ip_config, 'w') as file:
        file.write(to_write)
    os.system("sync")
    print("network config synced")
Ejemplo n.º 4
0
 def get(self):
     self.set_header("Content-Type","application/json")
     "这个try将来可能会去掉,因为好像没啥用。"
     try:
         self.write(json.dumps(dataCenter.json_tencent_status))
     except Exception as e:
         elog(e.__repr__())
         self.write(json.dumps({'status': 'fail', 'message': e.__repr__()}))
     finally:
         self.finish()
Ejemplo n.º 5
0
def recover_blink(dataCenter):
    # 尝试recover:如果没有文件,就建一个新文件。
    if not os.path.exists(setting.backup_blink):
        with open(setting.backup_blink, 'w') as file:
            file.write("")
        rlog("没有找到闪灯频率的备份")
        return

    with open(setting.backup_blink, 'r') as file:
        try:
            self.blink_freq = json.loads(file.read())
            print("成功载入闪灯频率")
            print(self.blink_freq)
        except Exception as e:
            print("没有载入闪灯频率==>")
            elog(e)
Ejemplo n.º 6
0
    def post(self):
        try:
            data = tornado.escape.json_decode(self.request.body)
            print(data)
        except Exception as e:
            self.write(
                json.dumps({
                    "err_code": -3,
                    "message": "failed to parse input as json"
                }))
            # self.return()
            elog(e)
            print(e)
            self.finish()

        codes_to_execute, cache_dic, error_data = dataCenter.parse_blink_freq(
            data)
        dataFeeder.blink_freq = cache_dic
        dataCenter.save_blink()
        dataFeeder.run_command(codes_to_execute)
        self.write(json.dumps(error_data))
        self.finish()
Ejemplo n.º 7
0
    def post(self):
        try:
            data = tornado.escape.json_decode(self.request.body)
        except Exception as e:
            elog(e)
            # print(e)
            self.set_status(400)
            self.write(json.dumps({"error": "unable to parse"}))
            self.finish()
        host = data.get("host")

        if not host:
            self.set_status(400)
            self.write(json.dumps({"error": "host not found"}))
            self.finish()

        if not valid(host):
            self.set_status(400)
            self.write(json.dumps({"error": "host not valid"}))
            self.finish()

        if dataCenter.host:
            self.set_status(200)
            self.write(json.dumps({"message": "successfully changed remote platform from %s to %s" % (dataCenter.host, host)}))
            dataCenter.host = host
            dataFeeder.upload_status()
            dataFeeder.upload_temp()
            self.finish()
        else:
            dataCenter.host = host
            self.set_status(200)
            self.write(json.dumps({"message": "successfully set remote platform to %s" % host}))
            print("dataCenter.host:===>", dataCenter.host)
            dataFeeder.upload_status()

            self.finish()

        dataCenter.save_host()
Ejemplo n.º 8
0
    def post(self):
        self.set_header("Content-Type", "application/json")
        try:
            apikey = self.get_argument("apikey")
            apikey = apikey.lower()
        except Exception as e:
            self.write(json.dumps({"err_code": -2}))
            self.finish()
            return

        if not apikey == "f6fdffe48c908deb0f4c3bd36c032e72":
            self.write(json.dumps({"err_code": -1}))
            self.finish()
            return

        try:
            action = self.get_argument("action")
            action = action.lower()
            # print(action)
        except Exception as e:
            self.write(json.dumps({"err_code": -2}))
            self.finish()
            elogger.exception(e)
            return

        if action == "set_status":
            try:
                data = tornado.escape.json_decode(self.request.body)
                print(data)
            except Exception as e:
                elog(e)
                # print(e)
                self.write(json.dumps({"err_code": -3}))
                self.finish()

            error_data, codes_to_execute = dataCenter.parse_setting(data)
            # print("parsing result", results)

            rlog("error_data:%s" % error_data)
            rlog("codes_to_execute:%s" % codes_to_execute)

            self.write(json.dumps(error_data))

            dataFeeder.run_command(codes_to_execute)

            dataCenter.save()

            self.finish()

        elif action == "set_blinkfreq":
            try:
                data = tornado.escape.json_decode(self.request.body)
                print(data)
            except Exception as e:
                self.write(
                    json.dumps({
                        "err_code": -3,
                        "message": "failed to parse input as json"
                    }))
                # self.return()
                elog(e)
                print(e)
                self.finish()

            codes_to_execute, cache_dic, error_data = dataCenter.parse_blink_freq(
                data)
            dataFeeder.blink_freq = cache_dic
            dataCenter.save_blink()
            dataFeeder.run_command(codes_to_execute)
            self.write(json.dumps(error_data))
            self.finish()

        else:
            self.write(json.dumps({"err_code": -4}))
            self.finish()
            return