Exemple #1
0
def ingress_apply():
    form = MyForm.FormK8sIngress()
    namespace = "default"
    msg = None
    if form.submit.data:
        try:
            domains = form.domains.data
            ingress_port = form.service_port.data
            service_name = form.service_name.data
            context = form.contexts.data
            db_ingress = db_op.k8s_ingress
            config.load_kube_config(config_file, context)
            api_instance = client.ExtensionsV1beta1Api()
            domains = [
                domain.strip() for domain in domains.splitlines() if domain
            ]
            try:
                # ingress信息写入数据库
                for ingress_domain in domains:
                    path = None
                    if '/' in ingress_domain:
                        ingress_domain = ingress_domain.split('/')[0]
                        path = '/{}'.format('/'.join(
                            ingress_domain.split('/')[1:]))
                    v = db_ingress(name='nginx-ingress',
                                   context=context,
                                   namespace=namespace,
                                   domain=ingress_domain,
                                   path=path,
                                   serviceName=service_name,
                                   servicePort=int(ingress_port))
                    db_op.DB.session.add(v)
                    db_op.DB.session.commit()
            except Exception as e:
                logging.error(e)
            else:
                # 从数据库读取ingress信息
                Rules = []
                domain_infos = db_ingress.query.with_entities(
                    distinct(db_ingress.domain)).all()
                domain_infos = [domain[0] for domain in domain_infos]
                for domain in domain_infos:
                    paths = []
                    Rules_infos = db_ingress.query.with_entities(
                        db_ingress.path, db_ingress.serviceName,
                        db_ingress.servicePort).filter(
                            and_(db_ingress.domain == domain,
                                 db_ingress.context == context)).all()
                    for infos in Rules_infos:
                        path, serviceName, servicePort = infos
                        if path:
                            paths.append(
                                client.V1beta1HTTPIngressPath(
                                    client.V1beta1IngressBackend(
                                        service_name=serviceName,
                                        service_port=int(servicePort)),
                                    path=path))
                    if paths:
                        Rules.append(
                            client.V1beta1IngressRule(
                                host=domain,
                                http=client.V1beta1HTTPIngressRuleValue(
                                    paths=paths)))
                    else:
                        path, serviceName, servicePort = Rules_infos[0]
                        Rules.append(
                            client.V1beta1IngressRule(
                                host=domain,
                                http=client.V1beta1HTTPIngressRuleValue(paths=[
                                    client.V1beta1HTTPIngressPath(
                                        client.V1beta1IngressBackend(
                                            service_name=serviceName,
                                            service_port=int(servicePort)))
                                ])))
                spec = client.V1beta1IngressSpec(rules=Rules)
                ingress = client.V1beta1Ingress(
                    api_version='extensions/v1beta1',
                    kind='Ingress',
                    metadata=client.V1ObjectMeta(
                        name='nginx-ingress',
                        namespace=namespace,
                        annotations={'kubernetes.io/ingress.class': 'nginx'}),
                    spec=spec)
                api_instance.patch_namespaced_ingress(body=ingress,
                                                      namespace=namespace,
                                                      name='nginx-ingress')
        except Exception as e:
            logging.error(e)
            msg = 'ingress配置失败!'
        else:
            msg = 'ingress配置完成!'
        finally:
            db_op.DB.session.remove()
    return render_template('k8s_ingress.html', form=form, msg=msg)
Exemple #2
0
def deployment_create():
    tools.Async_log(g.user, request.url)
    reload(MyForm)
    form = MyForm.FormK8sDeploy()
    mounts = defaultdict()
    if form.submit.data:
        context = form.contexts.data
        project = form.projects.data
        version = form.version.data
        object = form.object.data
        container_port = form.container_port.data
        ingress_port = form.ingress_port.data
        replicas = form.replicas.data
        request_cpu = form.request_cpu.data
        limit_cpu = form.limit_cpu.data
        request_mem = form.request_mem.data
        limit_mem = form.limit_mem.data
        domain = form.domain.data
        healthcheck = form.healthcheck.data
        mount_path2 = form.mount_path2.data
        mount_name2 = form.mount_name2.data
        sidecar = form.sidecar.data
        run_args = form.run_args.data
        run_args = run_args.splitlines()
        re_requests = {}
        re_limits = {}
        if mount_path2:
            mounts[mount_path2] = mount_name2
        try:
            if object and version and container_port and replicas:
                if object.endswith('.war') or object.endswith(
                        '.tar.gz') or object.endswith('.jar'):
                    dm_name = object.split('.')[0]
                    image = "%s/%s:%s" % (docker_registry, dm_name, version)
                    docker_file = "%s/%s" % (dockerfile_path, dm_name)
                    if os.path.exists(docker_file):
                        container_port = [
                            int(port) for port in container_port.split(',')
                        ]
                        if request_cpu and limit_cpu and request_mem and limit_mem:
                            if float(request_cpu) > float(limit_cpu) or float(
                                    request_mem) > float(limit_mem):
                                raise flash('限制资源不能小于请求资源!')
                            else:
                                re_requests = {
                                    'cpu': request_cpu,
                                    'memory': '%sG' % request_mem
                                }
                                re_limits = {
                                    'cpu': limit_cpu,
                                    'memory': '%sG' % limit_mem
                                }
                        if domain and not ingress_port:
                            raise flash('域名配置后还需配置容器对外服务端口!')
                        redis_key = 'op_k8s_create_%s' % time.strftime(
                            '%Y%m%d%H%M%S', time.localtime())
                        Scheduler = produce.SchedulerPublish()
                        Scheduler = Scheduler.Scheduler_mem(
                            k8s_resource.object_deploy, [
                                context, project, object, version, image,
                                run_args, container_port, ingress_port,
                                replicas, domain, re_requests, mounts,
                                healthcheck, sidecar, re_limits, redis_key
                            ])
                        Scheduler.start()
                        return render_template('deploy_show.html',
                                               redis_key=redis_key)
                    else:
                        flash("%s文件路径不存在!" % docker_file)
                else:
                    flash("%s包名应以.war或者.tar.gz结尾或者.jar结尾!" % object)
            else:
                flash('必填项参数不完整!')
            return render_template('Message.html')
        except Exception as e:
            logging.error(e)
            return redirect(url_for('error'))
    return render_template('k8s_deploy.html', form=form)
