コード例 #1
0
ファイル: views.py プロジェクト: idber/devops
def get_table_list(request):
    cps = ConfParserClass('conf/settings.conf')
    db_id = request.POST.get('db_id', '')
    dbinfo = DBInfo.objects.get(pk=db_id)
    db_username = cps.get('dba', 'username')
    db_pass = cps.get('dba', 'password')
    da_name = dbinfo.db_name
    table_list = []

    data = {
        'status': 0,
        'message': '',
        'table_list': table_list
    }

    try:
        # 获取表名称
        with MysqlConn(dbinfo.db_ip, dbinfo.db_port, da_name, db_username, db_pass) as cur:
            sql = """show tables;"""
            cur.execute(sql)
            table_list = [table[0] for table in cur.fetchall()]

        data = {
            'status': 0,
            'message': '',
            'table_list': table_list
        }

    except Exception as e:
        data['status'] = 1
        data['message'] = '获取表名称时发生错误.'

    return HttpResponse(json.dumps(data), content_type='application/json')
コード例 #2
0
def asset_web_ssh(request):
    cps = ConfParserClass('conf/settings.conf')

    if request.method == 'POST':
        ret = {
            "ip": cps.get('webssh', 'ip'),
            'port': cps.get('webssh', 'port'),
            "username": cps.get('webssh', 'username'),
            'password': cps.get('webssh', 'password'),
            "static": True
        }
        return HttpResponse(json.dumps(ret))
コード例 #3
0
def jenkins_list(request):
    if request.method == 'GET':

        return render(request, 'deploy/jenkins_list.html')
    elif request.method == 'POST':
        cp = ConfParserClass('conf/settings.conf')
        J = JenkinsJob(cp.get('jenkins', 'url'), cp.get('jenkins', 'username'),
                       cp.get('jenkins', 'password'))
        job_names = J.job_list()
        job_list = [J.job_query(j) for j in job_names]
        data = {'total': len(job_list), 'rows': job_list}
        return JsonResponse(data)
コード例 #4
0
def jenkins_manage(request, j_name=None, action=None):
    """
    Manage Jenkins
    """
    if request.user.has_perms(['asset.view_asset', 'asset.edit_asset']):
        cp = ConfParserClass('conf/settings.conf')
        J = JenkinsJob(cp.get('jenkins', 'url'), cp.get('jenkins', 'username'),
                       cp.get('jenkins', 'password'))
        if action == 'add':
            pass
        elif action == 'list':
            if request.method == 'GET':
                page_name = 'Jenkins 管理'

                return render(request, 'deploy/jenkins_list.html',
                              {'page_name': page_name})
            elif request.method == 'POST':
                job_names = J.job_list()
                job_list = [J.job_query(j) for j in job_names]
                data = {'total': len(job_list), 'rows': job_list}
                return JsonResponse(data)

        elif action == 'edit':  # 编辑构建项目
            page_name = '编辑构建项目'
        elif action == 'delete':  # 删除构建项目
            pass
        elif action == 'view':  # 查看构建项目
            """获取console信息"""
            ret = J.job_console(j_name)
            data = {'ret': ret, 'j_name': j_name, 'page_name': '日志详情'}
            return render(request, 'deploy/jenkins_detail.html', data)
        elif action == 'build':  # 开始构建项目
            Message.objects.create(type=u'部署管理',
                                   user=request.user,
                                   action='构建jenkins',
                                   action_ip=UserIP(request),
                                   content=u'JOB名称:{0}'.format(j_name))
            try:
                params = request.POST.get('params', {})
                ret = J.job_build(j_name, params=params)
            except:
                traceback.print_exc()
        return redirect('jenkins_manage', action='list')

    else:
        raise Http404
コード例 #5
0
def remote_execution(request):
    '''
    salt远程命令界面
    '''
    if request.user.has_perm('deploy.view_deploy'):
        if request.method == 'GET':
            tgt_list = ServerAsset.objects.all()
            data = {'tgt_list': tgt_list, 'page_name': '远程命令'}

            return render(request, 'sysadmin/remote_exec.html', data)

        else:
            print(request.POST)
            host = request.POST.getlist('host[]', [])
            # command = request.POST.get('command','')

            # 保险起见,命令先写死
            command = 'ls /root'
            if host and command:
                res = []
                for x in host:
                    cp = ConfParserClass('conf/settings.conf')
                    salt_url = cp.get('saltstack', 'url')
                    salt_username = cp.get('saltstack', 'username')
                    salt_password = cp.get('saltstack', 'password')

                    sapi = SaltApi(url=salt_url,
                                   username=salt_username,
                                   password=salt_password)
                    c = sapi.remote_execution(x, 'cmd.run', command)
                    res.append(c)

                    Message.objects.create(
                        type=u'系统管理',
                        user=request.user,
                        action='远程命令',
                        action_ip=UserIP(request),
                        content=u'command:{0},host:{1}'.format(command, host))
            else:
                res = []

            return JsonResponse(res, safe=False)
    else:
        raise Http404
