def _killRemoteCmd(self, host, executorPath, cmdStr, remoteUser, remoteSudo): pathIndex = cmdStr.find(executorPath) if pathIndex == -1: return False rawCmd = cmdStr[pathIndex:] rawCmd = ' '.join(rawCmd.split()) Log.cout(Log.INFO, 'kill ainst2 process on the remote host %s ...' % host) cmd = 'ssh %s@%s ps -efw | grep \'%s\' | grep -v \'ssh %s@%s\' | grep -v grep'\ % (remoteUser, host, rawCmd, remoteUser, host) out, err, code = process.runRedirected(cmd, self._killTimeout) if code != 0: Log.cout(Log.ERROR, 'get remote pid failed') return False pidList = [] contentList = out.split('\n') for content in contentList: if not content or not content.strip(): continue items = content.split() pidList.append(items[1]) if not pidList: return True pidSet = set(pidList) index = 0 while index < len(pidList): subPidList = self._getSubPidList(remoteUser, host, pidList[index]) for subPid in subPidList: if subPid not in pidSet: pidList.append(subPid) pidSet.add(subPid) index += 1 return self._killRemotePid(pidList, remoteUser, host, remoteSudo)
def remoteExecute(self, host, executorPath, remoteCmd, remoteUser, remoteTimeout=1200, retryTime=0, retryInterval=0, remoteSudo=False): while True: begin = time.time() Log.cout(Log.INFO, remoteCmd) out, err, code = process.runRedirected(remoteCmd, remoteTimeout) end = time.time() Log.cout(Log.INFO, out) if code == 0: Log.coutValue(Log.INFO, 'Process Remote host %s' % host, 'success') return True Log.cout(Log.ERROR, err) if end - begin >= remoteTimeout: Log.coutValue(Log.ERROR, 'Process Remote host %s' % host, 'timeout') self._killRemoteCmd(host, executorPath, remoteCmd, remoteUser, remoteSudo) else: Log.coutValue(Log.ERROR, 'Process Remote host %s' % host, 'failed') retryTime -= 1 if retryTime < 0: break time.sleep(retryInterval) return False
def _generateConfigByTemplate(self, srcPath, destPath, noReplace, settings): out, err, code = process.runRedirected(srcPath) if code != 0: Log.cout(Log.ERROR, 'Generat template config file %s failed: %s' % (destPath, err)) return False if not file_util.writeToFile(destPath, out): Log.cout(Log.ERROR, 'Write config file to %s failed' % destPath) return False return True
def _processScript(self, content): if not content: return True Log.cout(Log.INFO, content) out, err, code = process.runRedirected(content) if out: Log.cout(Log.INFO, out) if err: Log.cout(Log.ERROR, err) return code == 0
def _getSubPidList(self, remoteUser, host, pid): pidList = [] pattern = re.compile('\s+') out, err, code = process.runRedirected('ssh %s@%s ps -efw | grep %s'\ % (remoteUser, host, pid), self._killTimeout) if code != 0: Log.cout(Log.ERROR, 'Get host %s process %s sub process failed: %s'\ % (host, pid, err)) return pidList lines = out.strip().split('\n') for line in lines: line = pattern.split(line.strip()) if str(pid) == line[2]: pidList.append(int(line[1])) return pidList
def _killRemotePid(self, pidList, remoteUser, host, remoteSudo): hasFailed = False for pid in pidList: cmd = 'kill -9 %s' % pid if remoteSudo: cmd = 'sudo ' + cmd cmd = 'ssh %s@%s %s' % (remoteUser, host, cmd) Log.cout(Log.INFO, cmd) out, err, code = process.runRedirected(cmd, self._killTimeout) if code != 0: Log.cout(Log.ERROR, 'kill host %s process %s failed: %s'\ % (host, pid, err)) hasFailed = True if hasFailed: return False return True
else: version = verstring[i + 1:] release = None return (epoch, version, release) def rpm2dir(rpmPath, destDir, timeout=600): currentWorkdir = os.getcwd() if not file_util.isDir(destDir) and not file_util.makeDir(destDir): Log.cout(Log.ERROR, 'Make rpm dir %s failed' % destDir) return False try: os.chdir(destDir) except OSError, e: return False cmd = 'rpm2cpio %s | cpio -ivd --no-absolute-filenames' % rpmPath out, err, code = process.runRedirected(cmd, timeout) if code != 0: Log.cout(Log.ERROR, 'Cpio [%s] result code: %d' % (rpmPath, code)) try: os.chdir(currentWorkdir) except OSError, e: Log.cout(Log.ERROR, 'Chdir to %s failed' % currentWorkdir) return False return code == 0 def splitPkgName3(pkgName): epoch, name, ver, rel, arch = None, None, None, None, None # google-perftools-2.1-1.x86_64 # anet-devel-1.3.3-rc_1.x86_64 # AliWS-1.4.0.0-1.x86_64 # python-2.4.3-27.el5.x86_64
version = verstring[i + 1:] release = None return (epoch, version, release) def rpm2dir(rpmPath, destDir, timeout=600): currentWorkdir = os.getcwd() if not file_util.isDir(destDir) and not file_util.makeDir(destDir): Log.cout(Log.ERROR, 'Make rpm dir %s failed' % destDir) return False try: os.chdir(destDir) except OSError, e: return False cmd = 'rpm2cpio %s | cpio -ivd --no-absolute-filenames' % rpmPath out, err, code = process.runRedirected(cmd, timeout) if code != 0: Log.cout(Log.ERROR, 'Cpio [%s] result code: %d' % (rpmPath, code)) try: os.chdir(currentWorkdir) except OSError, e: Log.cout(Log.ERROR, 'Chdir to %s failed' % currentWorkdir) return False return code == 0 def splitPkgName3(pkgName): epoch, name, ver, rel, arch = None, None, None, None, None # google-perftools-2.1-1.x86_64 # anet-devel-1.3.3-rc_1.x86_64 # AliWS-1.4.0.0-1.x86_64