netplan

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: 10 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 netplan is the netplan behavior: the netplan files that osapi writes, validated and applied.

Index

Constants

View Source
const Dir = "/etc/netplan"

Dir is where netplan reads its config, and where osapi writes its own files, each named osapi-<something>.yaml.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"netplan", "The netplan files osapi writes, validated and applied",
	behavior.Node,
	behavior.Internal,
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Requires(managedfile.Behavior),
	behavior.Sudo("netplan"),
	behavior.Runs("netplan"),
	behavior.Writes(Dir+"/osapi-*.yaml"),
	behavior.Write("update", Update).
		Describe("Write a netplan file, validate it and apply it").
		Explain("Writes the file with mode 0600, validates the whole config with netplan "+
			"generate, and applies it with netplan apply. When netplan generate refuses "+
			"it, the file gets back the content it had, or a new file is removed, because "+
			"netplan reads every file in the directory. A file that already holds the "+
			"content changes nothing.").
		Example("A new file", UpdateInput{Path: dnsFile, Content: dnsYAML},
			Result{Path: dnsFile, Changed: true},
			behavior.Wrote(dnsFile, dnsYAML),
			behavior.Ran("netplan", "generate"),
			behavior.Ran("netplan", "apply"),
		).
		Fails("A file outside osapi's own", UpdateInput{Path: Dir + "/50-cloud-init.yaml", Content: dnsYAML},
			"'startswith' tag").
		Fails("A name that leaves the directory",
			UpdateInput{Path: Dir + "/osapi-../../shadow.yaml", Content: dnsYAML},
			"not one of osapi's netplan files").
		Fails("A name that is not YAML", UpdateInput{Path: Dir + "/osapi-dns.conf", Content: dnsYAML},
			"not one of osapi's netplan files"),
	behavior.Write("delete", Delete).
		Describe("Remove a netplan file and apply what is left").
		Example("A file osapi wrote", DeleteInput{Path: dnsFile},
			Result{Path: dnsFile, Changed: true},
			behavior.After(UpdateInput{Path: dnsFile, Content: dnsYAML}),
			behavior.Removed(dnsFile),
			behavior.Ran("netplan", "apply"),
		).
		Example("A file that is not there", DeleteInput{Path: dnsFile}, Result{Path: dnsFile}).
		Fails("A file outside osapi's own", DeleteInput{Path: Dir + "/50-cloud-init.yaml"},
			"'startswith' tag").
		Fails("A name that leaves the directory", DeleteInput{Path: Dir + "/osapi-../../shadow.yaml"},
			"not one of osapi's netplan files"),
	behavior.Read("get", Get).Key("name").
		NotAFact("the definition that netplan has of an interface is the configuration of netplan, not a fact of the host").
		Describe("Get what netplan status reports for an interface").
		Explain("Reads netplan status: the interface's type, its section and the ID of the "+
			"definition that matches it, which a write goes under, and each interface that "+
			"definition matches. netplan lists only the interfaces it manages, so an "+
			"interface that it does not list is an ethernet under its own name.").
		Example("A bond", GetInput{Name: "bond0"},
			Interface{
				Name:    "bond0",
				Type:    "bond",
				Section: "bonds",
				ID:      "bond0",
				Matches: []string{"bond0"},
			},
			behavior.Prints("netplan status --format json", status),
		).
		Example("A dummy", GetInput{Name: "osapi0"},
			Interface{
				Name:    "osapi0",
				Type:    "dummy-device",
				Section: "dummy-devices",
				ID:      "osapi0",
				Matches: []string{"osapi0"},
			},
			behavior.Prints("netplan status --format json",
				`{"osapi0":{"type":"dummy-device","id":"osapi0"}}`),
		).
		Example("An interface a match defines, as cloud-init's all for en*", GetInput{Name: "enp0s1"},
			Interface{
				Name: "enp0s1", Type: "ethernet", Section: "ethernets", ID: "all",
				Matches: []string{"enp0s1", "enp0s2"},
			},
			behavior.Prints("netplan status --format json", status),
		).
		Example("An interface netplan does not define", GetInput{Name: "lo"},
			Interface{
				Name:    "lo",
				Type:    "ethernet",
				Section: "ethernets",
				ID:      "lo",
				Matches: []string{"lo"},
			},
			behavior.Prints("netplan status --format json", status),
		).
		Example("A netplan without status", GetInput{Name: "eth0"},
			Interface{Name: "eth0", Section: "ethernets", ID: "eth0", Matches: []string{"eth0"}},
			behavior.Prints("netplan status --format json", "Invalid command: status\n"),
		).
		Example("An interface netplan does not manage", GetInput{Name: "eth9"},
			Interface{Name: "eth9", Section: "ethernets", ID: "eth9", Matches: []string{"eth9"}},
			behavior.Prints("netplan status --format json", status),
		),
))

Behavior writes, validates and applies the netplan files of the dns, interface and route behaviors, which require it. It keeps each file's record through managedfile.

Functions

func Section

func Section(
	t string,
) string

Section is the netplan section of an interface of type t.

Types

type DeleteInput

type DeleteInput struct {
	Path string `` /* 144-byte string literal not displayed */
}

DeleteInput is a netplan file to remove.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,min=1" doc:"The interface, such as eth0"`
}

GetInput names an interface.

type Interface

type Interface struct {
	Name    string   `json:"name"    doc:"The interface"`
	Type    string   `json:"type"    doc:"Its type, such as ethernet, bond or wifi; empty when netplan does not report it"`
	Section string   `json:"section" doc:"The netplan section that configures it, such as ethernets or bonds"`
	ID      string   `` /* 155-byte string literal not displayed */
	Matches []string `json:"matches" doc:"Each interface that the definition matches, this one too, in name order"`
}

Interface is an interface as netplan status reports it.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Interface, error)

Get is what netplan status reports for an interface.

type Provider

type Provider interface {
	// Apply writes a file, validates it with netplan generate, rolling it
	// back when that fails, and applies it. It changes nothing when the
	// file already holds the content.
	Apply(ctx context.Context, in UpdateInput) (bool, error)
	// Remove removes a file and applies what is left.
	Remove(ctx context.Context, path string) (bool, error)
	// Interface is what netplan status reports for an interface.
	Interface(ctx context.Context, name string) (Interface, error)
}

Provider writes, removes and reads netplan config.

type Result

type Result struct {
	Path    string `json:"path"    doc:"The file"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is a netplan file after a write or a removal.

func Delete

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

Delete removes a netplan file and applies what is left.

func Update

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

Update writes a netplan file and applies it.

type UpdateInput

type UpdateInput struct {
	Path     string            `` /* 158-byte string literal not displayed */
	Content  string            `json:"content"            validate:"required"                                doc:"The file's whole YAML"`
	Metadata map[string]string `json:"metadata,omitempty" validate:"omitempty"                               doc:"What the caller keeps in the file's record"`
}

UpdateInput is a netplan file to write and apply.

Directories

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

Jump to

Keyboard shortcuts

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