Skip to content

Commit

Permalink
Merge pull request #1 from mihais/JENKINS-8634
Browse files Browse the repository at this point in the history
Add support for VirtualBox 4
  • Loading branch information
choas committed Feb 19, 2012
2 parents 4927592 + ed8828e commit 0ff35af
Show file tree
Hide file tree
Showing 40 changed files with 983 additions and 150 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -11,3 +11,7 @@ work
.settings
.classpath
.project
/lib/.index/

# Netbeans project files
plugin/nb-configuration.xml
10 changes: 5 additions & 5 deletions client/pom.xml
Expand Up @@ -2,15 +2,15 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>virtualbox-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>virtualbox-client</artifactId>
<packaging>jar</packaging>
<name>Hudson VirtualBox Client</name>
<name>Jenkins VirtualBox Client</name>

<build>
<plugins>
Expand Down Expand Up @@ -58,9 +58,9 @@
</dependency>
-->
<dependency>
<groupId>org.jvnet.hudson.main</groupId>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>remoting</artifactId>
<version>1.340</version>
<version>1.421</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion client/src/main/java/hudson/plugins/virtualbox/Client.java
Expand Up @@ -32,7 +32,7 @@ public void start() throws Exception {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
String url = dom.getElementsByTagName("url").item(0).getTextContent();

System.out.println("Hudson: " + url);
System.out.println("Jenkins: " + url);
hudson.remoting.Launcher.main("-jnlpUrl", getJnlp(url));
}

Expand Down
10 changes: 10 additions & 0 deletions lib/org/virtualbox/readme.txt
@@ -0,0 +1,10 @@
Command used to install vboxws.jar version 4.1.8 to local repository:

mvn deploy:deploy-file
-DgroupId=org.virtualbox
-DartifactId=vboxws-41
-Dversion=4.1.8
-Dpackaging=jar
-Dfile=/PATH_TO_DOWNLOADED_JAR/vboxjws-4.1.8.jar
-Durl=file:/PATH_TO_PLUGIN_SOURCES/virtualbox-plugin/plugin/../lib
-DrepositoryId=virtualbox-libs
Binary file not shown.
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-40/4.0.8/vboxws-40-4.0.8.jar.md5
@@ -0,0 +1 @@
22cc25ee2ea8038e269bb4cbc49bed46
@@ -0,0 +1 @@
ae21f9aff680b02a85d169c447698947c196375c
8 changes: 8 additions & 0 deletions lib/org/virtualbox/vboxws-40/4.0.8/vboxws-40-4.0.8.pom
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-40</artifactId>
<version>4.0.8</version>
</project>
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-40/4.0.8/vboxws-40-4.0.8.pom.md5
@@ -0,0 +1 @@
54314f2a2bed73d0d422e563e09877a8
@@ -0,0 +1 @@
50bab10c14216796e925a3048974be371e140973
12 changes: 12 additions & 0 deletions lib/org/virtualbox/vboxws-40/maven-metadata.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-40</artifactId>
<version>4.0.8</version>
<versioning>
<versions>
<version>4.0.8</version>
</versions>
<lastUpdated>20120206105030</lastUpdated>
</versioning>
</metadata>
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-40/maven-metadata.xml.md5
@@ -0,0 +1 @@
54119670aa8c7ef70c625521e36dff2d
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-40/maven-metadata.xml.sha1
@@ -0,0 +1 @@
0894cbfdbc068ff2ce6aa326177e90989fcca581
Binary file not shown.
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-41/4.1.8/vboxws-41-4.1.8.jar.md5
@@ -0,0 +1 @@
3258ebbedce79596ab2560ee7b2565cf
@@ -0,0 +1 @@
16e096d2eb63090714fddecc72f722caaae4a995
8 changes: 8 additions & 0 deletions lib/org/virtualbox/vboxws-41/4.1.8/vboxws-41-4.1.8.pom
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-41</artifactId>
<version>4.1.8</version>
</project>
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-41/4.1.8/vboxws-41-4.1.8.pom.md5
@@ -0,0 +1 @@
674fed483f2f0d45f71388476bd876dd
@@ -0,0 +1 @@
8722eb98f2a6bb91d448d2d80652ade16706bc93
12 changes: 12 additions & 0 deletions lib/org/virtualbox/vboxws-41/maven-metadata.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-41</artifactId>
<version>4.1.8</version>
<versioning>
<versions>
<version>4.1.8</version>
</versions>
<lastUpdated>20120206104410</lastUpdated>
</versioning>
</metadata>
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-41/maven-metadata.xml.md5
@@ -0,0 +1 @@
59778cafe52099088661cf5aa7ab5bc0
1 change: 1 addition & 0 deletions lib/org/virtualbox/vboxws-41/maven-metadata.xml.sha1
@@ -0,0 +1 @@
dad4453a49809007259ef963935d5d6e939c197c
18 changes: 14 additions & 4 deletions plugin/pom.xml
Expand Up @@ -2,16 +2,16 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>virtualbox-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>virtualbox</artifactId>
<packaging>hpi</packaging>
<name>Hudson VirtualBox Plugin</name>
<url>http://wiki.hudson-ci.org/display/HUDSON/VirtualBox+Plugin</url>
<name>Jenkins VirtualBox Plugin</name>
<url>http://wiki.jenkins-ci.org/display/JENKINS/VirtualBox+Plugin</url>

