Dhandapani Gopal has uploaded a new change for review.

Change subject: [WIP] engine: Get Gluster Servers query  - Execute the gluster 
peer status command via ssh using the given servername and password  - Parse 
the xml output of the command and get the servernames which state is 3 (ie. 
Peer in Cluster)  - Fetch the finger pr
......................................................................

[WIP] engine: Get Gluster Servers query
 - Execute the gluster peer status command via ssh using the given servername 
and password
 - Parse the xml output of the command and get the servernames which state is 3 
(ie. Peer in Cluster)
 - Fetch the finger print of the servers and return

 - TODO: Will send another patch-set with test case.

Change-Id: Ic69a9a48bf227c8fa805c8aa9c4f08fa36ea9425
Signed-off-by: Dhandapani <dgo...@redhat.com>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/gluster/GlusterServersQueryParameters.java
3 files changed, 211 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/84/7584/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
new file mode 100644
index 0000000..5445459
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
@@ -0,0 +1,169 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.commons.lang.exception.ExceptionUtils;
+import 
org.ovirt.engine.core.common.queries.gluster.GlusterServersQueryParameters;
+import org.ovirt.engine.core.utils.hostinstall.IVdsInstallerCallback;
+import org.ovirt.engine.core.utils.hostinstall.VdsInstallerSSH;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * Query to fetch list of gluster hosts via ssh using the given serverName and 
password.
+ *
+ */
+public class GetGlusterServersQuery<P extends GlusterServersQueryParameters> 
extends GlusterQueriesCommandBase<P> {
+
+    private static final String SSH_COMMAND = "gluster peer status --xml";
+    private static final String PEER = "peer";
+    private static final String HOST_NAME = "hostName";
+    private static final String STATE = "state";
+
+    public VdsInstallerSSH wrapper;
+    private SimpleCallback callback;
+    List<String> hostNamesOrIp = new ArrayList<String>();
+
+    public GetGlusterServersQuery(P parameters) {
+        super(parameters);
+    }
+
+    @Override
+    protected void executeQueryCommand() {
+        wrapper = getVdsInstallerSSHInstance();
+        boolean executionStatus = true;
+//                wrapper.connect(getParameters().getServerName(), 
getParameters().getRootPassword());
+
+        if (executionStatus) {
+
+            if (runSSHCommand()) {
+                String glusterServers = callback.glusterServersXml;
+                DocumentBuilderFactory docBuilderFactory = 
DocumentBuilderFactory.newInstance();
+                DocumentBuilder docBuilder;
+                try {
+                    docBuilder = docBuilderFactory.newDocumentBuilder();
+                    Document doc = docBuilder.parse(glusterServers);
+                    // normalize text representation
+                    doc.getDocumentElement().normalize();
+                    NodeList listOfPeers = doc.getElementsByTagName(PEER);
+
+                    parseHostName(listOfPeers);
+
+                } catch (ParserConfigurationException e1) {
+                    log.errorFormat("There is problem in parsing xml." + 
e1.getMessage());
+                } catch (SAXException e2) {
+                    log.errorFormat("Error in parsing xml." + e2.getMessage());
+                } catch (IOException e3) {
+                    log.errorFormat("There is problem in reading the file." + 
e3.getMessage());
+                }
+                // Add current server also in the list
+                hostNamesOrIp.add(getParameters().getServerName());
+
+                
getQueryReturnValue().setReturnValue(getServerFingerPrintMap());
+            } else {
+                log.error("Could not get the peer list form the host: " + 
getParameters().getServerName());
+            }
+        }
+        else {
+            log.error("Could not connect to host: " + 
getParameters().getServerName());
+        }
+    }
+
+    private boolean runSSHCommand() {
+        callback = new SimpleCallback();
+        wrapper.setCallback(callback);
+        boolean peerStatus = wrapper.executeCommand(SSH_COMMAND);
+        return peerStatus;
+    }
+
+    public Map<String, String> getServerFingerPrintMap() {
+        Map<String, String> serverFingerprint = new HashMap<String, String>();
+        // Fetch fingerprint of every host and add it to map
+        for (String hostName : hostNamesOrIp) {
+            serverFingerprint.put(hostName, getFingerprint(hostName));
+        }
+        return serverFingerprint;
+    }
+
+    private String getFingerprint(String hostName) {
+        String fingerPrint = "";
+        try {
+            fingerPrint = wrapper.getServerKeyFingerprint(hostName);
+        } catch (Throwable e) {
+            log.errorFormat("Could not fetch fingerprint of host {0} with 
message: {1}",
+                    hostName, ExceptionUtils.getMessage(e));
+        } finally {
+            wrapper.shutdown();
+        }
+        return fingerPrint;
+    }
+
+    private void parseHostName(NodeList listOfPeers) {
+        for (int i=0; i < listOfPeers.getLength(); i++) {
+            Node firstPeer = listOfPeers.item(i);
+            if(firstPeer.getNodeType() == Node.ELEMENT_NODE){
+                Element firstHostElement = (Element)firstPeer;
+                int state = getIntValue(firstHostElement, STATE);
+                // Add the server only if the state is 3
+                if (state == 3) {
+                    hostNamesOrIp.add(getTextValue(firstHostElement, 
HOST_NAME));
+                }
+            }
+        }
+    }
+
+    private int getIntValue(Element element, String tagName) {
+        return Integer.parseInt(getTextValue(element,tagName));
+    }
+
+    private String getTextValue(Element element, String tagName) {
+        NodeList hostName = element.getElementsByTagName(tagName);
+        Element hostNameElement = (Element) hostName.item(0);
+
+        NodeList hostNameNode = hostNameElement.getChildNodes();
+        return hostNameNode.item(0).getNodeValue().trim();
+    }
+
+    public VdsInstallerSSH getVdsInstallerSSHInstance() {
+        return new VdsInstallerSSH();
+    }
+
+    private class SimpleCallback implements IVdsInstallerCallback {
+
+        public String glusterServersXml;
+
+        @Override
+        public void addError(String error) {
+        }
+
+        @Override
+        public void addMessage(String message) {
+            if (!message.toString().equals("")) {
+                glusterServersXml = message.toString();
+            }
+        }
+
+        @Override
+        public void connected() {
+        }
+
+        @Override
+        public void endTransfer() {
+        }
+
+        @Override
+        public void failed(String error) {
+        }
+    }
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
index 767e7bf..cbac40a 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
@@ -275,6 +275,7 @@
     GetGlusterVolumeOptionsInfo,
     GetGlusterVolumeBricks,
     GetGlusterBrickById,
