From 7a6c99d57dc257b8dafa78b665793bcaf43aae06 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:10:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E8=AF=81=E4=B9=A6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=BB=AD=E7=AD=BE=E6=97=A5=E5=BF=97=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20(#6784)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/request/website_ssl.go | 1 + backend/app/service/website_ssl.go | 33 ++++++++++++++++---------- backend/cron/job/ssl.go | 3 ++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/backend/app/dto/request/website_ssl.go b/backend/app/dto/request/website_ssl.go index 509aed434..156d9c00b 100644 --- a/backend/app/dto/request/website_ssl.go +++ b/backend/app/dto/request/website_ssl.go @@ -41,6 +41,7 @@ type WebsiteSSLApply struct { ID uint `json:"ID" validate:"required"` SkipDNSCheck bool `json:"skipDNSCheck"` Nameservers []string `json:"nameservers"` + DisableLog bool `json:"disableLog"` } type WebsiteAcmeAccountCreate struct { diff --git a/backend/app/service/website_ssl.go b/backend/app/service/website_ssl.go index 761903242..0cd752770 100644 --- a/backend/app/service/website_ssl.go +++ b/backend/app/service/website_ssl.go @@ -181,6 +181,13 @@ func (w WebsiteSSLService) Create(create request.WebsiteSSLCreate) (request.Webs return create, nil } +func printSSLLog(logger *log.Logger, msgKey string, params map[string]interface{}, disableLog bool) { + if disableLog { + return + } + logger.Println(i18n.GetMsgWithMap(msgKey, params)) +} + func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { var ( err error @@ -272,11 +279,13 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { defer logFile.Close() logger := log.New(logFile, "", log.LstdFlags) legoLogger.Logger = logger - startMsg := i18n.GetMsgWithMap("ApplySSLStart", map[string]interface{}{"domain": strings.Join(domains, ","), "type": i18n.GetMsgByKey(websiteSSL.Provider)}) - if websiteSSL.Provider == constant.DNSAccount { - startMsg = startMsg + i18n.GetMsgWithMap("DNSAccountName", map[string]interface{}{"name": dnsAccount.Name, "type": dnsAccount.Type}) + if !apply.DisableLog { + startMsg := i18n.GetMsgWithMap("ApplySSLStart", map[string]interface{}{"domain": strings.Join(domains, ","), "type": i18n.GetMsgByKey(websiteSSL.Provider)}) + if websiteSSL.Provider == constant.DNSAccount { + startMsg = startMsg + i18n.GetMsgWithMap("DNSAccountName", map[string]interface{}{"name": dnsAccount.Name, "type": dnsAccount.Type}) + } + legoLogger.Logger.Println(startMsg) } - legoLogger.Logger.Println(startMsg) resource, err := client.ObtainSSL(domains, privateKey) if err != nil { handleError(websiteSSL, err) @@ -296,7 +305,7 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { websiteSSL.Type = cert.Issuer.CommonName websiteSSL.Organization = cert.Issuer.Organization[0] websiteSSL.Status = constant.SSLReady - legoLogger.Logger.Println(i18n.GetMsgWithMap("ApplySSLSuccess", map[string]interface{}{"domain": strings.Join(domains, ",")})) + printSSLLog(logger, "ApplySSLSuccess", map[string]interface{}{"domain": strings.Join(domains, ",")}, apply.DisableLog) saveCertificateFile(websiteSSL, logger) if websiteSSL.ExecShell { @@ -304,11 +313,11 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { if websiteSSL.PushDir { workDir = websiteSSL.Dir } - legoLogger.Logger.Println(i18n.GetMsgByKey("ExecShellStart")) + printSSLLog(logger, "ExecShellStart", nil, apply.DisableLog) if err = cmd.ExecShellWithTimeOut(websiteSSL.Shell, workDir, logger, 30*time.Minute); err != nil { - legoLogger.Logger.Println(i18n.GetMsgWithMap("ErrExecShell", map[string]interface{}{"err": err.Error()})) + printSSLLog(logger, "ErrExecShell", map[string]interface{}{"err": err.Error()}, apply.DisableLog) } else { - legoLogger.Logger.Println(i18n.GetMsgByKey("ExecShellSuccess")) + printSSLLog(logger, "ExecShellSuccess", nil, apply.DisableLog) } } @@ -320,9 +329,9 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { websites, _ := websiteRepo.GetBy(websiteRepo.WithWebsiteSSLID(websiteSSL.ID)) if len(websites) > 0 { for _, website := range websites { - legoLogger.Logger.Println(i18n.GetMsgWithMap("ApplyWebSiteSSLLog", map[string]interface{}{"name": website.PrimaryDomain})) + printSSLLog(logger, "ApplyWebSiteSSLLog", map[string]interface{}{"name": website.PrimaryDomain}, apply.DisableLog) if err := createPemFile(website, *websiteSSL); err != nil { - legoLogger.Logger.Println(i18n.GetMsgWithMap("ErrUpdateWebsiteSSL", map[string]interface{}{"name": website.PrimaryDomain, "err": err.Error()})) + printSSLLog(logger, "ErrUpdateWebsiteSSL", map[string]interface{}{"name": website.PrimaryDomain, "err": err.Error()}, apply.DisableLog) } } nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) @@ -330,10 +339,10 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error { return } if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil { - legoLogger.Logger.Println(i18n.GetMsgByKey(constant.ErrSSLApply)) + printSSLLog(logger, constant.ErrSSLApply, nil, apply.DisableLog) return } - legoLogger.Logger.Println(i18n.GetMsgByKey("ApplyWebSiteSSLSuccess")) + printSSLLog(logger, "ApplyWebSiteSSLSuccess", nil, apply.DisableLog) } }() diff --git a/backend/cron/job/ssl.go b/backend/cron/job/ssl.go index 8a4797ff7..5349deb67 100644 --- a/backend/cron/job/ssl.go +++ b/backend/cron/job/ssl.go @@ -51,7 +51,8 @@ func (ssl *ssl) Run() { } } else { if err := sslService.ObtainSSL(request.WebsiteSSLApply{ - ID: s.ID, + ID: s.ID, + DisableLog: true, }); err != nil { global.LOG.Errorf("Failed to update the SSL certificate for the [%s] domain , err:%s", s.PrimaryDomain, err.Error()) continue