|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# TencentBlueKing is pleased to support the open source community by making |
| 3 | +# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. |
| 4 | +# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +# Licensed under the MIT License (the "License"); you may not use this file except |
| 6 | +# in compliance with the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://opensource.org/licenses/MIT |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 11 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | +# either express or implied. See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# We undertake not to change the open source license (MIT license) applicable |
| 16 | +# to the current version of the project delivered to anyone in the future. |
| 17 | + |
| 18 | +from django.core.exceptions import ValidationError |
| 19 | +from django.core.management.base import BaseCommand, CommandError |
| 20 | + |
| 21 | +from paasng.accessories.log.models import TenantLogConfig |
| 22 | +from paasng.core.tenant.user import get_init_tenant_id |
| 23 | + |
| 24 | + |
| 25 | +class Command(BaseCommand): |
| 26 | + help = "Create TenantLogConfig for a tenant" |
| 27 | + config_fields = ( |
| 28 | + "storage_cluster_id", |
| 29 | + "retention", |
| 30 | + "es_shards", |
| 31 | + "storage_replicas", |
| 32 | + "time_zone", |
| 33 | + ) |
| 34 | + |
| 35 | + def add_arguments(self, parser): |
| 36 | + tenant_group = parser.add_mutually_exclusive_group(required=True) |
| 37 | + tenant_group.add_argument("--tenant-id", help="tenant id") |
| 38 | + tenant_group.add_argument( |
| 39 | + "--default-tenant", |
| 40 | + action="store_true", |
| 41 | + default=False, |
| 42 | + help="为默认租户创建配置,不可与 --tenant-id 同时指定", |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + "--update", |
| 46 | + action="store_true", |
| 47 | + default=False, |
| 48 | + help="允许更新相同 tenant_id 已存在的配置", |
| 49 | + ) |
| 50 | + parser.add_argument("--storage-cluster-id", required=True, type=int, help="日志平台存储集群 ID") |
| 51 | + parser.add_argument("--retention", type=int, default=14, help="日志保存时间(天),默认 14") |
| 52 | + parser.add_argument("--es-shards", type=int, default=1, help="ES 索引分片数,默认 1") |
| 53 | + parser.add_argument("--storage-replicas", type=int, default=1, help="存储副本数,默认 1") |
| 54 | + parser.add_argument("--time-zone", type=int, default=8, help="时区,如 8 表示 UTC+8,默认 8") |
| 55 | + |
| 56 | + def handle(self, *args, **options): |
| 57 | + tenant_id = get_init_tenant_id() if options["default_tenant"] else options["tenant_id"] |
| 58 | + config_data = {field: options[field] for field in self.config_fields} |
| 59 | + allow_update = options["update"] |
| 60 | + existing = TenantLogConfig.objects.filter(tenant_id=tenant_id).first() |
| 61 | + |
| 62 | + if existing and not allow_update: |
| 63 | + raise CommandError(f"TenantLogConfig already exists for tenant_id={tenant_id}, use --update to overwrite") |
| 64 | + |
| 65 | + try: |
| 66 | + config = existing or TenantLogConfig(tenant_id=tenant_id) |
| 67 | + for field, value in config_data.items(): |
| 68 | + setattr(config, field, value) |
| 69 | + action = "Updated" if existing else "Created" |
| 70 | + |
| 71 | + config.full_clean() |
| 72 | + if existing: |
| 73 | + config.save(update_fields=[*config_data.keys(), "updated"]) |
| 74 | + else: |
| 75 | + config.save() |
| 76 | + except ValidationError as e: |
| 77 | + raise CommandError(f"Invalid TenantLogConfig data: {e}") |
| 78 | + |
| 79 | + self.stdout.write( |
| 80 | + self.style.SUCCESS( |
| 81 | + f"{action} TenantLogConfig for tenant_id={tenant_id}: " |
| 82 | + f"storage_cluster_id={config.storage_cluster_id}, retention={config.retention}, " |
| 83 | + f"es_shards={config.es_shards}, storage_replicas={config.storage_replicas}, " |
| 84 | + f"time_zone={config.time_zone}" |
| 85 | + ) |
| 86 | + ) |
0 commit comments