/// <summary> /// Stores deployment properties if returned by SSH command /// Used to populate VibesCmSwapView and EcSwapView /// </summary> /// <param name="cmChanged">The CM which was queried</param> /// <param name="deploymentProperties">The deployment properties file fetched</param> internal void StoreDeploymentProperties(VibesCm cmChanged, string deploymentProperties) { using (DataContext context = new DataContext()) { VibesCm cmToUpdate = context.HostCms.SingleOrDefault(c => c.Id == cmChanged.Id); XDocument xProperties = XDocument.Parse(deploymentProperties); // Loop through deployment properties and only fetch desired properties foreach (XElement node in xProperties.Descendants("parameter")) { // Property types to search for if ( node.Value.ToLower().Contains("http:") || node.FirstAttribute.Value.Contains("URL_TO") || node.FirstAttribute.Value == "useVibes" || node.FirstAttribute.Value.Contains("smart_suite_server_name") || node.FirstAttribute.Value.Contains("smart_suite_username") || node.FirstAttribute.Value.Contains("smart_suite_password") ) { string propertyKey = node.Attribute("name").Value; string propertyVal = node.Value; // Property Exists, update if (context.DeploymentProperties.Any(p => p.CmId == cmChanged.Id && p.PropertyKey == propertyKey)) { DeploymentProperty propToUpdate = context.DeploymentProperties.SingleOrDefault(p => p.CmId == cmChanged.Id && p.PropertyKey == propertyKey); propToUpdate.PropertyValue = propertyVal; context.Update(propToUpdate); continue; } else { // Property does not exist, add context.DeploymentProperties.Add(new DeploymentProperty { PropertyKey = propertyKey, PropertyValue = propertyVal, Cm = context.HostCms.Single(c => c.Id == cmChanged.Id), CmId = context.HostCms.Single(c => c.Id == cmChanged.Id).Id }); } } } context.SaveChanges(); } }
/// <summary> /// Autosave CM changes when PropertyChanged fires, used to save objects on keypress /// </summary> /// <param name="sender">The object changed</param> /// <param name="args">Propertychanged args</param> internal void PersistTargetChanges(object sender, PropertyChangedEventArgs args) { // Stop execution if sender is VM if (sender is SetupVm || sender is VibesCmSwapVm || sender is EcSwapVm) { return; } try { // Host on setup page if (sender is VibesHost newHostValues) { using (DataContext context = new DataContext()) { VibesHost hostToUpdate = context.EnvironmentHosts.Single(h => h.Id == newHostValues.Id); hostToUpdate.Name = newHostValues.Name ?? hostToUpdate.Name; hostToUpdate.Url = newHostValues.Url ?? hostToUpdate.Url; hostToUpdate.SshUsername = newHostValues.SshUsername ?? hostToUpdate.SshUsername; hostToUpdate.SshPassword = newHostValues.SshPassword ?? hostToUpdate.SshPassword; hostToUpdate.HostType = newHostValues.HostType; hostToUpdate.IndClustered = newHostValues.IndClustered; context.SaveChanges(); } } // CM on setup page else if (sender is VibesCm newCmValues) { using (DataContext context = new DataContext()) { VibesCm cmToUpdate = context.HostCms.Single(c => c.Id == newCmValues.Id); cmToUpdate.CmResourceName = newCmValues.CmResourceName ?? cmToUpdate.CmResourceName; cmToUpdate.CmPort = newCmValues.CmPort ?? cmToUpdate.CmPort; cmToUpdate.CmPath = newCmValues.CmPath ?? cmToUpdate.CmPath; cmToUpdate.CmCorePath = newCmValues.CmCorePath ?? cmToUpdate.CmCorePath; cmToUpdate.CmType = newCmValues.CmType; context.SaveChanges(); } } // CM deployment properties on swap page else if (sender is DeploymentProperty property) { Log.Information($"PropertyChanged Called, sender is {property.PropertyKey} and CM is {property.Cm.CmResourceName}"); using (DataContext context = new DataContext()) { if (context.HostCms.Any(p => p.Id == property.CmId)) { DeploymentProperty propToUpdate = context.DeploymentProperties.SingleOrDefault(p => p.Id == property.Id); propToUpdate.SearchPattern = property.SearchPattern; propToUpdate.ReplacePattern = property.ReplacePattern; context.SaveChanges(); } } } else { throw new ArgumentException("Unknown type supplied"); } } catch (Exception ex) { Log.Error($"Unable to persist target changes on {sender.GetType()}, Error: {ex.Message}"); Log.Error($"Stack Trace: {ex.StackTrace}"); } }