Skip to content

Commit

Permalink
feature: 提供 Agent 包管理后台接口 (closed TencentBlueKing#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Nov 10, 2023
1 parent 6aa02ca commit 24965a0
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 77 deletions.
22 changes: 8 additions & 14 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,16 +2489,13 @@ class Meta:
]


class AgentPackages(models.Model):
class GsePackages(orm.OperateRecordModel):
pkg_name = models.CharField(_("压缩包名"), max_length=128)
version = models.CharField(_("版本号"), max_length=128)
module = models.CharField(_("所属服务"), max_length=32)
project = models.CharField(_("工程名"), max_length=32, db_index=True)
pkg_size = models.IntegerField(_("包大小"))
pkg_path = models.CharField(_("包路径"), max_length=128)
md5 = models.CharField(_("md5值"), max_length=32)
pkg_mtime = models.CharField(_("包更新时间"), max_length=48)
pkg_ctime = models.CharField(_("包创建时间"), max_length=48)
location = models.CharField(_("安装包链接"), max_length=512)
os = models.CharField(
_("系统类型"),
Expand All @@ -2510,32 +2507,29 @@ class AgentPackages(models.Model):
cpu_arch = models.CharField(
_("CPU类型"), max_length=32, choices=constants.CPU_CHOICES, default=constants.CpuType.x86_64, db_index=True
)
creator = models.CharField(_("操作人"), max_length=45, default="admin")

is_release_version = models.BooleanField(_("是否已经发布版本"), default=True, db_index=True)
# 由于创建记录时,文件可能仍然在传输过程中,因此需要标志位判断是否已经可用
is_ready = models.BooleanField(_("插件是否可用"), default=True)

version_log = models.TextField(_("版本日志"), null=True, blank=True)
version_log_en = models.TextField(_("英文版本日志"), null=True, blank=True)

class Meta:
verbose_name = _("Agent包(AgentPackages)")
verbose_name_plural = _("Agent包(AgentPackages)")
verbose_name = _("Gse包(GsePackages)")
verbose_name_plural = _("Gse包(GsePackages)")


class AgentPackageDesc(models.Model):
class GsePackageDesc(orm.OperateRecordModel):
"""
Agent包信息表
Gse包描述表
"""

# 安装包名需要全局唯一,防止冲突
name = models.CharField(_("安装包名"), max_length=32, unique=True, db_index=True)
project = models.CharField(_("工程名"), max_length=32, unique=True, db_index=True)
description = models.TextField(_("安装包描述"))
module = models.CharField(_("所属服务"), max_length=32)
description_en = models.TextField(_("英文插件描述"), null=True, blank=True)
category = models.CharField(_("所属范围"), max_length=32, choices=constants.CATEGORY_CHOICES)

class Meta:
verbose_name = _("Agent信息(AgentPackageDesc)")
verbose_name_plural = _("Agent信息(AgentPackageDesc)")
verbose_name = _("Gse包描述(GsePackageDesc)")
verbose_name_plural = _("Gse包描述(GsePackageDesc)")
10 changes: 10 additions & 0 deletions apps/node_man/permissions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available.
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
27 changes: 27 additions & 0 deletions apps/node_man/permissions/package_manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available.
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from django.utils.translation import ugettext_lazy as _
from rest_framework import permissions

from apps.node_man.handlers.iam import IamHandler
from apps.utils.local import get_request_username


class PackageManagePermission(permissions.BasePermission):
message = _("您没有该操作的权限")

def has_permission(self, request, view):

if IamHandler().is_superuser(get_request_username()):
return True

return False
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,20 @@
from rest_framework import serializers

from apps.exceptions import ValidationError
from apps.node_man.models import AgentPackages


class AgentPackageSerializer(serializers.ModelSerializer):
class meta:
model = AgentPackages
fields = "__all__"
from apps.node_man.constants import GsePackageCode


class TagsSerializer(serializers.Serializer):
id = serializers.CharField()
name = serializers.CharField()
children = serializers.ListField()


class ConditionsSerializer(serializers.Serializer):
key = serializers.ChoiceField(choices=["version", "os_cpu_arch", "tags", "is_ready"])
values = serializers.ListField()


class SearchSerializer(serializers.Serializer):
os_cpu_arch = serializers.CharField(required=False)
tags = serializers.ListField(required=False)


class PackageDescSearchSerializer(serializers.Serializer):
os_cpu_arch = serializers.CharField(required=False)


class PackageSerializer(serializers.Serializer):
id = serializers.IntegerField()
pkg_name = serializers.CharField()
Expand All @@ -49,7 +35,6 @@ class PackageSerializer(serializers.Serializer):
tags = TagsSerializer(many=True)
creator = serializers.CharField()
pkg_ctime = serializers.DateTimeField()
host_count = serializers.IntegerField()
is_ready = serializers.BooleanField()


Expand All @@ -61,7 +46,7 @@ class PackageDescSerializer(serializers.Serializer):
is_ready = serializers.BooleanField()


class SearchResponseSerializer(serializers.Serializer):
class ListResponseSerializer(serializers.Serializer):
total = serializers.IntegerField()
list = PackageSerializer(many=True)

Expand All @@ -75,6 +60,10 @@ class OperateSerializer(serializers.Serializer):
is_ready = serializers.BooleanField()


class QuickSearchSerializer(serializers.Serializer):
project = serializers.ChoiceField(choices=GsePackageCode.list_choices())


# TODO 与plugin相同可抽取公共Serializer
class UploadSerializer(serializers.Serializer):
class PkgFileField(serializers.FileField):
Expand Down
5 changes: 2 additions & 3 deletions apps/node_man/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
)
from apps.node_man.views.healthz import HealthzViewSet
from apps.node_man.views.host_v2 import HostV2ViewSet
from apps.node_man.views.package_manage import (
AgentPackageDescViewSet,
from apps.node_man.views.package_manage import ( # AgentPackageDescViewSet,
PackageManageViewSet,
)
from apps.node_man.views.plugin import GsePluginViewSet
Expand Down Expand Up @@ -72,7 +71,7 @@
router.register(r"healthz", HealthzViewSet, basename="healthz")
router.register(r"sync_task", SyncTaskViewSet, basename="sync_task")
router.register(r"agent/package", PackageManageViewSet, basename="package_manage")
router.register(r"agent/package_desc", AgentPackageDescViewSet, basename="package_desc")
# router.register(r"agent/package_desc", AgentPackageDescViewSet, basename="package_desc")

biz_dispatcher = DjangoBasicResourceApiDispatcher(iam, settings.BK_IAM_SYSTEM_ID)
biz_dispatcher.register("biz", BusinessResourceProvider())
Expand Down
30 changes: 30 additions & 0 deletions apps/node_man/utils/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available.
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import django_filters
from django_filters.rest_framework import FilterSet

from apps.node_man.models import GsePackages


class GsePackageFilter(FilterSet):
tags = django_filters.CharFilter(name="tags", method="filter_tags")
os_cpu_arch = django_filters.CharFilter(name="os_cpu_arch", method="filter_os_cpu_arch")

def filter_tags(self, queryset, name, value):
pass

def filter_os_cpu_arch(self, queryset, name, value):
pass

class Meta:
model = GsePackages
fields = ["os_cpu_arch", "tags", "project", "created_by", "is_ready", "version"]
Loading

0 comments on commit 24965a0

Please sign in to comment.