mirror of
https://github.com/netinvent/npbackup.git
synced 2025-10-02 09:46:48 +08:00
GUI: WIP task creation for group operations
This commit is contained in:
parent
272b3a8b52
commit
768f3b500b
1 changed files with 61 additions and 18 deletions
|
@ -26,6 +26,8 @@ logger = getLogger()
|
||||||
def create_scheduled_task(
|
def create_scheduled_task(
|
||||||
config_file: str,
|
config_file: str,
|
||||||
type: str,
|
type: str,
|
||||||
|
repo: str = None,
|
||||||
|
group: str = None,
|
||||||
interval_minutes: int = None,
|
interval_minutes: int = None,
|
||||||
hour: int = None,
|
hour: int = None,
|
||||||
minute: int = None,
|
minute: int = None,
|
||||||
|
@ -65,29 +67,49 @@ def create_scheduled_task(
|
||||||
if not os.path.isabs(config_file):
|
if not os.path.isabs(config_file):
|
||||||
config_file = os.path.join(CURRENT_DIR, config_file)
|
config_file = os.path.join(CURRENT_DIR, config_file)
|
||||||
|
|
||||||
|
if repo:
|
||||||
|
if repo == "__all__":
|
||||||
|
subject = "all repositories"
|
||||||
|
else:
|
||||||
|
subject = f"repo {repo}"
|
||||||
|
object_args = f" --repo-name {repo}"
|
||||||
|
elif group:
|
||||||
|
if group == "__all__":
|
||||||
|
subject = "all groups"
|
||||||
|
else:
|
||||||
|
subject = f"group {group}"
|
||||||
|
object_args = f" --repo-group {group}"
|
||||||
|
else:
|
||||||
|
subject = f"default repo"
|
||||||
|
object_args = ""
|
||||||
if interval_minutes:
|
if interval_minutes:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Creating scheduled task {type} to run every {interval_minutes} minutes"
|
f"Creating scheduled task {type} for {subject} to run every {interval_minutes} minutes"
|
||||||
)
|
)
|
||||||
elif hour and minute:
|
elif hour and minute:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Creating scheduled task {type} to run at everyday at {hour}h{minute}"
|
f"Creating scheduled task {type} for {subject} to run at everyday at {hour}h{minute}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
return create_scheduled_task_windows(
|
return create_scheduled_task_windows(
|
||||||
config_file, type, CURRENT_EXECUTABLE, interval_minutes, hour, minute
|
config_file, type, CURRENT_EXECUTABLE, subject, object_args, interval_minutes, hour, minute
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return create_scheduled_task_unix(
|
return create_scheduled_task_unix(
|
||||||
config_file, type, CURRENT_EXECUTABLE, interval_minutes, hour, minute
|
config_file, type, CURRENT_EXECUTABLE, subject, object_args, interval_minutes, hour, minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def
|
||||||
|
|
||||||
|
|
||||||
def create_scheduled_task_unix(
|
def create_scheduled_task_unix(
|
||||||
config_file,
|
config_file: str,
|
||||||
type,
|
type: str,
|
||||||
cli_executable_path,
|
cli_executable_path: str,
|
||||||
|
subject: str,
|
||||||
|
object_args: str,
|
||||||
interval_minutes: int = None,
|
interval_minutes: int = None,
|
||||||
hour: int = None,
|
hour: int = None,
|
||||||
minute: int = None,
|
minute: int = None,
|
||||||
|
@ -100,14 +122,14 @@ def create_scheduled_task_unix(
|
||||||
cron_file = "/etc/cron.d/npbackup"
|
cron_file = "/etc/cron.d/npbackup"
|
||||||
|
|
||||||
if interval_minutes is not None:
|
if interval_minutes is not None:
|
||||||
TASK_ARGS = f'-c "{config_file}" --{type} --run-as-cli'
|
TASK_ARGS = f'-c "{config_file}" --{type} --run-as-cli{object_args}'
|
||||||
trigger = f"*/{interval_minutes} * * * *"
|
trigger = f"*/{interval_minutes} * * * *"
|
||||||
elif hour is not None and minute is not None:
|
elif hour is not None and minute is not None:
|
||||||
if type == "backup":
|
if type == "backup":
|
||||||
force_opt = " --force"
|
force_opt = " --force"
|
||||||
else:
|
else:
|
||||||
force_opt = ""
|
force_opt = ""
|
||||||
TASK_ARGS = f'-c "{config_file}" --{type}{force_opt} --run-as-cli'
|
TASK_ARGS = f'-c "{config_file}" --{type}{force_opt} --run-as-cli{object_args}'
|
||||||
trigger = f"{minute} {hour} * * * root"
|
trigger = f"{minute} {hour} * * * root"
|
||||||
else:
|
else:
|
||||||
raise ValueError("Bogus trigger given")
|
raise ValueError("Bogus trigger given")
|
||||||
|
@ -123,7 +145,7 @@ def create_scheduled_task_unix(
|
||||||
with open(cron_file, "r", encoding="utf-8") as file_handle:
|
with open(cron_file, "r", encoding="utf-8") as file_handle:
|
||||||
current_crontab = file_handle.readlines()
|
current_crontab = file_handle.readlines()
|
||||||
for line in current_crontab:
|
for line in current_crontab:
|
||||||
if f"--{type}" in line:
|
if f"--{type}" in line and {config_file} in line:
|
||||||
logger.info(f"Replacing existing {type} task")
|
logger.info(f"Replacing existing {type} task")
|
||||||
if replaced:
|
if replaced:
|
||||||
logger.info(f"Skipping duplicate {type} task")
|
logger.info(f"Skipping duplicate {type} task")
|
||||||
|
@ -148,10 +170,31 @@ def create_scheduled_task_unix(
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def get_scheduled_task_name_windows(
|
||||||
|
config_file: str,
|
||||||
|
type: str,
|
||||||
|
subject: str
|
||||||
|
) -> str:
|
||||||
|
return f"{PROGRAM_NAME} - {type.capitalize()} {config_file} {subject}"
|
||||||
|
|
||||||
|
|
||||||
|
def scheduled_task_exists_windows(
|
||||||
|
task_name
|
||||||
|
) -> bool:
|
||||||
|
exit_code, _ = command_runner(
|
||||||
|
'schtasks /TN "{}"'.format(task_name),
|
||||||
|
windows_no_window=True,
|
||||||
|
encoding="cp437",
|
||||||
|
)
|
||||||
|
return True if exit_code == 0 else False
|
||||||
|
|
||||||
|
|
||||||
def create_scheduled_task_windows(
|
def create_scheduled_task_windows(
|
||||||
config_file,
|
config_file: str,
|
||||||
type,
|
type: str,
|
||||||
cli_executable_path,
|
cli_executable_path: str,
|
||||||
|
subject: str,
|
||||||
|
object_args: str,
|
||||||
interval_minutes: int = None,
|
interval_minutes: int = None,
|
||||||
hour: int = None,
|
hour: int = None,
|
||||||
minute: int = None,
|
minute: int = None,
|
||||||
|
@ -163,12 +206,12 @@ def create_scheduled_task_windows(
|
||||||
else:
|
else:
|
||||||
runner = cli_executable_path
|
runner = cli_executable_path
|
||||||
task_args = ""
|
task_args = ""
|
||||||
temp_task_file = os.path.join(tempfile.gettempdir(), "backup_task.xml")
|
temp_task_file = os.path.join(tempfile.gettempdir(), "npbackup_task.xml")
|
||||||
|
|
||||||
task_name = f"{PROGRAM_NAME} - {type.capitalize()}"
|
task_name = get_scheduled_task_name_windows(config_file, type, subject)
|
||||||
|
|
||||||
if interval_minutes is not None:
|
if interval_minutes is not None:
|
||||||
task_args = f'{task_args}-c "{config_file}" --{type} --run-as-cli'
|
task_args = f'{task_args}-c "{config_file}" --{type} --run-as-cli{object_args}'
|
||||||
start_date = datetime.datetime.now().replace(microsecond=0).isoformat()
|
start_date = datetime.datetime.now().replace(microsecond=0).isoformat()
|
||||||
trigger = f"""<TimeTrigger>
|
trigger = f"""<TimeTrigger>
|
||||||
<Repetition>
|
<Repetition>
|
||||||
|
@ -180,7 +223,7 @@ def create_scheduled_task_windows(
|
||||||
<Enabled>true</Enabled>
|
<Enabled>true</Enabled>
|
||||||
</TimeTrigger>"""
|
</TimeTrigger>"""
|
||||||
elif hour is not None and minute is not None:
|
elif hour is not None and minute is not None:
|
||||||
task_args = f'{task_args}-c "{config_file}" --{type} --force --run-as-cli'
|
task_args = f'{task_args}-c "{config_file}" --{type} --force --run-as-cli{object_args}'
|
||||||
start_date = (
|
start_date = (
|
||||||
datetime.datetime.now()
|
datetime.datetime.now()
|
||||||
.replace(microsecond=0, hour=hour, minute=minute, second=0)
|
.replace(microsecond=0, hour=hour, minute=minute, second=0)
|
||||||
|
@ -236,7 +279,7 @@ def create_scheduled_task_windows(
|
||||||
<Actions Context="Author">
|
<Actions Context="Author">
|
||||||
<Exec>
|
<Exec>
|
||||||
<Command>"{runner}"</Command>
|
<Command>"{runner}"</Command>
|
||||||
<Arguments>{task_args}</Arguments>
|
<Arguments>{task_args}{object_args}</Arguments>
|
||||||
<WorkingDirectory>{executable_dir}</WorkingDirectory>
|
<WorkingDirectory>{executable_dir}</WorkingDirectory>
|
||||||
</Exec>
|
</Exec>
|
||||||
</Actions>
|
</Actions>
|
||||||
|
|
Loading…
Add table
Reference in a new issue