Skip to content

Commit

Permalink
show right server version when server is compiled from source (#3409)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 5, 2024
1 parent 2586782 commit c716947
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 30 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/binaries
/coverage*.txt
/apidocs/*.html
/internal/core/VERSION
/internal/servers/hls/hls.min.js
/internal/staticsources/rpicamera/mtxrpicam_*
4 changes: 3 additions & 1 deletion .github/workflows/code_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v3
with:
go-version: "1.22"

- run: touch internal/servers/hls/hls.min.js
- run: go generate ./...

- uses: golangci/golangci-lint-action@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/code_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: make test

Expand All @@ -24,6 +26,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: make test32

Expand All @@ -32,6 +36,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/nightly_binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: make binaries

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: make binaries

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/binaries
/coverage*.txt
/apidocs/*.html
/internal/core/VERSION
/internal/servers/hls/hls.min.js
/internal/staticsources/rpicamera/mtxrpicam_*
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/datarhei/gosrt v0.7.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/go-git/go-git/v5 v5.12.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/gookit/color v1.5.4
Expand All @@ -36,20 +37,31 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/asticode/go-astikit v0.30.0 // indirect
github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -65,15 +77,23 @@ require (
github.com/pion/stun v0.6.1 // indirect
github.com/pion/transport/v2 v2.2.4 // indirect
github.com/pion/turn/v2 v2.1.3 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
89 changes: 82 additions & 7 deletions go.sum

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core

import (
"context"
_ "embed"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -33,7 +34,10 @@ import (
"github.com/bluenviron/mediamtx/internal/servers/webrtc"
)

var version = "v0.0.0"
//go:generate go run ./versiongetter

//go:embed VERSION
var version []byte

var defaultConfPaths = []string{
"rtsp-simple-server.yml",
Expand Down Expand Up @@ -82,7 +86,7 @@ type Core struct {
// New allocates a Core.
func New(args []string) (*Core, bool) {
parser, err := kong.New(&cli,
kong.Description("MediaMTX "+version),
kong.Description("MediaMTX "+string(version)),
kong.UsageOnError(),
kong.ValueFormatter(func(value *kong.Value) string {
switch value.Name {
Expand Down
111 changes: 111 additions & 0 deletions internal/core/versiongetter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Package main contains an utility to get the server version
package main

import (
"log"
"os"
"strconv"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

// Golang version of git describe --tags
func gitDescribeTags(repo *git.Repository) (string, error) {
head, err := repo.Head()
if err != nil {
return "", err
}

tagIterator, err := repo.Tags()
if err != nil {
return "", err
}
defer tagIterator.Close()

tags := make(map[plumbing.Hash]*plumbing.Reference)

err = tagIterator.ForEach(func(t *plumbing.Reference) error {
if to, err2 := repo.TagObject(t.Hash()); err2 == nil {
tags[to.Target] = t
} else {
tags[t.Hash()] = t
}
return nil
})

if err != nil {
return "", err
}

cIter, err := repo.Log(&git.LogOptions{From: head.Hash()})
if err != nil {
return "", err
}

i := 0

for {
commit, err := cIter.Next()
if err != nil {
return "", err
}

if str, ok := tags[commit.Hash]; ok {
label := strings.TrimPrefix(string(str.Name()), "refs/tags/")

if i != 0 {
label += "-" + strconv.FormatInt(int64(i), 10) + "-" + head.Hash().String()[:8]
}

return label, nil
}

i++
}
}

func do() error {
log.Println("getting mediamtx version...")

repo, err := git.PlainOpen("../..")
if err != nil {
return err
}

version, err := gitDescribeTags(repo)
if err != nil {
return err
}

wt, err := repo.Worktree()
if err != nil {
return err
}

status, err := wt.Status()
if err != nil {
return err
}

if !status.IsClean() {
version += "-dirty"
}

err = os.WriteFile("VERSION", []byte(version), 0o644)
if err != nil {
return err
}

log.Printf("ok (%s)", version)
return nil
}

func main() {
err := do()
if err != nil {
log.Printf("ERR: %v", err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion internal/servers/hls/hlsjsdownloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func do() error {
}
version := strings.TrimSpace(string(buf))

log.Printf("downloading hls.js version %s...", version)
log.Printf("downloading hls.js %s...", version)

res, err := http.Get("https://github.com/video-dev/hls.js/releases/download/" + version + "/release.zip")
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/servers/hls/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
//go:embed index.html
var hlsIndex []byte

//nolint:typecheck
//go:embed hls.min.js
var hlsMinJS []byte

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func do() error {
}
version := strings.TrimSpace(string(buf))

log.Printf("downloading mediamtx-rpicamera version %s...", version)
log.Printf("downloading mediamtx-rpicamera %s...", version)

for _, f := range []string{"mtxrpicam_32.tar.gz", "mtxrpicam_64.tar.gz"} {
err = os.RemoveAll(strings.TrimSuffix(f, ".tar.gz"))
Expand Down
30 changes: 14 additions & 16 deletions scripts/binaries.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ WORKDIR /s
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
ARG VERSION
ENV CGO_ENABLED 0
RUN rm -rf tmp binaries
RUN mkdir tmp binaries
Expand All @@ -16,38 +15,38 @@ RUN go generate ./...

FROM build-base AS build-windows-amd64
ENV GOOS=windows GOARCH=amd64
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME).exe
RUN cd tmp && zip -q ../binaries/$(BINARY_NAME)_$${VERSION}_windows_amd64.zip $(BINARY_NAME).exe mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME).exe
RUN cd tmp && zip -q ../binaries/$(BINARY_NAME)_$$(cat ../internal/core/VERSION)_windows_amd64.zip $(BINARY_NAME).exe mediamtx.yml LICENSE

FROM build-base AS build-linux-amd64
ENV GOOS=linux GOARCH=amd64
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_linux_amd64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_linux_amd64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM build-base AS build-darwin-amd64
ENV GOOS=darwin GOARCH=amd64
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_darwin_amd64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_darwin_amd64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM build-base AS build-darwin-arm64
ENV GOOS=darwin GOARCH=arm64
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_darwin_arm64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_darwin_arm64.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM build-base AS build-linux-armv6
ENV GOOS=linux GOARCH=arm GOARM=6
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_linux_armv6.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_linux_armv6.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM build-base AS build-linux-armv7
ENV GOOS=linux GOARCH=arm GOARM=7
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_linux_armv7.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_linux_armv7.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM build-base AS build-linux-arm64
ENV GOOS=linux GOARCH=arm64
RUN go build -ldflags "-X github.com/bluenviron/mediamtx/internal/core.version=$$VERSION" -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$${VERSION}_linux_arm64v8.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE
RUN go build -o tmp/$(BINARY_NAME)
RUN tar -C tmp -czf binaries/$(BINARY_NAME)_$$(cat internal/core/VERSION)_linux_arm64v8.tar.gz --owner=0 --group=0 $(BINARY_NAME) mediamtx.yml LICENSE

FROM $(BASE_IMAGE)
COPY --from=build-windows-amd64 /s/binaries /s/binaries
Expand All @@ -62,7 +61,6 @@ export DOCKERFILE_BINARIES

binaries:
echo "$$DOCKERFILE_BINARIES" | DOCKER_BUILDKIT=1 docker build . -f - \
--build-arg VERSION=$$(git describe --tags) \
-t temp
docker run --rm -v $(PWD):/out \
temp sh -c "rm -rf /out/binaries && cp -r /s/binaries /out/"
1 change: 0 additions & 1 deletion scripts/lint.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ endef
export DOCKERFILE_APIDOCS_LINT

lint-golangci:
touch internal/servers/hls/hls.min.js
docker run --rm -v $(PWD):/app -w /app \
$(LINT_IMAGE) \
golangci-lint run -v
Expand Down

0 comments on commit c716947

Please sign in to comment.