コード例 #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)
コード例 #2
0
ファイル: deploy_excu.py プロジェクト: zeroCamus/rest_xops
 def start(self, log, version, serverid, record_id, webuser, start_time):
     self.start_time = start_time
     with open(log, 'a') as f:
         f.write('[INFO]版本: %s 执行用户: %s 开始时间: %s\n[INFO]本次部署日志路径: %s\n' %
                 (version, webuser, start_time, log))
     try:
         self.do_prev_deploy(log)
         self.do_checkout(version, log)
         self.do_post_deploy(log)
         for sid in serverid:
             if sid:
                 auth_info, auth_key = auth_init(sid)
                 if auth_info and auth_key:
                     connect = Shell(auth_info,
                                     connect_timeout=5,
                                     connect_kwargs=auth_key)
                     self.do_prev_release(log, connect)
                     self.do_release(log, connect)
                     self.do_post_release(log, connect)
                 else:
                     Tailf.send_message(webuser,
                                        '[ERROR]服务器ID%s已被删除,部署继续执行!' % sid)
             else:
                 Tailf.send_message(webuser, '没有选择远程服务器!!!')
         self.end(serverid, record_id)
     except Exception as e:
         Tailf.send_message(webuser, str(e))
コード例 #3
0
ファイル: deploy.py プロジェクト: showsmall/rest_xops
    def do_rollback(self, id, log, record_id):
        '''
        回滚
        '''
        sequence = 1
        with open(log, 'a') as f:
            f.write('[INFO]------正在执行回滚[%s]------\n' % (sequence))
        record = DeployRecord.objects.filter(id=int(id)).values()
        server_ids = record[0]['server_ids'].split(',')
        name = '回滚_' + str(id) + '_' + record_id
        for sid in server_ids:
            try:
                auth_info, auth_key = auth_init(sid)
                connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key)
                version_file = '%s/%s' % (record[0]['target_root'], record[0]['alias'] + '_version.txt')
                # 判断回滚版本是否存在
                command = '[ -d %s/%s ] || echo "false"' % (record[0]['target_releases'], record[0]['prev_record'])
                self.result = connect.run(command, write=log)
                if not self.result.stdout.strip() == 'false':
                    # 删除目标软链
                    command = 'find %s -type l -delete' % (record[0]['target_root'])
                    self.result = connect.run(command, write=log)
                    # 创建需回滚版本软链到webroot
                    command = 'ln -sfn %s/%s/* %s' % (
                    record[0]['target_releases'], record[0]['prev_record'], record[0]['target_root'])
                    if self.result.exited == 0: self.result = connect.run(command, write=log)
                    command = 'echo %s > %s' % (record[0]['prev_record'], version_file)
                    if self.result.exited == 0: self.result = connect.run(command, write=log)
                connect.close()
            except Exception as e:
                error_logger.error(e)

        # 处理完记录入库
        defaults = {
            'name': name,
            'record_id': record[0]['prev_record'],
            'alias': record[0]['alias'],
            'project_id': record[0]['project_id'],
            'server_ids': record[0]['server_ids'],
            'target_root': record[0]['target_root'],
            'target_releases': record[0]['target_releases'],
            'prev_record': record[0]['record_id'],
            'is_rollback':True,
            'status': 'Succeed'
        }
        if self.result.exited != 0 or self.result.stdout.strip() == 'false':
            defaults['status'] = 'Failed'
            defaults['is_rollback'] = False
            DeployRecord.objects.create(**defaults)
            Project.objects.filter(id=record[0]['project_id']).update(last_task_status='Failed')
            with open(log, 'a') as f:
                f.write('[ERROR]------回滚失败------\n')
                f.write('[ERROR]回滚的版本文件可能已删除!\n')
        else:
            DeployRecord.objects.create(**defaults)
            Project.objects.filter(id=record[0]['project_id']).update(last_task_status='Succeed')
            sequence = 6
            with open(log, 'a') as f:
                f.write('[INFO]------回滚完成,请重启应用[%s]------\n' % (sequence))
コード例 #4
0
ファイル: applog.py プロジェクト: heroessss/rest_xops
 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
