コード例 #1
0
    def create(self, request):
        """
        Create Network

        :param request: create parameter
        :return: organization ID
        :rtype: uuid
        """
        serializer = NetworkCreateBody(data=request.data)
        if serializer.is_valid(raise_exception=True):
            name = serializer.validated_data.get("name")
            consensus = serializer.validated_data.get("consensus")
            organizations = serializer.validated_data.get("organizations")
            database = serializer.validated_data.get("database")

            try:
                Network.objects.get(name=name)
            except ObjectDoesNotExist:
                pass
            orderers = []
            peers = []
            i = 0
            for organization in organizations:
                org = Organization.objects.get(pk=organization)
                orderers.append({"name": org.name, "hosts": []})
                peers.append({"name": org.name, "hosts": []})
                nodes = Node.objects.filter(org=org)
                for node in nodes:
                    if node.type == "peer":
                        peers[i]["hosts"].append({"name": node.name})
                    elif node.type == "orderer":
                        orderers[i]["hosts"].append({"name": node.name})
                i = i + 1

            ConfigTX(name).create(consensus=consensus, orderers=orderers, peers=peers)
            ConfigTxGen(name).genesis()

            block = self._genesis2base64(name)
            network = Network(name=name, consensus=consensus, organizations=organizations, genesisblock=block)
            network.save()

            for organization in organizations:
                Organization.objects.filter(pk=organization).update(network=network)
                org = Organization.objects.get(pk=organization)
                nodes = Node.objects.filter(org=org)
                for node in nodes:
                    try:
                        self._start_node(node.id)
                    except Exception as e:
                        print("err:", e)
                        pass

            response = NetworkIDSerializer(data=network.__dict__)
            if response.is_valid(raise_exception=True):
                return Response(
                    response.validated_data, status=status.HTTP_201_CREATED
                )
コード例 #2
0
ファイル: views.py プロジェクト: yeasy/cello
    def create(self, request):
        """
        Create Channel
        :param request: create parameter
        :return: Channel ID
        :rtype: uuid
        """

        serializer = ChannelCreateBody(data=request.data)
        if serializer.is_valid(raise_exception=True):
            name = serializer.validated_data.get("name")
            peers = serializer.validated_data.get("peers")
            orderers = serializer.validated_data.get("orderers")

            try:
                org = request.user.organization
                ConfigTX(org.network.name).createChannel(name, [org.name])
                ConfigTxGen(org.network.name).channeltx(
                    profile=name,
                    channelid=name,
                    outputCreateChannelTx="{}.tx".format(name))
                tx_path = "{}/{}/channel-artifacts/{}.tx".format(
                    CELLO_HOME, org.network.name, name)
                block_path = "{}/{}/channel-artifacts/{}.block".format(
                    CELLO_HOME, org.network.name, name)
                ordering_node = Node.objects.get(id=orderers[0])
                peer_node = Node.objects.get(id=peers[0])
                envs = init_env_vars(peer_node, org)
                peer_channel_cli = PeerChannel("v2.2.0", **envs)
                peer_channel_cli.create(channel=name,
                                        orderer_url="{}.{}:{}".format(
                                            ordering_node.name,
                                            org.name.split(".", 1)[1],
                                            str(7050)),
                                        channel_tx=tx_path,
                                        output_block=block_path)
                for i in range(len(peers)):
                    peer_node = Node.objects.get(id=peers[i])
                    envs = init_env_vars(peer_node, org)
                    #envs["CORE_PEER_LOCALMSPID"] = '{}MSP'.format(peer_node.name.split(".")[0].capitalize()) #Org1MSP
                    join_peers(envs, block_path)

                channel = Channel(name=name, network=org.network)
                channel.save()
                channel.organizations.add(org)
                channel.orderers.add(ordering_node)
                response = ChannelIDSerializer(data=channel.__dict__)
                if response.is_valid(raise_exception=True):
                    return Response(response.validated_data,
                                    status=status.HTTP_201_CREATED)
            except ObjectDoesNotExist:
                pass
コード例 #3
0
    def create(self, request):
        serializer = NetWorkCreateBody(data=request.data)
        if serializer.is_valid(raise_exception=True):
            network_name = serializer.validated_data.get("name")
            consensus = serializer.validated_data.get("consensus")
            organizations = serializer.validated_data.get("organizations")
            try:
                NetWork.objects.get(name=network_name)
            except ObjectDoesNotExist:
                pass
            orderers = []
            peers = []
            i = 0
            for organization in organizations:
                org = Organization.objects.get(name=organization)
                orderers.append({"name": organization, "hosts": []})
                peers.append({"name": organization, "hosts": []})
                nodes = Node.objects.filter(organization=org)
                for node in nodes:
                    if node.type == "peer":
                        peers[i]["hosts"].append({
                            "name":
                            node.name,
                            "port":
                            node.urls.split(":")[2]
                        })
                    elif node.type == "orderer":
                        orderers[i]["hosts"].append({
                            "name":
                            node.name,
                            "port":
                            node.urls.split(":")[2]
                        })
                i = i + 1

            ConfigTX(network_name).create(consensus=consensus,
                                          orderers=orderers,
                                          peers=peers)
            ConfigTxGen(network_name).genesis()

            network = NetWork(name=network_name,
                              consensus=consensus,
                              organizations=organizations)
            network.save()

            response = NetWorkIDSerializer(data=network.__dict__)
            if response.is_valid(raise_exception=True):
                return Response(response.validated_data,
                                status=status.HTTP_201_CREATED)
コード例 #4
0
    def create(self, request):
        """
        Create Network
        :param request: create parameter
        :return: organization ID
        :rtype: uuid
        """
        try:
            serializer = NetworkCreateBody(data=request.data)
            if serializer.is_valid(raise_exception=True):
                name = serializer.validated_data.get("name")
                consensus = serializer.validated_data.get("consensus")
                database = serializer.validated_data.get("database")

                try:
                    if Network.objects.get(name=name):
                        raise ResourceExists
                except ObjectDoesNotExist:
                    pass
                org = request.user.organization
                if org.network:
                    raise ResourceExists

                orderers = []
                peers = []
                orderers.append({"name": org.name, "hosts": []})
                peers.append({"name": org.name, "hosts": []})
                nodes = Node.objects.filter(organization=org)
                for node in nodes:
                    if node.type == "peer":
                        peers[0]["hosts"].append({"name": node.name})
                    elif node.type == "orderer":
                        orderers[0]["hosts"].append({"name": node.name})

                ConfigTX(name).create(consensus=consensus,
                                      orderers=orderers,
                                      peers=peers)
                ConfigTxGen(name).genesis()

                block = self._genesis2base64(name)
                network = Network(name=name,
                                  consensus=consensus,
                                  genesisblock=block)
                network.save()
                org.network = network
                org.save()
                nodes = Node.objects.filter(organization=org)
                for node in nodes:
                    try:
                        threading.Thread(target=self._start_node,
                                         args=(node.id, )).start()
                    except Exception as e:
                        raise e

                response = NetworkIDSerializer(data=network.__dict__)
                if response.is_valid(raise_exception=True):
                    return Response(ok(response.validated_data),
                                    status=status.HTTP_201_CREATED)
        except ResourceExists as e:
            raise e
        except Exception as e:
            return Response(err(e.args), status=status.HTTP_400_BAD_REQUEST)