Skip to content

Commit f37540c

Browse files
committed
cli: improve on update command
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
1 parent 4fb057a commit f37540c

File tree

3 files changed

+102
-10
lines changed

3 files changed

+102
-10
lines changed

environment/container/docker/docker.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/abiosoft/colima/cli"
88
"github.com/abiosoft/colima/config"
99
"github.com/abiosoft/colima/environment"
10+
"github.com/abiosoft/colima/util/debutil"
1011
)
1112

1213
// Name is container runtime name.
@@ -135,9 +136,11 @@ func (d dockerRuntime) Version(ctx context.Context) string {
135136
}
136137

137138
func (d *dockerRuntime) Update(ctx context.Context) error {
138-
return d.guest.Run(
139-
"sh",
140-
"-c",
141-
"sudo apt-get -qq update -y && sudo apt-get -qq install -y --allow-change-held-packages docker-ce docker-ce-cli containerd.io",
142-
)
139+
packages := []string{
140+
"docker-ce",
141+
"docker-ce-cli",
142+
"containerd.io",
143+
}
144+
145+
return debutil.UpdateRuntime(ctx, d.guest, d, Name, packages...)
143146
}

environment/container/incus/incus.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/abiosoft/colima/environment"
1515
"github.com/abiosoft/colima/environment/vm/lima/limautil"
1616
"github.com/abiosoft/colima/util"
17+
"github.com/abiosoft/colima/util/debutil"
1718
)
1819

1920
const incusBridgeInterface = "incusbr0"
@@ -280,9 +281,13 @@ type networkInfo struct {
280281
}
281282

282283
func (c *incusRuntime) Update(ctx context.Context) error {
283-
return c.guest.Run(
284-
"sh",
285-
"-c",
286-
"sudo apt-get -qq update -y && sudo apt-get -qq install -y --allow-change-held-packages incus incus-base incus-client incus-extra incus-ui-canonical",
287-
)
284+
packages := []string{
285+
"incus",
286+
"incus-base",
287+
"incus-client",
288+
"incus-extra",
289+
"incus-ui-canonical",
290+
}
291+
292+
return debutil.UpdateRuntime(ctx, c.guest, c, Name, packages...)
288293
}

util/debutil/debutil.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package debutil
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/abiosoft/colima/cli"
9+
"github.com/abiosoft/colima/environment"
10+
)
11+
12+
// packages is list of deb package names.
13+
type packages []string
14+
15+
// Upgradable returns the shell command to check if the packages are upgradable with apt.
16+
// The returned command should be passed to 'sh -c' or equivalent.
17+
func (p packages) Upgradable() string {
18+
cmd := "sudo apt list --upgradable | grep"
19+
for _, v := range p {
20+
cmd += fmt.Sprintf(" -e '^%s/'", v)
21+
}
22+
return cmd
23+
}
24+
25+
// Install returns the shell command to install the packages with apt.
26+
// The returned command should be passed to 'sh -c' or equivalent.
27+
func (p packages) Install() string {
28+
return "sudo apt-get install -y --allow-change-held-packages " + strings.Join(p, " ")
29+
}
30+
31+
func UpdateRuntime(
32+
ctx context.Context,
33+
guest environment.GuestActions,
34+
chain cli.CommandChain,
35+
runtime string,
36+
packageNames ...string,
37+
) error {
38+
a := chain.Init(ctx)
39+
log := a.Logger()
40+
41+
packages := packages(packageNames)
42+
43+
updatesAvailable := false
44+
45+
a.Stage("refreshing package manager")
46+
a.Add(func() error {
47+
return guest.RunQuiet(
48+
"sh",
49+
"-c",
50+
"sudo apt-get update -y",
51+
)
52+
})
53+
54+
a.Stage("checking for updates")
55+
a.Add(func() error {
56+
err := guest.RunQuiet(
57+
"sh",
58+
"-c",
59+
packages.Upgradable(),
60+
)
61+
updatesAvailable = err == nil
62+
return nil
63+
})
64+
65+
a.Add(func() (err error) {
66+
if !updatesAvailable {
67+
log.Warnf("no updates available for %s runtime", runtime)
68+
return
69+
}
70+
71+
log.Println("updating packages ...")
72+
err = guest.RunQuiet(
73+
"sh",
74+
"-c",
75+
packages.Install(),
76+
)
77+
if err == nil {
78+
log.Println("done")
79+
}
80+
return
81+
})
82+
83+
return a.Exec()
84+
}

0 commit comments

Comments
 (0)