<dependencies>
<dependency>
Expand All @@ -20,6 +20,16 @@
<version>3.1</version>
<classifier>java15</classifier>
</dependency>
<dependency>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-40</artifactId>
<version>4.0.8</version>
</dependency>
<dependency>
<groupId>org.virtualbox</groupId>
<artifactId>vboxws-41</artifactId>
<version>4.1.8</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Expand Up @@ -4,9 +4,8 @@
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.BuildWrapper;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Evgeny Mandrikov
Expand All @@ -26,16 +25,14 @@ public VirtualBoxBuildWrapper(String hostName, String virtualMachineName) {
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
VirtualBoxMachine machine = VirtualBoxPlugin.getVirtualBoxMachine(getHostName(), getVirtualMachineName());
listener.getLogger().println(Messages.VirtualBoxLauncher_startVM(machine));
VirtualBoxUtils.startVm(machine, "headless"); // TODO type
// TODO wait for start
VirtualBoxUtils.startVm(machine, "headless", new VirtualBoxTaskListenerLog(listener, "[VirtualBox] ")); // TODO type

class EnvironmentImpl extends Environment {
@Override
public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
VirtualBoxMachine machine = VirtualBoxPlugin.getVirtualBoxMachine(getHostName(), getVirtualMachineName());
listener.getLogger().println(Messages.VirtualBoxLauncher_stopVM(machine));
VirtualBoxUtils.stopVm(machine);
// TODO wait for stop
VirtualBoxUtils.stopVm(machine, new VirtualBoxTaskListenerLog(listener, "[VirtualBox] "));
return true;
}
}
Expand Down
29 changes: 18 additions & 11 deletions plugin/src/main/java/hudson/plugins/virtualbox/VirtualBoxCloud.java
Expand Up @@ -8,13 +8,13 @@
import hudson.slaves.NodeProvisioner;
import hudson.util.FormValidation;
import hudson.util.Scrambler;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

