private void refreshVClient(cancellableDateTime deadline) { // If we can't ping the box, assume we can't connect to the API either. We do this since I can't work out how to // set connection timeouts for the VMWare api (is there a way?). // We ping a few times, though, to allow for any packet loss going on. int pingRetries = 5; while (true) { Icmp pinger = new Icmp(_spec.kernelVMServer); if (pinger.Ping(TimeSpan.FromSeconds(3))) { // Success, so continue. break; } else { // No response. If we have retries left, use one, or throw if not. if (pingRetries == 0) { throw new WebException("Can't ping ESXi before trying to access web API"); } pingRetries--; deadline.doCancellableSleep(TimeSpan.FromSeconds(4)); } } // We can ping fine, so connect using HTTP. while (true) { DateTime connectionDeadline = DateTime.Now + TimeSpan.FromMinutes(5); try { VClient = new VimClientImpl(); VClient.Connect("https://" + _spec.kernelVMServer + "/sdk"); VClient.Login(_spec.kernelVMServerUsername, _spec.kernelVMServerPassword); break; } catch (Exception) { if (DateTime.Now > connectionDeadline) { throw; } } } }
public void waitForPingability(bool waitForState, cancellableDateTime deadline = null) { if (deadline == null) { deadline = new cancellableDateTime(); } // Wait for the box to go down/come up. Debug.Print("Waiting for box " + getBaseConnectionSpec().kernelDebugIPOrHostname + " to " + (waitForState ? "come up" : "go down")); while (true) { deadline.throwIfTimedOutOrCancelled(); if (waitForState) { Icmp pinger = new Icmp(Dns.GetHostAddresses(getBaseConnectionSpec().kernelDebugIPOrHostname).First()); if (pinger.Ping(TimeSpan.FromMilliseconds(500))) { Debug.Print(".. Box " + getBaseConnectionSpec().kernelDebugIPOrHostname + " pingable, giving it a few more seconds.."); deadline.doCancellableSleep(TimeSpan.FromSeconds(10)); Debug.Print(".. Box " + getBaseConnectionSpec().kernelDebugIPOrHostname + " assumed to be up now."); break; } } else { if (getPowerStatus() == false) { break; } } deadline.doCancellableSleep(TimeSpan.FromSeconds(5)); } Debug.Print(".. wait complete for box " + getBaseConnectionSpec().kernelDebugIPOrHostname); }