forked from chrislusf/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_server_executor_start.go
114 lines (99 loc) · 2.84 KB
/
agent_server_executor_start.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
package agent
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
"github.com/chrislusf/glow/driver/cmd"
"github.com/chrislusf/glow/driver/rsync"
"github.com/chrislusf/glow/resource"
"github.com/golang/protobuf/proto"
)
func (as *AgentServer) handleStart(conn net.Conn,
startRequest *cmd.StartRequest) *cmd.StartResponse {
reply := &cmd.StartResponse{}
stat := as.localExecutorManager.getExecutorStatus(startRequest.GetHashCode())
stat.RequestTime = time.Now()
dir := path.Join(*as.Option.Dir, startRequest.GetDir())
os.MkdirAll(dir, 0755)
err := rsync.FetchFilesTo(startRequest.GetHost()+":"+strconv.Itoa(int(startRequest.GetPort())), dir)
if err != nil {
log.Printf("Failed to download file: %v", err)
reply.Error = proto.String(err.Error())
return reply
}
allocated := resource.ComputeResource{
CPUCount: int(startRequest.GetResource().GetCpuCount()),
MemoryMB: int64(startRequest.GetResource().GetMemory()),
}
as.plusAllocated(allocated)
defer as.minusAllocated(allocated)
stat.StartTime = time.Now()
cmd := exec.Command(
startRequest.GetPath(),
as.adjustArgs(startRequest.Args, startRequest.GetHashCode())...,
)
cmd.Env = startRequest.Envs
cmd.Dir = dir
cmd.Stdout = conn
cmd.Stderr = conn
err = cmd.Start()
if err != nil {
log.Printf("Failed to start command %s under %s: %v",
cmd.Path, cmd.Dir, err)
reply.Error = proto.String(err.Error())
} else {
reply.Pid = proto.Int32(int32(cmd.Process.Pid))
}
stat.Process = cmd.Process
cmd.Wait()
stat.StopTime = time.Now()
// log.Printf("Finish command %v", cmd)
return reply
}
func (as *AgentServer) adjustArgs(args []string, requestId uint32) (ret []string) {
ret = []string{"-glow.request.id", fmt.Sprintf("%d", requestId)}
if as.Option.CertFiles.IsEnabled() {
var cleanedArgs []string
for _, arg := range args {
if strings.Contains(arg, "=") {
cleanedArgs = append(cleanedArgs, strings.SplitN(arg, "=", 2)...)
} else {
cleanedArgs = append(cleanedArgs, arg)
}
}
for i := 0; i < len(cleanedArgs); i++ {
ret = append(ret, cleanedArgs[i])
switch cleanedArgs[i] {
case "-cert.file":
ret = append(ret, as.Option.CertFiles.CertFile)
i++
case "-key.file":
ret = append(ret, as.Option.CertFiles.KeyFile)
i++
case "-ca.file":
ret = append(ret, as.Option.CertFiles.CaFile)
i++
}
}
} else {
ret = append(ret, args...)
}
// fmt.Printf("cmd: %v\n", ret)
return
}
func (as *AgentServer) plusAllocated(allocated resource.ComputeResource) {
as.allocatedResourceLock.Lock()
defer as.allocatedResourceLock.Unlock()
*as.allocatedResource = as.allocatedResource.Plus(allocated)
}
func (as *AgentServer) minusAllocated(allocated resource.ComputeResource) {
as.allocatedResourceLock.Lock()
defer as.allocatedResourceLock.Unlock()
*as.allocatedResource = as.allocatedResource.Minus(allocated)
}