コード例 #6
0
def aliyun_dns_list(request):
    cps = ConfParserClass('conf/settings.conf')
    client = AcsClient(cps.get('aliyun', 'accessKeyId'), cps.get('aliyun', 'accessSecret'), 'cn-hangzhou')

    req = CommonRequest()
    req.set_accept_format('json')
    req.set_domain('alidns.aliyuncs.com')
    req.set_method('POST')
    req.set_version('2015-01-09')
    req.set_action_name('DescribeDomainRecords')

    req.add_query_param('DomainName', 'dev4ops.cn')
    response = client.do_action_with_exception(req)
    response = str(response, encoding='utf-8')
    response = json.loads(response)['DomainRecords']['Record']

    for x in response:
        dr = {
            'rr': x['RR'],
            'status': x['Status'],
            'value': x['Value'],
            'type': x['Type'],
            'domainname': x['DomainName'],
            'ttl': x['TTL']
        }
        updated_values = {'rr': x['RR']}
        DnsRecords.objects.update_or_create(defaults=updated_values, **dr)

    if request.user.has_perm('asset.view_asset'):
        if request.method == 'GET':
            dr_list = DnsRecords.objects.all()

            data = {

                "page_name": '阿里云域名',
                "all_dns_list": dr_list
            }

            return render(request, 'sysadmin/dns_list.html', data)
    else:
        raise Http404
コード例 #7
0
ファイル: views.py プロジェクト: idber/devops
def db_metadata(request):
    """
    cloud列表、cloud详细
    """

    if request.method == 'GET':
        record_list = list(DBInfo.objects.all().values())
        data = {
            'record_list': record_list,
            'page_name': '数据字典'
            }

        return render(request, 'dba/db_metadata.html', data)

    elif request.method == 'POST':
        cps = ConfParserClass('conf/settings.conf')
        db_id = request.POST.get('db_id', '')
        dbinfo = DBInfo.objects.get(pk=db_id)
        db_username = cps.get('dba', 'username')
        db_pass = cps.get('dba', 'password')
        db_name = dbinfo.db_name

        table_name = request.POST.get('table_name', '')

        table_create = ''
        table_status = {}
        table_index = {}

        data = {
            'status': 0,
            'message': '',
            'table_create': table_create,
            'table_status': table_status,
            'table_index': table_index
        }

        try:
            # 获取表元数据

            with MysqlConn(dbinfo.db_ip, dbinfo.db_port, "information_schema", db_username, db_pass,
                           cursorclass=1) as cur:
                # 获取创建表的语句
                create_sql = """show create table {0}.{1};""".format(db_name, table_name)
                cur.execute(create_sql)
                table_create = cur.fetchone()['Create Table']

                # 获取表统计信息
                status_sql = """
                    SELECT table_schema,
                           table_name,
                           table_type,
                           engine,
                           version,
                           row_format,
                           table_rows,
                           avg_row_length,
                           data_length,
                           max_data_length,
                           index_length,
                           data_free,
                           auto_increment,
                           create_time,
                           update_time,
                           check_time,
                           table_collation,
                           checksum,
                           create_options,
                           table_comment
                    FROM tables
                    WHERE TABLE_schema = '{0}' AND TABLE_NAME = '{1}';
                """.format(db_name, table_name)
                cur.execute(status_sql)
                table_status = cur.fetchone()

                # 获取表索引信息
                index_sql = """
                    SELECT INDEX_NAME,
                           SEQ_IN_INDEX,
                           COLUMN_NAME,
                           COLLATION,
                           CARDINALITY,
                           CASE NON_UNIQUE WHEN 0 THEN 'YES' ELSE 'NO' END AS IS_UNIQUE,
                           NULLABLE,
                           INDEX_TYPE,
                           COLLATION,
                           INDEX_COMMENT
                    FROM STATISTICS
                    WHERE TABLE_schema = '{0}' AND TABLE_NAME = '{1}';
                """.format(db_name, table_name)
                cur.execute(index_sql)
                table_index = cur.fetchall()

                columns_sql = """select column_name, 
                                     column_default, 
                                     is_nullable, 
                                     column_type, 
                                     column_comment, 
                                     column_key, extra 
                                 from information_schema.columns 
                                 where table_schema='{0}' and table_name='{1}';""".format(db_name, table_name)
                cur.execute(columns_sql)
                table_columns = cur.fetchall()

                data['table_create'] = table_create
                data['table_status'] = json.dumps(table_status, cls=DjangoJSONEncoder)
                data['table_index'] = table_index
                data['table_columns'] = table_columns
        except Exception as e:
            traceback.print_exc()
            data['status'] = 1
            data['message'] = '获取表元数据时发生错误.'

        context = {
            'data': data,
            'db_id': db_id,
            'table_name': table_name,
            'page_name': '数据字典明细'
        }
        return render(request, 'dba/db_metadata.html', context)
