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 1 commit
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
5 changes: 3 additions & 2 deletions pipelines/package_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func TarFilename(opts TarFileOpts) string {
return fmt.Sprintf("%s.tar.gz", strings.Join(p, "_"))
}

func TarOptsFromFileName(filename string) TarFileOpts {
func TarOptsFromFileName(path string) TarFileOpts {
filename := filepath.Base(path)
n := WithoutExt(filename)
components := strings.Split(n, "_")
if len(components) != 5 {
Expand All @@ -77,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
84 changes: 83 additions & 1 deletion pipelines/package_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 +235,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 +252,72 @@ 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 || strings.TrimSpace(version) != taropts.Version {
return fmt.Errorf("failed to validate version")
guicaulada marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func validateUpgrade(ctx context.Context, d *dagger.Client, packages []*dagger.File, names []string) error {
firstName := names[0]
if strings.HasSuffix(firstName, ".deb") {
guicaulada marked this conversation as resolved.
Show resolved Hide resolved
return validateDebUpgrade(ctx, d, packages, names)
}

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

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

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 !strings.HasSuffix(name, ".deb") {
return fmt.Errorf("upgrade package extension mismatch")
}
guicaulada marked this conversation as resolved.
Show resolved Hide resolved

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"})

err := validateVersion(ctx, container, "/usr/share/grafana/VERSION", taropts)
if err != nil {
return err
}
guicaulada marked this conversation as resolved.
Show resolved Hide resolved

err = validateLicense(ctx, container, "/usr/share/grafana/LICENSE", taropts)
if err != nil {
return err
}
guicaulada marked this conversation as resolved.
Show resolved Hide resolved

lastopts = &taropts
}

return nil
}
Loading