2016-03-31 16:03:38 +08:00
|
|
|
import requests
|
|
|
|
from flask import abort, session
|
|
|
|
from webViews.log import logger
|
2017-04-07 16:25:57 +08:00
|
|
|
import os,sys,inspect
|
2016-03-31 16:03:38 +08:00
|
|
|
|
2017-02-28 00:19:32 +08:00
|
|
|
|
2017-04-07 16:25:57 +08:00
|
|
|
this_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe ()))[0]))
|
|
|
|
src_folder = os.path.realpath(os.path.abspath(os.path.join(this_folder,"../..", "src")))
|
|
|
|
if src_folder not in sys.path:
|
|
|
|
sys.path.insert(0, src_folder)
|
2016-03-31 16:03:38 +08:00
|
|
|
|
2018-06-11 00:14:41 +08:00
|
|
|
from utils import env
|
2017-04-07 16:25:57 +08:00
|
|
|
|
|
|
|
masterips=env.getenv('MASTER_IPS').split(",")
|
2017-04-09 18:18:12 +08:00
|
|
|
user_endpoint = "http://" + env.getenv('USER_IP') + ":" + str(env.getenv('USER_PORT'))
|
|
|
|
master_port=str(env.getenv('MASTER_PORT'))
|
2016-03-31 16:03:38 +08:00
|
|
|
|
2017-04-07 16:25:57 +08:00
|
|
|
def getip(masterip):
|
|
|
|
return masterip.split("@")[0]
|
|
|
|
|
|
|
|
def getname(masterip):
|
|
|
|
return masterip.split("@")[1]
|
|
|
|
|
2017-05-21 16:08:12 +08:00
|
|
|
|
2016-03-31 16:03:38 +08:00
|
|
|
class dockletRequest():
|
|
|
|
|
|
|
|
@classmethod
|
2017-04-07 16:25:57 +08:00
|
|
|
def post(self, url = '/', data = {}, endpoint = "http://0.0.0.0:9000"):
|
2016-03-31 16:03:38 +08:00
|
|
|
#try:
|
|
|
|
data = dict(data)
|
|
|
|
data['token'] = session['token']
|
|
|
|
logger.info ("Docklet Request: user = %s data = %s, url = %s"%(session['username'], data, url))
|
2016-11-17 14:22:40 +08:00
|
|
|
reqtype = url.split("/")[1]
|
|
|
|
userreq = {
|
|
|
|
'login',
|
2017-04-17 13:22:13 +08:00
|
|
|
'external_login',
|
2016-11-17 14:22:40 +08:00
|
|
|
'register',
|
|
|
|
'user',
|
|
|
|
'beans',
|
2017-04-07 16:25:57 +08:00
|
|
|
'notification',
|
2018-11-09 18:58:46 +08:00
|
|
|
'settings',
|
|
|
|
'bug'
|
2016-11-17 14:22:40 +08:00
|
|
|
}
|
2017-04-07 16:25:57 +08:00
|
|
|
if ":" not in endpoint:
|
2017-04-09 18:18:12 +08:00
|
|
|
endpoint = "http://"+endpoint+":"+master_port
|
2016-11-17 14:22:40 +08:00
|
|
|
if reqtype in userreq:
|
|
|
|
result = requests.post(user_endpoint + url, data=data).json()
|
|
|
|
else:
|
|
|
|
result = requests.post(endpoint + url, data=data).json()
|
2016-06-21 19:05:45 +08:00
|
|
|
# logger.info('response content: %s'%response.content)
|
|
|
|
# result = response.json()
|
2016-04-18 10:28:26 +08:00
|
|
|
if (result.get('success', None) == "false" and result.get('reason', None) == "Unauthorized Action"):
|
|
|
|
abort(401)
|
|
|
|
if (result.get('Unauthorized', None) == 'True'):
|
|
|
|
session['401'] = 'Token Expired'
|
2016-03-31 16:03:38 +08:00
|
|
|
abort(401)
|
2017-02-28 00:19:32 +08:00
|
|
|
logstr = "Docklet Response: user = %s result = %s, url = %s" % (session['username'], result, url)
|
|
|
|
if (sys.getsizeof(logstr) > 512):
|
|
|
|
logstr = "Docklet Response: user = %s, url = %s"%(session['username'], url)
|
|
|
|
logger.info(logstr)
|
2016-03-31 16:03:38 +08:00
|
|
|
return result
|
|
|
|
#except:
|
|
|
|
#abort(500)
|
2018-06-11 00:14:41 +08:00
|
|
|
|
2017-05-21 16:08:12 +08:00
|
|
|
@classmethod
|
|
|
|
def getdesc(self,mastername):
|
|
|
|
return env.getenv(mastername+"_desc")[1:-1]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def getalldesc(self):
|
|
|
|
masterips = self.post_to_all()
|
|
|
|
res={}
|
|
|
|
for masterip in masterips:
|
|
|
|
mastername = getname(masterip)
|
|
|
|
res[mastername]=env.getenv(mastername+"_desc")
|
|
|
|
return res
|
2017-02-28 00:19:32 +08:00
|
|
|
|
2017-04-07 16:25:57 +08:00
|
|
|
@classmethod
|
|
|
|
def post_to_all(self, url = '/', data={}):
|
|
|
|
if (url == '/'):
|
2017-05-08 15:47:34 +08:00
|
|
|
res = []
|
|
|
|
for masterip in masterips:
|
|
|
|
try:
|
|
|
|
requests.post("http://"+getip(masterip)+":"+master_port+"/isalive/",data=data)
|
|
|
|
except Exception as e:
|
|
|
|
logger.debug(e)
|
|
|
|
continue
|
|
|
|
res.append(masterip)
|
|
|
|
return res
|
2017-04-07 16:25:57 +08:00
|
|
|
data = dict(data)
|
|
|
|
data['token'] = session['token']
|
|
|
|
logger.info("Docklet Request: user = %s data = %s, url = %s"%(session['username'], data, url))
|
|
|
|
result = {}
|
|
|
|
for masterip in masterips:
|
2017-04-09 18:18:12 +08:00
|
|
|
try:
|
|
|
|
res = requests.post("http://"+getip(masterip)+":"+master_port+url,data=data).json()
|
|
|
|
except Exception as e:
|
|
|
|
logger.debug(e)
|
|
|
|
continue
|
2018-04-08 15:05:04 +08:00
|
|
|
if 'success' in res and res['success'] == 'true':
|
|
|
|
result[masterip] = res
|
|
|
|
logger.info("get result from %s success" % getip(masterip))
|
|
|
|
else:
|
|
|
|
logger.error("get result from %s failed" % getip(masterip))
|
2017-04-07 16:25:57 +08:00
|
|
|
|
|
|
|
return result
|
2016-03-31 16:03:38 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def unauthorizedpost(self, url = '/', data = None):
|
2016-04-13 14:30:44 +08:00
|
|
|
data = dict(data)
|
|
|
|
data_log = {'user': data.get('user', 'external')}
|
|
|
|
logger.info("Docklet Unauthorized Request: data = %s, url = %s" % (data_log, url))
|
2016-11-17 14:22:40 +08:00
|
|
|
result = requests.post(user_endpoint + url, data = data).json()
|
2016-03-31 16:03:38 +08:00
|
|
|
logger.info("Docklet Unauthorized Response: result = %s, url = %s"%(result, url))
|
|
|
|
return result
|