Exemple #3
0
def resource_pool(action=None, id=None):
    importlib.reload(MyForm)
    form = MyForm.MyFromResourcePool()
    form_third = MyForm.MyFromThirdResource()
    db_project = db_op.project_list
    db_server = db_idc.idc_servers
    db_third = db_idc.third_resource
    db_project_third = db_op.project_third
    source_type = 'self'

    def recyle_resource(ip, ssh_port, app_port):
        try:
            project_id = db_project.query.with_entities(db_project.id).filter(
                and_(db_project.ip == ip, db_project.ssh_port == ssh_port,
                     db_project.app_port == app_port)).all()
            third_id = db_third.query.with_entities(db_third.id).filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port,
                     db_third.app_port == app_port)).all()
            if project_id or third_id:
                # 删除自有资源表信息
                v = db_project.query.filter(
                    and_(db_project.ip == ip, db_project.ssh_port == ssh_port,
                         db_project.app_port == app_port)).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                # 删除第三方资源表信息
                v = db_third.query.filter(
                    and_(db_third.ip == ip, db_third.ssh_port == ssh_port,
                         db_third.app_port == app_port)).all()
                for c in v:
                    db_idc.DB.session.delete(c)
                    db_idc.DB.session.commit()
                # 删除项目资源表信息
                if third_id:
                    third_id = third_id[0][0]
                    v = db_project_third.query.filter(
                        db_project_third.third_id == third_id).all()
                    for c in v:
                        db_op.DB.session.delete(c)
                        db_op.DB.session.commit()
                if project_id:
                    project_id = project_id[0][0]
                    v = db_project_third.query.filter(
                        db_project_third.project_id == project_id).all()
                    for c in v:
                        db_op.DB.session.delete(c)
                        db_op.DB.session.commit()
            # 判断该服务器资源不再被使用
            project_vals = db_project.query.filter(
                and_(db_project.ip == ip,
                     db_project.ssh_port == ssh_port)).all()
            third_vals = db_third.query.filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
            if not project_vals and not third_vals:
                # 资源自动回收
                db_server.query.filter(db_server.ip == ip,
                                       db_server.ssh_port == ssh_port).update(
                                           {db_server.status: "未使用"})
                db_idc.DB.session.commit()
        except Exception as e:
            logging.error(e)

    tables = ['项目', '域名', '主机名', '应用服务', '应用端口', '开发语言', '部署环境', '操作']
    third_tables = ['项目', '应用服务', '集群类型', '主机名', '应用端口', '操作']
    #默认项目列表展示
    project = db_project.query.with_entities(distinct(
        db_project.project)).limit(1)
    project = project[0][0]
    values = db_project.query.with_entities(
        db_project.id, db_project.project, db_project.domain, db_project.ip,
        db_project.ssh_port, db_project.resource, db_project.app_port,
        db_project.sys_args,
        db_project.env).filter(db_project.project == project).all()
    third_values = []
    third_indexs = []
    # 按照项目查找
    if tools.http_args(request, 'project'):
        try:
            project = tools.http_args(request, 'project')
            servers = db_server.query.with_entities(db_server.ip,
                                                    db_server.ssh_port,
                                                    db_server.hostname).all()
            servers = {
                '%s:%s' % (info[0], info[1]): info[2]
                for info in servers
            }
            values = db_project.query.with_entities(
                db_project.id, db_project.ip, db_project.ssh_port,
                db_project.resource, db_project.app_port, db_project.sys_args,
                db_project.env).filter(db_project.project == project).order_by(
                    db_project.ip).all()
            if values:
                values = [
                    list(val) for val in values
                    if '%s:%s' % (val[1], val[2]) in servers
                ]
                for val in values:
                    val.insert(3, servers['%s:%s' % (val[1], val[2])])
                values.insert(0, tables[1:])
        except Exception as e:
            logging.error(e)
        try:
            third_id = db_project_third.query.with_entities(
                db_project_third.third_id).filter(
                    db_project_third.project == project).all()
            if third_id:
                third_id = tuple([val[0] for val in third_id])
                third_values = db_third.query.with_entities(
                    db_third.id, db_third.resource_type, db_third.cluster_type,
                    db_third.ip, db_third.ssh_port, db_third.app_port).filter(
                        db_third.id.in_(third_id)).order_by(
                            db_third.resource_type).all()
                if third_values:
                    third_values = [list(val) for val in third_values]
                    for val in third_values:
                        val.insert(5, servers['%s:%s' % (val[3], val[4])])
                    third_values.insert(0, third_tables[1:])
        except Exception as e:
            logging.error(e)
        return render_template('resource_info.html',
                               values=values,
                               third_values=third_values,
                               project=project,
                               third_indexs=third_indexs,
                               form=form)
    # 按照应用服务查找
    if tools.http_args(request, 'application'):
        app = tools.http_args(request, 'application')
        app_id = tools.http_args(request, 'application-id')
        tables = ['应用服务', '集群类型', '主机名', '应用端口']
        host_info = None
        values = []
        servers = db_server.query.with_entities(db_server.ip,
                                                db_server.ssh_port,
                                                db_server.hostname).all()
        servers = {'%s:%s' % (info[0], info[1]): info[2] for info in servers}
        if app_id:
            if app in ('tomcat', 'python', 'php'):
                try:
                    host_info = db_project.query.with_entities(
                        db_project.ip, db_project.ssh_port,
                        db_project.app_port).filter(
                            db_project.id == app_id).all()
                    if host_info:
                        host_info = [
                            servers['%s:%s' %
                                    (host_info[0][0], host_info[0][1])],
                            host_info[0][-1]
                        ]
                        third_id = db_project_third.query.with_entities(
                            db_project_third.third_id).filter(
                                and_(db_project_third.project_id ==
                                     app_id)).all()
                        if third_id:
                            third_id = [val[0] for val in third_id]
                            values = db_third.query.with_entities(
                                db_third.id, db_third.resource_type,
                                db_third.cluster_type, db_third.ip,
                                db_third.ssh_port, db_third.app_port).filter(
                                    db_third.id.in_(tuple(third_id))).order_by(
                                        db_third.resource_type).all()
                            if values:
                                values = [list(val) for val in values]
                except Exception as e:
                    logging.error(e)
            else:
                try:
                    host_info = db_third.query.with_entities(
                        db_third.ip, db_third.ssh_port,
                        db_third.app_port).filter(db_third.id == app_id).all()
                    if host_info:
                        host_info = [
                            servers['%s:%s' %
                                    (host_info[0][0], host_info[0][1])],
                            host_info[0][-1]
                        ]
                        project_id = db_project_third.query.with_entities(
                            db_project_third.project_id).filter(
                                and_(db_project_third.third_id ==
                                     app_id)).all()
                        if project_id:
                            project_id = [val[0] for val in project_id]
                            values = db_project.query.with_entities(
                                db_project.id, db_project.resource,
                                db_project.ip, db_project.ssh_port,
                                db_project.app_port).filter(
                                    db_project.id.in_(
                                        tuple(project_id))).order_by(
                                            db_project.resource).all()
                            if values:
                                values = [list(val) for val in values]
                                for i, vals in enumerate(values):
                                    vals.insert(2, '非集群')
                                    values[i] = vals
                except Exception as e:
                    logging.error(e)
        else:
            try:
                if app in ('php', 'tomcat', 'python'):
                    values = db_project.query.with_entities(
                        db_project.id, db_project.resource, db_project.ip,
                        db_project.ssh_port, db_project.app_port).filter(
                            db_project.resource == app).order_by(
                                db_project.ip).all()
                    if values:
                        values = [list(val) for val in values]
                        for i, vals in enumerate(values):
                            vals.insert(2, '非集群')
                            values[i] = vals
                else:
                    values = db_third.query.with_entities(
                        db_third.id, db_third.resource_type,
                        db_third.cluster_type, db_third.ip, db_third.ssh_port,
                        db_third.app_port).filter(
                            db_third.resource_type == app).order_by(
                                db_third.ip).all()
                    if values:
                        values = [list(val) for val in values]
            except Exception as e:
                logging.error(e)
        if values:
            for val in values:
                if '%s:%s' % (val[3], val[4]) in servers:
                    val.insert(5, servers['%s:%s' % (val[3], val[4])])
        return render_template('application_list.html',
                               values=values,
                               app=app,
                               tables=tables,
                               host_info=host_info,
                               form=form)
    # 第三方资源回收接口
    if tools.http_args(request, 'action') == 'recyle' and tools.http_args(
            request, 'ip') and tools.http_args(
                request, 'ssh_port') and tools.http_args(request, 'app_port'):
        ip = tools.http_args(request, 'ip')
        ssh_port = tools.http_args(request, 'ssh_port')
        app_port = tools.http_args(request, 'app_port')
        result = recyle_resource(ip, ssh_port, app_port)
        if result:
            flash(result)
        else:
            flash('%s %s %s 资源回收完成!' % (ip, ssh_port, app_port))
        return render_template('Message.html')
    #修改项目列表
    if action == 'del' and id:
        try:
            source_type = tools.http_args(request, 'resource_type')
            if tools.http_args(request, 'resource_type') == 'self':
                #修改自有资源列表
                db_project.query.filter(db_project.id == id).update({
                    db_project.project:
                    '',
                    db_project.domain:
                    '',
                    db_project.sys_args:
                    '',
                    db_project.env:
                    '',
                    db_project.gray:
                    '',
                    db_project.status:
                    '未分配',
                    db_project.update_date:
                    time.strftime('%Y-%m-%d', time.localtime())
                })
                db_op.DB.session.commit()
                # 删除项目资源表信息
                v = db_project_third.query.filter(
                    db_project_third.project_id == id).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                project = db_project.query.with_entities(
                    db_project.project).filter(db_project.id == id).all()
                project = project[0][0]
            if tools.http_args(request, 'resource_type') == 'third':
                #删除项目资源表信息
                v = db_project_third.query.filter(
                    db_project_third.third_id == id).all()
                for c in v:
                    db_op.DB.session.delete(c)
                    db_op.DB.session.commit()
                project = db_project_third.query.with_entities(
                    db_project_third.project).filter(
                        db_project_third.third_id == id).all()
                project = project[0][0]
        except Exception as e:
            logging.error(e)
    #资源池预分配
    if form.submit_allot.data:
        servers = form.servers.data
        ip, ssh_port = servers.split(":")
        resource = form.resource.data
        app_port = form.app_port.data.strip()
        if app_port:
            #写入资源表
            if resource in ('php', 'tomcat', 'python', 'java'):
                c = db_project(resource=resource,
                               project='',
                               domain='',
                               ip=ip,
                               ssh_port=ssh_port,
                               app_port=app_port,
                               business_id='',
                               sys_args='',
                               env='',
                               gray='',
                               status='未分配',
                               update_date=time.strftime(
                                   '%Y-%m-%d', time.localtime()))
                db_op.DB.session.add(c)
                db_op.DB.session.commit()
            else:
                c = db_third(resource_type=resource,
                             cluster_type='',
                             ip=ip,
                             ssh_port=ssh_port,
                             app_port=app_port,
                             busi_id=0,
                             department='',
                             person='',
                             contact='',
                             status='未分配',
                             update_date=time.strftime('%Y-%m-%d',
                                                       time.localtime()))
                db_idc.DB.session.add(c)
                db_idc.DB.session.commit()
            flash('%s应用资源预配成功!' % servers)
    #资源池资源加锁
    if form.submit_lcok.data:
        try:
            servers = form.servers.data
            ip, ssh_port = servers.split(":")
            project_vals = db_project.query.filter(
                and_(db_project.ip == ip,
                     db_project.ssh_port == ssh_port)).all()
            third_vals = db_third.query.filter(
                and_(db_third.ip == ip, db_third.ssh_port == ssh_port)).all()
            if project_vals or third_vals:
                db_server.query.filter(
                    and_(db_server.ip == ip,
                         db_server.ssh_port == ssh_port)).update(
                             {db_server.status: '使用中'})
                db_idc.DB.session.commit()
            else:
                raise flash('%s该资源还未分配应用资源,不能锁定!' % servers)
        except Exception as e:
            logging.error(e)
        else:
            flash('%s资源将不能在分配应用服务!' % servers)
    #第三方资源回收
    if form_third.submit_recucle.data:
        hosts = form_third.hosts.data
        if hosts:
            hosts = hosts.splitlines()
            for host in hosts:
                if len(host.split(':')) == 3:
                    ip, ssh_port, app_port = host.split(':')
                    result = recyle_resource(ip, ssh_port, app_port)
                    if result:
                        flash(result)
                    else:
                        flash('%s %s %s 资源回收完成!' % (ip, ssh_port, app_port))
                else:
                    flash('%s格式不符合要求!' % host)
    #项目列表增加主机
    if form.submit_add.data:
        try:
            resource_val = form.hosts_add.data
            project = form.Project.data
            resource_val = resource_val.split(':')
            resource, app_port, ip, ssh_port = resource_val
            vals = db_project.query.with_entities(
                db_project.domain, db_project.sys_args).filter(
                    and_(db_project.project == project,
                         db_project.env == '生产')).limit(1)
            domain, sys_args = vals[0]
            #写入自有资源表
            db_project.query.filter(
                and_(db_project.resource == resource, db_project.ip == ip,
                     db_project.ssh_port == ssh_port,
                     db_project.app_port == app_port).update({
                         db_project.project:
                         project,
                         db_project.domain:
                         domain,
                         db_project.sys_args:
                         sys_args,
                         db_project.env:
                         '生产',
                         db_project.gray:
                         '0',
                         db_project.status:
                         '使用中',
                         db_project.update_date:
                         time.strftime('%Y-%m-%d', time.localtime())
                     }))
            db_op.DB.session.commit()
            #重新加载数据
            values = db_project.query.with_entities(
                db_project.id, db_project.project, db_project.domain,
                db_project.ip, db_project.ssh_port, db_project.resource,
                db_project.app_port, db_project.sys_args,
                db_project.env).filter(db_project.project == project).all()
        except Exception as e:
            logging.error(e)
    #项目列表查询
    if form.submit_query.data:
        source_type = form.source_type.data
        project = form.Project.data
        #数据初始化
        values = []
        try:
            #判断是否需要条件查找
            if source_type == 'self':
                values = db_project.query.with_entities(
                    db_project.id, db_project.project, db_project.domain,
                    db_project.ip, db_project.ssh_port, db_project.resource,
                    db_project.app_port, db_project.sys_args,
                    db_project.env).filter(
                        db_project.project == project).all()
            if source_type == 'third':
                third_id = db_project_third.query.with_entities(
                    db_project_third.third_id).filter(
                        db_project_third.project == project).all()
                if third_id:
                    third_id = [id[0] for id in third_id]
                    third_values = db_third.query.with_entities(
                        db_third.id, db_third.resource_type,
                        db_third.cluster_type, db_third.ip, db_third.ssh_port,
                        db_third.app_port).filter(
                            db_third.id.in_(tuple(third_id))).order_by(
                                db_third.resource_type).all()
            if third_values:
                for i, t_val in enumerate(third_values):
                    t_val = list(t_val)
                    t_val.insert(1, project)
                    third_values[i] = t_val
        except Exception as e:
            flash(e)
    return render_template('resource_pool.html',
                           form=form,
                           form_third=form_third,
                           tables=tables,
                           third_tables=third_tables,
                           values=values,
                           third_values=third_values,
                           source_type=source_type,
                           project=project)
