Skip to content

Commit

Permalink
-Fixed JENKINS-14468
Browse files Browse the repository at this point in the history
-Fixed JENKINS-9471 (and duplicates JENKINS-8240, JENKINS-7179)
-Some comments improved
  • Loading branch information
tastybug committed Nov 15, 2012
1 parent 08ea24f commit 550e328
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 91 deletions.
30 changes: 15 additions & 15 deletions src/main/java/hudson/plugins/libvirt/Hypervisor.java
Expand Up @@ -79,7 +79,7 @@ public Hypervisor(String hypervisorType, String hypervisorHost, int hypervisorSs
}

private Connect makeConnection() {
String hypervisorUri = constructHypervisorURI();
String hypervisorUri = getHypervisorURI();
LOGGER.log(Level.INFO, "Trying to establish a connection to hypervisor URI: {0} as {1}/******",
new Object[]{hypervisorUri, username});
Connect hypervisorConnection = null;
Expand Down Expand Up @@ -132,15 +132,15 @@ public String getHypervisorSystemUrl() {
public String getUsername() {
return username;
}

public String getHypervisorDescription() {
return getHypervisorType() + " - " + getHypervisorHost();
}

public synchronized Map<String, Domain> getDomains() throws LibvirtException {
Map<String, Domain> domains = new HashMap<String, Domain>();
Connect hypervisorConnection = makeConnection();
LogRecord info = new LogRecord(Level.INFO, "Getting hypervisor domains");
LogRecord info = new LogRecord(Level.INFO, "Getting hypervisor domains.");
LOGGER.log(info);
if (hypervisorConnection != null) {
for (String c : hypervisorConnection.listDefinedDomains()) {
Expand All @@ -150,7 +150,7 @@ public synchronized Map<String, Domain> getDomains() throws LibvirtException {
domain = hypervisorConnection.domainLookupByName(c);
domains.put(domain.getName(), domain);
} catch (Exception e) {
LogRecord rec = new LogRecord(Level.INFO, "Error retreiving information for domain with name: {0}");
LogRecord rec = new LogRecord(Level.INFO, "Error retreiving information for domain with name: {0}.");
rec.setParameters(new Object[]{c});
rec.setThrown(e);
LOGGER.log(rec);
Expand All @@ -163,13 +163,13 @@ public synchronized Map<String, Domain> getDomains() throws LibvirtException {
domain = hypervisorConnection.domainLookupByID(c);
domains.put(domain.getName(), domain);
} catch (Exception e) {
LogRecord rec = new LogRecord(Level.INFO, "Error retreiving information for domain with id: {0}");
LogRecord rec = new LogRecord(Level.INFO, "Error retreiving information for domain with id: {0}.");
rec.setParameters(new Object[]{c});
rec.setThrown(e);
LOGGER.log(rec);
}
}
LogRecord rec = new LogRecord(Level.INFO, "Closing hypervisor connection");
LogRecord rec = new LogRecord(Level.INFO, "Closing hypervisor connection.");
LOGGER.log(rec);
hypervisorConnection.close();
} else {
Expand Down Expand Up @@ -198,7 +198,7 @@ public boolean canProvision(Label label) {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
sb.append("Hypervisor");
sb.append("{hypervisorUri='").append(hypervisorHost).append('\'');
sb.append(", username='").append(username).append('\'');
Expand All @@ -211,7 +211,7 @@ public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

public String constructHypervisorURI() {
public String getHypervisorURI() {
return constructHypervisorURI(hypervisorType, "ssh://", username, hypervisorHost, hypervisorSshPort, hypervisorSystemUrl);
}

Expand Down Expand Up @@ -251,24 +251,24 @@ public FormValidation doTestConnection(
@QueryParameter String username, @QueryParameter String hypervisorSystemUrl) throws Exception, ServletException {
try {
if (hypervisorHost == null) {
return FormValidation.error("Hypervisor Host is not specified");
return FormValidation.error("Hypervisor Host is not specified!");
}
if (hypervisorType == null) {
return FormValidation.error("Hypervisor type is not specified");
return FormValidation.error("Hypervisor type is not specified!");
}
if (username == null) {
return FormValidation.error("Username is not specified");
return FormValidation.error("Username is not specified!");
}

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

LogRecord rec = new LogRecord(Level.INFO,
"Testing connection to hypervisor: {0}");
rec.setParameters(new Object[]{hypervisorUri});
LOGGER.log(rec);
Connect hypervisorConnection = new Connect(hypervisorUri, false);
hypervisorConnection.close();
return FormValidation.ok("Connected successfully");
return FormValidation.ok("Successfully connected to: " + hypervisorUri);
} catch (LibvirtException e) {
LogRecord rec = new LogRecord(Level.WARNING,
"Failed to check hypervisor connection to {0} as {1}/******");
Expand All @@ -278,14 +278,14 @@ public FormValidation doTestConnection(
return FormValidation.error(e.getMessage());
} catch (UnsatisfiedLinkError e) {
LogRecord rec = new LogRecord(Level.WARNING,
"Failed to connect to hypervisor. Check libvirt installation on hudson machine!");
"Failed to connect to hypervisor. Check libvirt installation on jenkins machine!");
rec.setThrown(e);
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!");
"Failed to connect to hypervisor. Check libvirt installation on jenkins machine!");
rec.setThrown(e);
rec.setParameters(new Object[]{hypervisorHost, username});
LOGGER.log(rec);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/libvirt/PluginImpl.java
Expand Up @@ -63,7 +63,7 @@ public FormValidation doCheckStartupWaitingPeriodSeconds (@QueryParameter String
if (v < 0) {
return FormValidation.error("Negative value..");
} else if (v == 0) {
return FormValidation.warning("You declared this virtual machine to be ready right away. It probably needs a couple of seconds to get ready!");
return FormValidation.warning("You declared this virtual machine to be ready right away. It probably needs a couple of seconds before it is ready to process jobs!");
} else {
return FormValidation.ok();
}
Expand Down
85 changes: 31 additions & 54 deletions src/main/java/hudson/plugins/libvirt/VirtualMachineLauncher.java
Expand Up @@ -22,8 +22,8 @@
package hudson.plugins.libvirt;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.slaves.Cloud;
import hudson.slaves.ComputerLauncher;
Expand All @@ -46,28 +46,28 @@ public class VirtualMachineLauncher extends ComputerLauncher {
private String hypervisorDescription;
private String virtualMachineName;
private final int WAIT_TIME_MS;

@DataBoundConstructor
public VirtualMachineLauncher(ComputerLauncher delegate, String hypervisorDescription, String virtualMachineName, int waitingTimeSecs) {
super();
this.delegate = delegate;
this.virtualMachineName = virtualMachineName;
this.hypervisorDescription = hypervisorDescription;
this.WAIT_TIME_MS = waitingTimeSecs*1000;
buildVirtualMachine();
lookupVirtualMachineHandle();
}

private void buildVirtualMachine() {
private void lookupVirtualMachineHandle() {
if (hypervisorDescription != null && virtualMachineName != null) {
LOGGER.log(Level.INFO, "Building virtual machine object from names");
LOGGER.log(Level.INFO, "Grabbing hypervisor...");
Hypervisor hypervisor = null;
for (Cloud cloud : Hudson.getInstance().clouds) {
if (cloud instanceof Hypervisor && ((Hypervisor) cloud).getHypervisorDescription().equals(hypervisorDescription)) {
hypervisor = (Hypervisor) cloud;
break;
}
}
LOGGER.log(Level.INFO, "Hypervisor found... getting Virtual Machines associated");
LOGGER.log(Level.INFO, "Hypervisor found, searching for a matching virtual machine for \"" + virtualMachineName + "\"...");
for (VirtualMachine vm : hypervisor.getVirtualMachines()) {
if (vm.getName().equals(virtualMachineName)) {
virtualMachine = vm;
Expand All @@ -93,35 +93,35 @@ public boolean isLaunchSupported() {
@Override
public void launch(SlaveComputer slaveComputer, TaskListener taskListener)
throws IOException, InterruptedException {
taskListener.getLogger().println("Getting connection to the virtual datacenter");
if (virtualMachine == null) {
taskListener.getLogger().println("No connection ready to the Hypervisor... reconnecting...");
buildVirtualMachine();
}
try {
taskListener.getLogger().println("Virtual machine \"" + virtualMachineName + "\"(slave \"" + slaveComputer.getDisplayName() + "\") is to be started ...");
try {
if (virtualMachine == null) {
taskListener.getLogger().println("No connection ready to the Hypervisor, reconnecting...");
lookupVirtualMachineHandle();
if (virtualMachine == null) // still null? no such vm!
throw new Exception("Virtual machine \"" + virtualMachineName + "\"(slave \"" + slaveComputer.getDisplayName() + "\") not found on the specified hypervisor!");
}

Map<String, Domain> computers = virtualMachine.getHypervisor().getDomains();
taskListener.getLogger().println("Looking for the virtual machine on Hypervisor...");
for (String domainName : computers.keySet()) {
taskListener.getLogger().println("Looking for \"" + virtualMachineName + "\" on Hypervisor...");
for (String domainName : computers.keySet()) {
if (virtualMachine.getName().equals(domainName)) {
taskListener.getLogger().println("Virtual Machine Found");
Domain domain = computers.get(domainName);

if (domain.getInfo().state != DomainState.VIR_DOMAIN_BLOCKED && domain.getInfo().state != DomainState.VIR_DOMAIN_RUNNING) {
taskListener.getLogger().println("Starting virtual machine");
taskListener.getLogger().println("...Virtual machine found: starting, waiting for " + WAIT_TIME_MS + "ms to let it fully boot up...");
domain.create();

taskListener.getLogger().println("Waiting " + WAIT_TIME_MS + "ms for the VM machine to start up");
Thread.sleep(WAIT_TIME_MS);
} else {
taskListener.getLogger().println("Virtual machine is already running. No startup procedure required.");
taskListener.getLogger().println("Virtual machine found; it is already running, no startup required.");
}
taskListener.getLogger().println("Finished startup procedure... Connecting slave client");
taskListener.getLogger().println("Finished startup, connecting slave client");
delegate.launch(slaveComputer, taskListener);
return;
}
}
taskListener.getLogger().println("Error! Could not find virtual machine on the hypervisor");
throw new IOException("VM not found!");
taskListener.getLogger().println("Error! Could not find \"" + virtualMachineName + "\" on the hypervisor!");
throw new IOException("VM \"" + virtualMachine.getName() + "\"(slave \"" + slaveComputer.getDisplayName() + "\") not found!");
} catch (IOException e) {
e.printStackTrace(taskListener.getLogger());
throw e;
Expand All @@ -131,27 +131,27 @@ public void launch(SlaveComputer slaveComputer, TaskListener taskListener)
}

@Override
public void afterDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) {
taskListener.getLogger().println("Running disconnect procedure...");
public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) {
taskListener.getLogger().println("Virtual machine \"" + virtualMachineName + "\" (slave \"" + slaveComputer.getDisplayName() + "\") is to be shut down.");
delegate.afterDisconnect(slaveComputer, taskListener);
taskListener.getLogger().println("Shutting down Virtual Machine...");
try {

Map<String, Domain> computers = virtualMachine.getHypervisor().getDomains();
taskListener.getLogger().println("Looking for the virtual machine on Hypervisor...");
taskListener.getLogger().println("Looking for \"" + virtualMachineName + "\" on Hypervisor...");
for (String domainName : computers.keySet()) {
if (virtualMachine.getName().equals(domainName)) {
Domain domain = computers.get(domainName);
taskListener.getLogger().println("Virtual Machine Found");
if (domain.getInfo().state.equals(DomainState.VIR_DOMAIN_RUNNING) || domain.getInfo().state.equals(DomainState.VIR_DOMAIN_BLOCKED)) {
taskListener.getLogger().println("Shutting down virtual machine");
taskListener.getLogger().println("...Virtual machine found, shutting down.");
domain.shutdown();
Thread.sleep(10000); // gi
} else {
taskListener.getLogger().println("Virtual machine is already suspended. No shutdown procedure required.");
taskListener.getLogger().println("...Virtual machine found; it is already suspended, no shutdown required.");
}
return;
}
}
taskListener.getLogger().println("Error! Could not find virtual machine on the hypervisor");
taskListener.getLogger().println("Error! Could not find \"" + virtualMachineName + "\" on the hypervisor!");
} catch (Throwable t) {
taskListener.fatalError(t.getMessage(), t);
}
Expand All @@ -164,29 +164,6 @@ public void beforeDisconnect(SlaveComputer slaveComputer, TaskListener taskListe

@Override
public Descriptor<ComputerLauncher> getDescriptor() {
return Hudson.getInstance().getDescriptor(getClass());
throw new UnsupportedOperationException();
}
@Extension
public static final Descriptor<ComputerLauncher> DESCRIPTOR = new Descriptor<ComputerLauncher>() {

private String hypervisorDescription;
private String virtualMachineName;
private ComputerLauncher delegate;

public String getDisplayName() {
return "Virtual Machine Launcher";
}

public String getHypervisorDescription() {
return hypervisorDescription;
}

public String getVirtualMachineName() {
return virtualMachineName;
}

public ComputerLauncher getDelegate() {
return delegate;
}
};
}
10 changes: 0 additions & 10 deletions src/main/java/hudson/plugins/libvirt/VirtualMachineSlave.java
Expand Up @@ -122,16 +122,6 @@ public List<Hypervisor> getHypervisors() {
}
return result;
}

public List<Descriptor<ComputerLauncher>> getComputerLauncherDescriptors() {
List<Descriptor<ComputerLauncher>> result = new ArrayList<Descriptor<ComputerLauncher>>();
for (Descriptor<ComputerLauncher> launcher : Functions.getComputerLauncherDescriptors()) {
if (!VirtualMachineLauncher.DESCRIPTOR.getClass().isAssignableFrom(launcher.getClass())) {
result.add(launcher);
}
}
return result;
}

public String getHypervisorDescription() {
return hypervisorDescription;
Expand Down
Expand Up @@ -3,7 +3,7 @@
<f:entry title="${%Hypervisor}" field="hypervisorDescription" help="/plugin/libvirt-slave/help-libvirt-hypervisorUri.html">
<select class="setting-input" name="hypervisorDescription" value="${it.hypervisorDescription}"
onchange="updateListBox(document.getElementsByName('virtualMachineName')[0],'${rootURL}/plugin/libvirt-slave/computerNameValues?value='+encode(this.value))">
<option>Select an Hypervisor...</option>
<option>Select Hypervisor...</option>
<j:forEach var="d" items="${descriptor.getHypervisors()}" varStatus="loop">
<option selected="${d.hypervisorDescription==it.hypervisorDescription?'true':null}">
${d.hypervisorDescription}
Expand Down Expand Up @@ -46,7 +46,7 @@

<f:dropdownList name="slave.delegateLauncher" title="${%Secondary launch method}"
help="${descriptor.getHelpFile('launcher')}">
<j:forEach var="d" items="${descriptor.getComputerLauncherDescriptors()}" varStatus="loop">
<j:forEach var="d" items="${h.getComputerLauncherDescriptors()}" varStatus="loop">
<f:dropdownListBlock value="${d.clazz.name}" name="${d.displayName}"
selected="${it.delegateLauncher.descriptor==d}"
title="${d.displayName}">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/index.jelly
@@ -1,3 +1,3 @@
<div>
This plugin provide a way to create slave computer as VirtualMachine controlled by KVM/XEN (start before build, shutdown after)
This plugin provides a way to create slave computer as VirtualMachine controlled by KVM/XEN (start before build, shutdown after).
</div>
3 changes: 0 additions & 3 deletions src/main/webapp/help-libvirt-computerName.html
Expand Up @@ -3,7 +3,4 @@
Here you can find all configured machine in your hypervisor.<br/>
If you have an empty combo, maybe there is problem with the hypervisor connection.
</p>
<p>
Communication with hypervisor is done using ssh tunnel and you need to put hudson user key in known_keys inside hypervisor ssh folder.
</p>
</div>
6 changes: 3 additions & 3 deletions src/main/webapp/help-libvirt-hypervisorHost.html
@@ -1,11 +1,11 @@
<div>
<p>
Hostname or IP address of your Hypervisor machine. <br/>
Hostname or IP address of your Hypervisor. <br/>
Something like:<br/>
<b>machine.mydomain.com</b><br/>
<b>machine.mydomain.com</b> or<br/>
<b>192.168.10.10</b>
</p>
<p>
You must reach this machine using SSH and you need to exchange rsa key between hypervisor and hudson.
You must be able to reach this machine using SSH (pubkey authentication).
</p>
</div>
5 changes: 5 additions & 0 deletions src/main/webapp/help-libvirt-hypervisorSshPort.html
@@ -0,0 +1,5 @@
<div>
<p>
Select your SSH port (usually 22).
</p>
</div>
2 changes: 1 addition & 1 deletion src/main/webapp/help-libvirt-hypervisorType.html
@@ -1,5 +1,5 @@
<div>
<p>
Select your Hypervisor Type. Inside combox you can find libvirt supported types.
Select your Hypervisor type.
</p>
</div>
2 changes: 1 addition & 1 deletion src/main/webapp/help-libvirt-hypervisorUri.html
@@ -1,5 +1,5 @@
<div>
<p>
In this combox you can find all configured Hypervisor. To configure an Hypervisor you need to go in clod computer area inside admin page.
In this dropdown box you can find all configured Hypervisors. To configure an Hypervisor, open the Jenkins administration page.
</p>
</div>

0 comments on commit 550e328

Please sign in to comment.