Skip to content

Commit e99e890

Browse files
committed
linstor: move getHostname() to kvm/Pool and reimplement
1 parent 35aaddf commit e99e890

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;
@@ -69,28 +65,6 @@ private static String getLinstorRscName(String name) {
6965
return LinstorUtil.RSC_PREFIX + name;
7066
}
7167

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

12397
public LinstorStorageAdaptor() {
124-
localNodeName = getHostname();
98+
localNodeName = LinstorStoragePool.getHostname();
12599
}
126100

127101
@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)