def write_cookie(self): """写入cookie文件""" php_session_id = self.get_php_session_id() token = self.get_token() data = { 'PHPSESSID': php_session_id, 'token': token } file = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'data/cookie.json') # 写入cookie.json中 JsonUtil.write(data=data, file=file)
def standard_response(code, data): '''standard response ''' rst = {} rst['code'] = code rst['data'] = data return JsonUtil.object_2_json(rst)
def updateStatus(self, status): self.status = status self.lastUpdate = datetime.datetime.now() self.save() socketio.emit('webhook', JsonUtil.object_2_json(self.dict()), room=self.id)
def do_webhook_shell(webhook_id, histroy_id, data): webhook = WebHook.query.get(webhook_id) history = History.query.get(histroy_id) # server information ip = webhook.server.ip port = webhook.server.port account = webhook.server.account pkey = webhook.server.pkey # do what shell = webhook.shell # start to process, add history into database status = '2' history.updateStatus(status) webhook.updateStatus(status) try: success, log = SshUtil.do_ssh_cmd(ip, port, account, pkey, shell, JsonUtil.object_2_json(data)) status = '3' # error if success: status = '4' # success except Exception, e: success, log = False, 'Server SSH error: ' + str(e) status = '5' # except
def standard_response(success, data): '''standard response ''' rst = {} rst['success'] = success rst['data'] = data return JsonUtil.object_2_json(rst)
def add_redis(): host = RequestUtil.get_parameter('host', '') port = RequestUtil.get_parameter('port', '6379') password = RequestUtil.get_parameter('password', '') try: rst = RedisMonitor().ping(host=host, port=port, password=password) if not rst.get('success', ''): # ping 失败 return JsonUtil.object_2_json(rst) except: ResponseUtil.standard_response(0, 'Ping error!') # ping 通,添加 md5 = StringUtil.md5(host + str(port)) redis_info = RedisInfo.query.get(md5) if redis_info: redis_info.password = password else: redis_info = RedisInfo(md5=md5, host=host, port=port, password=password) redis_info.save() return ResponseUtil.standard_response(1, redis_info.dict())
def decorated_function(*args, **kwargs): if (RequestUtil.get_login_user(session) == ''): if type == 'page': return redirect(url_for('login', next=request.url)) else: return JsonUtil.object_2_json({'success': 0, 'data': 'the interface need to be login'}) return function(*args, **kwargs)
def updateStatus(self, status): self.update_time = datetime.datetime.now() self.status = status self.save() socketio.emit('history', JsonUtil.object_2_json(self.dict()), room=self.webhook.id)
def standard_response(success, data): '''接口的标准json返回值,统一使用同一个方法,便于统一修改 ''' rst = {} rst['success'] = success rst['data'] = data return JsonUtil.object_2_json(rst)
def api_webhook_retry(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhook_id = RequestUtil.get_parameter('webhook_id', '') data = { 'src': 'Manually executed' } webhook = WebHook.query.get(webhook_id) if not webhook: return ResponseUtil.standard_response(0, 'WebHooknot exist!') if not AuthUtil.has_readonly_auth(user_id, webhook_id): return ResponseUtil.standard_response(0, 'Permission deny!') # if webhook.status not in ['3', '4', '5']: # return ResponseUtil.standard_response(0, 'Webhook is Executing!') history = History(webhook_id=webhook.id, data=JsonUtil.object_2_json(data)) history.updateStatus('1') # status is waiting webhook.updateStatus('1') # do the async task tasks.do_webhook_shell.delay(webhook.id, history.id, data, user_id=user_id) return ResponseUtil.standard_response(1, webhook.dict())
def setUpClass(cls): super(TestOrderModule, cls).setUpClass() # 从cookie.json中读取PHPSESSID cookie_file = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'data/cookie.json') php_session_id = JsonUtil.get_data(cookie_file, 'PHPSESSID') cls.cookies = {"PHPSESSID": php_session_id}
def redis_ping(): try: host = RequestUtil.get_parameter('host', '') port = RequestUtil.get_parameter('port', '6379') password = RequestUtil.get_parameter('password', '') rst = RedisMonitor().ping(host=host, port=port, password=password) except: rst = {'success': 0, 'data': 'ping error!'} return JsonUtil.object_2_json(rst)
def redis_monitor(): try: md5 = RequestUtil.get_parameter('md5', '') redis_info = RedisInfo.query.get(md5) if redis_info: rst = RedisMonitor().get_info(host=redis_info.host, port=redis_info.port, password=redis_info.password) else: rst = {'success': 0, 'data': 'not exist redis informations!'} except: rst = {'success': 0, 'data': 'get redis realtime information error!'} return JsonUtil.object_2_json(rst)
def __init__(self): self.config_util = Config.ConfigUtil() self.base_url = self.config_util.get_option_value_by_section( 'crm', 'base_url') self.request = RequestUtil.RequestUtil(self.base_url) # 从cookie.json中读取PHPSESSID cookie_file = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'data/cookie.json') php_session_id = JsonUtil.get_data(cookie_file, 'PHPSESSID') self.cookies = {"PHPSESSID": php_session_id} self.headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" }
def api_for_webhook(key): '''git hook data ''' # try: data = RequestUtil.get_parameter('hook', None) if data is None: data = request.data # for test # data = WebhookData.github # data = WebhookData.gitlab # data = WebhookData.gitosc try: data = json.loads(data) webhook = WebHook.query.filter_by(key=key).first() if webhook: repo = webhook.repo branch = webhook.branch # then repo and branch is match the config. then do the shell if HookDataParse.get_repo_name( data) == repo and HookDataParse.get_repo_branch( data) == branch: # start to process, add history into database # waiting to done history = History(status='1', webhook_id=webhook.id, data=JsonUtil.object_2_json(data)) history.save() # status is waiting webhook.updateStatus('1') # do the async task tasks.do_webhook_shell.delay(webhook.id, history.id, data) return "Work put into Queue." return "Not match the Repo and Branch." else: return "The webhook is not exist." except Exception, e: return "Request is not valid Git webhook: " + str(e)
def do_webhook_shell(webhook_id, history_id, data): webhook = WebHook.query.get(webhook_id) history = History.query.get(history_id) # server information ip = webhook.server.ip port = webhook.server.port account = webhook.server.account pkey = webhook.server.pkey # do what shell = webhook.shell # start to process, add history into database status = '2' history.push_user = '******' % ( HookDataParse.get_push_name(data) or 'WebHook', HookDataParse.get_push_email(data) or 'Web GUI') history.updateStatus(status) webhook.updateStatus(status) try: success, log = SshUtil.do_ssh_cmd(ip, port, account, pkey, shell, JsonUtil.object_2_json(data)) status = '3' # error if success: status = '4' # success except Exception as e: success, log = False, 'Server SSH error: ' + str(e) status = '5' # except # ProgrammingError: You must not use 8-bit bytestrings unless you use a # text_factory log = unicode(log) # noqa history.status = status history.update_time = datetime.datetime.now() history.shell_log = log history.save() webhook.updateStatus(status)
def render_template(*args, **kwargs): kwargs['loginUser'] = JsonUtil.object_2_json(RequestUtil.get_login_user()) return flask.render_template(*args, **kwargs)
def standard_response(data): rst = {} rst['data'] = data return JsonUtil.object_2_json(rst)