class Agent(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of agent", default=make_uuid, editable=True, ) name = models.CharField( help_text="Agent name, can be generated automatically.", max_length=64, default=random_name("agent"), ) urls = models.URLField(help_text="Agent URL", null=True, blank=True) organization = models.ForeignKey( "Organization", null=True, on_delete=models.CASCADE, help_text="Organization of agent", related_name="agent", ) status = models.CharField( help_text="Status of agent", choices=HostStatus.to_choices(True), max_length=10, default=HostStatus.Active.name.lower(), ) type = models.CharField( help_text="Type of agent", choices=HostType.to_choices(True), max_length=32, default=HostType.Docker.name.lower(), ) config_file = models.FileField( help_text="Config file for agent", max_length=256, blank=True, upload_to=get_agent_config_file_path, ) created_at = models.DateTimeField(help_text="Create time of agent", auto_now_add=True) # free_port = models.IntegerField( # help_text="Agent free port.", # default=30000, # ) free_ports = ArrayField(models.IntegerField(blank=True), help_text="Agent free ports.", null=True) def delete(self, using=None, keep_parents=False): if self.config_file: if os.path.isfile(self.config_file.path): os.remove(self.config_file.path) shutil.rmtree(os.path.dirname(self.config_file.path), ignore_errors=True) super(Agent, self).delete(using, keep_parents) class Meta: ordering = ("-created_at", )
class AgentCreateBody(serializers.Serializer): name = serializers.CharField(min_length=NameMinLen, max_length=NameMaxLen, help_text=NameHelpText) worker_api = serializers.CharField( min_length=WorkerAPIMinLen, max_length=WorkerAPIMaxLen, help_text=WorkerApiHelpText, ) capacity = serializers.IntegerField(min_value=CapacityMinValue, help_text=CapacityHelpText) log_level = serializers.ChoiceField( choices=LogLevel.to_choices(), help_text=LogLevel.get_info("Log levels:"), ) type = serializers.ChoiceField( choices=HostType.to_choices(), help_text=HostType.get_info("Agent types:"), )
class NodeCreateBody(serializers.ModelSerializer): agent_type = serializers.ChoiceField( help_text="Agent type", choices=HostType.to_choices(True), required=False, ) ca = FabricCASerializer(help_text="CA configuration for node", required=False) class Meta: model = Node fields = ( "network_type", "network_version", "type", "agent_type", "agent", "ca", ) extra_kwargs = { "network_type": { "required": True }, "network_version": { "required": True }, "type": { "required": True }, } def validate(self, attrs): network_type = attrs.get("network_type") node_type = attrs.get("type") network_version = attrs.get("network_version") agent_type = attrs.get("agent_type") agent = attrs.get("agent") if network_type == NetworkType.Fabric.name.lower(): if network_version not in FabricVersions.values(): raise serializers.ValidationError("Not valid fabric version") if node_type not in FabricNodeType.names(): raise serializers.ValidationError( "Not valid node type for %s" % network_type) if agent_type is None and agent is None: raise serializers.ValidationError("Please set agent_type or agent") if agent_type and agent: if agent_type != agent.type: raise serializers.ValidationError( "agent type not equal to agent") return attrs
class Agent(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of agent", default=make_uuid, editable=True, ) name = models.CharField( help_text="Agent name, can be generated automatically.", max_length=64, default=random_name("agent"), ) worker_api = models.CharField( help_text="Worker api of agent", max_length=128, default="" ) govern = models.ForeignKey( Govern, help_text="Govern of agent", null=True, on_delete=models.CASCADE, ) organization = models.ForeignKey( "Organization", null=True, on_delete=models.CASCADE, help_text="Organization of agent", ) status = models.CharField( help_text="Status of agent", choices=HostStatus.to_choices(True), max_length=10, default=HostStatus.Active.name.lower(), ) log_level = models.CharField( help_text="Log level of agent", choices=LogLevel.to_choices(True), max_length=10, default=LogLevel.Info.name.lower(), ) type = models.CharField( help_text="Type of agent", choices=HostType.to_choices(True), max_length=32, default=HostType.Docker.name.lower(), ) schedulable = models.BooleanField( help_text="Whether agent can be scheduled", default=True ) capacity = models.IntegerField( help_text="Capacity of agent", default=1, validators=[MinValueValidator(1), MaxValueValidator(MAX_CAPACITY)], ) node_capacity = models.IntegerField( help_text="Capacity of node", default=6, validators=[ MinValueValidator(1), MaxValueValidator(MAX_NODE_CAPACITY), ], ) created_at = models.DateTimeField( help_text="Create time of agent", auto_now_add=True ) class Meta: ordering = ("-created_at",)
class Agent(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of agent", default=make_uuid, editable=True, ) name = models.CharField( help_text="Agent name, can be generated automatically.", max_length=64, default=random_name("agent"), ) image = models.CharField(help_text="Image name for deploy agent", max_length=64, default="") ip = models.GenericIPAddressField(help_text="Agent IP Address") govern = models.ForeignKey( Govern, help_text="Govern of agent", null=True, on_delete=models.CASCADE, ) organization = models.ForeignKey( "Organization", null=True, on_delete=models.CASCADE, help_text="Organization of agent", ) status = models.CharField( help_text="Status of agent", choices=HostStatus.to_choices(True), max_length=10, default=HostStatus.Active.name.lower(), ) log_level = models.CharField( help_text="Log level of agent", choices=LogLevel.to_choices(True), max_length=10, default=LogLevel.Info.name.lower(), ) type = models.CharField( help_text="Type of agent", choices=HostType.to_choices(True), max_length=32, default=HostType.Docker.name.lower(), ) schedulable = models.BooleanField( help_text="Whether agent can be scheduled", default=True) capacity = models.IntegerField( help_text="Capacity of agent", default=1, validators=[MinValueValidator(1), MaxValueValidator(MAX_CAPACITY)], ) node_capacity = models.IntegerField( help_text="Capacity of node", default=6, validators=[ MinValueValidator(1), MaxValueValidator(MAX_NODE_CAPACITY), ], ) config_file = models.FileField( help_text="Config file for agent", max_length=256, blank=True, upload_to=get_agent_config_file_path, ) created_at = models.DateTimeField(help_text="Create time of agent", auto_now_add=True) def delete(self, using=None, keep_parents=False): if self.config_file: if os.path.isfile(self.config_file.path): os.remove(self.config_file.path) shutil.rmtree(os.path.dirname(self.config_file.path), ignore_errors=True) super(Agent, self).delete(using, keep_parents) class Meta: ordering = ("-created_at", )