-
Notifications
You must be signed in to change notification settings - Fork 32
/
magefile.go
208 lines (174 loc) · 5 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build mage
// +build mage
package main
import (
"context"
"encoding/json"
"fmt"
"go/build"
"os"
"os/exec"
"strings"
"github.com/livekit/mageutil"
)
var Default = Build
const (
imageName = "livekit/ingress"
gstVersion = "1.22.5"
goVersion = "1.21.5"
)
var plugins = []string{"gstreamer", "gst-plugins-base", "gst-plugins-good", "gst-plugins-bad", "gst-plugins-ugly", "gst-libav"}
type packageInfo struct {
Dir string
}
func Proto() error {
ctx := context.Background()
fmt.Println("generating protobuf")
// parse go mod output
pkgOut, err := mageutil.Out(ctx, "go list -json -m github.com/livekit/protocol")
if err != nil {
return err
}
pi := packageInfo{}
if err = json.Unmarshal(pkgOut, &pi); err != nil {
return err
}
_, err = mageutil.GetToolPath("protoc")
if err != nil {
return err
}
protocGoPath, err := mageutil.GetToolPath("protoc-gen-go")
if err != nil {
return err
}
protocGrpcGoPath, err := mageutil.GetToolPath("protoc-gen-go-grpc")
if err != nil {
return err
}
// generate grpc-related protos
return mageutil.RunDir(ctx, "pkg/ipc", fmt.Sprintf(
"protoc"+
" --go_out ."+
" --go-grpc_out ."+
" --go_opt=paths=source_relative"+
" --go-grpc_opt=paths=source_relative"+
" --plugin=go=%s"+
" --plugin=go-grpc=%s"+
" -I%s -I=. ipc.proto",
protocGoPath, protocGrpcGoPath, pi.Dir,
))
}
func Bootstrap() error {
brewPrefix, err := getBrewPrefix()
if err != nil {
return err
}
for _, plugin := range plugins {
if _, err := os.Stat(fmt.Sprintf("%s%s", brewPrefix, plugin)); err != nil {
if err = run(fmt.Sprintf("brew install %s", plugin)); err != nil {
return err
}
}
}
return nil
}
func Build() error {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return run(fmt.Sprintf("go build -a -o %s/bin/ingress ./cmd/server", gopath))
}
func Test() error {
return run("go test -v ./pkg/...")
}
func BuildDocker() error {
return mageutil.Run(context.Background(),
fmt.Sprintf("docker pull livekit/gstreamer:%s-dev", gstVersion),
fmt.Sprintf("docker pull livekit/gstreamer:%s-prod", gstVersion),
fmt.Sprintf("docker build --no-cache -t %s:latest -f build/ingress/Dockerfile --build-arg GSTVERSION=%s --build-arg GOVERSION=%s .", imageName, gstVersion, goVersion),
)
}
func BuildDockerLinux() error {
return mageutil.Run(context.Background(),
fmt.Sprintf("docker pull livekit/gstreamer:%s-dev", gstVersion),
fmt.Sprintf("docker pull livekit/gstreamer:%s-prod", gstVersion),
fmt.Sprintf("docker build --no-cache --platform linux/amd64 -t %s:latest -f build/ingress/Dockerfile --build-arg GSTVERSION=%s --build-arg GOVERSION=%s .", imageName, gstVersion, goVersion),
)
}
func Integration(configFile string) error {
if err := Build(); err != nil {
return err
}
return Retest(configFile)
}
func Retest(configFile string) error {
err := WhipClient()
if err != nil {
return err
}
cmd := exec.Command("go", "test", "-v", "-count=1", "--tags=integration", "./test/...")
brewPrefix, err := getBrewPrefix()
if err != nil {
return err
}
var sb strings.Builder
sb.WriteString("GST_PLUGIN_PATH=")
for i, plugin := range plugins {
if i > 0 {
sb.WriteString(":")
}
sb.WriteString(brewPrefix)
sb.WriteString(plugin)
}
confStr, err := os.ReadFile(configFile)
if err != nil {
return err
}
cmd.Env = append(os.Environ(), sb.String(), "GST_DEBUG=3", fmt.Sprintf("INGRESS_CONFIG_BODY=%s", string(confStr)))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func Publish(ingressId string) error {
return run(fmt.Sprintf("gst-launch-1.0 -v flvmux name=mux ! rtmp2sink location=rtmp://localhost:1935/live/%s audiotestsrc freq=200 ! faac ! mux. videotestsrc pattern=ball is-live=true ! video/x-raw,width=1280,height=720 ! x264enc speed-preset=3 ! mux.", ingressId))
}
func WhipClient() error {
return run("go build -C ./test/livekit-whip-bot/cmd/whip-client/ ./...")
}
// helpers
func getBrewPrefix() (string, error) {
out, err := exec.Command("brew", "--prefix").Output()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/Cellar/", strings.TrimSpace(string(out))), nil
}
func run(commands ...string) error {
for _, command := range commands {
args := strings.Split(command, " ")
if err := runArgs(args...); err != nil {
return err
}
}
return nil
}
func runArgs(args ...string) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}