func doTerminate()

in term/term.go [95:181]


func doTerminate(d deps.Deps, group grp.InstanceGroup) error {
	leashed, err := d.MonkeyCfg.Leashed()

	if err != nil {
		return errors.Wrap(err, "not terminating: could not determine leashed status")
	}

	/*
		Do not allow running unleashed in the test environment.

		The prod deployment of chaos monkey is responsible for killing instances
		across environments, including test. We want to ensure that Chaos Monkey
		running in test cannot do harm.
	*/
	if d.Env.InTest() && !leashed {
		return UnleashedInTestEnv{}
	}

	var killer chaosmonkey.Terminator

	if leashed {
		killer = leashedKiller{}
	} else {
		killer = d.T
	}

	// get Chaos Monkey config info for this app
	appName := group.App()
	appCfg, err := d.ConfGetter.Get(appName)

	if err != nil {
		return errors.Wrapf(err, "not terminating: Could not retrieve config for app=%s", appName)
	}

	if !appCfg.Enabled {
		log.Printf("not terminating: enabled=false for app=%s", appName)
		return nil
	}

	if appCfg.Whitelist != nil {
		log.Printf("not terminating: app=%s has a whitelist which is no longer supported", appName)
		return nil
	}

	instance, ok := PickRandomInstance(group, *appCfg, d.Dep)
	if !ok {
		log.Printf("No eligible instances in group, nothing to terminate: %+v", group)
		return nil
	}

	log.Printf("Picked: %s", instance)

	loc, err := d.MonkeyCfg.Location()
	if err != nil {
		return errors.Wrap(err, "not terminating: could not retrieve location")
	}

	trm := chaosmonkey.Termination{Instance: instance, Time: d.Cl.Now(), Leashed: leashed}

	//
	// Check that we don't violate min time between terminations
	//
	err = d.Checker.Check(trm, *appCfg, d.MonkeyCfg.EndHour(), loc)
	if err != nil {
		return errors.Wrap(err, "not terminating: check for min time between terminations failed")
	}

	//
	// Record the termination with configured trackers
	//
	for _, tracker := range d.Trackers {
		err = tracker.Track(trm)
		if err != nil {
			return errors.Wrap(err, "not terminating: recording termination event failed")
		}
	}

	//
	// Actual instance termination happens here
	//
	err = killer.Execute(trm)
	if err != nil {
		return errors.Wrap(err, "termination failed")
	}

	return nil
}