Skip to content

Commit

Permalink
Add new vmtools upgrade workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Leonid Belenkiy <[email protected]>
  • Loading branch information
unbreakabl3 committed May 29, 2024
1 parent 06c7c8c commit cc74e42
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 213 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion upgrade_vm_tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.clouddepth</groupId>
<artifactId>upgrade_vm_tools</artifactId>
<version>1.0.73</version>
<version>1.0.82</version>
<packaging>package</packaging>

<scm>
Expand Down
18 changes: 0 additions & 18 deletions upgrade_vm_tools/src/actions/actionSample.js

This file was deleted.

39 changes: 21 additions & 18 deletions upgrade_vm_tools/src/actions/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class Functions {
* upgradePoweredOfVMs
*/

//TODO: rename it to upgradePoweredOfVM
public isUpgradePoweredOffVmAllowed(allowUpgradePoweredOffVm: boolean, vm: VcVirtualMachine): boolean {
if (allowUpgradePoweredOffVm || vm.runtime.powerState.value === "poweredOn") return true;
System.warn(`The current state '${vm.runtime.powerState.value}' is prohibited for the upgrade.`);
Expand Down Expand Up @@ -224,19 +223,24 @@ export class Functions {
System.getModule("com.vmware.library.vc.vm.tools").vim3WaitToolsStarted(vm, 2, vmtoolsTimeout);
} catch (e) {
const toolsStatus = vm.guest.toolsStatus.value;
throw new Error(`VMTools status: ${toolsStatus}. ${e}`);
throw new Error(`VMTools status: ${toolsStatus}: ${e}`);
}
}

public upgradeVmTools({ vm, allowReboot = false, waitForTools }: { vm: VcVirtualMachine; allowReboot: boolean; waitForTools: boolean }): boolean {
const toolsStatus = vm.guest.toolsStatus.value;

const toolsStatus = vm.guest.toolsVersionStatus2;
System.log("tools status: " + toolsStatus);
switch (toolsStatus) {
case "toolsOk":
case "guestToolsSupportedNew":
System.log("VMtools are newer than available. Skipping the upgrade");
case "guestToolsUnmanaged":
System.log("3rd party managed VMtools (open-vm-tools). Skipping the upgrade");
case "guestToolsCurrent":
System.log("VMware Tools are already running and up to date. Nothing to do.");
return true;
case "toolsOld":
case "toolsNotRunning":
case "guestToolsBlacklisted":
case "guestToolsNeedUpgrade":
case "guestToolsSupportedOld":
System.log("Starting VMware Tools upgrade...");
const upgradeArgs = allowReboot ? "/s /vqn" : '/s /v"/qn REBOOT=ReallySuppress"';
try {
Expand All @@ -249,14 +253,14 @@ export class Functions {
}
return true;
} catch (e) {
throw new Error(`Failed to upgrade VMware Tools. ${e}`);
throw new Error(`Failed to upgrade VMware Tools: ${e}`);
}

case "toolsNotInstalled":
throw new Error("Unable to upgrade VMware Tools because they are not currently installed.");
case "guestToolsNotInstalled":
throw new Error(`Unable to upgrade VMware Tools because they are not currently installed.`);

default:
throw new Error("Unexpected VMware Tools status: " + toolsStatus);
throw new Error(`Unexpected VMware Tools status: ${toolsStatus}`);
}
}

Expand All @@ -268,7 +272,6 @@ export class Functions {
}

System.log(`Current VMTools upgrade policy is '${currentVmToolsUpgradePolicy}'. Updating to '${desiredVmToolsUpgradePolicy}'.`);

const configSpec = new VcVirtualMachineConfigSpec();
const toolsSpec = new VcToolsConfigInfo();
toolsSpec.toolsUpgradePolicy = desiredVmToolsUpgradePolicy;
Expand All @@ -279,7 +282,7 @@ export class Functions {
System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task, true, 2);
System.log(`Successfully set VMTools upgrade policy to '${desiredVmToolsUpgradePolicy}'.`);
} catch (e) {
throw new Error(`Failed to set VMTools upgrade policy to '${desiredVmToolsUpgradePolicy}'. ${e}`);
throw new Error(`Failed to set VMTools upgrade policy to '${desiredVmToolsUpgradePolicy}': ${e}`);
}
}

Expand All @@ -288,11 +291,11 @@ export class Functions {
throw new Error("Required parameters are missing");
}
try {
const task = vm.createSnapshot_Task(name, description, memory, quiesce);
const task: VcTask = vm.createSnapshot_Task(name, description, memory, quiesce);
System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task, true, 2);
System.log(`Creating snapshot '${name}' on VM '${vm.name} was completed successfully'`);
} catch (error) {
throw new Error(`Failed to create snapshot '${name}' on VM '${vm.name}'. ${error}`);
throw new Error(`Failed to create snapshot '${name}' on VM '${vm.name}': ${error}`);
}
}

Expand All @@ -312,16 +315,16 @@ export class Functions {
}

