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

feature: simplify velox build configuration #165

Merged
merged 5 commits into from
Jun 21, 2024
Merged
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
14 changes: 7 additions & 7 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ on:
branches:
- master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 5 * * *'
# * is a special character in YAML so you have to quote this string
- cron: "30 5 * * *"

jobs:
golang:
Expand All @@ -19,8 +19,8 @@ jobs:
strategy:
fail-fast: true
matrix:
go: [ stable ]
os: [ "ubuntu-latest" ]
go: [stable]
os: ["ubuntu-latest"]
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v5 # action page: <https://github.com/actions/setup-go>
Expand Down Expand Up @@ -50,8 +50,8 @@ jobs:
strategy:
fail-fast: true
matrix:
go: [ stable ]
os: [ "ubuntu-latest" ]
go: [stable]
os: ["ubuntu-latest"]
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v5 # action page: <https://github.com/actions/setup-go>
Expand Down Expand Up @@ -79,5 +79,5 @@ jobs:
TIME: ${{ secrets.TIME }}
run: |
go install github.com/roadrunner-server/velox/v2024/cmd/vx@latest
vx build -c velox_rr_v2024.toml -o="."
vx build -c velox.toml -o="."
./rr --version
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: release

on:
release: # Docs: <https://help.github.com/en/articles/events-that-trigger-workflows#release-event-release>
types: [ published ]
types: [published]

jobs:
build:
Expand All @@ -11,10 +11,10 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ windows, darwin ] # linux, darwin, windows
compiler: [ gcc ] # gcc, musl-gcc
archiver: [ zip ] # tar, zip
arch: [ amd64 ] # amd64, 386
os: [windows, darwin] # linux, darwin, windows
compiler: [gcc] # gcc, musl-gcc
archiver: [zip] # tar, zip
arch: [amd64] # amd64, 386
include:
- os: linux
compiler: gcc
Expand All @@ -28,7 +28,7 @@ jobs:
compiler: gcc
archiver: tar
arch: arm64
- os: ''
- os: ""
compiler: musl-gcc # more info: <https://musl.libc.org/>
archiver: zip
arch: amd64
Expand Down Expand Up @@ -92,7 +92,6 @@ jobs:
- name: Create distributive
run: |
mkdir ${{ steps.dist-dir.outputs.name }}
rm velox.toml && mv velox_rr_v2024.toml velox.toml
mv "./${{ steps.values.outputs.binary-name }}" ./${{ steps.dist-dir.outputs.name }}/
cp ./README.md ./LICENSE ./velox.toml ./${{ steps.dist-dir.outputs.name }}/

Expand Down Expand Up @@ -167,7 +166,7 @@ jobs:
spiralscout/velox:${{ steps.values.outputs.version }}
spiralscout/velox:2024.1
spiralscout/velox:2024

ghcr.io/roadrunner-server/velox:latest
ghcr.io/roadrunner-server/velox:${{ steps.values.outputs.version }}
ghcr.io/roadrunner-server/velox:2024.1
Expand Down
94 changes: 57 additions & 37 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builder
import (
"bytes"
"fmt"
"log/slog"
"math/rand"
"os"
"os/exec"
Expand All @@ -11,11 +12,11 @@ import (
"regexp"
"strings"
"syscall"
"time"

"github.com/hashicorp/go-version"
"github.com/roadrunner-server/velox/v2024"
"github.com/roadrunner-server/velox/v2024/builder/templates"
"go.uber.org/zap"
)

const (
Expand All @@ -28,6 +29,7 @@ const (
executableName string = "rr"
// cleanup pattern
cleanupPattern string = "roadrunner-server*"
ldflags string = "-X github.com/roadrunner-server/roadrunner/v2024/internal/meta.version=%s -X github.com/roadrunner-server/roadrunner/v2024/internal/meta.buildTime=%s"
)

var replaceRegexp = regexp.MustCompile("(\t| )(.+) => (.+)")
Expand All @@ -36,16 +38,18 @@ type Builder struct {
rrTempPath string
out string
modules []*velox.ModulesInfo
log *zap.Logger
buildArgs []string
log *slog.Logger
debug bool
rrVersion string
}

func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out string, log *zap.Logger, buildArgs []string) *Builder {
func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out, rrVersion string, debug bool, log *slog.Logger) *Builder {
return &Builder{
rrTempPath: rrTmpPath,
modules: modules,
buildArgs: buildArgs,
out: out,
debug: debug,
rrVersion: rrVersion,
log: log,
}
}
Expand All @@ -63,9 +67,10 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
t.Entries = make([]*templates.Entry, len(b.modules))
for i := 0; i < len(b.modules); i++ {
t.Entries[i] = &templates.Entry{
Module: b.modules[i].ModuleName,
Module: b.modules[i].ModuleName,
// we need to set prefix to avoid collisions
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
StructureName: pluginStructureStr,
PseudoVersion: b.modules[i].PseudoVersion,
Replace: b.modules[i].Replace,
}
Expand Down Expand Up @@ -94,7 +99,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))
b.log.Debug("template", slog.String("template", buf.String()))

f, err := os.Open(b.rrTempPath)
if err != nil {
Expand All @@ -108,7 +113,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

for i := 0; i < len(files); i++ {
b.log.Info("[CLEANING UP]", zap.String("file/folder", files[i]))
b.log.Info("cleaning temporary folders", slog.String("file/folder", files[i]))
_ = os.RemoveAll(files[i])
}
}()
Expand All @@ -134,6 +139,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

// reuse buffer
buf.Reset()