コード例 #5
0
ファイル: deploy_excu.py プロジェクト: showsmall/rest_xops
 def start(self, log, version, serverid, record_id, webuser, start_time):
     self.start_time = start_time
     try:
         self.do_prev_deploy(log)
         self.do_checkout(version, log)
         self.do_post_deploy(log)
         for sid in serverid:
             if sid:
                 auth_info, auth_key = auth_init(sid)
                 if auth_info and auth_key:
                     connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key)
                     self.do_prev_release(log, connect)
                     self.do_release(log, connect)
                     self.do_post_release(log, connect)
                 else:
                     Tailf.send_message(webuser, '[ERROR]服务器ID%s已被删除,部署继续执行!' % sid)
             else:
                 Tailf.send_message(webuser, '没有选择远程服务器!!!')
         self.end(serverid, record_id)
     except Exception as e:
         Tailf.send_message(webuser, str(e))
コード例 #6
0
ファイル: deploy_excu.py プロジェクト: zhoufaye/rest_xops
 def start(self, log, version, serverid, record_id, webuser):
     try:
         self.do_prev_deploy(log)
         self.do_checkout(version, log)
         self.do_post_deploy(log)
         for sid in serverid:
             try:
                 auth_info, auth_key = auth_init(sid)
                 connect = Shell(auth_info,
                                 connect_timeout=5,
                                 connect_kwargs=auth_key)
                 self.do_prev_release(log, connect)
                 self.do_release(log, connect)
                 self.do_post_release(log, connect)
             except Exception as e:
                 send = Tailf()
                 send.send_message(webuser, str(e))
         self.end(serverid, record_id)
     except Exception as e:
         send = Tailf()
         send.send_message(webuser, str(e))
コード例 #7
0
 def run(self, id, log, version, serverid, record_id, webuser, start_time):
     info_logger.info(
         '[部署任务开始] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s' % (start_time, record_id, version, webuser, id))
     redis = RedisObj()
     redis.set('deploy_' + str(webuser) + '_' + str(id), '0')
     self.init(webuser, record_id, id)
     self.start_time = start_time
     with open(log, 'a') as f:
         f.write('[INFO]版本: %s 执行用户: %s 开始时间: %s\n[INFO]本次部署日志路径: %s\n' % (version, webuser, start_time, log))
     try:
         self.do_prev_deploy(log)
         self.do_checkout(version, log)
         self.do_post_deploy(log)
         for sid in serverid:
             try:
                 auth_info, auth_key = auth_init(sid)
                 connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key)
                 self.do_prev_release(log, connect)
                 self.do_release(log, connect)
                 self.do_post_release(log, connect)
             except Exception as e:
                 time.sleep(20)
                 Tailf.send_message(webuser, '[ERROR] 服务器为空或ID %s 可能已被删除!' % sid)
                 Tailf.send_message(webuser, '[ERROR] 错误信息:' % e)
                 error_logger.error('[部署任务错误] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s 信息:%s' % (
                 start_time, record_id, version, webuser, id, e))
         self.end(serverid, record_id)
         info_logger.info('[部署任务已结束] 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s' % (record_id, version, webuser, id))
     except Exception as e:
         Tailf.send_message(webuser, '[ERROR] 错误信息:' % e)
         error_logger.error('[部署任务错误] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s 信息:%s' % (
         start_time, record_id, version, webuser, id, e))
     finally:
         if self.localhost:
             # 关闭连接
             self.localhost.close()
         # 关闭local_tailf死循环
         redis.set('deploy_' + str(webuser) + '_' + str(id), '1')
