-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
314 lines (277 loc) · 8.15 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
)
type configsModel struct {
stfHostURL string
stfAccessToken string
deviceFilter string
deviceNumberLimit int
adbKeyPub string
adbKey string
}
//Device ...
type Device struct {
Serial string `json:"serial"`
}
//RemoteConnection ...
type RemoteConnection struct {
RemoteConnectURL string `json:"remoteConnectUrl"`
}
const devicesEndpoint = "/api/v1/devices"
const userDevicesEndpoint = "/api/v1/user/devices"
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var client = &http.Client{Timeout: time.Second * 30}
func main() {
configs := createConfigsModelFromEnvs()
configs.dump()
if err := configs.validate(); err != nil {
log.Errorf("Could not validate config, error: %s", err)
os.Exit(1)
}
serials, err := getSerials(configs)
if err != nil {
log.Errorf("Could not get device serials, error: %s", err)
os.Exit(2)
}
homeDir, err := getHomeDir()
if err != nil {
log.Errorf("Could not determine current user home directory, error: %s", err)
os.Exit(3)
}
if err := setAdbKeys(configs, homeDir); err != nil {
log.Errorf("Could not set ADB keys, error: %s", err)
os.Exit(4)
}
deviceCount := calculateDeviceCount(configs, serials)
var connectedDeviceSerials []string
connectedDeviceCount := 0
for _, serial := range serials {
if err := connectDeviceToADB(configs, serial); err != nil {
log.Warnf("Device %s ignored, error: %s", serial, err)
} else {
connectedDeviceCount++
connectedDeviceSerials = append(connectedDeviceSerials, serial)
}
if connectedDeviceCount >= deviceCount {
break
}
}
if err := exportArrayWithEnvman("STF_DEVICE_SERIAL_LIST", connectedDeviceSerials); err != nil {
log.Errorf("Could export device serials with envman, error: %s", err)
os.Exit(5)
}
if connectedDeviceCount == 0 {
log.Errorf("No devices can be connected to ADB")
os.Exit(6)
}
}
func calculateDeviceCount(configs configsModel, serials []string) int {
if configs.deviceNumberLimit > 0 && configs.deviceNumberLimit < len(serials) {
return configs.deviceNumberLimit
}
return len(serials)
}
func connectDeviceToADB(configs configsModel, serial string) error {
if err := addDeviceUnderControl(configs, serial); err != nil {
return fmt.Errorf("could not add device under control, error: %s", err)
}
remoteConnectURL, err := getRemoteConnectURL(configs, serial)
if err != nil {
return fmt.Errorf("could not get remote connect URL, error: %s", err)
}
if err := connectToAdb(remoteConnectURL); err != nil {
return fmt.Errorf("could not connect to ADB, error: %s", err)
}
return nil
}
func createConfigsModelFromEnvs() configsModel {
return configsModel{
stfHostURL: os.Getenv("stf_host_url"),
stfAccessToken: os.Getenv("stf_access_token"),
deviceFilter: getEnvOrDefault("device_filter", "."),
deviceNumberLimit: parseIntSafely(getEnvOrDefault("device_number_limit", "0")),
adbKeyPub: os.Getenv("adb_key_pub"),
adbKey: os.Getenv("adb_key"),
}
}
func getEnvOrDefault(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
func parseIntSafely(limit string) int {
i, err := strconv.Atoi(limit)
if err != nil {
return 0
}
return i
}
func (configs configsModel) dump() {
log.Infof("Config:")
log.Infof("STF host: %s", configs.stfHostURL)
log.Infof("Device filter: %s", configs.deviceFilter)
log.Infof("Device number limit: %d", configs.deviceNumberLimit)
}
func (configs *configsModel) validate() error {
if !strings.HasPrefix(configs.stfHostURL, "http") {
return fmt.Errorf("invalid STF host: %s", configs.stfHostURL)
}
if configs.stfAccessToken == "" {
return errors.New("STF access token cannot be empty")
}
return nil
}
func (configs *configsModel) isAnyAdbKeySet() bool {
return configs.adbKey != "" || configs.adbKeyPub != ""
}
func setAdbKeys(configs configsModel, homeDir string) error {
if err := saveNonEmptyAdbKey(configs.adbKey, homeDir, "adbkey", 0600); err != nil {
return err
}
if err := saveNonEmptyAdbKey(configs.adbKeyPub, homeDir, "adbkey.pub", 0644); err != nil {
return err
}
if configs.isAnyAdbKeySet() {
return command.RunCommand("adb", "kill-server")
}
return nil
}
func saveNonEmptyAdbKey(key, homeDir, fileName string, mode os.FileMode) error {
if key != "" {
adbKeyPath := filepath.Join(homeDir, ".android", fileName)
if err := ioutil.WriteFile(adbKeyPath, []byte(key), mode); err != nil {
return err
}
}
return nil
}
func getHomeDir() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
return currentUser.HomeDir, nil
}
func connectToAdb(remoteConnectURL string) error {
log.Infof("Connecting ADB to %s", remoteConnectURL)
output, err := command.RunCommandAndReturnCombinedStdoutAndStderr("adb", "connect", remoteConnectURL)
if err != nil {
return err
}
log.Debugf(string(output))
return nil
}
func getRemoteConnectURL(configs configsModel, serial string) (string, error) {
req, err := http.NewRequest("POST", configs.stfHostURL+userDevicesEndpoint+"/"+serial+"/remoteConnect", nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+configs.stfAccessToken)
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
if err != nil {
return "", err
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Warnf("Failed to close response body, error: %s", err)
}
}()
bodyBytes, err := ioutil.ReadAll(response.Body)
if response.StatusCode != 200 {
return "", fmt.Errorf("request failed, status: %s | body: %s", response.Status, string(bodyBytes))
}
if err != nil {
return "", err
}
var remoteConnection RemoteConnection
err = json.Unmarshal(bodyBytes, &remoteConnection)
return remoteConnection.RemoteConnectURL, err
}
func addDeviceUnderControl(configs configsModel, serial string) error {
device := &Device{Serial: serial}
body, err := json.Marshal(device)
if err != nil {
return err
}
req, err := http.NewRequest("POST", configs.stfHostURL+userDevicesEndpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+configs.stfAccessToken)
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
if err != nil {
return err
}
if err := response.Body.Close(); err != nil {
return err
}
if response.StatusCode != 200 {
return fmt.Errorf("request failed, status: %s", response.Status)
}
return nil
}
func getSerials(configs configsModel) ([]string, error) {
req, err := http.NewRequest("GET", configs.stfHostURL+devicesEndpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+configs.stfAccessToken)
response, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Warnf("Failed to close response body, error: %s", err)
}
}()
if response.StatusCode != 200 {
return nil, fmt.Errorf("request failed, status: %s", response.Status)
}
var stdout, stderr bytes.Buffer
cmd := command.New("jq", "-r", ".devices[] | select(.present and .owner == null and ("+configs.deviceFilter+")) | .serial")
cmd.SetStdin(response.Body)
cmd.SetStdout(&stdout)
cmd.SetStderr(&stderr)
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("could not create GET devices list request, error: %s | output: %s", err, stderr.String())
}
serials := strings.Fields(stdout.String())
if len(serials) == 0 {
return nil, fmt.Errorf("could not find present, not used devices satisfying filter: %s", configs.deviceFilter)
}
shuffleSlice(serials)
return serials, nil
}
func shuffleSlice(slice []string) {
for i := range slice {
j := random.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
func exportArrayWithEnvman(keyStr string, values []string) error {
body, err := json.Marshal(values)
if err != nil {
return err
}
return command.RunCommand("bitrise", "envman", "add", "--key", keyStr, "--value", string(body))
}