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 """Module implementing the execution of opcode post hooks in a separate process
32
33 This process receives the job_id from the master process and runs global post
34 hooks for the last opcode whose execution started before the job process
35 disappeared.
36
37 """
38
39 import contextlib
40 import logging
41 import os
42 import sys
43
44 from ganeti import constants
45 from ganeti import hooksmaster
46 from ganeti import pathutils
47 from ganeti import utils
48 from ganeti.jqueue import JobQueue
49 from ganeti.rpc import transport
50 from ganeti.server import masterd
51 from ganeti.utils import livelock
52
53
55 """Retrieve the job id from the master process
56
57 This also closes standard input/output
58
59 @rtype: int
60
61 """
62 logging.debug("Reading job id from the master process")
63 logging.debug("Opening transport over stdin/out")
64 with contextlib.closing(transport.FdTransport((0, 1))) as trans:
65 job_id = int(trans.Call(""))
66 logging.debug("Got job id %d", job_id)
67 return job_id
68
69
71
72 debug = int(os.environ["GNT_DEBUG"])
73
74 logname = pathutils.GetLogFilename("jobs")
75 utils.SetupLogging(logname, "job-post-hooks-startup", debug=debug)
76 job_id = _GetMasterInfo()
77 utils.SetupLogging(logname, "job-%s-post-hooks" % (job_id,), debug=debug)
78
79 try:
80 job = JobQueue.SafeLoadJobFromDisk(None, job_id, try_archived=False,
81 writable=False)
82 assert job.id == job_id, "The job id received %d differs " % job_id + \
83 "from the serialized one %d" % job.id
84
85 target_op = None
86 for op in job.ops:
87 if op.start_timestamp is None:
88 break
89 target_op = op
90
91
92
93 if target_op is None:
94 sys.exit(0)
95
96 livelock_name = livelock.LiveLockName("post-hooks-executor-%d" % job_id)
97 context = masterd.GanetiContext(livelock_name)
98 cfg_tmp = context.GetConfig(job_id)
99
100
101 cfg = cfg_tmp.GetDetachedConfig()
102 cfg_tmp.OutDate()
103
104 hooksmaster.ExecGlobalPostHooks(target_op.input.OP_ID,
105 cfg.GetMasterNodeName(),
106 context.GetRpc(cfg).call_hooks_runner,
107 logging.warning, cfg.GetClusterName(),
108 cfg.GetMasterNode(), job_id,
109 constants.POST_HOOKS_STATUS_DISAPPEARED)
110 except Exception:
111 logging.exception("Exception when trying to run post hooks of job %d",
112 job_id)
113 finally:
114 logging.debug("Post hooks exec for disappeared job %d finalized", job_id)
115 logging.debug("Removing livelock file %s", livelock_name.GetPath())
116 os.remove(livelock_name.GetPath())
117
118 sys.exit(0)
119
120 if __name__ == '__main__':
121 main()
122