コード例 #8
0
                'error': None,
            }
            user = request.user.username
            os.environ[user] = "false"
            print(2, os.environ[user])
            Message.objects.create(type=u'系统管理',
                                   user=request.user,
                                   action='web日志',
                                   action_ip=UserIP(request),
                                   content=u'status:{0};error:{1}'.format(
                                       ret['status'], ret['error']))
            return HttpResponse(json.dumps(ret))


cps = ConfParserClass('conf/settings.conf')
sapi = SaltApi(url=cps.get('saltstack', 'url'),
               username=cps.get('saltstack', 'username'),
               password=cps.get('saltstack', 'password'))


@login_required
def salt_key_list(request):
    '''
    salt主机列表
    '''

    if request.user.is_superuser:
        minions = SaltHost.objects.filter(status=True)
        minions_pre = SaltHost.objects.filter(status=False)
        data = {
            'all_minions': minions,
コード例 #9
0
                                   action_ip=UserIP(request),
                                   content=u'status:{0};error:{1}'.format(ret['status'],ret['error']))
            return JsonResponse(ret)
        else:
            ret = {'status': True, 'error': None, }
            user = request.user.username
            os.environ[user] = "false"
            print(2, os.environ[user])
            Message.objects.create(type=u'系统管理', user=request.user, action='web日志',
                                   action_ip=UserIP(request),
                                   content=u'status:{0};error:{1}'.format(ret['status'],ret['error']))
            return HttpResponse(json.dumps(ret))


cps = ConfParserClass('conf/settings.conf')
sapi = SaltApi(url=cps.get('saltstack', 'url'), username=cps.get('saltstack', 'username'),
               password=cps.get('saltstack', 'password'))

