From d323f4501b18612e577836ab805d5a0ac1724b8d Mon Sep 17 00:00:00 2001 From: Kevin Minehart Date: Thu, 21 Sep 2023 12:23:06 -0500 Subject: [PATCH 1/2] Add enterprise commit to buildinfo --- containers/buildinfo.go | 49 ++++++++++++++++++++++++++++++++-------- containers/enterprise.go | 18 +++++++++++++-- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/containers/buildinfo.go b/containers/buildinfo.go index 8c911dd5..de5cd1f7 100644 --- a/containers/buildinfo.go +++ b/containers/buildinfo.go @@ -3,6 +3,7 @@ package containers import ( "context" "fmt" + "log" "strings" "time" @@ -13,19 +14,25 @@ import ( ) type BuildInfo struct { - Version string - Commit string - Branch string - Timestamp time.Time + Version string + Commit string + EnterpriseCommit string + Branch string + Timestamp time.Time } func (b *BuildInfo) LDFlags() []string { - return []string{ + flags := []string{ fmt.Sprintf("main.version=%s", strings.TrimPrefix(b.Version, "v")), fmt.Sprintf("main.commit=%s", b.Commit), fmt.Sprintf("main.buildstamp=%d", b.Timestamp.Unix()), fmt.Sprintf("main.buildBranch=%s", b.Branch), } + + if b.EnterpriseCommit != "" { + flags = append(flags, fmt.Sprintf("main.enterpriseCommit=%s", b.EnterpriseCommit)) + } + return flags } const GitImage = "alpine/git:v2.36.3" @@ -40,6 +47,8 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, WithMountedDirectory("/src", dir). WithWorkdir("/src") + enterpriseSha, _ := enterpriseCommit(ctx, container) + sha, err := revParseShort(ctx, container) if err != nil { span.RecordError(err) @@ -57,15 +66,37 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, timestamp := time.Now() result := &BuildInfo{ - Version: version, - Commit: sha, - Branch: branch, - Timestamp: timestamp, + Version: version, + Commit: sha, + EnterpriseCommit: enterpriseSha, + Branch: branch, + Timestamp: timestamp, } span.SetAttributes(attribute.String("version", version), attribute.String("commit", sha), attribute.String("branch", branch), attribute.String("timestamp", timestamp.Format(time.RFC3339))) return result, nil } +func enterpriseCommit(ctx context.Context, container *dagger.Container) (string, error) { + var err error + c := container. + WithEntrypoint([]string{}). + WithExec([]string{"/bin/sh", "-c", "cat /src/.enterprise-commit || return 0"}) + + log.Println("Getting container exit error") + c, err = ExitError(ctx, c) + if err != nil { + return "", nil + } + + log.Println("Getting container stdout") + stdout, err := c.Stdout(ctx) + if err != nil { + return "", nil + } + + return strings.TrimSpace(stdout), nil +} + func revParseShort(ctx context.Context, container *dagger.Container) (string, error) { var err error c := container.WithExec([]string{"rev-parse", "--short", "HEAD"}) diff --git a/containers/enterprise.go b/containers/enterprise.go index fe53eccc..4961b561 100644 --- a/containers/enterprise.go +++ b/containers/enterprise.go @@ -1,15 +1,29 @@ package containers -import "dagger.io/dagger" +import ( + "dagger.io/dagger" +) const BusyboxImage = "busybox:1.36" func InitializeEnterprise(d *dagger.Client, grafana *dagger.Directory, enterprise *dagger.Directory) *dagger.Directory { - return d.Container().From(BusyboxImage). + hash := d.Container().From(GitImage). + WithDirectory("/src/grafana-enterprise", enterprise). + WithWorkdir("/src/grafana-enterprise"). + WithEntrypoint([]string{}). + WithExec([]string{"/bin/sh", "-c", "git rev-parse --short HEAD > .enterprise-commit"}). + File("/src/grafana-enterprise/.enterprise-commit") + + // Initializes Grafana Enterprise in the Grafana directory + g := d.Container().From(BusyboxImage). WithDirectory("/src/grafana", grafana). WithDirectory("/src/grafana-enterprise", enterprise). WithWorkdir("/src/grafana-enterprise"). + WithFile("/src/grafana/.enterprise-commit", hash). WithExec([]string{"/bin/sh", "build.sh"}). WithExec([]string{"cp", "LICENSE", "../grafana"}). + WithExec([]string{"cat", "../grafana/.enterprise-commit"}). Directory("/src/grafana") + + return g } From e7b8a93ac5a30035ee2062e8b9489de6088ecc3e Mon Sep 17 00:00:00 2001 From: Horst Gutmann Date: Wed, 18 Oct 2023 11:39:41 +0200 Subject: [PATCH 2/2] Implement review feedback --- containers/buildinfo.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/containers/buildinfo.go b/containers/buildinfo.go index de5cd1f7..2bb09139 100644 --- a/containers/buildinfo.go +++ b/containers/buildinfo.go @@ -3,7 +3,6 @@ package containers import ( "context" "fmt" - "log" "strings" "time" @@ -47,7 +46,7 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, WithMountedDirectory("/src", dir). WithWorkdir("/src") - enterpriseSha, _ := enterpriseCommit(ctx, container) + enterpriseSha := enterpriseCommit(ctx, container) sha, err := revParseShort(ctx, container) if err != nil { @@ -72,29 +71,33 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, Branch: branch, Timestamp: timestamp, } - span.SetAttributes(attribute.String("version", version), attribute.String("commit", sha), attribute.String("branch", branch), attribute.String("timestamp", timestamp.Format(time.RFC3339))) + span.SetAttributes( + attribute.String("version", version), + attribute.String("commit", sha), + attribute.String("enterpriseCommit", enterpriseSha), + attribute.String("branch", branch), + attribute.String("timestamp", timestamp.Format(time.RFC3339)), + ) return result, nil } -func enterpriseCommit(ctx context.Context, container *dagger.Container) (string, error) { +func enterpriseCommit(ctx context.Context, container *dagger.Container) string { var err error c := container. WithEntrypoint([]string{}). WithExec([]string{"/bin/sh", "-c", "cat /src/.enterprise-commit || return 0"}) - log.Println("Getting container exit error") c, err = ExitError(ctx, c) if err != nil { - return "", nil + return "" } - log.Println("Getting container stdout") stdout, err := c.Stdout(ctx) if err != nil { - return "", nil + return "" } - return strings.TrimSpace(stdout), nil + return strings.TrimSpace(stdout) } func revParseShort(ctx context.Context, container *dagger.Container) (string, error) {