1 // MsgSyncReq: install packages on the remote host (Debian)
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 "ruderich.org/simon/safcm"
29 func (s *Sync) syncPackagesDebian() error {
30 s.log.Debugf("packages: checking %s (debian detected)",
31 strings.Join(s.req.Packages, " "))
32 installed, err := s.debianInstalledPackages()
37 for _, x := range s.req.Packages {
39 install = append(install, x)
42 if len(install) == 0 {
46 for _, x := range install {
47 s.resp.PackageChanges = append(s.resp.PackageChanges,
57 s.log.Verbosef("packages: installing %s", strings.Join(install, " "))
58 cmd := exec.Command("/usr/bin/apt-get", append([]string{"install",
59 // Don't require further acknowledgment; this won't perform
62 // Don't perform upgrades
65 "--no-install-recommends",
66 // Don't overwrite existing config files
67 "-o", "Dpkg::Options::=--force-confdef",
68 "-o", "Dpkg::Options::=--force-confold",
70 cmd.Env = append(os.Environ(),
71 // Don't ask questions during installation
72 "DEBIAN_FRONTEND=noninteractive",
74 _, err = s.cmd.CombinedOutputCmd("packages", cmd)
82 func (s *Sync) debianInstalledPackages() (map[string]bool, error) {
83 out, _, err := s.cmd.Run("packages",
84 "/usr/bin/dpkg-query",
86 `--showformat=${Status}\t${Package}\n`,
91 lines := strings.Split(strings.TrimSpace(string(out)), "\n")
93 res := make(map[string]bool)
94 for _, line := range lines {
95 xs := strings.Split(line, "\t")
97 return nil, fmt.Errorf("invalid dpkg-query line %q",
100 // We only care if the package is currently successfully
101 // installed (last two fields). If a package is on hold (first
102 // field) this is fine as well.
103 if strings.HasSuffix(xs[0], " ok installed") {