-
Notifications
You must be signed in to change notification settings - Fork 3
/
bricks.go
130 lines (112 loc) · 3.36 KB
/
bricks.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
package mageutil
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// StopBinaries iterates over all binary files and terminates their corresponding processes.
func StopBinaries() {
for binary := range serviceBinaries {
fullPath := GetBinFullPath(binary)
KillExistBinary(fullPath)
}
}
// StartBinaries Start all binary services.
func StartBinaries() error {
for binary, count := range serviceBinaries {
binFullPath := filepath.Join(OpenIMOutputHostBin, binary)
for i := 0; i < count; i++ {
args := []string{"-i", strconv.Itoa(i), "-c", OpenIMOutputConfig}
cmd := exec.Command(binFullPath, args...)
fmt.Printf("Starting %s\n", cmd.String())
cmd.Dir = OpenIMOutputHostBin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start %s with args %v: %v", binFullPath, args, err)
}
}
}
return nil
}
// StartTools starts all tool binaries.
func StartTools() error {
for _, tool := range toolBinaries {
toolFullPath := GetToolFullPath(tool)
cmd := exec.Command(toolFullPath, "-c", OpenIMOutputConfig)
fmt.Printf("Starting %s\n", cmd.String())
cmd.Dir = OpenIMOutputHostBinTools
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start %s with error: %v", toolFullPath, err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("failed to execute %s with exit code: %v", toolFullPath, err)
}
fmt.Printf("Starting %s successfully \n", cmd.String())
}
return nil
}
// KillExistBinaries iterates over all binary files and kills their corresponding processes.
func KillExistBinaries() {
var paths []string
for binary := range serviceBinaries {
fullPath := GetBinFullPath(binary)
paths = append(paths, fullPath)
}
BatchKillExistBinaries(paths)
}
// CheckBinariesStop checks if all binary files have stopped and returns an error if there are any binaries still running.
func CheckBinariesStop() error {
var runningBinaries []string
ps, err := FetchProcesses()
if err != nil {
return err
}
for binary := range serviceBinaries {
fullPath := GetBinFullPath(binary)
if CheckProcessInMap(ps, fullPath) {
runningBinaries = append(runningBinaries, binary)
}
}
if len(runningBinaries) > 0 {
return fmt.Errorf("the following binaries are still running: %s", strings.Join(runningBinaries, ", "))
}
return nil
}
// CheckBinariesRunning checks if all binary files are running as expected and returns any errors encountered.
func CheckBinariesRunning() error {
var errorMessages []string
ps, err := FetchProcesses()
if err != nil {
return err
}
for binary, expectedCount := range serviceBinaries {
fullPath := GetBinFullPath(binary)
err := CheckProcessNames(fullPath, expectedCount, ps)
if err != nil {
errorMessages = append(errorMessages, fmt.Sprintf("binary %s is not running as expected: %v", binary, err))
}
}
if len(errorMessages) > 0 {
return fmt.Errorf(strings.Join(errorMessages, "\n"))
}
return nil
}
// PrintListenedPortsByBinaries iterates over all binary files and prints the ports they are listening on.
func PrintListenedPortsByBinaries() error {
ps, err := FindPIDsByBinaryPath()
if err != nil {
return err
}
for binary, _ := range serviceBinaries {
basePath := GetBinFullPath(binary)
fullPath := basePath
PrintBinaryPorts(fullPath, ps)
}
return nil
}