ntp

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ntp is the ntp behavior: the NTP servers that chrony uses, and its sync status.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"ntp", "The NTP servers of chrony, and its sync status",
	behavior.Title("NTP"),
	behavior.Node,
	behavior.API,
	behavior.Area(area.Config),
	behavior.CLI,
	behavior.Requires(managedfile.Behavior),
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Runs("chronyc"),
	behavior.Sudo("chronyc"),
	behavior.Writes(sourcesFile),
	behavior.Validator("ntp_server", serverRE.MatchString,
		func(t rule.Target) { t.Pattern(serverPattern) },
		"is not a hostname or an IP address"),
	behavior.Read("get", Get).
		NotAFact("the sync status and the sources are the live state of chrony, not a fact of the host").
		Describe("Get the sync status of chrony and its sources").
		Explain("Reads chronyc tracking, for whether chrony is synchronized, its stratum, "+
			"its offset from NTP time and the source that it follows, and chronyc "+
			"sources, for each source that it has.").
		Example("A host synchronized to time.cloudflare.com", GetInput{},
			Status{
				Synchronized:  true,
				Stratum:       3,
				Offset:        "+0.000003422s",
				CurrentSource: "time.cloudflare.com",
				Servers:       []string{"time.cloudflare.com", "0.pool.ntp.org"},
			},
			behavior.Prints("chronyc tracking", tracking),
			behavior.Prints("chronyc -c sources", sources),
		).
		Example("A host that chrony does not synchronize", GetInput{},
			Status{Offset: "-0.000001834s"},
			behavior.Prints("chronyc tracking",
				"System time     : 0.000001834 seconds slow of NTP time\n"+
					"Leap status     : Not synchronised\n"),
			behavior.Prints("chronyc -c sources", ""),
		),
	behavior.Write("create", Create).
		Describe("Set the NTP servers that chrony uses").
		Explain("Writes a server line for each server to "+sourcesFile+", and reloads "+
			"the sources with chronyc reload sources. A file that has the same servers "+
			"does not change, and reports changed: false.").
		Example("Set two servers", CreateInput{Servers: []string{"0.pool.ntp.org", "1.pool.ntp.org"}},
			Result{Changed: true},
			behavior.Wrote(sourcesFile, "server 0.pool.ntp.org iburst\nserver 1.pool.ntp.org iburst\n"),
			behavior.Ran("chronyc", "reload", "sources"),
			ntpReadBack,
		).
		Example("The servers that the host has", CreateInput{Servers: []string{"0.pool.ntp.org"}},
			Result{},
			behavior.After(CreateInput{Servers: []string{"0.pool.ntp.org"}}),
			ntpReadBack,
		).
		Fails("No servers", CreateInput{}, "required").
		Fails("An empty list of servers", CreateInput{Servers: []string{}}, "min").
		Fails("A server with a directive after it",
			CreateInput{Servers: []string{"0.pool.ntp.org iburst\nallow all"}},
			"is not a hostname or an IP address").
		Fails("More than 16 servers", CreateInput{Servers: make17()}, "max"),
	behavior.Write("update", Update).
		Describe("Replace the NTP servers that osapi set").
		Explain("The same as create, for servers that osapi set already. It fails with "+
			"not found when osapi set none. The servers that the file has already "+
			"report changed: false.").
		Example("Replace the servers", UpdateInput{Servers: []string{"time.cloudflare.com"}},
			Result{Changed: true},
			behavior.After(CreateInput{Servers: []string{"0.pool.ntp.org"}}),
			behavior.Wrote(sourcesFile, "server time.cloudflare.com iburst\n"),
			behavior.Ran("chronyc", "reload", "sources"),
			ntpReadBack,
		).
		Example("The servers that the file has", UpdateInput{Servers: []string{"0.pool.ntp.org"}},
			Result{},
			behavior.After(CreateInput{Servers: []string{"0.pool.ntp.org"}}),
			ntpReadBack,
		).
		Fails("When osapi set no servers", UpdateInput{Servers: []string{"0.pool.ntp.org"}},
			"osapi manages no servers").
		Fails("No servers", UpdateInput{}, "required").
		Fails("An empty list of servers", UpdateInput{Servers: []string{}}, "min").
		Fails("A server with a directive after it",
			UpdateInput{Servers: []string{"0.pool.ntp.org iburst\nallow all"}},
			"is not a hostname or an IP address").
		Fails("More than 16 servers", UpdateInput{Servers: make17()}, "max"),
	behavior.Write("delete", Delete).
		Describe("Remove the NTP servers that osapi set").
		Explain("Removes "+sourcesFile+" and reloads the sources, so chrony uses its "+
			"other sources. When osapi set no servers, it reports changed: false.").
		Example("Remove the servers", DeleteInput{}, Result{Changed: true},
			behavior.After(CreateInput{Servers: []string{"0.pool.ntp.org"}}),
			behavior.Removed(sourcesFile),
			behavior.Ran("chronyc", "reload", "sources"),
			ntpReadBack,
		).
		Example("When osapi set no servers", DeleteInput{}, Result{},
			ntpReadBack,
		),
))

Behavior is the ntp definition. chronyc reads the sync status, which is the live state of chrony and not a fact of the host; the servers that osapi sets go to one file under /etc/chrony/sources.d through managedfile.

Functions

This section is empty.

Types

type CreateInput

type CreateInput struct {
	Servers []string `` /* 129-byte string literal not displayed */
}

CreateInput is create's input: the servers chrony uses.

type DeleteInput

type DeleteInput struct{}

DeleteInput is delete's input: delete has none.

type GetInput

type GetInput struct{}

GetInput is get's input: get has none.

type Provider

type Provider interface {
	// Get returns chrony's sync status and its sources.
	Get(ctx context.Context) (Status, error)
	// Create writes the servers that osapi manages, and reports whether the
	// host changed. A file with the same servers is left alone.
	Create(ctx context.Context, servers []string) (bool, error)
	// Update replaces the servers that osapi manages. It fails with
	// behavior.ErrNotFound when osapi manages none.
	Update(ctx context.Context, servers []string) (bool, error)
	// Delete removes the servers that osapi manages, and reports whether a
	// file was removed.
	Delete(ctx context.Context) (bool, error)
}

Provider is what each platform implements.

type Result

type Result struct {
	Changed bool `json:"changed" doc:"Whether the host changed"`
}

Result is the output of each write: whether it changed the host.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (Result, error)

Create manages the servers that chrony uses.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	_ DeleteInput,
) (Result, error)

Delete stops managing the servers.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update replaces the servers that osapi manages.

type Status

type Status struct {
	Synchronized  bool     `json:"synchronized"             doc:"Whether chrony is synchronized to a source"`
	Stratum       int      `json:"stratum,omitempty"        doc:"The NTP stratum of the host"`
	Offset        string   `json:"offset,omitempty"         doc:"The offset of the system clock from NTP time, such as +0.000003422s"`
	CurrentSource string   `json:"current_source,omitempty" doc:"The source that chrony follows now"`
	Servers       []string `json:"servers,omitempty"        doc:"The sources that chrony has"`
}

Status is get's output: whether chrony is synchronized, and its sources.

func Get

func Get(
	ctx context.Context,
	p Provider,
	_ GetInput,
) (Status, error)

Get reads chrony's sync status and its sources.

type UpdateInput

type UpdateInput struct {
	Servers []string `` /* 129-byte string literal not displayed */
}

UpdateInput is update's input: the servers that replace the ones osapi set.

Directories

Path Synopsis
Package mocks holds the generated double of the ntp provider, for the methods' tests.
Package mocks holds the generated double of the ntp provider, for the methods' tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL