Skip to content

Commit f54a7e4

Browse files
authored
feat: Modify the format of the data sent to the update platform (#221)
send json string to update platform Task: https://pms.uniontech.com/task-view-380017.html
1 parent 340345c commit f54a7e4

6 files changed

Lines changed: 102 additions & 17 deletions

File tree

src/internal/updateplatform/message_report.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ type ShellCheck struct {
4545
Shell string `json:"shell"` //检查脚本的内容
4646
}
4747

48+
// 发送给更新平台的状态信息
49+
type StatusMessage struct {
50+
Type string `json:"type"` //消息类型,info,warning,error
51+
UpdateType string `json:"updateType"` //system.UpdateType类型
52+
JobDescription string `json:"jobDescription"` //job.Description
53+
Detail string `json:"detail"` //消息详情
54+
}
55+
4856
type UpdatePlatformManager struct {
4957
config *Config
5058
allowPostSystemUpgradeMessageType system.UpdateType
@@ -1316,12 +1324,19 @@ func (m *UpdatePlatformManager) UpdateAllPlatformDataSync() error {
13161324
}
13171325

13181326
// PostStatusMessage 将检查\下载\安装过程中所有异常状态和每个阶段成功的正常状态上报
1319-
func (m *UpdatePlatformManager) PostStatusMessage(body string) {
1320-
logger.Debug("post status msg:", body)
1327+
func (m *UpdatePlatformManager) PostStatusMessage(message StatusMessage) {
1328+
logger.Debugf("post status msg, type:%v, detail:%v", message.Type, message.Detail)
13211329
if (m.config.PlatformDisabled & DisabledProcess) != 0 {
13221330
return
13231331
}
1324-
buf := bytes.NewBufferString(body)
1332+
1333+
msg, err := json.Marshal(message)
1334+
if err != nil {
1335+
logger.Warningf("marshal status message failed:%v", err)
1336+
return
1337+
}
1338+
1339+
buf := bytes.NewBufferString(string(msg))
13251340
filePath := fmt.Sprintf("/tmp/%s_%s.xz", "update", time.Now().Format("20231019102233444"))
13261341
response, err := m.genPostProcessResponse(buf, filePath)
13271342
if err != nil {

src/lastore-daemon/manager.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ func NewManager(service *dbusutil.Service, updateApi system.System, c *config.Co
162162
if c.UpgradeStatus.Status == system.UpgradeRunning {
163163
m.rebootTimeoutTimer = time.AfterFunc(600*time.Second, func() {
164164
// 启动后600s如果没有触发检查,那么上报更新失败
165-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("the check has not been triggered after reboot for 600 seconds"))
165+
166+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
167+
Type: "error",
168+
Detail: "the check has not been triggered after reboot for 600 seconds",
169+
})
170+
166171
err = m.delRebootCheckOption(all)
167172
if err != nil {
168173
logger.Warning(err)
@@ -895,6 +900,7 @@ func (m *Manager) sendNotify(appName string, replacesId uint32, appIcon string,
895900
cmdArgs := []string{
896901
"-u", username, "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/" + strconv.Itoa(int(uid)) + "/bus",
897902
}
903+
// 安全地添加参数,避免命令注入
898904
cmdArgs = append(cmdArgs, args...)
899905
cmd := exec.Command("sudo", cmdArgs...)
900906
logger.Info(cmd.String())
@@ -909,7 +915,9 @@ func (m *Manager) sendNotify(appName string, replacesId uint32, appIcon string,
909915
} else {
910916
str := outBuffer.String()
911917
if len(str) >= 12 {
912-
num, err := strconv.ParseUint(str[8:len(str)-3], 10, 0)
918+
// 确保解析的字符串是有效的数字
919+
numStr := str[8 : len(str)-3]
920+
num, err := strconv.ParseUint(numStr, 10, 32)
913921
if err != nil {
914922
logger.Warning(err)
915923
} else {
@@ -949,6 +957,7 @@ func (m *Manager) closeNotify(id uint32) error {
949957
cmdArgs := []string{
950958
"-u", username, "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/" + strconv.Itoa(int(uid)) + "/bus",
951959
}
960+
// 安全地添加参数,避免命令注入
952961
cmdArgs = append(cmdArgs, args...)
953962
cmd := exec.Command("sudo", cmdArgs...)
954963
logger.Info(cmd.String())

src/lastore-daemon/manager_check.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ func (m *Manager) checkUpgrade(sender dbus.Sender, checkMode system.UpdateType,
114114
go func() {
115115
m.inhibitAutoQuitCountAdd()
116116
defer m.inhibitAutoQuitCountSub()
117-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v postcheck error: %v", checkOrder.JobType(), job.Description))
117+
118+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
119+
Type: "error",
120+
UpdateType: checkOrder.JobType(),
121+
JobDescription: job.Description,
122+
Detail: fmt.Sprintf("%v postcheck error: %v", checkOrder.JobType(), job.Description),
123+
})
124+
118125
m.reportLog(upgradeStatusReport, false, job.Description)
119126
}()
120127
inhibit(false)

src/lastore-daemon/manager_download.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ func (m *Manager) prepareDistUpgrade(sender dbus.Sender, origin system.UpdateTyp
187187
m.inhibitAutoQuitCountAdd()
188188
defer m.inhibitAutoQuitCountSub()
189189
m.reportLog(downloadStatusReport, false, j.Description)
190-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("download %v package failed, detail is %v", mode.JobType(), job.Description)) // 上报下载失败状态
190+
// 上报下载失败状态
191+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
192+
Type: "error",
193+
UpdateType: mode.JobType(),
194+
JobDescription: job.Description,
195+
Detail: fmt.Sprintf("download %v package failed, detail is %v", mode.JobType(), job.Description),
196+
})
191197
}()
192198
return nil
193199
},
@@ -220,7 +226,14 @@ func (m *Manager) prepareDistUpgrade(sender dbus.Sender, origin system.UpdateTyp
220226
m.inhibitAutoQuitCountSub()
221227
}
222228
}
223-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("download %v package success", j.updateTyp.JobType())) // 上报下载成功状态
229+
230+
// 上报下载成功状态
231+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
232+
Type: "info",
233+
UpdateType: j.updateTyp.JobType(),
234+
JobDescription: j.Description,
235+
Detail: fmt.Sprintf("download %v package success", j.updateTyp.JobType()),
236+
})
224237
return nil
225238
},
226239
string(system.EndStatus): func() error {

src/lastore-daemon/manager_update.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ func (m *Manager) updateSource(sender dbus.Sender) (*Job, error) {
150150
go func() {
151151
m.inhibitAutoQuitCountAdd()
152152
defer m.inhibitAutoQuitCountSub()
153-
m.updatePlatform.PostStatusMessage("update source success")
153+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
154+
Type: "info",
155+
Detail: "update source success",
156+
})
154157
}()
155158
m.updatePlatform.SaveCache(m.config)
156159
job.setPropProgress(1.0)
@@ -177,7 +180,12 @@ func (m *Manager) updateSource(sender dbus.Sender) (*Job, error) {
177180
m.inhibitAutoQuitCountAdd()
178181
defer m.inhibitAutoQuitCountSub()
179182
m.reportLog(updateStatusReport, false, job.Description)
180-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("apt-get update failed, detail is %v , option is %+v", job.Description, job.option))
183+
184+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
185+
Type: "error",
186+
JobDescription: job.Description,
187+
Detail: fmt.Sprintf("apt-get update failed, detail is %v , option is %+v", job.Description, job.option),
188+
})
181189
}()
182190
return nil
183191
},
@@ -472,7 +480,10 @@ func (m *Manager) refreshUpdateInfos(sync bool) {
472480
go func() {
473481
m.inhibitAutoQuitCountAdd()
474482
defer m.inhibitAutoQuitCountSub()
475-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("generate package list error, detail is %v:", e))
483+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
484+
Type: "error",
485+
Detail: fmt.Sprintf("generate package list error, detail is %v:", e),
486+
})
476487
}()
477488
logger.Warning(e)
478489
}
@@ -488,7 +499,12 @@ func (m *Manager) refreshUpdateInfos(sync bool) {
488499
go func() {
489500
m.inhibitAutoQuitCountAdd()
490501
defer m.inhibitAutoQuitCountSub()
491-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("generate package list error, detail is %v:", e))
502+
503+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
504+
Type: "error",
505+
Detail: fmt.Sprintf("generate package list error, detail is %v:", e),
506+
})
507+
492508
}()
493509
logger.Warning(e)
494510
}

