private static void WaitForDeploymentStatus( DeploymentStatus deploymentStatus, InstanceStatus instanceStatus, string subscriptionId, string serviceName, DeploymentSlot deploymentSlot, X509Certificate2 managementCertificate, bool mostIsGoodEnough) { // assume we haven't matched the desired status bool statusMatches = false; do { // sleep for a bit... Thread.Sleep(TimeSpan.FromSeconds(5)); WriteStatus("."); // get the current status FullDeploymentStatus current = Utilities.GetDeploymentSlotStatus( subscriptionId, serviceName, deploymentSlot, managementCertificate); // if the main status matches if (current.MainStatus == deploymentStatus) { // good so far... statusMatches = true; int countMatch = 0; // see if all instance status's also match foreach (InstanceDetails instance in current.Instances) { if (instance.Status != instanceStatus) { // we have a bad apple statusMatches = false; } else { countMatch++; } } if (mostIsGoodEnough && ((double)countMatch / current.Instances.Count) >= 0.8) { statusMatches = true; } } }while (!statusMatches); WriteStatusLine(string.Format(" {0}", deploymentStatus)); }
public static FullDeploymentStatus GetDeploymentSlotStatus( string subscriptionId, string serviceName, DeploymentSlot deploymentSlot, X509Certificate2 managementCertificate) { // Build uri string // format:https://management.core.windows.net/<subscription-id>/services/hostedservices // /<service-name>/deploymentslots/<deployment-name/ var url = string.Format("{0}{1}/services/hostedservices/{2}/deploymentslots/{3}", Constants.AzureManagementUrlBase, subscriptionId, serviceName, deploymentSlot); // make uri request using created uri string HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // make header, method, and certificated requests request.Method = "GET"; request.ClientCertificates.Add(managementCertificate); request.Headers.Add(Constants.VersionHeader, Constants.VersionTarget); request.ContentType = Constants.ContentTypeXml; // Get the response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // put response into string text using (StreamReader dataStream = new StreamReader(response.GetResponseStream())) { string text = dataStream.ReadToEnd(); // create an xml document XmlDocument xml = new XmlDocument(); // load up the response text as xml xml.LoadXml(text); // get the NS manager XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable); ns.AddNamespace("az", Constants.AzureXmlNamespace); // return the status DeploymentStatus currentStatus; var statusText = xml.SelectSingleNode("//az:Status", ns).InnerText; if (Enum.TryParse <DeploymentStatus>(statusText, true, out currentStatus)) { FullDeploymentStatus fullStatus = new FullDeploymentStatus { MainStatus = currentStatus }; // now try to get the status values for each instance XmlNodeList instances = xml.SelectNodes("//az:RoleInstance", ns); foreach (XmlNode instance in instances) { var instanceStatus = new InstanceDetails { RoleName = instance.SelectSingleNode("az:RoleName", ns).InnerText, InstanceName = instance.SelectSingleNode("az:InstanceName", ns).InnerText, Status = (InstanceStatus)Enum.Parse(typeof(InstanceStatus), instance.SelectSingleNode("az:InstanceStatus", ns).InnerText) }; fullStatus.Instances.Add(instanceStatus); } return(fullStatus); } else { throw new ArgumentOutOfRangeException("Status", "The status returned for the deployment is outside the range of acceptable values"); } } }