func computeExitCode(c *ec2macosinit.InitConfig, e int) (exitCode int) {
	exceeded, err := c.RetriesExceeded()
	if err != nil {
		c.Log.Errorf("Error while getting retry information: %s", err)
		return 1
	}
	if exceeded {
		c.Log.Errorf("Number of fatal retries (%d) exceeded, exiting 0 to avoid infinite runs",
			c.FatalCounts.Count)
		return 0
	}
	c.Log.Infof("Fatal [%d/%d] of this boot", c.FatalCounts.Count, ec2macosinit.PerBootFatalLimit)
	err = c.FatalCounts.IncrementFatalCount()
	if err != nil {
		c.Log.Errorf("Unable to write fatal counts to file: %s", err)
	}
	return e
}
computeExitCode checks to see if the number of fatal retries has been exceeded. If not, it increments the counter,
stored in a temporary file, and returns the requested exit code. If the count is exceeded, it returns 0 to avoid
launchd restarting forever due to the KeepAlive setting.
package main
import (
	"fmt"
	"path"
	"sync"
	"time"
	"github.com/aws/ec2-macos-init/lib/ec2macosinit"
)
func run(c *ec2macosinit.InitConfig) {
	c.Log.Info("Fetching instance ID from IMDS...")
	err := SetupInstanceID(c)
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 1), "Unable to get instance ID: %s", err)
	}
	c.Log.Infof("Running on instance %s", c.IMDS.InstanceID)
	startTime := time.Now()
	c.Log.Info("Reading init config...")
	err = c.ReadConfig(path.Join(baseDir, configFile))
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 66), "Error while reading init config file: %s", err)
	}
	c.Log.Info("Successfully read init config")
	c.Log.Info("Validating config...")
	err = c.ValidateAndIdentify()
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 65), "Error found during init config validation: %s", err)
	}
	c.Log.Info("Successfully validated config")
	c.Log.Info("Prioritizing modules...")
	err = c.PrioritizeModules()
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 1), "Error preparing and identifying modules: %s", err)
	}
	c.Log.Info("Successfully prioritized modules")
	c.Log.Info("Creating instance history directories for current instance...")
	err = c.CreateDirectories()
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 73), "Error creating instance history directories: %s", err)
	}
	c.Log.Info("Successfully created directories")
	c.Log.Info("Getting instance history...")
	err = c.GetInstanceHistory()
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 1), "Error getting instance history: %s", err)
	}
	c.Log.Info("Successfully gathered instance history")
	var aggregateFatal bool
	var aggFatalModuleName string
	for i := 0; i < len(c.ModulesByPriority); i++ {
		c.Log.Infof("Processing priority level %d (%d modules)...\n", i+1, len(c.ModulesByPriority[i]))
		wg := sync.WaitGroup{}
		for j := 0; j < len(c.ModulesByPriority[i]); j++ {
			wg.Add(1)
			go func(m *ec2macosinit.Module, h *[]ec2macosinit.History) {
				if m.ShouldRun(c.IMDS.InstanceID, *h) {
					c.Log.Infof("Running module [%s] (type: %s, group: %d)\n", m.Name, m.Type, m.PriorityGroup)
					var message string
					var err error
					ctx := &ec2macosinit.ModuleContext{
						Logger: c.Log,
						IMDS:   &c.IMDS,
					}
					switch t := m.Type; t {
					case "command":
						message, err = m.CommandModule.Do(ctx)
					case "motd":
						message, err = m.MOTDModule.Do(ctx)
					case "sshkeys":
						message, err = m.SSHKeysModule.Do(ctx)
					case "userdata":
						message, err = m.UserDataModule.Do(ctx)
					case "networkcheck":
						message, err = m.NetworkCheckModule.Do(ctx)
					case "systemconfig":
						message, err = m.SystemConfigModule.Do(ctx)
					case "usermanagement":
						message, err = m.UserManagementModule.Do(ctx)
					default:
						message = "unknown module type"
						err = fmt.Errorf("unknown module type")
					}
					if err != nil {
						c.Log.Infof("Error while running module [%s] (type: %s, group: %d) with message: %s and err: %s\n", m.Name, m.Type, m.PriorityGroup, message, err)
						if m.FatalOnError {
							aggregateFatal = true
							aggFatalModuleName = m.Name
						}
					} else {
						m.Success = true
						c.Log.Infof("Successfully completed module [%s] (type: %s, group: %d) with message: %s\n", m.Name, m.Type, m.PriorityGroup, message)
					}
				} else {
					m.Success = true
					c.Log.Infof("Skipping module [%s] (type: %s, group: %d) due to Run type setting\n", m.Name, m.Type, m.PriorityGroup)
				}
				wg.Done()
			}(&c.ModulesByPriority[i][j], &c.InstanceHistory)
		}
		wg.Wait()
		c.Log.Infof("Successfully completed processing of priority level %d\n", i+1)
		if aggregateFatal {
			break
		}
	}
	c.Log.Infof("Writing instance history for instance %s...", c.IMDS.InstanceID)
	err = c.WriteHistoryFile()
	if err != nil {
		c.Log.Fatalf(computeExitCode(c, 73), "Error writing instance history file: %s", err)
	}
	c.Log.Info("Successfully wrote instance history")
	if aggregateFatal {
		c.Log.Fatalf(computeExitCode(c, 1), "Exiting after %s due to failure in module [%s] with FatalOnError set", time.Since(startTime).String(), aggFatalModuleName)
	}
	c.Log.Infof("EC2 macOS Init completed in %s", time.Since(startTime).String())
}