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 AgentPatchBody(serializers.Serializer): name = serializers.CharField( min_length=NameMinLen, max_length=NameMaxLen, help_text=NameHelpText, required=False, allow_null=True, ) capacity = serializers.IntegerField( min_value=CapacityMinValue, required=False, allow_null=True, help_text=CapacityHelpText, ) log_level = serializers.ChoiceField( choices=LogLevel.to_choices(), required=False, allow_null=True, help_text=LogLevel.get_info("Log levels:"), )
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", )