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); }
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; } }
private static String doRun(String cmdstr, String baseDir, boolean withRoot, boolean isVerbose) { ShellRunner runner = new ShellRunner(); runner.command = cmdstr; runner.baseDir = baseDir; runner.verbose = isVerbose; runner.withSudo = withRoot; ShellResult ret = runner.run(); ret.raiseExceptionIfFail(); StringBuilder sb = new StringBuilder(String.format("exec shell: %s\n", cmdstr)); // if isVerbose is set, there is nothing to read as stream has been closed if (!isVerbose) { sb.append(String.format("stdout: %s\n", ret.getStdout())); sb.append(String.format("stderr: %s\n", ret.getStderr())); } return sb.toString(); }
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(); } }
ShellResult res = ShellUtils.runAndReturn(cmd); if ("".equals(res.getStdout()) || res.isReturnCode(2)) { sb = new StringBuilder(String.format("\nfailed to apply salt state[minion id:%s, state name:%s]", minionId, stateName)); sb.append(String.format("\ncommand: %s", cmd)); sb.append(String.format("\nno json output from the command, it's probably caused by the minion hasn't connected to master, will retry", res.getStdout())); logger.debug(sb.toString()); return false; res.raiseExceptionIfFail(); HashMap output = JSONObjectUtil.toObject(res.getStdout(), HashMap.class); for (Object val : output.values()) { if (val instanceof List) { sb = new StringBuilder(String.format("\nfailed to apply salt state[minion id:%s, state name:%s]", minionId, stateName)); sb.append(String.format("\ncommand: %s", cmd)); sb.append(String.format("\n%s", res.getStdout())); throw new SaltException(sb.toString()); sb = new StringBuilder(String.format("\nfailed to apply salt state[minion id:%s, state name:%s]", minionId, stateName)); sb.append(String.format("\ncommand: %s", cmd)); sb.append(String.format("\n%s", res.getStdout())); throw new SaltException(sb.toString());
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, srcRes.getStdout(), srcRes.getStderr())); Map<String, String> dstMd5sum = new HashMap<String, String>(); for (String s : srcRes.getStdout().split("\n")) { if (StringUtils.isBlank(s)) { continue;
stdoutConsumer.join(TimeUnit.SECONDS.toMillis(30)); ShellResult ret = new ShellResult(); ret.setCommand(command); ret.setRetCode(process.exitValue()); if (stderrLog == LOG_TO_STRING) { ret.setStderr(stderr.toString()); } else if (stderrLog == LOG_TO_FILE) { stderr.close(); ret.setStdout(stdout.toString()); } else if (stdoutLog == LOG_TO_FILE) { stdout.close();
sret.raiseExceptionIfFail(); String srcMd5 = sret.getStdout().split(" ")[0]; if (!destMd5.equals(srcMd5)) { logger.debug(String.format("file MD5 changed, src[%s, md5:%s] dest[%s, md5, %s]", sourceFilePath,
public void raiseExceptionIfFail() { raiseExceptionIfFail(0); }
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 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(); }
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; }
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)); }
SshResult sret = new SshResult(); sret.setCommandToExecute(cmd); sret.setReturnCode(ret.getRetCode()); sret.setStderr(ret.getStderr()); sret.setStdout(ret.getStdout()); if (sret.getReturnCode() == 255 && privateKeyFile != null) { sret.setSshFailure(true);
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() )); } }
SshResult sret = new SshResult(); sret.setCommandToExecute(script); sret.setReturnCode(ret.getRetCode()); sret.setStderr(ret.getStderr()); sret.setStdout(ret.getStdout()); if (sret.getReturnCode() == 255 && privateKeyFile != null) { sret.setSshFailure(true);