CI: Move fetch-images sub-command from build-pipeline repo (#55298)
* Move fetch images from build-pipeline * Modify drone * Replace zerolog with builtin log * Fix lint
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/build/config"
|
||||
"github.com/grafana/grafana/pkg/build/docker"
|
||||
"github.com/grafana/grafana/pkg/build/gcloud"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
alpine = "alpine"
|
||||
ubuntu = "ubuntu"
|
||||
)
|
||||
|
||||
func FetchImages(c *cli.Context) error {
|
||||
if c.NArg() > 0 {
|
||||
if err := cli.ShowSubcommandHelp(c); err != nil {
|
||||
return cli.NewExitError(err.Error(), 1)
|
||||
}
|
||||
return cli.NewExitError("", 1)
|
||||
}
|
||||
|
||||
metadata, err := GenerateMetadata(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buildConfig, err := config.GetBuildConfig(metadata.ReleaseMode.Mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := docker.Config{
|
||||
Archs: buildConfig.Docker.Architectures,
|
||||
Distribution: buildConfig.Docker.Distribution,
|
||||
Bucket: buildConfig.Docker.PrereleaseBucket,
|
||||
Edition: c.String("edition"),
|
||||
Tag: metadata.GrafanaVersion,
|
||||
}
|
||||
|
||||
edition := fmt.Sprintf("-%s", cfg.Edition)
|
||||
|
||||
err = gcloud.ActivateServiceAccount()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var basesStr []string
|
||||
for _, base := range cfg.Distribution {
|
||||
switch base {
|
||||
case alpine:
|
||||
basesStr = append(basesStr, "")
|
||||
case ubuntu:
|
||||
basesStr = append(basesStr, "-ubuntu")
|
||||
default:
|
||||
return fmt.Errorf("unrecognized base %q", base)
|
||||
}
|
||||
}
|
||||
|
||||
err = downloadFromGCS(cfg, basesStr, edition)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = loadImages(cfg, basesStr, edition)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadImages(cfg docker.Config, basesStr []string, edition string) error {
|
||||
log.Println("Loading fetched image files to local docker registry...")
|
||||
log.Printf("Number of images to be loaded: %d\n", len(basesStr)*len(cfg.Archs))
|
||||
for _, base := range basesStr {
|
||||
for _, arch := range cfg.Archs {
|
||||
imageFilename := fmt.Sprintf("grafana%s-%s%s-%s.img", edition, cfg.Tag, base, arch)
|
||||
log.Printf("image file name: %s\n", imageFilename)
|
||||
//nolint:gosec
|
||||
cmd := exec.Command("docker", "load", "-i", imageFilename)
|
||||
cmd.Dir = "."
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("out: %s\n", out)
|
||||
return fmt.Errorf("error loading image: %q", err)
|
||||
}
|
||||
log.Printf("Successfully loaded %s!\n %s\n", fmt.Sprintf("grafana%s-%s%s-%s", edition, cfg.Tag, base, arch), out)
|
||||
}
|
||||
}
|
||||
log.Println("Images successfully loaded!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func downloadFromGCS(cfg docker.Config, basesStr []string, edition string) error {
|
||||
log.Printf("Downloading Docker images from GCS bucket: %s\n", cfg.Bucket)
|
||||
|
||||
for _, base := range basesStr {
|
||||
for _, arch := range cfg.Archs {
|
||||
src := fmt.Sprintf("gs://%s/%s/grafana%s-%s%s-%s.img", cfg.Bucket, cfg.Tag, edition, cfg.Tag, base, arch)
|
||||
args := strings.Split(fmt.Sprintf("-m cp -r %s .", src), " ")
|
||||
//nolint:gosec
|
||||
cmd := exec.Command("gsutil", args...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download: %w\n%s", err, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("Successfully fetched image files from %s bucket!\n", cfg.Bucket)
|
||||
return nil
|
||||
}
|
||||
@@ -135,6 +135,21 @@ func main() {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "docker",
|
||||
Usage: "Handle Grafana Docker images",
|
||||
Subcommands: cli.Commands{
|
||||
{
|
||||
Name: "fetch",
|
||||
Usage: "Fetch Grafana Docker images",
|
||||
ArgsUsage: "[version]",
|
||||
Action: ArgCountWrapper(1, FetchImages),
|
||||
Flags: []cli.Flag{
|
||||
&editionFlag,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
|
||||
@@ -22,9 +22,6 @@ func StoreStorybook(c *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
storybookBucket := buildConfig.Buckets.Storybook
|
||||
srcPath := buildConfig.Buckets.StorybookSrcDir
|
||||
|
||||
@@ -28,9 +28,10 @@ type PluginSignature struct {
|
||||
}
|
||||
|
||||
type Docker struct {
|
||||
ShouldSave bool `json:"shouldSave,omitempty"`
|
||||
Distribution []Distribution `json:"distribution,omitempty"`
|
||||
Architectures []Architecture `json:"archs,omitempty"`
|
||||
ShouldSave bool `json:"shouldSave,omitempty"`
|
||||
Distribution []Distribution `json:"distribution,omitempty"`
|
||||
Architectures []Architecture `json:"archs,omitempty"`
|
||||
PrereleaseBucket string `json:"prereleaseBucket,omitempty"`
|
||||
}
|
||||
|
||||
type Buckets struct {
|
||||
|
||||
@@ -151,6 +151,7 @@ var Versions = VersionMap{
|
||||
Alpine,
|
||||
Ubuntu,
|
||||
},
|
||||
PrereleaseBucket: "grafana-prerelease/artifacts/docker",
|
||||
},
|
||||
Buckets: Buckets{
|
||||
Artifacts: "grafana-prerelease/artifacts/downloads",
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/build/config"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Bucket string
|
||||
Edition string
|
||||
Tag string
|
||||
Distribution []config.Distribution
|
||||
Archs []config.Architecture
|
||||
DockerHubRepo string
|
||||
Security bool
|
||||
}
|
||||
Reference in New Issue
Block a user