Commit bbb77cf6 authored by tarak.li's avatar tarak.li

pre上线版本

parent 1a025edb
......@@ -3,6 +3,7 @@
import os
import socket
import sys
import traceback
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'webroot'))
......@@ -26,7 +27,7 @@ class Consul(object):
def register_device(self, name, addr, port: int):
data = {
"ID": "{}".format(name),
"Name": name,
"Name": "infrastructure-micro-teleport-v2",
"Address": addr,
"Port": int(port),
}
......@@ -43,14 +44,19 @@ class Consul(object):
def main():
from app.app_env import PATH_APP_ROOT, PATH_DATA
from app.base.webapp import tp_web_app
_web_app = tp_web_app()
if not _web_app.init(PATH_APP_ROOT, PATH_DATA):
return 1
# from app.base.configs import tp_cfg
# cfg = tp_cfg()
# consul_host = cfg.plugin.consul
# Consul(consul_host).register_device('micro-remote', current_ip(), 7190)
return _web_app.run()
from app.base.logger import log
try:
_web_app = tp_web_app()
if not _web_app.init(PATH_APP_ROOT, PATH_DATA):
return 1
from app.base.configs import tp_cfg
cfg = tp_cfg()
consul_host = cfg.plugin.consul
server_host = cfg.plugin.server
Consul(consul_host).register_device('micro-remote', server_host, 7190)
return _web_app.run()
except:
log.e(str(traceback.format_exc()))
if __name__ == '__main__':
......
......@@ -17,10 +17,9 @@ from tornado.concurrent import run_on_executor
import tornado.web
from app.base.controller import TPBaseHandler
from app.base.logger import *
from app.const import TP_PRIVILEGE_ASSET_CREATE, TPE_PARAM, TPE_OK, TPE_JSON_FORMAT, TP_AUTH_TYPE_PASSWORD, \
TP_AUTH_TYPE_PRIVATE_KEY, TPE_EXISTS, TPE_FAILED, TPE_NOT_EXISTS, TPE_DATABASE
from app.const import *
from app.model import plugin
from app.model.plugin import free_host
from app.model.plugin import free_host, host_id_lock
from app.base.configs import tp_cfg
from pyDes import des, CBC, PAD_PKCS5
import math
......@@ -94,6 +93,9 @@ class TPBasePluginHandler(TPBaseHandler):
self._mode = self.MODE_JSON
self.local_ip = current_ip()
self.outer_ip = network_ip()
cfg = tp_cfg()
release_ip = cfg.plugin.release_ip
self.release_ip = release_ip
async def prepare(self):
# req = requests.get("http://txt.go.sohu.com/ip/soip")
......@@ -133,10 +135,11 @@ class TPBasePluginHandler(TPBaseHandler):
def control_ip(self, ip):
items = ip.split(".")[:2]
if ip == '127.0.0.1' or items == self.outer_ip.split(".")[:2] or items == self.local_ip.split(".")[:2]:
if ip == '127.0.0.1' or ip in self.release_ip \
or items == self.outer_ip.split(".")[:2] or items == self.local_ip.split(".")[:2]:
return True
else:
if self.query("acc", "id", {"ip": ip}):
if self.query("acc", "id", {"host_ip": ip}):
return True
return False
......@@ -156,7 +159,7 @@ class TPBasePluginHandler(TPBaseHandler):
return [props.get(i) or default for i in fields]
@run_on_executor
def request_api(self, url, data=None, json=None):
def request_api(self, url, data=None, json=None, method="post"):
_user = {'id': 1, 'type': 1, 'auth_type': 2, 'username': 'admin', 'surname': 'admin', 'ldap_dn': '',
'role_id': 1,
'state': 1, 'fail_count': 0, 'lock_time': 0, 'email': '929749555@qq.com', 'create_time': 1583104533,
......@@ -166,7 +169,10 @@ class TPBasePluginHandler(TPBaseHandler):
self._s_id = "tp_1583808860_5c0a3c718114f429"
self.set_session('user', _user, 12 * 60 * 60)
cookies = {"_sid": self._s_id, "username": "admin"}
resp = requests.post(url, data=data, json=json, cookies=cookies)
if method == 'put':
resp = requests.put(url, data=data, json=json, cookies=cookies)
else:
resp = requests.post(url, data=data, json=json, cookies=cookies)
return resp.json()
def query(self, table, field, filter):
......@@ -304,7 +310,8 @@ class UpdateHostHandler(TPBasePluginHandler):
self.finish_json(1003, "修改主机异常")
return
acc_id = self.query("acc", "id", {"host_id": host_id, "username": username})
old_username = self.query("remote_host", "username", {"id": host_id})
acc_id = self.query("acc", "id", {"host_id": host_id, "username": old_username})
if not acc_id:
self.finish_json(1011, "未发现该服务器信息")
......@@ -399,7 +406,7 @@ class GetHostInfoHandler(TPBasePluginHandler):
try:
host_id = int(host_id)
except:
self.finish_json(1001, "参数传递异常,请检查请求参数")
self.finish_json(1001, "提交参数异常,请检查请求参数")
return
err, host_info = plugin.get_account_info(host_id=host_id, mch_no=mch_no)
......@@ -439,11 +446,11 @@ class GetSessionInfoHandler(TPBasePluginHandler):
try:
host_id = int(host_id)
except:
self.finish_json(1001, "参数传递异常,请检查请求参数")
self.finish_json(1001, "提交参数异常,请检查请求参数")
return
if not host_id and not mch_no and not (ip and username and password):
self.finish_json(1001, "缺少必要参数异常")
self.finish_json(1001, "缺少必填项")
return
if ip and username and password:
......@@ -542,7 +549,7 @@ class BindPayAccountHandler(TPBasePluginHandler):
comp_id = int(comp_id)
biz_id = int(biz_id)
except:
self.finish_json(1001, "参数传递异常,请检查请求参数")
self.finish_json(1001, "提交参数异常,请检查请求参数")
return
ip, username, host_password, account, password, mch_no \
......@@ -551,12 +558,11 @@ class BindPayAccountHandler(TPBasePluginHandler):
host_password = host_password.replace("*", "")
if not biz_id or not mch_no or not comp_id or not account:
self.finish_json(1001, "缺少必要参数异常:biz_id,mch_no")
self.finish_json(1001, "缺少必填项")
return
# not host_id and not host_assigned and (not ip or not username or not host_password)
if not host_id and not host_assigned and (not ip or not username):
self.finish_json(1001, "手动分配,缺少必要参数异常:ip,username,password")
self.finish_json(1001, "手动分配,缺少必填项")
return
if not host_id:
......@@ -572,12 +578,19 @@ class BindPayAccountHandler(TPBasePluginHandler):
self.finish_json(1010, "未发现空闲主机")
return
else:
is_insert = False
if host_password:
data = {"ip": ip, "os_type": 1, "username": username, "password": host_password}
url = "http://127.0.0.1:7190/plugin/update_host"
resp = await self.request_api(url, json=data)
host_id = resp.get("data", 0)
is_insert = True if host_id else False
host_id = host_id or self.query("host", 'id', {"ip": ip})
if not is_insert:
data = {"ip": ip, "os_type": 1, "username": username, "password": host_password, "host_id": host_id}
url = "http://127.0.0.1:7190/plugin/update_host"
resp = await self.request_api(url, json=data, method="put")
# host_id = resp.get("data", 0)
if not host_id:
self.finish_json(1010, "无法找到对应主机信息")
......@@ -588,9 +601,9 @@ class BindPayAccountHandler(TPBasePluginHandler):
id = self.query("remote_account_host_bind", "id", {"host_id": host_id, "mch_no": mch_no})
password = des_encrypt(password).decode()
des_password = des_encrypt(password).decode()
args = {"mch_no": mch_no, "comp_id": comp_id, "host_id": host_id, "host_assigned": host_assigned,
"account_source": 1, "account": account, "password": password, "login_status": 0, "mch_name": "",
"account_source": 1, "account": account, "password": des_password, "login_status": 0, "mch_name": "",
"biz_id": biz_id, "ip": ip, "host_password": host_password, "username": username}
log.i("id:{}".format(id))
......@@ -599,6 +612,7 @@ class BindPayAccountHandler(TPBasePluginHandler):
old_account = self.query('remote_account_host_bind', "account", {"id": id})
if account != old_account:
self.finish_json(1011, "不允许修改绑定账户")
host_id_lock.pop(host_id, None)
return
err = plugin.update_account_host_bind(self, args)
......@@ -608,13 +622,15 @@ class BindPayAccountHandler(TPBasePluginHandler):
self.finish_json(1012, "设备不存在")
elif err == TPE_OK:
self.finish_json(0)
host_id_lock.pop(host_id, None)
return
if not password:
host_id_lock.pop(host_id, None)
self.finish_json(1001, "缺少必要参数异常:password")
return
args['password'] = password
args['password'] = des_password
err, info = plugin.add_account_host_bind(self, args)
if err == TPE_EXISTS:
......@@ -628,6 +644,7 @@ class BindPayAccountHandler(TPBasePluginHandler):
else:
self.finish_json(0)
host_id_lock.pop(host_id, None)
if err != TPE_OK:
return
......
......@@ -752,7 +752,7 @@ class DoResetPasswordHandler(TPBaseJsonHandler):
err, msg = yield mail.tp_send_mail(
email,
'Teleport用户,您好!\n\n请访问以下链接以重设您的micro-remote登录密码。此链接将于本邮件寄出24小时之后失效。\n'
'micro-remote用户,您好!\n\n请访问以下链接以重设您的micro-remote登录密码。此链接将于本邮件寄出24小时之后失效。\n'
'访问此链接,将会为您打开密码重置页面,然后您可以设定新密码。\n\n'
'如果您并没有做重设密码的操作,请忽略本邮件,请及时联系您的系统管理员!\n\n'
'{reset_url}\n\n\n\n'
......
# coding: utf-8
import datetime
from app.base.logger import log
from app.base.db import get_db, SQL
from app.base.utils import tp_timestamp_utc_now
from app.const import TPE_EXISTS, TPE_DATABASE, TPE_OK, TP_GROUP_HOST, TPE_NOT_EXISTS, TPE_FAILED
host_id_lock = {}
def tp_timestamp_utc_now():
return int(datetime.datetime.now().timestamp())
def get_host_list(sql_limit, os_type, ip, search, status):
# 仿 host.get_hosts get_hosts
......@@ -44,7 +51,7 @@ def get_bind_info(host_id):
s = SQL(get_db())
s.select_from('remote_account_host_bind',
['id', 'mch_no', 'comp_id', 'host_id', 'host_assigned', 'account_source', 'account',
'login_status', 'mch_name', 'create_tiem'], alt_name='a')
'login_status', 'mch_name', 'create_time'], alt_name='a')
# 判断
if host_id:
......@@ -63,7 +70,7 @@ def get_account_info(host_id, mch_no):
s = SQL(get_db())
s.select_from('remote_account_host_bind',
['id', 'mch_no', 'comp_id', 'host_id', 'host_assigned', 'account_source', 'account',
'login_status', 'mch_name', 'create_tiem'], alt_name='a')
'login_status', 'mch_name', 'create_time'], alt_name='a')
# 判断
if mch_no:
......@@ -111,11 +118,16 @@ def free_host():
from tp_remote_host a LEFT join tp_remote_account_host_bind b on a.id = b.host_id
where b.id is null"""
if host_id_lock:
not_id = ' and a.id not in ({})'.format(','.join([str(id) for id in host_id_lock.keys()]))
sql = sql + not_id
db_ret = db.query(sql)
if not db_ret and len(db_ret) == 0:
return TPE_NOT_EXISTS, 0
return TPE_OK, db_ret[0][0]
host_id = db_ret[0][0]
host_id_lock[host_id] = 1
return TPE_OK, host_id
def add_remote_host(handler, args):
......@@ -135,7 +147,7 @@ def add_remote_host(handler, args):
os_type=args.get("os_type", 0),
ip=args.get("ip", ""), status=args.get("status", 0), username=args.get("username", ""),
password=args.get("password", ""),
name=args.get("name", ""), remark=args.get("desc", ""), create_time=_time_now,
name=args.get("name", ""), remark=args.get("remark", ""), create_time=_time_now,
create_by=operator['id'],
update_time=_time_now, update_by=operator['id'], )
......@@ -214,7 +226,7 @@ def add_account_host_bind(handler, args):
if db_ret and (comp_id,) not in db_ret:
return TPE_FAILED, 0
sql = 'INSERT INTO `tp_remote_account_host_bind` (biz_id,mch_no, comp_id, host_id, host_assigned, account_source, account, password, login_status, mch_name, create_tiem, create_by, update_time, update_by) VALUES ' \
sql = 'INSERT INTO `tp_remote_account_host_bind` (biz_id,mch_no, comp_id, host_id, host_assigned, account_source, account, password, login_status, mch_name, create_time, create_by, update_time, update_by) VALUES ' \
'({biz_id},"{mch_no}", {comp_id}, {host_id}, {host_assigned}, {account_source}, "{account}", "{password}", {login_status},"{mch_name}", "{create_time}", "{create_by}", "{update_time}", "{update_by}");' \
''.format(mch_no=args.get("mch_no", ""), comp_id=args.get("comp_id", 0), host_id=args.get("host_id", 0),
host_assigned=args.get("host_assigned", 0), account_source=args.get("account_source", 1),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment