private void ProcessUserDataTask(object ignored) { CtxTrace.TraceInformation(); // Check sanity of configuration if (userDataUrl.Contains(DhcpServerPlaceholder)) { List <IPAddress> dhcpServers = NetworkUtilities.GetDhcpServers(); if (dhcpServers.Count == 0) { CtxTrace.TraceError("Configured to get UserData from DHCP server, but no DHCP server found"); stateService.InitialisationComplete = true; } } // UserData may be a little slow to be delivered while (!stateService.InitialisationComplete && !stopSignalled) { string userData = GetUserData(); if (!string.IsNullOrEmpty(userData)) { // May be multiple root elements, so wrap for Xml parser string wrappedUserData = string.Format("<dummyRoot>{0}</dummyRoot>", userData); try { XDocument doc = XDocument.Parse(wrappedUserData); XElement script = doc.XPathSelectElement("//script"); if (script != null) { // Script may initiate a reboot, so mark the processing done once userData read. stateService.InitialisationComplete = true; ExecuteScript(script.Value); } script = doc.XPathSelectElement("//powershell"); if (script != null) { // Script may initiate a reboot, so mark the processing done once userData read. stateService.InitialisationComplete = true; ExecutePowerShellScript(script.Value); } } catch (Exception ex) { CtxTrace.TraceError(ex); } break; } // Sleep a while but ensure timely response to task cancel DateTime waitUntil = DateTime.Now + TimeSpan.FromSeconds(5); while (!stopSignalled && (DateTime.Now < waitUntil)) { Thread.Sleep(TimeSpan.FromMilliseconds(1000)); } } CtxTrace.TraceInformation("Task exit"); }
private string GetUserData() { if (userDataUrl.Contains(DhcpServerPlaceholder)) { List <IPAddress> dhcpServers = NetworkUtilities.GetDhcpServers(); foreach (IPAddress address in dhcpServers) { string userData = NetworkUtilities.HttpGet(userDataUrl.Replace(DhcpServerPlaceholder, address.ToString())); if (userData != null) { return(userData); } } return(null); } else { return(NetworkUtilities.HttpGet(userDataUrl)); } }