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

feat: Magefile #687

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 16 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

.PHONY: all test clean images protos assets check_assets release-prepare release-perform
.PHONY: all test clean images protos assets check_assets release-prepare release-perform mage mage-remove mage-upgrade
.DEFAULT_GOAL := all

CHARTS := production/helm/loki production/helm/promtail production/helm/loki-stack
Expand Down Expand Up @@ -312,3 +312,16 @@ push-plugin: build-plugin

enable-plugin:
docker plugin enable grafana/loki-docker-driver:$(PLUGIN_TAG)

$(GOPATH)/bin/mage:
go get -u -d github.com/magefile/mage
cd $(GOPATH)/src/github.com/magefile/mage && go run bootstrap.go

mage: $(GOPATH)/bin/mage
@echo "\n>> MAGE VERSION"
mage -version

mage-remove:
rm $(GOPATH)/bin/mage

mage-upgrade: mage-remove mage
28 changes: 28 additions & 0 deletions mage/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package magefile

import (
"os"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)

type Assets mg.Namespace

func (Assets) CheckAssets() error {
err := sh.Run("git", "diff", "--exit-code", "pkg/promtail/server/ui")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git() is available as a function (cmds.go)

if err != nil {
return errors.Wrap(err, "assets have been changed, run `mage assets:generateAssets` and then commit changes to continue")
}
return err
}

func (Assets) GenerateAssets() error {
return sh.RunWith(
map[string]string{
"GOOS": os.Getenv("GOOS"),
},
"go", "generate", "-x", "-v", "./pkg/promtail/server/ui",
)
}
70 changes: 70 additions & 0 deletions mage/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package magefile

import (
"fmt"
"log"
"os"
"strings"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

const vendor = "github.com/grafana/loki/vendor/"

// Build Namespace groups all targets related to building applications
type Build mg.Namespace

func (Build) Promtail() error {
return compile("promtail")
}

func (Build) Loki() error {
return compile("loki")
}

func (Build) Logcli() error {
return compile("logcli")
}

func compile(app string) error {
debug := os.Getenv("DEBUG") != ""
log.Printf("Building %v, debug=%v", app, debug)

var flags = map[string]string{
"ldflags": strings.Join(ldflags(debug), " "),
"tags": "netgo",
"o": fmt.Sprintf("cmd/%s/%s", app, app),
}

if debug {
flags["gcflags"] = "all=-N -l"
}

flagSlice := joinMap(flags, "-%v=%v")
args := append(flagSlice, "./cmd/"+app)

return sh.RunWith(
map[string]string{
"CGO_ENABLED": "0",
"GOOS": os.Getenv("GOOS"),
"GOARCH": os.Getenv("GOARCH"),
},
"go", append([]string{"build"}, args...)...,
)
}

func ldflags(debug bool) (flags []string) {
var vars = joinMap(map[string]string{
vendor + "github.com/prometheus/common/version.Branch": revParse("--short", "HEAD"),
vendor + "github.com/prometheus/common/version.Version": stdout("./tools/image-tag"),
vendor + "github.com/prometheus/common/version.Revision": revParse("--abbrev-ref", "HEAD"),
}, "-X %v=%v")

flags = []string{`-extldflags "-static"`}
if !debug {
flags = append(flags, "-s", "-w")
}
flags = append(flags, vars...)
return flags
}
9 changes: 9 additions & 0 deletions mage/cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package magefile

import "github.com/magefile/mage/sh"

var (
helm = sh.RunCmd("helm")
git = sh.RunCmd("git")
kubectl = sh.RunCmd("kubectl")
)
142 changes: 142 additions & 0 deletions mage/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package magefile

import (
"fmt"
"log"
"os"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

type Helm mg.Namespace

var CHARTS = [...]string{"production/helm/loki", "production/helm/promtail", "production/helm/loki-stack"}

func (Helm) Build() error {
if err := rmGlob("production/helm/*/requirements.lock"); err != nil {
return err
}
if err := helm("init", "-c"); err != nil {
return err
}

for _, chart := range CHARTS {
if err := helm("dependency", "build", chart); err != nil {
return err
}
if err := helm("lint", chart); err != nil {
return err
}
if err := helm("package", chart); err != nil {
return err
}
}

if err := rmGlob("production/helm/*/requirements.lock"); err != nil {
return err
}

return nil
}

func (Helm) Publish() error {
if err := sh.Copy("production/helm/README.md", "index.md"); err != nil {
return err
}

if os.Getenv("CI") != "" {
log.Println("Running in CI, setting up git")
user := os.Getenv("CIRCLE_USERNAME")
if err := git("config", "user.email", fmt.Sprintf("%v@users.noreply.github.com", user)); err != nil {
return err
}
if err := git("config", "user.name", user); err != nil {
return err
}
}

if err := git("checkout", "gh-pages"); err != nil {
log.Println("gh-pages checkout failed, switching to orphan mode")
if err := git("checkout", "--orphan", "gh-pages"); err != nil {
return err
}
if err := sh.Rm("."); err != nil {
return err
}
}
if _, err := os.Stat("charts"); os.IsNotExist(err) {
if err := os.Mkdir("charts", 0755); err != nil {
return err
}
}

if err := mvGlobToDir("*.tgz", "charts"); err != nil {
return err
}
if err := os.Rename("index.md", "charts/index.md"); err != nil {
return err
}

if err := helm("repo", "index", "charts/"); err != nil {
return err
}

if err := git("add", "charts"); err != nil {
return err
}

if err := git("commit", "-m",
fmt.Sprintf("[skip ci] Publishing helm charts: %v", os.Getenv("CIRCLE_SHA1")),
); err != nil {
return err
}

if err := git("push", "origin", "gh-pages"); err != nil {
return err
}
return nil
}

func (h Helm) Install() error {
if err := kubectl("apply", "-f", "tools/helm.yaml"); err != nil {
return err
}

if err := helm("init", "--wait", "--service-account=helm", "--upgrade"); err != nil {
return err
}

return h.upgrade()
}

func (h Helm) Debug() error {
return h.upgrade("--dry-run", "--debug")
}

func (h Helm) Upgrade() error {
return h.upgrade()
}

func (Helm) upgrade(flags ...string) error {
tag := func(name string) string {
return name
}

args := []string{"upgrade", "--wait", "--install", "-f=tools/dev.values.yaml"}
args = append(args, flags...)
args = append(args, "loki-stack", "./production/helm/loki-stack")
args = append(args, []string{
"--set", tag("promtail"),
"--set", tag("loki"),
}...)

if err := helm(args...); err != nil {
return err
}
return nil
}

func (Helm) Clean() error {
return helm("delete", "--purge", "loki-stack")
}
54 changes: 54 additions & 0 deletions mage/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package magefile

import (
"fmt"
"os"
"path/filepath"

"github.com/magefile/mage/sh"
)

func rmGlob(glob string) error {
files, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, f := range files {
if err := sh.Rm(f); err != nil {
return err
}
}
return nil
}

func mvGlobToDir(glob, dir string) error {
files, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, f := range files {
if err := os.Rename(f, filepath.Join(dir, f)); err != nil {
return err
}
}
return nil
}

func revParse(args ...string) string {
return stdout("git", append([]string{"rev-parse"}, args...)...)
}

func joinMap(mapping map[string]string, pattern string) (out []string) {
for k, v := range mapping {
out = append(out, fmt.Sprintf(pattern, k, v))
}
return
}

func stdout(cmd string, args ...string) string {
out, err := sh.Output(cmd, args...)
if err != nil {
panic(err)
}
return out
}
Loading