Exemple #4
0
def resource_modify():
    importlib.reload(MyForm)
    form = MyForm.FormResourceModify()
    INFOS = []
    if form.submit.data:
        try:
            resource = form.resource.data
            source_type = form.source_type.data
            action = form.action.data
            hosts = form.hosts.data
            app_port = form.app_port.data
            business = form.select_busi.data
            actions = {'add': '新增', 'del': '删除'}
            if resource and hosts and app_port:
                db_server = db_idc.idc_servers
                db_third = db_idc.third_resource
                db_business = db_op.business
                # 判断是否为第三方资源服务
                if resource.strip() in ('tomcat', 'php', 'python'):
                    raise INFOS.append('录入仅限于第三方资源服务!')
                # 获取业务相关信息
                busi = db_business.query.with_entities(
                    db_business.person, db_business.contact).filter(
                        db_business.id == int(business)).all()
                if busi:
                    person, contact = busi[0]
                else:
                    person = contact = None
                for host in hosts.splitlines():
                    # 判断主机是否存在
                    host = host.strip()
                    infos = db_server.query.with_entities(
                        db_server.ip, db_server.ssh_port).filter(
                            or_(db_server.hostname == host,
                                db_server.ip == host)).all()
                    if infos:
                        ip, ssh_port = infos[0]
                        try:
                            # 增加资源信息操作
                            if action == 'add':
                                c = db_third(resource_type=resource,
                                             cluster_type=source_type,
                                             ip=ip,
                                             ssh_port=ssh_port,
                                             app_port=int(app_port.strip()),
                                             busi_id=int(business),
                                             department='',
                                             person=person,
                                             contact=contact,
                                             status='使用中',
                                             update_date='0000-00-00')
                                db_idc.DB.session.add(c)
                                db_idc.DB.session.commit()
                            # 删除资源信息操作
                            if action == 'del':
                                v = db_third.query.filter(
                                    and_(
                                        db_third.resource_type == resource,
                                        db_third.ip == ip,
                                        db_third.ssh_port == ssh_port,
                                        db_third.app_port == int(
                                            app_port.strip()))).all()
                                if v:
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                else:
                                    raise INFOS.append("%s 没有找到相关资源服务信息" %
                                                       host)
                        except Exception as e:
                            if 'old-style' not in str(e):
                                logging.error(e)
                            if not INFOS:
                                INFOS.append('执行操作发生异常!')
                        else:
                            INFOS.append("%s %s操作执行成功!" %
                                         (host, actions[action]))
                    else:
                        raise INFOS.append('%s 没有找到服务器信息!' % host)
            else:
                INFOS.append('输入框均需要填写信息!')
        except Exception as e:
            if 'old-style' not in str(e):
                logging.error(e)
            if not INFOS:
                INFOS.append('执行操作异常!')
    return render_template('resource_modify.html', form=form, INFOS=INFOS)
Exemple #5
0
def deploy():
    db_project = db_op.project_list
    form = MyForm.MyForm_deploy()
    #判断自有资源
    if form.submit.data:
        project = form.project.data
        domain = form.domain.data
        business_id = form.select_busi.data
        resource = form.select_resource.data
        dev = form.select_dev.data
        if project and resource and domain:
            try:
                val = db_project.query.filter(
                    db_project.project == project.strip()).all()
                if val:
                    raise flash("%s项目已存在,如需修改项目部署列表,请在'资源变更'菜单下操作." % project,
                                'error')
                else:
                    for source in resource:
                        #修改自有资源表
                        source = source.split(':')
                        db_project.query.filter(
                            and_(db_project.resource == source[0],
                                 db_project.ip == source[1],
                                 db_project.ssh_port == source[2],
                                 db_project.app_port == source[3])).update({
                                     db_project.project:
                                     project.strip(),
                                     db_project.domain:
                                     domain.strip(),
                                     db_project.business_id:
                                     int(business_id),
                                     db_project.sys_args:
                                     dev,
                                     db_project.env:
                                     '生产',
                                     db_project.gray:
                                     '0',
                                     db_project.status:
                                     '使用中',
                                     db_project.update_date:
                                     time.strftime('%Y-%m-%d',
                                                   time.localtime())
                                 })
                        db_op.DB.session.commit()
                        # 开始部署环境
                        # 配置负载均衡
                        # 配置dns解析
                        flash('%s:%s:%s:%s添加到项目部署列表成功!' % tuple(source))
            except Exception as e:
                flash(e)
        else:
            flash('所有输入框均为必填项!')
        return render_template('Message.html')
    #判断第三方资源
    if form.submit_third.data:
        db_third = db_idc.third_resource
        department = form.department.data
        person = form.person.data
        contact = form.contact.data
        thirds = form.select_third.data
        if thirds and department and contact and person:
            department = department.strip()
            contact = contact.strip()
            person = person.strip()
            for third in thirds:
                third = third.split(':')
                try:
                    #写入第三方资源表
                    db_third.query.filter(
                        and_(db_third.resource_type == third[0],
                             db_third.ip == third[1],
                             db_third.ssh_port == third[2],
                             db_third.app_port == third[3])).update({
                                 db_third.cluster_type:
                                 '',
                                 db_third.department:
                                 department,
                                 db_third.person:
                                 person,
                                 db_third.contact:
                                 contact,
                                 db_third.status:
                                 '使用中',
                                 db_third.busi_id:
                                 0,
                                 db_third.update_date:
                                 time.strftime('%Y-%m-%d', time.localtime())
                             })
                    db_idc.DB.session.commit()
                except Exception as e:
                    logging.error(e)
                else:
                    flash('%s:%s:%s:%s环境分配录入完成' % tuple(third))
    importlib.reload(MyForm)
    form = MyForm.MyForm_deploy()
    return render_template('deploy.html', form=form)
Exemple #6
0
def publish():
    try:
        tools.Async_log(g.user, request.url)
        importlib.reload(MyForm)
        secret_key = tools.Produce(length=12, chars=string.digits)
        Msg_Key = 'op_publish_msg_%s' % secret_key
        form = MyForm.MyForm_publish()
        if form.submit.data:
            try:
                if Redis.exists(Msg_Key):
                    raise flash('上线操作过于频繁,请稍等%s秒......' % Redis.ttl(Msg_Key))
                package_url = form.package_url.data
                describe = form.describe.data
                package_md5 = form.package_md5.data
                package_type = form.package_type.data
                publish_type = form.publish_type.data
                check_url = form.check_url.data
                restart = form.restart.data
                execute = form.execute.data
                rb_project = form.project.data
                rb_version = form.version.data
                gray = form.gray.data
                if execute == 'rollback':
                    if not rb_project and not rb_version:
                        raise logging.error("choice can not be empty!")
                if execute == 'publish':
                    if not package_url or not package_md5 or not check_url or not describe:
                        raise logging.error("input can not be empty!")
                    Package_name = package_url.split('/')[-1]
                    package_name = Package_name.replace('.zip', '')
                    rb_project = '-'.join(package_name.split('-')[:-1])
                INFOS = {
                    'package_url': package_url,
                    'package_md5': package_md5,
                    'package_type': package_type,
                    'publish_type': publish_type,
                    'user': g.user,
                    'describe': describe.replace('"', '').replace("'", ''),
                    'gray': int(gray),
                    'restart': restart,
                    'execute': execute,
                    'check_url': check_url.replace('https', 'http'),
                    'project': rb_project,
                    'version': rb_version,
                    'channel': 'web',
                    'callback_url': 'None',
                    'token': 'None',
                    'timestamp': int(time.time())
                }
                #启动代码分发控制中心
                Scheduler = produce.Scheduler_publish()
                Scheduler = Scheduler.Scheduler_mem(
                    task_publish.Publish_center, [INFOS, Msg_Key, secret_key])
                Scheduler.start()
                return render_template('publish_show.html',
                                       secret_key=secret_key)
            except Exception as e:
                if 'old-style' not in str(e):
                    logging.error(e)
    except Exception as e:
        logging.error(e)
        return redirect(url_for('error'))
    return render_template('publish.html', form=form)
