-
Notifications
You must be signed in to change notification settings - Fork 910
added CPU config and clne vm along with CPU reconfig #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArpitSharma2800
wants to merge
4
commits into
vmware:master
Choose a base branch
from
ArpitSharma2800:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from pyVmomi import vim | ||
| from pyVim.task import WaitForTasks, WaitForTask | ||
| from tools import cli, pchelper, service_instance, tasks | ||
|
|
||
| def clone_vm_CPU_reconfig(si, vm_name, new_vmname, datacenter_name, host_ip, power_on, datastore_name=None, cpu=None, memory=None): | ||
| content = si.RetrieveContent() | ||
| VM = pchelper.get_obj(content, [vim.VirtualMachine], vm_name) | ||
| datacenter = pchelper.get_obj(content, [vim.Datacenter], datacenter_name) | ||
| destination_host = pchelper.get_obj(content, [vim.HostSystem], host_ip) | ||
| destfolder = datacenter.vmFolder | ||
| host_dest_ip = destination_host.parent.host[0] | ||
| source_pool = destination_host.parent.resourcePool | ||
| for i in range(len(destination_host.parent.host)): | ||
| if destination_host.parent.host[i].name == host_ip: | ||
| host_dest_ip = destination_host.parent.host[i] | ||
|
|
||
| if datastore_name: | ||
| datastore = pchelper.search_for_obj(content, [vim.Datastore], datastore_name) | ||
| else: | ||
| datastore = pchelper.get_obj( | ||
| content, [vim.Datastore], VM.datastore[0].info.name) | ||
|
|
||
| relospec = vim.vm.RelocateSpec() | ||
| relospec.datastore = datastore | ||
| relospec.pool = source_pool | ||
| relospec.host = host_dest_ip | ||
|
|
||
| clonespec = vim.vm.CloneSpec() | ||
| clonespec.location = relospec | ||
|
|
||
| print("Running Task") | ||
| task = VM.Clone(folder=destfolder, name=new_vmname, spec=clonespec) | ||
| WaitForTask(task) | ||
| print("vm cloned") | ||
|
|
||
| vm = pchelper.get_obj(content, [vim.VirtualMachine], new_vmname) | ||
| config = create_config_spec(cpus=cpu, memory=memory) | ||
| WaitForTasks([vm.ReconfigVM_Task(spec=config)], si=si) | ||
| print(vm.config.hardware.numCPU) | ||
|
|
||
| if power_on: | ||
| task = vm.PowerOnVM_Task() | ||
| tasks.wait_for_tasks(si, [task]) | ||
| print("{0}".format(task.info.state)) | ||
|
|
||
| print("VM All tasks completed.") | ||
|
|
||
|
|
||
| def create_config_spec(memory, cpus): | ||
| config = vim.vm.ConfigSpec() | ||
| config.memoryMB = int(memory) | ||
| config.numCPUs = int(cpus) | ||
| return config | ||
|
|
||
| def main(): | ||
| parser = cli.Parser() | ||
| parser.add_required_arguments(cli.Argument.VM_NAME, cli.Argument.DATACENTER_NAME, | ||
| cli.Argument.DATASTORE_NAME, cli.Argument.ESX_IP,) | ||
| parser.add_optional_arguments(cli.Argument.POWER_ON) | ||
|
|
||
| parser.add_custom_argument('--cpu', required=False, action='store', default=2, | ||
| help='Version/release CPUs of the Virtual machine CPUs') | ||
|
|
||
| parser.add_custom_argument('--memory', required=False, action='store', default=4, | ||
| help='Version/release memory (MB) of the Virtual machine memories') | ||
|
|
||
| parser.add_custom_argument('--clone_name', required=False, action='store', default=None, | ||
| help='Version/release name of the Virtual machine clone name') | ||
| args = parser.get_args() | ||
| si = service_instance.connect(args) | ||
| clone_vm_CPU_reconfig(si, args.vm_name, args.clone_name, args.datacenter_name, args.esx_ip, args.power_on, args.datastore_name, args.cpu, args.memory) | ||
|
|
||
|
|
||
| # start this thing | ||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| from pyVmomi import vim | ||
| from tools import cli, service_instance, pchelper, tasks | ||
| from pyVim.task import WaitForTasks | ||
|
|
||
|
|
||
| def main(): | ||
| parser = cli.Parser() | ||
| parser.add_required_arguments(cli.Argument.VM_NAME) | ||
| parser.add_optional_arguments(cli.Argument.POWER_ON) | ||
| # custom argument to get CPU count | ||
| parser.add_custom_argument('--cpu', required=False, action='store', default=None, | ||
| help='Version/release number of the Virtual machine CPUs') | ||
| args = parser.get_args() | ||
| si = service_instance.connect(args) | ||
|
|
||
| content = si.RetrieveContent() | ||
| # getting vm details | ||
| vm = pchelper.get_obj(content, [vim.VirtualMachine], args.vm_name) | ||
| OLDCPU = vm.config.hardware.numCPU | ||
| if not vm: | ||
| print("Could not find VM %s" % args.vm_name) | ||
| else: | ||
| print("Upgrading VM CPUs %s" % args.vm_name) | ||
|
|
||
| if args.cpu is not None: | ||
| print("Upgraded CPU will be %s" % args.cpu) | ||
| # updating CPU | ||
| CPU = int(vm.config.hardware.numCPU) + int(args.cpu) | ||
| else: | ||
| CPU = vm.config.hardware.numCPU | ||
|
|
||
| if OLDCPU < CPU: | ||
| # Powering off VM for CPU change | ||
| if format(vm.runtime.powerState) == "poweredOn": | ||
| print("Attempting to power off {0}".format(vm.name)) | ||
| TASK = vm.PowerOffVM_Task() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No caps for the "TASK". Make it "task" |
||
| tasks.wait_for_tasks(si, [TASK]) | ||
| print("{0}".format(TASK.info.state)) | ||
|
|
||
| spec = vim.vm.ConfigSpec() | ||
| spec.numCPUs = CPU | ||
| WaitForTasks([vm.ReconfigVM_Task(spec=spec)], si=si) | ||
| print(vm.config.hardware.numCPU) | ||
|
|
||
| if args.power_on: | ||
| task = vm.PowerOnVM_Task() | ||
| tasks.wait_for_tasks(si, [task]) | ||
| print("{0}".format(task.info.state)) | ||
| else: | ||
| print("provide valid argument") | ||
|
|
||
|
|
||
| # Start the script | ||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| from datetime import datetime, timedelta | ||
| from tools import cli, service_instance | ||
| from pyVmomi import vim | ||
| from pyVim import connect | ||
|
|
||
|
|
||
| def main(): | ||
| print("Trying to connect to VCENTER SERVER . . .") | ||
| parser = cli.Parser() | ||
| parser.add_required_arguments( | ||
| cli.Argument.MINUTES, cli.Argument.VM_NAME, cli.Argument.SNAPSHOT_NAME) | ||
| parser.add_optional_arguments(cli.Argument.POWER_ON) | ||
| parser.add_custom_argument('--description', required=False, action='store', default=None, | ||
| help='Description of snapshot') | ||
| args = parser.get_args() | ||
| try: | ||
| date_time = datetime.now() + timedelta(minutes=int(args.minutes)) | ||
| except ValueError: | ||
| print('Unrecognized date format') | ||
| return -1 | ||
|
|
||
| si = service_instance.connect(args) | ||
|
|
||
| print("Connected to VCENTER SERVER !") | ||
|
|
||
| view = si.content.viewManager.CreateContainerView(si.content.rootFolder, | ||
| [vim.VirtualMachine], | ||
| True) | ||
| vms = [vm for vm in view.view if vm.name == args.vm_name] | ||
|
|
||
| if not vms: | ||
| print('VM not found') | ||
| connect.Disconnect(si) | ||
| return -1 | ||
| vm = vms[0] | ||
|
|
||
| print("Executing Scheduling process !") | ||
|
|
||
| spec = vim.scheduler.ScheduledTaskSpec() | ||
| spec.name = args.snapshot_name + args.vm_name | ||
| spec.description = args.description | ||
| spec.scheduler = vim.scheduler.OnceTaskScheduler() | ||
| spec.scheduler.runAt = date_time | ||
| spec.action = vim.action.MethodAction() | ||
| spec.action.name = vim.VirtualMachine.CreateSnapshot_Task | ||
| spec.action.argument = [vim.MethodActionArgument()] * 4 | ||
| spec.action.argument[0] = vim.MethodActionArgument() | ||
| spec.action.argument[0].value = args.snapshot_name | ||
| spec.action.argument[1] = vim.MethodActionArgument() | ||
| spec.action.argument[1].value = args.description | ||
| spec.action.argument[2] = vim.MethodActionArgument() | ||
| spec.action.argument[2].value = True | ||
| spec.action.argument[3] = vim.MethodActionArgument() | ||
| spec.action.argument[3].value = False | ||
| print(spec.action.argument) | ||
| spec.enabled = True | ||
| task = si.content.scheduledTaskManager.CreateScheduledTask(vm, spec) | ||
| if task is not None: | ||
| print('Scheduled Task Successfully') | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| from datetime import datetime, timedelta | ||
| from tools import cli, service_instance | ||
| from pyVmomi import vim | ||
| from pyVim import connect | ||
|
|
||
|
|
||
| def main(): | ||
| print("Trying to connect to VCENTER SERVER . . .") | ||
| parser = cli.Parser() | ||
| parser.add_required_arguments(cli.Argument.MINUTES, cli.Argument.VM_NAME) | ||
| parser.add_optional_arguments(cli.Argument.POWER_ON) | ||
| parser.add_custom_argument('--task', required=False, action='store', default=None, | ||
| help='Task name for removal of all snapshots') | ||
| args = parser.get_args() | ||
| try: | ||
| date_time = datetime.now() + timedelta(minutes=int(args.minutes)) | ||
| print(date_time) | ||
| # dt = datetime.strptime(args.date, '%d/%m/%Y %H:%M') | ||
| except ValueError: | ||
| print('Unrecognized date format') | ||
| return -1 | ||
|
|
||
| si = service_instance.connect(args) | ||
|
|
||
| print("Connected to VCENTER SERVER !") | ||
|
|
||
| view = si.content.viewManager.CreateContainerView(si.content.rootFolder, | ||
| [vim.VirtualMachine], | ||
| True) | ||
| vms = [vm for vm in view.view if vm.name == args.vm_name] | ||
|
|
||
| if not vms: | ||
| print('VM not found') | ||
| connect.Disconnect(si) | ||
| return -1 | ||
| vm = vms[0] | ||
|
|
||
| spec = vim.scheduler.ScheduledTaskSpec() | ||
| spec.name = args.task + args.vm_name | ||
| spec.scheduler = vim.scheduler.OnceTaskScheduler() | ||
| spec.scheduler.runAt = date_time | ||
| spec.action = vim.action.MethodAction() | ||
| spec.action.name = vim.VirtualMachine.RemoveAllSnapshots_Task | ||
| print(spec.action.argument) | ||
| spec.enabled = True | ||
| if si.content.scheduledTaskManager.CreateScheduledTask(vm, spec) is not None: | ||
| print('Scheduled Task Successfully') | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| from datetime import datetime, timedelta | ||
| from tools import cli, service_instance | ||
| from pyVmomi import vim | ||
| from pyVim import connect | ||
|
|
||
|
|
||
| def get_snapshots_by_name_recursively(snapshots, snapname): | ||
| snap_obj = [] | ||
| for snapshot in snapshots: | ||
| if snapshot.name == snapname: | ||
| snap_obj.append(snapshot) | ||
| else: | ||
| snap_obj = snap_obj + get_snapshots_by_name_recursively( | ||
| snapshot.childSnapshotList, snapname) | ||
| return snap_obj | ||
|
|
||
|
|
||
| def main(): | ||
| print("Trying to connect to VCENTER SERVER . . .") | ||
| parser = cli.Parser() | ||
| parser.add_required_arguments( | ||
| cli.Argument.MINUTES, cli.Argument.VM_NAME, cli.Argument.SNAPSHOT_NAME) | ||
| parser.add_optional_arguments(cli.Argument.POWER_ON) | ||
| args = parser.get_args() | ||
| try: | ||
| date_time = datetime.now() + timedelta(minutes=int(args.minutes)) | ||
| except ValueError: | ||
| print('Unrecognized date format') | ||
| return -1 | ||
|
|
||
| si = service_instance.connect(args) | ||
|
|
||
| print("Connected to VCENTER SERVER !") | ||
|
|
||
| view = si.content.viewManager.CreateContainerView(si.content.rootFolder, | ||
| [vim.VirtualMachine], | ||
| True) | ||
| vms = [vm for vm in view.view if vm.name == args.vm_name] | ||
|
|
||
| if not vms: | ||
| print('VM not found') | ||
| connect.Disconnect(si) | ||
| return -1 | ||
| vm = vms[0] | ||
|
|
||
| snap_obj = get_snapshots_by_name_recursively( | ||
| vm.snapshot.rootSnapshotList, args.snapshot_name) | ||
| print("type") | ||
| print(type(snap_obj[0].snapshot)) | ||
| print(snap_obj) | ||
| if len(snap_obj) == 1: | ||
| snap_obj = snap_obj[0].snapshot | ||
| spec = vim.scheduler.ScheduledTaskSpec() | ||
| spec.name = args.snapshot_name + args.vm_name + "remove" | ||
| spec.action = vim.action.MethodAction() | ||
| spec.scheduler = vim.scheduler.OnceTaskScheduler() | ||
| spec.scheduler.runAt = date_time | ||
| spec.action.name = vim.vm.Snapshot.RemoveSnapshot_Task | ||
|
|
||
| else: | ||
| print("No snapshots found with name: %s on VM: %s" % ( | ||
| args.snapshot_name, vm.name)) | ||
|
|
||
| spec.action.argument = [vim.MethodActionArgument()] * 1 | ||
| spec.action.argument[0] = vim.MethodActionArgument() | ||
| spec.action.argument[0].value = False | ||
| spec.enabled = True | ||
| if si.content.scheduledTaskManager.CreateObjectScheduledTask(snap_obj, spec) is not None: | ||
| print('Scheduled Task Successfully') | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run both the samples in python 2 and 3