src/lastore-daemon/manager_upgrade.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ func (m *Manager) distUpgrade(sender dbus.Sender, mode system.UpdateType, needAd
360360
uuid, err = m.prepareAptCheck(mode)
361361
if err != nil {
362362
logger.Warning(err)
363-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v gen dut meta failed, detail is: %v", mode.JobType(), err.Error()))
363+
364+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
365+
Type: "error",
366+
UpdateType: mode.JobType(),
367+
Detail: fmt.Sprintf("%v gen dut meta failed, detail is: %v", mode.JobType(), err.Error()),
368+
})
369+
364370
if unref != nil {
365371
unref()
366372
}
@@ -371,7 +377,12 @@ func (m *Manager) distUpgrade(sender dbus.Sender, mode system.UpdateType, needAd
371377
systemErr := dut.CheckSystem(dut.PreCheck, nil) // 只是为了执行precheck的hook脚本
372378
if systemErr != nil {
373379
logger.Info(systemErr)
374-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v CheckSystem failed, detail is: %v", mode.JobType(), systemErr.Error()))
380+
381+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
382+
Type: "error",
383+
UpdateType: mode.JobType(),
384+
Detail: fmt.Sprintf("%v CheckSystem failed, detail is: %v", mode.JobType(), systemErr.Error()),
385+
})
375386
return systemErr
376387
}
377388
if !system.CheckInstallAddSize(mode) {
@@ -395,7 +406,12 @@ func (m *Manager) distUpgrade(sender dbus.Sender, mode system.UpdateType, needAd
395406
systemErr := dut.CheckSystem(dut.MidCheck, nil)
396407
if systemErr != nil {
397408
logger.Info(systemErr)
398-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v CheckSystem failed, detail is: %v", mode.JobType(), systemErr.Error()))
409+
410+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
411+
Type: "error",
412+
UpdateType: mode.JobType(),
413+
Detail: fmt.Sprintf("%v CheckSystem failed, detail is: %v", mode.JobType(), systemErr.Error()),
414+
})
399415
return systemErr
400416
}
401417
if m.statusManager.abStatus == system.HasBackedUp {
@@ -592,7 +608,12 @@ func (m *Manager) preFailedHook(job *Job, mode system.UpdateType, uuid string) e
592608
allErrMsg = append(allErrMsg, string(msg))
593609
}
594610
}
595-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v upgrade failed, detail is: %v; all error message is %v", mode.JobType(), job.Description, strings.Join(allErrMsg, "\n")))
611+
612+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
613+
Type: "error",
614+
UpdateType: mode.JobType(),
615+
Detail: fmt.Sprintf("%v upgrade failed, detail is: %v; all error message is %v", mode.JobType(), job.Description, strings.Join(allErrMsg, "\n")),
616+
})
596617
}()
597618
m.statusManager.SetUpdateStatus(mode, system.UpgradeErr)
598619
// 如果安装失败,那么需要将version文件一直缓存,防止下次检查更新时version版本变高
@@ -612,7 +633,11 @@ func (m *Manager) preUpgradeCmdSuccessHook(job *Job, needChangeGrub bool, mode s
612633
}
613634
m.statusManager.SetUpdateStatus(mode, system.Upgraded)
614635
job.setPropProgress(1.00)
615-
m.updatePlatform.PostStatusMessage(fmt.Sprintf("%v install package success,need reboot and check", mode))
636+
637+
m.updatePlatform.PostStatusMessage(updateplatform.StatusMessage{
638+
Type: "info",
639+
Detail: fmt.Sprintf("%v install package success, need reboot and check", mode.JobType()),
640+
})
616641
return nil
617642
}
618643

0 commit comments

Comments
 (0)