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); }
public void UnleashRandomMonkey() { BuildMonkeysListAndInitializeRandomSeedIfNotDoneAlready(); int nextRandomNumber = random.Next(_monkeys.Count); ParentMonkey monkey = _monkeys[nextRandomNumber]; _logger.Log(string.Format("Unleashing random Monkey named {0}", monkey)); monkey.Unleash(); }
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 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); } }
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); } } }
public static Settings LoadSettings(string filename, ChaosLogger logger) { var serializer = new XmlSerializer(typeof(Settings)); try { logger.Log(string.Format("Loading settings from file {0}", filename)); var fileStream = new FileStream(filename, FileMode.Open); var settings = (Settings)serializer.Deserialize(fileStream); return settings; } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); throw new Exception("Cannot load settings"); } }
public static Settings LoadSettings(string filename, ChaosLogger logger) { var serializer = new XmlSerializer(typeof(Settings)); try { logger.Log(string.Format("Loading settings from file {0}", filename)); var fileStream = new FileStream(filename, FileMode.Open); var settings = (Settings)serializer.Deserialize(fileStream); return(settings); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); throw new Exception("Cannot load settings"); } }
public override IList <ParentMonkey> GetMonkeys(Settings settings, ChaosLogger logger) { logger.Log("Hard coded MonkyeListProvider returning EC2Monkey"); return(new List <ParentMonkey>() { new EC2Monkey(settings, logger) }); }
static void Main(string[] args) { CommandLineParser parser = new CommandLineParser(args); CommandLineSettings commandLineSettings = parser.GetCommandLineSettings(); var logger = new ChaosLogger(commandLineSettings.Settings.LogFileName); try { LoadSettings(commandLineSettings.LoadSettingsFile, logger); if (IsDisclaimerAccepted(commandLineSettings.AcceptDisclaimer, logger)) { } else { logger.Log("Disclaimer not accepted, exiting"); return; } if (commandLineSettings.ApplicationMode.Equals("Pluggable")) { } else { if (!CommandLineValidator.ValidateSettings(commandLineSettings.Settings, logger)) { logger.Log("Invalid settings. use '-?' for help on parameters. Exiting."); return; } } Tasks.UnleashChaos(commandLineSettings.Settings, logger); SaveSettingsFile(commandLineSettings.SaveSettingsFile, commandLineSettings.Settings, logger); } finally { logger.Close(); if (commandLineSettings.ShowHelp) { parser.WriteOptionDescriptions(Console.Out); } } }
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); } }
private static bool IsDisclaimerAccepted(bool acceptDisclaimer, ChaosLogger logger) { if (!acceptDisclaimer) { if (IsDisclaimerAcceptedViaDialog(logger)) { logger.Log("Disclaimer accepted."); return true; } else { return false; } } else { logger.Log("Disclaimer accepted via startup parameters."); return true; } }
private static bool IsDisclaimerAccepted(bool acceptDisclaimer, ChaosLogger logger) { if (!acceptDisclaimer) { if (IsDisclaimerAcceptedViaDialog(logger)) { logger.Log("Disclaimer accepted."); return(true); } else { return(false); } } else { logger.Log("Disclaimer accepted via startup parameters."); return(true); } }
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); } }
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"); } }
private static void LoadSettings(string loadSettingsFile, ChaosLogger logger) { if (!string.IsNullOrEmpty(loadSettingsFile)) { try { Tasks.LoadSettings(loadSettingsFile, logger); } catch (Exception ex) { logger.Log("ERROR: " + ex.Message); return; } } }
static void Main(string[] args) { CommandLineParser parser = new CommandLineParser(args); CommandLineSettings commandLineSettings= parser.GetCommandLineSettings(); var logger = new ChaosLogger(commandLineSettings.Settings.LogFileName); try { LoadSettings(commandLineSettings.LoadSettingsFile, logger); if (IsDisclaimerAccepted(commandLineSettings.AcceptDisclaimer, logger)) { } else { logger.Log("Disclaimer not accepted, exiting"); return; } if (commandLineSettings.ApplicationMode.Equals("Pluggable")) { } else { if (!CommandLineValidator.ValidateSettings(commandLineSettings.Settings, logger)) { logger.Log("Invalid settings. use '-?' for help on parameters. Exiting."); return; } } Tasks.UnleashChaos(commandLineSettings.Settings, logger); SaveSettingsFile(commandLineSettings.SaveSettingsFile, commandLineSettings.Settings, logger); } finally { logger.Close(); if (commandLineSettings.ShowHelp) { parser.WriteOptionDescriptions(Console.Out); } } }
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); } } }