1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 22:18:07 +08:00

fix: 解决证书自动续签日志报错的问题 (#6784)

This commit is contained in:
zhengkunwang 2024-10-21 15:10:20 +08:00 committed by ssongliu
parent 3027ae4a50
commit 2076a8a8f3
3 changed files with 24 additions and 13 deletions

View File

@ -41,6 +41,7 @@ type WebsiteSSLApply struct {
ID uint `json:"ID" validate:"required"` ID uint `json:"ID" validate:"required"`
SkipDNSCheck bool `json:"skipDNSCheck"` SkipDNSCheck bool `json:"skipDNSCheck"`
Nameservers []string `json:"nameservers"` Nameservers []string `json:"nameservers"`
DisableLog bool `json:"disableLog"`
} }
type WebsiteAcmeAccountCreate struct { type WebsiteAcmeAccountCreate struct {

View File

@ -181,6 +181,13 @@ func (w WebsiteSSLService) Create(create request.WebsiteSSLCreate) (request.Webs
return create, nil 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 { func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
var ( var (
err error err error
@ -272,11 +279,13 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
defer logFile.Close() defer logFile.Close()
logger := log.New(logFile, "", log.LstdFlags) logger := log.New(logFile, "", log.LstdFlags)
legoLogger.Logger = logger legoLogger.Logger = logger
startMsg := i18n.GetMsgWithMap("ApplySSLStart", map[string]interface{}{"domain": strings.Join(domains, ","), "type": i18n.GetMsgByKey(websiteSSL.Provider)}) if !apply.DisableLog {
if websiteSSL.Provider == constant.DNSAccount { startMsg := i18n.GetMsgWithMap("ApplySSLStart", map[string]interface{}{"domain": strings.Join(domains, ","), "type": i18n.GetMsgByKey(websiteSSL.Provider)})
startMsg = startMsg + i18n.GetMsgWithMap("DNSAccountName", map[string]interface{}{"name": dnsAccount.Name, "type": dnsAccount.Type}) 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) resource, err := client.ObtainSSL(domains, privateKey)
if err != nil { if err != nil {
handleError(websiteSSL, err) handleError(websiteSSL, err)
@ -296,7 +305,7 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
websiteSSL.Type = cert.Issuer.CommonName websiteSSL.Type = cert.Issuer.CommonName
websiteSSL.Organization = cert.Issuer.Organization[0] websiteSSL.Organization = cert.Issuer.Organization[0]
websiteSSL.Status = constant.SSLReady 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) saveCertificateFile(websiteSSL, logger)
if websiteSSL.ExecShell { if websiteSSL.ExecShell {
@ -304,11 +313,11 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
if websiteSSL.PushDir { if websiteSSL.PushDir {
workDir = websiteSSL.Dir 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 { 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 { } 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)) websites, _ := websiteRepo.GetBy(websiteRepo.WithWebsiteSSLID(websiteSSL.ID))
if len(websites) > 0 { if len(websites) > 0 {
for _, website := range websites { 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 { 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) nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
@ -330,10 +339,10 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
return return
} }
if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil { if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil {
legoLogger.Logger.Println(i18n.GetMsgByKey(constant.ErrSSLApply)) printSSLLog(logger, constant.ErrSSLApply, nil, apply.DisableLog)
return return
} }
legoLogger.Logger.Println(i18n.GetMsgByKey("ApplyWebSiteSSLSuccess")) printSSLLog(logger, "ApplyWebSiteSSLSuccess", nil, apply.DisableLog)
} }
}() }()

View File

@ -51,7 +51,8 @@ func (ssl *ssl) Run() {
} }
} else { } else {
if err := sslService.ObtainSSL(request.WebsiteSSLApply{ if err := sslService.ObtainSSL(request.WebsiteSSLApply{
ID: s.ID, ID: s.ID,
DisableLog: true,
}); err != nil { }); err != nil {
global.LOG.Errorf("Failed to update the SSL certificate for the [%s] domain , err:%s", s.PrimaryDomain, err.Error()) global.LOG.Errorf("Failed to update the SSL certificate for the [%s] domain , err:%s", s.PrimaryDomain, err.Error())
continue continue