コード例 #8
0
    def post(self, request, format=None):
        if request.data['excu'] == 'init':
            # 项目初始化
            id = request.data['id']
            result = self.repo_init(id)
            if result.exited == 0:
                Project.objects.filter(id=id).update(status='Succeed')
                info_logger.info('初始化项目:' + str(id) + ',执行成功!')
                http_status = OK
                msg = '初始化成功!'
            else:
                error_logger.error('初始化项目:%s 执行失败! 错误信息:%s' %
                                   (str(id), result.stderr))
                http_status = BAD
                msg = '初始化项目:%s 执行失败! 错误信息:%s' % (str(id), result.stderr)

            return XopsResponse(msg, status=http_status)

        elif request.data['excu'] == 'deploy':
            # 部署操作
            id = request.data['id']
            webuser = request.user.username
            alias = request.data['alias']
            self.start_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            record_id = str(alias) + '_' + str(self.start_time)
            name = '部署_' + record_id
            DeployRecord.objects.create(name=name,
                                        alias=alias,
                                        status='Failed',
                                        project_id=int(id))
            Project.objects.filter(id=id).update(last_task_status='Failed')
            local_log_path = self._path.rstrip('/') + '/' + str(
                id) + '_' + str(request.data['alias']) + '/logs'
            log = local_log_path + '/' + record_id + '.log'
            version = request.data['version'].strip()
            serverid = request.data['server_ids']
            # 调用celery异步任务
            deploy.delay(id, log, version, serverid, record_id, webuser,
                         self.start_time)
            return XopsResponse(record_id)

        elif request.data['excu'] == 'rollback':
            # 回滚
            id = request.data['id']
            project_id = request.data['project_id']
            alias = request.data['alias']
            self.start_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            record_id = str(alias) + '_' + str(self.start_time)
            log = self._path.rstrip('/') + '/' + str(project_id) + '_' + str(
                alias) + '/logs/' + record_id + '.log'
            self.do_rollback(id, log, record_id)
            return XopsResponse(record_id)

        elif request.data['excu'] == 'deploymsg':
            # 部署控制台消息读取
            try:
                id = request.data['id']
                alias = request.data['alias']
                record = request.data['record']
                scenario = int(request.data['scenario'])
                logfile = self._path.rstrip('/') + '/' + str(id) + '_' + str(
                    alias) + '/logs/' + record + '.log'
                webuser = request.user.username
                if scenario == 0:
                    local_tailf.delay(logfile, webuser, id)
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'readlog' and request.data[
                'scenario'] == 1:
            # 读取部署日志
            try:
                id = request.data['id']
                alias = request.data['alias']
                record = request.data['record']
                logfile = self._path.rstrip('/') + '/' + str(id) + '_' + str(
                    alias) + '/logs/' + record + '.log'
                response = FileResponse(open(logfile, 'rb'))
                response['Content-Type'] = 'text/plain'
                return response
            except Exception:
                http_status = BAD
                request_status = '执行错误:文件不存在!'
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'app_start':
            # 项目启动
            try:
                app_start = request.data['app_start']
                host = request.data['host']
                webuser = request.user.username
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                app_start = app_start.strip().replace('&&',
                                                      '').replace('||', '')
                connect.run(app_start, ws=True, webuser=webuser)
                connect.close()
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = '执行错误:' + str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'app_stop':
            # 项目停止
            try:
                app_stop = request.data['app_stop']
                host = request.data['host']
                webuser = request.user.username
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                app_stop = app_stop.strip().replace('&&', '').replace('||', '')
                connect.run(app_stop, ws=True, webuser=webuser)
                connect.close()
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = '执行错误:' + str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'tail_start':
            # 日志监控
            try:
                filter_text = str(request.data['filter'])
                app_log_file = request.data['app_log_file']
                host = request.data['host']
                webuser = request.user.username
                device_info = DeviceInfo.objects.filter(id=int(host)).values()
                host = device_info[0]['hostname']
                auth_type = device_info[0]['auth_type']
                connect_info = ConnectionInfo.objects.filter(
                    hostname=host, auth_type=auth_type).values()
                user = connect_info[0]['username']
                passwd = connect_info[0]['password']
                port = connect_info[0]['port']
                tail = Tailf()
                tail.remote_tail(host,
                                 port,
                                 user,
                                 passwd,
                                 app_log_file,
                                 webuser,
                                 filter_text=filter_text)
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'tail_stop':
            # 日志监控停止
            try:
                webuser = request.user.username
                redis = RedisObj()
                redis.set('remote_tail_' + str(webuser), '1')
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = str(e)
            return XopsResponse(request_status, status=http_status)
