Skip to content
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
8 changes: 8 additions & 0 deletions dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (
"fmt"
"io"
"io/fs"
"log"
"maps"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/magefile/mage/mg"

"github.com/elastic/elastic-agent/internal/pkg/agent/install"
"github.com/elastic/elastic-agent/pkg/component"

Expand Down Expand Up @@ -270,6 +273,11 @@ func (b *dockerBuilder) dockerSave(tag string, templateExtraArgs ...map[string]i
}
outputFile = filepath.Join(distributionsDir, outputTar)
}

if mg.Verbose() {
log.Printf(">>>> saving docker image %s to %s", tag, outputFile)
}

var stderr bytes.Buffer
cmd := exec.Command("docker", "save", tag)
cmd.Stderr = &stderr
Expand Down
70 changes: 48 additions & 22 deletions dev-tools/mage/pkgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"slices"
"strconv"
"strings"
"text/template"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -88,27 +89,28 @@ type OSPackageArgs struct {

// PackageSpec specifies package metadata and the contents of the package.
type PackageSpec struct {
Name string `yaml:"name,omitempty"`
ServiceName string `yaml:"service_name,omitempty"`
OS string `yaml:"os,omitempty"`
Arch string `yaml:"arch,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Snapshot bool `yaml:"snapshot"`
FIPS bool `yaml:"fips"`
Version string `yaml:"version,omitempty"`
License string `yaml:"license,omitempty"`
URL string `yaml:"url,omitempty"`
Description string `yaml:"description,omitempty"`
DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
PreInstallScript string `yaml:"pre_install_script,omitempty"`
PostInstallScript string `yaml:"post_install_script,omitempty"`
PostRmScript string `yaml:"post_rm_script,omitempty"`
Files map[string]PackageFile `yaml:"files"`
Qualifier string `yaml:"qualifier,omitempty"` // Optional
OutputFile string `yaml:"output_file,omitempty"` // Optional
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
ExtraTags []string `yaml:"extra_tags,omitempty"` // Optional
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
Name string `yaml:"name,omitempty"`
ServiceName string `yaml:"service_name,omitempty"`
OS string `yaml:"os,omitempty"`
Arch string `yaml:"arch,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Snapshot bool `yaml:"snapshot"`
FIPS bool `yaml:"fips"`
Version string `yaml:"version,omitempty"`
License string `yaml:"license,omitempty"`
URL string `yaml:"url,omitempty"`
Description string `yaml:"description,omitempty"`
DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
DockerImageNameTemplate string `yaml:"docker_image_name_template,omitempty"` // Optional: template of the docker image name
PreInstallScript string `yaml:"pre_install_script,omitempty"`
PostInstallScript string `yaml:"post_install_script,omitempty"`
PostRmScript string `yaml:"post_rm_script,omitempty"`
Files map[string]PackageFile `yaml:"files"`
Qualifier string `yaml:"qualifier,omitempty"` // Optional
OutputFile string `yaml:"output_file,omitempty"` // Optional
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
ExtraTags []string `yaml:"extra_tags,omitempty"` // Optional
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package

evalContext map[string]interface{}
packageDir string
Expand Down Expand Up @@ -488,6 +490,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {

// ImageName computes the image name from the spec.
func (s PackageSpec) ImageName() string {
if s.DockerImageNameTemplate != "" {
imageNameTmpl, err := template.New("dockerImageTemplate").Parse(s.DockerImageNameTemplate)
if err != nil {
panic(fmt.Errorf("parsing docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
}

data := s.toMap()
for k, v := range varMap() {
data[k] = v
}

buf := new(strings.Builder)
err = imageNameTmpl.Execute(buf, data)
if err != nil {
panic(fmt.Errorf("rendering docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
}

imageName := buf.String()
if mg.Verbose() {
log.Printf("rendered image name for %s variant %s: %s", s.Name, s.DockerVariant, imageName)
}
return imageName
}

if s.DockerVariant == Basic {
return s.Name
}
Expand Down Expand Up @@ -791,7 +817,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
args = append(args, "--vendor", spec.Vendor)
}
if spec.License != "" {
args = append(args, "--license", strings.Replace(spec.License, " ", "-", -1))
args = append(args, "--license", strings.ReplaceAll(spec.License, " ", "-"))
}
if spec.Description != "" {
args = append(args, "--description", spec.Description)
Expand Down
41 changes: 40 additions & 1 deletion dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ shared:

- &agent_docker_spec
<<: *agent_binary_spec
# Default docker image name is <spec name>-<docker variant>
extra_vars:
dockerfile: 'Dockerfile.elastic-agent.tmpl'
docker_entrypoint: 'docker-entrypoint.elastic-agent.tmpl'
Expand Down Expand Up @@ -1551,7 +1552,6 @@ specs:
source: data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}}
symlink: true
mode: 0755

- os: linux
arch: amd64
types: [ docker ]
Expand All @@ -1578,6 +1578,40 @@ specs:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
- os: linux
arch: amd64
types: [ docker ]
spec:
<<: *docker_fips_spec
<<: *agent_docker_fips_spec
# The cloud image is always based on Wolfi
<<: *docker_builder_spec
<<: *agent_docker_cloud_fips_spec
<<: *elastic_license_for_binaries
name: "elastic-agent-fips-cloud"
docker_image_name_template: "{{.BeatName}}-fips-cloud"
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
- os: linux
arch: arm64
types: [ docker ]
spec:
<<: *docker_fips_spec
<<: *agent_docker_fips_spec
# The cloud image is always based on Wolfi
<<: *docker_builder_arm_spec
<<: *agent_docker_cloud_fips_spec
<<: *elastic_license_for_binaries
name: "elastic-agent-fips-cloud"
docker_image_name_template: "{{.BeatName}}-fips-cloud"
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

- os: linux
arch: amd64
types: [ docker ]
Expand All @@ -1588,9 +1622,12 @@ specs:
<<: *docker_builder_spec
<<: *agent_docker_cloud_fips_spec
<<: *elastic_license_for_binaries
name: "elastic-agent-cloud-fips"
docker_image_name_template: "{{.BeatName}}-cloud-fips"
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

- os: linux
arch: arm64
types: [ docker ]
Expand All @@ -1601,6 +1638,8 @@ specs:
<<: *docker_builder_arm_spec
<<: *agent_docker_cloud_fips_spec
<<: *elastic_license_for_binaries
name: "elastic-agent-cloud-fips"
docker_image_name_template: "{{.BeatName}}-cloud-fips"
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
Expand Down
Loading