|
@ -7,6 +7,7 @@ import time |
|
|
import sys |
|
|
import sys |
|
|
from server_pool import ServerPool |
|
|
from server_pool import ServerPool |
|
|
import Config |
|
|
import Config |
|
|
|
|
|
import traceback |
|
|
|
|
|
|
|
|
class DbTransfer(object): |
|
|
class DbTransfer(object): |
|
|
|
|
|
|
|
@ -75,13 +76,17 @@ class DbTransfer(object): |
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def pull_db_all_user(): |
|
|
def pull_db_all_user(): |
|
|
#数据库所有用户信息 |
|
|
#数据库所有用户信息 |
|
|
|
|
|
keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'switch', 'enable', 'plan' ] |
|
|
conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, |
|
|
conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, |
|
|
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') |
|
|
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') |
|
|
cur = conn.cursor() |
|
|
cur = conn.cursor() |
|
|
cur.execute("SELECT port, u, d, transfer_enable, passwd, switch, enable, plan FROM user") |
|
|
cur.execute("SELECT " + ','.join(keys) + " FROM user") |
|
|
rows = [] |
|
|
rows = [] |
|
|
for r in cur.fetchall(): |
|
|
for r in cur.fetchall(): |
|
|
rows.append(list(r)) |
|
|
d = {} |
|
|
|
|
|
for column in xrange(len(keys)): |
|
|
|
|
|
d[keys[column]] = r[column] |
|
|
|
|
|
rows.append(d) |
|
|
cur.close() |
|
|
cur.close() |
|
|
conn.close() |
|
|
conn.close() |
|
|
return rows |
|
|
return rows |
|
@ -95,33 +100,34 @@ class DbTransfer(object): |
|
|
for row in rows: |
|
|
for row in rows: |
|
|
try: |
|
|
try: |
|
|
import switchrule |
|
|
import switchrule |
|
|
allow = switchrule.isTurnOn(row[7], row[5]) and row[6] == 1 and row[1] + row[2] < row[3] |
|
|
allow = switchrule.isTurnOn(row) and row['enable'] == 1 and row['u'] + row['d'] < row['transfer_enable'] |
|
|
except Exception, e: |
|
|
except Exception, e: |
|
|
allow = False |
|
|
allow = False |
|
|
|
|
|
|
|
|
cur_servers[row[0]] = row[4] |
|
|
port = row['port'] |
|
|
|
|
|
passwd = row['passwd'] |
|
|
|
|
|
cur_servers[port] = passwd |
|
|
|
|
|
|
|
|
if ServerPool.get_instance().server_is_run(row[0]) > 0: |
|
|
if ServerPool.get_instance().server_is_run(port) > 0: |
|
|
if not allow: |
|
|
if not allow: |
|
|
logging.info('db stop server at port [%s]' % (row[0])) |
|
|
logging.info('db stop server at port [%s]' % (port,)) |
|
|
ServerPool.get_instance().del_server(row[0]) |
|
|
ServerPool.get_instance().del_server(port) |
|
|
elif (row[0] in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[row[0]]._config['password'] != row[4]) \ |
|
|
elif (port in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[port]._config['password'] != passwd) \ |
|
|
or (row[0] in ServerPool.get_instance().tcp_ipv6_servers_pool and ServerPool.get_instance().tcp_ipv6_servers_pool[row[0]]._config['password'] != row[4]): |
|
|
or (port in ServerPool.get_instance().tcp_ipv6_servers_pool and ServerPool.get_instance().tcp_ipv6_servers_pool[port]._config['password'] != passwd): |
|
|
#password changed |
|
|
#password changed |
|
|
logging.info('db stop server at port [%s] reason: password changed' % (row[0])) |
|
|
logging.info('db stop server at port [%s] reason: password changed' % (port,)) |
|
|
ServerPool.get_instance().del_server(row[0]) |
|
|
ServerPool.get_instance().del_server(port) |
|
|
elif ServerPool.get_instance().server_run_status(row[0]) is False: |
|
|
|
|
|
if allow: |
|
|
if allow and ServerPool.get_instance().server_is_run(port) == 0: |
|
|
logging.info('db start server at port [%s] pass [%s]' % (row[0], row[4])) |
|
|
logging.info('db start server at port [%s] pass [%s]' % (port, passwd)) |
|
|
ServerPool.get_instance().new_server(row[0], row[4]) |
|
|
ServerPool.get_instance().new_server(port, passwd) |
|
|
|
|
|
|
|
|
for row in last_rows: |
|
|
for row in last_rows: |
|
|
if row[0] in cur_servers: |
|
|
if row['port'] in cur_servers: |
|
|
if row[4] == cur_servers[row[0]]: |
|
|
|
|
|
pass |
|
|
pass |
|
|
else: |
|
|
else: |
|
|
logging.info('db stop server at port [%s] reason: port not exist' % (row[0])) |
|
|
logging.info('db stop server at port [%s] reason: port not exist' % (row['port'])) |
|
|
ServerPool.get_instance().del_server(row[0]) |
|
|
ServerPool.get_instance().del_server(row['port']) |
|
|
|
|
|
|
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def thread_db(): |
|
|
def thread_db(): |
|
@ -131,18 +137,15 @@ class DbTransfer(object): |
|
|
socket.setdefaulttimeout(timeout) |
|
|
socket.setdefaulttimeout(timeout) |
|
|
last_rows = [] |
|
|
last_rows = [] |
|
|
while True: |
|
|
while True: |
|
|
#logging.warn('db loop') |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
DbTransfer.get_instance().push_db_all_user() |
|
|
DbTransfer.get_instance().push_db_all_user() |
|
|
rows = DbTransfer.get_instance().pull_db_all_user() |
|
|
rows = DbTransfer.get_instance().pull_db_all_user() |
|
|
DbTransfer.del_server_out_of_bound_safe(last_rows, rows) |
|
|
DbTransfer.del_server_out_of_bound_safe(last_rows, rows) |
|
|
last_rows = rows |
|
|
last_rows = rows |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn('db thread except:%s' % e) |
|
|
trace = traceback.format_exc() |
|
|
|
|
|
logging.error(trace) |
|
|
|
|
|
#logging.warn('db thread except:%s' % e) |
|
|
finally: |
|
|
finally: |
|
|
time.sleep(15) |
|
|
time.sleep(15) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#SQLData.pull_db_all_user() |
|
|
|
|
|
#print DbTransfer.get_instance().test() |
|
|
|
|
|