Skip to content

Commit 6de5e90

Browse files
committed
linstor: move getHostname() to kvm/Pool and reimplement
1 parent 04fe834 commit 6de5e90

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,23 @@
1616
// under the License.
1717
package com.cloud.hypervisor.kvm.storage;
1818

19-
import java.io.BufferedReader;
20-
import java.io.IOException;
21-
import java.io.InputStreamReader;
2219
import java.util.Collections;
2320
import java.util.HashMap;
2421
import java.util.List;
2522
import java.util.Map;
2623
import java.util.Optional;
27-
import java.util.StringJoiner;
2824

2925
import javax.annotation.Nonnull;
3026

27+
import com.cloud.storage.Storage;
28+
import com.cloud.utils.exception.CloudRuntimeException;
3129
import org.apache.cloudstack.storage.datastore.util.LinstorUtil;
3230
import org.apache.cloudstack.utils.qemu.QemuImg;
3331
import org.apache.cloudstack.utils.qemu.QemuImgException;
3432
import org.apache.cloudstack.utils.qemu.QemuImgFile;
3533
import org.apache.log4j.Logger;
3634
import org.libvirt.LibvirtException;
3735

38-
import com.cloud.storage.Storage;
39-
import com.cloud.utils.exception.CloudRuntimeException;
4036
import com.linbit.linstor.api.ApiClient;
4137
import com.linbit.linstor.api.ApiException;
4238
import com.linbit.linstor.api.Configuration;
@@ -73,28 +69,6 @@ private static String getLinstorRscName(String name) {
7369
return LinstorUtil.RSC_PREFIX + name;
7470
}
7571

76-
private String getHostname() {
77-
// either there is already some function for that in the agent or a better way.
78-
ProcessBuilder pb = new ProcessBuilder("/usr/bin/hostname");
79-
try
80-
{
81-
String result;
82-
Process p = pb.start();
83-
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
84-
85-
StringJoiner sj = new StringJoiner(System.getProperty("line.separator"));
86-
reader.lines().iterator().forEachRemaining(sj::add);
87-
result = sj.toString();
88-
89-
p.waitFor();
90-
p.destroy();
91-
return result.trim();
92-
} catch (IOException | InterruptedException exc) {
93-
Thread.currentThread().interrupt();
94-
throw new CloudRuntimeException("Unable to run '/usr/bin/hostname' command.");
95-
}
96-
}
97-
9872
private void logLinstorAnswer(@Nonnull ApiCallRc answer) {
9973
if (answer.isError()) {
10074
s_logger.error(answer.getMessage());
@@ -125,7 +99,7 @@ private void handleLinstorApiAnswers(ApiCallRcList answers, String excMessage) {
12599
}
126100

127101
public LinstorStorageAdaptor() {
128-
localNodeName = getHostname();
102+
localNodeName = LinstorStoragePool.getHostname();
129103
}
130104

131105
@Override

plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStoragePool.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
import com.cloud.agent.api.to.HostTO;
2626
import com.cloud.hypervisor.kvm.resource.KVMHABase.HAStoragePool;
2727
import com.cloud.storage.Storage;
28+
import com.cloud.utils.exception.CloudRuntimeException;
29+
import com.cloud.utils.script.OutputInterpreter;
30+
import com.cloud.utils.script.Script;
31+
import org.apache.log4j.Logger;
2832

2933
public class LinstorStoragePool implements KVMStoragePool {
34+
private static final Logger s_logger = Logger.getLogger(LinstorStoragePool.class);
3035
private final String _uuid;
3136
private final String _sourceHost;
3237
private final int _sourcePort;
@@ -219,6 +224,17 @@ public String getStorageNodeId() {
219224
return null;
220225
}
221226

227+
static String getHostname() {
228+
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
229+
Script sc = new Script("hostname", Duration.millis(10000L), s_logger);
230+
String res = sc.execute(parser);
231+
if (res != null) {
232+
throw new CloudRuntimeException(String.format("Unable to run 'hostname' command: %s", res));
233+
}
234+
String response = parser.getLines();
235+
return response.trim();
236+
}
237+
222238
@Override
223239
public Boolean checkingHeartBeat(HAStoragePool pool, HostTO host) {
224240
return null;

0 commit comments

Comments
 (0)