X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/90eb468c7ea5a55294789c8ea7e5b96841100f40..58b22b6e77a87146ef7e6048b69112df3b62e9e6:/lib/utils.py diff --git a/lib/utils.py b/lib/utils.py index 97d6b81..76adfc5 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -203,7 +203,18 @@ def _RunCmdPipe(cmd, env, via_shell, cwd): fcntl.fcntl(fd, fcntl.F_SETFL, status | os.O_NONBLOCK) while fdmap: - for fd, event in poller.poll(): + try: + pollresult = poller.poll() + except EnvironmentError, eerr: + if eerr.errno == errno.EINTR: + continue + raise + except select.error, serr: + if serr[0] == errno.EINTR: + continue + raise + + for fd, event in pollresult: if event & select.POLLIN or event & select.POLLPRI: data = fdmap[fd][1].read() # no data from read signifies EOF (the same as POLLHUP) @@ -274,6 +285,32 @@ def RemoveFile(filename): raise +def RenameFile(old, new, mkdir=False, mkdir_mode=0750): + """Renames a file. + + @type old: string + @param old: Original path + @type new: string + @param new: New path + @type mkdir: bool + @param mkdir: Whether to create target directory if it doesn't exist + @type mkdir_mode: int + @param mkdir_mode: Mode for newly created directories + + """ + try: + return os.rename(old, new) + except OSError, err: + # In at least one use case of this function, the job queue, directory + # creation is very rare. Checking for the directory before renaming is not + # as efficient. + if mkdir and err.errno == errno.ENOENT: + # Create directory and try again + os.makedirs(os.path.dirname(new), mkdir_mode) + return os.rename(old, new) + raise + + def _FingerprintFile(filename): """Compute the fingerprint of a file. @@ -834,7 +871,6 @@ def SetEtcHostsEntry(file_name, ip, hostname, aliases): try: f = open(file_name, 'r') try: - written = False for line in f: fields = line.split() if fields and not fields[0].startswith('#') and ip == fields[0]: @@ -1007,7 +1043,7 @@ def TcpPing(target, port, timeout=10, live_port_needed=False, source=None): """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sucess = False + success = False if source is not None: try: @@ -1459,10 +1495,24 @@ def KillProcess(pid, signal_=signal.SIGTERM, timeout=30, _helper(pid, signal_, waitpid) if timeout <= 0: return + + # Wait up to $timeout seconds end = time.time() + timeout + wait = 0.01 while time.time() < end and IsProcessAlive(pid): - time.sleep(0.1) + try: + (result_pid, _) = os.waitpid(pid, os.WNOHANG) + if result_pid > 0: + break + except OSError: + pass + time.sleep(wait) + # Make wait time longer for next try + if wait < 0.1: + wait *= 1.5 + if IsProcessAlive(pid): + # Kill process if it's still alive _helper(pid, signal.SIGKILL, waitpid)