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: detected debian")
32 installed, err := s.debianInstalledPackages()
37 s.log.Debugf("packages: checking %s",
38 strings.Join(s.req.Packages, " "))
40 for _, x := range s.req.Packages {
42 install = append(install, x)
45 if len(install) == 0 {
49 for _, x := range install {
50 s.resp.PackageChanges = append(s.resp.PackageChanges,
60 s.log.Verbosef("packages: installing %s", strings.Join(install, " "))
61 cmd := exec.Command("/usr/bin/apt-get", append([]string{"install",
62 // Don't require further acknowledgment; this won't perform
65 // Don't perform upgrades
68 "--no-install-recommends",
69 // Don't overwrite existing config files
70 "-o", "Dpkg::Options::=--force-confdef",
71 "-o", "Dpkg::Options::=--force-confold",
73 cmd.Env = append(os.Environ(),
74 // Don't ask questions during installation
75 "DEBIAN_FRONTEND=noninteractive",
77 _, err = s.cmd.CombinedOutputCmd("packages", cmd)
85 func (s *Sync) debianInstalledPackages() (map[string]bool, error) {
86 out, _, err := s.cmd.Run("packages",
87 "/usr/bin/dpkg-query",
89 `--showformat=${Status}\t${Package}\n`,
94 lines := strings.Split(strings.TrimSpace(string(out)), "\n")
96 res := make(map[string]bool)
97 for _, line := range lines {
98 xs := strings.Split(line, "\t")
100 return nil, fmt.Errorf("invalid dpkg-query line %q",
103 // We only care if the package is currently successfully
104 // installed (last two fields). If a package is on hold (first
105 // field) this is fine as well.
106 if strings.HasSuffix(xs[0], " ok installed") {