/
soft_package_service_impl.go
377 lines (308 loc) · 8.87 KB
/
soft_package_service_impl.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package plugins
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/bitwormhole/starter/markup"
"github.com/bitwormhole/starter/util"
"github.com/bitwormhole/starter/vlog"
"github.com/bitwormhole/wpm/server/components/packs"
"github.com/bitwormhole/wpm/server/data/dao"
"github.com/bitwormhole/wpm/server/data/dbagent"
"github.com/bitwormhole/wpm/server/data/dxo"
"github.com/bitwormhole/wpm/server/data/entity"
"github.com/bitwormhole/wpm/server/service"
"github.com/bitwormhole/wpm/server/utils"
"github.com/bitwormhole/wpm/server/web/dto"
)
// PluginServiceImpl ...
type PluginServiceImpl struct {
markup.Component `id:"SoftwarePackageService"`
SoftwarePackageDAO dao.SoftwarePackageDAO `inject:"#SoftwarePackageDAO"`
GormDBAgent dbagent.GormDBAgent `inject:"#GormDBAgent"`
NamespaceService service.NamespaceService `inject:"#NamespaceService"`
HTTPClient service.HTTPClientService `inject:"#HTTPClientService"`
HTTPClientEx service.HTTPClientExService `inject:"#HTTPClientExService"`
TrashService service.TrashService `inject:"#TrashService"`
IntentTemplateSer service.IntentTemplateService `inject:"#IntentTemplateService"`
ExecutableSer service.ExecutableService `inject:"#ExecutableService"`
ContentTypeSer service.ContentTypeService `inject:"#ContentTypeService"`
MediaSer service.MediaService `inject:"#MediaService"`
AppDataService service.AppDataService `inject:"#AppDataService"`
FileSystemService service.FileSystemService `inject:"#FileSystemService"`
InstallerRegistryList []packs.InstallerRegistry `inject:".packs.InstallerRegistry"`
lockerForGenInstaID sync.Mutex
lockerForInstall sync.Mutex
}
func (inst *PluginServiceImpl) _Impl() service.SoftwarePackageService {
return inst
}
// GetPacksManger ...
func (inst *PluginServiceImpl) GetPacksManger() packs.Manager {
return inst
}
func (inst *PluginServiceImpl) dto2entity(o1 *dto.SoftwarePackage) (*entity.SoftwarePackage, error) {
o2 := &entity.SoftwarePackage{}
o2.ID = o1.ID
o2.URN = o1.URN
o2.UUID = o1.UUID
o2.Referer = o1.Referer
o2.Installation = o1.Installation
o2.Name = o1.Name
o2.ModuleName = o1.ModuleName
o2.FileName = o1.FileName
o2.Namespace = o1.Namespace
o2.Icon = o1.Icon
o2.Title = o1.Title
o2.Description = o1.Description
o2.OS = o1.OS
o2.Arch = o1.Arch
o2.Version = o1.Version
o2.Revision = o1.Revision
o2.ReleaseAt = o1.ReleaseAt.GetTime()
o2.ContentType = o1.ContentType
o2.SHA256SUM = o1.SHA256SUM
o2.Size = o1.Size
o2.WebPageURL = o1.WebPageURL
o2.DownloadURL = o1.DownloadURL
// o2.ResourceURL = o1.ResourceURL
return o2, nil
}
func (inst *PluginServiceImpl) entity2dto(o1 *entity.SoftwarePackage) (*dto.SoftwarePackage, error) {
o2 := &dto.SoftwarePackage{}
o2.ID = o1.ID
o2.URN = o1.URN
o2.UUID = o1.UUID
o2.Referer = o1.Referer
o2.Installation = o1.Installation
o2.Name = o1.Name
o2.ModuleName = o1.ModuleName
o2.FileName = o1.FileName
o2.Namespace = o1.Namespace
o2.Icon = o1.Icon
o2.Title = o1.Title
o2.Description = o1.Description
o2.OS = o1.OS
o2.Arch = o1.Arch
o2.Version = o1.Version
o2.Revision = o1.Revision
o2.ReleaseAt = util.NewTime(o1.ReleaseAt)
o2.ContentType = o1.ContentType
o2.SHA256SUM = o1.SHA256SUM
o2.Size = o1.Size
o2.WebPageURL = o1.WebPageURL
o2.DownloadURL = o1.DownloadURL
// o2.ResourceURL = o1.ResourceURL
o2.Installed = o1.Installation > 0
return o2, nil
}
func (inst *PluginServiceImpl) entity2dtoList(src []*entity.SoftwarePackage) ([]*dto.SoftwarePackage, error) {
dst := make([]*dto.SoftwarePackage, 0)
for _, item := range src {
item2, err := inst.entity2dto(item)
if err != nil {
return nil, err
}
dst = append(dst, item2)
}
return dst, nil
}
func (inst *PluginServiceImpl) prepareSave(o *entity.SoftwarePackage) error {
ss := &utils.Strings{}
urnb := strings.Builder{}
name := ss.TrimToLower(o.Name)
ns := ss.TrimToLower(o.Namespace)
ver := ss.TrimToLower(o.Version)
rev := "r" + strconv.Itoa(o.Revision)
os := ss.TrimToLower(o.OS)
arch := ss.TrimToLower(o.Arch)
urnb.WriteString(ns)
urnb.WriteString("#")
urnb.WriteString(name)
urnb.WriteString("-")
urnb.WriteString(ver)
urnb.WriteString("-")
urnb.WriteString(os)
urnb.WriteString("-")
urnb.WriteString(arch)
urnb.WriteString("-")
urnb.WriteString(rev)
o.ModuleName = ns + "#" + name
o.URN = dxo.NewSoftwarePackageURN(urnb.String())
return nil
}
// Find ...
func (inst *PluginServiceImpl) Find(ctx context.Context, id dxo.SoftwarePackageID) (*dto.SoftwarePackage, error) {
dao := inst.SoftwarePackageDAO
o1, err := dao.Find(id)
if err != nil {
return nil, err
}
return inst.entity2dto(o1)
}
// ListAll ...
func (inst *PluginServiceImpl) ListAll(ctx context.Context) ([]*dto.SoftwarePackage, error) {
dao := inst.SoftwarePackageDAO
list1, err := dao.ListAll()
if err != nil {
return nil, err
}
return inst.entity2dtoList(list1)
}
// ListByModuleName ...
func (inst *PluginServiceImpl) ListByModuleName(ctx context.Context, moduleName string) ([]*dto.SoftwarePackage, error) {
dao := inst.SoftwarePackageDAO
srclist, err := dao.ListByModuleName(moduleName)
if err != nil {
return nil, err
}
return inst.entity2dtoList(srclist)
}
// Insert ...
func (inst *PluginServiceImpl) Insert(ctx context.Context, o1 *dto.SoftwarePackage) (*dto.SoftwarePackage, error) {
o2, err := inst.dto2entity(o1)
if err != nil {
return nil, err
}
inst.prepareSave(o2)
dao := inst.SoftwarePackageDAO
o3, err := dao.Insert(o2)
if err != nil {
return nil, err
}
return inst.entity2dto(o3)
}
// UpdateItem ...
func (inst *PluginServiceImpl) UpdateItem(ctx context.Context, id dxo.SoftwarePackageID, o1 *dto.SoftwarePackage) (*dto.SoftwarePackage, error) {
o2, err := inst.dto2entity(o1)
if err != nil {
return nil, err
}
inst.prepareSave(o2)
dao := inst.SoftwarePackageDAO
o3, err := dao.Update(id, o2)
if err != nil {
return nil, err
}
return inst.entity2dto(o3)
}
// UpdateList ...
func (inst *PluginServiceImpl) UpdateList(ctx context.Context) error {
// 下载更新的包信息
up := &myPakcageListUpdater{
packageSer: inst,
namespaceSer: inst.NamespaceService,
httpclientSer: inst.HTTPClientEx,
}
err := up.Fetch(ctx)
if err != nil {
return err
}
// 清除过时的包信息
cl := &myPakcageListCleaner{
packageSer: inst,
namespaceSer: inst.NamespaceService,
httpclientSer: inst.HTTPClientEx,
}
err = cl.Clean(ctx)
if err != nil {
return err
}
// 清除数据库中已经软删除的对象
err = inst.TrashService.Clean()
if err != nil {
return err
}
return up.Save(ctx)
}
// Remove ...
func (inst *PluginServiceImpl) Remove(ctx context.Context, id dxo.SoftwarePackageID) error {
dao := inst.SoftwarePackageDAO
return dao.Remove(id)
}
func (inst *PluginServiceImpl) generateInstallationID() dxo.InstallationID {
inst.lockerForGenInstaID.Lock()
defer func() {
inst.lockerForGenInstaID.Unlock()
}()
time.Sleep(time.Second)
n := util.Now().Int64()
return dxo.InstallationID(n)
}
// Install ...
func (inst *PluginServiceImpl) Install(ctx context.Context, id dxo.SoftwarePackageID) error {
// dao := inst.SoftwarePackageDAO
// fetch
p, err := inst.Find(ctx, id)
if err != nil {
return err
}
vlog.Warn("install package:", p.ID, " ", p.URN)
installer := myPakcageInstaller{
context: ctx,
SoftwarePackageSer: inst,
HTTPClient: inst.HTTPClient,
HTTPClientEx: inst.HTTPClientEx,
IntentTemplateSer: inst.IntentTemplateSer,
ExecutableSer: inst.ExecutableSer,
ContentTypeSer: inst.ContentTypeSer,
MediaSer: inst.MediaSer,
AppDataSer: inst.AppDataService,
}
installer.installation = inst.generateInstallationID()
p, err = installer.Install(p)
if err != nil {
return err
}
// update
p.Installed = true
p.Installation = installer.installation
// save
_, err = inst.UpdateItem(ctx, id, p)
if err != nil {
return err
}
return nil
}
// Uninstall ...
func (inst *PluginServiceImpl) Uninstall(ctx context.Context, id dxo.SoftwarePackageID) error {
p, err := inst.Find(ctx, id)
if err != nil {
return err
}
vlog.Warn("uninstall package:", p.ID, " ", p.URN)
uni := myPakcageUninstaller{
context: ctx,
agent: inst.GormDBAgent,
installation: p.Installation,
FileSystemSer: inst.FileSystemService,
}
err = uni.Uninstall()
if err != nil {
return err
}
p.Installed = false
p.Installation = 0
_, err = inst.UpdateItem(ctx, id, p)
return err
}
// GetInstaller ...
func (inst *PluginServiceImpl) GetInstaller(ic *packs.InstallingContext) (packs.Installer, error) {
if ic == nil {
return nil, fmt.Errorf("param:ic(InstallingContext) is nil")
}
src := inst.InstallerRegistryList
for _, r1 := range src {
r2 := r1.GetInstallerRegistration()
installer := r2.Installer
if installer != nil {
if installer.Accept(ic) {
return installer, nil
}
}
}
return nil, fmt.Errorf("no installer for " + ic.String())
}