Skip to content

Commit

Permalink
show right server version when server is compiled from source
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Oct 5, 2024
1 parent 2586782 commit 8881863
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 20 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_*
2 changes: 1 addition & 1 deletion .github/workflows/code_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
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
5 changes: 5 additions & 0 deletions .github/workflows/code_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ jobs:
with:
go-version: "1.22"

- name: GitHub Tag Name example
run: |
echo "Tag name from GITHUB_REF_NAME: $GITHUB_REF_NAME"
echo "Tag name from github.ref_name: ${{ github.ref_name }}"
- run: make test-highlevel-nodocker
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
107 changes: 107 additions & 0 deletions internal/core/versiongetter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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
}

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

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

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 version...")

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

label, 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() {
label += "-dirty"
}

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

log.Println("ok")
return nil
}

func main() {
err := do()
if err != nil {
log.Printf("ERR: %v", err)
os.Exit(1)
}
}
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
15 changes: 7 additions & 8 deletions scripts/binaries.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@ 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 go build -o tmp/$(BINARY_NAME).exe
RUN cd tmp && zip -q ../binaries/$(BINARY_NAME)_$${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 go build -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

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 go build -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

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 go build -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

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 go build -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

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 go build -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

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 go build -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

FROM $(BASE_IMAGE)
Expand All @@ -62,7 +62,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/"
2 changes: 1 addition & 1 deletion scripts/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test-nodocker: test-internal test-core
define DOCKERFILE_TEST
ARG ARCH
FROM $$ARCH/$(BASE_IMAGE)
RUN apk add --no-cache make gcc musl-dev
RUN apk add --no-cache make gcc musl-dev git
WORKDIR /s
COPY go.mod go.sum ./
RUN go mod download
Expand Down

0 comments on commit 8881863

Please sign in to comment.