forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
163 lines (144 loc) · 3.62 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
// 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"
"fmt"
"net"
"os"
"os/exec"
"strings"
"github.com/magefile/mage/sh"
"github.com/livekit/mageutil"
)
var Default = Build
const livekitServerVersion = "master"
func Build() error {
fmt.Println("building...")
cmd := exec.Command("go", "build", ".")
connectStd(cmd)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func connectStd(cmd *exec.Cmd) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
const (
testApiKey = "devkey: secret"
)
func getLocalIPAddresses() ([]string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
loopBacks := make([]string, 0)
addresses := make([]string, 0)
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
var ip net.IP
switch typedAddr := addr.(type) {
case *net.IPNet:
ip = typedAddr.IP.To4()
case *net.IPAddr:
ip = typedAddr.IP.To4()
default:
continue
}
if ip == nil {
continue
}
if ip.IsLoopback() {
loopBacks = append(loopBacks, ip.String())
} else {
addresses = append(addresses, ip.String())
}
}
}
if len(addresses) > 0 {
return addresses, nil
}
if len(loopBacks) > 0 {
return loopBacks, nil
}
return nil, fmt.Errorf("could not find local IP address")
}
func Test() error {
fmt.Println("testing local packages...")
if err := mageutil.Run(context.Background(), "go test -short ./pkg/... -count=1"); err != nil {
return err
}
fmt.Println("starting livekit-server...")
confString := `
port: 7880
bind_addresses:
- ""
rtc:
udp_port: 7882
tcp_port: 7881
turn:
enabled: true
domain: ""
cert_file: ""
key_file: ""
tls_port: 0
udp_port: 3478
relay_range_start: 30000
relay_range_end: 30020
external_tls: false
log_level: debug
logging:
pion_level: info
`
parameters := []string{
`run`, `-e`, `LIVEKIT_KEYS=` + testApiKey, `-e`, `LIVEKIT_CONFIG=` + string(confString),
`-d`, `--rm`, `-p7880:7880`, `-p7881:7881`, `-p7882:7882/udp`, `-p3478:3478/udp`,
}
for p := 30000; p <= 30020; p++ {
parameters = append(parameters, fmt.Sprintf("-p%d:%d/udp", p, p))
}
parameters = append(parameters, []string{`--name`, `livekit-server`, `livekit/livekit-server:` + livekitServerVersion}...)
if addresses, _ := getLocalIPAddresses(); len(addresses) > 0 {
fmt.Println("set node ip", addresses[0])
parameters = append(parameters, `--node-ip`, addresses[0])
}
if err := sh.RunV(`docker`, parameters...); err != nil {
return err
}
defer func() {
fmt.Println("stop livekit-server...")
run(nil, "docker stop livekit-server")
}()
fmt.Println("testing...")
testflags := os.Getenv("TestFlags")
return run(map[string]string{"LIVEKIT_KEYS": testApiKey}, `go test -race `+testflags)
}
func run(env map[string]string, commands ...string) error {
for _, command := range commands {
args := strings.Split(command, " ")
_, err := sh.Exec(env, os.Stdout, os.Stderr, args[0], args[1:]...)
if err != nil {
return err
}
}
return nil
}