Skip to content

Commit e764a9d

Browse files
committed
actions: apt-file: initial implementation of action to install local debian packages
The apt-file action allows local .deb packages to be installed using the `apt` command, much like the apt action but for local packages rather than those from apt repositories. Resolves: #157 Closes: #165 Signed-off-by: Christopher Obbard <[email protected]>
1 parent ee15bc4 commit e764a9d

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ of each other.
3131

3232
Some of the actions provided by debos to customize and produce images are:
3333

34+
* apt-file: installs packages and their dependencies from local 'deb' files
3435
* apt: install packages and their dependencies with 'apt'
3536
* debootstrap: construct the target rootfs with debootstrap
3637
* download: download a single file from the internet

actions/apt_file_action.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
AptFile Action
3+
4+
Install packages from .deb files and their dependencies to the target rootfs
5+
with 'apt'.
6+
7+
Yaml syntax:
8+
- action: apt-file
9+
origin: name
10+
recommends: bool
11+
unauthenticated: bool
12+
packages:
13+
- package1
14+
- package2
15+
16+
Mandatory properties:
17+
18+
- packages -- list of packages to install. Resolves Unix-style glob patterns.
19+
20+
Optional properties:
21+
22+
- origin -- reference to named file or directory. Defaults to recipe directory.
23+
24+
- recommends -- boolean indicating if suggested packages will be installed. Defaults to false.
25+
26+
- unauthenticated -- boolean indicating if unauthenticated packages can be installed. Defaults to false.
27+
28+
29+
Example to install named packages in a subdirectory under `debs/`:
30+
31+
- action: apt-file
32+
description: Test install from file
33+
packages:
34+
- pkgs/bmap-tools_*_all.deb
35+
- pkgs/fakemachine_*_amd64.deb
36+
37+
38+
Example to download and install a package:
39+
40+
- action: download
41+
url: http://ftp.us.debian.org/debian/pool/main/b/bmap-tools/bmap-tools_3.5-2_all.deb
42+
name: bmap-tools-pkg
43+
44+
- action: apt-file
45+
description: Test install from download
46+
origin: bmap-tools-pkg
47+
48+
*/
49+
package actions
50+
51+
import (
52+
"fmt"
53+
"os"
54+
"path"
55+
"path/filepath"
56+
"strings"
57+
"github.com/go-debos/debos"
58+
)
59+
60+
type AptFileAction struct {
61+
debos.BaseAction `yaml:",inline"`
62+
Recommends bool
63+
Unauthenticated bool
64+
Origin string
65+
Packages []string
66+
}
67+
68+
func (apt *AptFileAction) Run(context *debos.DebosContext) error {
69+
apt.LogStart()
70+
var origin string
71+
aptOptions := []string{"apt", "-oDpkg::Progress-Fancy=0", "--yes"}
72+
pkgs := []string{}
73+
74+
c := debos.NewChrootCommandForContext(*context)
75+
c.AddEnv("DEBIAN_FRONTEND=noninteractive")
76+
77+
// get the full path of a named origin
78+
if len(apt.Origin) > 0 {
79+
var found bool
80+
if origin, found = context.Origins[apt.Origin]; !found {
81+
return fmt.Errorf("Origin not found '%s'", apt.Origin)
82+
}
83+
} else {
84+
// otherwise fallback to RecipeDir
85+
origin = context.RecipeDir
86+
}
87+
88+
/* create a list of full paths of packages to install: if the origin is a
89+
* single file (e.g download action) then just return that package, otherwise
90+
* append package name to the origin path and glob to create a list of packages */
91+
file, err := os.Stat(origin)
92+
if err != nil {
93+
return err
94+
}
95+
if file.IsDir() {
96+
if len(apt.Packages) == 0 {
97+
return fmt.Errorf("No packages defined")
98+
}
99+
100+
for _, pkg := range apt.Packages {
101+
// resolve globs
102+
source := path.Join(origin, pkg)
103+
matches, err := filepath.Glob(source)
104+
if err != nil {
105+
return err
106+
}
107+
if len(matches) == 0 {
108+
return fmt.Errorf("File(s) not found after globbing: %s", pkg)
109+
}
110+
111+
pkgs = append(pkgs, matches...)
112+
}
113+
} else {
114+
pkgs = append(pkgs, origin)
115+
}
116+
117+
/* bind mount each package into rootfs & update the list with the
118+
* path relative to the chroot */
119+
for idx, pkg := range pkgs {
120+
// check for duplicates after globbing
121+
for j := idx + 1; j < len(pkgs); j++ {
122+
if pkgs[j] == pkg {
123+
return fmt.Errorf("Duplicate package found: %s", pkg)
124+
}
125+
}
126+
127+
// only bind-mount if the package is outside the rootfs
128+
if strings.HasPrefix(pkg, context.Rootdir) {
129+
pkg = strings.TrimPrefix(pkg, context.Rootdir)
130+
} else {
131+
c.AddBindMount(pkg, "")
132+
}
133+
134+
// update pkgs with the resolved path
135+
pkgs[idx] = "." + pkg
136+
}
137+
138+
err = c.Run("apt-file", "apt-get", "update")
139+
if err != nil {
140+
return err
141+
}
142+
143+
if !apt.Recommends {
144+
aptOptions = append(aptOptions, "--no-install-recommends")
145+
}
146+
147+
if apt.Unauthenticated {
148+
aptOptions = append(aptOptions, "--allow-unauthenticated")
149+
}
150+
151+
aptOptions = append(aptOptions, "install")
152+
aptOptions = append(aptOptions, pkgs...)
153+
err = c.Run("apt-file", aptOptions...)
154+
if err != nil {
155+
return err
156+
}
157+
158+
err = c.Run("apt-file", "apt-get", "clean")
159+
if err != nil {
160+
return err
161+
}
162+
163+
return nil
164+
}

actions/recipe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Mandatory properties for receipt:
3737
3838
Supported actions
3939
40+
- apt-file -- https://godoc.org/github.com/go-debos/debos/actions#hdr-AptFile_Action
41+
4042
- apt -- https://godoc.org/github.com/go-debos/debos/actions#hdr-Apt_Action
4143
4244
- debootstrap -- https://godoc.org/github.com/go-debos/debos/actions#hdr-Debootstrap_Action
@@ -106,6 +108,8 @@ func (y *YamlAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
106108
y.Action = &UnpackAction{}
107109
case "run":
108110
y.Action = &RunAction{}
111+
case "apt-file":
112+
y.Action = &AptFileAction{}
109113
case "apt":
110114
y.Action = &AptAction{}
111115
case "ostree-commit":

actions/recipe_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestParse_syntax(t *testing.T) {
4949
architecture: arm64
5050
5151
actions:
52+
- action: apt-file
5253
- action: apt
5354
- action: debootstrap
5455
- action: download

0 commit comments

Comments
 (0)