use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.
the class ReconfigureDisk method addSCSIController.
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;
}
use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.
the class ReconfigureNetworkAdapters method reconfigureNetwork.
public boolean reconfigureNetwork(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
PrintStream jLogger = listener.getLogger();
String expandedDeviceLabel = deviceLabel;
String expandedMacAddress = macAddress;
String expandedPortGroup = portGroup;
String expandedDistributedPortGroup = distributedPortGroup;
String expandedDistributedPortId = distributedPortId;
EnvVars env;
try {
env = run.getEnvironment(listener);
} catch (Exception e) {
throw new VSphereException(e);
}
if (run instanceof AbstractBuild) {
// Add in matrix axes..
env.overrideAll(((AbstractBuild) run).getBuildVariables());
expandedDeviceLabel = env.expand(deviceLabel);
expandedMacAddress = env.expand(macAddress);
expandedPortGroup = env.expand(portGroup);
expandedDistributedPortGroup = env.expand(distributedPortGroup);
expandedDistributedPortId = env.expand(distributedPortId);
}
VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: " + deviceAction.getLabel() + " Network Adapter \"" + expandedDeviceLabel + "\"");
VirtualEthernetCard vEth = null;
if (deviceAction == DeviceAction.ADD) {
vEth = new VirtualE1000();
vEth.setBacking(new VirtualEthernetCardNetworkBackingInfo());
Description description = vEth.getDeviceInfo();
if (description == null) {
description = new Description();
}
description.setLabel(expandedDeviceLabel);
vEth.setDeviceInfo(description);
} else {
vEth = findNetworkDeviceByLabel(vm.getConfig().getHardware().getDevice(), expandedDeviceLabel);
}
if (vEth == null) {
throw new VSphereException("Could not find network device named " + expandedDeviceLabel);
}
// change mac address
if (!expandedMacAddress.isEmpty()) {
VSphereLogger.vsLogger(jLogger, "Reconfiguring MAC Address -> " + expandedMacAddress);
vEth.setMacAddress(expandedMacAddress);
}
// extract backing from ethernet virtual card, always available
VirtualDeviceBackingInfo virtualDeviceBackingInfo = vEth.getBacking();
// change our port group
if (standardSwitch && !expandedPortGroup.isEmpty()) {
VSphereLogger.vsLogger(jLogger, "Reconfiguring Network Port Group -> " + expandedPortGroup);
if (virtualDeviceBackingInfo instanceof VirtualEthernetCardNetworkBackingInfo) {
VirtualEthernetCardNetworkBackingInfo backing = (VirtualEthernetCardNetworkBackingInfo) virtualDeviceBackingInfo;
Network networkPortGroup = getVsphere().getNetworkPortGroupByName(getVM(), expandedPortGroup);
if (networkPortGroup != null) {
backing.deviceName = expandedPortGroup;
} else {
VSphereLogger.vsLogger(jLogger, "Failed to find Network for Port Group -> " + expandedPortGroup);
}
} else {
VSphereLogger.vsLogger(jLogger, "Network Device -> " + expandedDeviceLabel + " isn't standard switch");
}
} else // change out distributed switch port group
if (distributedSwitch && !expandedDistributedPortGroup.isEmpty()) {
VSphereLogger.vsLogger(jLogger, "Reconfiguring Distributed Switch Port Group -> " + expandedDistributedPortGroup + " Port Id -> " + expandedDistributedPortId);
if (virtualDeviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
VirtualEthernetCardDistributedVirtualPortBackingInfo virtualEthernetCardDistributedVirtualPortBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) virtualDeviceBackingInfo;
DistributedVirtualPortgroup distributedVirtualPortgroup = getVsphere().getDistributedVirtualPortGroupByName(getVM(), expandedDistributedPortGroup);
if (distributedVirtualPortgroup != null) {
DistributedVirtualSwitch distributedVirtualSwitch = getVsphere().getDistributedVirtualSwitchByPortGroup(distributedVirtualPortgroup);
DistributedVirtualSwitchPortConnection distributedVirtualSwitchPortConnection = new DistributedVirtualSwitchPortConnection();
distributedVirtualSwitchPortConnection.setSwitchUuid(distributedVirtualSwitch.getUuid());
distributedVirtualSwitchPortConnection.setPortgroupKey(distributedVirtualPortgroup.getKey());
distributedVirtualSwitchPortConnection.setPortKey(expandedDistributedPortId);
virtualEthernetCardDistributedVirtualPortBackingInfo.setPort(distributedVirtualSwitchPortConnection);
VSphereLogger.vsLogger(jLogger, "Distributed Switch Port Group -> " + expandedDistributedPortGroup + "Port Id -> " + expandedDistributedPortId + " successfully configured!");
} else {
VSphereLogger.vsLogger(jLogger, "Failed to find Distributed Virtual Portgroup for Port Group -> " + expandedDistributedPortGroup);
}
} else {
VSphereLogger.vsLogger(jLogger, "Network Device -> " + expandedDeviceLabel + " isn't distributed switch");
}
}
VirtualDeviceConfigSpec vdspec = new VirtualDeviceConfigSpec();
vdspec.setDevice(vEth);
if (deviceAction == DeviceAction.EDIT) {
vdspec.setOperation(VirtualDeviceConfigSpecOperation.edit);
} else if (deviceAction == DeviceAction.REMOVE) {
vdspec.setOperation(VirtualDeviceConfigSpecOperation.remove);
}
// add change into config spec
VirtualDeviceConfigSpec[] deviceConfigSpecs = spec.getDeviceChange();
if (deviceConfigSpecs == null) {
deviceConfigSpecs = new VirtualDeviceConfigSpec[1];
} else {
deviceConfigSpecs = Arrays.copyOf(deviceConfigSpecs, deviceConfigSpecs.length + 1);
}
deviceConfigSpecs[deviceConfigSpecs.length - 1] = vdspec;
spec.setDeviceChange(deviceConfigSpecs);
VSphereLogger.vsLogger(jLogger, "Finished!");
return true;
}
use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.
the class ReconfigureMemory method reconfigureMemory.
public boolean reconfigureMemory(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
PrintStream jLogger = listener.getLogger();
String expandedMemorySize = memorySize;
EnvVars env;
try {
env = run.getEnvironment(listener);
} catch (Exception e) {
throw new VSphereException(e);
}
if (run instanceof AbstractBuild) {
// Add in matrix axes..
env.overrideAll(((AbstractBuild) run).getBuildVariables());
expandedMemorySize = env.expand(memorySize);
}
VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: Memory");
spec.setMemoryMB(Long.valueOf(expandedMemorySize));
VSphereLogger.vsLogger(jLogger, "Finished!");
return true;
}
use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.
the class RenameSnapshot method renameSnapshot.
public boolean renameSnapshot(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
PrintStream jLogger = listener.getLogger();
String expandedVm = vm;
String expandedOldName = oldName;
String expandedNewName = newName;
String expandedNewDescription = newDescription;
EnvVars env;
try {
env = run.getEnvironment(listener);
} catch (Exception e) {
throw new VSphereException(e);
}
if (run instanceof AbstractBuild) {
// Add in matrix axes..
env.overrideAll(((AbstractBuild) run).getBuildVariables());
expandedVm = env.expand(vm);
expandedOldName = env.expand(oldName);
expandedNewName = env.expand(newName);
expandedNewDescription = env.expand(newDescription);
}
VSphereLogger.vsLogger(jLogger, "Renaming snapshot of VM \"" + expandedVm + "\" from \"" + expandedOldName + "\" to \"" + expandedNewName + "\" with description \"" + expandedNewDescription + "\". Please wait ...");
try {
vsphere.renameVmSnapshot(expandedVm, expandedOldName, expandedNewName, expandedNewDescription);
} catch (Exception e) {
throw new VSphereException(e);
}
VSphereLogger.vsLogger(jLogger, "Renamed!");
return true;
}
use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.
the class RevertToSnapshot method revertToSnapshot.
private boolean revertToSnapshot(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException {
PrintStream jLogger = listener.getLogger();
String expandedSnap = snapshotName;
String expandedVm = vm;
EnvVars env;
try {
env = run.getEnvironment(listener);
} catch (Exception e) {
throw new VSphereException(e);
}
if (run instanceof AbstractBuild) {
// Add in matrix axes..
env.overrideAll(((AbstractBuild) run).getBuildVariables());
expandedSnap = env.expand(snapshotName);
expandedVm = env.expand(vm);
}
VSphereLogger.vsLogger(jLogger, "Reverting to snapshot \"" + expandedSnap + "\" for VM " + expandedVm + "...");
vsphere.revertToSnapshot(expandedVm, expandedSnap);
VSphereLogger.vsLogger(jLogger, "Complete.");
return true;
}
Aggregations