Exemple #7
0
def nodes_show():
    form = MyForm.FormK8sContexts()
    return render_template('k8s-nodes-show.html',form=form)
Exemple #8
0
def new_business():
    db_business = db_op.business
    db_project = db_op.project_list
    db_project_other = db_op.project_other
    db_servers = db_idc.idc_servers
    form = MyForm.MyForm_deploy()
    Error = []
    Info = []
    BUSI = db_business.query.with_entities(db_business.id,
                                           db_business.business).all()
    BUSI = json.dumps([{
        "id": str(info[0]),
        "text": str(info[1])
    } for info in BUSI])
    if form.submit.data:
        business = form.business.data
        describe = form.describe.data
        person = form.person.data
        contact = form.contact.data
        resource = form.area_resource.data
        dev_type = form.select_dev.data
        project = form.project.data
        domain = form.domain.data
        try:
            if not business:
                raise Error.append("业务信息为不能为空!")
            business_id = db_business.query.with_entities(
                db_business.id).filter(
                    db_business.business == business.strip()).all()
            if not business_id:
                if project:
                    if not project.endswith('.jar'):
                        if not resource or not describe:
                            raise Error.append("带*输入框均为必填项!")
                else:
                    raise Error.append("项目名称为不能为空!")
            #判断主机信息输入格式
            if ',' in resource:
                infos = resource.split(',')
            else:
                infos = resource.splitlines()
            for info in infos:
                #判断是否带应用端口
                if ':' in info:
                    host, app_port = info.split(':')
                else:
                    host = info
                    app_port = None
                #判断是ip还是hostname
                if len(host.split('.')) > 3:
                    ips = db_servers.query.with_entities(
                        db_servers.id, db_servers.ip,
                        db_servers.ssh_port).filter(
                            db_servers.ip == host).all()
                else:
                    ips = db_servers.query.with_entities(
                        db_servers.id, db_servers.ip,
                        db_servers.ssh_port).filter(
                            db_servers.hostname == host).all()
                if ips:
                    server_id, ip, ssh_port = ips[0]
                    if not business_id:
                        #写入业务信息
                        c = db_business(business=business.strip(),
                                        describe=describe.strip(),
                                        person=person,
                                        contact=contact)
                        db_op.DB.session.add(c)
                        db_op.DB.session.commit()
                    #查询业务ID
                    new_id = db_business.query.with_entities(
                        db_business.id).filter(
                            db_business.business == business.strip()).all()
                    new_id = new_id[0][0]
                    update_date = time.strftime('%Y-%m-%d', time.localtime())
                    #判断是否正式项目
                    if app_port:
                        if dev_type == 'java':
                            sys_args = dev_type
                            dev_type = 'tomcat'
                        #写入正式项目数据库
                        exist_project = db_project.query.filter(
                            db_project.project == project).all()
                        if exist_project:
                            Error.append("%s项目信息已存在!" % project)
                        else:
                            #判断域名信息
                            if domain:
                                c = db_project(resource=dev_type,
                                               project=project,
                                               domain=domain,
                                               ip=ip,
                                               ssh_port=ssh_port,
                                               app_port=app_port,
                                               business_id=new_id,
                                               sys_args=sys_args,
                                               env='生产',
                                               gray=0,
                                               status='使用中',
                                               update_date=update_date)
                                db_op.DB.session.add(c)
                                db_op.DB.session.commit
                            else:
                                Error.append("%s新项目需提供域名信息!" % project)
                    else:
                        exist_other = db_project_other.query.filter(
                            db_project_other.project == project).all()
                        if exist_other:
                            db_project_other.query.filter(
                                db_project_other.project == project).update(
                                    {db_project_other.business_id: new_id})
                            db_op.DB.session.commit()
                        else:
                            #写入非正式项目数据库
                            c = db_project_other(lable=dev_type,
                                                 project=project,
                                                 server_id=server_id,
                                                 business_id=new_id,
                                                 update_time=update_date)
                            db_op.DB.session.add(c)
                            db_op.DB.session.commit()
                else:
                    Error.append("%s主机信息没有找到!" % host)
        except Exception as e:
            if 'old-style' not in str(e):
                logging.error(e)
        else:
            Info.append("%s业务相关信息录入完成." % business.strip())
    return render_template('new_business.html',
                           form=form,
                           Error=Error,
                           Info=Info,
                           BUSI=BUSI)
Exemple #9
0
def hpa():
    try:
        reload(MyForm)
        form = MyForm.FormData()
        td = time.strftime('%Y-%m-%d', time.localtime())
        if 'select_date' in request.cookies:
            td = request.cookies['select_date']
        valus = []
        line = None
        db_k8s_deploy = db_op.k8s_deploy
        db_project = db_op.project_list
        Key = 'op_k8s_ingress_log'
        keys = tables = ('name', 'deployment', '最大副本', '最小副本', '当前副本', 'CPU阀值',
                         'CPU当前值', 'QPS当前值', '管理')
        config.load_kube_config(config_file, g.context)
        v1 = client.AutoscalingV1Api()
        ret = v1.list_horizontal_pod_autoscaler_for_all_namespaces()
        for i in ret.items:
            rps = 0
            RPS = []
            try:
                project = db_k8s_deploy.query.with_entities(
                    db_k8s_deploy.project).filter(
                        and_(
                            db_k8s_deploy.deployment ==
                            i.spec.scale_target_ref.name, db_k8s_deploy.context
                            == g.context)).limit(1).all()
                if project:
                    domains = db_project.query.with_entities(
                        db_project.domain).filter(
                            db_project.project == project[0][0]).limit(
                                1).all()
                    if domains:
                        domains = [domain[0] for domain in domains if domain]
                        if domains[0]:
                            for domain in domains[0].split(','):
                                vals = RC.hgetall('%s_%s_%s' %
                                                  (Key, domain, td))
                                vals = sorted(vals.items(),
                                              key=lambda item: item[0])
                                if vals:
                                    RPS.append(int(int(vals[-1][-1]) / 60))
                            if RPS:
                                rps = RPS[0]
                                if len(RPS) > 1:
                                    rps = reduce(lambda x, y: x + y, RPS)
            except Exception as e:
                logging.error(e)
            try:
                valus.append([
                    i.metadata.name, i.spec.scale_target_ref.name,
                    i.spec.max_replicas, i.spec.min_replicas,
                    i.status.current_replicas,
                    '{0}%'.format(i.spec.target_cpu_utilization_percentage),
                    '{0}%'.format(i.status.current_cpu_utilization_percentage),
                    rps
                ])
            except Exception as e:
                logging.error(e)
        if valus:
            Key = 'op_hpa_chart_%s_%s' % (g.context, td)
            infos = RC.hgetall(Key)
            infos = sorted(infos.items(),
                           key=lambda item: item[0].split('_')[-1])
            line = Line('HPA动态伸缩实时状态',
                        width='110%',
                        height='250px',
                        title_pos='8%',
                        title_text_size=14)
            for project in valus:
                attr = []
                vals = []
                for info in infos:
                    if project[0] in info[0]:
                        attr.append(str(info[0].split('_')[-1]))
                        vals.append(int(info[1]))
                if attr and vals:
                    line.add(
                        project[0],
                        attr,
                        vals,
                        is_toolbox_show=False,
                        is_smooth=True,
                        mark_point=["max", "min"],
                        mark_point_symbolsize=60,
                        legend_pos='20%',
                        is_datazoom_show=True,
                        datazoom_range=[v for v in range(100, 10)],
                        datazoom_type='both',
                    )
        return render_template('k8s-resource.html',
                               valus=valus,
                               tables=tables,
                               keys=keys,
                               line=line,
                               form=form,
                               resource='HPA')
    except Exception as e:
        logging.error(e)
Exemple #10
0
def assets():
    try:
        form = MyForm.MyForm_server()
        tables=('机柜数量','物理机数量','虚拟机数量')
        ns = ('网络设备数量','存储设备数量','应用部署情况')
        phosts=[]
        vhosts=[]
        networks = []
        stores = []
        total = []
        #获取机房机柜信息
        db_idc_id = db_idc.idc_id
        db_hosts = db_idc.idc_servers
        values = db_idc_id.query.with_entities(db_idc_id.aid, func.count(db_idc_id.cid)).group_by(db_idc_id.aid).all()
        values = [list(val) for val in values]
        c_val = db_idc_id.query.with_entities(func.count(db_idc_id.cid)).filter(~db_idc_id.cid.in_(('KVM','OSS',''))).all()
        p_val =  db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(db_hosts.host_type=='physical').all()
        v_val = db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(db_hosts.host_type == 'vm').all()
        e_val = db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(and_(db_hosts.host_type=='physical',db_hosts.expird_date < tt)).all()
        w_val = db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(and_(db_hosts.host_type=='physical',db_hosts.expird_date >= tt,db_hosts.expird_date <= dt)).all()
        try:
            total.append(len(values))
            total.append(int(c_val[0][0]))
            total.append(int(p_val[0][0]))
            if e_val:
                total.append(int(e_val[0][0]))
            else:
                total.append(0)
            if w_val:
                total.append(int(w_val[0][0]))
            else:
                total.append(0)
            total.append(int(v_val[0][0]))
            Key = "op_disconnet_assets_count"
            d_val = Redis.smembers(Key)
            if d_val:
                total.append(len(d_val))
            else:
                total.append(0)
        except Exception as e:
            logging.error(e)
        for val in values:
            try:
                #获取指定机房机柜的服务器信息
                idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(db_idc_id.aid==val[0]).all()
                idc_id = tuple([id[0] for id in idc_id])
                #统计物理服务器数量
                phost_count = db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(and_(db_hosts.host_type=='physical',db_hosts.idc_id.in_(idc_id))).all()
                phosts.append(phost_count)
                #获取虚拟机数量
                vhost_count = db_hosts.query.with_entities(func.count(db_hosts.ip)).filter(and_(db_hosts.host_type=='vm',db_hosts.idc_id.in_(idc_id))).all()
                vhosts.append(vhost_count)
                # 获取网络设备和存储设备、附属设备
                db_network = db_idc.idc_networks
                db_store = db_idc.idc_store
                network_count = db_network.query.with_entities(func.count(db_network.ip)).filter(db_network.idc_id.in_(idc_id)).all()
                networks.append(network_count)
                store_count = db_store.query.with_entities(func.count(db_store.ip)).filter(db_store.idc_id.in_(idc_id)).all()
                stores.append(store_count)
            except Exception as e:
                logging.error(e)
        #信息进行合并
        try:
            phosts=[host[0][0] for host in phosts]
            vhosts = [host[0][0] for host in vhosts]
            networks = [host[0][0] for host in networks]
            stores = [host[0][0] for host in stores]
            for i,val in enumerate(values):
                if int(vhosts[i]) > 0:
                    val[1] = int(val[1]) -1
                if int(phosts[i]) == 0:
                    val[1] = 0
                val.append(phosts[i])
                val.append(vhosts[i])
                val.append(networks[i])
                val.append(stores[i])
                val.append('查看')
                values[i] = val
        except Exception as e:
            logging.error(e)
        return render_template('assets.html', values=values, tables=tables, ns=ns, form=form,total=total)
    except Exception as e:
        logging.error(e,"error")
        flash('获取数据错误!',"error")
        return render_template('Message.html')
