mirror of https://github.com/jumpserver/jumpserver
perf: 添加 gather facts automation
parent
c79c3f8aec
commit
75ec9d4173
|
@ -21,7 +21,6 @@ class ChangeSecretManager(BasePlaybookManager):
|
|||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.method_hosts_mapper = defaultdict(list)
|
||||
self.playbooks = []
|
||||
self.password_strategy = self.execution.automation.password_strategy
|
||||
self.ssh_key_strategy = self.execution.automation.ssh_key_strategy
|
||||
self._password_generated = None
|
||||
|
|
|
@ -1,77 +1,23 @@
|
|||
import os
|
||||
import shutil
|
||||
from copy import deepcopy
|
||||
from collections import defaultdict
|
||||
|
||||
import yaml
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from ops.ansible import PlaybookRunner
|
||||
from ..base.manager import BasePlaybookManager
|
||||
from assets.automations.methods import platform_automation_methods
|
||||
|
||||
|
||||
class GatherFactsManager(BasePlaybookManager):
|
||||
method_name = 'gather_facts'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.id_method_mapper = {
|
||||
method['id']: method
|
||||
for method in platform_automation_methods
|
||||
if method['method'] == self.method_name
|
||||
}
|
||||
self.method_hosts_mapper = defaultdict(list)
|
||||
self.playbooks = []
|
||||
self.host_asset_mapper = {}
|
||||
|
||||
def inventory_kwargs(self):
|
||||
return {
|
||||
}
|
||||
@classmethod
|
||||
def method_type(cls):
|
||||
return 'gather_facts'
|
||||
|
||||
def host_callback(self, host, asset=None, **kwargs):
|
||||
super().host_callback(host, asset=asset, **kwargs)
|
||||
self.host_asset_mapper[host['name']] = asset
|
||||
|
||||
def on_host_success(self, host, result):
|
||||
print("Host: {}".format(host))
|
||||
print("Result: {}".format(result))
|
||||
|
||||
def generate_playbook(self):
|
||||
playbook = []
|
||||
for method_id, host_names in self.method_hosts_mapper.items():
|
||||
method = self.id_method_mapper[method_id]
|
||||
method_playbook_dir_path = method['dir']
|
||||
method_playbook_dir_name = os.path.basename(method_playbook_dir_path)
|
||||
sub_playbook_dir = os.path.join(os.path.dirname(self.playbook_path), method_playbook_dir_name)
|
||||
shutil.copytree(method_playbook_dir_path, sub_playbook_dir)
|
||||
sub_playbook_path = os.path.join(sub_playbook_dir, 'main.yml')
|
||||
|
||||
with open(sub_playbook_path, 'r') as f:
|
||||
host_playbook_play = yaml.safe_load(f)
|
||||
|
||||
if isinstance(host_playbook_play, list):
|
||||
host_playbook_play = host_playbook_play[0]
|
||||
|
||||
step = 10
|
||||
hosts_grouped = [host_names[i:i+step] for i in range(0, len(host_names), step)]
|
||||
for i, hosts in enumerate(hosts_grouped):
|
||||
plays = []
|
||||
play = deepcopy(host_playbook_play)
|
||||
play['hosts'] = ':'.join(hosts)
|
||||
plays.append(play)
|
||||
|
||||
playbook_path = os.path.join(sub_playbook_dir, 'part_{}.yml'.format(i))
|
||||
with open(playbook_path, 'w') as f:
|
||||
yaml.safe_dump(plays, f)
|
||||
self.playbooks.append(playbook_path)
|
||||
|
||||
playbook.append({
|
||||
'name': method['name'] + ' for part {}'.format(i),
|
||||
'import_playbook': os.path.join(method_playbook_dir_name, 'part_{}.yml'.format(i))
|
||||
})
|
||||
|
||||
with open(self.playbook_path, 'w') as f:
|
||||
yaml.safe_dump(playbook, f)
|
||||
|
||||
print("Generate playbook done: " + self.playbook_path)
|
||||
|
||||
def get_runner(self):
|
||||
return PlaybookRunner(
|
||||
self.inventory_path,
|
||||
self.playbook_path,
|
||||
self.runtime_dir
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -8,9 +8,6 @@ class VerifyAutomation(BaseAutomation):
|
|||
class Meta:
|
||||
verbose_name = _("Verify strategy")
|
||||
|
||||
def to_attr_json(self):
|
||||
attr_json = super().to_attr_json()
|
||||
attr_json.update({
|
||||
'type': StrategyChoice.verify
|
||||
})
|
||||
return attr_json
|
||||
def save(self, *args, **kwargs):
|
||||
self.type = 'verify'
|
||||
super().save(*args, **kwargs)
|
||||
|
|
|
@ -11,6 +11,15 @@ from ops.tasks import execute_automation_strategy
|
|||
from assets.models import Node, Asset
|
||||
|
||||
|
||||
class AutomationTypes(models.TextChoices):
|
||||
ping = 'ping', _('Ping')
|
||||
gather_facts = 'gather_facts', _('Gather facts')
|
||||
create_account = 'create_account', _('Create account')
|
||||
change_secret = 'change_secret', _('Change secret')
|
||||
verify_account = 'verify_account', _('Verify account')
|
||||
gather_accounts = 'gather_accounts', _('Gather accounts')
|
||||
|
||||
|
||||
class BaseAutomation(JMSOrgBaseModel, PeriodTaskModelMixin):
|
||||
accounts = models.JSONField(default=list, verbose_name=_("Accounts"))
|
||||
nodes = models.ManyToManyField(
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import BaseAutomation
|
||||
|
||||
|
||||
class GatherFactsAutomation(BaseAutomation):
|
||||
class Meta:
|
||||
verbose_name = _("Gather asset facts")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.type = 'gather_facts'
|
||||
super().save(*args, **kwargs)
|
||||
|
|
@ -5,6 +5,8 @@ from django.contrib.auth.hashers import PBKDF2PasswordHasher
|
|||
|
||||
class Hasher:
|
||||
name = 'sm3'
|
||||
block_size = 64
|
||||
digest_size = 32
|
||||
|
||||
def __init__(self, key):
|
||||
self.key = key
|
||||
|
@ -12,10 +14,19 @@ class Hasher:
|
|||
def hexdigest(self):
|
||||
return sm3.sm3_hash(func.bytes_to_list(self.key))
|
||||
|
||||
def digest(self):
|
||||
return bytes.fromhex(self.hexdigest())
|
||||
|
||||
@staticmethod
|
||||
def hash(msg):
|
||||
def hash(msg=b''):
|
||||
return Hasher(msg)
|
||||
|
||||
def update(self, msg):
|
||||
self.key += msg
|
||||
|
||||
def copy(self):
|
||||
return Hasher(self.key)
|
||||
|
||||
|
||||
class PBKDF2SM3PasswordHasher(PBKDF2PasswordHasher):
|
||||
algorithm = "pbkdf2_sm3"
|
||||
|
|
Loading…
Reference in New Issue