Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for headers artifacts #385

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type Artifacts struct {
// the [ImageConfig].
Links []ArtifactSymlinkConfig `yaml:"links,omitempty" json:"links,omitempty"`

// Headers is a list of header files and/or folders to be installed.
// On linux this would typically be installed to /usr/include/.
Headers map[string]ArtifactConfig `yaml:"headers,omitempty" json:"headers,omitempty"`

// TODO: other types of artifacts (libexec, etc)
}

Expand Down Expand Up @@ -135,5 +139,8 @@ func (a *Artifacts) IsEmpty() bool {
if len(a.Links) > 0 {
return false
}
if len(a.Headers) > 0 {
return false
}
return true
}
7 changes: 7 additions & 0 deletions docs/spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@
},
"type": "array",
"description": "Links is the list of symlinks to be installed with the package\nLinks should only be used if the *package* should contain the link.\nFor making a container compatible with another image, use [PostInstall] in\nthe [ImageConfig]."
},
"headers": {
"additionalProperties": {
"$ref": "#/$defs/ArtifactConfig"
},
"type": "object",
"description": "Headers is a list of header files and/or folders to be installed.\nOn linux this would typically be installed to /usr/include/."
}
},
"additionalProperties": false,
Expand Down
28 changes: 19 additions & 9 deletions frontend/deb/debroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
"io"
"path/filepath"
Expand All @@ -13,6 +12,8 @@ import (
"strings"
"sync"

_ "embed"

"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend/pkg/bkfs"
"github.com/moby/buildkit/client/llb"
Expand All @@ -25,6 +26,9 @@ const customSystemdPostinstFile = "custom_systemd_postinst.sh.partial"
//go:embed templates/patch-header.txt
var patchHeader []byte

//go:embed templates/debian_install_header.sh
var debianInstall []byte

// This creates a directory in the debian root directory for each patch, and copies the patch files into it.
// The format for each patch dir matches what would normaly be under `debian/patches`, just that this is a separate dir for every source we are patching
// This is purely for documenting in the source package how patches are applied in a more readable way than the big merged patch file.
Expand Down Expand Up @@ -276,26 +280,23 @@ func createInstallScripts(worker llb.State, spec *dalec.Spec, dir string) []llb.
return nil
}

states := make([]llb.State, 0, len(spec.Artifacts.Binaries)+len(spec.Artifacts.Manpages))
states := make([]llb.State, 1)
base := llb.Scratch().File(llb.Mkdir(dir, 0o755, llb.WithParents(true)))

installBuf := bytes.NewBuffer(nil)
writeInstallHeader := sync.OnceFunc(func() {
fmt.Fprintln(installBuf, "#!/usr/bin/dh-exec")
fmt.Fprintln(installBuf)
fmt.Fprintln(installBuf, string(debianInstall))
})

writeInstall := func(src, dir, name string) {
// This is wrapped in a sync.OnceFunc so that this only has an effect the
// first time it is called.
writeInstallHeader()

if filepath.Base(src) != name {
fmt.Fprintln(installBuf, src, "=>", filepath.Join(dir, name))
return
}
name = strings.TrimSuffix(name, "*")
dest := filepath.Join("debian", spec.Name, dir, name)
fmt.Fprintln(installBuf, "do_install", filepath.Dir(dest), dest, src)

fmt.Fprintln(installBuf, src, dir+"/")
}

if len(spec.Artifacts.Binaries) > 0 {
Expand Down Expand Up @@ -387,6 +388,15 @@ func createInstallScripts(worker llb.State, spec *dalec.Spec, dir string) []llb.
}
}

if len(spec.Artifacts.Headers) > 0 {
sorted := dalec.SortMapKeys(spec.Artifacts.Headers)
for _, key := range sorted {
cfg := spec.Artifacts.Headers[key]
resolved := cfg.ResolveName(key)
writeInstall(key, filepath.Join("/usr/include", cfg.SubPath), resolved)
}
}

if units := spec.Artifacts.Systemd.GetUnits(); len(units) > 0 {
// deb-systemd will look for service files in DEBIAN/<package-name>[.<service-name>].<unit-type>
// To handle this we'll create symlinks to the actual unit files in the source.
Expand Down
25 changes: 25 additions & 0 deletions frontend/deb/templates/debian_install_header.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -eux

do_install() {
local parent="${1}"
shift

local dest="${1}"
shift

mkdir -p "${parent}"

local files=($@)

# When the number of files passed in is more than 1, then dest *must* refer
# to a directory
if test ${#files[@]} -gt 1; then
mkdir -p "${dest}"
fi

for src in ${files[@]}; do
cp --reflink=auto -a "${src}" "${dest}"
done
}
15 changes: 15 additions & 0 deletions frontend/rpm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ func (w *specWrapper) Install() fmt.Stringer {
fmt.Fprintln(b, "ln -sf", l.Source, "%{buildroot}/"+l.Dest)
}

headersKeys := dalec.SortMapKeys(w.Spec.Artifacts.Headers)
for _, h := range headersKeys {
cfg := w.Spec.Artifacts.Headers[h]
copyArtifact(`%{buildroot}/%{_includedir}`, h, &cfg)
}

b.WriteString("\n")
return b
}
Expand Down Expand Up @@ -673,6 +679,15 @@ func (w *specWrapper) Files() fmt.Stringer {
fmt.Fprintln(b, l.Dest)
}

if len(w.Spec.Artifacts.Headers) > 0 {
headersKeys := dalec.SortMapKeys(w.Spec.Artifacts.Headers)
for _, h := range headersKeys {
hf := w.Spec.Artifacts.Headers[h]
path := filepath.Join(`%{_includedir}`, hf.SubPath, hf.ResolveName(h))
fmt.Fprintln(b, path)
}
}

b.WriteString("\n")
return b
}
Expand Down
41 changes: 41 additions & 0 deletions frontend/rpm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,47 @@ fi
want := `%files
%license %{_licensedir}/test-pkg/licenses/LICENSE.md

`
assert.Equal(t, want, got)
})

t.Run("test headers templating using defaults", func(t *testing.T) {
t.Parallel()
w := &specWrapper{Spec: &dalec.Spec{
Name: "test-pkg",
Artifacts: dalec.Artifacts{
Headers: map[string]dalec.ArtifactConfig{
"test-headers": {},
},
},
}}

got := w.Files().String()
want := `%files
%{_includedir}/test-headers

`
assert.Equal(t, want, got)
})

t.Run("test headers templating using ArtifactConfig", func(t *testing.T) {
t.Parallel()
w := &specWrapper{Spec: &dalec.Spec{
Name: "test-pkg",
Artifacts: dalec.Artifacts{
Headers: map[string]dalec.ArtifactConfig{
"test-headers": {
Name: "sub-module-headers",
SubPath: "sub-module",
},
},
},
}}

got := w.Files().String()
want := `%files
%{_includedir}/sub-module/sub-module-headers

`
assert.Equal(t, want, got)
})
Expand Down
Loading