-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastore.go
262 lines (221 loc) · 5.41 KB
/
lastore.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
/*
* Copyright (C) 2015 ~ 2017 Deepin Technology Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"internal/system"
"net/http"
"os"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/linuxdeepin/go-dbus-factory/com.deepin.lastore"
"pkg.deepin.io/lib/dbus1"
)
var CMDTester = cli.Command{
Name: "test",
Usage: `Use lastore-daemon to run jobs
search will search apps from dstore. It will list all apps
if there hasn't any input.
install/remove will execute the command with the input
package name.
upgrade will first update source and then upgrade packages
if there has any one.
`,
Action: MainTester,
Flags: []cli.Flag{
cli.StringFlag{
Name: "job,j",
Value: "",
Usage: "install|remove|upgrade|prepare_upgrade|search|update",
},
},
}
func MainTester(c *cli.Context) {
var err error
switch c.String("job") {
case "install":
err = LastoreInstall(c.Args().First())
case "remove":
err = LastoreRemove(c.Args().First())
case "upgrade":
err = LastoreUpgrade()
case "update":
err = LastoreUpdate()
case "search":
err = LastoreSearch(c.GlobalString("dstoreapi"), c.Args().First(), c.GlobalBool("debug"))
case "prepare_upgrade":
err = LastorePrepareUpgrade()
default:
cli.ShowCommandHelp(c, "test")
}
if err != nil {
fmt.Println("E:", err)
os.Exit(-1)
}
}
func LastoreUpdate() error {
m := getLastore()
fmt.Println("Connected lastore-daemon..")
fmt.Println("Try updating source")
j, err := m.UpdateSource(0)
if err != nil {
return err
}
fmt.Println()
fmt.Printf("Created Job: %q successful\n", j)
return waitJob(j)
}
func LastoreRemove(p string) error {
m := getLastore()
fmt.Println("Connected lastore-daemon..")
fmt.Println("Try removing", p)
j, err := m.RemovePackage(0, "RemoveForTesing "+p, p)
if err != nil {
return err
}
fmt.Println()
fmt.Printf("Created Job: %q successful\n", j)
return waitJob(j)
}
func LastoreInstall(p string) error {
m := getLastore()
fmt.Println("Connected lastore-daemon..")
fmt.Println("Try installing", p)
j, err := m.InstallPackage(0, "InstallForTesing "+p, p)
if err != nil {
return err
}
fmt.Println()
fmt.Printf("Created Job: %q successful\n", j)
return waitJob(j)
}
func LastorePrepareUpgrade() error {
m := getLastore()
fmt.Println("Connected lastore-daemon..")
j, err := m.PrepareDistUpgrade(0)
if err != nil {
return err
}
fmt.Println()
fmt.Printf("Created Job: %q successful\n", j)
return waitJob(j)
}
func LastoreSearch(server string, p string, debug bool) error {
resp, err := http.Get(server + "/info/all")
if err != nil {
return err
}
defer resp.Body.Close()
apps := make(map[string]interface{})
d := json.NewDecoder(resp.Body)
err = d.Decode(&apps)
if err != nil {
return err
}
for app, info := range apps {
if p == "" || strings.Contains(app, p) {
if debug {
fmt.Printf("-%s %v\n", app, info)
} else {
fmt.Println(app)
}
}
}
return nil
}
func LastoreUpgrade() error {
m := getLastore()
fmt.Println("Connected lastore-daemon..")
fmt.Println("Try updating /var/lib/apt/lists .....")
j, err := m.UpdateSource(0)
if err != nil {
fmt.Printf("Created Job: %v failed\n", err)
return err
}
if err = waitJob(j); err != nil {
return err
}
fmt.Printf("Created Job: %q successful\n", j)
fmt.Println()
list, err := m.UpgradableApps().Get(0)
if err != nil {
return err
}
if len(list) == 0 {
fmt.Println("There hasn't any packages need be upgrade.")
return nil
}
fmt.Printf("Try upgrading %v\n", list)
j, err = m.DistUpgrade(0)
if err != nil {
return err
}
fmt.Printf("Created Job: %q successful\n", j)
return waitJob(j)
}
func getLastore() *lastore.Lastore {
sysBus, err := dbus.SystemBus()
if err != nil {
panic(err)
}
return lastore.NewLastore(sysBus)
}
func showLine(j *lastore.Job) string {
id, _ := j.Id().Get(0)
type0, _ := j.Type().Get(0)
status, _ := j.Status().Get(0)
progress, _ := j.Progress().Get(0)
description, _ := j.Description().Get(0)
return fmt.Sprintf("id:%v(%v)\tProgress:%v:%v%%\tDesc:%q",
id, type0, status, progress*100, description)
}
func waitJob(p dbus.ObjectPath) error {
sysBus, err := dbus.SystemBus()
if err != nil {
return err
}
j, err := lastore.NewJob(sysBus, p)
if err != nil {
return err
}
status, _ := j.Status().Get(0)
if status != "" {
fmt.Println(showLine(j))
}
for l := showLine(j); status != ""; {
t := showLine(j)
if t != l {
l = t
fmt.Println(t)
}
switch system.Status(status) {
case system.ReadyStatus, system.RunningStatus:
case system.PausedStatus:
return fmt.Errorf("job be paused")
case system.SucceedStatus:
fmt.Println("succeeful finished")
return nil
case system.FailedStatus:
return fmt.Errorf("job %v failed %v", p, j)
}
time.Sleep(time.Millisecond * 50)
status, _ = j.Status().Get(0)
}
return err
}