Ejemplo n.º 1
0
    def delete(self, id, record=False, forced=False):
        """ Delete a cluster instance

        Clean containers, remove db entry. Only operate on active host.

        :param id: id of the cluster to delete
        :param record: Whether to record into the released collections
        :param forced: Whether to removing user-using cluster, for release
        :return:
        """
        logger.debug("Delete cluster: id={}, forced={}".format(id, forced))

        try:
            cluster = ClusterModel.objects.get(id=id)
        except Exception:
            logger.warning("Cannot find cluster {}".format(id))
            return False

        c = self.db_update_one({"id": id}, {"status": NETWORK_STATUS_DELETING},
                               after=False)
        # we are safe from occasional applying now
        user_id = c.user_id  # original user_id
        if not forced and user_id != "":
            # not forced, and chain is used by normal user, then no process
            logger.warning("Cannot delete cluster {} by "
                           "user {}".format(id, user_id))
            cluster.update(set__user_id=user_id, upsert=True)
            return False
        else:
            cluster.update(set__status=NETWORK_STATUS_DELETING, upsert=True)

        host_id, worker_api, network_type, consensus_plugin, cluster_size = \
            str(c.host.id), c.worker_api, \
            c.network_type if c.network_type else NETWORK_TYPE_FABRIC_PRE_V1, \
            c.consensus_plugin if c.consensus_plugin else \
            CONSENSUS_PLUGINS_FABRIC_V1[0], \
            c.size if c.size else NETWORK_SIZE_FABRIC_PRE_V1[0]

        # port = api_url.split(":")[-1] or CLUSTER_PORT_START
        h = self.host_handler.get_active_host_by_id(host_id)
        if not h:
            logger.warning("Host {} inactive".format(host_id))
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        if network_type == NETWORK_TYPE_FABRIC_V1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
        elif network_type == NETWORK_TYPE_FABRIC_V1_1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
            config.network_type = NETWORK_TYPE_FABRIC_V1_1
        elif network_type == NETWORK_TYPE_FABRIC_V1_2:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
            config.network_type = NETWORK_TYPE_FABRIC_V1_2
        elif network_type == NETWORK_TYPE_FABRIC_PRE_V1:
            config = FabricPreNetworkConfig(consensus_plugin=consensus_plugin,
                                            consensus_mode='',
                                            size=cluster_size)
        else:
            return False

        config.update({"env": cluster.env})

        delete_result = self.cluster_agents[h.type].delete(
            id, worker_api, config)
        if not delete_result:
            logger.warning("Error to run compose clean work")
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        # remove cluster info from host
        logger.info("remove cluster from host, cluster:{}".format(id))
        h.update(pull__clusters=id)

        c.delete()
        return True
