]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/packages_debian.go
0dcbc4f4c571cfdadfbeb1b2a4c972c82c53b9fa
[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: detected debian")
31
32         installed, err := s.debianInstalledPackages()
33         if err != nil {
34                 return err
35         }
36
37         s.log.Debugf("packages: checking %s",
38                 strings.Join(s.req.Packages, " "))
39         var install []string
40         for _, x := range s.req.Packages {
41                 if !installed[x] {
42                         install = append(install, x)
43                 }
44         }
45         if len(install) == 0 {
46                 return nil
47         }
48
49         for _, x := range install {
50                 s.resp.PackageChanges = append(s.resp.PackageChanges,
51                         safcm.PackageChange{
52                                 Name: x,
53                         })
54         }
55
56         if s.req.DryRun {
57                 return nil
58         }
59
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
63                 // dangerous actions
64                 "--assume-yes",
65                 // Don't perform upgrades
66                 "--no-upgrade",
67                 // Nobody needs those
68                 "--no-install-recommends",
69                 // Don't overwrite existing config files
70                 "-o", "Dpkg::Options::=--force-confdef",
71                 "-o", "Dpkg::Options::=--force-confold",
72         }, install...)...)
73         cmd.Env = append(os.Environ(),
74                 // Don't ask questions during installation
75                 "DEBIAN_FRONTEND=noninteractive",
76         )
77         _, err = s.cmd.CombinedOutputCmd("packages", cmd)
78         if err != nil {
79                 return err
80         }
81
82         return nil
83 }
84
85 func (s *Sync) debianInstalledPackages() (map[string]bool, error) {
86         out, _, err := s.cmd.Run("packages",
87                 "/usr/bin/dpkg-query",
88                 "--show",
89                 `--showformat=${Status}\t${Package}\n`,
90         )
91         if err != nil {
92                 return nil, err
93         }
94         lines := strings.Split(strings.TrimSpace(string(out)), "\n")
95
96         res := make(map[string]bool)
97         for _, line := range lines {
98                 xs := strings.Split(line, "\t")
99                 if len(xs) != 2 {
100                         return nil, fmt.Errorf("invalid dpkg-query line %q",
101                                 line)
102                 }
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") {
107                         res[xs[1]] = true
108                 }
109         }
110         return res, nil
111 }