def desktop_close_notice(self): try: resp = errcode.get_error_result() cmd_msg = CommandMsg() cmd_msg.cmdstr = 'vm_shutdown' cmd_msg.BodyType = CommandBodyType.TEXT data = self.task.get("data") data['desktop_name'] = data["instance_name"] data['dsk_uuid'] = data["instance_uuid"] data.pop("instance_name") data.pop("instance_uuid") data['dsk_type'] = "kvm" data['status'] = 0 terminal_mac = data['terminal_mac'] data.pop("terminal_mac") body = str(json.dumps(data).encode('utf-8'), encoding='utf-8') cmd_msg.Body = "Command:{}|{}".format(cmd_msg.cmdstr, body) if terminal_mac: table_terminal_api = db_api.YzyTerminalTableCtrl( current_app.db) qry_terminal = table_terminal_api.select_terminal_by_mac( terminal_mac) if qry_terminal and qry_terminal.status == '1': self.send_thrift_cmd(terminal_mac, cmd_msg) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def client_closed(self, oprot, app): client_md5 = self.get_md5(str(oprot)) self.token_client_tmp.pop(client_md5) if client_md5 in self.token_client.keys(): self.token_client.pop(client_md5) logging.debug('pop token_client mac {}'.format(client_md5)) token_mac = {v: k for k, v in self.mac_token.items()} logging.debug('mac_token = {}, token_mac = {}'.format( self.mac_token, token_mac)) if client_md5 in token_mac.keys(): mac = token_mac[client_md5] self.mac_token.pop(mac) logging.debug('pop mac_token mac {}'.format(mac)) if client_md5 in self.token_status.keys(): self.token_status.pop(client_md5) logging.debug( 'pop token_status token {}'.format(client_md5)) # set terminal status to offline user_name = None try: with app.app_context(): table_api = db_api.YzyTerminalTableCtrl(current_app.db) table_api.update_terminal_by_mac(**{ 'mac': mac, 'status': '0' }) logging.debug( 'set yzy_terminal offline {}'.format(mac)) except Exception as err: logging.error( 'delete user desktop records err mac: {}, user: {}'. format(mac, user_name)) logging.error(err) logging.error(''.join(traceback.format_exc()))
def modify_terminal_name(self): try: resp = errcode.get_error_result() cmd_msg = CommandMsg() cmd_msg.cmdstr = 'update_config' cmd_msg.BodyType = CommandBodyType.TEXT cmd_msg.Body = "Command:%s" % cmd_msg.cmdstr data = self.task.get("data") logging.debug(data) # yzy_terminal update for mac in data.keys(): table_api = db_api.YzyTerminalTableCtrl(current_app.db) qry_terminal = table_api.select_terminal_by_mac(mac) if qry_terminal: terminal_values = { 'mac': mac, 'conf_version': str(int(qry_terminal.conf_version) + 1), 'name': data[mac] } table_api.update_terminal_by_mac(**terminal_values) if qry_terminal.status == '1': self.send_thrift_cmd(mac, cmd_msg) else: logging.warning( 'mac not found in yzy_terminal {}'.format(mac)) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def delete_group(self): try: resp = errcode.get_error_result() group_uuid = self.task.get("data").get("group_uuid") # yzy_terminal update table_api = db_api.YzyTerminalTableCtrl(current_app.db) table_api.reset_group_uuid(group_uuid) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def delete(self): """ just delete database tables records just for not online terminal :return: """ try: resp = errcode.get_error_result() mac_list = self.task.get("data").get("mac_list").split(',') logging.debug( 'Will exec delete macs tables records {}'.format(mac_list)) table_api = db_api.YzyTerminalTableCtrl(current_app.db) for mac in mac_list: table_api.delete_terminal_by_mac(mac) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def change_group(self): try: resp = errcode.get_error_result() mac_list = self.task.get("data").get("mac_list").split(',') to_group_uuid = self.task.get("data").get("to_group_uuid") # yzy_terminal update for mac in mac_list: table_api = db_api.YzyTerminalTableCtrl(current_app.db) qry_terminal = table_api.select_terminal_by_mac(mac) if qry_terminal: terminal_values = {'mac': mac, 'group_uuid': to_group_uuid} table_api.update_terminal_by_mac(**terminal_values) else: logging.debug( 'mac not found in yzy_terminal {}'.format(mac)) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def do_status_check(app): # 1. iterate mac_token: # 1.1 if token_status exists # 1.1.1 if status is False: # update yzy_terminal status to '0' # close client # pop mac_token, token_client, token_status # 1.1.2 if status is True: # reset status to False(terminal call Ping to set True every 10 seconds) # 1.2 if token_status not exists, print error # set loop_seconds must > 10 loop_seconds = 30 while True: try: with app.app_context(): table_api = db_api.YzyTerminalTableCtrl(current_app.db) token_client = app.token_client mac_token = app.mac_token token_status = app.token_status logging.debug( 'token_client {}, mac_token {}, token_status {}'.format( token_client, mac_token, token_status)) mac_token_keys = list(mac_token.keys()) token_client_keys = list(token_client.keys()) # update database data not in mac_token all_online_terminals = table_api.select_all_online_terminal() all_online_terminals = [ qry.mac for qry in all_online_terminals if qry.mac ] except_terminals = [ mac for mac in all_online_terminals if mac not in mac_token_keys ] if except_terminals: logging.debug( "database except terminals: {}, set offline".format( except_terminals)) table_api.update_terminal_by_macs( except_terminals, db_api.TerminalStatus.OFFLINE) for mac in mac_token_keys: token_id = mac_token[mac] if token_id in token_status.keys(): if token_status[token_id]: token_status[token_id] = False else: mac_token.pop(mac) token_status.pop(token_id) logging.debug( 'pop mac_token, pop token_status {}'.format( mac)) if token_id in token_client_keys: token_client[token_id].trans.close() token_client.pop(token_id) logging.debug( 'close client, pop token_client {}'.format( mac)) else: logging.error( 'token_id no key in token_client !!!') qry = table_api.select_terminal_by_mac(mac) if qry: table_api.update_terminal_by_mac(**{ 'mac': mac, 'status': '0' }) logging.debug( 'set yzy_terminal offline {}'.format(mac)) else: logging.error( 'mac not in yzy_terminal {} !!!'.format( mac)) else: logging.error('token_id no key in token_status !!!') except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) time.sleep(loop_seconds)
def set_terminal_offline(app): with app.app_context(): table_api = db_api.YzyTerminalTableCtrl(current_app.db) table_api.reset_all_terminal_offline()
def set_terminal(self): try: resp = errcode.get_error_result() cmd_msg = CommandMsg() cmd_msg.cmdstr = 'update_config' cmd_msg.BodyType = CommandBodyType.TEXT cmd_msg.Body = "Command:%s" % cmd_msg.cmdstr logging.debug('get data {}'.format(self.task.get("data"))) mac_list = self.task.get("data").get("mac_list").split(',') show_desktop_type = self.task.get("data").get("mode").get( "show_desktop_type") auto_desktop = self.task.get("data").get("mode").get( "auto_desktop") open_strategy = self.task.get("data").get("mode").get( "open_strategy") close_desktop_strategy = self.task.get("data").get("mode").get( "close_desktop_strategy") close_terminal_strategy = self.task.get("data").get("mode").get( "close_terminal_strategy") current_screen_info = self.task.get("data").get("program").get( "current_screen_info") server_ip = self.task.get("data").get("program").get("server_ip") show_modify_user_passwd = self.task.get("data").get("program").get( "show_modify_user_passwd") terminal_setup_passwd = self.task.get("data").get("program").get( "terminal_setup_passwd") window_mode = self.task.get("data").get("windows").get( "window_mode") goto_local_desktop = self.task.get("data").get("windows").get( "disconnect_setup").get("goto_local_desktop") goto_local_auth = self.task.get("data").get("windows").get( "disconnect_setup").get("goto_local_auth") show_local_button = self.task.get("data").get("windows").get( "show").get("show_local_button") goto_local_passwd = self.task.get("data").get("windows").get( "show").get("goto_local_passwd") hide_tools = self.task.get("data").get("program").get("hide_tools") top_level_service_ip = self.task.get("data").get("teaching").get( "top_level_service_ip") teacher_service_ip = self.task.get("data").get("teaching").get( "teacher_service_ip") classroom_num = self.task.get("data").get("teaching").get( "classroom_num") multicast_ip = self.task.get("data").get("teaching").get( "multicast_ip") multicast_port = self.task.get("data").get("teaching").get( "multicast_port") set_mode_info = { 'show_desktop_type': show_desktop_type, 'auto_desktop': auto_desktop, 'open_strategy': open_strategy, 'close_desktop_strategy': close_desktop_strategy, 'close_terminal_strategy': close_terminal_strategy } set_windows_info = { 'window_mode': window_mode, 'disconnect_setup': { 'goto_local_desktop': goto_local_desktop, 'goto_local_auth': goto_local_auth }, 'show': { 'show_local_button': show_local_button, 'goto_local_passwd': goto_local_passwd } } set_teaching_info = { 'top_level_service_ip': top_level_service_ip, 'teacher_service_ip': teacher_service_ip, 'classroom_num': classroom_num, 'multicast_ip': multicast_ip, 'multicast_port': multicast_port } # yzy_terminal update setup_conf for mac in mac_list: table_api = db_api.YzyTerminalTableCtrl(current_app.db) qry_terminal = table_api.select_terminal_by_mac(mac) if qry_terminal: setup_info = json.loads(qry_terminal.setup_info) logging.debug(setup_info) setup_info['mode'] = set_mode_info set_program_info = { 'server_ip': server_ip, 'server_port': 9999, 'show_modify_user_passwd': show_modify_user_passwd, 'terminal_setup_passwd': terminal_setup_passwd, 'hide_tools': hide_tools, 'current_screen_info': { 'width': int(current_screen_info.split('*')[0]), 'height': int(current_screen_info.split('*')[1]) }, 'screen_info_list': setup_info['program']['screen_info_list'] } setup_info['program'] = set_program_info setup_info['windows'] = set_windows_info setup_info['teaching'] = set_teaching_info terminal_values = { 'mac': qry_terminal.mac, 'conf_version': str(int(qry_terminal.conf_version) + 1), 'setup_info': json.dumps(setup_info) } table_api.update_terminal_by_mac(**terminal_values) if qry_terminal.status == '1': self.send_thrift_cmd(mac, cmd_msg) else: logging.error( 'mac not found in yzy_terminal {}'.format(mac)) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def modify_ip(self): try: resp = errcode.get_error_result() cmd_msg = CommandMsg() cmd_msg.cmdstr = 'update_ip' cmd_msg.BodyType = CommandBodyType.TEXT # Use dhcp config modify_ip_method = self.task.get("data").get("modify_ip_method") if modify_ip_method == "dhcp": group_uuid = self.task.get("data").get('group_uuid') table_api = db_api.YzyTerminalTableCtrl(current_app.db) terminals = table_api.select_terminal_by_group_uuid(group_uuid) ip_info = { "IsDhcp": 1, } for term in terminals: values = { 'mac': term.mac, 'conf_version': str(int(term.conf_version) + 1), 'is_dhcp': 1 } table_api.update_terminal_by_mac(**values) if term.status == "1": ip_info["Mac"] = term.mac body = str(json.dumps(ip_info).encode('utf-8'), encoding='utf-8') cmd_msg.Body = "Command:{}|{}".format( cmd_msg.cmdstr, body) logging.debug(cmd_msg) self.send_thrift_cmd(mac, cmd_msg) logging.debug("Modify group's %s terminals to dhcp success." % group_uuid) return resp mac_list = self.task.get("data").get("mac_list").split(',') ip_list = self.task.get("data").get("to_ip_list").split(',') gateway = self.task.get("data").get("gateway") mask = self.task.get("data").get("mask") dns1 = self.task.get("data").get("dns1", "") dns2 = self.task.get("data").get("dns2", "") if not (is_ip_addr(gateway) and is_netmask(mask)[0] and len(mac_list) == len(ip_list)): logging.error( "param error, gateway {}, mask {}, dns1 {}, dns2 {}". format(gateway, mask, dns1, dns2)) return errcode.get_error_result("RequestParamError") ip_info = { "IsDhcp": 0, "Ip": "", "Subnet": mask, "Gateway": gateway, "Mac": "", "DNS1": dns1, "DNS2": dns2, } for mac in mac_list: table_api = db_api.YzyTerminalTableCtrl(current_app.db) qry_terminal = table_api.select_terminal_by_mac(mac) ip = ip_list[mac_list.index(mac)] ip_info["Ip"] = ip ip_info["Mac"] = mac body = str(json.dumps(ip_info).encode('utf-8'), encoding='utf-8') cmd_msg.Body = "Command:{}|{}".format(cmd_msg.cmdstr, body) logging.debug(cmd_msg) if qry_terminal: terminal_values = { 'mac': mac, 'conf_version': str(int(qry_terminal.conf_version) + 1), 'ip': ip, 'mask': ip_info['Subnet'], 'gateway': ip_info['Gateway'], 'is_dhcp': int(ip_info['IsDhcp']), 'dns1': ip_info['DNS1'], 'dns2': ip_info['DNS2'] } table_api.update_terminal_by_mac(**terminal_values) if qry_terminal.status == '1': self.send_thrift_cmd(mac, cmd_msg) else: logging.warning( 'mac not found in yzy_terminal {}'.format(mac)) return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") return resp
def terminal_order(self): logging.debug('{}.{} be called'.format(self.__class__.__name__, sys._getframe().f_code.co_name)) try: resp = errcode.get_error_result() group_uuid = self.task.get("data").get("group_uuid") start_id = self.task.get("data").get("start_num") cmd_msg = CommandMsg() cmd_msg.cmdstr = 'order' cmd_msg.BodyType = CommandBodyType.TEXT cmd_msg.Body = "Command:%s" % cmd_msg.cmdstr cmd_msg.ArgsDic = {'terminal_id': str(start_id)} # select all records from yzy_terminal use group_uuid table_api = db_api.YzyTerminalTableCtrl(current_app.db) qrys = table_api.select_terminal_by_group_uuid(group_uuid) mac_list = [qry.mac for qry in qrys] if not len(mac_list): logging.error("param error, group_uuid {}".format(group_uuid)) return errcode.get_error_result("RequestParamError") insert_data = { 'order_macs': ','.join(mac_list), 'start_id': start_id, 'current_id': start_id, 'confirm_macs': "", 'confirm_ids': "", 'datetime': dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } # session insert redis if self.rds.ping_server(): # delele all group_uuid order seesions match_key = 'command:order:{}:*'.format(group_uuid) key_names = self.rds.keys(match_key) for key_name in key_names: logging.debug("delete old key: {}".format(key_name)) self.rds.delete(key_name) cmd_msg.batch_num = self.rds.incr("cmd_batch_num") order_key = 'command:order:{}:{}'.format( group_uuid, cmd_msg.batch_num) self.rds.set(order_key, json.dumps(insert_data)) self.rds.expire(order_key, self.rds.live_seconds) else: logging.error('Redis server error') resp = errcode.get_error_result(error="RedisServerError") resp['data'] = {} resp['data']['batch_num'] = cmd_msg.batch_num return resp for mac in mac_list: # redis add a record to save order session for order_confirm self.send_thrift_cmd(mac, cmd_msg) resp['data'] = {} resp['data']['batch_num'] = cmd_msg.batch_num return resp except Exception as err: logging.error(err) logging.error(''.join(traceback.format_exc())) resp = errcode.get_error_result(error="OtherError") resp['data'] = {} resp['data']['batch_num'] = cmd_msg.batch_num return resp