Skip to content

Commit

Permalink
upgrade dagger cli / module and go
Browse files Browse the repository at this point in the history
  • Loading branch information
kminehart committed Jul 8, 2024
1 parent ab55246 commit d08b9df
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 97 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ FROM alpine:3.19 AS dagger

# TODO: pull the binary from registry.dagger.io/cli:v0.9.8 (or similar) when
# https://github.com/dagger/dagger/issues/6887 is resolved
ARG DAGGER_VERSION=v0.9.8
ARG DAGGER_VERSION=v0.11.6
ADD https://github.com/dagger/dagger/releases/download/${DAGGER_VERSION}/dagger_${DAGGER_VERSION}_linux_amd64.tar.gz /tmp
RUN tar zxf /tmp/dagger_${DAGGER_VERSION}_linux_amd64.tar.gz -C /tmp
RUN mv /tmp/dagger /bin/dagger

FROM golang:1.22-alpine

ARG DAGGER_VERSION=v0.9.8
ARG DAGGER_VERSION=v0.11.6

WORKDIR /src
RUN apk add --no-cache git wget bash jq
Expand Down
6 changes: 6 additions & 0 deletions arguments/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ var (
Usage: "Overrides the organization of the images",
Value: "grafana",
}
ProDockerRepoFlag = &cli.StringFlag{
Name: "pro-repo",
Usage: "Overrides the docker repository of the built images",
Value: "grafana-pro",
}
ProTagFormatFlag = &cli.StringFlag{
Name: "pro-tag-format",
Usage: "Provide a go template for formatting the docker tag(s) for Grafana Pro images",
Expand All @@ -70,5 +75,6 @@ var (
// The docker registry for Grafana Pro is often different than the one for Grafana & Enterprise
ProDockerRegistry = pipeline.NewStringFlagArgument(ProDockerRegistryFlag)
ProDockerOrg = pipeline.NewStringFlagArgument(ProDockerOrgFlag)
ProDockerRepo = pipeline.NewStringFlagArgument(ProDockerRepoFlag)
ProTagFormat = pipeline.NewStringFlagArgument(ProTagFormatFlag)
)
2 changes: 1 addition & 1 deletion arguments/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
DefaultGoVersion = "1.22.3"
DefaultGoVersion = "1.22.4"
DefaultViceroyVersion = "v0.4.0"
)

Expand Down
22 changes: 8 additions & 14 deletions arguments/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ type GrafanaDirectoryOpts struct {
PatchesRef string
}

