示例#1
0
def create_cluster_hosts_dict(cluster):
    roles = {"master": 1, "worker": cluster.worker_size}
    hosts = []
    deploy_template = cluster.plan.deploy_template
    domain = cluster.name + '.' + cluster.cluster_doamin_suffix
    if deploy_template == Plan.DEPLOY_TEMPLATE_MULTIPLE:
        roles['master'] = 3
    for role, size in roles.items():
        compute_model_name = cluster.plan.compute_models[role]
        compute_model = {}
        if cluster.plan.region.template.name == 'openstack':
            for model in cluster.plan.vars['compute_models']:
                if model['name'] == compute_model_name:
                    compute_model = model['meta']
        else:
            compute_model = get_compute_model_meta(compute_model_name)
        for i in range(1, size + 1):
            name = role + "{}.".format(i) + "{}".format(domain)
            zone = get_zone(cluster.plan.get_zones(), i)
            ## 选择到了zone 后更新 zone参数
            if zone:
                cluster.configs.update(zone.vars)
                cluster.save()
            if not zone:
                raise RuntimeError('Can not find  available ip address!')
            host = {
                "role": role,
                "cpu": compute_model['cpu'],
                "memory": compute_model['memory'] * 1024,
                "name": name,
                "short_name": role + "{}".format(i),
                "domain": domain,
                "zone": zone.to_dict(),
                "zone_name": zone.name,
            }
            # 自定义模板处理
            if zone.vars.get('template_type') is not None and zone.vars[
                    'template_type'] == 'customize' and zone.vars[
                        'guest_id'] != '':
                host['guest_id'] = zone.vars['guest_id']

            host_set = Host.objects.filter(name=name)
            if host_set:
                host.update({"ip": host_set.first().ip})
            else:
                host.update({"ip": zone.allocate_ip(), "new": True})
            hosts.append(host)
    return hosts
def create_cluster_hosts_dict(cluster):
    roles = {"master": 1, "worker": cluster.worker_size}
    hosts = []
    deploy_template = cluster.plan.deploy_template
    domain = cluster.name + "." + Setting.objects.get(
        key="domain_suffix").value
    if deploy_template == Plan.DEPLOY_TEMPLATE_MULTIPLE:
        roles['master'] = 3
    for role, size in roles.items():
        compute_model_name = cluster.plan.compute_models[role]
        compute_model = {}
        if cluster.plan.region.template.name == 'openstack':
            for model in cluster.plan.vars['compute_models']:
                if model['name'] == compute_model_name:
                    compute_model = model['meta']
        else:
            compute_model = get_compute_model_meta(compute_model_name)
        for i in range(1, size + 1):
            name = role + "{}.".format(i) + "{}".format(domain)
            zone = get_zone(cluster.plan.get_zones(), i)
            if not zone:
                raise RuntimeError('Can not find  available ip address!')
            host = {
                "role": role,
                "cpu": compute_model['cpu'],
                "memory": compute_model['memory'] * 1024,
                "name": name,
                "short_name": role + "{}".format(i),
                "domain": domain,
                "zone": zone.to_dict(),
                "zone_name": zone.name,
            }
            host_set = Host.objects.filter(name=name)
            if host_set:
                host.update({"ip": host_set.first().ip})
            else:
                host.update({"ip": zone.allocate_ip(), "new": True})
            hosts.append(host)
    return hosts