Exemple #11
0
def assets_get(action=None):
    #公共参数
    Args = {info:tools.http_args(request,info) for info in ('aid','ip','port','type','host_type','action','page','hostname')}
    search_key = 'search_results_%s' % g.token
    form = MyForm.MyForm_server()
    db = db_idc.idc_servers
    db_idc_id = db_idc.idc_id
    db_zabbix = db_idc.zabbix_info
    idc_vals = db_idc_id.query.with_entities(db_idc_id.id, db_idc_id.aid,db_idc_id.cid).all()
    idc_val = {val[0]: val[1] for val in idc_vals}
    cid_val = {val[0]: val[-1] for val in idc_vals}
    values = []
    tables = ['机房','机柜','IP', 'ssh端口', '主机名', '服务器型号', '操作系统', 'CPU', '内存', '磁盘数', '磁盘总量', '远程管理IP','购买日期','状态']
    try:
        # 导出数据功能
        if action:
            if action == 'export':
                try:
                    file_name = "/tmp/export_assets.xlsx"
                    values = [list(val) for val in eval(Redis.get(search_key))]
                    if os.path.exists(file_name):
                        os.remove(file_name)
                    pyexcel.save_as(array=values, dest_file_name=file_name,sheet_name='export_assets')
                except Exception as e:
                    logging.error(e)
                else:
                    return send_file(file_name, as_attachment=True)
        # 判断是否查询
        if form.submit.data:
            ts = form.text.data.strip()
            Infos = {'ip': db.ip, 'sn': db.sn, 'hostname': db.hostname, 'status': db.status}
            if ts:
                try:
                    if form.select.data == 'cid':
                        cid = ts.strip()
                        if not ts.endswith('机柜') and cid != 'KVM':
                            cid = '%s机柜'%ts.strip()
                        # 优先进行精确匹配
                        idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(db_idc_id.cid == cid).all()
                        if not idc_id:
                            # 精确匹配不到结果后进行模糊匹配
                            idc_id = db_idc_id.query.with_entities(db_idc_id.id).filter(db_idc_id.cid.like('%{0}%'.format(cid))).all()
                        idc_id = tuple([id[0] for id in idc_id])
                        values = db.query.with_entities(db.idc_id, db.ip, db.ssh_port, db.hostname,
                                                        db.productname, db.system, db.cpu_core, db.mem, db.disk_count,
                                                        db.disk_size, db.idrac,db.purch_date, db.status).filter(db.idc_id.in_(idc_id)).all()
                    if form.select.data == 'buy_date':
                        start_time = ts.split('to')[0].strip()
                        end_time = ts.split('to')[-1].strip()
                        values = db.query.with_entities(db.idc_id, db.ip, db.ssh_port, db.hostname,
                                                            db.productname, db.system, db.cpu_core, db.mem,
                                                            db.disk_count, db.disk_size, db.idrac, db.purch_date,db.status).filter(and_(db.purch_date>=start_time,db.purch_date <= end_time,db.host_type=='physical')).all()
                    if form.select.data in ('sn','hostname','ip','status'):
                        val = db.query.with_entities(db.ip,db.ssh_port).filter(Infos[form.select.data] == ts).all()
                        #优先进行精确匹配
                        if val and len(val) == 1:
                            ip,ssh_port = val[0]
                            return redirect("assets_get?type=server&ip=%s&port=%s"%(ip,ssh_port))
                        else:
                            #精确匹配不到结果后进行模糊匹配
                            values = db.query.with_entities(db.idc_id, db.ip, db.ssh_port, db.hostname,
                                                                db.productname, db.system, db.cpu_core, db.mem,
                                                                db.disk_count, db.disk_size, db.idrac,db.purch_date, db.status).filter(Infos[form.select.data].like('%{0}%'.format(ts))).all()
                except Exception as e:
                    logging.error(e)
                else:
                    try:
                    #获取服务器信息
                        if values:
                            values = [list(val) for val in values]
                            for val in values:
                                id = val[0]
                                val[0] = idc_val[id]
                                val.insert(1,cid_val[id])
                            export_values = [val for val in values]
                            export_values.insert(0, tables)
                            Redis.set(search_key, export_values)
                    except Exception as e:
                        logging.error(e)
                    return render_template('server_list.html', values=values, tables=tables,form=form,export=True,assets_type = 'server')
        #获取API接口参数
        if Args:
            try:
                if Args['aid']:
                    aid = Args['aid']
                    idc_ids = db_idc_id.query.with_entities(db_idc_id.id).filter(db_idc_id.aid == aid).all()
                    if idc_ids:
                        idc_ids = tuple([val[0] for val in idc_ids])
                else:
                    idc_ids = db_idc_id.query.with_entities(db_idc_id.id).all()
                    if idc_ids:
                        idc_ids = tuple([val[0] for val in idc_ids])
            except Exception as e:
                logging.error(e)
            #判断是否为服务器
            if Args['type']=='server':
                if Args['action']:
                    try:
                        action = Args['action']
                        if action == 'all_list':
                            if Args['host_type']:
                                host_type = Args['host_type']
                                if idc_ids:
                                    values = db.query.with_entities(db.idc_id,db.ip, db.ssh_port, db.hostname, db.productname,db.system, db.cpu_core, db.mem,db.disk_count,db.disk_size,db.idrac,db.purch_date,db.status).filter(and_(db.idc_id.in_(idc_ids), db.host_type == host_type)).all()
                        if action == 'expire':
                            values = db.query.with_entities(db.idc_id,db.ip, db.ssh_port, db.hostname, db.productname,
                                                            db.system, db.cpu_core, db.mem, db.disk_count,db.disk_size, db.idrac,db.purch_date,
                                                            db.status).filter(
                                and_(db.host_type == 'physical', db.expird_date < tt,db.idc_id !=0)).order_by(db.idc_id).all()
                        if action == 'about_to':
                            values = db.query.with_entities(db.idc_id,db.ip, db.ssh_port, db.hostname, db.productname,
                                                            db.system, db.cpu_core, db.mem, db.disk_count,db.disk_size, db.idrac,db.purch_date,
                                                            db.status).filter(
                                and_(db.host_type == 'physical', db.expird_date >= tt,db.expird_date <= dt,db.idc_id !=0)).order_by(db.idc_id).all()
                        if action == 'search':
                            if Redis.exists(search_key):
                                values = eval(Redis.get(search_key))
                        if values:
                            Redis.set(search_key, values)
                            values = [list(val) for val in values]
                            for val in values:
                                id = val[0]
                                val[0] = idc_val[id]
                                val.insert(1,cid_val[id])
                    except Exception as e:
                        logging.error(e)
                    return render_template('server_list.html', values=values, tables=tables,form=form,export=True,assets_type = Args['type'])
                if (Args['ip'] and Args['port']) or Args['hostname']:
                    try:
                        total_infos = defaultdict()
                        ip = Args['ip']
                        ssh_port = Args['port']
                        hostname = Args['hostname']
                        total_infos['ssh_port'] = ssh_port
                        #获取服务器硬件信息
                        db_server = db_idc.idc_servers
                        if ip and ssh_port:
                            total_infos['ip'] = ip
                            server_info = db_server.query.with_entities(db_server.idc_id,db_server.ip,db_server.ssh_port,db_server.s_ip,db_server.host_type,db_server.hostname,
                                                                    db_server.sn,db_server.manufacturer,db_server.productname,db_server.system,db_server.cpu_info,
                                                                    db_server.cpu_core,db_server.mem,db_server.disk_count,db_server.disk_size,db_server.idrac,db_server.purch_date,db_server.expird_date,
                                                                    db_server.status,db_server.comment).filter(and_(db_server.ip==ip,db_server.ssh_port==ssh_port)).all()
                        if hostname:
                            total_infos['ip'] = hostname
                            server_info = db_server.query.with_entities(db_server.idc_id,db_server.ip,db_server.ssh_port,db_server.s_ip,db_server.host_type,db_server.hostname,
                                                                    db_server.sn,db_server.manufacturer,db_server.productname,db_server.system,db_server.cpu_info,
                                                                    db_server.cpu_core,db_server.mem,db_server.disk_count,db_server.disk_size,db_server.idrac,db_server.purch_date,db_server.expird_date,
                                                                    db_server.status,db_server.comment).filter(db_server.hostname==hostname).all()
                            ip = server_info[0][1]
                            ssh_port = int(server_info[0][2])
                    except Exception as e:
                        logging.error(e)
                    if server_info:
                        try:
                            server_info = list(server_info[0])
                            # 获取服务器机房机柜信息
                            idc_info = db_idc_id.query.with_entities(db_idc_id.aid,db_idc_id.cid).filter(db_idc_id.id==int(server_info[0])).all()
                            server_info.pop(0)
                            if idc_info:
                                server_info.insert(0,idc_info[0][1])
                                server_info.insert(0, idc_info[0][0])
                            else:
                                server_info.insert(0,None)
                                server_info.insert(0,None)
                            table_info = ['机房','机柜','IP','SSH_PORT','附属ip','主机类型','hostname','sn','生产厂家','服务器型号','操作系统','cpu信息','cpu核数','内存','磁盘数','磁盘总量','idrac','采购日期','过保日期','状态','管理','备注']
                            total_infos['server_info'] = [table_info,server_info]
                        except Exception as e:
                            logging.error(e)
                        try:
                            tt = datetime.datetime.now() - datetime.timedelta(minutes=15)
                            tt = tt.strftime('%Y-%m-%d %H:%M:%S')
                            zabbix_infos =[0,0,0,0,0]
                            vals = db_zabbix.query.with_entities(db_zabbix.icmpping, db_zabbix.cpu_load,
                                                                 db_zabbix.mem_use, db_zabbix.disk_io,
                                                                 db_zabbix.openfile).filter(
                                and_(db_zabbix.ip == server_info[2], db_zabbix.ssh_port == server_info[3],db_zabbix.update_time>tt)).all()
                            if vals:
                                zabbix_infos = [float(val) for val in list(vals[0])]
                            total_infos['zabbix_infos'] = zabbix_infos
                        except Exception as e:
                            logging.error(e)
                        try:
                            # 获取第三方资源信息
                            third_table = ['应用服务', '应用端口', '所属项目', '集群类型', '业务使用','所属部门','负责人', '联系方式']
                            project_table = ['应用服务', '应用端口', '所属项目', '域名', '开发语言', '环境', '状态','所属业务']
                            total_infos['pool_project'] = True
                            db_third = db_idc.third_resource
                            db_project = db_op.project_list
                            db_busi = db_op.business
                            db_project_third = db_op.project_third
                            busis = db_busi.query.with_entities(db_busi.id,db_busi.business).all()
                            busis = {int(busi[0]):busi[1] for busi in busis}
                            busis[0] = '未知业务'
                            project_third = db_project_third.query.with_entities(db_project_third.third_id,db_project_third.project).all()
                            if project_third:
                                project_third = {info[0]:info[1] for info in project_third}
                            third_info = db_third.query.with_entities(db_third.id,db_third.resource_type, db_third.app_port, db_third.cluster_type,db_third.busi_id ,db_third.department, db_third.person,db_third.contact).filter(
                                and_(db_third.ip == ip, db_third.ssh_port == ssh_port,)).all()
                            if third_info:
                                third_info = [list(info) for info in third_info]
                                third_id= [info[0] for info in third_info]
                                for i,info in enumerate(third_info):
                                    info = list(info)
                                    info[4] = busis[int(info[4])]
                                    if project_third:
                                        if third_id[i] in project_third.keys():
                                            info.insert(3,project_third[third_id[i]])
                                        else:
                                            info.insert(3,'')
                                    else:
                                        info.insert(3, '')
                                    third_info[i] = info
                                third_info.insert(0, third_table)
                                total_infos['third_info'] = third_info
                        except Exception as e:
                            logging.error(e)
                        try:
                            #获取自有资源信息
                            project_info = db_project.query.with_entities(db_project.id,db_project.resource,db_project.app_port,db_project.project,db_project.domain,db_project.sys_args,db_project.env,db_project.status,db_project.business_id).filter(and_(db_project.ip==ip,db_project.ssh_port==ssh_port)).all()
                            project_info = [list(info) for info in project_info]
                            if project_info:
                                for info in project_info:
                                    business = db_busi.query.with_entities(db_busi.business).filter(db_busi.id == int(info[-1])).all()
                                    info[-1] = '%s:%s'%(info[-1],business[0][0])
                                project_info.insert(0,project_table)
                                total_infos['project_info'] = project_info
                        except Exception as e:
                            logging.error(e)
                    return render_template('server_infos.html',total_infos=total_infos)
            #判断是否是存储设备
            try:
                if Args['type'] == 'store' and Args['action']:
                    db_store = db_idc.idc_store
                    tables = ('机房','机柜','设备型号', 'ip', '购买日期', '过保日期', '状态', '备注')
                    if idc_ids:
                        val = db_store.query.with_entities(db_store.idc_id,db_store.type,db_store.ip,db_store.purch_date,
                                                           db_store.expird_date,db_store.status,
                                                           db_store.comment).filter(db_store.idc_id.in_(idc_ids)).all()
                        values = [list(va) for va in val]
                        for value in values:
                            idc_id = int(value[0])
                            cid = db_idc_id.query.with_entities(db_idc_id.aid,db_idc_id.cid).filter(db_idc_id.id==idc_id).all()
                            value.pop(0)
                            value.insert(0,cid[0][1])
                            value.insert(0, cid[0][0])
            except Exception as e:
                logging.error(e)
            #判断是否是网络设备
            try:
                if Args['type'] == 'network' and Args['action']:
                    db_network = db_idc.idc_networks
                    tables = ('机房','机柜','设备型号', 'ip', '冗余', '购买日期', '过保日期', '状态', '备注')
                    if idc_ids:
                        val = db_network.query.with_entities(db_network.idc_id,
                                                             db_network.type,db_network.ip,db_network.redundance,
                                                             db_network.purch_date,db_network.expird_date,db_network.status,
                                                             db_network.comment).filter(db_network.idc_id.in_(idc_ids)).all()
                        values = [list(va) for va in val]
                        for value in values:
                            idc_id = int(value[0])
                            cid = db_idc_id.query.with_entities(db_idc_id.aid,db_idc_id.cid).filter(db_idc_id.id==idc_id).all()
                            value.pop(0)
                            value.insert(0,cid[0][1])
                            value.insert(0, cid[0][0])
            except Exception as e:
                logging.error(e)
    except Exception as e:
        logging.error(e)
        flash('获取数据错误!',"error")
        return render_template('Message.html')
    return render_template('server_list.html', values=values, tables=tables, form=form, export=False,assets_type = Args['type'])