Ejemplo n.º 2
0
    def delete(self, id, record=False, forced=True):
        """ Delete a cluster instance

        Clean containers, remove db entry. Only operate on active host.

        :param id: id of the cluster to delete
        :param record: Whether to record into the released collections
        :param forced: Whether to removing user-using cluster, for release
        :return:
        """
        logger.debug("Delete cluster: id={}, forced={}".format(id, forced))

        try:
            cluster = ClusterModel.objects.get(id=id)
        except Exception:
            logger.warning("Cannot find cluster {}".format(id))
            return False
        user_id = str(cluster.user.id)
        channel = ChannelModel.objects.get(cluster=cluster)
        channel = channel.alias
        logger.info('delete channel :{}'.format(channel))
        from tasks import send_delete_cluster
        # send_delete_cluster.delay(body={'BlockchainSign': id})
        send_delete_cluster(body={'BlockchainSign': id, "ChannelId": channel}, user_id=user_id)
        c = self.db_update_one({"id": id}, {"status": NETWORK_STATUS_DELETING},
                               after=False)
        # we are safe from occasional applying now
        # user_id = c.user_id  # original user_id
        if not forced:
            # not forced, and chain is used by normal user, then no process
            logger.warning("Cannot delete cluster {} by "
                           "user {}".format(id, user_id))
            cluster.update(
                set__user_id=user_id,
                upsert=True
            )
            return False
        else:
            cluster.update(set__status=NETWORK_STATUS_DELETING, upsert=True)

        host_id, worker_api, network_type, consensus_plugin = \
            str(c.host.id), c.host.worker_api, \
            c.network_type if c.network_type else NETWORK_TYPE_FABRIC_PRE_V1, \
            c.consensus_plugin if c.consensus_plugin else \
                CONSENSUS_PLUGINS_FABRIC_V1[0]

        # port = api_url.split(":")[-1] or CLUSTER_PORT_START
        cluster_size = ''
        h = self.host_handler.get_active_host_by_id(host_id)
        if not h:
            logger.warning("Host {} inactive".format(host_id))
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        if network_type == NETWORK_TYPE_FABRIC_V1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin)

        elif network_type == NETWORK_TYPE_FABRIC_V1_1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin)
            config.network_type = NETWORK_TYPE_FABRIC_V1_1

        elif network_type == NETWORK_TYPE_FABRIC_V1_2:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin)
            config.network_type = NETWORK_TYPE_FABRIC_V1_2

        elif network_type == NETWORK_TYPE_FABRIC_PRE_V1:
            config = FabricPreNetworkConfig(consensus_plugin=consensus_plugin,
                                            consensus_mode='',
                                            size=cluster_size)
        else:
            return False

        config.update({
            "env": cluster.env
        })

        delete_result = self.cluster_agents[h.host_type].delete(id, worker_api, config)
        config_path = BLOCKCHAIN_CONFIG_FILES_PATH + '/{}'.format(id)
        if os.path.exists(config_path):
            shutil.rmtree(config_path)

        if not delete_result:
            logger.warning("Error to run compose clean work")
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        # remove cluster info from host
        logger.info("remove cluster from host, cluster:{}".format(id))
        h.update(pull__clusters=id)
        orgs = OrgModel.objects(cluster=cluster)
        for org in orgs:
            org_users = org.users
            for user in org_users:
                if int(user.role) in [2, 3]:
                    user.delete()

        c.delete()
        return True
Ejemplo n.º 3
0
    def delete(self, id, record=False, forced=False):
        """ Delete a cluster instance

        Clean containers, remove db entry. Only operate on active host.

        :param id: id of the cluster to delete
        :param record: Whether to record into the released collections
        :param forced: Whether to removing user-using cluster, for release
        :return:
        """
        logger.debug("Delete cluster: id={}, forced={}".format(id, forced))

        try:
            cluster = ClusterModel.objects.get(id=id)
        except Exception:
            logger.warning("Cannot find cluster {}".format(id))
            return False

        c = self.db_update_one({"id": id}, {"user_id": SYS_DELETER},
                               after=False)
        # we are safe from occasional applying now
        user_id = c.user_id  # original user_id
        if not forced and user_id != "" and not user_id.startswith(SYS_USER):
            # not forced, and chain is used by normal user, then no process
            logger.warning("Cannot delete cluster {} by "
                           "user {}".format(id, user_id))
            cluster.update(
                set__user_id=user_id,
                upsert=True
            )
            return False

        # 0. forced
        #  1. user_id == SYS_DELETER or ""
        #  Then, add deleting flag to the db, and start deleting
        if not user_id.startswith(SYS_DELETER):
            cluster.update(
                set__user_id=SYS_DELETER + user_id,
                upsert=True
            )
        host_id, worker_api, network_type, consensus_plugin, cluster_size = \
            str(c.host.id), c.worker_api, \
            c.network_type if c.network_type else NETWORK_TYPE_FABRIC_PRE_V1, \
            c.consensus_plugin if c.consensus_plugin else \
            CONSENSUS_PLUGINS_FABRIC_V1[0], \
            c.size if c.size else NETWORK_SIZE_FABRIC_PRE_V1[0]

        # port = api_url.split(":")[-1] or CLUSTER_PORT_START
        h = self.host_handler.get_active_host_by_id(host_id)
        if not h:
            logger.warning("Host {} inactive".format(host_id))
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        if network_type == NETWORK_TYPE_FABRIC_V1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
        elif network_type == NETWORK_TYPE_FABRIC_PRE_V1:
            config = FabricPreNetworkConfig(consensus_plugin=consensus_plugin,
                                            consensus_mode='',
                                            size=cluster_size)
        else:
            return False

        config.update({
            "env": cluster.env
        })
        if not self.cluster_agents[h.type].delete(id, worker_api,
                                                  config):
            logger.warning("Error to run compose clean work")
            cluster.update(set__user_id=user_id, upsert=True)
            return False

        c.delete()
        return True
