-
-
Save anonymous/5777983 to your computer and use it in GitHub Desktop.
| #!/usr/bin/perl -w | |
| # William Lam | |
| # http://www.virtuallyghetto.com/ | |
| use strict; | |
| use warnings; | |
| use VMware::VIRuntime; | |
| use VMware::VILib; | |
| my %opts = ( | |
| vmname => { | |
| type => "=s", | |
| help => "Name of VM", | |
| required => 1, | |
| }, | |
| ); | |
| Opts::add_options(%opts); | |
| Opts::parse(); | |
| Opts::validate(); | |
| Util::connect(); | |
| my $vmname = Opts::get_option('vmname'); | |
| # searches for VM object based on vmname input | |
| my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', properties => ['name'], filter => {'name' => $vmname}); | |
| if(defined($vm)) { | |
| # performs simple async task, save the Task Ref | |
| my $taskRef = $vm->PowerOnVM_Task(); | |
| # get Task Ref view | |
| my $taskView = Vim::get_view(mo_ref => $taskRef); | |
| # get Task MoRef ID | |
| my $taskMoRefId = $taskView->{'mo_ref'}->value; | |
| # print out MoRef ID + state of task | |
| print "MoRefID: " . $taskMoRefId . "\t " . "State:" . $taskView->info->state->val . "\n"; | |
| # adding a dummer timer, you could do something else and then check back later using MoRef ID | |
| sleep 10; | |
| # build a new MO using the Task MoRef ID | |
| my $newTaskRef = ManagedObjectReference->new(type => "Task", value => $taskMoRefId); | |
| # get Task Ref view | |
| my $newTaskView = Vim::get_view(mo_ref => $newTaskRef); | |
| # get Task MoRef ID from new object to ensure it's the same one | |
| my $newTaskMoRefId = $taskView->{'mo_ref'}->value; | |
| # print Task MoRef ID + state and you should see it change | |
| print "MoRefID: " . $newTaskMoRefId . "\t " . "State:" . $newTaskView->info->state->val . "\n"; | |
| # power off VM | |
| $vm->PowerOffVM_Task(); | |
| } else { | |
| print "Unable to find $vmname\n"; | |
| } | |
| Util::disconnect(); | |
| ### SAMPLE OUTPUT ### | |
| lamw:~/vmware » ./taskExample.pl --server mini --username root --vmname VM1 | |
| MoRefID: haTask-20-vim.VirtualMachine.powerOn-506061389 State:running | |
| MoRefID: haTask-20-vim.VirtualMachine.powerOn-506061389 State:success |
My current situation. All I have is a string containing the key from the TaskInfo, i.e., someone is hitting our API at /task/
and I want to return the TaskInfo object for that task
Are you sure the Task object still exists in the system? If you take a look at the error message, it says it may not longer be valid/exists which sounds like you're trying to access a Task object that's no longer in the system.
It definitely does because if I do the following where $fqdn is given a specific hostname
my $vm_view = Vim::find_entity_view (
view_type => "VirtualMachine",
properties => [ 'name' ],
filter => { 'name' => "$fqdn" } );
die "Failed to get virtual machine '$fqdn'!" unless $vm_view;
my $TaskManager = Vim::get_view( mo_ref => Vim::get_service_content()->taskManager )
|| die "Unable to initialize TaskManager!";
my $TaskFilterSpecByEntity = TaskFilterSpecByEntity->new(
entity => $vm_view,
recursion => TaskFilterSpecRecursionOption->new( "self" ),
);
my $TaskFilterSpec = TaskFilterSpec->new(
entity => $TaskFilterSpecByEntity );
my $TaskHistoryCollector_mor = $TaskManager->CreateCollectorForTasks( filter => $TaskFilterSpec );
my $TaskHistoryCollector = Vim::get_view ( mo_ref => $TaskHistoryCollector_mor )
|| die "Unable to get TaskHistoryCollector view!";
$TaskHistoryCollector->RewindCollector();
my $tasks = $TaskHistoryCollector->ReadNextTasks( maxCount => 10 );
And I get back
/* chop */
[
{
"cancelable": "0",
"cancelled": "0",
"completeTime": "2013-04-01T21:50:15.018Z",
"descriptionId": "Drm.ExecuteVMotionLRO",
"entity": {
"type": "VirtualMachine",
"value": "vm-22805"
},
"entityName": "myhostnamehere",
"eventChainId": "75089172",
"key": "task-1173277",
"queueTime": "2013-04-01T21:49:05.251Z",
"reason": {},
"startTime": "2013-04-01T21:49:05.397Z",
"state": {
"val": "success"
},
"task": {
"type": "Task",
"value": "task-1173277"
}
}
]So you're saying that the task_id you provided earlier is showing up in the $tasks array ref?
So at the very least it exists somewhere and I want to get it from that magical place when they give me that ID
Right, so using the
Vim::get_view( mo_ref => ManagedObjectReference->new(type => 'Task', value => 'task-1173277'));I get that error message. But as you can see in that output, it's somewhere
The only thing I can think of is that TaskHistoryCollector returns completed tasks and get_view will not
Well TaskHistoryCollector can return whatever you want, it's all based on your spec definition as defined by the API docs here http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.TaskFilterSpec.html
So any ideas why get_view doesn't return it but TaskHistoryCollector does?
The tl;dr is I have a task key and need to get the TaskInfo for it. It looks like the get_view approach is the correct way but it's not working so if I could either A) get it working or B) Figure out why it's not returning the task I would be stoked
FYI - It does work for me. I don't have a chatty system right now, but I just did a self-vMotion which looks like the type of task you had originally. Then looked at the latestTask property and used "task-102" and it worked for me.
http://www.youtube.com/watch?v=krD4hdGvGHM I initiated two new tasks (powerOff and powerOn) and after they finished I hit /task/task-(task-id) (Endpoint that runs the get_view code) and both worked. However the older tasks in the list of tasks for that machine (Tasks returned from TaskCollector) did not work. I wonder if there's some TTL on get_view or something odd. Either way, at least now I can handle the Fault and and know the code is right.
Workflow:
Initiate task on Machine A
/task/task-(new task id)
Hooray, get stuff
Initiate new task on Machine A
/task/task-(new task id)
Hooray, get stuff
/task/task-(old task id)
Boooo, task is gone, despite still showing up in the list of tasks for that machine with full details and with the same task id as it originally had. Nothing changed about the task as far as I can tell except that get_view won't return it
However,
Initiate task on Machine A
/task/task-id get stuff
Initiate Task on Machine B
/task/task-id get stuff
/task/task-old-task-id get stuff
tl;dr I think get_view can only return the most recent task (FOR A SINGLE MACHINE). That seems.... very, very wrong and/or broken.
EDIT: Gah! This doesn't seem to be the case either.
EDIT2: Seems to be time related, it stopped being returned from get_view around 5-7 minutes
OR
OR
None of these work