Skip to content

Commit

Permalink
linstor: move getHostname() to kvm/Pool and reimplement
Browse files Browse the repository at this point in the history
  • Loading branch information
rp- committed Feb 7, 2024
1 parent 04fe834 commit f72cd73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,23 @@
// 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;
import org.apache.cloudstack.utils.qemu.QemuImgFile;
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;
Expand Down Expand Up @@ -73,28 +69,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());
Expand Down Expand Up @@ -125,7 +99,7 @@ private void handleLinstorApiAnswers(ApiCallRcList answers, String excMessage) {
}

public LinstorStorageAdaptor() {
localNodeName = getHostname();
localNodeName = LinstorStoragePool.getHostname();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -219,6 +224,17 @@ public String getStorageNodeId() {
return null;
}

static String getHostname() {
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
Script sc = new Script("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;
Expand Down

0 comments on commit f72cd73

Please sign in to comment.