This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-cron.py
executable file
·68 lines (56 loc) · 1.61 KB
/
add-cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/env python3
import json
import os
from subprocess import check_output, list2cmdline, run
hook_path = os.path.abspath(os.path.dirname(__file__))
command = [
os.path.join(hook_path, ".venv", "bin", "certbot"),
"renew",
"--post-hook",
os.path.join(hook_path, "hook.py"),
]
configured = {
"type": "userdefined",
# see https://forum.openmediavault.org/index.php?thread/16752-api-cron/
"uuid": "fa4b1c66-ef79-11e5-87a0-0002b3a176b4",
"dayofmonth": "*",
"dayofweek": "*",
"enable": True,
"everyndayofmonth": False,
"everynhour": False,
"everynminute": False,
"execution": "daily",
"hour": "13",
"minute": "24",
"month": "*",
"username": "root",
"command": list2cmdline(command),
"sendemail": False,
"comment": "Automatically Renew Certificate",
}
def rpc(service, method, params):
run(["omv-rpc", service, method, json.dumps(params)], check=True)
def apply_changes(*modules):
rpc("Config", "applyChanges", {"force": False, "modules": modules})
def get_cron():
params = {
"type": ["userdefined"],
"start": 0,
"limit": 25,
"sortfield": "enable",
"sortdir": "ASC",
}
command = ["omv-rpc", "Cron", "getList", json.dumps(params)]
tasks = json.loads(check_output(command))
for task in tasks["data"]:
if task["comment"] == configured["comment"]:
return task
return configured
def main():
task = get_cron()
configured["uuid"] = task["uuid"]
rpc("Cron", "set", configured)
apply_changes("Cron")
apply_changes()
if __name__ == "__main__":
main()