private (ComputeType type, object dep) DetectVMType(ARMSubscription arm, RDFESubscription rdfe) { var armDep = AnalyzeARMDeployment(arm); if (armDep == null) { var paasDep = AnalyseRDFEDeployment(rdfe); if (paasDep == null) { return(ComputeType.Unknown, null); } else { return(ComputeType.PaaS, paasDep); } } else { switch (armDep.Type) { case Constants.AnalyzerARMDeploymentIaaSType: return(ComputeType.IaaS, armDep); case Constants.AnalyzerARMDeploymentVMSSType: return(ComputeType.VMSS, armDep); default: return(ComputeType.Unknown, null); } } }
private (ARMSubscription arm, RDFESubscription rdfe) CallARMAndRDFE(bool checkARM = true, bool checkRDFE = true) { // TODO analyse ARM and REDFE in parallel ARMSubscription arm = checkARM ? AnalyzeARMSubscription(SubscriptionId, this.ResourceGroupName) : null; RDFESubscription rdfe = checkRDFE ? AnalyzeRDFESubscription(SubscriptionId) : null; return(arm, rdfe); }
private RDFEDeployment AnalyseRDFEDeployment(RDFESubscription rdfe) { if (rdfe == null) { return(null); } try { string VMName = TryConvertInstanceNameToVMNamePaaS(this.VMName); List <RDFEDeployment> rdfeDeps = new List <RDFEDeployment>(); rdfeDeps = rdfe.deployments.Where(x => x.HostedServiceName.ToLowerInvariant() == this.ResourceGroupName.ToLowerInvariant() || this.ResourceGroupName.ToLowerInvariant().Contains(x.Id.ToLowerInvariant())).ToList(); if (rdfeDeps.Count > 1) { var tmp = rdfeDeps.Where(x => x.HostedServiceName.ToLowerInvariant() == this.ResourceGroupName.ToLowerInvariant() || this.ResourceGroupName.ToLowerInvariant().Contains(x.Id.ToLowerInvariant())).ToList(); if (tmp.Count > 0) { rdfeDeps = tmp; } } if (rdfeDeps.Count == 1) { return(rdfeDeps.First()); } else if (rdfeDeps.Count > 1) { // Best effort guess var paasVMName = VMName == this.VMName ? String.Format("{0}_IN_0", VMName) : this.VMName; var ret = rdfeDeps.Where(x => x.RoleInstances.Where( y => y.RoleInstanceName.ToLowerInvariant().Trim() == paasVMName.ToLowerInvariant().Trim() || y.ID.ToString().ToLowerInvariant() == VMName.ToLowerInvariant().Trim() ).ToList().Count >= 1).ToList(); if (ret.Count() > 0) { return(ret.First()); } else { return(rdfeDeps.First()); } } else { return(null); } } catch // Unknown, should check Kusto { return(null); } }
private RDFESubscription AnalyzeRDFESubscriptionResult(string xml) { xml = xml.Replace("=== <", "<").Replace("> ===", ">"). Replace(":<", ": <").Replace("&", "&").Trim(); var duck = new Regex(@".?<.*>", RegexOptions.Compiled); foreach (var match in duck.Matches(xml)) { var subs = match.ToString(); if (!subs.Trim().StartsWith("<")) { subs = subs.Replace("<", "<").Replace(">", ">"); xml = xml.Replace(match.ToString(), subs); } } xml = xml.Replace("<", "\n<").Replace(">", ">\n").Trim(); xml = xml.Replace("xmlns:xsd", "xmlns_xsd").Replace("xmlns:xsi", "xmlns_xsi"); var xmlArray = xml.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); var comp = new Regex(@"^\s*\w*(Label|LastRefreshTime):.*", RegexOptions.Singleline | RegexOptions.Compiled); for (int i = 0; i < xmlArray.Length; ++i) { if (comp.IsMatch(xmlArray[i])) { var pieces = xmlArray[i].Split(new[] { ':' }, 2); pieces[1] = WebUtility.UrlEncode(pieces[1]); xmlArray[i] = String.Format("{0}:{1}", pieces[0], pieces[1]); } xmlArray[i] = xmlArray[i].Trim(); // If its one of the weird label, or xml like <M P=... /> we want to ignore them due to some weird characters. // an xml minimum size is 4 : <X/> if (xmlArray[i].Length > 4 && xmlArray[i][0] == '<' && xmlArray[i][2] == ' ' && xmlArray[i].Last() == '>') { xmlArray[i] = ""; } } xmlArray = xmlArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); xml = string.Join(Environment.NewLine, xmlArray); var serializer = new XmlSerializer(typeof(Json2Class.RDFESubscriptionWrapper.Subscription)); using (var stream = new StringReader(xml)) using (var reader = XmlReader.Create(stream)) { var result = (Json2Class.RDFESubscriptionWrapper.Subscription)serializer.Deserialize(reader); var multiDeployments = result.HostedService; if (multiDeployments == null) { Log.Warning("No RDFE HostedService deployment found."); return(null); } var rdfeSubscription = new RDFESubscription(); foreach (var element in multiDeployments) { if (element.Deployment == null) { continue; } var deployments = BuildDeployment(element); if (deployments != null) { rdfeSubscription.deployments = rdfeSubscription.deployments.Concat(deployments).ToList(); } } return(rdfeSubscription); } }