From b71ff8278c58d6f5f1182b648529fbf5f16fdb99 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 27 Feb 2024 23:51:28 +0100 Subject: feat(stdlib): bootstrapped stdlib with debian and systemd promises --- stdlib/os/debian/apt.go | 70 +++++++++++++++++++++++++++++++++++++++++++ stdlib/os/debian/sources.list | 6 ++++ 2 files changed, 76 insertions(+) create mode 100644 stdlib/os/debian/apt.go create mode 100644 stdlib/os/debian/sources.list (limited to 'stdlib/os/debian') diff --git a/stdlib/os/debian/apt.go b/stdlib/os/debian/apt.go new file mode 100644 index 0000000..63ce497 --- /dev/null +++ b/stdlib/os/debian/apt.go @@ -0,0 +1,70 @@ +package debian + +import ( + "bufio" + "bytes" + _ "embed" + "log/slog" + "os" + "os/exec" + + "git.adyxax.org/adyxax/gonf/v2/gonf" + "git.adyxax.org/adyxax/gonf/v2/stdlib/os/systemd" +) + +var packages map[string]string + +func init() { + packages_list() +} + +//go:embed sources.list +var sources_list []byte + +func Promise() { + rootRO := gonf.ModeUserGroup(0444, "root", "root") + gonf.Default("debian-release", "stable") + gonf.AppendVariable("debian-extra-sources", "# Extra sources") + apt_update := gonf.Command("apt-get", "update", "-qq") + gonf.File("/etc/apt/sources.list").Permissions(rootRO).Template(sources_list).Promise().IfRepaired(apt_update) + gonf.SetPackagesConfiguration(packages_install, packages_list, apt_update) + gonf.Service("opensmtpd").State("enabled", "started").Promise() + systemd.Promise() +} + +func packages_install(names []string) gonf.Status { + allKept := true + for _, n := range names { + if _, ok := packages[n]; !ok { + allKept = false + } + } + if allKept { + return gonf.KEPT + } + args := append([]string{"install", "-y", "--no-install-recommends"}, names...) + cmd := gonf.CommandWithEnv([]string{"DEBIAN_FRONTEND=noninteractive", "LC_ALL=C"}, "apt-get", args...) + cmd.Resolve() + packages_list() + return cmd.Status +} + +func packages_list() { + packages = make(map[string]string) + ecmd := exec.Command("dpkg-query", "-W") + out, err := ecmd.CombinedOutput() + if err != nil { + slog.Error("dpkg-query", "error", err) + os.Exit(1) + } + s := bufio.NewScanner(bytes.NewReader(out)) + s.Split(bufio.ScanWords) + for s.Scan() { + name := s.Text() + if !s.Scan() { + slog.Error("dpkg-query", "error", "parsing error: no version after name") + os.Exit(1) + } + packages[name] = s.Text() + } +} diff --git a/stdlib/os/debian/sources.list b/stdlib/os/debian/sources.list new file mode 100644 index 0000000..90397c6 --- /dev/null +++ b/stdlib/os/debian/sources.list @@ -0,0 +1,6 @@ +deb https://deb.debian.org/debian {{ var "debian-release" }} main contrib non-free non-free-firmware +deb https://deb.debian.org/debian {{ var "debian-release" }}-backports main contrib non-free non-free-firmware +deb https://deb.debian.org/debian {{ var "debian-release" }}-updates main contrib non-free non-free-firmware +deb https://security.debian.org/debian-security {{ var "debian-release" }}-security main contrib non-free non-free-firmware + +{{ var "debian-extra-sources" }} -- cgit v1.2.3