Skip to content

Commit

Permalink
-Fixing JENKINS-14617
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybug committed Nov 12, 2012
1 parent fb32ab2 commit a9c654c
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/main/java/hudson/plugins/libvirt/Hypervisor.java
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010, Byte-Code srl <http://www.byte-code.com>
* Copyright (C) 2012 Philipp Bartsch <tastybug@tastybug.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -15,7 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Date: Mar 04, 2010
* Author: Marco Mornati<mmornati@byte-code.com>
* @author Marco Mornati<mmornati@byte-code.com>
* @author Philipp Bartsch <tastybug@tastybug.com>
*/
package hudson.plugins.libvirt;

Expand Down Expand Up @@ -60,7 +62,6 @@ public class Hypervisor extends Cloud {
private final String username;
private transient Map<String, Domain> domains = null;
private transient List<VirtualMachine> virtualMachineList = null;
private transient Connect hypervisorConnection = null;

@DataBoundConstructor
public Hypervisor(String hypervisorType, String hypervisorHost, int hypervisorSshPort, String hypervisorSystemUrl, String username) {
Expand All @@ -81,18 +82,17 @@ private Connect makeConnection() {
String hypervisorUri = constructHypervisorURI();
LOGGER.log(Level.INFO, "Trying to establish a connection to hypervisor URI: {0} as {1}/******",
new Object[]{hypervisorUri, username});
if (hypervisorConnection == null) {
try {
hypervisorConnection = new Connect(hypervisorUri, false);
LOGGER.log(Level.INFO, "Established connection to hypervisor URI: {0} as {1}/******",
new Object[]{hypervisorUri, username});
} catch (LibvirtException e) {
LogRecord rec = new LogRecord(Level.WARNING,
"Failed to establish connection to hypervisor URI: {0} as {1}/******");
rec.setThrown(e);
rec.setParameters(new Object[]{hypervisorUri, username});
LOGGER.log(rec);
}
Connect hypervisorConnection = null;
try {
hypervisorConnection = new Connect(hypervisorUri, false);
LOGGER.log(Level.INFO, "Established connection to hypervisor URI: {0} as {1}/******",
new Object[]{hypervisorUri, username});
} catch (LibvirtException e) {
LogRecord rec = new LogRecord(Level.WARNING,
"Failed to establish connection to hypervisor URI: {0} as {1}/******");
rec.setThrown(e);
rec.setParameters(new Object[]{hypervisorUri, username});
LOGGER.log(rec);
}
return hypervisorConnection;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public String getHypervisorDescription() {

public synchronized Map<String, Domain> getDomains() throws LibvirtException {
Map<String, Domain> domains = new HashMap<String, Domain>();
hypervisorConnection = makeConnection();
Connect hypervisorConnection = makeConnection();
LogRecord info = new LogRecord(Level.INFO, "Getting hypervisor domains");
LOGGER.log(info);
if (hypervisorConnection != null) {
Expand Down Expand Up @@ -168,7 +168,10 @@ public synchronized Map<String, Domain> getDomains() throws LibvirtException {
rec.setThrown(e);
LOGGER.log(rec);
}
}
}
LogRecord rec = new LogRecord(Level.INFO, "Closing hypervisor connection");
LOGGER.log(rec);
hypervisorConnection.close();
} else {
LogRecord rec = new LogRecord(Level.SEVERE, "Cannot connect to datacenter {0} as {1}/******");
rec.setParameters(new Object[]{hypervisorHost, username});
Expand Down Expand Up @@ -209,9 +212,15 @@ public DescriptorImpl getDescriptor() {
}

public String constructHypervisorURI() {
return hypervisorType.toLowerCase() + "+ssh://" + username + "@" + hypervisorHost + ":" + hypervisorSshPort + "/" + hypervisorSystemUrl + "?no_tty=1";
return constructHypervisorURI(hypervisorType, "ssh://", username, hypervisorHost, hypervisorSshPort, hypervisorSystemUrl);
}

private static String constructHypervisorURI (String hypervisorType, String protocol, String userName, String hypervisorHost, int hypervisorPort, String hypervisorSysUrl) {
// Fixing JENKINS-14617
final String separator = (hypervisorSysUrl.contains("?")) ? "&" : "?";
return hypervisorType.toLowerCase() + "+" + protocol + userName + "@" + hypervisorHost + ":" + hypervisorPort + "/" + hypervisorSysUrl + separator + "no_tty=1";
}

@Extension
public static final class DescriptorImpl extends Descriptor<Cloud> {

Expand Down Expand Up @@ -251,7 +260,7 @@ public FormValidation doTestConnection(
return FormValidation.error("Username is not specified");
}

String hypervisorUri = hypervisorType.toLowerCase() + "+ssh://" + username + "@" + hypervisorHost + ":" + hypervisorSshPort + "/" + hypervisorSystemUrl + "?no_tty=1";
String hypervisorUri = constructHypervisorURI (hypervisorType, "+ssh://", username, hypervisorHost, Integer.parseInt(hypervisorSshPort), hypervisorSystemUrl);

LogRecord rec = new LogRecord(Level.INFO,
"Testing connection to hypervisor: {0}");
Expand All @@ -274,6 +283,13 @@ public FormValidation doTestConnection(
rec.setParameters(new Object[]{hypervisorHost, username});
LOGGER.log(rec);
return FormValidation.error(e.getMessage());
} catch (Exception e) {
LogRecord rec = new LogRecord(Level.WARNING,
"Failed to connect to hypervisor. Check libvirt installation on hudson machine!");
rec.setThrown(e);
rec.setParameters(new Object[]{hypervisorHost, username});
LOGGER.log(rec);
return FormValidation.error(e.getMessage());
}
}

Expand Down

0 comments on commit a9c654c

Please sign in to comment.