added some optimizations

This commit is contained in:
yoan
2024-08-23 11:33:43 +08:00
parent a5beb26d9d
commit f394e34861
13 changed files with 192 additions and 69 deletions

View File

@@ -31,18 +31,23 @@ type Deployer interface {
Deploy(ctx context.Context) error
}
func Get(record *models.Record) (Deployer, error) {
func Get(record *models.Record, cert *applicant.Certificate) (Deployer, error) {
access := record.ExpandedOne("targetAccess")
option := &DeployerOption{
DomainId: record.Id,
Domain: record.GetString("domain"),
Product: getProduct(record),
Access: access.GetString("config"),
Certificate: applicant.Certificate{
}
if cert != nil {
option.Certificate = *cert
} else {
option.Certificate = applicant.Certificate{
Certificate: record.GetString("certificate"),
PrivateKey: record.GetString("privateKey"),
},
}
}
switch record.GetString("targetType") {
case targetAliyunOss:
return NewAliyun(option)

View File

@@ -26,9 +26,11 @@ func deploy(ctx context.Context, record *models.Record) error {
app.GetApp().Logger().Error("部署失败", "err", r)
}
}()
var certificate *applicant.Certificate
currRecord, err := app.GetApp().Dao().FindRecordById("domains", record.Id)
history := NewHistory(record)
defer history.commit()
// ############1.检查域名配置
history.record(checkPhase, "开始检查", nil)
@@ -73,25 +75,21 @@ func deploy(ctx context.Context, record *models.Record) error {
app.GetApp().Logger().Error("获取applicant失败", "err", err)
return err
}
certificate, err := applicant.Apply()
certificate, err = applicant.Apply()
if err != nil {
history.record(applyPhase, "申请证书失败", err)
app.GetApp().Logger().Error("申请证书失败", "err", err)
return err
}
history.record(applyPhase, "申请证书成功", nil)
if err = saveCert(ctx, record, certificate); err != nil {
history.record(applyPhase, "保存证书失败", err)
app.GetApp().Logger().Error("保存证书失败", "err", err)
return err
}
history.setCert(certificate)
}
history.record(applyPhase, "保存证书成功", nil, true)
// ############3.部署证书
history.record(deployPhase, "开始部署", nil, false)
deployer, err := deployer.Get(currRecord)
deployer, err := deployer.Get(currRecord, certificate)
if err != nil {
history.record(deployPhase, "获取deployer失败", err)
app.GetApp().Logger().Error("获取deployer失败", "err", err)
@@ -99,38 +97,14 @@ func deploy(ctx context.Context, record *models.Record) error {
}
if err = deployer.Deploy(ctx); err != nil {
setDeployed(ctx, record, false)
app.GetApp().Logger().Error("部署失败", "err", err)
history.record(deployPhase, "部署失败", err)
return err
}
setDeployed(ctx, record, true)
app.GetApp().Logger().Info("部署成功")
history.record(deployPhase, "部署成功", nil, true)
return nil
}
func setDeployed(ctx context.Context, record *models.Record, deployed bool) error {
record.Set("deployed", deployed)
if err := app.GetApp().Dao().SaveRecord(record); err != nil {
return err
}
return nil
}
func saveCert(ctx context.Context, record *models.Record, cert *applicant.Certificate) error {
record.Set("certUrl", cert.CertUrl)
record.Set("certStableUrl", cert.CertStableUrl)
record.Set("privateKey", cert.PrivateKey)
record.Set("certificate", cert.Certificate)
record.Set("issuerCertificate", cert.IssuerCertificate)
record.Set("csr", cert.Csr)
record.Set("expiredAt", time.Now().Add(time.Hour*24*90))
if err := app.GetApp().Dao().SaveRecord(record); err != nil {
return err
}
return nil
}

View File

@@ -14,10 +14,12 @@ func create(ctx context.Context, record *models.Record) error {
}
if record.GetBool("rightnow") {
go func() {
if err := deploy(ctx, record); err != nil {
app.GetApp().Logger().Error("deploy failed", "err", err)
}
}()
if err := deploy(ctx, record); err != nil {
return err
}
}
scheduler := app.GetScheduler()
@@ -45,9 +47,11 @@ func update(ctx context.Context, record *models.Record) error {
if record.GetBool("rightnow") {
if err := deploy(ctx, record); err != nil {
return err
}
go func() {
if err := deploy(ctx, record); err != nil {
app.GetApp().Logger().Error("deploy failed", "err", err)
}
}()
}
err := scheduler.Add(record.Id, record.GetString("crontab"), func() {

View File

@@ -1,6 +1,7 @@
package domains
import (
"certimate/internal/applicant"
"certimate/internal/utils/app"
"certimate/internal/utils/xtime"
"time"
@@ -20,6 +21,7 @@ type history struct {
Phase Phase `json:"phase"`
PhaseSuccess bool `json:"phaseSuccess"`
DeployedAt string `json:"deployedAt"`
Cert *applicant.Certificate `json:"cert"`
}
func NewHistory(record *models.Record) *history {
@@ -52,6 +54,10 @@ func (a *history) record(phase Phase, msg string, err error, pass ...bool) {
}
func (a *history) setCert(cert *applicant.Certificate) {
a.Cert = cert
}
func (a *history) commit() error {
collection, err := app.GetApp().Dao().FindCollectionByNameOrId("deployments")
if err != nil {
@@ -78,6 +84,19 @@ func (a *history) commit() error {
domainRecord.Set("lastDeployedAt", a.DeployedAt)
domainRecord.Set("lastDeployment", record.Id)
domainRecord.Set("rightnow", false)
if a.Phase == deployPhase && a.PhaseSuccess {
domainRecord.Set("deployed", true)
}
cert := a.Cert
if cert != nil {
domainRecord.Set("certUrl", cert.CertUrl)
domainRecord.Set("certStableUrl", cert.CertStableUrl)
domainRecord.Set("privateKey", cert.PrivateKey)
domainRecord.Set("certificate", cert.Certificate)
domainRecord.Set("issuerCertificate", cert.IssuerCertificate)
domainRecord.Set("csr", cert.Csr)
domainRecord.Set("expiredAt", time.Now().Add(time.Hour*24*90))
}
if err := app.GetApp().Dao().SaveRecord(domainRecord); err != nil {
return err