Ejemplo n.º 4
0
    def delete(self, id, record=False, forced=False):
        """ Delete a cluster instance

        Clean containers, remove db entry. Only operate on active host.

        :param id: id of the cluster to delete
        :param record: Whether to record into the released collections
        :param forced: Whether to removing user-using cluster, for release
        :return:
        """
        logger.debug("Delete cluster: id={}, forced={}".format(id, forced))

        try:
            cluster = ClusterModel.Query.get(id=id)
        except Exception:
            logger.warning("Cannot find cluster {}".format(id))
            return False

        c = self.db_update_one({"id": id}, {"status": NETWORK_STATUS_DELETING},
                               after=False)
        # we are safe from occasional applying now
        user_id = c.user_id  # original user_id
        if not forced and user_id and user_id != "":
            # not forced, and chain is used by normal user, then no process
            logger.warning("Cannot delete cluster {} by "
                           "user {}".format(id, user_id))
            cluster = ClusterModel(objectId=cluster.objectId, user_id=user_id)
            cluster.save()
            return False
        else:
            cluster = ClusterModel(objectId=cluster.objectId,
                                   status=NETWORK_STATUS_DELETING)
            cluster.save()

        host_id, worker_api, network_type, consensus_plugin, cluster_size = \
            str(c.host.id), c.worker_api, \
            c.network_type if c.network_type else NETWORK_TYPE_FABRIC_PRE_V1, \
            c.consensus_plugin if c.consensus_plugin else \
            CONSENSUS_PLUGINS_FABRIC_V1[0], \
            c.size if c.size else NETWORK_SIZE_FABRIC_PRE_V1[0]

        # port = api_url.split(":")[-1] or CLUSTER_PORT_START
        h = self.host_handler.get_active_host_by_id(host_id)
        if not h:
            logger.warning("Host {} inactive".format(host_id))
            cluster = ClusterModel(objectId=cluster.objectId, user_id=user_id)
            cluster.save()
            return False

        if network_type == NETWORK_TYPE_FABRIC_V1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
        elif network_type == NETWORK_TYPE_FABRIC_V1_1:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
            config.network_type = NETWORK_TYPE_FABRIC_V1_1
        elif network_type == NETWORK_TYPE_FABRIC_V1_2:
            config = FabricV1NetworkConfig(consensus_plugin=consensus_plugin,
                                           size=cluster_size)
            config.network_type = NETWORK_TYPE_FABRIC_V1_2
        elif network_type == NETWORK_TYPE_FABRIC_PRE_V1:
            config = FabricPreNetworkConfig(consensus_plugin=consensus_plugin,
                                            consensus_mode='',
                                            size=cluster_size)
        else:
            return False

        config.update({
            "env": c.env
        })

        delete_result = self.cluster_agents[h.type].delete(id, worker_api,
                                                           config)
        if not delete_result:
            logger.warning("Error to run compose clean work")
            cluster = ClusterModel(objectId=cluster.objectId,
                                   user_id=user_id)
            cluster.save()
            return False

        # remove cluster info from host
        logger.info("remove cluster from host, cluster:{}".format(id))
        # h.update(pull__clusters=id)
        # h = HostModel(objectId=h.objectId)

        c.delete()
        # service_ports = ServicePort.Query.filter(cluster=c.as_pointer)
        # batcher = ParseBatcher()
        # batcher.batch_delete(service_ports)
        # containers = Container.Query.filter(cluster=c.as_pointer)
        # batcher.batch_delete(containers)
        return True