mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
Add NTP Server Configuration
This commit is contained in:
parent
6f396b3833
commit
afbfed5cb6
5 changed files with 95 additions and 0 deletions
37
internal/chrony/apply.go
Normal file
37
internal/chrony/apply.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package chrony
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
systemctl "github.com/coreos/go-systemd/v22/dbus"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
"nfsense.net/nfsense/internal/util"
|
||||
)
|
||||
|
||||
const chronyConfigFile = "/etc/chrony.conf"
|
||||
|
||||
func ApplyNTPConfiguration(currentConfig config.Config, pendingConfig config.Config) error {
|
||||
|
||||
conf, err := GenerateChronyConfiguration(pendingConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Generating Chrony Configuration: %w", err)
|
||||
}
|
||||
|
||||
err = util.OverwriteFile(chronyConfigFile, conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Writing Chrony Configuration: %w", err)
|
||||
}
|
||||
|
||||
conn, err := systemctl.NewSystemConnectionContext(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Opening Dbus Connection: %w", err)
|
||||
}
|
||||
|
||||
_, err = conn.ReloadOrRestartUnitContext(context.Background(), "chronyd.service", "replace", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("restarting chronyd.service: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
17
internal/chrony/config.go
Normal file
17
internal/chrony/config.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package chrony
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func GenerateChronyConfiguration(conf config.Config) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "config.tmpl", conf)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("executing server.tmpl template: %w", err)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
26
internal/chrony/template.go
Normal file
26
internal/chrony/template.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package chrony
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"text/template"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
//go:embed template
|
||||
var templateFS embed.FS
|
||||
var templates *template.Template
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
templates, err = template.New("").Funcs(template.FuncMap{
|
||||
"getInterfaceNetworkAddressCIDR": getInterfaceNetworkAddressCIDR,
|
||||
}).ParseFS(templateFS, "template/*.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getInterfaceNetworkAddressCIDR(conf config.Config, name string) string {
|
||||
return conf.Network.Interfaces[name].Address.Masked().String()
|
||||
}
|
13
internal/chrony/template/config.tmpl
Normal file
13
internal/chrony/template/config.tmpl
Normal file
|
@ -0,0 +1,13 @@
|
|||
pool pool.ntp.org iburst
|
||||
driftfile /var/lib/chrony/drift
|
||||
makestep 1.0 3
|
||||
rtcsync
|
||||
keyfile /etc/chrony.keys
|
||||
ntsdumpdir /var/lib/chrony
|
||||
leapsectz right/UTC
|
||||
logdir /var/log/chrony
|
||||
|
||||
# Allowed Networks
|
||||
{{- range $i, $server := .Service.NTPServers }}
|
||||
allow {{ getInterfaceNetworkAddressCIDR $ $server.Interface }}
|
||||
{{- end }}
|
2
main.go
2
main.go
|
@ -16,6 +16,7 @@ import (
|
|||
"nfsense.net/nfsense/internal/api/network"
|
||||
"nfsense.net/nfsense/internal/api/object"
|
||||
"nfsense.net/nfsense/internal/api/service"
|
||||
"nfsense.net/nfsense/internal/chrony"
|
||||
"nfsense.net/nfsense/internal/config"
|
||||
dhcp "nfsense.net/nfsense/internal/dhcp_server"
|
||||
"nfsense.net/nfsense/internal/jsonrpc"
|
||||
|
@ -104,4 +105,5 @@ func RegisterAPIMethods(apiHandler *jsonrpc.Handler, configManager *config.Confi
|
|||
func RegisterApplyFunctions(configManager *config.ConfigManager) {
|
||||
configManager.RegisterApplyFunction(networkd.ApplyNetworkdConfiguration)
|
||||
configManager.RegisterApplyFunction(dhcp.ApplyDHCPServerConfiguration)
|
||||
configManager.RegisterApplyFunction(chrony.ApplyNTPConfiguration)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue