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