]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/packages_debian.go
sync: remove "detected" log message in packages/services
[safcm/safcm.git] / cmd / safcm-remote / sync / packages_debian.go
1 // MsgSyncReq: install packages on the remote host (Debian)
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
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.
9 //
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.
14 //
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/>.
17
18 package sync
19
20 import (
21         "fmt"
22         "os"
23         "os/exec"
24         "strings"
25
26         "ruderich.org/simon/safcm"
27 )
28
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()
33         if err != nil {
34                 return err
35         }
36         var install []string
37         for _, x := range s.req.Packages {
38                 if !installed[x] {
39                         install = append(install, x)
40                 }
41         }
42         if len(install) == 0 {
43                 return nil
44         }
45
46         for _, x := range install {
47                 s.resp.PackageChanges = append(s.resp.PackageChanges,
48                         safcm.PackageChange{
49                                 Name: x,
50                         })
51         }
52
53         if s.req.DryRun {
54                 return nil
55         }
56
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
60                 // dangerous actions
61                 "--assume-yes",
62                 // Don't perform upgrades
63                 "--no-upgrade",
64                 // Nobody needs those
65                 "--no-install-recommends",
66                 // Don't overwrite existing config files
67                 "-o", "Dpkg::Options::=--force-confdef",
68                 "-o", "Dpkg::Options::=--force-confold",
69         }, install...)...)
70         cmd.Env = append(os.Environ(),
71                 // Don't ask questions during installation
72                 "DEBIAN_FRONTEND=noninteractive",
73         )
74         _, err = s.cmd.CombinedOutputCmd("packages", cmd)
75         if err != nil {
76                 return err
77         }
78
79         return nil
80 }
81
82 func (s *Sync) debianInstalledPackages() (map[string]bool, error) {
83         out, _, err := s.cmd.Run("packages",
84                 "/usr/bin/dpkg-query",
85                 "--show",
86                 `--showformat=${Status}\t${Package}\n`,
87         )
88         if err != nil {
89                 return nil, err
90         }
91         lines := strings.Split(strings.TrimSpace(string(out)), "\n")
92
93         res := make(map[string]bool)
94         for _, line := range lines {
95                 xs := strings.Split(line, "\t")
96                 if len(xs) != 2 {
97                         return nil, fmt.Errorf("invalid dpkg-query line %q",
98                                 line)
99                 }
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") {
104                         res[xs[1]] = true
105                 }
106         }
107         return res, nil
108 }