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 deb and rpm package upgrade validation #94

Merged
merged 14 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 12 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ import (
"github.com/urfave/cli/v2"
)

var ValidateUpgradeCommand = &cli.Command{
Name: "upgrade",
Action: PipelineAction(pipelines.ValidatePackageUpgrade),
Usage: "Validates if a .deb or a .rpm package (--from) can be upgraded by another .deb or .rpm package (--to)",
Flags: JoinFlagsWithDefault(
PackageInputFlags,
GCPFlags,
),
}

var ValidateCommand = &cli.Command{
Name: "validate",
Action: PipelineAction(pipelines.ValidatePackage),
Description: "Validates a grafana.tar.gz for the given distributions (--distro) placed in the destination directory (--destination)",
Description: "Validates grafana .tar.gz, .deb, .rpm and .docker.tar.gz packages and places the results in the destination directory (--destination)",
Subcommands: []*cli.Command{ValidateUpgradeCommand},
Flags: JoinFlagsWithDefault(
PackageInputFlags,
GrafanaFlags,
Expand Down
6 changes: 3 additions & 3 deletions pipelines/package_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func TarFilename(opts TarFileOpts) string {
return fmt.Sprintf("%s.tar.gz", strings.Join(p, "_"))
}

func TarOptsFromFileName(filename string) TarFileOpts {
filename = filepath.Base(filename)
func TarOptsFromFileName(path string) TarFileOpts {
filename := filepath.Base(path)
n := WithoutExt(filename)
components := strings.Split(n, "_")
if len(components) != 5 {
Expand All @@ -78,7 +78,7 @@ func TarOptsFromFileName(filename string) TarFileOpts {
// arm-7 should become arm/v7
arch = strings.Join([]string{archv[0], archv[1]}, "/")
}
edition := ""
edition := "oss"
guicaulada marked this conversation as resolved.
Show resolved Hide resolved
if n := strings.Split(name, "-"); len(n) != 1 {
edition = n[1]
}
Expand Down
91 changes: 90 additions & 1 deletion pipelines/package_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"path/filepath"
"strings"

"dagger.io/dagger"
Expand Down Expand Up @@ -54,6 +55,19 @@ func ValidatePackage(ctx context.Context, d *dagger.Client, src *dagger.Director
return grp.Wait()
}

func ValidatePackageUpgrade(ctx context.Context, d *dagger.Client, src *dagger.Directory, args PipelineArgs) error {
packages, err := containers.GetPackages(ctx, d, args.PackageInputOpts, args.GCPOpts)
if err != nil {
return err
}

if len(packages) < 2 {
return fmt.Errorf("at least two packages required for upgrade")
}

return validateUpgrade(ctx, d, packages, args.PackageInputOpts.Packages)
}

func distroPlatform(distro executil.Distribution) dagger.Platform {
platform := executil.Platform(distro)
if _, arch := executil.OSAndArch(distro); arch == "arm" {
Expand Down Expand Up @@ -222,7 +236,7 @@ func validateTarball(ctx context.Context, d *dagger.Client, pkg *dagger.File, sr
return containers.ValidatePackage(d, service, src, yarnCache, nodeVersion), nil
}

// validateLicense uses the given service and license path to validate the license for each edition (enterprise or oss)
// validateLicense uses the given container and license path to validate the license for each edition (enterprise or oss)
func validateLicense(ctx context.Context, service *dagger.Container, licensePath string, taropts TarFileOpts) error {
license, err := service.File(licensePath).Contents(ctx)
if taropts.Edition == "enterprise" {
Expand All @@ -239,3 +253,78 @@ func validateLicense(ctx context.Context, service *dagger.Container, licensePath

return nil
}

// validateVersion uses the given container and version path to validate the version for each edition (enterprise or oss)
func validateVersion(ctx context.Context, service *dagger.Container, versionPath string, taropts TarFileOpts) error {
version, err := service.File(versionPath).Contents(ctx)
if err != nil {
return err
}

if strings.TrimSpace(version) != taropts.Version {
return fmt.Errorf("version in package does not match version in package name")
}

return nil
}

// validateUpgrade verifies the extension of the first package and proceeds with upgrade validation for the same extension
func validateUpgrade(ctx context.Context, d *dagger.Client, packages []*dagger.File, names []string) error {
firstName := names[0]
if filepath.Ext(firstName) == ".deb" {
return validateDebUpgrade(ctx, d, packages, names)
}

if strings.HasSuffix(firstName, ".rpm") {
return nil
}

return fmt.Errorf("invalid upgrade package extension")
}

// validateDebUpgrade receives a list of packages and package names, the names are used to retrieve information such as distro and edition
// the function expects all the packages to have the same distro, otherwise it outputs a distro mismatch error
// each package is installed to the same container and the license and version files are validated to see if the installation succeeded
func validateDebUpgrade(ctx context.Context, d *dagger.Client, packages []*dagger.File, names []string) error {
kminehart marked this conversation as resolved.
Show resolved Hide resolved
var lastopts *TarFileOpts
var container *dagger.Container
for i, name := range names {
if ext := filepath.Ext(name); ext != ".deb" {
return fmt.Errorf("expected a file ending in .deb, received '%s'", ext)
}

pkg := packages[i]
taropts := TarOptsFromFileName(name)
if container == nil {
container = d.Container(dagger.ContainerOpts{
Platform: distroPlatform(taropts.Distro),
}).From("debian:latest").
WithExec([]string{"apt-get", "update"}).
WithWorkdir("/usr/share/grafana")
}

if lastopts != nil {
if lastopts.Distro != taropts.Distro {
return fmt.Errorf("upgrade package distro mismatch")
}

log.Printf("Validating deb package upgrade from v%s-%s to v%s-%s using debian:latest and platform %s\n", lastopts.Version, lastopts.Edition, taropts.Version, taropts.Edition, lastopts.Distro)
}

container = container.
WithFile("/src/package.deb", pkg).
WithExec([]string{"apt-get", "install", "-y", "/src/package.deb"})

if err := validateVersion(ctx, container, "/usr/share/grafana/VERSION", taropts); err != nil {
return err
}

if err := validateLicense(ctx, container, "/usr/share/grafana/LICENSE", taropts); err != nil {
return err
}

lastopts = &taropts
}

return nil
}
Loading