Skip to content

Commit

Permalink
Add disk feature in Reconfigure Hardware panel
Browse files Browse the repository at this point in the history
- Select the biggest available Datastore
- Add SCSI Controller when needed
- Follow disks naming convension
  • Loading branch information
calj authored and Camille Meulien committed Jan 15, 2015
1 parent db8104d commit 6c62a16
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/* Copyright 2014, Camille Meulien
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jenkinsci.plugins.vsphere.builders;

import com.vmware.vim25.*;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.util.FormValidation;

import org.jenkinsci.plugins.vsphere.tools.VSphereException;
import org.jenkinsci.plugins.vsphere.tools.VSphereLogger;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import javax.servlet.ServletException;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReconfigureDisk extends ReconfigureStep {

private final String diskSize;
private final static Pattern filenamePattern = Pattern.compile("^\\[[^]]*\\] (.*)$");

@DataBoundConstructor
public ReconfigureDisk(String diskSize) throws VSphereException {
this.diskSize = diskSize;
}

public String getDiskSize() {
return diskSize;
}

public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws VSphereException {

PrintStream jLogger = listener.getLogger();
EnvVars env;

try {
env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables());
int diskSize = Integer.parseInt(env.expand(this.diskSize));

VirtualDeviceConfigSpec vdiskSpec = createAddDiskConfigSpec(vm, diskSize, jLogger);
VirtualDeviceConfigSpec [] vdiskSpecArray = {vdiskSpec};

spec.setDeviceChange(vdiskSpecArray);
VSphereLogger.vsLogger(jLogger, "Configuration done");
} catch (Exception e) {
throw new VSphereException(e);
}

return true;
}

private VirtualDeviceConfigSpec createAddDiskConfigSpec(
VirtualMachine vm, int diskSize, PrintStream jLogger) throws Exception
{
return createAddDiskConfigSpec(vm, diskSize, jLogger, 0);
}

private VirtualDeviceConfigSpec createAddDiskConfigSpec(
VirtualMachine vm, int diskSize, PrintStream jLogger, Integer retry) throws Exception
{
VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec();
VirtualDisk disk = new VirtualDisk();
VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo();
VirtualSCSIController scsiController = null;

int key = 0;
int unitNumber;
int diskSizeInKB = diskSize * 1024 * 1024;

String diskMode = "persistent";
HashMap<String, Boolean> diskNames = new HashMap<String, Boolean>();

for (VirtualDevice vmDevice : vm.getConfig().getHardware().getDevice()) {
if (vmDevice instanceof VirtualSCSIController) {
int[] list = ((VirtualSCSIController)vmDevice).getDevice();
if (scsiController == null && (list == null || list.length < 15)) {
scsiController = (VirtualSCSIController) vmDevice;
}
} else if (vmDevice instanceof VirtualDisk) {
if (vmDevice.getBacking() instanceof VirtualDeviceFileBackingInfo) {
VirtualDeviceFileBackingInfo info = (VirtualDeviceFileBackingInfo) vmDevice.getBacking();
Matcher m = filenamePattern.matcher(info.getFileName());
if (m.matches()) {
diskNames.put(m.group(1), true);
} else {
VSphereLogger.vsLogger(jLogger, String.format("Warning: unreconnized disk filename format: %s", info.getFileName()));
}
}
}
}

String diskName = null;
for (int i = 1; ; ++i) {
if (!diskNames.containsKey(String.format("%s/%s_%d.vmdk", vm.getName(), vm.getName(), i))) {
diskName = String.format("%s_%d", vm.getName(), i);
break;
}
}

VSphereLogger.vsLogger(jLogger, String.format("Preparing to add disk %s of %dGB", diskName, diskSize));

if (scsiController == null) {
if (retry > 1) {
throw new VSphereException("Unable to add a SCSI Controller");
}
VSphereLogger.vsLogger(jLogger, String.format("Adding a SCSI Controller"));
addSCSIController(vm);
return createAddDiskConfigSpec(vm, diskSize, jLogger, retry + 1);
}

unitNumber = selectUnitNumber(vm, scsiController);
key = scsiController.getKey();

VSphereLogger.vsLogger(jLogger, String.format("Controller key: %d Unit Number %d", key, unitNumber));

String dsName = selectDatastore(diskSizeInKB, jLogger);
if (dsName == null)
{
return null;
}
String fileName = "["+ dsName +"] "+ vm.getName() + "/" + diskName + ".vmdk";

diskfileBacking.setFileName(fileName);
diskfileBacking.setDiskMode(diskMode);

disk.setControllerKey(key);
disk.setUnitNumber(unitNumber);
disk.setBacking(diskfileBacking);
disk.setCapacityInKB(diskSizeInKB);
disk.setKey(-1);

diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create);
diskSpec.setDevice(disk);

return diskSpec;
}

private VirtualLsiLogicController addSCSIController(VirtualMachine vm) throws Exception {
VirtualMachineConfigInfo vmConfig = vm.getConfig();
VirtualPCIController pci = null;
Set<Integer> scsiBuses = new HashSet<Integer>();

for (VirtualDevice vmDevice : vmConfig.getHardware().getDevice()) {
if (vmDevice instanceof VirtualPCIController) {
pci = (VirtualPCIController) vmDevice;
} else if (vmDevice instanceof VirtualSCSIController) {
VirtualSCSIController ctrl = (VirtualSCSIController) vmDevice;
scsiBuses.add(ctrl.getBusNumber());
}
}
if (pci == null) {
throw new VSphereException("No PCI controller found");
}
VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceSpec = new VirtualDeviceConfigSpec();
deviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
scsiCtrl.setControllerKey(pci.getKey());
scsiCtrl.setSharedBus(VirtualSCSISharing.noSharing);
for (int i=0 ; ; ++i) {
if (!scsiBuses.contains(Integer.valueOf(i))) {
scsiCtrl.setBusNumber(i);
break;
}
}
deviceSpec.setDevice(scsiCtrl);
vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] {deviceSpec});
Task task = vm.reconfigVM_Task(vmSpec);
task.waitForTask();
return scsiCtrl;
}

private int selectUnitNumber(VirtualMachine vm, VirtualController controller) {
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
int unitNumber = 0;

map.put(7, true); // Unit number 7 is reserved for the controller

for (VirtualDevice vmDevice : vm.getConfig().getHardware().getDevice()) {
if (vmDevice.getUnitNumber() != null &&
(vmDevice.getControllerKey() == controller.getKey() || vmDevice.getKey() == controller.getKey())) {
map.put(vmDevice.getUnitNumber(), true);
}
}
while (map.containsKey(unitNumber)) {
unitNumber++;
}
return unitNumber;
}

private String selectDatastore(int sizeInKB, PrintStream jLogger) throws Exception
{
Datastore datastore = null;
long freeSpace = 0;

for (ManagedEntity entity : vsphere.getDatastores()) {
if (entity instanceof Datastore) {
Datastore ds = (Datastore)entity;
long fs = ds.getSummary().getFreeSpace();
if (fs > sizeInKB && fs > freeSpace) {
datastore = ds;
freeSpace = fs;
}
}
}

if (datastore == null) {
throw new VSphereException("No datastore with enough space found");
}

VSphereLogger.vsLogger(jLogger, String.format("Selected datastore `%s` with free size: %dGB", datastore.getName(), freeSpace / 1024 / 1024 / 1024));
return datastore.getName();
}

@Extension
public static final class ReconfigureDiskDescriptor extends ReconfigureStepDescriptor {

public ReconfigureDiskDescriptor() {
load();
}

public FormValidation doCheckDiskSize(@QueryParameter String value)
throws IOException, ServletException {

if (value.length() == 0)
return FormValidation.error(Messages.validation_required("Disk size"));
return FormValidation.ok();
}

@Override
public String getDisplayName() {
return Messages.vm_title_ReconfigureDisk();
}

public FormValidation doTestData(@QueryParameter String diskSize) {
try {
if (Integer.valueOf(diskSize) < 0) {
return FormValidation.error(Messages.validation_positiveInteger(diskSize));
}

return FormValidation.ok();

} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

}
24 changes: 21 additions & 3 deletions src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,32 @@ private Datastore getDatastoreByName(final String datastoreName, ManagedEntity r
return (Datastore) new InventoryNavigator(rootEntity).searchManagedEntity("Datastore", datastoreName);
}

/**
* @return - ManagedEntity array of Datastore
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
public ManagedEntity[] getDatastores() throws VSphereException {
try {
return new InventoryNavigator(
getServiceInstance().getRootFolder()).searchManagedEntities(
"Datastore");
} catch (Exception e) {
throw new VSphereException(e);
}
}

/**
* @param poolName - Name of pool to use
* @return - ResourcePool obect
* @return - ResourcePool object
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
* @throws MalformedURLException
* @throws VSphereException
*/
private ResourcePool getResourcePoolByName(final String poolName, ManagedEntity rootEntity) throws InvalidProperty, RuntimeFault, RemoteException, MalformedURLException {
if (rootEntity==null) rootEntity=getServiceInstance().getRootFolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vm.title.Reconfigure=Reconfigure VM
vm.title.ReconfigureNetworkAdapter=Edit Network Adapter
vm.title.ReconfigureCpu=Edit CPU
vm.title.ReconfigureMemory=Edit Memory
vm.title.ReconfigureDisk=Add a disk
vm.title.Rename=Rename VM
vm.title.RenameSnapshot=Rename Snapshot
vm.title.TakeSnapshot=Take Snapshot
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
Copyright 2013, Camille Meulien
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?jelly escape-by-default='true'?>

<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="${%Disk size in GB}" field="diskSize">
<f:textbox clazz="required" />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The disk size in GB.
</div>

0 comments on commit 6c62a16

Please sign in to comment.