コード例 #9
0
ファイル: deploy.py プロジェクト: caoxianwei/rest_xops
    def post(self, request, format=None):
        if request.data['excu'] == 'init':
            # 项目初始化
            id = request.data['id']
            result = self.repo_init(id)
            if result.exited == 0:
                Project.objects.filter(id=id).update(status='Succeed')
                info_logger.info('初始化项目:' + str(id) + ',执行成功!')
                http_status = OK
                msg = '初始化成功!'
            else:
                error_logger.error('初始化项目:%s 执行失败! 错误信息:%s' %
                                   (str(id), result.stderr))
                http_status = BAD
                msg = '初始化项目:%s 执行失败! 错误信息:%s' % (str(id), result.stderr)

            return XopsResponse(msg, status=http_status)

        elif request.data['excu'] == 'deploy':
            # 部署操作
            id = request.data['id']
            webuser = request.user.username
            alias = request.data['alias']
            self.start_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            record_id = str(alias) + '_' + str(self.start_time)
            name = '部署_' + record_id
            DeployRecord.objects.create(name=name,
                                        alias=alias,
                                        status='Failed',
                                        project_id=int(id))
            Project.objects.filter(id=id).update(last_task_status='Failed')
            local_log_path = self._path.rstrip('/') + '/' + str(
                id) + '_' + str(request.data['alias']) + '/logs'
            log = local_log_path + '/' + record_id + '.log'
            version = request.data['version'].strip()
            serverid = request.data['server_ids']
            deploy = DeployExcu(webuser, record_id, id)
            deploy.start(log, version, serverid, record_id, webuser)
            return XopsResponse(record_id)

        elif request.data['excu'] == 'rollback':
            # 回滚
            id = request.data['id']
            project_id = request.data['project_id']
            alias = request.data['alias']
            self.start_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            record_id = str(alias) + '_' + str(self.start_time)
            log = self._path.rstrip('/') + '/' + str(project_id) + '_' + str(
                alias) + '/logs/' + record_id + '.log'
            self.do_rollback(id, log, record_id)
            return XopsResponse(record_id)

        elif request.data['excu'] == 'deploymsg':
            # 部署控制台消息读取
            try:
                id = request.data['id']
                alias = request.data['alias']
                record = request.data['record']
                scenario = int(request.data['scenario'])
                logfile = self._path.rstrip('/') + '/' + str(id) + '_' + str(
                    alias) + '/logs/' + record + '.log'
                webuser = request.user.username
                msg = Tailf()
                if scenario == 0:
                    msg.local_tail(logfile, webuser)
                else:
                    msg.read_file(logfile, webuser)
                http_status = OK
                request_status = '执行成功!'
            except Exception:
                http_status = BAD
                request_status = '执行错误:文件不存在!'
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'app_start':
            # 项目启动
            try:
                app_start = request.data['app_start']
                host = request.data['host']
                webuser = request.user.username
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                app_start = app_start.strip().replace('&&',
                                                      '').replace('||', '')
                commands = '/bin/bash -e %s' % (app_start)
                connect.run(commands, ws=True, webuser=webuser)
                connect.close()
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = '执行错误:' + str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'app_stop':
            # 项目停止
            try:
                app_stop = request.data['app_stop']
                host = request.data['host']
                webuser = request.user.username
                auth_info, auth_key = auth_init(host)
                connect = Shell(auth_info,
                                connect_timeout=5,
                                connect_kwargs=auth_key)
                app_stop = app_stop.strip().replace('&&', '').replace('||', '')
                commands = '/bin/bash -e %s' % (app_stop)
                connect.run(commands, ws=True, webuser=webuser)
                connect.close()
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = '执行错误:' + str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'tail_start':
            # 日志监控
            try:
                filter_text = str(request.data['filter'])
                app_log_file = request.data['app_log_file']
                host = request.data['host']
                webuser = request.user.username
                device_info = DeviceInfo.objects.filter(id=int(host)).values()
                host = device_info[0]['hostname']
                auth_type = device_info[0]['auth_type']
                connect_info = ConnectionInfo.objects.filter(
                    hostname=host, auth_type=auth_type).values()
                user = connect_info[0]['username']
                passwd = connect_info[0]['password']
                port = connect_info[0]['port']
                tail = Tailf()
                tail.remote_tail(host,
                                 port,
                                 user,
                                 passwd,
                                 app_log_file,
                                 webuser,
                                 filter_text=filter_text)
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = str(e)
            return XopsResponse(request_status, status=http_status)

        elif request.data['excu'] == 'tail_stop':
            # 日志监控停止
            try:
                webuser = request.user.username
                if hasattr(gl, '_global_dict'):
                    tail_key = 'tail_' + str(webuser)
                    if tail_key in gl._global_dict.keys():
                        client = gl.get_value('tail_' + str(webuser))
                        client.close()
                http_status = OK
                request_status = '执行成功!'
            except Exception as e:
                http_status = BAD
                request_status = str(e)
            return XopsResponse(request_status, status=http_status)