mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 13:39:53 +00:00
refactor: clean code
This commit is contained in:
parent
8af5235e4d
commit
2a68372713
@ -28,9 +28,9 @@ type applyConfig struct {
|
|||||||
DisableFollowCNAME bool
|
DisableFollowCNAME bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApplyResult struct {
|
type ApplyCertResult struct {
|
||||||
PrivateKey string
|
|
||||||
Certificate string
|
Certificate string
|
||||||
|
PrivateKey string
|
||||||
IssuerCertificate string
|
IssuerCertificate string
|
||||||
ACMECertUrl string
|
ACMECertUrl string
|
||||||
ACMECertStableUrl string
|
ACMECertStableUrl string
|
||||||
@ -38,13 +38,15 @@ type ApplyResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type applicant interface {
|
type applicant interface {
|
||||||
Apply() (*ApplyResult, error)
|
Apply() (*ApplyCertResult, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) {
|
func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) {
|
||||||
// 获取授权配置
|
if node.Type != domain.WorkflowNodeTypeApply {
|
||||||
accessRepo := repository.NewAccessRepository()
|
return nil, fmt.Errorf("node type is not apply")
|
||||||
|
}
|
||||||
|
|
||||||
|
accessRepo := repository.NewAccessRepository()
|
||||||
access, err := accessRepo.GetById(context.Background(), node.GetConfigString("providerAccessId"))
|
access, err := accessRepo.GetById(context.Background(), node.GetConfigString("providerAccessId"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("access record not found: %w", err)
|
return nil, fmt.Errorf("access record not found: %w", err)
|
||||||
@ -71,7 +73,7 @@ func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*ApplyResult, error) {
|
func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*ApplyCertResult, error) {
|
||||||
record, _ := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name='sslProvider'")
|
record, _ := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name='sslProvider'")
|
||||||
|
|
||||||
sslProvider := &acmeSSLProviderConfig{
|
sslProvider := &acmeSSLProviderConfig{
|
||||||
@ -131,7 +133,7 @@ func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*App
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ApplyResult{
|
return &ApplyCertResult{
|
||||||
PrivateKey: string(certificates.PrivateKey),
|
PrivateKey: string(certificates.PrivateKey),
|
||||||
Certificate: string(certificates.Certificate),
|
Certificate: string(certificates.Certificate),
|
||||||
IssuerCertificate: string(certificates.IssuerCertificate),
|
IssuerCertificate: string(certificates.IssuerCertificate),
|
||||||
@ -166,6 +168,6 @@ type proxyApplicant struct {
|
|||||||
applyConfig *applyConfig
|
applyConfig *applyConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *proxyApplicant) Apply() (*ApplyResult, error) {
|
func (d *proxyApplicant) Apply() (*ApplyCertResult, error) {
|
||||||
return apply(d.applicant, d.applyConfig)
|
return apply(d.applicant, d.applyConfig)
|
||||||
}
|
}
|
||||||
|
@ -2,47 +2,55 @@ package deployer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/usual2970/certimate/internal/applicant"
|
|
||||||
"github.com/usual2970/certimate/internal/domain"
|
"github.com/usual2970/certimate/internal/domain"
|
||||||
"github.com/usual2970/certimate/internal/pkg/core/deployer"
|
"github.com/usual2970/certimate/internal/pkg/core/deployer"
|
||||||
"github.com/usual2970/certimate/internal/pkg/core/logger"
|
"github.com/usual2970/certimate/internal/pkg/core/logger"
|
||||||
|
"github.com/usual2970/certimate/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeployerOption struct {
|
|
||||||
NodeId string `json:"nodeId"`
|
|
||||||
Domains string `json:"domains"`
|
|
||||||
AccessConfig string `json:"accessConfig"`
|
|
||||||
AccessRecord *domain.Access `json:"-"`
|
|
||||||
DeployConfig domain.DeployConfig `json:"deployConfig"`
|
|
||||||
Certificate applicant.ApplyResult `json:"certificate"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Deployer interface {
|
type Deployer interface {
|
||||||
Deploy(ctx context.Context) error
|
Deploy(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWithProviderAndOption(provider string, option *DeployerOption) (Deployer, error) {
|
func NewWithDeployNode(node *domain.WorkflowNode, certdata struct {
|
||||||
deployer, logger, err := createDeployer(domain.DeployProviderType(provider), option.AccessRecord.Config, option.DeployConfig.NodeConfig)
|
Certificate string
|
||||||
|
PrivateKey string
|
||||||
|
},
|
||||||
|
) (Deployer, error) {
|
||||||
|
if node.Type != domain.WorkflowNodeTypeApply {
|
||||||
|
return nil, fmt.Errorf("node type is not deploy")
|
||||||
|
}
|
||||||
|
|
||||||
|
accessRepo := repository.NewAccessRepository()
|
||||||
|
access, err := accessRepo.GetById(context.Background(), node.GetConfigString("providerAccessId"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("access record not found: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
deployer, logger, err := createDeployer(domain.DeployProviderType(node.GetConfigString("provider")), access.Config, node.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &proxyDeployer{
|
return &proxyDeployer{
|
||||||
option: option,
|
|
||||||
logger: logger,
|
logger: logger,
|
||||||
deployer: deployer,
|
deployer: deployer,
|
||||||
|
deployCertificate: certdata.Certificate,
|
||||||
|
deployPrivateKey: certdata.PrivateKey,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 暂时使用代理模式以兼容之前版本代码,后续重新实现此处逻辑
|
// TODO: 暂时使用代理模式以兼容之前版本代码,后续重新实现此处逻辑
|
||||||
type proxyDeployer struct {
|
type proxyDeployer struct {
|
||||||
option *DeployerOption
|
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
deployer deployer.Deployer
|
deployer deployer.Deployer
|
||||||
|
deployCertificate string
|
||||||
|
deployPrivateKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *proxyDeployer) Deploy(ctx context.Context) error {
|
func (d *proxyDeployer) Deploy(ctx context.Context) error {
|
||||||
_, err := d.deployer.Deploy(ctx, d.option.Certificate.Certificate, d.option.Certificate.PrivateKey)
|
_, err := d.deployer.Deploy(ctx, d.deployCertificate, d.deployPrivateKey)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package domain
|
|
||||||
|
|
||||||
// Deprecated: TODO: 即将废弃
|
|
||||||
type DeployConfig struct {
|
|
||||||
NodeId string `json:"nodeId"`
|
|
||||||
NodeConfig map[string]any `json:"nodeConfig"`
|
|
||||||
Provider string `json:"provider"`
|
|
||||||
ProviderAccessId string `json:"providerAccessId"`
|
|
||||||
}
|
|
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/usual2970/certimate/internal/applicant"
|
|
||||||
"github.com/usual2970/certimate/internal/deployer"
|
"github.com/usual2970/certimate/internal/deployer"
|
||||||
"github.com/usual2970/certimate/internal/domain"
|
"github.com/usual2970/certimate/internal/domain"
|
||||||
"github.com/usual2970/certimate/internal/repository"
|
"github.com/usual2970/certimate/internal/repository"
|
||||||
@ -58,34 +57,10 @@ func (d *deployNode) Run(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
accessRepo := repository.NewAccessRepository()
|
deploy, err := deployer.NewWithDeployNode(d.node, struct {
|
||||||
access, err := accessRepo.GetById(context.Background(), d.node.GetConfigString("providerAccessId"))
|
Certificate string
|
||||||
if err != nil {
|
PrivateKey string
|
||||||
d.AddOutput(ctx, d.node.Name, "获取授权配置失败", err.Error())
|
}{Certificate: cert.Certificate, PrivateKey: cert.PrivateKey})
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
option := &deployer.DeployerOption{
|
|
||||||
NodeId: d.node.Id,
|
|
||||||
Domains: cert.SubjectAltNames,
|
|
||||||
AccessConfig: access.Config,
|
|
||||||
AccessRecord: access,
|
|
||||||
Certificate: applicant.ApplyResult{
|
|
||||||
ACMECertUrl: cert.ACMECertUrl,
|
|
||||||
ACMECertStableUrl: cert.ACMECertStableUrl,
|
|
||||||
PrivateKey: cert.PrivateKey,
|
|
||||||
Certificate: cert.Certificate,
|
|
||||||
IssuerCertificate: cert.IssuerCertificate,
|
|
||||||
},
|
|
||||||
DeployConfig: domain.DeployConfig{
|
|
||||||
NodeId: d.node.Id,
|
|
||||||
NodeConfig: d.node.Config,
|
|
||||||
Provider: d.node.GetConfigString("provider"),
|
|
||||||
ProviderAccessId: access.Id,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
deploy, err := deployer.NewWithProviderAndOption(d.node.GetConfigString("provider"), option)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.AddOutput(ctx, d.node.Name, "获取部署对象失败", err.Error())
|
d.AddOutput(ctx, d.node.Name, "获取部署对象失败", err.Error())
|
||||||
return err
|
return err
|
||||||
|
@ -154,6 +154,7 @@ const DeployNodeForm = ({ node }: DeployFormProps) => {
|
|||||||
const handleProviderSelect = (value: string) => {
|
const handleProviderSelect = (value: string) => {
|
||||||
if (fieldProvider === value) return;
|
if (fieldProvider === value) return;
|
||||||
|
|
||||||
|
// TODO: 暂时不支持切换部署目标,需后端调整,否则之前若存在部署结果输出就不会再部署
|
||||||
// 切换部署目标时重置表单,避免其他部署目标的配置字段影响当前部署目标
|
// 切换部署目标时重置表单,避免其他部署目标的配置字段影响当前部署目标
|
||||||
if (node.config?.provider === value) {
|
if (node.config?.provider === value) {
|
||||||
formInst.resetFields();
|
formInst.resetFields();
|
||||||
@ -179,7 +180,13 @@ const DeployNodeForm = ({ node }: DeployFormProps) => {
|
|||||||
<Form {...formProps} form={formInst} disabled={formPending} layout="vertical">
|
<Form {...formProps} form={formInst} disabled={formPending} layout="vertical">
|
||||||
<Show when={!!fieldProvider} fallback={<DeployProviderPicker onSelect={handleProviderPick} />}>
|
<Show when={!!fieldProvider} fallback={<DeployProviderPicker onSelect={handleProviderPick} />}>
|
||||||
<Form.Item name="provider" label={t("workflow_node.deploy.form.provider.label")} rules={[formRule]}>
|
<Form.Item name="provider" label={t("workflow_node.deploy.form.provider.label")} rules={[formRule]}>
|
||||||
<DeployProviderSelect allowClear placeholder={t("workflow_node.deploy.form.provider.placeholder")} showSearch onSelect={handleProviderSelect} />
|
<DeployProviderSelect
|
||||||
|
allowClear
|
||||||
|
disabled
|
||||||
|
placeholder={t("workflow_node.deploy.form.provider.placeholder")}
|
||||||
|
showSearch
|
||||||
|
onSelect={handleProviderSelect}
|
||||||
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item className="mb-0">
|
<Form.Item className="mb-0">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user