public removeSnapshot({ vm, removeChildren, consolidate, snapshotName }: { vm: VcVirtualMachine; removeChildren: boolean; consolidate: boolean; snapshotName: string }): void {
const vmSnapshots = this.getVmSnapshot(vm);
const vmSnapshots: VcVirtualMachineSnapshot[] = this.getVmSnapshot(vm);
if (!vmSnapshots) return;
vmSnapshots.forEach((snapshot) => {
if (snapshot.name === snapshotName) {
try {
const task = snapshot.removeSnapshot_Task(removeChildren, consolidate);
System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task, true, 2);
System.log(`Snapshot '${snapshot.name}' was removed successfully`);
System.log(`Snapshot '${snapshotName}' was removed successfully`);
} catch (error) {
const errorMessage = `Failed to remove snapshot '${snapshot.name}' on VM '${vm.name}': ${error instanceof Error ? error.message : String(error)}`;
const errorMessage = `Failed to remove snapshot '${snapshotName}' on VM '${vm.name}': ${error instanceof Error ? error.message : String(error)}`;
throw new Error(errorMessage);
}
}
Expand Down
2 changes: 1 addition & 1 deletion upgrade_vm_tools/src/license/THIRD-PARTY.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
# Please fill the missing licenses for dependencies :
#
#
#Fri May 24 19:35:16 CEST 2024
#Sun May 26 16:23:58 CEST 2024
classworlds--classworlds--1.1-alpha-2=
org.codehaus.plexus--plexus-container-default--1.0-alpha-9-stable-1=
64 changes: 0 additions & 64 deletions upgrade_vm_tools/src/test/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe("isUpgradePoweredOffVmAllowed", () => {
const vm = { runtime: { powerState: { value: "poweredOn" } } };
const allowUpgradePoweredOffVm = true;
//@ts-ignore
//TODO: convert 'vm' to be of type VCVirtualMachine
const result = func.isUpgradePoweredOffVmAllowed(allowUpgradePoweredOffVm, vm);
expect(result).toBe(true);
});
Expand All @@ -27,7 +26,6 @@ describe("isUpgradePoweredOffVmAllowed", () => {
const vm = { runtime: { powerState: { value: "poweredOff" } } };
const allowUpgradePoweredOffVm = true;
//@ts-ignore
//TODO: convert 'vm' to be of type VCVirtualMachine
const result = func.isUpgradePoweredOffVmAllowed(allowUpgradePoweredOffVm, vm);
expect(result).toBe(true);
});
Expand All @@ -37,7 +35,6 @@ describe("isUpgradePoweredOffVmAllowed", () => {
const vm = { runtime: { powerState: { value: "poweredOff" } } };
const allowUpgradePoweredOffVm = false;
//@ts-ignore
//TODO: convert 'vm' to be of type VCVirtualMachine
const result = func.isUpgradePoweredOffVmAllowed(allowUpgradePoweredOffVm, vm);
expect(result).toBe(false);
expect(spy).toHaveBeenCalledWith(`The current state '${vm.runtime.powerState.value}' is prohibited for the upgrade.`);
Expand Down Expand Up @@ -212,67 +209,6 @@ describe("isUpgradeVmTemplatesAllowed", () => {
});
});

//TODO: fix it
// describe("getVmDiskMode", () => {
// beforeEach(() => {
// func = new Functions();
// (<any>VcVirtualDiskFlatVer2BackingInfo) = {
// device: function () {}
// };
// });
// it("should return the disk device if found", () => {
// const mockVm = {
// config: {
// hardware: {
// device: [
// {
// /* Other device */
// },
// {
// /* Another device */
// },
// { instanceof: VcVirtualDiskFlatVer2BackingInfo } // Mock disk device
// ]
// }
// },
// name: "MyVM"
// };
// //@ts-ignore
// const result = func.getVmDiskMode(mockVm);
// //@ts-ignore
// expect(result).toBe(mockVm.config.hardware.device[2]); // Verify returned device
// });

// it("should return null if no disks are found", () => {
// const mockVm = {
// config: {
// hardware: {
// device: [
// {
// /* Other device */
// }
// ]
// }
// },
// name: "MyVM"
// };
// const spy = spyOn(System, "log");
// //@ts-ignore
// const result = func.getVmDiskMode(mockVm);
// expect(result).toBeNull();
// //expect(console.log).toHaveBeenCalledWith(`No disks found for virtual machine '${mockVm.name}'`); // Verify log message
// });

// it("should return null if VM configuration or hardware information is missing", () => {
// const mockVm = { config: null, name: "MyVM" };
// //@ts-ignore
// const result = func.getVmDiskMode(mockVm);
// const spy = spyOn(System, "log");
// expect(result).toBeNull();
// // expect(console.log).toHaveBeenCalledWith(`VM configuration or hardware information missing for '${mockVm.name}'`); // Verify log message
// });
// });

describe("setVmToolsUpgradePolicy", () => {
let vm: any;
let System: any;
Expand Down
Loading

0 comments on commit cc74e42

Please sign in to comment.