This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
reg.go
211 lines (192 loc) · 6.7 KB
/
reg.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package agent
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/docker/dockercloud-agent/utils"
)
type RegResponseForm struct {
UserCaCert string `json:"user_ca_cert"`
UUID string `json:"uuid"`
CertCommonName string `json:"external_fqdn"`
DockerTarURL string `json:"docker_url"`
NgrokTarURL string `json:"ngrok_url"`
PublicIpAddress string `json:"public_ip"`
}
type RegPostForm struct {
AgentVersion string `json:"agent_version"`
}
type RegPatchForm struct {
Public_cert string `json:"public_cert"`
AgentVersion string `json:"agent_version"`
DockerVersion string `json:"docker_version,omitempty"`
}
type RegGetForm struct {
AgentVersion string `json:"agent_version"`
DockerUrl string `json:"docker_url"`
ExternalFqdn string `json:"external_fqdn"`
NgrokUrl string `json:"ngrok_url"`
PublicCert string `json:"public_cert"`
ResourceUri string `json:"resource_uri"`
State string `json:"state"`
Tunnel string `json:"tunnel"`
UserCaCert string `json:"user_ca_cert"`
UUID string `json:"uuid"`
NgrokHost string `json:"ngrok_server_addr"`
}
func RegPost(url, caFilePath, configFilePath string) error {
form := RegPostForm{}
form.AgentVersion = VERSION
data, err := json.Marshal(form)
if err != nil {
SendError(err, "Fatal: Json marshal error", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatal("Cannot marshal the POST form", err)
}
return register(url, "POST", Conf.Token, Conf.UUID, caFilePath, configFilePath, data)
}
func RegPatch(url, caFilePath, certFilePath, configFilePath string) error {
form := RegPatchForm{}
form.AgentVersion = VERSION
form.DockerVersion = GetDockerClientVersion(path.Join(DockerHome, DockerBinaryName))
cert, err := GetCertificate(certFilePath)
if err != nil {
SendError(err, "Fatal: Failed to load public certificate", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatal("Cannot read public certificate:", err)
}
form.Public_cert = *cert
data, err := json.Marshal(form)
if err != nil {
SendError(err, "Fatal: Json marshal error", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatal("Cannot marshal the PATCH form", err)
}
return register(url, "PATCH", Conf.Token, Conf.UUID, caFilePath, configFilePath, data)
}
func VerifyRegistration(url string) {
headers := []string{"Authorization TutumAgentToken " + Conf.Token,
"Content-Type application/json",
"User-Agent dockercloud-agent/" + VERSION}
body, err := SendRequest("GET", utils.JoinURL(url, Conf.UUID), nil, headers)
if err != nil {
SendError(err, "SendRequest error", nil)
Logger.Printf("Get registration info error, %s", err)
} else {
var form RegGetForm
if err = json.Unmarshal(body, &form); err != nil {
SendError(err, "Json unmarshal error", nil)
Logger.Println("Cannot unmarshal the response", err)
} else {
if form.State == "Deployed" {
Logger.Printf("Node registration to %s succeeded", Conf.Host)
return
}
}
}
time.Sleep(5 * time.Minute)
body, err = SendRequest("GET", utils.JoinURL(url, Conf.UUID), nil, headers)
if err != nil {
SendError(err, "Failed to get registration info after 5 minutes", nil)
Logger.Printf("Get registration info error, %s", err)
} else {
var form RegGetForm
if err = json.Unmarshal(body, &form); err != nil {
SendError(err, "Json unmarshal error", nil)
Logger.Println("Cannot unmarshal the response", err)
} else {
if form.State == "Deployed" {
Logger.Printf("Node registration to %s succeeded", Conf.Host)
} else {
Logger.Printf("Node registration to %s timed out", Conf.Host)
Logger.Println("Node state:", form.State)
}
}
}
}
func register(url, method, token, uuid, caFilePath, configFilePath string, data []byte) error {
if token == "" {
fmt.Fprintf(os.Stderr, "The token is empty. Please run 'dockercloud-agent set Token=xxx' first!\n")
os.RemoveAll(AgentPidFile)
Logger.Fatal("The token is empty. Please run 'dockercloud-agent set Token=xxx' first!")
}
for i := 1; ; i *= 2 {
if i > MaxWaitingTime {
i = 1
}
body, err := sendRegRequest(url, method, token, uuid, data)
if err == nil {
if err = handleRegResponse(body, caFilePath, configFilePath); err == nil {
return nil
} else {
Logger.Printf("Failed to handle the registration response, %s. Retry in %d seconds", err, i)
time.Sleep(time.Duration(i) * time.Second)
continue
}
}
if method == "POST" && (err.Error() == "401") {
SendError(err, "Registration unauthorized: POST", nil)
Logger.Print("Cannot register node in Docker Cloud: unauthorized. Please try again with a new token.")
Logger.Print("Removing the invalid token from config file")
os.RemoveAll(AgentPidFile)
Conf.Token = ""
if err := SaveConf(path.Join(AgentHome, ConfigFileName), Conf); err != nil {
SendError(err, "Failed to save config to the conf file", nil)
Logger.Print(err)
}
time.Sleep(10 * time.Second)
Logger.Fatal("Docker Cloud agent is terminated")
}
if method == "PATCH" && (err.Error() == "404" || err.Error() == "401") {
return err
}
SendError(err, "Registration HTTP error", nil)
Logger.Printf("Registration failed, %s. Retry in %d seconds", err, i)
time.Sleep(time.Duration(i) * time.Second)
}
}
func sendRegRequest(url, method, token, uuid string, data []byte) ([]byte, error) {
headers := []string{"Authorization TutumAgentToken " + token,
"Content-Type application/json",
"User-Agent dockercloud-agent/" + VERSION}
return SendRequest(method, utils.JoinURL(url, uuid), data, headers)
}
func handleRegResponse(body []byte, caFilePath, configFilePath string) error {
var responseForm RegResponseForm
// Save user ca cert file
if err := json.Unmarshal(body, &responseForm); err != nil {
SendError(err, "Json unmarshal error", nil)
Logger.Println("Failed to unmarshal the response", err)
return err
}
if err := ioutil.WriteFile(caFilePath, []byte(responseForm.UserCaCert), 0644); err != nil {
SendError(err, "Failed to save user ca cert file", nil)
Logger.Println("Failed to save", caFilePath, err)
return err
}
// Update global Conf
isModified := false
if Conf.CertCommonName != responseForm.CertCommonName {
Logger.Printf("Cert CommonName has been changed from %s to %s", Conf.CertCommonName, responseForm.CertCommonName)
isModified = true
Conf.CertCommonName = responseForm.CertCommonName
}
if Conf.UUID != responseForm.UUID {
Logger.Printf("UUID has been changed from %s to %s", Conf.UUID, responseForm.UUID)
isModified = true
Conf.UUID = responseForm.UUID
}
DockerTarURL = responseForm.DockerTarURL
if responseForm.NgrokTarURL != "" {
NgrokTarURL = responseForm.NgrokTarURL
}
// Save to configuration file
if isModified {
Logger.Println("Updating configuration file...")
return SaveConf(configFilePath, Conf)
}
return nil
}