forked from canonical/concierge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeb_handler.go
More file actions
109 lines (87 loc) · 2.55 KB
/
deb_handler.go
File metadata and controls
109 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package packages
import (
"fmt"
"log/slog"
"github.com/canonical/concierge/internal/system"
)
// NewDeb constructs a new Deb instance.
func NewDeb(name string) *Deb {
return &Deb{Name: name}
}
// Deb is a simple representation of a package installed from the Ubuntu archive.
type Deb struct {
Name string
}
// NewDebHandler constructs a new instance of a DebHandler.
func NewDebHandler(system system.Worker, debs []*Deb) *DebHandler {
return &DebHandler{
Debs: debs,
system: system,
}
}
// DebHandler can install or remove a set of debs.
type DebHandler struct {
Debs []*Deb
system system.Worker
}
// Prepare updates the apt cache and installs a set of debs from the archive.
func (h *DebHandler) Prepare() error {
if len(h.Debs) == 0 {
return nil
}
err := h.updateAptCache()
if err != nil {
return fmt.Errorf("failed to update apt cache: %w", err)
}
for _, deb := range h.Debs {
err := h.installDeb(deb)
if err != nil {
return fmt.Errorf("failed to install deb: %w", err)
}
}
return nil
}
// Restore removes a set of debs from the machine.
func (h *DebHandler) Restore() error {
for _, deb := range h.Debs {
err := h.removeDeb(deb)
if err != nil {
return fmt.Errorf("failed to remove deb: %w", err)
}
}
cmd := system.NewCommand("apt-get", []string{"autoremove", "-y"})
_, err := h.system.Run(cmd, system.Exclusive())
if err != nil {
return fmt.Errorf("failed to install apt package: %w", err)
}
return nil
}
// installDeb uses `apt` to install the package on the system from the archives.
func (h *DebHandler) installDeb(d *Deb) error {
cmd := system.NewCommand("apt-get", []string{"install", "-y", d.Name})
_, err := h.system.Run(cmd, system.Exclusive())
if err != nil {
return fmt.Errorf("failed to install apt package '%s': %w", d.Name, err)
}
slog.Info("Installed apt package", "package", d.Name)
return nil
}
// Remove uninstalls the deb from the system with `apt`.
func (h *DebHandler) removeDeb(d *Deb) error {
cmd := system.NewCommand("apt-get", []string{"remove", "-y", d.Name})
_, err := h.system.Run(cmd, system.Exclusive())
if err != nil {
return fmt.Errorf("failed to remove apt package '%s': %w", d.Name, err)
}
slog.Info("Removed apt package", "package", d.Name)
return nil
}
// updateAptCache is a helper method to update the host's package cache.
func (h *DebHandler) updateAptCache() error {
cmd := system.NewCommand("apt-get", []string{"update"})
_, err := h.system.Run(cmd, system.Exclusive())
if err != nil {
return fmt.Errorf("failed to update apt package lists: %w", err)
}
return nil
}