def save(user_id): id = RequestUtil.get_parameter(request, 'id', None) title = RequestUtil.get_parameter(request, 'title', None) completed = RequestUtil.get_parameter(request, 'completed', None) if completed == 'true': completed = True else: completed = False if id: todo = Todo.query.filter_by(id=id).first() else: todo = Todo(user_id=user_id, add_time=DateUtil.now_datetime()) # new todo if todo and todo.user_id == user_id: # update it. if title: todo.title = title if completed is not None: todo.completed = completed # print todo.dict() todo.save() return ResponseUtil.standard_response(1, todo.dict()) else: return ResponseUtil.standard_response(0, 'save error.')
def github_authorized(oauth_token): if oauth_token is None: flash("Authorization failed.") return redirect(url_for('index')) session['oauth_token'] = oauth_token me = github.get('user') user_id = me['login'] # is user exist user = User.query.get(user_id) if user is None: # not exist, add user = User(id=user_id) # update github user information user.last_login = DateUtil.now_datetime() user.name = me.get('name', user_id) user.location = me.get('location', '') user.avatar = me.get('avatar_url', '') user.save() RequestUtil.login_user(user.dict()) return redirect(url_for('index'))
def api_history_list(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhook_id = RequestUtil.get_parameter('webhook_id', '') if not AuthUtil.has_readonly_auth(user_id, webhook_id): return ResponseUtil.standard_response(0, 'Permission deny!') page = RequestUtil.get_parameter('page', '1') try: page = int(page) if page < 1: page = 1 except: page = 1 page_size = 25 paginations = History.query\ .filter_by(webhook_id=webhook_id)\ .order_by(History.id.desc())\ .paginate(page, page_size, error_out=False) histories = [history.dict() for history in paginations.items] data = { 'histories': histories, 'has_prev': paginations.has_prev, 'has_next': paginations.has_next, 'page': paginations.page } return ResponseUtil.standard_response(1, data)
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 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 loginApi(username, password): user = User.query.filter_by(username=username).first() if not user or not user.verify_password(password): return ResponseUtil.standard_response(0, '用户名不存在或密码错误') user.last_login = DateUtil.now_datetime() user.save() RequestUtil.login_user(user.dict()) return ResponseUtil.standard_response(1, user.dict())
def redis_ping(): try: host = RequestUtil.get_parameter(request, 'host', '') port = int(RequestUtil.get_parameter(request, 'port', '6379')) password = RequestUtil.get_parameter(request, 'password', '') rst = RedisMonitor().ping(host=host, port=port, password=password) except: rst = {'success': 0, 'data': 'ping error'} return OtherUtil.object_2_dict(rst)
def redis_ping(): try: host = RequestUtil.get_parameter(request, 'host', '') port = int(RequestUtil.get_parameter(request, 'port', '6379')) password = RequestUtil.get_parameter(request, 'password', '') rst = RedisMonitor().ping(host = host, port = port, password = password) except: rst = {'success': 0, 'data': 'ping error'} return OtherUtil.object_2_dict(rst)
def redis_ping(): try: host = RequestUtil.get_parameter(request, "host", "") port = int(RequestUtil.get_parameter(request, "port", "6379")) password = RequestUtil.get_parameter(request, "password", "") rst = RedisMonitor().ping(host=host, port=port, password=password) except: rst = {"success": 0, "data": "ping error"} return OtherUtil.object_2_dict(rst)
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 api_webhook_delete(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhook_id = RequestUtil.get_parameter('webhook_id', '') webhook = WebHook.query.filter_by(user_id=user_id, id=webhook_id).first() if not webhook: return ResponseUtil.standard_response(0, 'Permition deny!') webhook.delete() return ResponseUtil.standard_response(1, 'Success')
def add_redis(): redis_host = RequestUtil.get_parameter(request, 'host', '') redis_port = int(RequestUtil.get_parameter(request, 'port', '6379')) redis_pass = RequestUtil.get_parameter(request, 'password', '') email = RequestUtil.get_parameter(request, 'email', '') rid = redisinfo_dbs.add_redis(redis_host, redis_port, redis_pass, email) if rid: rst = {'success': 1, 'data': ''} else: rst = {'success': 0, 'data': 'add redis error'} return OtherUtil.object_2_dict(rst)
def add_redis(): redis_host = RequestUtil.get_parameter(request, "host", "") redis_port = int(RequestUtil.get_parameter(request, "port", "6379")) redis_pass = RequestUtil.get_parameter(request, "password", "") email = RequestUtil.get_parameter(request, "email", "") rid = redisinfo_dbs.add_redis(redis_host, redis_port, redis_pass, email) if rid: rst = {"success": 1, "data": ""} else: rst = {"success": 0, "data": "add redis error"} return OtherUtil.object_2_dict(rst)
def api_server_delete(): # login user user_id = RequestUtil.get_login_user().get('id', '') server_id = RequestUtil.get_parameter('server_id', '') server = Server.query.filter_by(user_id=user_id, id=server_id).first() if not server: return ResponseUtil.standard_response(0, 'Permition deny!') server.deleted = True server.save() return ResponseUtil.standard_response(1, 'Success')
def api_webhook_delete(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhook_id = RequestUtil.get_parameter('webhook_id', '') # 验证创建者权限 webhook = AuthUtil.has_admin_auth(user_id, webhook_id) if not webhook: return ResponseUtil.standard_response(0, 'Permission deny!') webhook.deleted = True webhook.save() return ResponseUtil.standard_response(1, 'Success')
def flushall_redis(): try: md5 = RequestUtil.get_parameter('md5', '') db = RequestUtil.get_parameter('db', '0') redis_info = RedisInfo.query.get(md5) if redis_info: r = RedisUtil.flushall(redis_info.host, redis_info.port, redis_info.password, db) if r: return ResponseUtil.standard_response(1, 'Success!') return ResponseUtil.standard_response(0, 'Flush db error!') return ResponseUtil.standard_response(0, 'Not Found!') except: return ResponseUtil.standard_response(0, 'Connect to redis error!')
def get_server_paramter(): try: type = RequestUtil.get_parameter(request, "type", "") host = RequestUtil.get_parameter(request, "host", "1") port = int(RequestUtil.get_parameter(request, "port", "6379")) password = RequestUtil.get_parameter(request, "password", "") if type == "http": rst = HttpMonitor().get_info(url=host) elif type == "redis": rst = RedisMonitor().get_info(host=host, port=port, password=password) else: rst = {"success": 1, "data": 0} except: rst = {"success": 0, "data": ""} return OtherUtil.object_2_dict(rst)
def api_collaborator_new(webhook_id, user_id): # login user login_user_id = RequestUtil.get_login_user().get('id', '') if login_user_id == user_id: return ResponseUtil.standard_response(0, '`%s` is Creator!' % user_id) if not AuthUtil.has_admin_auth(login_user_id, webhook_id): return ResponseUtil.standard_response(0, 'Permission deny!') collaborator = Collaborator.query.filter_by(webhook_id=webhook_id, user_id=user_id).first() # not exist if collaborator: return ResponseUtil.standard_response(0, 'Collaborator exist!') # 开始添加 user = User.query.get(user_id) if not user: user = User(id=user_id, name=user_id) user.save() collaborator = Collaborator(webhook_id=webhook_id, user=user) collaborator.save() return ResponseUtil.standard_response(1, collaborator.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 api_webhook_list(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhooks = AuthUtil.has_auth_webhooks(user_id) # 转json webhooks = [webhook.dict(True) for webhook in webhooks] return ResponseUtil.standard_response(1, webhooks)
def api_server_new(ip, port, account, pkey, name=None, id=None): # login user user_id = RequestUtil.get_login_user().get('id', '') server_id = id name = name if name else ip try: success, log = SshUtil.do_ssh_cmd( ip, port, account, pkey, 'ls -lh', timeout=5) if success: if server_id: # update webhook # you can only update the webhook which you create. server = Server.query.filter_by( id=server_id, user_id=user_id).first() if not server: return ResponseUtil.standard_response( 0, 'Server is not exist!') server.ip = ip server.port = port server.account = account server.pkey = pkey server.name = name else: server = Server(ip=ip, port=port, account=account, pkey=pkey, user_id=user_id, name=name) server.save() return ResponseUtil.standard_response( 1, server.dict(with_pkey=True)) except Exception as e: print(e) return ResponseUtil.standard_response(0, 'Server SSH connect error!')
def redis_info(): md5 = RequestUtil.get_parameter('md5', '') redis_info = RedisInfo.query.get(md5) if redis_info: return ResponseUtil.standard_response(1, redis_info.dict()) return ResponseUtil.standard_response(0, 'Not Found!')
def api_webhook_list(): # login user user_id = RequestUtil.get_login_user().get('id', '') # create webhooks created_webhooks = WebHook.query.filter_by(user_id=user_id, deleted=False).all() # collaborator webhooks collaborated_webhooks = \ WebHook.query.join(Collaborator, Collaborator.webhook_id == WebHook.id) \ .filter(Collaborator.user_id == user_id) \ .filter(WebHook.deleted == false()).all() webhooks = created_webhooks + collaborated_webhooks # to dict webhooks = {'id%s' % webhook.id: webhook for webhook in webhooks} # value webhooks = webhooks.values() # sort sorted(webhooks, key=lambda webhook: webhook.id) webhooks = [webhook.dict(True) for webhook in webhooks] return ResponseUtil.standard_response(1, webhooks)
def flushall_redis(): try: redis_md5 = RequestUtil.get_parameter(request, 'md5', '') db = RequestUtil.get_parameter(request, 'db', 0) redis_info = redisinfo_dbs.get_redis(redis_md5) if redis_info: r = RedisUtil.flushall(redis_info['redis_host'], redis_info['redis_port'], redis_info['redis_pass'], db) if r: rst = {'success': 1, 'data': ''} else: rst = {'success': 0, 'data': 'flush db error!'} else: rst = {'success': 0, 'data': 'not exist redis informations'} except: rst = {'success': 0, 'data': 'connect to redis error'} return OtherUtil.object_2_dict(rst)
def index(): user_id = RequestUtil.get_login_user(session) user = User.query.filter_by(id=user_id).first() if user: return flask.render_template('index.html', user_id=user_id) else: return flask.render_template('login.html')
def del_redis(): redis_md5 = RequestUtil.get_parameter(request, 'md5', '') rid = redisinfo_dbs.delete_redis(redis_md5) if rid: rst = {'success': 1, 'data': ''} else: rst = {'success': 0, 'data': 'del redis error'} return OtherUtil.object_2_dict(rst)
def api_webhook_list(): # login user user_id = RequestUtil.get_login_user().get('id', '') webhooks = WebHook.query.filter_by(user_id=user_id, deleted=False).all() webhooks = [webhook.dict(True) for webhook in webhooks] return ResponseUtil.standard_response(1, webhooks)
def get_server_paramter(): try: type = RequestUtil.get_parameter(request, 'type', '') host = RequestUtil.get_parameter(request, 'host', '1') port = int(RequestUtil.get_parameter(request, 'port', '6379')) password = RequestUtil.get_parameter(request, 'password', '') if type == 'http': rst = HttpMonitor().get_info(url=host) elif type == 'redis': rst = RedisMonitor().get_info(host=host, port=port, password=password) else: rst = {'success': 1, 'data': 0} except: rst = {'success': 0, 'data': ''} return OtherUtil.object_2_dict(rst)
def del_redis(): md5 = RequestUtil.get_parameter('md5', '') redis_info = RedisInfo.query.get(md5) if redis_info: redis_info.delete() return ResponseUtil.standard_response(1, 'Success!') return ResponseUtil.standard_response(0, 'Not Found!')
def api_server_list(): # login user user_id = RequestUtil.get_login_user().get('id', '') servers = Server.query.filter_by(user_id=user_id, deleted=False).all() servers = [server.dict(with_pkey=True) for server in servers] return ResponseUtil.standard_response(1, servers)
def del_redis(): redis_md5 = RequestUtil.get_parameter(request, "md5", "") rid = redisinfo_dbs.delete_redis(redis_md5) if rid: rst = {"success": 1, "data": ""} else: rst = {"success": 0, "data": "del redis error"} return OtherUtil.object_2_dict(rst)
def on_socketio_connect(): # 连接时自动监听所有有权限的 webhook user_id = RequestUtil.get_login_user().get('id', '') # 未登录,拒绝连接 if not user_id: return False webhooks = AuthUtil.has_auth_webhooks(user_id) for webhook in webhooks: flask_socketio.join_room(webhook.id)
def remove(user_id): id = RequestUtil.get_parameter(request, 'id', None) if id: todo = Todo.query.filter_by(id=id).first() if todo: todo.delete() return ResponseUtil.standard_response(1, 'remove success.') return ResponseUtil.standard_response(0, 'remove error.')
def api_collaborator_list(webhook_id): # login user user_id = RequestUtil.get_login_user().get('id', '') if not AuthUtil.has_readonly_auth(user_id, webhook_id): return ResponseUtil.standard_response(0, 'Permission deny!') collaborators = Collaborator.query.filter_by(webhook_id=webhook_id).all() collaborators = [collaborator.dict() for collaborator in collaborators] return ResponseUtil.standard_response(1, collaborators)
def get_redis_paramter(): try: redis_md5 = RequestUtil.get_parameter(request, 'md5', '') redis_info = redisinfo_dbs.get_redis(redis_md5) if redis_info: rst = RedisMonitor().get_info(host = redis_info['redis_host'], port = redis_info['redis_port'], password = redis_info['redis_pass']) else: rst = {'success': 0, 'data': 'not exist redis informations'} except: rst = {'success': 0, 'data': 'get redis realtime information error'} return OtherUtil.object_2_dict(rst)
def api_webhook_new(): # login user user_id = RequestUtil.get_login_user().get('id', '') server_id = RequestUtil.get_parameter('server_id', '') # server must be added by yourself if not Server.query.filter_by(id=server_id, user_id=user_id).first(): return ResponseUtil.standard_response(0, 'Permition deny!') repo = RequestUtil.get_parameter('repo', '') branch = RequestUtil.get_parameter('branch', '') shell = RequestUtil.get_parameter('shell', '') if not all((repo, branch, shell, server_id)): return ResponseUtil.standard_response(0, 'Form data can not be blank!') webhook = WebHook(repo=repo, branch=branch, shell=shell, server_id=server_id, user_id=user_id, key=StringUtil.md5_token()) webhook.save() return ResponseUtil.standard_response(1, webhook.dict(with_key=True))
def get_redis_paramter(): try: redis_md5 = RequestUtil.get_parameter(request, "md5", "") redis_info = redisinfo_dbs.get_redis(redis_md5) if redis_info: rst = RedisMonitor().get_info( host=redis_info["redis_host"], port=redis_info["redis_port"], password=redis_info["redis_pass"] ) else: rst = {"success": 0, "data": "not exist redis informations"} except: rst = {"success": 0, "data": "get redis realtime information error"} return OtherUtil.object_2_dict(rst)