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 pro:docker option #334

Merged
merged 9 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
27 changes: 27 additions & 0 deletions arguments/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,38 @@ var (
Value: docker.DefaultBoringTagFormat,
}

ProDockerRegistryFlag = &cli.StringFlag{
Name: "pro-registry",
Usage: "Prefix the image name with the registry provided",
Value: "docker.io",
}
ProDockerOrgFlag = &cli.StringFlag{
Name: "pro-org",
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",
Value: docker.DefaultProTagFormat,
}

DockerRegistry = pipeline.NewStringFlagArgument(DockerRegistryFlag)
DockerOrg = pipeline.NewStringFlagArgument(DockerOrgFlag)
AlpineImage = pipeline.NewStringFlagArgument(AlpineImageFlag)
UbuntuImage = pipeline.NewStringFlagArgument(UbuntuImageFlag)
TagFormat = pipeline.NewStringFlagArgument(TagFormatFlag)
UbuntuTagFormat = pipeline.NewStringFlagArgument(UbuntuTagFormatFlag)
BoringTagFormat = pipeline.NewStringFlagArgument(BoringTagFormatFlag)

// 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"
kminehart marked this conversation as resolved.
Show resolved Hide resolved
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
70 changes: 70 additions & 0 deletions arguments/pro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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{
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",
},
}

// ProDirectory will provide the valueFunc that initializes and returns a *dagger.Directory that has a repository that has the Grafana Pro docker image.
// Where possible, when cloning and no authentication options are provided, the valuefunc will try to use the configured github CLI for cloning.
var ProDirectory = pipeline.Argument{
Name: "pro-dir",
Description: "The source tree of that has the Dockerfile for Grafana Pro",
Flags: ProDirectoryFlags,
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) {
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
}
Loading