Django automatically released (release 3 - Installation)

Upgrade services with respect to the rollback, the deployment of a new service to be more complex to meet the following requirements:

  • Has been running for the host service instance can not be repeated deployment
  • Need to start the process of the configuration file must first be synchronized to the host

Before the upgrade, rollback refer to the operating process does not involve changing the configuration file

Manage profiles, synchronization is more complex, into the back, where it is focused on the service is installed

Can not be repeated deployments, this is easier to achieve, balance sheet, and examples of tables to be associated with the query, return all assets that do not exist in the instance table, the code would look something like:

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,
    })

Page and upgrade similar to:
Services Installation

In addition to front-end can not select the host has been deployed services, when we send data (comma-separated host id) to the back end when, also need to be verified:

  • These id format is correct, whether the normal back-end analytic
  • Are there instances associated with it has deployed in the service host id
  • Host id exists in the table of assets

The appropriate test code:

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)

After verification by, you can create a record in the instance table, and the status flag is installed in , and then initiates an asynchronous task to do a specific operation:


    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 发起任务

Related pages and code more, put it here

Guess you like

Origin www.cnblogs.com/wbjxxzx/p/12076664.html