Exemple #12
0
def assets_manage():
    form = MyForm.MyForm_assets_manage()
    db_idc_id = db_idc.idc_id
    db_server = db_idc.idc_servers
    db_network = db_idc.idc_networks
    db_store = db_idc.idc_store
    db_third = db_idc.third_resource
    db_project = db_op.project_list
    db_zabbix = db_idc.zabbix_info
    db_project_third = db_op.project_third
    change_infos = defaultdict()
    if tools.http_args(request, 'mid') == 'assets_manage':
        if tools.http_args(request, 'aid') and tools.http_args(
                request, 'sid') and tools.http_args(request, 'action'):
            if ':' in tools.http_args(request, 'sid'):
                js_ip, js_ssh_port = tools.http_args(request, 'sid').split(':')
                vals = db_server.query.with_entities(
                    db_server.idc_id, db_server.idrac, db_server.purch_date,
                    db_server.expird_date).filter(
                        and_(db_server.ip == js_ip,
                             db_server.ssh_port == js_ssh_port)).all()
                if vals:
                    idc_id, idrac, purch_date, expird_date = vals[0]
                    cids = db_idc_id.query.with_entities(
                        db_idc_id.aid,
                        db_idc_id.cid).filter(db_idc_id.id == idc_id).all()
                    if cids:
                        aid, cid = cids[0]
                        change_infos['aid'] = aid
                        change_infos['cid'] = cid
                        change_infos['idrac'] = idrac
                        change_infos['purch_date'] = purch_date
                        change_infos['expird_date'] = expird_date
                        change_infos['sid'] = tools.http_args(request, 'sid')
            else:
                change_infos['aid'] = tools.http_args(request, 'aid')
                change_infos['sid'] = tools.http_args(request, 'sid')
            change_infos['action'] = tools.http_args(request, 'action')
    if form.submit.data:
        try:
            aid = form.select_aid.data
            ips = form.text.data.strip()
            ips = set(ips.splitlines())
            rack = form.rack.data.strip()
            if rack:
                if not rack.endswith('机柜') and rack != 'KVM':
                    rack = '%s机柜' % rack
                rack.upper()
            action = form.select_action.data
            purch = form.purch.data
            expird = form.expird.data
            device = form.select_device.data
            idrac = form.idrac.data.strip()
            device_type = form.device_type.data.strip()
            fault = form.fault.data
            old_host = form.old_host.data
            #新增设备
            if action == 'add':
                if ips:
                    for info in ips:
                        if info:
                            if rack and purch and expird:
                                host_type = 'physical'
                                if rack == 'KVM':
                                    host_type = 'vm'
                                # 获取机房机柜信息
                                idc_id = db_idc_id.query.with_entities(
                                    db_idc_id.id).filter(
                                        and_(db_idc_id.aid == aid,
                                             db_idc_id.cid == rack)).all()
                                if not idc_id:
                                    c = db_idc_id(aid=aid, cid=rack)
                                    db_idc.DB.session.add(c)
                                    db_idc.DB.session.commit()
                                    idc_id = db_idc_id.query.with_entities(
                                        db_idc_id.id).filter(
                                            and_(db_idc_id.aid == aid,
                                                 db_idc_id.cid == rack)).all()
                                idc_id = int(idc_id[0][0])
                                #判断是否为服务器
                                if device == 'server':
                                    if ':' in info:
                                        if len(info.split(':')) == 2:
                                            ip, ssh_port = info.split(':')
                                        else:
                                            raise flash('%s格式不符合要求!' % info)
                                    else:
                                        ip, ssh_port = (info.strip(), 20443)
                                    #查询资产信息
                                    values = db_server.query.filter(
                                        and_(db_server.ip == ip,
                                             db_server.ssh_port ==
                                             ssh_port)).all()
                                    if values:
                                        #获取自动发现信息
                                        discovery_values = db_server.query.filter(
                                            and_(
                                                db_server.ip == ip,
                                                db_server.ssh_port == ssh_port,
                                                db_server.status ==
                                                '新发现')).all()
                                        #修改自动发现信息
                                        if discovery_values:
                                            if idc_id:
                                                db_server.query.filter(
                                                    and_(
                                                        db_server.ip == ip,
                                                        db_server.ssh_port ==
                                                        ssh_port,
                                                        db_server.status ==
                                                        '新发现')
                                                ).update({
                                                    db_server.idc_id:
                                                    idc_id,
                                                    db_server.status:
                                                    '未使用',
                                                    db_server.host_type:
                                                    host_type,
                                                    db_server.purch_date:
                                                    purch,
                                                    db_server.expird_date:
                                                    expird,
                                                    db_server.idrac:
                                                    idrac
                                                })
                                                db_idc.DB.session.commit()
                                            flash('%s服务器上架成功!' % info)
                                        else:
                                            flash('%s服务器已存在!' % info)
                                    else:
                                        #添加资产信息
                                        if tcpping(host=ip,
                                                   port=ssh_port,
                                                   timeout=3):
                                            s = db_server(
                                                idc_id=idc_id,
                                                ip=ip,
                                                ssh_port=ssh_port,
                                                idrac=idrac,
                                                purch_date=purch,
                                                expird_date=expird,
                                                status='未使用',
                                                s_ip=None,
                                                host_type=host_type,
                                                hostname='',
                                                sn='',
                                                manufacturer='',
                                                productname='',
                                                system='',
                                                cpu_info='',
                                                cpu_core='',
                                                mem='',
                                                disk_count=0,
                                                disk_size='',
                                                comment='',
                                            )
                                            db_idc.DB.session.add(s)
                                            db_idc.DB.session.commit()
                                            flash('%s服务器上架成功!' % info)
                                        else:
                                            flash(
                                                '运维平台尝试登录{0}的ssh端口失败,服务器不能上架!'.
                                                format(info))
                                #判断是否为网络设备
                                if device == 'network':
                                    if device_type and purch and expird:
                                        ip = info
                                        #查询资产信息
                                        values = db_network.query.filter(
                                            and_(db_network.ip == ip,
                                                 db_network.idc_id ==
                                                 idc_id)).all()
                                        if values:
                                            raise flash('%s网络设备资产已存在!' % info)
                                        #添加资产信息
                                        v = db_network(idc_id=idc_id,
                                                       type=device_type,
                                                       ip=ip,
                                                       redundance='是',
                                                       purch_date=purch,
                                                       expird_date=expird,
                                                       status='使用中',
                                                       comment='')
                                        db_idc.DB.session.add(v)
                                        db_idc.DB.session.commit()
                                        flash('%s网络设备上架成功!' % ip)
                                    else:
                                        raise flash('上架网络设备,设备型号不能为空!')
                                #判断是否为存储设备
                                if device == 'store':
                                    if device_type:
                                        ip = info
                                        #获取资产信息
                                        values = db_store.query.filter(
                                            and_(db_store.ip == ip,
                                                 db_store.idc_id ==
                                                 idc_id)).all()
                                        if values:
                                            raise flash('%s存储设备资产已存在!' % info)
                                        #添加资产信息
                                        v = db_store(idc_id=idc_id,
                                                     type=device_type,
                                                     ip=ip,
                                                     purch_date=purch,
                                                     expird_date=expird,
                                                     status='使用中',
                                                     comment='')
                                        db_idc.DB.session.add(v)
                                        db_idc.DB.session.commit()
                                        flash('%s存储设备上架成功!' % ip)
                                    else:
                                        raise flash('上架存储设备,设备型号不能为空!')
                            else:
                                raise flash('必填项目不能为空!', 'error')
            #下架设备
            if action == 'down':
                if ips:
                    for info in ips:
                        if info:
                            idc_id = db_idc_id.query.with_entities(
                                db_idc_id.id).filter(
                                    db_idc_id.aid == aid).all()
                            idc_id = tuple([id[0] for id in idc_id])
                            #判断为服务器删除
                            if device == 'server':
                                if ':' in info:
                                    if len(info.split(':')) == 2:
                                        ip, ssh_port = info.split(':')
                                    else:
                                        raise flash('%s格式不符合要求!' % info)
                                else:
                                    ip, ssh_port = (info, 20443)
                                server_values = db_server.query.filter(
                                    and_(db_server.ip == ip,
                                         db_server.ssh_port == ssh_port,
                                         db_server.idc_id.in_(idc_id))).all()
                                if server_values:
                                    #判断服务器是否在使用
                                    project_values = db_project.query.filter(
                                        and_(
                                            db_project.ip == ip,
                                            db_project.ssh_port == ssh_port,
                                            db_project.status == '使用中')).all()
                                    third_values = db_third.query.filter(
                                        and_(db_third.ip == ip,
                                             db_third.ssh_port == ssh_port,
                                             db_third.status == '使用中')).all()
                                    if project_values or third_values:
                                        raise flash('%s分配有应用资源,不能下架!' % info)
                                    #删除自有资源信息
                                    v = db_project.query.filter(
                                        and_(db_project.ip == ip,
                                             db_project.ssh_port ==
                                             ssh_port)).all()
                                    for c in v:
                                        db_op.DB.session.delete(c)
                                        db_op.DB.session.commit()
                                    # 删除第三方资源信息
                                    v = db_third.query.filter(
                                        and_(db_third.ip == ip,
                                             db_third.ssh_port ==
                                             ssh_port)).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    # 删除zabbix信息
                                    v = db_zabbix.query.filter(
                                        and_(db_zabbix.ip == ip,
                                             db_zabbix.ssh_port ==
                                             ssh_port)).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    # 删除服务器信息
                                    v = db_server.query.filter(
                                        and_(db_server.ip == ip,
                                             db_server.ssh_port == ssh_port,
                                             db_server.idc_id.in_(
                                                 idc_id))).all()
                                    for c in v:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s服务器下架成功!' % info)
                                else:
                                    flash('%s资产没有找到!' % info)
                            else:
                                ip = info
                                #查询是否为网络设备
                                v_network = db_network.query.filter(
                                    and_(db_network.ip == ip,
                                         db_network.idc_id.in_(idc_id))).all()
                                v_store = db_store.query.filter(
                                    and_(db_store.ip == ip,
                                         db_store.idc_id.in_(idc_id))).all()
                                if v_network:
                                    for c in v_network:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s网络设备下架成功!' % info)
                                #查询是否为存储设备
                                elif v_store:
                                    for c in v_store:
                                        db_idc.DB.session.delete(c)
                                        db_idc.DB.session.commit()
                                    flash('%s存储设备下架成功!' % info)
                                else:
                                    flash('%s资产没有找到!' % info)
            #修改设备信息
            if action == 'modify':
                if ips:
                    for info in ips:
                        if info:
                            #修改服务器信息
                            if device == 'server':
                                if ':' in info:
                                    if len(info.split(':')) == 2:
                                        ip, ssh_port = info.split(':')
                                    else:
                                        raise flash('%s格式不符合要求!' % info)
                                else:
                                    ip, ssh_port = (info, 20443)
                                #修改IP信息
                                if old_host:
                                    if ':' in old_host.strip():
                                        if len(old_host.split(':')) == 2:
                                            old_ip, old_ssh_port = old_host.split(
                                                ':')
                                        else:
                                            raise flash('%s格式不符合要求!' % info)
                                    else:
                                        old_ip = old_host.strip()
                                        old_ssh_port = 20443
                                    if len(ips) == 1:
                                        if tcpping(host=ip,
                                                   port=ssh_port,
                                                   timeout=5):
                                            # 获取third_id
                                            third_id = db_third.query.with_entities(
                                                db_third.id).filter(
                                                    and_(
                                                        db_third.ip == ip,
                                                        db_third.ssh_port ==
                                                        ssh_port)).all()
                                            # 清除项目资源表
                                            if third_id:
                                                v = db_project_third.query.filter(
                                                    db_project_third.third_id
                                                    == int(third_id[0]
                                                           [0])).all()
                                                if v:
                                                    for c in v:
                                                        db_op.DB.session.delete(
                                                            c)
                                                        db_op.DB.session.commit(
                                                        )
                                            # 删除第三方资源信息
                                            v = db_third.query.filter(
                                                and_(
                                                    db_third.ip == ip,
                                                    db_third.ssh_port ==
                                                    ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            # 删除zabbix信息
                                            v = db_zabbix.query.filter(
                                                and_(
                                                    db_zabbix.ip == ip,
                                                    db_zabbix.ssh_port ==
                                                    ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            #修改自有资源信息
                                            db_project.query.filter(
                                                and_(
                                                    db_project.ip == old_ip,
                                                    db_project.ssh_port ==
                                                    old_ssh_port)).update({
                                                        db_project.ip:
                                                        ip,
                                                        db_project.ssh_port:
                                                        ssh_port
                                                    })
                                            db_op.DB.session.commit()
                                            #删除新发现服务器信息
                                            v = db_server.query.filter(
                                                and_(
                                                    db_server.ip == ip,
                                                    db_server.ssh_port ==
                                                    ssh_port)).all()
                                            if v:
                                                for c in v:
                                                    db_idc.DB.session.delete(c)
                                                    db_idc.DB.session.commit()
                                            #修改服务器信息
                                            db_server.query.filter(
                                                and_(
                                                    db_server.ip == old_ip,
                                                    db_server.ssh_port ==
                                                    old_ssh_port)).update({
                                                        db_server.ip:
                                                        ip,
                                                        db_server.ssh_port:
                                                        ssh_port
                                                    })
                                            db_idc.DB.session.commit()
                                        else:
                                            raise flash(
                                                '运维平台尝试登录新IP{0}的ssh端口失败,服务器IP不能修改!'
                                                .format(ip))
                                    else:
                                        raise flash('修改服务器IP,新IP信息只能填写一行!',
                                                    'error')
                                values = db_server.query.filter(
                                    and_(
                                        db_server.ip == ip,
                                        db_server.ssh_port == ssh_port)).all()
                                if values:
                                    db_infos = {}
                                    if idrac:
                                        db_infos[db_server.idrac] = idrac
                                    if purch:
                                        db_infos[db_server.purch_date] = purch
                                    if expird:
                                        db_infos[
                                            db_server.expird_date] = expird
                                    if fault:
                                        db_infos[db_server.status] = '维护中'
                                    if db_infos:
                                        db_server.query.filter(
                                            and_(
                                                db_server.ip == ip,
                                                db_server.ssh_port ==
                                                ssh_port)).update(db_infos)
                                    if rack:
                                        val = db_idc_id.query.filter(
                                            and_(db_idc_id.aid == aid,
                                                 db_idc_id.cid == rack)).all()
                                        if not val:
                                            v = db_idc_id(aid=aid, cid=rack)
                                            db_idc.DB.session.add(v)
                                            db_idc.DB.session.commit()
                                        idc_id = db_idc_id.query.with_entities(
                                            db_idc_id.id).filter(
                                                and_(db_idc_id.aid == aid,
                                                     db_idc_id.cid ==
                                                     rack)).all()
                                        idc_id = int(idc_id[0][0])
                                        db_server.query.filter(
                                            and_(
                                                db_server.ip == ip,
                                                db_server.ssh_port ==
                                                ssh_port)).update(
                                                    {db_server.idc_id: idc_id})

                                    try:
                                        db_idc.DB.session.commit()
                                    except Exception as e:
                                        flash(e, 'error')
                                    else:
                                        flash('%s服务器信息修改成功!' % info)
                                else:
                                    flash('%s资产没有找到!' % info)
                            else:
                                #修改网络或者存储设备信息
                                ip = info
                                network_infos = {}
                                store_infos = {}
                                v_network = db_network.query.filter(
                                    and_(db_network.ip == ip)).all()
                                v_store = db_store.query.filter(
                                    and_(db_store.ip == ip)).all()
                                if v_network or v_store:
                                    if device_type:
                                        network_infos[
                                            db_network.type] = device_type
                                        store_infos[
                                            db_store.type] = device_type
                                    if purch:
                                        network_infos[
                                            db_network.purch_date] = purch
                                        store_infos[
                                            db_store.purch_date] = purch
                                    if expird:
                                        network_infos[
                                            db_network.expird_date] = expird
                                        store_infos[
                                            db_store.expird_date] = expird
                                    if network_infos and store_infos:
                                        db_network.query.filter(
                                            and_(db_network.aid == aid,
                                                 db_network.ip == ip)).update(
                                                     network_infos)
                                        db_store.query.filter(
                                            db_store.ip == ip).update(
                                                store_infos)
                                    if rack:
                                        val = db_idc_id.query.filter(
                                            and_(db_idc_id.aid == aid,
                                                 db_idc_id.cid == rack)).all()
                                        if not val:
                                            v = db_idc_id(aid=aid, cid=rack)
                                            db_idc.DB.session.add(v)
                                            db_idc.DB.session.commit()
                                        idc_id = db_idc_id.query.with_entities(
                                            db_idc_id.id).filter(
                                                and_(db_idc_id.aid == aid,
                                                     db_idc_id.cid ==
                                                     rack)).all()
                                        idc_id = int(idc_id[0][0])
                                        db_network.query.filter(
                                            and_(db_network.ip == ip)).update(
                                                {db_network.idc_id: idc_id})
                                        db_store.query.filter(
                                            and_(db_store.ip == ip)).update(
                                                {db_store.idc_id: idc_id})
                                    else:
                                        flash('%设备修改信息为空' % info)
                                    try:
                                        db_idc.DB.session.commit()
                                    except Exception as e:
                                        flash(e, 'error')
                                    else:
                                        flash('%s服务器信息修改成功!' % info)
                                else:
                                    raise flash('%s资产没有找到!' % info)
        except Exception as e:
            db_idc.DB.session.rollback()
            db_op.DB.session.rollback()
            if 'old' not in str(e):
                logging.error(e)
                flash(e)
        finally:
            return render_template('Message.html')
    return render_template('assets_manage.html',
                           form=form,
                           change_infos=change_infos)
Exemple #13
0
def resource_report():
    try:
        form = MyForm.FormResourceReport()
        days = tools.http_args(request, 'days')
        date = datetime.datetime.now()
        db_server = db_idc.idc_servers
        if days:
            days = int(days)
            update_date = date - datetime.timedelta(days=days)
        else:
            days = 30
            update_date = date - datetime.timedelta(days=30)
        datazoom = True
        if days == 30:
            datazoom = False
        Bars = {}
        titles = {30: '近一个月', 90: '近三个月', 180: '近六个月', 360: '近一年内'}
        db_third = db_idc.third_resource
        db_project = db_op.project_list
        vals = db_project.query.with_entities(
            db_project.update_date, func.count(db_project.update_date)).filter(
                and_(db_project.resource == 'tomcat',
                     db_project.update_date != '',
                     db_project.update_date >= update_date)).group_by(
                         db_project.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            tomcat_bar = Bar("tomcat(%s)" % titles[days],
                             width='110%',
                             height='100%',
                             title_pos='center',
                             title_text_size=14)
            tomcat_bar.add("",
                           attrs,
                           datas,
                           is_label_show=True,
                           is_toolbox_show=False,
                           legend_orient='vertical',
                           legend_pos='right',
                           xaxis_interval=0,
                           is_random=True,
                           xaxis_rotate=15,
                           is_datazoom_show=datazoom,
                           datazoom_type='both')
            Bars['tomcat'] = tomcat_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '',
                     db_third.update_date >= update_date)).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            redis_bar = Bar("redis(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            redis_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['redis'] = redis_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '')).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            mysql_bar = Bar("mysql(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            mysql_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['mysql'] = mysql_bar
        vals = db_third.query.with_entities(
            db_third.update_date, func.count(db_third.update_date)).filter(
                and_(db_third.resource_type == 'redis',
                     db_third.update_date != '')).group_by(
                         db_third.update_date).all()
        if vals:
            attrs = [val[0] for val in vals]
            datas = [val[-1] for val in vals]
            nginx_bar = Bar("nginx(%s)" % titles[days],
                            width='110%',
                            height='100%',
                            title_pos='center',
                            title_text_size=14)
            nginx_bar.add("",
                          attrs,
                          datas,
                          is_label_show=True,
                          is_toolbox_show=False,
                          legend_orient='vertical',
                          legend_pos='right',
                          xaxis_interval=0,
                          is_random=True,
                          xaxis_rotate=15,
                          is_datazoom_show=datazoom,
                          datazoom_type='both')
            Bars['nginx'] = nginx_bar
        try:
            td = time.strftime("%Y-%m-%d", time.localtime())
            std = datetime.datetime.now() - datetime.timedelta(days=180)
            std = std.strftime("%Y-%m-%d")
            if RC.exists('op_web_vm_vales_%s' % td) and RC.exists(
                    'op_web_py_vales_%s' % td):
                vm_vals = eval(RC.get('op_web_vm_vales_%s' % td))
                py_vals = eval(RC.get('op_web_py_vales_%s' % td))
            else:
                vm_vals = db_server.query.with_entities(
                    db_server.purch_date,
                    func.count(db_server.purch_date)).filter(
                        and_(db_server.host_type == 'vm',
                             db_server.purch_date > std)).group_by(
                                 db_server.purch_date).all()
                RC.set('op_web_vm_vales_%s' % td, vm_vals)
                RC.expire('op_web_vm_vales_%s', 86400)
                py_vals = db_server.query.with_entities(
                    db_server.purch_date,
                    func.count(db_server.purch_date)).filter(
                        and_(db_server.host_type == 'physical',
                             db_server.purch_date > std)).group_by(
                                 db_server.purch_date).all()
                RC.set('op_web_py_vales_%s' % td, py_vals)
                RC.expire('op_web_py_vales_%s', 86400)
        except Exception as e:
            logging.error(e)
        server_bar = Bar("近6个月新增服务器数量",
                         title_pos='center',
                         title_text_size=12,
                         width='110%',
                         height='230px')
        try:
            attrs = sorted(
                set([val[0] for val in vm_vals if val] +
                    [val[0] for val in py_vals if val]))
            vm_vals = {val[0]: val[1] for val in vm_vals}
            py_vals = {val[0]: val[1] for val in py_vals}
            for attr in attrs:
                if attr not in vm_vals:
                    vm_vals[attr] = 0
                if attr not in py_vals:
                    py_vals[attr] = 0
            vm_vals = sorted(vm_vals.items(), key=lambda item: item[0])
            vm_vals = [val[1] for val in vm_vals]
            py_vals = sorted(py_vals.items(), key=lambda item: item[0])
            py_vals = [val[1] for val in py_vals]
            attrs = ['-'.join(val.split('-')[1:]) for val in attrs]
            vm_counts = reduce(lambda x, y: x + y, vm_vals)
            server_bar.add('虚拟机%s台' % vm_counts,
                           attrs,
                           vm_vals,
                           is_label_show=True,
                           is_toolbox_show=False,
                           xaxis_interval=0,
                           xaxis_rotate=25,
                           legend_pos='70%')
            py_counts = reduce(lambda x, y: x + y, py_vals)
            server_bar.add('物理机%s台' % py_counts,
                           attrs,
                           py_vals,
                           is_label_show=True,
                           is_toolbox_show=False,
                           xaxis_interval=0,
                           xaxis_rotate=25,
                           legend_pos='70%')
        except Exception as e:
            logging.error(e)
        return render_template('resource_report.html',
                               Bars=Bars,
                               form=form,
                               days=days,
                               server_bar=server_bar)
    except Exception as e:
        logging.error(e)
        return redirect(url_for('error'))