/**
* {@link Cloud} implementation for VirtualBox.
Expand Down Expand Up @@ -52,15 +52,21 @@ public boolean canProvision(Label label) {
return false;
}

private List<VirtualBoxMachine> retrieveMachines() {
return VirtualBoxUtils.getMachines(this);
public synchronized List<VirtualBoxMachine> refreshVirtualMachinesList() {
virtualBoxMachines = VirtualBoxUtils.getMachines(this, new VirtualBoxSystemLog(LOG, "[VirtualBox] "));
return virtualBoxMachines;
}

public List<VirtualBoxMachine> getVirtualMachines() {
if (virtualBoxMachines == null) {
virtualBoxMachines = retrieveMachines();
public synchronized VirtualBoxMachine getVirtualMachine(String virtualMachineName) {
if (null == virtualBoxMachines) {
refreshVirtualMachinesList();
}
return virtualBoxMachines;
for (VirtualBoxMachine machine: virtualBoxMachines) {
if (virtualMachineName.equals(machine.getName())) {
return machine;
}
}
return null;
}

@Extension
Expand All @@ -79,9 +85,10 @@ public FormValidation doTestConnection(
@QueryParameter String username,
@QueryParameter String password
) {
LOG.info("Testing connection to " + url + " with username " + username);
LOG.log(Level.INFO, "Testing connection to {0} with username {1}", new Object[]{url, username});
try {
VirtualBoxUtils.getMachines(new VirtualBoxCloud("testConnection", url, username, password));
VirtualBoxUtils.getMachines(new VirtualBoxCloud("testConnection", url, username, password),
new VirtualBoxSystemLog(LOG, "[VirtualBox] "));
return FormValidation.ok(Messages.VirtualBoxHost_success());
} catch (Throwable e) {
return FormValidation.error(e.getMessage());
Expand Down
Expand Up @@ -2,9 +2,8 @@

import hudson.model.Slave;
import hudson.slaves.SlaveComputer;
import org.kohsuke.stapler.HttpResponse;

import java.io.IOException;
import org.kohsuke.stapler.HttpResponse;

/**
* @author Evgeny Mandrikov
Expand Down
Expand Up @@ -4,7 +4,6 @@
import hudson.model.TaskListener;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.SlaveComputer;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -35,22 +34,20 @@ public VirtualBoxComputerLauncher(ComputerLauncher delegate) {
@Override
public void launch(SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException {
VirtualBoxSlave slave = ((VirtualBoxComputer) computer).getNode();
log(listener, "Launching node " + slave.getVirtualMachineName());
try {
// Connect to VirtualBox host
VirtualBoxMachine machine = VirtualBoxPlugin.getVirtualBoxMachine(slave.getHostName(), slave.getVirtualMachineName());
if (machine == null) {
listener.fatalError("Unable to find specified machine");
return;
}
// TODO check virtual machine state - if started, then do nothing
// if no, then start
log(listener, Messages.VirtualBoxLauncher_startVM(machine));
long result = VirtualBoxUtils.startVm(machine, slave.getVirtualMachineType());
long result = VirtualBoxUtils.startVm(machine, slave.getVirtualMachineType(), new VirtualBoxTaskListenerLog(listener, "[VirtualBox] "));
if (result != 0) {
listener.fatalError("Unable to launch");
return;
}
// TODO result may be != 0
} catch (Throwable e) {
listener.fatalError(e.getMessage(), e);
e.printStackTrace(listener.getLogger());
Expand Down Expand Up @@ -110,19 +107,17 @@ public void afterDisconnect(SlaveComputer computer, TaskListener listener) {
log(listener, "Starting stage 2 afterDisconnect");
getCore().afterDisconnect(computer, listener);
log(listener, "Stage 2 afterDisconnect completed");

try {
// Connect to VirtualBox host
VirtualBoxMachine machine = VirtualBoxPlugin.getVirtualBoxMachine(slave.getHostName(), slave.getVirtualMachineName());
if (machine == null) {
listener.fatalError("Unable to find specified machine");
}
// TODO check virtual machine state - if stopped, then do nothing
// if no, then stop
log(listener, Messages.VirtualBoxLauncher_stopVM(machine));
long result = VirtualBoxUtils.stopVm(machine);
long result = VirtualBoxUtils.stopVm(machine, new VirtualBoxTaskListenerLog(listener, "[VirtualBox] "));
if (result != 0) {
listener.fatalError("Unable to stop");
return;
}
} catch (Throwable e) {
listener.fatalError(e.getMessage(), e);
Expand Down
@@ -0,0 +1,17 @@
package hudson.plugins.virtualbox;

import java.util.List;

/**
* @author Mihai Serban
*/
public interface VirtualBoxControl {
public long startVm(VirtualBoxMachine machine, String virtualMachineType, VirtualBoxLogger log);
public long stopVm(VirtualBoxMachine machine, VirtualBoxLogger log);

public List<VirtualBoxMachine> getMachines(VirtualBoxCloud host, VirtualBoxLogger log);
public String getMacAddress(VirtualBoxMachine machine, VirtualBoxLogger log);
public void disconnect();

public boolean isConnected();
}

0 comments on commit 0ff35af

Please sign in to comment.