-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.go
79 lines (67 loc) · 1.41 KB
/
server.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
package main
import (
"net"
"os"
"strings"
"google.golang.org/grpc"
"yunion.io/x/log"
"yunion.io/x/executor/apis"
"yunion.io/x/executor/server"
)
type SExecuteService struct {
}
func NewExecuteService() *SExecuteService {
return &SExecuteService{}
}
func (s *SExecuteService) fixPathEnv() error {
var paths = []string{
"/usr/local/sbin",
"/usr/local/bin",
"/sbin",
"/bin",
"/usr/sbin",
"/usr/bin",
}
return os.Setenv("PATH", strings.Join(paths, ":"))
}
func (s *SExecuteService) prepareEnv() error {
if err := s.fixPathEnv(); err != nil {
return err
}
return nil
}
func (s *SExecuteService) runService() {
grpcServer := grpc.NewServer()
apis.RegisterExecutorServer(grpcServer, &server.Executor{})
if _, err := os.Stat(socketPath); !os.IsNotExist(err) {
// socket file already exist, remove first
if err := os.Remove(socketPath); err != nil {
log.Fatalln(err)
}
}
listener, err := net.Listen("unix", socketPath)
if err != nil {
log.Fatalln(err)
}
defer listener.Close()
log.Infof("Init net listener on %s succ", socketPath)
err = grpcServer.Serve(listener)
if err != nil {
log.Fatalln(err)
}
}
func (s *SExecuteService) initService() {
if len(socketPath) == 0 {
log.Fatalf("missing socket path")
}
if err := s.prepareEnv(); err != nil {
log.Fatalln(err)
}
}
func (s *SExecuteService) Run() {
s.initService()
s.runService()
}
func Server() {
NewExecuteService().Run()
}