|
| 1 | +package setup |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | +) |
| 8 | + |
| 9 | +// Configuration |
| 10 | +type Conf struct { |
| 11 | + hooksList map[string]Hook |
| 12 | + oneshot bool |
| 13 | +} |
| 14 | + |
| 15 | +// Create a new configuration from env variables |
| 16 | +func NewConf() Conf { |
| 17 | + conf := Conf{ |
| 18 | + hooksList: make(map[string]Hook, 0), |
| 19 | + } |
| 20 | + conf.AddHooks() |
| 21 | + conf.AddUserHooks("pre", "PRE") |
| 22 | + conf.AddUserHooks("post", "POST") |
| 23 | + conf.oneshot = false |
| 24 | + if oneshot, isset := os.LookupEnv("ONESHOT"); isset && oneshot == "true" { |
| 25 | + conf.oneshot = true |
| 26 | + } |
| 27 | + return conf |
| 28 | +} |
| 29 | + |
| 30 | +// Return true if Oneshot is set |
| 31 | +func (conf Conf) Oneshot() bool { |
| 32 | + return conf.oneshot |
| 33 | +} |
| 34 | + |
| 35 | +// Run exit hooks |
| 36 | +func (conf Conf) RunExitHooks() { |
| 37 | + conf.RunExitHook("pre") |
| 38 | + conf.RunExitHook("nat4") |
| 39 | + conf.RunExitHook("iproute") |
| 40 | + conf.RunExitHook("post") |
| 41 | +} |
| 42 | + |
| 43 | +// Run init hooks |
| 44 | +func (conf Conf) RunInitHooks() { |
| 45 | + conf.RunInitHook("pre") |
| 46 | + conf.RunInitHook("iproute") |
| 47 | + conf.RunInitHook("nat4") |
| 48 | + conf.RunInitHook("post") |
| 49 | +} |
| 50 | + |
| 51 | +// Add a new hook to the configuration |
| 52 | +func (conf Conf) AddUserHooks(name string, env string) { |
| 53 | + conf.hooksList[name] = NewUserHooks( |
| 54 | + fmt.Sprintf("%s init", name), fmt.Sprintf("%s_INIT_HOOK", env), |
| 55 | + fmt.Sprintf("%s exit", name), fmt.Sprintf("%s_EXIT_HOOK", env)) |
| 56 | +} |
| 57 | + |
| 58 | +// Add default hooks |
| 59 | +func (conf Conf) AddHooks() { |
| 60 | + conf.hooksList["iproute"] = NewIPRouteHooks( |
| 61 | + "iproute init", "ROUTES_INIT", |
| 62 | + "iproute exit", "ROUTES_EXIT") |
| 63 | + conf.hooksList["nat4"] = NewNat4Hooks() |
| 64 | +} |
| 65 | + |
| 66 | +// Run an init hook |
| 67 | +func (conf Conf) RunInitHook(name string) { |
| 68 | + if conf.hooksList[name] != nil { |
| 69 | + if err := conf.hooksList[name].RunInit(); err != nil { |
| 70 | + log.Printf("Error while running %s init hook: %s", name, err) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Run an exit hook |
| 76 | +func (conf Conf) RunExitHook(name string) { |
| 77 | + if conf.hooksList[name] != nil { |
| 78 | + if err := conf.hooksList[name].RunExit(); err != nil { |
| 79 | + log.Printf("Error while running %s exit hook: %s", name, err) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Log the configuration |
| 85 | +func (conf Conf) Log() { |
| 86 | + log.Println("Current configuration:") |
| 87 | + if conf.oneshot { |
| 88 | + log.Printf("\t- mode oneshot is enabled") |
| 89 | + } else { |
| 90 | + log.Printf("\t- mode oneshot is disabled") |
| 91 | + } |
| 92 | + for _, h := range conf.hooksList { |
| 93 | + for _, s := range h.String() { |
| 94 | + log.Printf("\t- %s\n", s) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments