Example #1
0
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')
Example #2
0
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)
Example #3
0
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!')
Example #4
0
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())
Example #5
0
 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)
Example #6
0
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())
Example #7
0
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)
Example #8
0
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)
Example #9
0
def api_server_new():
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')

    ip = RequestUtil.get_parameter('ip', '')
    name = RequestUtil.get_parameter('name', ip)
    name = name and name or ip
    port = RequestUtil.get_parameter('port', 22)
    account = RequestUtil.get_parameter('account', '')
    pkey = RequestUtil.get_parameter('pkey', '')

    if not all((ip, name, port, account, pkey)):
        return ResponseUtil.standard_response(0, 'Form data can not be blank!')

    try:
        success, log = SshUtil.do_ssh_cmd(ip,
                                          port,
                                          account,
                                          pkey,
                                          'ls -lh',
                                          timeout=5)
        if success:
            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())
    except Exception, e:
        print e
Example #10
0
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)
Example #11
0
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)
Example #12
0
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)
Example #13
0
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)
Example #14
0
def api_server_delete(server_id):
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')
    server = Server.query.filter_by(user_id=user_id, id=server_id).first()
    if not server:
        return ResponseUtil.standard_response(0, 'Permission deny!')

    server.deleted = True
    server.save()

    return ResponseUtil.standard_response(1, 'Success')
Example #15
0
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')
Example #16
0
def api_server_new():
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')

    ip = RequestUtil.get_parameter('ip', '')
    name = RequestUtil.get_parameter('name', ip)
    name = name and name or ip
    port = RequestUtil.get_parameter('port', 22)
    account = RequestUtil.get_parameter('account', '')
    pkey = RequestUtil.get_parameter('pkey', '')

    if not all((ip, name, port, account, pkey)):
        return ResponseUtil.standard_response(0, 'Form data can not be blank!')

    try:
        success, log = SshUtil.do_ssh_cmd(ip,
                                          port,
                                          account,
                                          pkey,
                                          'ls -lh',
                                          timeout=5)
        if success:
            server_id = RequestUtil.get_parameter('id', '')
            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!')
Example #17
0
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')
Example #18
0
def api_collaborator_delete(collaborator_id):
    # login user
    user_id = RequestUtil.get_login_user().get('id', '')

    collaborator = Collaborator.query.get(collaborator_id)
    if not collaborator:
        return ResponseUtil.standard_response(0, 'Permission deny!')

    webhook_id = collaborator.webhook_id

    if not AuthUtil.has_admin_auth(user_id, webhook_id):
        return ResponseUtil.standard_response(0, 'Permission deny!')

    collaborator.delete()

    return ResponseUtil.standard_response(1, 'Success')
Example #19
0
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_id = RequestUtil.get_parameter('id', '')
    if webhook_id:
        # update webhook
        # you can only update the webhook which you create.
        webhook = WebHook.query.filter_by(
            id=webhook_id, user_id=user_id).first()
        if not webhook:
            return ResponseUtil.standard_response(0, 'WebHook is not exist!')
        webhook.repo = repo
        webhook.branch = branch
        webhook.shell = shell
        webhook.server_id = server_id
    else:
        # new webhook
        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))
Example #20
0
def render_template(*args, **kwargs):
    kwargs['loginUser'] = JsonUtil.object_2_json(RequestUtil.get_login_user())
    return flask.render_template(*args, **kwargs)
Example #21
0
def get_login_user():
    rst = RequestUtil.get_login_user()
    if (rst is None):
        return ResponseUtil.standard_response(1, '请重新登录')
    return ResponseUtil.standard_response(0, rst)
Example #22
0
File: api.py Project: hustcc/TODO
def api(api):
    user_id = RequestUtil.get_login_user(session)
    rst = hanlder.get(api, for_404)(user_id)
    if rst:
        return rst
    return ResponseUtil.standard_response(1, 'test')