From 25bded69ab540e36d67d1897efc2cd8ba4c41917 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 8 Sep 2022 20:31:04 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E5=92=8C=E8=B5=84=E4=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/category.py | 0 apps/assets/const.py | 2 +- .../migrations/0111_auto_20220908_1958.py | 29 +++++ apps/assets/models/platform.py | 1 + .../host/change_password_linux/main.yml | 2 - apps/assets/serializers/platform.py | 16 +-- ...tegytask_changeauthstrategy_collectstra.py | 123 ++++++++++++++++++ 7 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 apps/assets/api/category.py create mode 100644 apps/assets/migrations/0111_auto_20220908_1958.py create mode 100644 apps/ops/migrations/0023_automationstrategy_automationstrategyexecution_automationstrategytask_changeauthstrategy_collectstra.py diff --git a/apps/assets/api/category.py b/apps/assets/api/category.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/assets/const.py b/apps/assets/const.py index cbb3abd89..93b6032d3 100644 --- a/apps/assets/const.py +++ b/apps/assets/const.py @@ -92,7 +92,7 @@ class HostTypes(PlatformMixin, ChoicesMixin, models.TextChoices): def platform_constraints(cls): return { cls.LINUX: { - '_protocols': ['ssh', 'sftp', 'rdp', 'vnc', 'telnet'] + '_protocols': ['ssh', 'rdp', 'vnc', 'telnet'] }, cls.WINDOWS: { '_protocols': ['ssh', 'rdp', 'vnc'], diff --git a/apps/assets/migrations/0111_auto_20220908_1958.py b/apps/assets/migrations/0111_auto_20220908_1958.py new file mode 100644 index 000000000..7fb913fae --- /dev/null +++ b/apps/assets/migrations/0111_auto_20220908_1958.py @@ -0,0 +1,29 @@ +# Generated by Django 3.2.14 on 2022-09-08 11:58 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0110_auto_20220901_1542'), + ] + + operations = [ + migrations.AddField( + model_name='platform', + name='domain_enabled', + field=models.BooleanField(default=True, verbose_name='Domain enalbed'), + ), + migrations.AlterField( + model_name='account', + name='asset', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='accounts', to='assets.asset', verbose_name='Asset'), + ), + migrations.AlterField( + model_name='asset', + name='name', + field=models.CharField(max_length=128, verbose_name='Name'), + ), + ] diff --git a/apps/assets/models/platform.py b/apps/assets/models/platform.py index 9feb5325a..ebf289038 100644 --- a/apps/assets/models/platform.py +++ b/apps/assets/models/platform.py @@ -31,6 +31,7 @@ class Platform(models.Model): comment = models.TextField(blank=True, null=True, verbose_name=_("Comment")) # 资产有关的 charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset")) + domain_enabled = models.BooleanField(default=True, verbose_name=_("Domain enalbed")) protocols_enabled = models.BooleanField(default=True, verbose_name=_("Protocols enabled")) protocols = models.ManyToManyField(PlatformProtocol, blank=True, verbose_name=_("Protocols")) gather_facts_enabled = models.BooleanField(default=False, verbose_name=_("Gather facts enabled")) diff --git a/apps/assets/playbooks/platform/host/change_password_linux/main.yml b/apps/assets/playbooks/platform/host/change_password_linux/main.yml index 402c7fa8d..6b5f0df66 100644 --- a/apps/assets/playbooks/platform/host/change_password_linux/main.yml +++ b/apps/assets/playbooks/platform/host/change_password_linux/main.yml @@ -1,4 +1,3 @@ -{% for account in accounts %} - hosts: {{ account.asset.name }} vars: account: @@ -7,4 +6,3 @@ public_key: {{ account.public_key }} roles: - change_password -{% endfor %} diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py index 600098b86..9559ea70d 100644 --- a/apps/assets/serializers/platform.py +++ b/apps/assets/serializers/platform.py @@ -17,14 +17,13 @@ class ProtocolSettingSerializer(serializers.Serializer): ('tls', 'TLS'), ('nla', 'NLA'), ] - # Common - required = serializers.BooleanField(required=True, initial=False, label=_("Required")) - # RDP console = serializers.BooleanField(required=False) - security = serializers.ChoiceField(choices=SECURITY_CHOICES, default='any', required=False) + security = serializers.ChoiceField(choices=SECURITY_CHOICES, default='any') + # SFTP - sftp_home = serializers.CharField(default='/tmp', required=False) + sftp_enabled = serializers.BooleanField(default=True, label=_("SFTP enabled")) + sftp_home = serializers.CharField(default='/tmp', label=_("SFTP home")) class PlatformProtocolsSerializer(serializers.ModelSerializer): @@ -39,7 +38,6 @@ class PlatformSerializer(JMSWritableNestedModelSerializer): type = LabeledChoiceField(choices=AllTypes.choices, label=_("Type")) category = LabeledChoiceField(choices=Category.choices, label=_("Category")) protocols = PlatformProtocolsSerializer(label=_('Protocols'), many=True, required=False) - type_constraints = serializers.ReadOnlyField(required=False, read_only=True) su_method = LabeledChoiceField( choices=[('sudo', 'sudo su -'), ('su', 'su - ')], label='切换方式', required=False, default='sudo' @@ -49,17 +47,17 @@ class PlatformSerializer(JMSWritableNestedModelSerializer): model = Platform fields_mini = ['id', 'name', 'internal'] fields_small = fields_mini + [ - 'category', 'type', + 'category', 'type', 'charset', ] fields = fields_small + [ - 'protocols_enabled', 'protocols', + 'protocols_enabled', 'protocols', 'domain_enabled', 'gather_facts_enabled', 'gather_facts_method', 'su_enabled', 'su_method', 'gather_accounts_enabled', 'gather_accounts_method', 'create_account_enabled', 'create_account_method', 'verify_account_enabled', 'verify_account_method', 'change_password_enabled', 'change_password_method', - 'type_constraints', 'comment', 'charset', + 'comment', ] extra_kwargs = { 'su_enabled': {'label': '启用切换账号'}, diff --git a/apps/ops/migrations/0023_automationstrategy_automationstrategyexecution_automationstrategytask_changeauthstrategy_collectstra.py b/apps/ops/migrations/0023_automationstrategy_automationstrategyexecution_automationstrategytask_changeauthstrategy_collectstra.py new file mode 100644 index 000000000..361869794 --- /dev/null +++ b/apps/ops/migrations/0023_automationstrategy_automationstrategyexecution_automationstrategytask_changeauthstrategy_collectstra.py @@ -0,0 +1,123 @@ +# Generated by Django 3.2.14 on 2022-09-08 11:58 + +import common.db.fields +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('assets', '0111_auto_20220908_1958'), + ('ops', '0022_auto_20220817_1346'), + ] + + operations = [ + migrations.CreateModel( + name='AutomationStrategy', + fields=[ + ('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')), + ('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), + ('date_created', models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created')), + ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), + ('name', models.CharField(max_length=128, verbose_name='Name')), + ('is_periodic', models.BooleanField(default=False)), + ('interval', models.IntegerField(blank=True, default=24, null=True, verbose_name='Cycle perform')), + ('crontab', models.CharField(blank=True, max_length=128, null=True, verbose_name='Regularly perform')), + ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), + ('accounts', models.JSONField(default=list, verbose_name='Accounts')), + ('comment', models.TextField(blank=True, verbose_name='Comment')), + ('assets', models.ManyToManyField(blank=True, related_name='automation_strategy', to='assets.Asset', verbose_name='Assets')), + ('nodes', models.ManyToManyField(blank=True, related_name='automation_strategy', to='assets.Node', verbose_name='Nodes')), + ], + options={ + 'verbose_name': 'Automation plan', + 'unique_together': {('org_id', 'name')}, + }, + ), + migrations.CreateModel( + name='AutomationStrategyExecution', + fields=[ + ('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')), + ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), + ('date_created', models.DateTimeField(auto_now_add=True)), + ('timedelta', models.FloatField(default=0.0, null=True, verbose_name='Time')), + ('date_start', models.DateTimeField(auto_now_add=True, verbose_name='Date start')), + ('snapshot', common.db.fields.EncryptJsonDictTextField(blank=True, default=dict, null=True, verbose_name='Automation snapshot')), + ('trigger', models.CharField(choices=[('manual', 'Manual trigger'), ('timing', 'Timing trigger')], default='manual', max_length=128, verbose_name='Trigger mode')), + ('strategy', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='execution', to='ops.automationstrategy', verbose_name='Automation strategy')), + ], + options={ + 'verbose_name': 'Automation strategy execution', + }, + ), + migrations.CreateModel( + name='CollectStrategy', + fields=[ + ('automationstrategy_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='ops.automationstrategy')), + ], + options={ + 'verbose_name': 'Collect strategy', + }, + bases=('ops.automationstrategy',), + ), + migrations.CreateModel( + name='PushStrategy', + fields=[ + ('automationstrategy_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='ops.automationstrategy')), + ], + options={ + 'verbose_name': 'Push strategy', + }, + bases=('ops.automationstrategy',), + ), + migrations.CreateModel( + name='VerifyStrategy', + fields=[ + ('automationstrategy_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='ops.automationstrategy')), + ], + options={ + 'verbose_name': 'Verify strategy', + }, + bases=('ops.automationstrategy',), + ), + migrations.CreateModel( + name='AutomationStrategyTask', + fields=[ + ('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')), + ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), + ('is_success', models.BooleanField(default=False, verbose_name='Is success')), + ('timedelta', models.FloatField(default=0.0, null=True, verbose_name='Time')), + ('date_start', models.DateTimeField(auto_now_add=True, verbose_name='Date start')), + ('reason', models.CharField(blank=True, max_length=1024, null=True, verbose_name='Reason')), + ('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='assets.account', verbose_name='Account')), + ('asset', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='assets.asset', verbose_name='Asset')), + ('execution', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='task', to='ops.automationstrategyexecution', verbose_name='Automation strategy execution')), + ], + options={ + 'verbose_name': 'Automation strategy task', + }, + ), + migrations.CreateModel( + name='ChangeAuthStrategy', + fields=[ + ('automationstrategy_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='ops.automationstrategy')), + ('is_password', models.BooleanField(default=True)), + ('password_strategy', models.CharField(blank=True, choices=[('custom', 'Custom password'), ('random_one', 'All assets use the same random password'), ('random_all', 'All assets use different random password')], max_length=128, null=True, verbose_name='Password strategy')), + ('password_rules', common.db.fields.JsonDictCharField(blank=True, max_length=2048, null=True, verbose_name='Password rules')), + ('password', common.db.fields.EncryptCharField(blank=True, max_length=256, null=True, verbose_name='Password')), + ('is_ssh_key', models.BooleanField(default=False)), + ('ssh_key_strategy', models.CharField(blank=True, choices=[('add', 'Append SSH KEY'), ('set', 'Empty and append SSH KEY'), ('set_jms', 'Replace (The key generated by JumpServer) ')], max_length=128, null=True, verbose_name='SSH Key strategy')), + ('private_key', common.db.fields.EncryptTextField(blank=True, max_length=4096, null=True, verbose_name='SSH private key')), + ('public_key', common.db.fields.EncryptTextField(blank=True, max_length=4096, null=True, verbose_name='SSH public key')), + ('recipients', models.ManyToManyField(blank=True, related_name='recipients_change_auth_strategy', to=settings.AUTH_USER_MODEL, verbose_name='Recipient')), + ], + options={ + 'verbose_name': 'Change auth strategy', + }, + bases=('ops.automationstrategy',), + ), + ]