2022-03-08 10:45:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-04-04 08:29:28 +00:00
|
|
|
config "novit.tech/direktil/pkg/bootstrapconfig"
|
2022-03-08 10:45:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupNetworks(cfg *config.Config) {
|
|
|
|
if len(cfg.Networks) == 0 {
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("no networks configured.")
|
2022-03-08 10:45:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ifNames := make([]string, 0)
|
|
|
|
{
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
fatal("failed")
|
|
|
|
}
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
ifNames = append(ifNames, iface.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assigned := map[string]bool{}
|
|
|
|
|
|
|
|
for _, network := range cfg.Networks {
|
2024-01-20 15:41:54 +00:00
|
|
|
log := log.With().Str("network", network.Name).Logger()
|
|
|
|
log.Info().Msg("setting up network")
|
2022-03-08 10:45:56 +00:00
|
|
|
|
|
|
|
// compute available names
|
|
|
|
if len(assigned) != 0 {
|
|
|
|
newNames := make([]string, 0, len(ifNames))
|
|
|
|
for _, n := range ifNames {
|
|
|
|
if assigned[n] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newNames = append(newNames, n)
|
|
|
|
}
|
|
|
|
ifNames = newNames
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign envvars
|
|
|
|
envvars := make([]string, 0, 1+len(network.Interfaces))
|
|
|
|
envvars = append(envvars, "PATH=/bin:/sbin:/usr/bin:/usr/sbin")
|
|
|
|
|
|
|
|
for _, match := range network.Interfaces {
|
|
|
|
envvar := new(strings.Builder)
|
|
|
|
envvar.WriteString(match.Var)
|
|
|
|
envvar.WriteByte('=')
|
|
|
|
|
|
|
|
for i, m := range regexpSelectN(match.N, match.Regexps, ifNames) {
|
|
|
|
if i != 0 {
|
|
|
|
envvar.WriteByte(' ')
|
|
|
|
}
|
|
|
|
envvar.WriteString(m)
|
|
|
|
assigned[m] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
envvars = append(envvars, envvar.String())
|
|
|
|
}
|
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Strs("env", envvars).Msg("running script")
|
|
|
|
|
2022-03-08 10:45:56 +00:00
|
|
|
cmd := exec.Command("/bin/sh", "-c", network.Script)
|
|
|
|
cmd.Env = envvars
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fatal("failed to setup network: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|