class ClusterQuery(PageQuerySerializer): consensus_plugin = serializers.ChoiceField( required=False, allow_null=True, help_text=ConsensusPlugin.get_info("Consensus Plugin:", list_str=True), choices=ConsensusPlugin.to_choices(True), ) name = serializers.CharField( required=False, allow_null=True, min_length=NAME_MIN_LEN, max_length=NAME_MAX_LEN, help_text=NAME_HELP_TEXT, ) host_id = serializers.CharField(help_text="Host ID", required=False, allow_null=True) network_type = serializers.ChoiceField( required=False, allow_null=True, help_text=NetworkType.get_info("Network Types:", list_str=True), choices=NetworkType.to_choices(), ) size = serializers.IntegerField( required=False, allow_null=True, min_value=SIZE_MIN_VALUE, max_value=SIZE_MAX_VALUE, help_text="Size of cluster", )
class NodeCreateBody(serializers.Serializer): network_type = serializers.ChoiceField( help_text=NetworkType.get_info("Network types:", list_str=True), choices=NetworkType.to_choices(True), ) type = serializers.ChoiceField( help_text=FabricNodeType.get_info("Node Types:", list_str=True), choices=FabricNodeType.to_choices(True), )
class Network(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of network", default=make_uuid, editable=True, ) govern = models.ForeignKey(Govern, help_text="Govern of node", null=True, on_delete=models.CASCADE) type = models.CharField( help_text="Type of network, %s" % NetworkType.values(), max_length=64, default=NetworkType.Fabric.value, ) version = models.CharField( help_text=""" Version of network. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) created_at = models.DateTimeField(help_text="Create time of network", auto_now_add=True) class Meta: ordering = ("-created_at", )
class ClusterCreateBody(serializers.Serializer): name = serializers.CharField( min_length=NAME_MIN_LEN, max_length=NAME_MAX_LEN, help_text=NAME_HELP_TEXT, ) host_id = serializers.CharField(help_text="Host ID") network_type = serializers.ChoiceField( help_text=NetworkType.get_info("Network Types:", list_str=True), choices=NetworkType.to_choices(), ) size = serializers.IntegerField( min_value=SIZE_MIN_VALUE, max_value=SIZE_MAX_VALUE, help_text="Size of cluster", ) consensus_plugin = serializers.ChoiceField( help_text=ConsensusPlugin.get_info("Consensus Plugin:", list_str=True), choices=ConsensusPlugin.to_choices(True), )
class Network(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of network", default=make_uuid, editable=True, ) name = models.CharField( help_text="network name, can be generated automatically.", max_length=64, default=random_name("netowrk"), ) type = models.CharField( help_text="Type of network, %s" % NetworkType.values(), max_length=64, default=NetworkType.Fabric.value, ) version = models.CharField( help_text=""" Version of network. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) created_at = models.DateTimeField(help_text="Create time of network", auto_now_add=True) consensus = models.CharField( help_text="Consensus of network", max_length=128, default="raft", ) organizations = ArrayField(models.CharField(max_length=128, blank=True), help_text="organizations of network", default=list, null=True) genesisblock = models.TextField( help_text="genesis block", null=True, ) database = models.CharField( help_text="database of network", max_length=128, default="leveldb", ) class Meta: ordering = ("-created_at", )
class Node(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of node", default=make_uuid, editable=True, ) name = models.CharField(help_text="Node name", max_length=64, default="") network_type = models.CharField( help_text="Network type of node", choices=NetworkType.to_choices(True), default=NetworkType.Fabric.name.lower(), max_length=64, ) network_version = models.CharField( help_text=""" Version of network for node. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) type = models.CharField( help_text=""" Node type defined for network. Fabric available types: %s """ % (FabricNodeType.names()), max_length=64, ) urls = JSONField( help_text="URL configurations for node", null=True, blank=True, default=dict, ) user = models.OneToOneField( UserProfile, help_text="User of node", null=True, on_delete=models.CASCADE, ) govern = models.ForeignKey( Govern, help_text="Govern of node", null=True, on_delete=models.CASCADE ) organization = models.ForeignKey( Organization, help_text="Organization of node", null=True, on_delete=models.CASCADE, ) agent = models.ForeignKey( Agent, help_text="Agent of node", null=True, on_delete=models.CASCADE ) network = models.ForeignKey( Network, help_text="Network which node joined.", on_delete=models.CASCADE, null=True, ) created_at = models.DateTimeField( help_text="Create time of network", auto_now_add=True ) status = models.CharField( help_text="Status of node", choices=NodeStatus.to_choices(True), max_length=64, default=NodeStatus.Deploying.name.lower(), ) compose_file = models.FileField( help_text="Compose file for node, if agent type is docker.", max_length=256, upload_to=get_compose_file_path, blank=True, null=True, ) class Meta: ordering = ("-created_at",) def get_compose_file_path(self): return "%s/org/%s/agent/docker/compose_files/%s/docker-compose.yml" % ( MEDIA_ROOT, str(self.organization.id), str(self.id), ) def save( self, force_insert=False, force_update=False, using=None, update_fields=None, ): if self.name == "": self.name = random_name(self.type) super(Node, self).save( force_insert, force_update, using, update_fields ) def delete(self, using=None, keep_parents=False): if self.compose_file: compose_file_path = Path(self.compose_file.path) if os.path.isdir(os.path.dirname(compose_file_path)): shutil.rmtree(os.path.dirname(compose_file_path)) super(Node, self).delete(using, keep_parents)