// compatibility with version 2
Expand All @@ -157,16 +163,17 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))
b.log.Debug("template", slog.String("template", buf.String()))

_, err = goModFile.Write(buf.Bytes())
if err != nil {
return err
}

// reuse buffer
buf.Reset()

b.log.Info("[SWITCHING WORKING DIR]", zap.String("wd", b.rrTempPath))
b.log.Info("switching working directory", slog.String("wd", b.rrTempPath))
err = syscall.Chdir(b.rrTempPath)
if err != nil {
return err
Expand All @@ -182,7 +189,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("[CHECKING OUTPUT DIR]", zap.String("dir", b.out))
b.log.Info("creating output directory", slog.String("dir", b.out))
err = os.MkdirAll(b.out, os.ModeDir)
if err != nil {
return err
Expand All @@ -193,7 +200,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("[MOVING EXECUTABLE]", zap.String("file", filepath.Join(b.rrTempPath, executableName)), zap.String("to", filepath.Join(b.out, executableName)))
b.log.Info("moving binary", slog.String("file", filepath.Join(b.rrTempPath, executableName)), slog.String("to", filepath.Join(b.out, executableName)))
err = moveFile(filepath.Join(b.rrTempPath, executableName), filepath.Join(b.out, executableName))
if err != nil {
return err
Expand All @@ -203,7 +210,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

func (b *Builder) Write(d []byte) (int, error) {
b.log.Debug("[STDERR OUTPUT]", zap.ByteString("log", d))
b.log.Debug("[STDERR OUTPUT]", slog.Any("log", d))
return len(d), nil
}

Expand Down Expand Up @@ -232,25 +239,38 @@ func randStringBytes(n int) string {

func (b *Builder) goBuildCmd(out string) error {
var cmd *exec.Cmd
if len(b.buildArgs) != 0 {
buildCmdArgs := make([]string, 0, len(b.buildArgs)+5)
buildCmdArgs = append(buildCmdArgs, "build")
// verbose
buildCmdArgs = append(buildCmdArgs, "-v")
// build args
buildCmdArgs = append(buildCmdArgs, b.buildArgs...)
// output file
buildCmdArgs = append(buildCmdArgs, "-o")
// path
buildCmdArgs = append(buildCmdArgs, out)
// path to main.go
buildCmdArgs = append(buildCmdArgs, rrMainGo)
cmd = exec.Command("go", buildCmdArgs...)
} else {
cmd = exec.Command("go", "build", "-o", out, rrMainGo)
}

b.log.Info("[EXECUTING CMD]", zap.String("cmd", cmd.String()))

buildCmdArgs := make([]string, 0, 5)
buildCmdArgs = append(buildCmdArgs, "build", "-v", "-trimpath")

// var ld []string
switch b.debug {
case true:
// debug flags
// turn off optimizations
buildCmdArgs = append(buildCmdArgs, "-gcflags", "-N")
// turn off inlining
buildCmdArgs = append(buildCmdArgs, "-gcflags", "-l")
// build with debug tags
buildCmdArgs = append(buildCmdArgs, "-tags", "debug")
case false:
buildCmdArgs = append(buildCmdArgs, "-ldflags", "-s")
}

// LDFLAGS for version and build time, always appended
buildCmdArgs = append(buildCmdArgs, "-ldflags")
buildCmdArgs = append(buildCmdArgs, fmt.Sprintf(ldflags, b.rrVersion, time.Now().UTC().Format(time.RFC3339)))

// output
buildCmdArgs = append(buildCmdArgs, "-o")
// path
buildCmdArgs = append(buildCmdArgs, out)
// path to main.go
buildCmdArgs = append(buildCmdArgs, rrMainGo)

cmd = exec.Command("go", buildCmdArgs...)

b.log.Info("building RoadRunner", slog.String("cmd", cmd.String()))
cmd.Stderr = b
cmd.Stdout = b
err := cmd.Start()
Expand All @@ -265,7 +285,7 @@ func (b *Builder) goBuildCmd(out string) error {
}

func (b *Builder) goModDowloadCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod download"))
b.log.Info("downloading dependencies", slog.String("cmd", "go mod download"))
cmd := exec.Command("go", "mod", "download")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -280,7 +300,7 @@ func (b *Builder) goModDowloadCmd() error {
}

func (b *Builder) goModTidyCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy"))
b.log.Info("updating dependencies", slog.String("cmd", "go mod tidy"))
cmd := exec.Command("go", "mod", "tidy")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -295,7 +315,7 @@ func (b *Builder) goModTidyCmd() error {
}

func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
b.log.Info("[REPLACING DEPENDENCIES]", zap.String("dependency", repl))
b.log.Info("found replace, processing", slog.String("dependency", repl))
modFile, err := os.ReadFile(path.Join(repl, goModStr))
if err != nil {
return nil
Expand All @@ -306,7 +326,7 @@ func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
for i := 0; i < len(replaces); i++ {
split := strings.Split(strings.TrimSpace(replaces[i][0]), " => ")
if len(split) != 2 {
b.log.Error("not enough split args", zap.String("replace", replaces[i][0]))
b.log.Error("not enough split args", slog.String("replace", replaces[i][0]))
continue
}

Expand Down
4 changes: 2 additions & 2 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package builder

import (
"log/slog"
"os"
"path"
"testing"

"github.com/roadrunner-server/velox/v2024"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ func setup() *Builder {
"dummy_multiple_absolute_remote": []byte(replaceGoModMultipleRemote),
}

b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", zap.NewNop(), []string{})
b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", "v2024.1.0", false, slog.Default())

b.modules = []*velox.ModulesInfo{
{
Expand Down
Loading
Loading