def load_setting(config): # ok """ 返回 .setting 中的配置 参数 1. config: 具体配置 返回值示例 dicts = { "subnet": "192.168.0.0/18", "self_ip": "192.168.12.1", "iface": "eth0", "gateway": "192.168.7.1" } """ dicts = { "code": 1, "msg": "", "result": [] } try: with open(".setting", "r") as fp: dicts["result"] = json.load(fp)[config] dicts["code"] = 0 except Exception, e: pt.log(traceback.format_exc(), level="error", description="load setting: %s failed!" % (config), path=".master_log") dicts["msg"] = str(e)
def ip_ls(subnet): # ok """ 返回可用的 ip 列表 1. subnet: ip 所处的网段 返回值示例 dicts = { "code": 0, "msg": "", "result": [ "ip1", "ip2", "ip3", ] } """ dicts = {"code": 1, "msg": "", "result": []} used_ip = json.loads(ip_used(subnet)) if used_ip["code"]: dicts["msg"] = used_ip["msg"] else: try: ips = [ip.strNormal() for ip in IPy.IP(subnet)] dicts["result"] = list(set(ips) - set(used_ip["result"])) dicts["code"] = 0 except Exception, e: pt.log(traceback.format_exc(), level="error", description="the subnet must be in standard form.", path=".master_log") dicts["msg"] = " (%s)" % str(e)
def ip_assign(subnet, ip=0): # ok """ 返回一个可用的 ip 1. subnet: ip 所处的网段 2. ip: 是否随机选择; 可选; 若不为0,则必须为一个 ip 地址, 为 0 时由系统随机选择 返回值示例 dicts = { "code": 0, "msg": "", "result": "192.168.12.31" } """ dicts = { "code": 1, "msg": "", "result": "" } result = json.loads(ip_ls(subnet)) if result["code"]: dicts["msg"] = result["msg"] else: ip_unused = result["result"] if ip == 0: # 由系统选择 if ip_unused: dicts["result"] = random.choice(ip_unused) dicts["code"] = 0 else: dicts["msg"] = "无剩余 ip 地址" else: if ip not in ip_unused: dicts["msg"] = "ip 地址已被占用" else: try: if ip in IPy.IP(subnet): dicts["result"] = ip dicts["code"] = 0 else: dicts["msg"] = "ip 不在网段 %s 内" % subnet except Exception, e: pt.log(traceback.format_exc(), level="error", description="this ip is illegal.", path=".master_log") dicts["msg"] = "ip 地址非法"
def command2slave(ip, mission, port=1100, timeout=60): # ok """ 对 slave 指派任务 参数 1. ip: slave 的 ip 2. mission: 具体任务, 格式如下: { "mission": "", # 具体的任务 "commands": { "command": "", # 具体的命令 "arg": [], # 参数列表 } } 3. port: 与 slave 的通信端口; 可选; 默认为 1100 4. timeout: 等待 slave 返回的超时时间; 可选; 默认为 60s 返回值示例 dicts = { "code": 0, "msg": "", "result": "" # 与 slave 返回的值一致 } """ dicts = { "code": 1, "msg": "", "result": "" } socket.setdefaulttimeout(timeout) sk = socket.socket() for i in range(3): try: sk.connect((ip, port)) break except Exception, e: if i == 2: pt.log(traceback.format_exc(), level="error", description="connect to slave: %s:%s failed" % (ip, port), path=".master_log") dicts["msg"] = "connect to slave: %s:%s failed" % (ip, port) return json.dumps(dicts)
for i in range(3): try: sk.connect((ip, port)) break except Exception, e: if i == 2: pt.log(traceback.format_exc(), level="error", description="connect to slave: %s:%s failed" % (ip, port), path=".master_log") dicts["msg"] = "connect to slave: %s:%s failed" % (ip, port) return json.dumps(dicts) try: sk.sendall("xxxxxxxxxx") # 认证 key server_reply = sk.recv(1024) if server_reply == "hello, my master": # 认证成功 sk.sendall(mission) dicts = json.loads(sk.recv(1024000)) sk.close() else: # 认证失败 dicts["msg"] = "sign in failed" except Exception, e: pt.log(traceback.format_exc(), level="error", description="send a mission to slave(%s) failed" % (ip), path=".master_log") dicts["msg"] = "send a mission to slave(%s) failed: %s" % (ip, str(e)) return json.dumps(dicts)