-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmassupload.go
192 lines (169 loc) · 4.92 KB
/
massupload.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
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ota
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
iotclient "github.com/arduino/iot-client-go"
)
const (
numConcurrentUploads = 10
)
// MassUploadParams contains the parameters needed to
// perform a Mass OTA upload.
type MassUploadParams struct {
DeviceIDs []string
Tags map[string]string
File string
Deferred bool
FQBN string
}
// Result of an ota upload on a device.
type Result struct {
ID string
Err error
}
// MassUpload command is used to mass upload a firmware OTA,
// on devices of Arduino IoT Cloud.
func MassUpload(params *MassUploadParams, cred *config.Credentials) ([]Result, error) {
if params.DeviceIDs == nil && params.Tags == nil {
return nil, errors.New("provide either DeviceIDs or Tags")
} else if params.DeviceIDs != nil && params.Tags != nil {
return nil, errors.New("cannot use both DeviceIDs and Tags. only one of them should be not nil")
}
// Generate .ota file
otaDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, fmt.Errorf("%s: %w", "cannot create temporary folder", err)
}
otaFile := filepath.Join(otaDir, "temp.ota")
defer os.RemoveAll(otaDir)
err = Generate(params.File, otaFile, params.FQBN)
if err != nil {
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
}
iotClient, err := iot.NewClient(cred)
if err != nil {
return nil, err
}
// Prepare the list of device-ids to update
d, err := idsGivenTags(iotClient, params.Tags)
if err != nil {
return nil, err
}
d = append(params.DeviceIDs, d...)
valid, invalid, err := validateDevices(iotClient, d, params.FQBN)
if err != nil {
return nil, fmt.Errorf("failed to validate devices: %w", err)
}
if len(valid) == 0 {
return invalid, nil
}
expiration := otaExpirationMins
if params.Deferred {
expiration = otaDeferredExpirationMins
}
res := run(iotClient, valid, otaFile, expiration)
res = append(res, invalid...)
return res, nil
}
type deviceLister interface {
DeviceList(tags map[string]string) ([]iotclient.ArduinoDevicev2, error)
}
func idsGivenTags(lister deviceLister, tags map[string]string) ([]string, error) {
if tags == nil {
return nil, nil
}
devs, err := lister.DeviceList(tags)
if err != nil {
return nil, fmt.Errorf("%s: %w", "cannot retrieve devices from cloud", err)
}
devices := make([]string, 0, len(devs))
for _, d := range devs {
devices = append(devices, d.Id)
}
return devices, nil
}
func validateDevices(lister deviceLister, ids []string, fqbn string) (valid []string, invalid []Result, err error) {
devs, err := lister.DeviceList(nil)
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", "cannot retrieve devices from cloud", err)
}
for _, id := range ids {
var found *iotclient.ArduinoDevicev2
for _, d := range devs {
if d.Id == id {
found = &d
break
}
}
// Device not found on the cloud
if found == nil {
inv := Result{ID: id, Err: fmt.Errorf("not found")}
invalid = append(invalid, inv)
continue
}
// Device FQBN doesn't match the passed one
if found.Fqbn != fqbn {
inv := Result{ID: id, Err: fmt.Errorf("has FQBN '%s' instead of '%s'", found.Fqbn, fqbn)}
invalid = append(invalid, inv)
continue
}
valid = append(valid, id)
}
return valid, invalid, nil
}
type otaUploader interface {
DeviceOTA(id string, file *os.File, expireMins int) error
}
func run(uploader otaUploader, ids []string, otaFile string, expiration int) []Result {
type job struct {
id string
file *os.File
}
jobs := make(chan job, len(ids))
resCh := make(chan Result, len(ids))
results := make([]Result, 0, len(ids))
for _, id := range ids {
file, err := os.Open(otaFile)
if err != nil {
r := Result{ID: id, Err: fmt.Errorf("cannot open ota file")}
results = append(results, r)
continue
}
jobs <- job{id: id, file: file}
}
close(jobs)
for i := 0; i < numConcurrentUploads; i++ {
go func() {
for job := range jobs {
err := uploader.DeviceOTA(job.id, job.file, expiration)
resCh <- Result{ID: job.id, Err: err}
}
}()
}
for range ids {
r := <-resCh
results = append(results, r)
}
return results
}