func()

in google_guest_agent/network/manager/systemd_networkd_linux.go [187:236]


func (n *systemdNetworkd) IsManaging(ctx context.Context, iface string) (bool, error) {
	// Check the version.
	exists, err := cliExists("networkctl")
	if !exists {
		return false, err
	}

	res := run.WithOutput(ctx, "networkctl", "--version")
	if res.ExitCode != 0 {
		return false, fmt.Errorf("error checking networkctl version: %v", res.StdErr)
	}
	// The version is the second field of the first line.
	versionString := strings.Split(strings.Split(res.StdOut, "\n")[0], " ")[1]
	version, err := strconv.Atoi(versionString)
	if err != nil {
		return false, fmt.Errorf("error parsing systemd version: %v", err)
	}
	if version < minSupportedVersion {
		logger.Infof("systemd-networkd version %v not supported: minimum %v required", version, minSupportedVersion)
		return false, nil
	}

	// First check if the service is running.
	res = run.WithOutput(ctx, "systemctl", "is-active", "systemd-networkd.service")
	if res.ExitCode != 0 {
		return false, nil
	}

	// Check systemd network configuration.
	res = run.WithOutput(ctx, "/bin/sh", "-c", fmt.Sprintf("networkctl status %s --json=short", iface))
	if res.ExitCode != 0 {
		return false, fmt.Errorf("failed to check systemd-networkd network status: %v", res.StdErr)
	}

	interfaceStatus := make(map[string]any)

	if err = json.Unmarshal([]byte(res.StdOut), &interfaceStatus); err != nil {
		return false, fmt.Errorf("failed to unmarshal interface status: %v", err)
	}

	for _, statusKey := range n.networkCtlKeys {
		state, found := interfaceStatus[statusKey]
		if !found {
			continue
		}

		return state == "configured", nil
	}
	return false, fmt.Errorf("could not determine interface state, one of %v was not present", n.networkCtlKeys)
}