zoukankan      html  css  js  c++  java
  • Django实现自动发布(3发布-安装)

    相对于服务的升级、回退,新部署一个服务要复杂一些,要满足以下要求:

    • 已经运行了服务实例的主机不能重复部署
    • 进程启动需要的配置文件要先同步到主机上

    之前的升级、回退都是指进程的操作,不涉及配置文件的变更

    配置文件的管理、同步比较复杂,放到后面,这里就专注于服务的安装了

    不能重复部署,这个比较容易实现,资产表和实例表做个关联查询,返回所有在实例表中不存在的资产,代码大概是这个样子:

    def get(self, request, service_id):
        deployed = MicroServiceInstance.objects.filter(microservice_id=service_id)
        deployed_id_set = set([v.host_id for v in deployed])
    
        data = [{
            'id': item.id,
            'ip': item.ip,
            'hostname': item.hostname,
            'enable': False if item.id in deployed_id_set else True
        } for item in Asset.objects.all()]
    
        return JsonResponse({
            'data': data,
            'count': len(data),
            'code': 0,
        })
    
    

    页面与升级类似:
    服务安装

    除了前端不能选择已部署服务的主机,当我们发送数据(以逗号分隔的主机id)给后端时,也需要进行校验:

    • 这批id格式是否正确,后端能否正常解析
    • 主机id中是否有已经部署了该服务的实例与之关联
    • 主机id在资产表中是否存在

    相应的检查代码:

    def post(self, request, service_id, pk):
        comma_host_ids = request.POST.get('host', '').strip()
        if not comma_host_ids:
            return JsonResponse({'msg': '主机不能为空'}, status=417)
        elif not re.match(r'[0-9,]', comma_host_ids):
            return JsonResponse({'msg': '请发送正确的主机id'}, status=417)
    
        deployed_insts = MicroServiceInstance.objects.filter(microservice_id=service_id)
        idset = set([int(x) for x in comma_host_ids.split(',') if x])
        deployed_hosts = [x for x in deployed_insts if x.host_id in idset]
    
        if deployed_hosts:
            return JsonResponse({'msg': '主机{}已部署相关服务'.format(
                ','.join(x.host.ip for x in deployed_hosts)
            )}, status=417)
    
        q = Q()
        q.connector = 'OR'
        for _id in idset:
            q.children.append(('id', _id))
        hosts = Asset.objects.filter(q)
    
        if hosts.count() != len(idset):
            return JsonResponse({'msg': '请发送正确的主机id'}, status=417)
    

    校验通过后,就可以在实例表中创建一条记录,并标记状态为安装中, 然后发起异步任务去做具体的操作:

    
        installing_insts = []
        for host in hosts:
            d = {
                'microservice_id': service.id,
                'version_id': version.id,
                'host_id': host.id,
                'description': '{} instance'.format(service.name),
                'status': InstanceStatus.installing.value, # 安装中
                'locked': True,
                'updated_by': request.user,
            }
            inst = MicroServiceInstance.objects.create(**d)
            installing_insts.append(inst.id)
    
        # TODO 发起任务
    

    相关的页面和代码比较多,放到 这里

  • 相关阅读:
    将一堆图片自适应页面排列
    用正则匹配富文本中的文本,并替换其内容
    实战
    从 defineProperty 到 Proxy
    CSS 实现蜂巢/六边形图集
    Umi 小白纪实(三)—— 震惊!路由竟然如此强大!
    无题
    贺文
    模型可解释性方法--lime
    多模态融合注记
  • 原文地址:https://www.cnblogs.com/wbjxxzx/p/12076664.html
Copyright © 2011-2022 走看看