-
Notifications
You must be signed in to change notification settings - Fork 16
/
hooks.py
91 lines (67 loc) · 2.03 KB
/
hooks.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
from os.path import *
import executil
from registry import registry
from conf import Conf
class HookError(Exception):
pass
def _is_signed(fpath, keyring):
fpath_sig = fpath + ".sig"
if not exists(fpath_sig):
return False
try:
executil.getoutput("gpg --keyring=%s --verify" % keyring, fpath_sig)
return True
except:
return False
def _run_hooks(path, args, keyring=None):
if not isdir(path):
return
for fname in os.listdir(path):
fpath = join(path, fname)
if not os.access(fpath, os.X_OK):
continue
if fpath.endswith(".sig"):
continue
if keyring and not _is_signed(fpath, keyring):
continue
try:
executil.system(fpath, *args)
except executil.ExecError, e:
raise HookError("`%s %s` non-zero exitcode (%d)" % \
(fpath, " ".join(args), e.exitcode))
class Hooks:
"""
Backup hook invocation:
pre hook
create extras
inspect hook
run duplicity to create/update backup archives
post hook
Restore hook invocation:
pre hook
run duplicity to get extras + overlay
inspect hook
apply restore to system
post hook
"""
BASENAME = "hooks.d"
LOCAL_HOOKS = os.environ.get("TKLBAM_HOOKS", join(Conf.DEFAULT_PATH, BASENAME))
PROFILE_KEYRING = "/etc/apt/trusted.gpg.d/turnkey.gpg"
def __init__(self, name):
self.name = name
def _run(self, state):
_run_hooks(self.LOCAL_HOOKS, (self.name, state))
if registry.profile:
_run_hooks(join(registry.profile, self.BASENAME), (self.name, state), keyring=self.PROFILE_KEYRING)
def pre(self):
self._run("pre")
def post(self):
self._run("post")
def inspect(self, extras_path):
orig_cwd = os.getcwd()
os.chdir(extras_path)
self._run("inspect")
os.chdir(orig_cwd)
backup = Hooks("backup")
restore = Hooks("restore")