Skip to content

Commit

Permalink
Add a new artifact: docker-pro; remove pro logic from docker artifact (
Browse files Browse the repository at this point in the history
  • Loading branch information
kminehart authored Jul 15, 2024
1 parent 069469c commit 89dd15c
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 155 deletions.
149 changes: 4 additions & 145 deletions artifacts/package_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log/slog"
"strings"

"dagger.io/dagger"
"github.com/grafana/grafana-build/arguments"
Expand All @@ -19,19 +18,13 @@ var (
DockerArguments = arguments.Join(
TargzArguments,
[]pipeline.Argument{
arguments.ProDirectory,
arguments.DockerRegistry,
arguments.DockerOrg,
arguments.AlpineImage,
arguments.UbuntuImage,
arguments.TagFormat,
arguments.UbuntuTagFormat,
arguments.BoringTagFormat,

arguments.ProDockerRegistry,
arguments.ProDockerOrg,
arguments.ProDockerRepo,
arguments.ProTagFormat,
},
)
DockerFlags = flags.JoinFlags(
Expand All @@ -45,15 +38,13 @@ var DockerInitializer = Initializer{
Arguments: DockerArguments,
}

// PacakgeDocker uses a built tar.gz package to create a .rpm installer for RHEL-ish Linux distributions.
// PacakgeDocker uses a built tar.gz package to create a docker image from the Dockerfile in the tar.gz
type Docker struct {
Name packages.Name
Version string
BuildID string
Distro backend.Distribution
Enterprise bool
Pro bool
ProDir *dagger.Directory

Ubuntu bool
Registry string
Expand All @@ -62,20 +53,8 @@ type Docker struct {
BaseImage string
TagFormat string

// ProRegistry is the docker registry when using the `pro` name. (e.g. hub.docker.io)
ProRegistry string
// ProOrg is the docker org when using the `pro` name. (e.g. grafana)
ProOrg string
// ProOrg is the docker repo when using the `pro` name. (e.g. grafana-pro)
ProRepo string
// ProTagFormat is the docker tag format when using the `pro` name. (e.g. {{ .version }}-{{ .os }}-{{ .arch }})
ProTagFormat string

Tarball *pipeline.Artifact

// Building the Pro image requires a Debian package instead of a tar.gz
Deb *pipeline.Artifact

// Src is the Grafana source code for running e2e tests when validating.
// The grafana source should not be used for anything else when building a docker image. All files in the Docker image, including the Dockerfile, should be
// from the tar.gz file.
Expand All @@ -84,37 +63,12 @@ type Docker struct {
}

func (d *Docker) Dependencies(ctx context.Context) ([]*pipeline.Artifact, error) {
if d.Pro {
return []*pipeline.Artifact{
d.Deb,
}, nil
}

return []*pipeline.Artifact{
d.Tarball,
}, nil
}

func (d *Docker) proBuilder(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
deb, err := opts.Store.File(ctx, d.Deb)
if err != nil {
return nil, fmt.Errorf("error getting deb from state: %w", err)
}

socket := opts.Client.Host().UnixSocket("/var/run/docker.sock")

return opts.Client.Container().From("docker").
WithUnixSocket("/var/run/docker.sock", socket).
WithMountedDirectory("/src", d.ProDir).
WithMountedFile("/src/grafana.deb", deb).
WithWorkdir("/src"), nil
}

func (d *Docker) Builder(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
if d.Pro {
return d.proBuilder(ctx, opts)
}

targz, err := opts.Store.File(ctx, d.Tarball)
if err != nil {
return nil, err
Expand All @@ -123,38 +77,7 @@ func (d *Docker) Builder(ctx context.Context, opts *pipeline.ArtifactContainerOp
return docker.Builder(opts.Client, opts.Client.Host().UnixSocket("/var/run/docker.sock"), targz), nil
}

func (d *Docker) buildPro(ctx context.Context, builder *dagger.Container, opts *pipeline.ArtifactContainerOpts) (*dagger.File, error) {
tags, err := docker.Tags(d.ProOrg, d.ProRegistry, []string{d.ProRepo}, d.ProTagFormat, packages.NameOpts{
Name: d.Name,
Version: d.Version,
BuildID: d.BuildID,
Distro: d.Distro,
})

if err != nil {
return nil, err
}

builder = docker.Build(opts.Client, builder, &docker.BuildOpts{
Dockerfile: "./docker/hosted-grafana-all/Dockerfile",
Tags: tags,
Platform: dagger.Platform("linux/amd64"),
BuildArgs: []string{
"RELEASE_TYPE=main",
// I think because deb files use a ~ as a version delimiter of some kind, so the hg docker image uses that instead of a -
fmt.Sprintf("GRAFANA_VERSION=%s", strings.Replace(d.Version, "-", "~", 1)),
},
})

// Save the resulting docker image to the local filesystem
return builder.WithExec([]string{"docker", "save", tags[0], "-o", "pro.tar"}).File("pro.tar"), nil
}

func (d *Docker) BuildFile(ctx context.Context, builder *dagger.Container, opts *pipeline.ArtifactContainerOpts) (*dagger.File, error) {
if d.Pro {
return d.buildPro(ctx, builder, opts)
}

tags, err := docker.Tags(d.Org, d.Registry, d.Repositories, d.TagFormat, packages.NameOpts{
Name: d.Name,
Version: d.Version,
Expand Down Expand Up @@ -192,9 +115,6 @@ func (d *Docker) publishPro(ctx context.Context, opts *pipeline.ArtifactContaine
}

func (d *Docker) Publisher(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
if d.Pro {
return d.publishPro(ctx, opts)
}
socket := opts.Client.Host().UnixSocket("/var/run/docker.sock")
return opts.Client.Container().From("docker").WithUnixSocket("/var/run/docker.sock", socket), nil
}
Expand Down Expand Up @@ -227,10 +147,6 @@ func (d *Docker) VerifyFile(ctx context.Context, client *dagger.Client, file *da
return nil
}

if d.Pro {
return nil
}

return docker.Verify(ctx, client, file, d.Src, d.YarnCache, d.Distro)
}

Expand All @@ -239,12 +155,6 @@ func (d *Docker) VerifyDirectory(ctx context.Context, client *dagger.Client, dir
}

func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string, state pipeline.StateHandler) (*pipeline.Artifact, error) {
var (
pro bool
tarball *pipeline.Artifact
deb *pipeline.Artifact
)

options, err := pipeline.ParseFlags(artifact, DockerFlags)
if err != nil {
return nil, err
Expand All @@ -255,24 +165,9 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
return nil, err
}

if p.Name == packages.PackagePro {
pro = true
}

// The Pro docker image depends on the deb while everything else depends on the targz
if pro {
artifact, err := NewDebFromString(ctx, log, artifact, state)
if err != nil {
return nil, err
}
deb = artifact
} else {
artifact, err := NewTarballFromString(ctx, log, artifact, state)
if err != nil {
return nil, err
}

tarball = artifact
tarball, err := NewTarballFromString(ctx, log, artifact, state)
if err != nil {
return nil, err
}

ubuntu, err := options.Bool(flags.Ubuntu)
Expand Down Expand Up @@ -320,22 +215,6 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
if err != nil {
return nil, err
}
proRegistry, err := state.String(ctx, arguments.ProDockerRegistry)
if err != nil {
return nil, err
}
proOrg, err := state.String(ctx, arguments.ProDockerOrg)
if err != nil {
return nil, err
}
proRepo, err := state.String(ctx, arguments.ProDockerRepo)
if err != nil {
return nil, err
}
proTagFormat, err := state.String(ctx, arguments.ProTagFormat)
if err != nil {
return nil, err
}

base := alpineImage
if ubuntu {
Expand All @@ -347,18 +226,6 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
format = boringFormat
}

var (
proDir *dagger.Directory
)
if pro {
dir, err := state.Directory(ctx, arguments.ProDirectory)
if err != nil {
return nil, err
}

proDir = dir
}

src, err := state.Directory(ctx, arguments.GrafanaDirectory)
if err != nil {
return nil, err
Expand All @@ -379,10 +246,7 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
BuildID: p.BuildID,
Distro: p.Distribution,
Enterprise: p.Enterprise,
Pro: pro,
ProDir: proDir,
Tarball: tarball,
Deb: deb,

Ubuntu: ubuntu,
BaseImage: base,
Expand All @@ -391,11 +255,6 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
Repositories: repos,
TagFormat: format,

ProRegistry: proRegistry,
ProOrg: proOrg,
ProRepo: proRepo,
ProTagFormat: proTagFormat,

Src: src,
YarnCache: yarnCache,
},
Expand Down
Loading

0 comments on commit 89dd15c

Please sign in to comment.