dnf 更新通知
ddatsh
先装好
dnf install dnf-automatic
搞个定时任务,执行后调接口检查,有更新发通知
/usr/bin/dnf-automatic > /tmp/dnf.log
curl -s http://127.0.0.1:8080/check/dnf
[root@dd ~]# /usr/bin/dnf-automatic
Last metadata expiration check: 1:38:42 ago on Fri 20 Sep 2024 07:44:36 PM CST.
The following updates were downloaded on 'dd':
===============================================================
Package Arch Version Repository Size
===============================================================
Upgrading:
mypackage x86_64 1.1-1.el9 root_rpmbuild_RPMS_x86_64 9.5 k
Transaction Summary
===============================================================
Upgrade 1 Package
跳过第一行 Last metadata expiration check ,后面有内容即有更新,发通知
func checkDnf(c *gin.Context) {
file, err := os.Open("/tmp/dnf.log")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
// 跳过第一行
scanner.Scan()
// 读取剩余的行
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
result := strings.Join(lines, "\n")
if result != "" {
_, err = db.Redis.Get(c.Request.Context(), "dnf").Result()
if errors.Is(err, redis.Nil) {
slog.InfoContext(c.Request.Context(), "send dnf update message")
message.SendWechatMessage(c, result)
db.Redis.Set(c.Request.Context(), "dnf", "true", 0)
} else if err != nil {
_ = c.Error(err)
return
} else {
slog.InfoContext(c.Request.Context(), "ignore dnf update message")
}
} else {
slog.InfoContext(c.Request.Context(), "no dnf update")
}
}
避免重复更新通知,搜到个 post-transaction-actions Plugin
之前发过通知,redis里置个标识,通过这个执行后状态通知再删除redis标识重置通知状态
cat /etc/dnf/plugins/post-transaction-actions.d/update.action
*:any:if [ "$state" = "upgraded" ]; then /data/bin/redis-cli del dnf; fi