|
| 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 | +} |
0 commit comments