@login_required
def salt_key_list(request):
    '''
    salt主机列表
    '''

    if request.user.is_superuser:
        minions = SaltHost.objects.filter(status=True)
        minions_pre = SaltHost.objects.filter(status=False)
        data = {
            'all_minions': minions,
            'all_minions_pre': minions_pre,
            'page_name': 'salt主机列表'
コード例 #10
0
ファイル: scripts.py プロジェクト: xfinQ/devops
def deploy_thread(deploy_id):

    deploy_service = DeploysService(deploy_id)
    deploy = Deploys.objects.get(pk=deploy_id)

    cp = ConfParserClass('conf/settings.conf')
    salt_url = cp.get('saltstack', 'url')
    salt_username = cp.get('saltstack', 'username')
    salt_password = cp.get('saltstack', 'password')

    sapi = SaltApi(url=salt_url,
                   username=salt_username,
                   password=salt_password)

    if deploy.status != 3:
        return
    try:
        deploy_service.update_deploy(progress=0, status=2)
        deploy_service.append_comment("开始部署:\n")

        time.sleep(3)
        # ========= checkouting ==========
        deploy_service.update_deploy(progress=10, status=2)
        deploy_service.append_comment("检出应用程序包。。。\n")
        url = urljoin(settings.NG_FILE_BASE, deploy.project.name,
                      deploy.version)
        url = urljoin(url, deploy.pkg_name)
        time.sleep(1)
        sapi.remote_execution(
            '*', 'cmd.run',
            'wget {0} -o {1}'.format(url, deploy.project.target_path))
        time.sleep(8)
        deploy_service.append_comment("下载完成\n")
        deploy_service.update_deploy(progress=15, status=2)

        # =========== before deploy  =================
        deploy_service.append_comment("准备部署 目标主机: {0}\n".format(
            deploy.host.public_ip))
        cmd = (
            "mkdir -p {remote_history_dir} && chmod -R 777 {remote_history_dir}"
            .format(remote_history_dir=os.path.join(
                deploy.project.remote_history_dir,
                deploy.created_at.strftime('%Y%m%d-%H%M%S'))))

        # execute(hostfab.remoted, rd=cmd, sudoif=1)
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        deploy_service.append_comment("--删除过期的历史备份\n")
        cmd = (
            "WORKSPACE='{0}' && cd $WORKSPACE && ls -1t | tail -n +{1} | xargs rm -rf"
            .format(deploy.project.remote_history_dir,
                    cp.get('deploy', 'backup_count')))
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        time.sleep(8)
        # ========== create dest dirs ===================

        cmd = ("mkdir -p {dest_path} ".format(
            dest_path=deploy.project.deploy_path))
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        # execute(hostfab.remoted, rd=cmd, sudoif=1)

        deploy_service.append_comment("部署结束!\n")
        deploy_service.update_deploy(progress=83, status=2)
        time.sleep(8)
        # =============== after deploy =============
        deploy_service.append_comment("启动服务并将康检查\n")
        deploy_service.append_comment("应用启动正常\n")
        deploy_service.append_comment("完成,结束!\n")

    except BaseException as err:
        traceback.print_exc()

        deploy_service.append_comment(repr(err))
        deploy_service.update_deploy(progress=100, status=0)
    else:
        deploy_service.update_deploy(progress=100, status=1)
    finally:
        if deploy_service:
            deploy_service.start_deploy()
コード例 #11
0
ファイル: scripts.py プロジェクト: idber/devops
def deploy_thread(deploy):
    cp = ConfParserClass('conf/settings.conf')
    deploy_service = DeploysService(deploy.id)
    jenkins_ip = cp.get('jenkins', 'url')
    jenkins_job = deploy.project.jenkins_name
    jenkins_user = cp.get('jenkins', 'user')
    jenkins_pass = base64.b64decode(cp.get('jenkins', 'pass'))
    salt_url = cp.get('saltstack', 'url')
    salt_username = cp.get('saltstack', 'username')
    salt_password = cp.get('saltstack', 'password')

    sapi = SaltApi(url=salt_url,
                   username=salt_username,
                   password=salt_password)

    if deploy.status != 3:
        return
    try:
        deploy_service.update_deploy(progress=0, status=2)

        # =======before checkout========
        deploy_service.update_deploy(progress=5, status=2)
        deploy_service.append_comment(
            "Jenkins 任务:\n--任务名称:{0}\n".format(jenkins_job))
        time.sleep(8)

        # ======== Jenkins ========
        if deploy.jenkins_job.strip() == 'yes':
            deploy_service.append_comment("--拉取代码并编译打包(by git and maven)\n")
            jenkins = JenkinsJob(username=jenkins_user,
                                 password=jenkins_pass,
                                 jobname='{0}'.format(jenkins_job))
            j = jenkins.jobbuild()
            result = j['result']
            num = int(j['num'])
            if not result == 'SUCCESS':
                deploy_service.append_comment("--Jenkins 构建失败\n\n")
                raise Exception("Jenkins")
            else:
                deploy_service.append_comment(
                    "--Jenkins 构建成功\n"
                    "--任务日志请访问:/deploy/code/jenkins/?jobname={0}.{1}&&num={2}\n\n"
                    .format(deploy.branch, jenkins_job, num))
        else:
            deploy_service.append_comment("--Jenkins 任务本次不执行构建\n\n")
        time.sleep(8)
        deploy_service.update_deploy(progress=15, status=2)

        deploy_service.append_comment("开始部署:\n")
        # ========= checkouting ==========
        deploy_service.append_comment("检出应用程序包。。。\n")
        time.sleep(1)
        sapi.remote_execution(
            '*', 'cmd.run',
            'wget http://devops-wctest.chinacloudapp.cn/file_download_manage/test01.war -o /root/test01.war'
        )
        time.sleep(8)
        # =========== before deploy  =================
        deploy_service.append_comment("准备部署 目标主机: {0}\n".format(
            deploy.host.public_ip))
        cmd = (
            "mkdir -p {remote_history_dir} && chmod -R 777 {remote_history_dir}"
            .format(remote_history_dir=os.path.join(
                deploy.project.remote_history_dir,
                deploy.created_at.strftime('%Y%m%d-%H%M%S'))))

        # execute(hostfab.remoted, rd=cmd, sudoif=1)
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        deploy_service.append_comment("--删除过期的历史备份\n")
        cmd = (
            "WORKSPACE='{0}' && cd $WORKSPACE && ls -1t | tail -n +{1} | xargs rm -rf"
            .format(deploy.project.remote_history_dir,
                    cp.get('deploy', 'backup_count')))
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        time.sleep(8)
        # ========== create dest dirs ===================

        cmd = ("mkdir -p {dest_path} ".format(
            dest_path=deploy.project.dest_path))
        sapi.remote_execution(deploy.host.public_ip, 'cmd.run', cmd)
        # execute(hostfab.remoted, rd=cmd, sudoif=1)

        deploy_service.append_comment("部署完成!\n")
        deploy_service.update_deploy(progress=83, status=2)
        time.sleep(8)
        # =============== after deploy =============
        deploy_service.append_comment("启动服务并将康检查\n")
        deploy_service.append_comment("应用启动正常\n")
        deploy_service.append_comment("完成,结束!\n")

    except BaseException as err:
        traceback.print_exc()

        deploy_service.append_comment(repr(err))
        deploy_service.update_deploy(progress=100, status=0)
    else:
        deploy_service.update_deploy(progress=100, status=1)
    finally:
        if deploy_service:
            deploy_service.start_deploy()