diff --git a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java index 046e5d343c69..daef76eae45b 100644 --- a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java +++ b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java @@ -16,18 +16,16 @@ // under the License. package com.cloud.hypervisor.kvm.storage; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.StringJoiner; import javax.annotation.Nonnull; +import com.cloud.storage.Storage; +import com.cloud.utils.exception.CloudRuntimeException; import org.apache.cloudstack.storage.datastore.util.LinstorUtil; import org.apache.cloudstack.utils.qemu.QemuImg; import org.apache.cloudstack.utils.qemu.QemuImgException; @@ -35,8 +33,6 @@ import org.apache.log4j.Logger; import org.libvirt.LibvirtException; -import com.cloud.storage.Storage; -import com.cloud.utils.exception.CloudRuntimeException; import com.linbit.linstor.api.ApiClient; import com.linbit.linstor.api.ApiException; import com.linbit.linstor.api.Configuration; @@ -69,28 +65,6 @@ private static String getLinstorRscName(String name) { return LinstorUtil.RSC_PREFIX + name; } - private String getHostname() { - // either there is already some function for that in the agent or a better way. - ProcessBuilder pb = new ProcessBuilder("/usr/bin/hostname"); - try - { - String result; - Process p = pb.start(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - - StringJoiner sj = new StringJoiner(System.getProperty("line.separator")); - reader.lines().iterator().forEachRemaining(sj::add); - result = sj.toString(); - - p.waitFor(); - p.destroy(); - return result.trim(); - } catch (IOException | InterruptedException exc) { - Thread.currentThread().interrupt(); - throw new CloudRuntimeException("Unable to run '/usr/bin/hostname' command."); - } - } - private void logLinstorAnswer(@Nonnull ApiCallRc answer) { if (answer.isError()) { s_logger.error(answer.getMessage()); @@ -121,7 +95,7 @@ private void handleLinstorApiAnswers(ApiCallRcList answers, String excMessage) { } public LinstorStorageAdaptor() { - localNodeName = getHostname(); + localNodeName = LinstorStoragePool.getHostname(); } @Override diff --git a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStoragePool.java b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStoragePool.java index d03095218742..45cf7e3c6f91 100644 --- a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStoragePool.java +++ b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStoragePool.java @@ -25,8 +25,13 @@ import com.cloud.agent.api.to.HostTO; import com.cloud.hypervisor.kvm.resource.KVMHABase.HAStoragePool; import com.cloud.storage.Storage; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.script.OutputInterpreter; +import com.cloud.utils.script.Script; +import org.apache.log4j.Logger; public class LinstorStoragePool implements KVMStoragePool { + private static final Logger s_logger = Logger.getLogger(LinstorStoragePool.class); private final String _uuid; private final String _sourceHost; private final int _sourcePort; @@ -219,6 +224,17 @@ public String getStorageNodeId() { return null; } + static String getHostname() { + OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); + Script sc = new Script("/usr/bin/hostname", Duration.millis(10000L), s_logger); + String res = sc.execute(parser); + if (res != null) { + throw new CloudRuntimeException(String.format("Unable to run '/usr/bin/hostname' command: %s", res)); + } + String response = parser.getLines(); + return response.trim(); + } + @Override public Boolean checkingHeartBeat(HAStoragePool pool, HostTO host) { return null;