From 0d2bfaa7684cf88f6d11b1b66810a3be1dd34ee1 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 2 Nov 2022 20:33:27 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E6=B7=BB=E5=8A=A0=20applet=20host=20ac?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0109_auto_20221102_2017.py | 53 +++++++++++++++++++ apps/assets/models/base.py | 9 ++-- apps/terminal/api/applet/host.py | 17 ++++++ .../migrations/0057_auto_20221102_1941.py | 24 +++++++++ apps/terminal/models/applet/host.py | 4 ++ apps/terminal/utils.py | 3 +- 6 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 apps/assets/migrations/0109_auto_20221102_2017.py create mode 100644 apps/terminal/migrations/0057_auto_20221102_1941.py diff --git a/apps/assets/migrations/0109_auto_20221102_2017.py b/apps/assets/migrations/0109_auto_20221102_2017.py new file mode 100644 index 000000000..a9dcc4446 --- /dev/null +++ b/apps/assets/migrations/0109_auto_20221102_2017.py @@ -0,0 +1,53 @@ +# Generated by Django 3.2.14 on 2022-11-02 12:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0108_auto_20221027_1053'), + ] + + operations = [ + migrations.AddField( + model_name='account', + name='is_active', + field=models.BooleanField(default=True, verbose_name='Is active'), + ), + migrations.AddField( + model_name='account', + name='updated_by', + field=models.CharField(blank=True, max_length=32, null=True, verbose_name='Updated by'), + ), + migrations.AddField( + model_name='accounttemplate', + name='is_active', + field=models.BooleanField(default=True, verbose_name='Is active'), + ), + migrations.AddField( + model_name='accounttemplate', + name='updated_by', + field=models.CharField(blank=True, max_length=32, null=True, verbose_name='Updated by'), + ), + migrations.AddField( + model_name='gateway', + name='updated_by', + field=models.CharField(blank=True, max_length=32, null=True, verbose_name='Updated by'), + ), + migrations.AlterField( + model_name='account', + name='date_created', + field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='accounttemplate', + name='date_created', + field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='gateway', + name='date_created', + field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created'), + ), + ] diff --git a/apps/assets/models/base.py b/apps/assets/models/base.py index f293b9a93..f40e65bad 100644 --- a/apps/assets/models/base.py +++ b/apps/assets/models/base.py @@ -6,7 +6,6 @@ import uuid from hashlib import md5 import sshpubkeys -from django.core.cache import cache from django.db import models from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -19,7 +18,7 @@ from common.utils import ( ) from common.db import fields from assets.const import Connectivity -from orgs.mixins.models import OrgModelMixin +from orgs.mixins.models import JMSOrgBaseModel logger = get_logger(__file__) @@ -48,14 +47,13 @@ class AbsConnectivity(models.Model): abstract = True -class BaseAccount(OrgModelMixin): +class BaseAccount(JMSOrgBaseModel): class SecretType(models.TextChoices): password = 'password', _('Password') ssh_key = 'ssh_key', _('SSH key') access_key = 'access_key', _('Access key') token = 'token', _('Token') - id = models.UUIDField(default=uuid.uuid4, primary_key=True) name = models.CharField(max_length=128, verbose_name=_("Name")) username = models.CharField(max_length=128, blank=True, verbose_name=_('Username'), db_index=True) secret_type = models.CharField( @@ -63,9 +61,8 @@ class BaseAccount(OrgModelMixin): ) secret = fields.EncryptTextField(blank=True, null=True, verbose_name=_('Secret')) privileged = models.BooleanField(verbose_name=_("Privileged"), default=False) + is_active = models.BooleanField(default=True, verbose_name=_("Is active")) comment = models.TextField(blank=True, verbose_name=_('Comment')) - date_created = models.DateTimeField(auto_now_add=True, verbose_name=_("Date created")) - date_updated = models.DateTimeField(auto_now=True, verbose_name=_("Date updated")) created_by = models.CharField(max_length=128, null=True, verbose_name=_('Created by')) @property diff --git a/apps/terminal/api/applet/host.py b/apps/terminal/api/applet/host.py index e5dee8c7f..c9e08e4eb 100644 --- a/apps/terminal/api/applet/host.py +++ b/apps/terminal/api/applet/host.py @@ -14,6 +14,23 @@ class AppletHostViewSet(viewsets.ModelViewSet): serializer_class = serializers.AppletHostSerializer queryset = AppletHost.objects.all() + @action(methods=['post'], detail=True) + def report(self, request, *args, **kwargs): + # TODO: + # 1. 上报 安装的 Applets 每小时 + # 2. Host 和 Terminal 关联 + instance = self.get_object() + instance.sync() + return Response({'msg': 'ok'}) + + @action(methods=['get'], detail=True) + def accounts(self, request, *args, **kwargs): + # TODO: + # 1. 返回 host 上的所有用户, host 可以去创建和更新 每小时 + # 2. 密码长度最少 8 位,包含大小写字母和数字和特殊字符 + instance = self.get_object() + return Response(instance.get_accounts()) + class AppletHostDeploymentViewSet(viewsets.ModelViewSet): serializer_class = serializers.AppletHostDeploymentSerializer diff --git a/apps/terminal/migrations/0057_auto_20221102_1941.py b/apps/terminal/migrations/0057_auto_20221102_1941.py new file mode 100644 index 000000000..56f1e699a --- /dev/null +++ b/apps/terminal/migrations/0057_auto_20221102_1941.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.14 on 2022-11-02 11:41 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0056_auto_20221101_1353'), + ] + + operations = [ + migrations.AddField( + model_name='applethost', + name='terminal', + field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='applet_host', to='terminal.terminal', verbose_name='Terminal'), + ), + migrations.AlterField( + model_name='appletpublication', + name='status', + field=models.CharField(default='ready', max_length=16, verbose_name='Status'), + ), + ] diff --git a/apps/terminal/models/applet/host.py b/apps/terminal/models/applet/host.py index 9c2338591..e21e173df 100644 --- a/apps/terminal/models/applet/host.py +++ b/apps/terminal/models/applet/host.py @@ -17,6 +17,10 @@ class AppletHost(Host): date_inited = models.DateTimeField(null=True, blank=True, verbose_name=_('Date inited')) date_synced = models.DateTimeField(null=True, blank=True, verbose_name=_('Date synced')) status = models.CharField(max_length=16, verbose_name=_('Status')) + terminal = models.OneToOneField( + 'terminal.Terminal', on_delete=models.PROTECT, null=True, blank=True, + related_name='applet_host', verbose_name=_('Terminal') + ) applets = models.ManyToManyField( 'Applet', verbose_name=_('Applet'), through='AppletPublication', through_fields=('host', 'applet'), diff --git a/apps/terminal/utils.py b/apps/terminal/utils.py index eed67f408..c41ce9bb3 100644 --- a/apps/terminal/utils.py +++ b/apps/terminal/utils.py @@ -5,13 +5,12 @@ from itertools import groupby, chain from django.conf import settings from django.core.files.storage import default_storage - import jms_storage from common.utils import get_logger +from tickets.models import TicketSession from . import const from .models import ReplayStorage -from tickets.models import TicketSession logger = get_logger(__name__)