Ejemplo n.º 1
0
def standard_response(success, data):
    '''standard response
    '''
    rst = {}
    rst['success'] = success
    rst['data'] = data
    return JsonUtil.object_2_json(rst)
Ejemplo n.º 2
0
def standard_response(code, data):
    '''standard response
    '''
    rst = {}
    rst['code'] = code
    rst['data'] = data
    return JsonUtil.object_2_json(rst)
Ejemplo n.º 3
0
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())
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def standard_response(success, data):
    '''接口的标准json返回值,统一使用同一个方法,便于统一修改
    '''
    rst = {}
    rst['success'] = success
    rst['data'] = data
    return JsonUtil.object_2_json(rst)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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())
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
def standard_response(data):
    rst = {}
    rst['data'] = data
    return JsonUtil.object_2_json(rst)
Ejemplo n.º 15
0
def render_template(*args, **kwargs):
    kwargs['loginUser'] = JsonUtil.object_2_json(RequestUtil.get_login_user())
    return flask.render_template(*args, **kwargs)