-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Closed
feat: Magefile #687
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
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", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
)