pyvmomi-community-samples icon indicating copy to clipboard operation
pyvmomi-community-samples copied to clipboard

User is not getting consistent result when running the script

Open solohan83 opened this issue 5 years ago • 2 comments

Hi,

I wrote a simple script using pyvmomi verion 7.0.1, to schedule VM snapshots:

while not vm:
    try:

        si = connect.SmartConnectNoSSL(host=list(vCenter_dict.values())[dict_position],
                                user=vCenterUser,
                                pwd=vCenterPassword,
                                port=443)
        atexit.register(connect.Disconnect, si)
    except IOError as ex:
        pass
    if not si:
        raise SystemExit("Unable to connect to host with supplied info.")

    vm = si.content.searchIndex.FindByIp(None, vmIP, True)



if int(sys.argv[2]) == 4:
    #mode 4, schedule a snapshot task in the future
    snapshotName = sys.argv[4]
    snapshotOwner = sys.argv[5]
    scheduleTime = sys.argv[6]
    userEmail = sys.argv[7]
    #first, delete any previously created run once tasks
    RetrieveEntityScheduledTask=si.content.scheduledTaskManager.RetrieveEntityScheduledTask(vm)
    #print("Retrieve VM Scheduled Tasks :",RetrieveEntityScheduledTask)
    for vmScheduledTask in RetrieveEntityScheduledTask:
        #print("Retrive vm Scheduled Tasks: ",vmScheduledTask)
        #get schedule task name
        #taskName = vmScheduledTask.info.name
        #print("RunOnce Task Name: ",taskName)

        OnceTaskScheduler = vmScheduledTask.info.scheduler
        #print(OnceTaskScheduler)
        if isinstance(OnceTaskScheduler, vim.scheduler.OnceTaskScheduler):
            vmScheduledTask.RemoveScheduledTask()

    #now schedule the new snapshot task
    dt = datetime.strptime(scheduleTime, '%Y-%m-%d %H:%M:%S %z')
    spec = vim.scheduler.ScheduledTaskSpec()
    spec.name = snapshotName
    spec.description = snapshotOwner
    spec.notification = userEmail
    spec.scheduler = vim.scheduler.OnceTaskScheduler()
    spec.scheduler.runAt = dt
    spec.action = vim.action.MethodAction()
    spec.action.name = vim.VirtualMachine.CreateSnapshot
    spec.action.argument = [vim.MethodActionArgument()] * 4
    spec.action.argument[0] = vim.MethodActionArgument()
    spec.action.argument[0].value = snapshotName
    spec.action.argument[1] = vim.MethodActionArgument()
    spec.action.argument[1].value = snapshotOwner
    spec.action.argument[2] = vim.MethodActionArgument()
    spec.action.argument[2].value = False
    spec.action.argument[3] = vim.MethodActionArgument()
    spec.action.argument[3].value = False
    spec.enabled = True

    task = si.content.scheduledTaskManager.CreateScheduledTask(vm, spec)
    snapshot = task.info.result
    print(snapshot)

It always comes back successful no error. However, half of the time, if I look at vCenter, there is no snapshot scheduled. And some times it works.

I could manually create/schedule the snapshots in vCenter, or through powercli.

The bizzaire part is, if another user login within different credential, and try it on the same VM, it may work.

The user may take 25 to 60 VM snapshots, only around half could go through.

I am not sure where it is wrong, could it be the number of API connections?

solohan83 avatar Mar 25 '21 21:03 solohan83

Hi @NikitaIT ! Sorry for the delay here, but I'm prepping this code to coordinate for v5 docs and in fact we are removing this entire section from docs, so we aren't maintaining code for it! When I deleted the main view transitions branch here, it automatically closed your PR.

I appreciate you taking the time to fix this, and I'm really sorry we didn't get around to maintaining it better as we went along before we completely reorganized and removed this content! 😅 Thanks so much for the PR, and please keep holding us accountable!

sarah11918 avatar Nov 28 '24 12:11 sarah11918