-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstallserver.go
More file actions
160 lines (141 loc) · 3.55 KB
/
Copy pathinstallserver.go
File metadata and controls
160 lines (141 loc) · 3.55 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
goadb "github.com/yosemite-open/go-adb"
)
// The json tag is to sync with REST API https://github.com/openatx/atx-agent
type SyncState struct {
ID string `json:"id"`
Copied int `json:"copiedSize"`
Total int `json:"totalSize"`
State string `json:"message"`
asnycCopier *goadb.AsyncWriter
}
func (s *SyncState) Update() error {
if s.asnycCopier == nil {
return errors.New("asnycCopier is nil")
}
if s.Total == 0 {
s.Total = int(s.asnycCopier.TotalSize)
s.Copied = int(s.asnycCopier.BytesCompleted())
} else if s.Copied != s.Total {
s.Copied = int(s.asnycCopier.BytesCompleted())
}
return nil
}
type Dashboard struct {
m sync.Mutex
states map[string]*SyncState
}
func NewDashboard() *Dashboard {
return &Dashboard{
states: make(map[string]*SyncState),
}
}
func (d *Dashboard) AddSyncState() (id string, state *SyncState) {
d.m.Lock()
defer d.m.Unlock()
id = UniqID()
state = &SyncState{ID: id}
d.states[id] = state
return
}
// If not found, return nil
func (d *Dashboard) Get(id string) *SyncState {
d.m.Lock()
defer d.m.Unlock()
return d.states[id]
}
func (d *Dashboard) DeleteAfter(id string, duration time.Duration) {
go func() {
time.Sleep(duration)
d.m.Lock()
defer d.m.Unlock()
delete(d.states, id)
}()
}
func registerHTTPHandler() {
m := mux.NewRouter()
dashboard := NewDashboard()
adb, err := goadb.New()
if err != nil {
panic(err)
}
m.HandleFunc("/install/{serial}", func(w http.ResponseWriter, r *http.Request) {
serial := mux.Vars(r)["serial"]
device := adb.Device(goadb.DeviceWithSerial(serial))
url := r.FormValue("url")
if url == "" {
http.Error(w, "form value \"url\" is required", http.StatusBadRequest)
return
}
id, state := dashboard.AddSyncState()
tmpPath := fmt.Sprintf("/sdcard/tmp-%s.apk", id)
aw, err := device.DoSyncHTTPFile(tmpPath, url, 0644)
if err != nil {
http.Error(w, err.Error(), 500)
dashboard.DeleteAfter(id, 1*time.Minute)
return
}
state.State = "pushing"
state.asnycCopier = aw
io.WriteString(w, id)
go func() {
defer device.RunCommand("rm", tmpPath)
defer dashboard.DeleteAfter(id, 5*time.Minute)
<-aw.Done
err := aw.Err()
if err != nil {
state.State = "err: " + err.Error()
return
}
state.State = "installing"
// do install
output, err := device.RunCommand("pm", "install", "-r", "-t", tmpPath)
if err != nil {
state.State = "err: " + err.Error() + ":" + output
return
}
if strings.Contains(output, "Success") {
state.State = "finished"
} else {
state.State = "err: " + strings.TrimSpace(output)
}
}()
}).Methods("POST")
m.HandleFunc("/install/{serial}/{id}", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/install/"+mux.Vars(r)["id"], 302)
})
m.HandleFunc("/install/{id}", func(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
state := dashboard.Get(id)
if state == nil {
state = &SyncState{
State: "finished",
}
}
state.Update()
data, _ := json.Marshal(state)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}).Methods("GET")
m.HandleFunc("/install/{id}", func(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
state := dashboard.Get(id)
if state == nil {
io.WriteString(w, "already deleted")
return
}
state.asnycCopier.Cancel()
io.WriteString(w, "canceled")
}).Methods("DELETE")
http.Handle("/install", m)
}