2022-10-08 11:12:04 +00:00
|
|
|
import os
|
2023-12-05 11:09:17 +00:00
|
|
|
import shutil
|
2024-03-22 08:23:12 +00:00
|
|
|
import uuid
|
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
from django.conf import settings
|
2023-12-05 11:09:17 +00:00
|
|
|
from django.utils._os import safe_join
|
2024-04-22 05:51:52 +00:00
|
|
|
from .interface import interface
|
2022-09-30 10:49:45 +00:00
|
|
|
from .callback import DefaultCallback
|
2024-04-22 05:51:52 +00:00
|
|
|
from .exception import CommandInBlackListException
|
2023-07-17 05:59:45 +00:00
|
|
|
from ..utils import get_ansible_log_verbosity
|
2020-12-29 13:05:18 +00:00
|
|
|
|
2024-04-22 05:51:52 +00:00
|
|
|
__all__ = ['AdHocRunner', 'PlaybookRunner', 'SuperPlaybookRunner', 'UploadFileRunner']
|
2024-04-08 09:54:34 +00:00
|
|
|
|
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
class AdHocRunner:
|
|
|
|
cmd_modules_choices = ('shell', 'raw', 'command', 'script', 'win_shell')
|
2024-04-23 10:59:39 +00:00
|
|
|
need_local_connection_modules_choices = ("mysql", "postgresql", "sqlserver", "huawei")
|
2017-12-06 10:31:51 +00:00
|
|
|
|
2024-04-23 10:59:39 +00:00
|
|
|
def __init__(self, inventory, job_module, module, module_args='', pattern='*', project_dir='/tmp/',
|
|
|
|
extra_vars=None,
|
2022-12-27 08:06:21 +00:00
|
|
|
dry_run=False, timeout=-1):
|
2024-04-22 05:51:52 +00:00
|
|
|
if extra_vars is None:
|
|
|
|
extra_vars = {}
|
2022-09-30 10:49:45 +00:00
|
|
|
self.id = uuid.uuid4()
|
2017-12-10 16:29:25 +00:00
|
|
|
self.inventory = inventory
|
2022-09-30 10:49:45 +00:00
|
|
|
self.pattern = pattern
|
|
|
|
self.module = module
|
2024-04-23 10:59:39 +00:00
|
|
|
self.job_module = job_module
|
2022-09-30 10:49:45 +00:00
|
|
|
self.module_args = module_args
|
|
|
|
self.project_dir = project_dir
|
|
|
|
self.cb = DefaultCallback()
|
|
|
|
self.runner = None
|
2022-11-16 13:06:14 +00:00
|
|
|
self.extra_vars = extra_vars
|
2022-12-26 11:02:05 +00:00
|
|
|
self.dry_run = dry_run
|
2022-12-27 08:06:21 +00:00
|
|
|
self.timeout = timeout
|
2024-04-23 10:59:39 +00:00
|
|
|
self.envs = {}
|
2022-09-30 10:49:45 +00:00
|
|
|
|
|
|
|
def check_module(self):
|
|
|
|
if self.module not in self.cmd_modules_choices:
|
|
|
|
return
|
2023-05-31 09:16:48 +00:00
|
|
|
if self.module_args and self.module_args.split()[0] in settings.SECURITY_COMMAND_BLACKLIST:
|
2023-06-14 12:04:20 +00:00
|
|
|
raise CommandInBlackListException(
|
|
|
|
"Command is rejected by black list: {}".format(self.module_args.split()[0]))
|
2022-09-30 10:49:45 +00:00
|
|
|
|
2024-04-23 10:59:39 +00:00
|
|
|
def set_local_connection(self):
|
|
|
|
if self.job_module in self.need_local_connection_modules_choices:
|
|
|
|
self.envs.update({"LOCAL_CONNECTION_ENABLED": "1"})
|
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
def run(self, verbosity=0, **kwargs):
|
|
|
|
self.check_module()
|
2024-04-23 10:59:39 +00:00
|
|
|
self.set_local_connection()
|
2023-07-17 05:59:45 +00:00
|
|
|
verbosity = get_ansible_log_verbosity(verbosity)
|
2022-09-30 10:49:45 +00:00
|
|
|
|
2022-10-08 11:12:04 +00:00
|
|
|
if not os.path.exists(self.project_dir):
|
|
|
|
os.mkdir(self.project_dir, 0o755)
|
2024-03-22 08:23:12 +00:00
|
|
|
private_env = safe_join(self.project_dir, 'env')
|
|
|
|
if os.path.exists(private_env):
|
|
|
|
shutil.rmtree(private_env)
|
2022-10-08 11:12:04 +00:00
|
|
|
|
2024-04-22 05:51:52 +00:00
|
|
|
interface.run(
|
2022-12-27 08:06:21 +00:00
|
|
|
timeout=self.timeout if self.timeout > 0 else None,
|
2022-11-16 13:06:14 +00:00
|
|
|
extravars=self.extra_vars,
|
2024-04-23 10:59:39 +00:00
|
|
|
envvars=self.envs,
|
2022-09-30 10:49:45 +00:00
|
|
|
host_pattern=self.pattern,
|
|
|
|
private_data_dir=self.project_dir,
|
2017-03-05 15:30:14 +00:00
|
|
|
inventory=self.inventory,
|
2022-09-30 10:49:45 +00:00
|
|
|
module=self.module,
|
|
|
|
module_args=self.module_args,
|
|
|
|
verbosity=verbosity,
|
|
|
|
event_handler=self.cb.event_handler,
|
|
|
|
status_handler=self.cb.status_handler,
|
|
|
|
**kwargs
|
2017-12-06 10:31:51 +00:00
|
|
|
)
|
2022-09-30 10:49:45 +00:00
|
|
|
return self.cb
|
2017-03-05 15:30:14 +00:00
|
|
|
|
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
class PlaybookRunner:
|
2022-10-12 10:08:57 +00:00
|
|
|
def __init__(self, inventory, playbook, project_dir='/tmp/', callback=None):
|
2024-04-10 10:14:49 +00:00
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
self.id = uuid.uuid4()
|
2017-12-10 16:29:25 +00:00
|
|
|
self.inventory = inventory
|
2022-09-30 10:49:45 +00:00
|
|
|
self.playbook = playbook
|
|
|
|
self.project_dir = project_dir
|
2022-10-12 10:08:57 +00:00
|
|
|
if not callback:
|
|
|
|
callback = DefaultCallback()
|
|
|
|
self.cb = callback
|
2024-03-22 08:23:12 +00:00
|
|
|
self.envs = {}
|
2017-03-06 15:34:54 +00:00
|
|
|
|
2024-04-10 10:14:49 +00:00
|
|
|
def copy_playbook(self):
|
|
|
|
entry = os.path.basename(self.playbook)
|
|
|
|
playbook_dir = os.path.dirname(self.playbook)
|
|
|
|
project_playbook_dir = os.path.join(self.project_dir, "project")
|
2024-04-15 11:29:22 +00:00
|
|
|
shutil.copytree(playbook_dir, project_playbook_dir, dirs_exist_ok=True)
|
2024-04-10 10:14:49 +00:00
|
|
|
self.playbook = entry
|
|
|
|
|
2022-09-30 10:49:45 +00:00
|
|
|
def run(self, verbosity=0, **kwargs):
|
2024-04-10 10:14:49 +00:00
|
|
|
self.copy_playbook()
|
|
|
|
|
2023-07-17 05:59:45 +00:00
|
|
|
verbosity = get_ansible_log_verbosity(verbosity)
|
2024-03-22 08:23:12 +00:00
|
|
|
private_env = safe_join(self.project_dir, 'env')
|
|
|
|
if os.path.exists(private_env):
|
|
|
|
shutil.rmtree(private_env)
|
2018-03-30 14:03:43 +00:00
|
|
|
|
2024-04-22 05:51:52 +00:00
|
|
|
interface.run(
|
2022-09-30 10:49:45 +00:00
|
|
|
private_data_dir=self.project_dir,
|
2017-03-05 15:30:14 +00:00
|
|
|
inventory=self.inventory,
|
2022-09-30 10:49:45 +00:00
|
|
|
playbook=self.playbook,
|
|
|
|
verbosity=verbosity,
|
|
|
|
event_handler=self.cb.event_handler,
|
|
|
|
status_handler=self.cb.status_handler,
|
2023-01-16 11:02:09 +00:00
|
|
|
host_cwd=self.project_dir,
|
2024-03-22 08:23:12 +00:00
|
|
|
envvars=self.envs,
|
2022-09-30 10:49:45 +00:00
|
|
|
**kwargs
|
2017-03-05 15:30:14 +00:00
|
|
|
)
|
2022-09-30 10:49:45 +00:00
|
|
|
return self.cb
|
2022-10-08 08:55:14 +00:00
|
|
|
|
|
|
|
|
2024-03-22 08:23:12 +00:00
|
|
|
class SuperPlaybookRunner(PlaybookRunner):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.envs = {"LOCAL_CONNECTION_ENABLED": "1"}
|
|
|
|
|
|
|
|
|
2023-12-05 11:09:17 +00:00
|
|
|
class UploadFileRunner:
|
2024-04-11 10:19:07 +00:00
|
|
|
def __init__(self, inventory, project_dir, job_id, dest_path, callback=None):
|
2023-12-05 11:09:17 +00:00
|
|
|
self.id = uuid.uuid4()
|
|
|
|
self.inventory = inventory
|
2024-04-11 10:19:07 +00:00
|
|
|
self.project_dir = project_dir
|
2023-12-05 11:09:17 +00:00
|
|
|
self.cb = DefaultCallback()
|
2024-04-11 10:19:07 +00:00
|
|
|
upload_file_dir = safe_join(settings.SHARE_DIR, 'job_upload_file')
|
2023-12-05 11:09:17 +00:00
|
|
|
self.src_paths = safe_join(upload_file_dir, str(job_id))
|
2023-12-14 11:44:53 +00:00
|
|
|
self.dest_path = safe_join("/tmp", dest_path)
|
2023-12-05 11:09:17 +00:00
|
|
|
|
|
|
|
def run(self, verbosity=0, **kwargs):
|
|
|
|
verbosity = get_ansible_log_verbosity(verbosity)
|
2024-04-22 05:51:52 +00:00
|
|
|
interface.run(
|
2024-04-11 10:19:07 +00:00
|
|
|
private_data_dir=self.project_dir,
|
2023-12-05 11:09:17 +00:00
|
|
|
host_pattern="*",
|
|
|
|
inventory=self.inventory,
|
|
|
|
module='copy',
|
|
|
|
module_args=f"src={self.src_paths}/ dest={self.dest_path}",
|
|
|
|
verbosity=verbosity,
|
|
|
|
event_handler=self.cb.event_handler,
|
|
|
|
status_handler=self.cb.status_handler,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.src_paths)
|
|
|
|
except OSError as e:
|
|
|
|
print(f"del upload tmp dir {self.src_paths} failed! {e}")
|
|
|
|
return self.cb
|