-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrestapi.go
277 lines (252 loc) · 7.37 KB
/
restapi.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/gorilla/mux"
"github.com/openatx/u2init/flashget"
"github.com/pkg/errors"
"github.com/qiniu/log"
goadb "github.com/yosemite-open/go-adb"
)
const (
PACKAGE_DOWNLOAD = "downloading"
PACKAGE_PUSHING = "pushing"
PACKAGE_INSTALL = "installing"
PACKAGE_FAILURE = "failure"
PACKAGE_SUCCESS = "success"
)
func renderHTML(w http.ResponseWriter, filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
http.Error(w, err.Error(), 404)
return
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
func renderJSON(w http.ResponseWriter, v interface{}, statusCode ...int) {
data, _ := json.Marshal(v)
if len(statusCode) == 1 {
w.WriteHeader(statusCode[0])
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
func renderJSONSuccess(w http.ResponseWriter, v interface{}) {
v2 := map[string]interface{}{
"success": true,
"data": v,
}
renderJSON(w, v2)
}
type ADevice struct {
Serial string `json:"serial"`
Model string `json:"model"`
Product string `json:"product"`
Udid string `json:"udid"`
AgentPort int `json:"agentPort"`
}
type InstallInfo struct {
Id string `json:"id"`
Status string `json:"status"`
Serial string `json:"serial"`
DeviceFilePath string `json:"deviceFilePath"`
Description string `json:"description"`
Downloader *flashget.Downloader `json:"-"`
PushBeganAt time.Time `json:"-"`
}
type PackageManager struct {
downloads map[string]*InstallInfo
dmer *flashget.DownloadManager
mu sync.RWMutex
}
func newPackageManager() *PackageManager {
return &PackageManager{
downloads: make(map[string]*InstallInfo),
dmer: flashget.NewDownloadManager(),
}
}
// func (pm *PackageManager) PushFromUrl(serial, url string) ()
func (pm *PackageManager) handleAPKFromUrl(serial, url string, noInstall bool) (info InstallInfo, err error) {
id := UniqID()
dl, err := pm.dmer.Retrive(url)
if err != nil {
return
}
log.Infof("Serial %s http download process %p", serial, dl)
pm.mu.Lock()
defer pm.mu.Unlock()
insInfo := &InstallInfo{
Id: id,
Serial: serial,
Status: PACKAGE_DOWNLOAD,
Downloader: dl,
}
pm.downloads[id] = insInfo
go func() {
dl.Wait()
if !dl.Finished() {
insInfo.Status = PACKAGE_FAILURE
insInfo.Description = "http download failed: " +
insInfo.Downloader.Status + " " + insInfo.Downloader.Description
return
}
d := adb.Device(goadb.DeviceWithSerial(serial))
f, er := os.Open(dl.Filename)
if er != nil {
insInfo.Status = PACKAGE_FAILURE
insInfo.Description = "open file " + dl.Filename + " error: " + er.Error()
return
}
dstFilepath := fmt.Sprintf("/sdcard/tmp/u2init-%s.apk", id)
insInfo.Status = PACKAGE_PUSHING
insInfo.PushBeganAt = time.Now()
insInfo.DeviceFilePath = dstFilepath
_, er = d.WriteToFile(dstFilepath, f, 0644)
if er != nil {
insInfo.Status = PACKAGE_FAILURE
insInfo.Description = "push file to device err: " + er.Error()
return
}
if noInstall {
insInfo.Description = "Skip install, just pushed"
insInfo.Status = PACKAGE_SUCCESS
return
}
insInfo.Status = PACKAGE_INSTALL
output, er := d.RunTimeoutCommand(time.Minute*5, "pm", "install", "-r", "-t", dstFilepath)
if er != nil {
insInfo.Description = "pm install error: " + er.Error()
insInfo.Status = PACKAGE_FAILURE
return
}
output = strings.TrimSpace(output)
insInfo.Description = output
if strings.Contains(output, "Failure") {
insInfo.Status = PACKAGE_FAILURE
return
}
insInfo.Status = PACKAGE_SUCCESS
}()
return *pm.downloads[id], nil
}
func (pm *PackageManager) Get(id string) (info InstallInfo, err error) {
pm.mu.RLock()
defer pm.mu.RUnlock()
pinfo, exists := pm.downloads[id]
if !exists {
err = errors.New("PackageManager can not found id: " + id)
return
}
return *pinfo, nil
}
func init() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderHTML(w, "index.html")
})
router := mux.NewRouter()
pm := newPackageManager()
pm.dmer.EnableAutoRecycle()
// Note, use router.HandleFunc will redirect /devices to /devices/
http.HandleFunc("/devices", func(w http.ResponseWriter, r *http.Request) {
devs := dm.All()
renderJSONSuccess(w, devs)
})
router.HandleFunc("/devices/{serial}/info", func(w http.ResponseWriter, r *http.Request) {
serial := mux.Vars(r)["serial"]
d, ok := dm.Get(serial)
if !ok {
renderJSON(w, map[string]interface{}{
"success": false,
"description": fmt.Sprintf("serial %s not found", serial),
}, 404)
return
}
renderJSONSuccess(w, d)
})
router.HandleFunc("/devices/{serial}/pkgs", func(w http.ResponseWriter, r *http.Request) {
// check params
serial := mux.Vars(r)["serial"]
url := r.FormValue("url")
noInstall := strings.ToLower(r.FormValue("noInstall")) == "true"
if url == "" {
renderJSON(w, map[string]interface{}{
"success": false,
"description": "url is required",
}, http.StatusBadRequest) // 400
return
}
// check adb device
d := adb.Device(goadb.DeviceWithSerial(serial))
info, err := d.DeviceInfo()
if err != nil {
w.WriteHeader(http.StatusBadRequest) // 400
renderJSON(w, map[string]interface{}{
"success": false,
"description": "device error: " + err.Error(),
}, 500)
return
}
// call download manager to download file
insInfo, err := pm.handleAPKFromUrl(serial, url, noInstall)
if err != nil {
renderJSON(w, map[string]interface{}{
"success": false,
"description": "http download: " + err.Error(),
}, 400)
return
}
renderJSON(w, map[string]interface{}{
"success": true,
"data": map[string]string{
"id": insInfo.Id,
"serial": info.Serial,
"product": info.Product,
"model": info.Model,
},
})
}).Methods("POST")
router.HandleFunc("/devices/{serial}/pkgs/{id}",
func(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
insInfo, err := pm.Get(id)
if err != nil {
renderJSON(w, map[string]interface{}{
"success": false,
"description": "pm error: " + err.Error(),
}, 404)
return
}
if insInfo.Status == PACKAGE_DOWNLOAD {
total := bytefmt.ByteSize(uint64(insInfo.Downloader.ContentLength))
copied := bytefmt.ByteSize(uint64(insInfo.Downloader.Written()))
insInfo.Description = fmt.Sprintf("%s / %s - %s", copied, total, insInfo.Downloader.HumanSpeed())
}
if insInfo.Status == PACKAGE_PUSHING {
d := adb.Device(goadb.DeviceWithSerial(insInfo.Serial))
fileInfo, err := d.Stat(insInfo.DeviceFilePath)
if err == nil {
total := bytefmt.ByteSize(uint64(insInfo.Downloader.ContentLength))
copied := bytefmt.ByteSize(uint64(fileInfo.Size))
speedByte := int(float64(fileInfo.Size) / time.Since(insInfo.PushBeganAt).Seconds())
speed := bytefmt.ByteSize(uint64(speedByte))
insInfo.Description = fmt.Sprintf("%s / %s speed: %s/s", copied, total, speed)
}
}
renderJSON(w, map[string]interface{}{
"success": true,
"data": insInfo,
})
}).Methods("GET")
http.Handle("/devices/", router)
}