2022-03-08 10:45:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2024-11-04 15:18:53 +00:00
|
|
|
udev "github.com/jochenvg/go-udev"
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-04 15:18:53 +00:00
|
|
|
type Iface struct {
|
|
|
|
Name string
|
|
|
|
AliasOf string
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces := make([]NameAliases, 0)
|
2022-03-08 10:45:56 +00:00
|
|
|
{
|
2024-11-04 15:18:53 +00:00
|
|
|
enum := new(udev.Udev).NewEnumerate()
|
|
|
|
enum.AddMatchSubsystem("net")
|
|
|
|
|
|
|
|
devs, err := enum.Devices()
|
2022-03-08 10:45:56 +00:00
|
|
|
if err != nil {
|
2024-11-04 15:18:53 +00:00
|
|
|
fatal("udev enumeration failed")
|
2022-03-08 10:45:56 +00:00
|
|
|
}
|
2024-11-04 15:18:53 +00:00
|
|
|
|
|
|
|
for _, dev := range devs {
|
|
|
|
iface := nameAliases(dev.Sysname(),
|
|
|
|
dev.PropertyValue("INTERFACE"),
|
|
|
|
dev.PropertyValue("ID_NET_NAME"),
|
|
|
|
dev.PropertyValue("ID_NET_NAME_PATH"),
|
|
|
|
dev.PropertyValue("ID_NET_NAME_MAC"),
|
|
|
|
dev.PropertyValue("ID_NET_NAME_SLOT"),
|
|
|
|
)
|
|
|
|
log.Info().Str("name", iface.Name).Any("aliases", iface.Aliases).Msg("found network device")
|
|
|
|
ifaces = append(ifaces, iface)
|
2022-03-08 10:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-11-04 15:18:53 +00:00
|
|
|
unassigned := filter(ifaces, func(iface NameAliases) bool {
|
|
|
|
return !assigned[iface.Name]
|
|
|
|
})
|
2022-03-08 10:45:56 +00:00
|
|
|
|
|
|
|
// 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('=')
|
2024-11-04 15:18:53 +00:00
|
|
|
for i, m := range regexpSelectN(match.N, match.Regexps, unassigned) {
|
|
|
|
assigned[m] = true
|
2022-03-08 10:45:56 +00:00
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
envvar.WriteByte(' ')
|
|
|
|
}
|
|
|
|
envvar.WriteString(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|