Package ganeti :: Package jqueue :: Module post_hooks_exec
[hide private]
[frames] | no frames]

Source Code for Module ganeti.jqueue.post_hooks_exec

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2015 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 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   
54 -def _GetMasterInfo():
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
70 -def main():
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 # We should run post hooks only if opcode execution has been started. 92 # Note that currently the opcodes inside a job execute sequentially. 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 # Get static snapshot of the config and release it in order to prevent 100 # further synchronizations. 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: # pylint: disable=W0703 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