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
190 changes: 55 additions & 135 deletions dev-tools/mage/checksums.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/otiai10/copy"

"github.com/elastic/elastic-agent/dev-tools/mage/manifest"
"github.com/elastic/elastic-agent/dev-tools/packaging"
)

const ComponentSpecFileSuffix = ".spec.yml"
Expand Down Expand Up @@ -94,157 +95,76 @@ func ChecksumsWithoutManifest(versionedFlatPath string, versionedDropPath string
}

// This is a helper function for flattenDependencies that's used when building from a manifest
func ChecksumsWithManifest(requiredPackage string, versionedFlatPath string, versionedDropPath string, manifestResponse *manifest.Build) map[string]string {
func ChecksumsWithManifest(platform, dependenciesVersion string, versionedFlatPath string, versionedDropPath string, manifestResponse *manifest.Build) map[string]string {
checksums := make(map[string]string)
if manifestResponse == nil {
return checksums
}

// Iterate over the component projects in the manifest
projects := manifestResponse.Projects
for componentName := range projects {
// Iterate over the individual package files within each component project
for pkgName := range projects[componentName].Packages {
// Only care about packages that match the required package constraint (os/arch)
if strings.Contains(pkgName, requiredPackage) {
// Iterate over the external binaries that we care about for packaging agent
for _, spec := range manifest.ExpectedBinaries {
// If the individual package doesn't match the expected prefix, then continue
// FIXME temporarily skip fips packages until elastic-agent FIPS is in place
if !strings.HasPrefix(pkgName, spec.BinaryName) || strings.Contains(pkgName, "-fips-") {
if mg.Verbose() {
log.Printf(">>>>>>> Package [%s] skipped", pkgName)
}
continue
}

if mg.Verbose() {
log.Printf(">>>>>>> Package [%s] matches requiredPackage [%s]", pkgName, requiredPackage)
}

// Get the version from the component based on the version in the package name
// This is useful in the case where it's an Independent Agent Release, where
// the opted-in projects will be one patch version ahead of the rest of the
// opted-out/previously-released projects
componentVersion := getComponentVersion(componentName, requiredPackage, projects[componentName])
if mg.Verbose() {
log.Printf(">>>>>>> Component [%s]/[%s] version is [%s]", componentName, requiredPackage, componentVersion)
}

// Combine the package name w/ the versioned flat path
fullPath := filepath.Join(versionedFlatPath, pkgName)

// Eliminate the file extensions to get the proper directory
// name that we need to copy
var dirToCopy string
if strings.HasSuffix(fullPath, ".tar.gz") {
dirToCopy = fullPath[:strings.LastIndex(fullPath, ".tar.gz")]
} else if strings.HasSuffix(fullPath, ".zip") {
dirToCopy = fullPath[:strings.LastIndex(fullPath, ".zip")]
} else {
dirToCopy = fullPath
}
if mg.Verbose() {
log.Printf(">>>>>>> Calculated directory to copy: [%s]", dirToCopy)
}

// Set copy options
options := copy.Options{
OnSymlink: func(_ string) copy.SymlinkAction {
return copy.Shallow
},
Sync: true,
}
if mg.Verbose() {
log.Printf("> prepare to copy %s into %s ", dirToCopy, versionedDropPath)
}

// Do the copy
err := copy.Copy(dirToCopy, versionedDropPath, options)
if err != nil {
panic(err)
}

// copy spec file for match
specName := filepath.Base(dirToCopy)
idx := strings.Index(specName, "-"+componentVersion)
if idx != -1 {
specName = specName[:idx]
}
if mg.Verbose() {
log.Printf(">>>> Looking to copy spec file: [%s]", specName)
}

checksum, err := CopyComponentSpecs(specName, versionedDropPath)
if err != nil {
panic(err)
}

checksums[specName+ComponentSpecFileSuffix] = checksum
}
// Iterate over the external binaries that we care about for packaging agent
for _, spec := range packaging.ExpectedBinaries {

if spec.PythonWheel {
if mg.Verbose() {
log.Printf(">>>>>>> Component %s/%s is a Python wheel, skipping", spec.ProjectName, spec.BinaryName)
}
continue
}
}

return checksums
}
if !spec.SupportsPlatform(platform) {
log.Printf(">>>>>>> Component %s/%s does not support platform %s, skipping", spec.ProjectName, spec.BinaryName, platform)
continue
}

// This function is used when building with a Manifest. In that manifest, it's possible
// for projects in an Independent Agent Release to have different versions since the opted-in
// ones will be one patch version higher than the opted-out/previously released projects.
// This function tries to find the versions from the package name
func getComponentVersion(componentName string, requiredPackage string, componentProject manifest.Project) string {
var componentVersion string
var foundIt bool
// Iterate over all the packages in the component project
for pkgName := range componentProject.Packages {
// Only care about the external binaries that we want to package
for _, spec := range manifest.ExpectedBinaries {
// If the given component name doesn't match the external binary component, skip
// FIXME temporarily skip fips packages until elastic-agent FIPS is in place
if componentName != spec.ProjectName || strings.Contains(pkgName, "-fips-") {
continue
manifestPackage, err := manifest.ResolveManifestPackage(manifestResponse.Projects[spec.ProjectName], spec, dependenciesVersion, platform)
if err != nil {
if mg.Verbose() {
log.Printf(">>>>>>> Error resolving package for [%s/%s]", spec.BinaryName, platform)
}
continue
}

// Split the package name on the binary name prefix plus a dash
firstSplit := strings.Split(pkgName, spec.BinaryName+"-")
if len(firstSplit) < 2 {
continue
}
// Combine the package name w/ the versioned flat path
fullPath := filepath.Join(versionedFlatPath, manifestPackage.Name)

// Eliminate the file extensions to get the proper directory
// name that we need to copy
var dirToCopy string
if strings.HasSuffix(fullPath, ".tar.gz") {
dirToCopy = fullPath[:strings.LastIndex(fullPath, ".tar.gz")]
} else if strings.HasSuffix(fullPath, ".zip") {
dirToCopy = fullPath[:strings.LastIndex(fullPath, ".zip")]
} else {
dirToCopy = fullPath
}
if mg.Verbose() {
log.Printf(">>>>>>> Calculated directory to copy: [%s]", dirToCopy)
}

// Get the second part of the first split
secondHalf := firstSplit[1]
if len(secondHalf) < 2 {
continue
}
// Set copy options
options := copy.Options{
OnSymlink: func(_ string) copy.SymlinkAction {
return copy.Shallow
},
Sync: true,
}
if mg.Verbose() {
log.Printf("> prepare to copy %s into %s ", dirToCopy, versionedDropPath)
}

// Make sure the second half matches the required package
if strings.Contains(secondHalf, requiredPackage) {
// ignore packages with names where this splitting doesn't results in proper version
if strings.Contains(secondHalf, "docker-image") {
continue
}
if strings.Contains(secondHalf, "oss-") {
continue
}

// The component version should be the first entry after splitting w/ the requiredPackage
componentVersion = strings.Split(secondHalf, "-"+requiredPackage)[0]
foundIt = true
// break out of inner loop
break
}
// Do the copy
err = copy.Copy(dirToCopy, versionedDropPath, options)
if err != nil {
panic(err)
}
if foundIt {
// break out of outer loop
break

checksum, err := CopyComponentSpecs(spec.BinaryName, versionedDropPath)
if err != nil {
panic(err)
}
}

if componentVersion == "" {
errMsg := fmt.Sprintf("Unable to determine component version for [%s]", componentName)
panic(errMsg)
checksums[spec.BinaryName+ComponentSpecFileSuffix] = checksum
}

return componentVersion
return checksums
}
Loading
Loading