Ejemplo n.º 1
0
    def post(self, request, format=None):
        if request.data['excu'] == 'list':
            try:
                app_log_path = request.data['app_log_path']
                host = request.data['host']
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                commands = "find %s -name '*.log'" % (app_log_path)
                result = connect.run(commands).stdout
                res = []
                for i in result.split():
                    res.append(i)
                res = filter(None, res)
                connect.close()
                http_status = OK
            except Exception as e:
                http_status = BAD
                res = '执行错误:' + str(e)
            return XopsResponse(res, status=http_status)

        elif request.data['excu'] == 'filedown':
            file_path = request.data['file_path']
            host = request.data['host']
            file_name = os.path.basename(file_path)
            dir_name = os.path.dirname(file_path)
            old_file_name = os.path.splitext(file_name)
            if old_file_name[1] == '.log':
                new_file_name = old_file_name[0] + '.tar.gz'
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                commands = 'mkdir -p /tmp/remote/ && tar czf /tmp/remote/%s -C %s %s' % (
                    new_file_name, dir_name, file_name)
                connect.run(commands)
                connect.get('/tmp/remote/' + new_file_name,
                            '/tmp/' + new_file_name)
                response = FileResponse(open('/tmp/' + new_file_name, 'rb'))
                response['Content-Type'] = 'application/octet-stream'
                response[
                    'Content-Disposition'] = 'attachment;filename="%s"' % old_file_name[
                        0]
                commands = 'rm -f /tmp/remote/%s' % (new_file_name)
                connect.run(commands)
                connect.close()
                os.remove('/tmp/' + new_file_name)
                return response
            else:
                return XopsResponse('请求文件格式不对!', status=BAD)
Ejemplo n.º 2
0
 def get(self, request, format=None):
     file_path = request.query_params['file']
     host = request.query_params['sid']
     file_name = os.path.basename(file_path)
     new_file_name = os.path.splitext(file_name)[0] + '.tar.gz'
     auth_info, auth_key = auth_init(host)
     connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key)
     commands = 'mkdir -p /tmp/remote/ && tar czf /tmp/remote/%s %s' % (
         new_file_name, file_path)
     connect.run(commands)
     connect.get('/tmp/remote/' + new_file_name, '/tmp/' + new_file_name)
     response = FileResponse(open('/tmp/' + new_file_name, 'rb'))
     response['Content-Type'] = 'application/octet-stream'
     response[
         'Content-Disposition'] = 'attachment;filename="%s"' % new_file_name
     commands = 'rm -f /tmp/remote/%s' % (new_file_name)
     connect.run(commands)
     connect.close()
     os.remove('/tmp/' + new_file_name)
     return response