public static ShellResult runAndReturn(String cmdsr) { return runAndReturn(cmdsr, null); }
protected int setConsoleProxyOverridenIp(String newIp) { if (CoreGlobalProperty.UNIT_TEST_ON) { return 0; } ShellResult rst = ShellUtils.runAndReturn( "/usr/bin/zstack-ctl configure consoleProxyOverriddenIp=" + newIp ); return rst.getRetCode(); }
public static void insertRuleToFilterTable(String rule) { ShellResult ret = ShellUtils.runAndReturn(String.format("/sbin/iptables-save | grep -- '%s' > /dev/null", rule)); if (ret.getRetCode() == 0) { return; } ret = ShellUtils.runAndReturn(String.format("/sbin/iptables %s", rule.replace("-A", "-I"))); ret.raiseExceptionIfFail(); } }
public static void appendRuleToFilterTable(String rule) { ShellResult ret = ShellUtils.runAndReturn(String.format("/sbin/iptables-save | grep -- '%s' > /dev/null", rule)); if (ret.getRetCode() == 0) { return; } ret = ShellUtils.runAndReturn(String.format("/sbin/iptables %s", rule)); ret.raiseExceptionIfFail(); }
public static boolean isIpRoutedByDefaultGateway(String ip) { ShellResult res = ShellUtils.runAndReturn(String.format("ip route get %s | grep -q \"via $(ip route | awk '/default/ {print $3}')\"", ip)); return res.isReturnCode(0); }
ShellResult res = ShellUtils.runAndReturn(cmd);
public static boolean killProcess(int pid, Integer timeout) { timeout = timeout == null ? 30 : timeout; if (!TimeUtils.loopExecuteUntilTimeoutIgnoreExceptionAndReturn(timeout, 1, TimeUnit.SECONDS, () -> { ShellUtils.runAndReturn(String.format("kill %s", pid)); return !new ProcessFinder().processExists(pid); })) { logger.warn(String.format("cannot kill the process[PID:%s] after %s seconds, kill -9 it", pid, timeout)); ShellUtils.runAndReturn(String.format("kill -9 %s", pid)); } if (!TimeUtils.loopExecuteUntilTimeoutIgnoreExceptionAndReturn(5, 1, TimeUnit.SECONDS, () -> !new ProcessFinder().processExists(pid))) { logger.warn(String.format("FAILED TO KILL -9 THE PROCESS[PID:%s], THE KERNEL MUST HAVE SOMETHING RUN", pid)); return false; } else { return true; } }
public int run(String cmd, Object...args) { if (args != null) { cmd = String.format(cmd, args); } lastCommand = cmd; ShellResult res = ShellUtils.runAndReturn(cmd, path, useSudo); lastReturnCode = res.getRetCode(); lastStdout = res.getStdout(); lastStderr = res.getStderr(); if (SET_E) { errorOnFailure(); } return lastReturnCode; } }
protected int run(String cmd, boolean sudo, Object...args) { if (args != null) { cmd = String.format(cmd, args); } lastCommand = cmd; ShellResult res = ShellUtils.runAndReturn(cmd, sudo); lastReturnCode = res.getRetCode(); lastStdout = res.getStdout(); lastStderr = res.getStderr(); if (SET_E) { errorOnFailure(); } return lastReturnCode; }
@Override public boolean needDeploy() { String srcScript = script.format(srcFolder); ShellResult srcRes = ShellUtils.runAndReturn(srcScript, false); if (!srcRes.isReturnCode(0)) { throw new OperationFailureException(operr("cannot check md5sum of files in the folder[%s].\nstdout:%s\nstderr:%s", srcFolder,
private void checkIfManagementNetworkReachable(String managementNetworkUuid) { SimpleQuery<IpRangeVO> q = dbf.createQuery(IpRangeVO.class); q.add(IpRangeVO_.l3NetworkUuid, Op.EQ, managementNetworkUuid); List<IpRangeVO> iprs = q.list(); if (iprs.isEmpty()) { throw new ApiMessageInterceptionException(operr("the management network[uuid:%s] doesn't have any IP range", managementNetworkUuid)); } String startIp = iprs.get(0).getStartIp(); if (!NetworkUtils.isIpRoutedByDefaultGateway(startIp)) { // the mgmt server is in the same subnet of the mgmt network return; } String gateway = iprs.get(0).getGateway(); for (int i=0; i<3; i++) { ShellResult ret = ShellUtils.runAndReturn(String.format("ping -c 1 -W 2 %s", gateway)); if (ret.isReturnCode(0)) { return; } } throw new ApiMessageInterceptionException(argerr("the management network[uuid:%s, gateway:%s] is not reachable", managementNetworkUuid, gateway)); }
ShellResult ret = ShellUtils.runAndReturn(ssh); SshResult sret = new SshResult(); sret.setCommandToExecute(cmd);
ShellResult sret = ShellUtils.runAndReturn(String.format("md5sum %s", sourceFilePath)); sret.raiseExceptionIfFail(); String srcMd5 = sret.getStdout().split(" ")[0];
ShellResult ret = ShellUtils.runAndReturn(ssh); SshResult sret = new SshResult(); sret.setCommandToExecute(script);
private void checkManagementIp(VmInstanceSpec spec, boolean isNewCreated) { if (CoreGlobalProperty.UNIT_TEST_ON) { return; } if (!ApplianceVmConstant.APPLIANCE_VM_TYPE.equals(spec.getVmInventory().getType())) { return; } VmNicInventory mgmtNic; if (isNewCreated) { final ApplianceVmSpec aspec = spec.getExtensionData(ApplianceVmConstant.Params.applianceVmSpec.toString(), ApplianceVmSpec.class); mgmtNic = CollectionUtils.find(spec.getDestNics(), new Function<VmNicInventory, VmNicInventory>() { @Override public VmNicInventory call(VmNicInventory arg) { return arg.getL3NetworkUuid().equals(aspec.getManagementNic().getL3NetworkUuid()) ? arg : null; } }); } else { ApplianceVmInventory apvm = ApplianceVmInventory.valueOf(dbf.findByUuid(spec.getVmInventory().getUuid(), ApplianceVmVO.class)); mgmtNic = apvm.getManagementNic(); } DebugUtils.Assert(mgmtNic!=null, String.format("cannot find management nic of appliance vm[uuid:%s, newCreated: %s]", spec.getVmInventory().getUuid(), isNewCreated)); ShellResult ret = ShellUtils.runAndReturn(String.format("ping -c 1 -W 1 %s", mgmtNic.getIp())); if (ret.isReturnCode(0)) { throw new OperationFailureException(err(ApplianceVmErrors.MANAGEMENT_IP_OCCUPIED, "the management nic IP[%s] has been occupied by another device in the data center, we can ping it", mgmtNic.getIp() )); } }