+    GetGlusterServers,
 
     // Default type instead of having to null check
     Unknown(VdcQueryAuthType.User);
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/gluster/GlusterServersQueryParameters.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/gluster/GlusterServersQueryParameters.java
new file mode 100644
index 0000000..9da0272
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/gluster/GlusterServersQueryParameters.java
@@ -0,0 +1,41 @@
+package org.ovirt.engine.core.common.queries.gluster;
+
+import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
+
+/**
+ * Parameter class for Gluster Hosts queries
+ */
+public class GlusterServersQueryParameters extends VdcQueryParametersBase {
+
+    private static final long serialVersionUID = -3541250057200360191L;
+
+    private String serverName;
+
+    private String rootPassword;
+
+    public GlusterServersQueryParameters(String serverName, String 
rootPassword) {
+        setServerName(serverName);
+        setRootPassword(rootPassword);
+    }
+
+    public GlusterServersQueryParameters(String serverName) {
+        setServerName(serverName);
+    }
+
+    public String getServerName() {
+        return serverName;
+    }
+
+    public void setServerName(String serverName) {
+        this.serverName = serverName;
+    }
+
+    public String getRootPassword() {
+        return rootPassword;
+    }
+
+    public void setRootPassword(String rootPassword) {
+        this.rootPassword = rootPassword;
+    }
+
+}


--
To view, visit http://gerrit.ovirt.org/7584
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic69a9a48bf227c8fa805c8aa9c4f08fa36ea9425
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Dhandapani Gopal <dgo...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to