forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostapi.go
148 lines (138 loc) · 4.81 KB
/
hostapi.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
package hostapi
import (
"os"
"time"
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
"github.com/golang/glog"
"github.com/rancher/agent/service/hostapi/config"
"github.com/rancher/agent/service/hostapi/console"
"github.com/rancher/agent/service/hostapi/dockersocketproxy"
"github.com/rancher/agent/service/hostapi/events"
"github.com/rancher/agent/service/hostapi/exec"
"github.com/rancher/agent/service/hostapi/logs"
"github.com/rancher/agent/service/hostapi/proxy"
"github.com/rancher/agent/service/hostapi/stats"
"github.com/rancher/agent/service/hostapi/util"
rclient "github.com/rancher/go-rancher/client"
"github.com/rancher/websocket-proxy/backend"
)
func StartUp() {
for {
err := config.Parse()
if err != nil {
time.Sleep(time.Duration(5) * time.Second)
continue
}
defer glog.Flush()
if config.Config.PidFile != "" {
logrus.Infof("Writing pid %d to %s", os.Getpid(), config.Config.PidFile)
if err := ioutil.WriteFile(config.Config.PidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
logrus.Errorf("Failed to write pid file %s: %v", config.Config.PidFile, err)
time.Sleep(time.Duration(5) * time.Second)
continue
}
}
if config.Config.LogFile != "" {
if output, err := os.OpenFile(config.Config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
logrus.Errorf("Failed to log to file %s: %v", config.Config.LogFile, err)
time.Sleep(time.Duration(5) * time.Second)
continue
} else {
logrus.SetOutput(output)
}
}
processor := events.NewDockerEventsProcessor(config.Config.EventsPoolSize)
err = processor.Process()
if err != nil {
logrus.Error(err)
time.Sleep(time.Duration(5) * time.Second)
continue
}
break
}
for {
rancherClient, err := util.GetRancherClient()
if err != nil {
logrus.Error(err)
time.Sleep(time.Duration(5) * time.Second)
continue
}
tokenRequest := &rclient.HostApiProxyToken{
ReportedUuid: config.Config.HostUUID,
}
tokenResponse, err := getConnectionToken(0, tokenRequest, rancherClient)
if err != nil {
time.Sleep(time.Duration(5) * time.Second)
continue
} else if tokenResponse == nil {
// nil error and blank token means the proxy is turned off. Just block forever so main function doesn't exit
var block chan bool
<-block
}
config.Config.HostUUID = tokenResponse.ReportedUuid
handlers := make(map[string]backend.Handler)
handlers["/v1/logs/"] = &logs.Handler{}
handlers["/v2-beta/logs/"] = &logs.Handler{}
handlers["/v1/stats/"] = &stats.Handler{}
handlers["/v2-beta/stats/"] = &stats.Handler{}
handlers["/v1/hoststats/"] = &stats.HostStatsHandler{}
handlers["/v2-beta/hoststats/"] = &stats.HostStatsHandler{}
handlers["/v1/containerstats/"] = &stats.ContainerStatsHandler{}
handlers["/v2-beta/containerstats/"] = &stats.ContainerStatsHandler{}
handlers["/v1/exec/"] = &exec.Handler{}
handlers["/v2-beta/exec/"] = &exec.Handler{}
handlers["/v1/console/"] = &console.Handler{}
handlers["/v2-beta/console/"] = &console.Handler{}
handlers["/v1/dockersocket/"] = &dockersocketproxy.Handler{}
handlers["/v2-beta/dockersocket/"] = &dockersocketproxy.Handler{}
handlers["/v1/container-proxy/"] = &proxy.Handler{}
handlers["/v2-beta/container-proxy/"] = &proxy.Handler{}
if err := backend.ConnectToProxy(tokenResponse.Url+"?token="+tokenResponse.Token, handlers); err != nil {
logrus.Errorf("failed to connect to proxy. err: %v", err)
time.Sleep(time.Duration(5) * time.Second)
continue
}
}
}
const maxWaitOnHostTries = 20
func getConnectionToken(try int, tokenReq *rclient.HostApiProxyToken, rancherClient *rclient.RancherClient) (*rclient.HostApiProxyToken, error) {
if try >= maxWaitOnHostTries {
return nil, fmt.Errorf("Reached max retry attempts for getting token")
}
tokenResponse, err := rancherClient.HostApiProxyToken.Create(tokenReq)
if err != nil {
if apiError, ok := err.(*rclient.ApiError); ok {
if apiError.StatusCode == 422 {
m := map[string]string{}
apiBody := apiError.Body
fmt.Println(apiBody)
parts := strings.Split(apiBody, ", ")
for _, part := range parts {
data := strings.Split(part, "=")
if len(data) == 2 {
m[data[0]] = data[1]
}
}
if strings.EqualFold(m["code"], "InvalidReference") && strings.EqualFold(m["fieldName"], "reportedUuid") {
logrus.WithField("reportedUuid", config.Config.HostUUID).WithField("Attempt", try).Infof("Host not registered yet. Sleeping 1 second and trying again.")
time.Sleep(time.Second)
try++
return getConnectionToken(try, tokenReq, rancherClient) // Recursion!
}
} else if apiError.StatusCode == 501 {
logrus.Infof("Host-api proxy disabled. Will not connect.")
return nil, nil
}
return nil, err
}
}
return tokenResponse, nil
}
type ParsedError struct {
Code string
FieldName string
}