From f16f016094a049377ad5d806305811ef6c9dba36 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Fri, 18 Oct 2024 15:52:36 +0200 Subject: [PATCH 1/2] Improve timesyncd plugin (#182) - 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) From 786d50bb43b217ad273eeb1219ca45ae96b14ee2 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Mon, 21 Oct 2024 09:56:00 +0200 Subject: [PATCH 2/2] Update timesyncd.go (#184) --- pkg/plugins/timesyncd.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/plugins/timesyncd.go b/pkg/plugins/timesyncd.go index 31a68e5f..70647821 100644 --- a/pkg/plugins/timesyncd.go +++ b/pkg/plugins/timesyncd.go @@ -40,11 +40,7 @@ func Timesyncd(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) e } for k, v := range s.TimeSyncd { - if v == "" { - cfg.Section("Time").Key(k).SetValue("\"\"") - } else { - cfg.Section("Time").Key(k).SetValue(v) - } + cfg.Section("Time").Key(k).SetValue(v) } cfg.SaveTo(path)