public void Load() { if (File.Exists(SettingsPath)) { try { var json = File.ReadAllText(SettingsPath); Settings = JsonConvert.DeserializeObject<Settings>(json); if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() != Settings.Version) { Settings = new Settings(); } else { Settings = new Settings(); } } catch (Exception e) { Settings = new Settings(); _loggerService.Log(e.Message); MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Settings = new Settings(); } Settings.PropertyChanged -= SettingsOnPropertyChanged; Settings.PropertyChanged += SettingsOnPropertyChanged; }
public static void UnleashChaos(Settings settings, ChaosLogger logger) { var ec2Factory = new Ec2Factory(settings.AwsAccessKey, settings.AwsSecretKey, settings.ServiceUrl, logger); var random = new Random(); if (settings.Repeat == 0) { settings.Repeat = 1; } if (settings.Repeat > 1) { logger.Log(string.Format("Repeating {0} times", settings.Repeat)); } for (var t = 0; t < settings.Repeat; t++) { logger.Log(string.Format("Looking for instances in {0} with tag {1}={2}", settings.ServiceUrl, settings.Tagkey, settings.TagValue)); List<Reservation> instances; try { instances = ec2Factory.ListInstancesByTag(settings.Tagkey, settings.TagValue); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); return; } if (instances.Count == 0) { logger.Log("No instances found"); } else { logger.Log(string.Format("Found {0} candidate instance(s) for chaos!", instances.Count)); var victimIndex = random.Next(0, instances.Count); var instanceId = instances[victimIndex].RunningInstance[0].InstanceId; logger.Log(string.Format("Randomly chosen instance {0} ({1}) as the chaos victim.", instanceId, instances[victimIndex].RunningInstance[0].PublicDnsName)); logger.Log(string.Format("Terminating {0}...", instanceId)); ec2Factory.TerminateInstance(instanceId); logger.Log(string.Format("{0} terminated", instanceId)); } if (settings.Delay <= 0) continue; logger.Log(string.Format("Waiting {0} ms", settings.Delay)); System.Threading.Thread.Sleep(settings.Delay); } }
public static void SaveSettings(string filename, Settings settings, ChaosLogger logger) { var serializer = new XmlSerializer(typeof(Settings)); try { TextWriter writer = new StreamWriter(filename); serializer.Serialize(writer, settings); writer.Close(); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); throw new Exception("Cannot save settings"); } }
public static void UnleashChaos(Settings settings, ChaosLogger logger) { if (settings.Repeat == 0) { settings.Repeat = 1; } if (settings.Repeat > 1) { logger.Log(string.Format("Repeating {0} times", settings.Repeat)); } MonkeyKeeper keeper = new MonkeyKeeper(settings, logger, new HardCodedMonkeyListBuilder()); for (var times = 0; times < settings.Repeat; times++) { keeper.UnleashRandomMonkey(); if (settings.Delay <= 0) return; logger.Log(string.Format("Waiting {0} ms", settings.Delay)); System.Threading.Thread.Sleep(settings.Delay); } }
/// <summary> /// Constructor /// </summary> public TrayAppContext() { _settings = new Domain.Settings(); _config = new Config(_settings); _controller = new Controller(_settings); // Construct a dynamic-ish context menu structure CreateContextMenuEntries(); try { _config.Load(); if (_settings.IsCheckUpdates()) { CheckUpdates(); } _controller.Initialize(); } catch (Exception e) { // Config has invalid options TooltipMsg(e.Message, "error"); } }
public static bool ValidateSettings(Settings settings, ChaosLogger logger) { var settingsValid = true; if (string.IsNullOrEmpty(settings.ServiceUrl) && !string.IsNullOrEmpty(settings.Ec2Endpoint)) { settings.ServiceUrl = ServiceUrlFromEndPointName(settings.Ec2Endpoint); if (string.IsNullOrEmpty(settings.ServiceUrl)) { logger.Log("ERROR: Cannot find service url for endpoint '" + settings.Ec2Endpoint + "'"); settingsValid = false; } } if (string.IsNullOrEmpty(settings.ServiceUrl)) { logger.Log("ERROR: No service URL found"); settingsValid = false; } if (string.IsNullOrEmpty(settings.Tagkey)) { logger.Log("ERROR: Tag key needed"); settingsValid = false; } if (string.IsNullOrEmpty(settings.TagValue)) { logger.Log("ERROR: Tag value needed"); settingsValid = false; } return settingsValid; }
static void Main(string[] args) { var settings = new Settings(); var showHelp = false; var saveSettingsFile = string.Empty; var acceptDisclaimer = false; var loadSettingsFile = string.Empty; var options = new OptionSet() { { "a=|awsaccesskey=", "Access key of AWS IAM user that can list and terminate instances", x => settings.AwsAccessKey = x }, { "d=|delay=", "Delay (milliseconds) before chaos is unleashed again (if repeat option set)", (int x) => settings.Delay = x }, { "D|acceptdisclaimer", "Chaos Monkey is designed to break stuff, setting this option means that you acknowledge this", x => acceptDisclaimer = x != null }, { "e=|endpoint=", "AWS endpoint name (US-East, US-West, EU, Asia-Pacific-Singapore, Asia-Pacific-Japan)", x => settings.Ec2Endpoint = x }, { "h|?|help", "Show help (this screen)", x => showHelp = x != null }, { "i=|loadsettings=", "Load settings xml file", x => loadSettingsFile = x }, { "l=|log=", "Save log to file", x => settings.LogFileName = x }, { "o=|savesettings=", "Save settings to xml file", x => saveSettingsFile = x }, { "r=|repeat=", "Number of times chaos is unleashed (default 1)", (int x) => settings.Repeat = x }, { "s=|awssecretkey=", "Access key of AWS IAM user that can list and terminate instances", x => settings.AwsSecretKey = x }, { "S=|serviceurl=", "URL of EC2 service endpoint (use e|endpoint to use defaults)", x => settings.ServiceUrl = x }, { "t=|tagkey=", "Key of Tag that will be search for in instances e.g. if EC2 tag is chaos=1, ChaosMonkey TagKey=chaos", x => settings.Tagkey = x }, { "v=|tagvalue=", "Value of Tag that will be search for in instances e.g. if EC2 tag is chaos=1, ChaosMonkey TagValue=1", x => settings.TagValue = x } }; options.Parse(args); var logger = new ChaosLogger(settings.LogFileName); try { if (!string.IsNullOrEmpty(loadSettingsFile)) { try { Tasks.LoadSettings(loadSettingsFile, logger); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); return; } } if (!acceptDisclaimer) { Console.WriteLine("WARNING!!! ChaosMonkey is going to break stuff. Press 'D' to indemnify the ChaosMonkey or its authors/contributors to your actions"); var key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar != 'D') { logger.Log("Disclaimer not accepted, exiting"); return; } logger.Log("Disclaimer accepted."); } else { logger.Log("Disclaimer accepted via startup parameters."); } if (!Tasks.ValidateSettings(settings, logger)) { logger.Log("Invalid settings. use '-?' for help on parameters. Exiting."); return; } Tasks.UnleashChaos(settings, logger); if (!string.IsNullOrEmpty(saveSettingsFile)) { logger.Log(string.Format("Saving settings to {0}", saveSettingsFile)); try { Tasks.SaveSettings(saveSettingsFile, settings, logger); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); } } } finally { logger.Close(); if (showHelp) { options.WriteOptionDescriptions(Console.Out); } } }
private static void SaveSettingsFile(string saveSettingsFile, Settings settings, ChaosLogger logger) { if (!string.IsNullOrEmpty(saveSettingsFile)) { logger.Log(string.Format("Saving settings to {0}", saveSettingsFile)); try { Tasks.SaveSettings(saveSettingsFile, settings, logger); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); } } }