示例#1
0
def get_cluster_resource(cluster, item_id):
    objs = []
    nodes = Node.objects.filter(project_id=cluster.id)
    for node in nodes:
        if node.host_id is not None:
            node_obj = ItemResource(resource_type=ItemResource.RESOURCE_TYPE_HOST, resource_id=node.host_id,
                                    item_id=item_id)
            objs.append(node_obj)
    if cluster.persistent_storage == 'nfs':
        nfs_name = cluster.configs['nfs']
        nfs = NfsStorage.objects.get(name=nfs_name)
        nfs_obj = ItemResource(resource_type=ItemResource.RESOURCE_TYPE_STORAGE, resource_id=nfs.id,
                               item_id=item_id)
        objs.append(nfs_obj)
    if cluster.persistent_storage == 'ceph':
        ceph = ClusterCephStorage.objects.get(cluster_id=cluster.id)
        ceph_obj = ItemResource(resource_type=ItemResource.RESOURCE_TYPE_STORAGE, resource_id=ceph.id,
                                item_id=item_id)
        objs.append(ceph_obj)
    if cluster.deploy_type == Cluster.CLUSTER_DEPLOY_TYPE_AUTOMATIC:
        plan_obj = ItemResource(resource_type=ItemResource.RESOURCE_TYPE_PLAN, resource_id=cluster.plan_id,
                                item_id=item_id)
        objs.append(plan_obj)
    cluster_backup = ClusterBackup.objects.filter(project_id=cluster.id)
    if len(cluster_backup) > 0:
        backup_obj = ItemResource(resource_type=ItemResource.RESOURCE_TYPE_BACKUP_STORAGE,
                                  resource_id=cluster_backup[0].backup_storage_id,
                                  item_id=item_id)
        objs.append(backup_obj)
    return objs
示例#2
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     if request.data.get('item_name'):
         item = Item.objects.get(name=request.data.get('item_name'))
         cluster = Cluster.objects.get(name=request.data.get('name'))
         itemResource = ItemResource(item_id=item.id, resource_id=cluster.id,
                                     resource_type=ItemResource.RESOURCE_TYPE_CLUSTER)
         itemResource.save()
     return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
示例#3
0
def create_nodes(cluster, hosts_dict):
    hosts = []
    new_nodes = []
    for host_dict in hosts_dict:
        zone = Zone.objects.get(name=host_dict["zone_name"])
        defaults = {
            "name": host_dict['name'],
            "ip": host_dict['ip'],
            "zone": zone,
            "status": Host.HOST_STATUS_CREATING,
            "auto_gather_info": False
        }
        if zone.vars.get('template_type') is not None and zone.vars[
                'template_type'] == 'customize' and zone.vars[
                    'template_password']:
            defaults['password'] = zone.vars['template_password']

        if host_dict.get('new', False):
            result = Host.objects.update_or_create(defaults,
                                                   name=host_dict['name'])
            host = result[0]
            node = cluster.create_node(host_dict['role'], host)
            item_resource = ItemResource.objects.get(resource_id=cluster.id)
            item_r = ItemResource(
                item_id=item_resource.item_id,
                resource_id=host.id,
                resource_type=ItemResource.RESOURCE_TYPE_HOST)
            item_r.save()
            new_nodes.append(node)
            hosts.append(host)
    client = get_cloud_client(cluster.plan.mixed_vars)
    terraform_result = client.apply_terraform(cluster, hosts_dict)
    if not terraform_result:
        for node in new_nodes:
            node.host.delete()
            raise RuntimeError("create host error!")
    if cluster.plan.mixed_vars.get('provider') == 'openstack':
        print("sleep 20s,等待sshd服务可用")
        sleep(20)
    for host in hosts:
        host.gather_info(retry=5)
示例#4
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)

        item_ids = request.data.get('item_id', None)
        if item_ids and len(item_ids) > 0:
            item_resources = []
            for item_id in item_ids:
                item_resources.append(ItemResource(item_id=item_id, resource_id=serializer.data['id'],
                                                   resource_type=ItemResource.RESOURCE_TYPE_PLAN))
            ItemResource.objects.bulk_create(item_resources)

        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
示例#5
0
    def post(self, request, *args, **kwargs):
        item_resources = request.data
        objs = []
        for item_resource in item_resources:
            if item_resource['resource_type'] == ItemResource.RESOURCE_TYPE_CLUSTER:
                cluster = Cluster.objects.get(id=item_resource['resource_id'])
                objs = get_cluster_resource(cluster, item_resource['item_id'])
            obj = ItemResource(resource_type=item_resource['resource_type'], resource_id=item_resource['resource_id'],
                               item_id=item_resource['item_id'])
            objs.append(obj)

        result = ItemResource.objects.bulk_create(objs)
        response = HttpResponse(content_type='application/json')
        response.write(json.dumps({'msg': '授权成功'}))
        return response