Skip to content

Commit

Permalink
Improve timesyncd plugin
Browse files Browse the repository at this point in the history
 - 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 <[email protected]>
  • Loading branch information
Itxaka committed Oct 18, 2024
1 parent 87b55bb commit 34ab28c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions pkg/plugins/timesyncd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ package plugins

import (
"os"
"path/filepath"

"github.com/mudler/yip/pkg/logger"
"github.com/mudler/yip/pkg/schema"
"github.com/twpayne/go-vfs/v4"
"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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/timesyncd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 34ab28c

Please sign in to comment.