mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
add basepath, remove old, configs, reload networkd
This commit is contained in:
parent
7de307285e
commit
2c1df2a4ad
1 changed files with 43 additions and 1 deletions
|
@ -1,20 +1,31 @@
|
|||
package networkd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
const basepath = "/etc/systemd/network"
|
||||
|
||||
func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig definitions.Config) error {
|
||||
files, err := GenerateNetworkdConfiguration(pendingConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Generating Networkd Configuration: %w", err)
|
||||
}
|
||||
|
||||
err = RemoveContents(basepath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Removing old Config Files: %w", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
f, err := os.Create("out/" + file.Name)
|
||||
f, err := os.Create(basepath + "/" + file.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating File: %w", err)
|
||||
}
|
||||
|
@ -29,5 +40,36 @@ func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig
|
|||
return fmt.Errorf("syncing File: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command("systemctl", "restart", "systemd-networkd")
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("restarting networkd: %w", err)
|
||||
}
|
||||
slog.Info("networkd output", "out", out.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveContents(dir string) error {
|
||||
d, err := os.Open(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Close()
|
||||
names, err := d.Readdirnames(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
err = os.RemoveAll(filepath.Join(dir, name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue