using System;
using System.Collections;
using ClientUtils;
using VimApi;
namespace VMotion {
///
/// Virtual Machine Power Ops implementation.
///
public class VMotion : ClientBase {
public const int EXTRA_ARGS = 4;
public const int ARG_FIRSTORNAMED_VM = ClientInfo.ARG_LAST_BASE + 1;
public const int ARG_VMNAME = ClientInfo.ARG_LAST_BASE + 2;
public const int ARG_POWEROPNAME = ClientInfo.ARG_LAST_BASE + 3;
public const int ARG_POWERONHOST = ClientInfo.ARG_LAST_BASE + 4;
public VMotion(string name, string[] args) :
base(name, EXTRA_ARGS, " ", args) {
}
private bool UseFirstVm {
get {
String whichVm = clientInfo.getArg(ARG_FIRSTORNAMED_VM);
bool useFirstVm = true;
if (whichVm == null || whichVm.Length == 0) {
useFirstVm = true;
} else {
useFirstVm = "any".Equals(whichVm.ToLower());
}
return useFirstVm;
}
}
private string VmName {
get {
return clientInfo.getArg(ARG_VMNAME);
}
}
private string PowerOpName {
get {
return clientInfo.getArg(ARG_POWEROPNAME);
}
}
private string PowerOnHost {
get {
return clientInfo.getArg(ARG_POWERONHOST);
}
}
public void DoPowerOps() {
ArrayList morlist = null;
ManagedObjectReference vmmor = null;
vmmor = clientInfo.SvcUtil.GetDecendentMoRef(null, "VirtualMachine", VmName);
String errmsg = "";
if (vmmor == null)
{
errmsg = "Unable to find VirtualMachine named : " + VmName + " in Inventory";
}
try {
//TODO: find required host.
string powerOnHostName = PowerOnHost;
ManagedObjectReference hostmor = null;
hostmor = clientInfo.SvcUtil.GetDecendentMoRef(null, "HostSystem", powerOnHostName);
bool nonTaskOp = false;
ManagedObjectReference taskmor = null;
// taskmor = clientInfo.Connection.Service.PowerOnVM_Task(vmmor, hostmor);
taskmor = clientInfo.Connection.Service.MigrateVM_Task(vmmor, null, hostmor, VirtualMachineMovePriority.highPriority);
// If we get a valid task reference, monitor the task for success or failure
// and report task completion or failure.
if (taskmor != null) {
log.LogLine("Got Valid Task Reference");
object[] result =
clientInfo.SvcUtil.WaitForValues(
taskmor, new string[] { "info.state", "info.error" },
new string[] { "state" }, // info has a property - state for state of the task
new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
);
// Wait till the task completes.
if (result[0].Equals(TaskInfoState.success)) {
log.LogLine(clientInfo.AppName + " : Successful " + PowerOpName + " for VM : " + VmName);
} else {
log.LogLine(clientInfo.AppName + " : Failed " + PowerOpName + " for VM : " + VmName);
}
} else if (nonTaskOp) {
log.LogLine(clientInfo.AppName + " : Successful " + PowerOpName + " for VM : " + VmName);
}
} catch (Exception e) {
clientInfo.Util.LogException(e);
log.LogLine(clientInfo.AppName + " : Failed " + PowerOpName + " for VM : " + VmName);
throw e;
}
}
///
/// The main entry point for the application.
///
[STAThread]
public static void Main(string[] args) {
VMotion vmpo = new VMotion("VmPowerOps", args);
if (!vmpo.clientInfo.HasMinimumArgs) {
return;
}
RunDelegate rd = new RunDelegate(vmpo.DoPowerOps);
vmpo.Run(rd);
}
}
}