func (o *GrafanaDirectoryOpts) githubToken(ctx context.Context) (string, error) {
func githubToken(ctx context.Context, token string) (string, error) {
// Since GrafanaDir was not provided, we must clone it.
ght := o.GitHubToken
ght := token

// If GitHubToken was not set from flag
if ght != "" {
Expand All @@ -79,7 +79,7 @@ func (o *GrafanaDirectoryOpts) githubToken(ctx context.Context) (string, error)
return token, nil
}

func GrafanaDirectoryOptsFromFlags(ctx context.Context, c cliutil.CLIContext) (*GrafanaDirectoryOpts, error) {
func GrafanaDirectoryOptsFromFlags(c cliutil.CLIContext) *GrafanaDirectoryOpts {
return &GrafanaDirectoryOpts{
GrafanaRepo: c.String("grafana-repo"),
EnterpriseRepo: c.String("enterprise-repo"),
Expand All @@ -91,7 +91,7 @@ func GrafanaDirectoryOptsFromFlags(ctx context.Context, c cliutil.CLIContext) (*
PatchesRepo: c.String("patches-repo"),
PatchesPath: c.String("patches-path"),
PatchesRef: c.String("patches-ref"),
}, nil
}
}

func cloneOrMount(ctx context.Context, client *dagger.Client, localPath, repo, ref string, ght string) (*dagger.Directory, error) {
Expand Down Expand Up @@ -156,12 +156,9 @@ func applyPatches(ctx context.Context, client *dagger.Client, src *dagger.Direct
}

func grafanaDirectory(ctx context.Context, opts *pipeline.ArgumentOpts) (any, error) {
o, err := GrafanaDirectoryOptsFromFlags(ctx, opts.CLIContext)
if err != nil {
return nil, err
}
o := GrafanaDirectoryOptsFromFlags(opts.CLIContext)

ght, err := o.githubToken(ctx)
ght, err := githubToken(ctx, o.GitHubToken)
if err != nil {
log.Println("No github token found:", err)
}
Expand Down Expand Up @@ -202,17 +199,14 @@ func grafanaDirectory(ctx context.Context, opts *pipeline.ArgumentOpts) (any, er

func enterpriseDirectory(ctx context.Context, opts *pipeline.ArgumentOpts) (any, error) {
// Get the Grafana directory...
o, err := GrafanaDirectoryOptsFromFlags(ctx, opts.CLIContext)
if err != nil {
return nil, err
}
o := GrafanaDirectoryOptsFromFlags(opts.CLIContext)

grafanaDir, err := grafanaDirectory(ctx, opts)
if err != nil {
return nil, fmt.Errorf("error initializing grafana directory: %w", err)
}

ght, err := o.githubToken(ctx)
ght, err := githubToken(ctx, o.GitHubToken)
if err != nil {
return nil, nil
}
Expand Down
49 changes: 47 additions & 2 deletions arguments/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ package arguments

import (
"context"
"fmt"

"github.com/grafana/grafana-build/cliutil"
"github.com/grafana/grafana-build/pipeline"
"github.com/urfave/cli/v2"
)

var ProDirectoryFlags = []cli.Flag{
&cli.StringFlag{},
&cli.StringFlag{
Name: "hosted-grafana-dir",
Usage: "Local clone of HG to use, instead of git cloning",
Required: false,
},
&cli.StringFlag{
Name: "hosted-grafana-repo",
Usage: "https `.git` repository to use for hosted-grafana",
Required: false,
Value: "https://github.com/grafana/hosted-grafana.git",
},
&cli.StringFlag{
Name: "hosted-grafana-ref",
Usage: "git ref to checkout",
Required: false,
Value: "main",
},
}

// GrafanaDirectory will provide the valueFunc that initializes and returns a *dagger.Directory that has Grafana in it.

Check failure on line 32 in arguments/pro.go

View workflow job for this annotation

GitHub Actions / golang-ci

ST1022: comment on exported var ProDirectory should be of the form "ProDirectory ..." (stylecheck)
Expand All @@ -20,6 +38,33 @@ var ProDirectory = pipeline.Argument{
ValueFunc: proDirectory,
}

type ProDirectoryOpts struct {
GitHubToken string
HGDir string
HGRepo string
HGRef string
}

func ProDirectoryOptsFromFlags(c cliutil.CLIContext) *ProDirectoryOpts {
return &ProDirectoryOpts{
GitHubToken: c.String("github-token"),
HGDir: c.String("hosted-grafana-dir"),
HGRepo: c.String("hosted-grafana-repo"),
HGRef: c.String("hosted-grafana-ref"),
}
}

func proDirectory(ctx context.Context, opts *pipeline.ArgumentOpts) (any, error) {
return nil, nil
o := ProDirectoryOptsFromFlags(opts.CLIContext)
ght, err := githubToken(ctx, o.GitHubToken)
if err != nil {
return nil, fmt.Errorf("could not get GitHub token: %w", err)
}

src, err := cloneOrMount(ctx, opts.Client, o.HGDir, o.HGRepo, o.HGRef, ght)
if err != nil {
return nil, err
}

return src, nil
}
94 changes: 75 additions & 19 deletions artifacts/package_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"strings"

"dagger.io/dagger"
"github.com/grafana/grafana-build/arguments"
Expand All @@ -18,6 +19,7 @@ var (
DockerArguments = arguments.Join(
TargzArguments,
[]pipeline.Argument{
arguments.ProDirectory,
arguments.DockerRegistry,
arguments.DockerOrg,
arguments.AlpineImage,
Expand All @@ -28,6 +30,7 @@ var (

arguments.ProDockerRegistry,
arguments.ProDockerOrg,
arguments.ProDockerRepo,
arguments.ProTagFormat,
},
)
Expand Down Expand Up @@ -59,6 +62,15 @@ 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
Expand Down Expand Up @@ -89,16 +101,11 @@ func (d *Docker) proBuilder(ctx context.Context, opts *pipeline.ArtifactContaine
return nil, fmt.Errorf("error getting deb from state: %w", err)
}

src, err := opts.State.Directory(ctx, arguments.ProDirectory)
if err != nil {
return nil, err
}

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

return opts.Client.Container().From("docker").
WithUnixSocket("/var/run/docker.sock", socket).
WithMountedDirectory("/src", src).
WithMountedDirectory("/src", d.ProDir).
WithMountedFile("/src/grafana.deb", deb).
WithWorkdir("/src"), nil
}
Expand All @@ -117,7 +124,7 @@ func (d *Docker) Builder(ctx context.Context, opts *pipeline.ArtifactContainerOp
}

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

builder = builder.
WithExec([]string{"docker", "build", "--platform=linux/amd64", ".", "-f", "./cmd/hgrun/Dockerfile", "-t", "hrun:latest"})

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=%s",
"GRAFANA_VERSION=%s",
"RELEASE_TYPE=prerelease",
// 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)),
"HGRUN_IMAGE=hgrun:latest",
},
})

return nil, nil
// 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) {
Expand Down Expand Up @@ -170,6 +180,7 @@ func (d *Docker) BuildFile(ctx context.Context, builder *dagger.Container, opts
"JS_SRC=tgz-builder",
fmt.Sprintf("BASE_IMAGE=%s", d.BaseImage),
},
Dockerfile: "./docker/hosted-grafana-all/Dockerfile",
}

b := docker.Build(opts.Client, builder, buildOpts)
Expand All @@ -182,7 +193,7 @@ func (d *Docker) BuildDir(ctx context.Context, builder *dagger.Container, opts *
}

func (d *Docker) publishPro(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
return nil, nil
panic("not implemented")
}

func (d *Docker) Publisher(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
Expand Down Expand Up @@ -220,6 +231,11 @@ func (d *Docker) VerifyFile(ctx context.Context, client *dagger.Client, file *da
if _, arch := backend.OSAndArch(d.Distro); arch == "riscv64" {
return nil
}

if d.Pro {
return nil
}

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

Expand All @@ -228,10 +244,11 @@ 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) {
tarball, err := NewTarballFromString(ctx, log, artifact, state)
if err != nil {
return nil, err
}
var (
pro bool
tarball *pipeline.Artifact
deb *pipeline.Artifact
)

options, err := pipeline.ParseFlags(artifact, DockerFlags)
if err != nil {
Expand All @@ -243,6 +260,26 @@ 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
}

ubuntu, err := options.Bool(flags.Ubuntu)
if err != nil {
return nil, err
Expand Down Expand Up @@ -288,6 +325,22 @@ 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 @@ -300,12 +353,9 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
}

var (
pro bool
proDir *dagger.Directory
)

if p.Name == packages.PackagePro {
pro = true
if pro {
dir, err := state.Directory(ctx, arguments.ProDirectory)
if err != nil {
return nil, err
Expand Down Expand Up @@ -337,6 +387,7 @@ func NewDockerFromString(ctx context.Context, log *slog.Logger, artifact string,
Pro: pro,
ProDir: proDir,
Tarball: tarball,
Deb: deb,

Ubuntu: ubuntu,
BaseImage: base,
Expand All @@ -345,6 +396,11 @@ 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 d08b9df

Please sign in to comment.