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); 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));
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()));
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)); }
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() )); } }