From 34ab28c3060df01a887de91443e8f6f545d0a553 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Fri, 18 Oct 2024 15:03:00 +0200 Subject: [PATCH] Improve timesyncd plugin - Set empty value if the value as strings if value is empty instead of omitting the value - Write config to /etc/systemd/timesyncd.conf.d/10-yip.conf to properly override the existing config and any others shipped with the system Signed-off-by: Itxaka --- pkg/plugins/timesyncd.go | 17 ++++++++++++++--- pkg/plugins/timesyncd_test.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/plugins/timesyncd.go b/pkg/plugins/timesyncd.go index 4457a2ca..31a68e5f 100644 --- a/pkg/plugins/timesyncd.go +++ b/pkg/plugins/timesyncd.go @@ -2,6 +2,7 @@ package plugins import ( "os" + "path/filepath" "github.com/mudler/yip/pkg/logger" "github.com/mudler/yip/pkg/schema" @@ -9,19 +10,25 @@ import ( "gopkg.in/ini.v1" ) -const timeSyncd = "/etc/systemd/timesyncd.conf" +const timeSyncd = "/etc/systemd/timesyncd.conf.d/10-yip.conf" func Timesyncd(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) error { if len(s.TimeSyncd) == 0 { return nil } var errs error - path, err := fs.RawPath(timeSyncd) if err != nil { return err } + if _, err := fs.Stat(filepath.Dir(timeSyncd)); os.IsNotExist(err) { + err = fs.Mkdir(filepath.Dir(timeSyncd), os.ModeDir|os.ModePerm) + if err != nil { + return err + } + } + if _, err := fs.Stat(timeSyncd); os.IsNotExist(err) { f, _ := fs.Create(timeSyncd) f.Close() @@ -33,7 +40,11 @@ func Timesyncd(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) e } for k, v := range s.TimeSyncd { - cfg.Section("Time").Key(k).SetValue(v) + if v == "" { + cfg.Section("Time").Key(k).SetValue("\"\"") + } else { + cfg.Section("Time").Key(k).SetValue(v) + } } cfg.SaveTo(path) diff --git a/pkg/plugins/timesyncd_test.go b/pkg/plugins/timesyncd_test.go index 511c0722..2c36146c 100644 --- a/pkg/plugins/timesyncd_test.go +++ b/pkg/plugins/timesyncd_test.go @@ -44,7 +44,7 @@ var _ = Describe("Timesyncd", func() { }, fs, testConsole) Expect(err).ShouldNot(HaveOccurred()) - file, err := fs.Open("/etc/systemd/timesyncd.conf") + file, err := fs.Open("/etc/systemd/timesyncd.conf.d/10-yip.conf") Expect(err).ShouldNot(HaveOccurred()) b, err := ioutil.ReadAll(file)