/
distributor.go
197 lines (166 loc) · 6.05 KB
/
distributor.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
package saltboot
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"sync"
"fmt"
"io"
"mime/multipart"
"github.com/hortonworks/salt-bootstrap/saltboot/model"
)
func DistributeRequest(clients []string, endpoint string, user string, pass string, signature string, signed string) <-chan model.Response {
var wg sync.WaitGroup
wg.Add(len(clients))
c := make(chan model.Response, len(clients))
for idx, client := range clients {
go func(client string, index int) {
defer wg.Done()
log.Printf("[distribute] Send request to client: %s", client)
var clientAddr string
if strings.Contains(client, ":") {
clientAddr = client
} else {
clientAddr = client + ":" + strconv.Itoa(DetermineBootstrapPort())
}
req, err := http.NewRequest("POST", "http://"+clientAddr+endpoint+"?index="+strconv.Itoa(index), bytes.NewBufferString(signed))
req.Header.Set(SIGNATURE, signature)
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, pass)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("[distribute] [ERROR] Failed to send request to: %s, error: %s", client, err.Error())
c <- model.Response{StatusCode: http.StatusInternalServerError, ErrorText: err.Error(), Address: client}
return
}
body, _ := ioutil.ReadAll(resp.Body)
decoder := json.NewDecoder(strings.NewReader(string(body)))
var response model.Response
if err := decoder.Decode(&response); err != nil {
log.Printf("[distribute] [ERROR] Failed to decode response, error: %s", err.Error())
}
response.Address = client
if response.StatusCode == 0 {
response.StatusCode = resp.StatusCode
}
log.Printf("[distribute] Request to: %s result: %s", client, response.String())
c <- response
defer closeIt(resp.Body)
}(client, idx)
}
wg.Wait()
close(c)
return c
}
func Distribute(clients []string, payload []byte, endpoint string, user string, pass string) <-chan model.Response {
var wg sync.WaitGroup
wg.Add(len(clients))
c := make(chan model.Response, len(clients))
for _, client := range clients {
go func(client string) {
defer wg.Done()
log.Printf("[distribute] Send request to client: %s", client)
var clientAddr string
if strings.Contains(client, ":") {
clientAddr = client
} else {
clientAddr = client + ":" + strconv.Itoa(DetermineBootstrapPort())
}
req, err := http.NewRequest("POST", "http://"+clientAddr+endpoint, bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, pass)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("[distribute] [ERROR] Failed to send request to: %s, error: %s", client, err.Error())
c <- model.Response{StatusCode: http.StatusInternalServerError, ErrorText: err.Error(), Address: client}
return
}
body, _ := ioutil.ReadAll(resp.Body)
decoder := json.NewDecoder(strings.NewReader(string(body)))
var response model.Response
if err := decoder.Decode(&response); err != nil {
log.Printf("[distribute] [ERROR] Failed to decode response, error: %s", err.Error())
}
response.Address = client
if response.StatusCode == 0 {
response.StatusCode = resp.StatusCode
}
log.Printf("[distribute] Request to: %s result: %s", client, response.String())
c <- response
defer closeIt(resp.Body)
}(client)
}
wg.Wait()
close(c)
return c
}
func DistributeFileUploadRequest(endpoint string, user string, pass string, targets []string, path string,
permissions string, file multipart.File, header *multipart.FileHeader, signature string) <-chan model.Response {
var wg sync.WaitGroup
wg.Add(len(targets))
c := make(chan model.Response, len(targets))
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
_ = bodyWriter.WriteField("path", path)
_ = bodyWriter.WriteField("permissions", permissions)
fileWriter, err := bodyWriter.CreateFormFile("file", header.Filename)
if err != nil {
log.Println("[DistributeFileUploadRequest] error writing file header to buffer")
for _, target := range targets {
c <- model.Response{StatusCode: http.StatusInternalServerError, ErrorText: err.Error(), Address: target}
}
return c
}
_, err = io.Copy(fileWriter, file)
if err != nil {
fmt.Println("[DistributeFileUploadRequest] error writing file content to buffer")
for _, target := range targets {
c <- model.Response{StatusCode: http.StatusInternalServerError, ErrorText: err.Error(), Address: target}
}
return c
}
closeIt(bodyWriter)
fileContent := bodyBuf.Bytes()
for i, target := range targets {
go func(target string, index int) {
defer wg.Done()
log.Printf("[DistributeFileUploadRequest] Send file upload request to target: %s", target)
var targetAddress string
if strings.Contains(target, ":") {
targetAddress = target
} else {
targetAddress = target + ":" + strconv.Itoa(DetermineBootstrapPort())
}
req, err := http.NewRequest("POST", "http://"+targetAddress+endpoint, bytes.NewReader(fileContent))
req.Header.Set(SIGNATURE, signature)
req.Header.Set("Content-Type", bodyWriter.FormDataContentType())
req.SetBasicAuth(user, pass)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("[DistributeFileUploadRequest] [ERROR] Failed to send request to: %s, error: %s", target, err.Error())
c <- model.Response{StatusCode: http.StatusInternalServerError, ErrorText: err.Error(), Address: target}
return
}
body, _ := ioutil.ReadAll(resp.Body)
defer closeIt(resp.Body)
if resp.StatusCode != http.StatusCreated {
log.Printf("[DistributeFileUploadRequest] Error response from: %s, error: %s", target, body)
c <- model.Response{StatusCode: resp.StatusCode, ErrorText: string(body), Address: target}
return
} else {
log.Printf("[DistributeFileUploadRequest] Request to: %s result: %s", target, body)
c <- model.Response{StatusCode: http.StatusCreated, Status: string(body), Address: target}
}
}(target, i)
}
wg.Wait()
close(c)
return c
}