public override void Unleash() { var random = new Random(); var ec2Factory = new Ec2Factory(_settings.AwsAccessKey, _settings.AwsSecretKey, _settings.ServiceUrl, _logger); _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)); } }
public void terminate_first_chaos_instance() { var instances = ec2Factory.ListInstancesByTag("Chaos", "1"); if (instances.Count > 0) { ec2Factory.TerminateInstance(instances[0].RunningInstance[0].InstanceId); } }
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); } }