feat: add baotapanel console deployer

This commit is contained in:
Fu Diwei
2025-02-15 23:32:09 +08:00
parent 6d8301b159
commit b734ffcf9d
16 changed files with 447 additions and 161 deletions

View File

@@ -0,0 +1,87 @@
package baotapanelconsole
import (
"context"
"errors"
xerrors "github.com/pkg/errors"
"github.com/usual2970/certimate/internal/pkg/core/deployer"
"github.com/usual2970/certimate/internal/pkg/core/logger"
btsdk "github.com/usual2970/certimate/internal/pkg/vendors/btpanel-sdk"
)
type BaotaPanelConsoleDeployerConfig struct {
// 宝塔面板地址。
ApiUrl string `json:"apiUrl"`
// 宝塔面板接口密钥。
ApiKey string `json:"apiKey"`
// 是否自动重启。
AutoRestart bool `json:"autoRestart"`
}
type BaotaPanelConsoleDeployer struct {
config *BaotaPanelConsoleDeployerConfig
logger logger.Logger
sdkClient *btsdk.BaoTaPanelClient
}
var _ deployer.Deployer = (*BaotaPanelConsoleDeployer)(nil)
func New(config *BaotaPanelConsoleDeployerConfig) (*BaotaPanelConsoleDeployer, error) {
return NewWithLogger(config, logger.NewNilLogger())
}
func NewWithLogger(config *BaotaPanelConsoleDeployerConfig, logger logger.Logger) (*BaotaPanelConsoleDeployer, error) {
if config == nil {
return nil, errors.New("config is nil")
}
if logger == nil {
return nil, errors.New("logger is nil")
}
client, err := createSdkClient(config.ApiUrl, config.ApiKey)
if err != nil {
return nil, xerrors.Wrap(err, "failed to create sdk client")
}
return &BaotaPanelConsoleDeployer{
logger: logger,
config: config,
sdkClient: client,
}, nil
}
func (d *BaotaPanelConsoleDeployer) Deploy(ctx context.Context, certPem string, privkeyPem string) (*deployer.DeployResult, error) {
// 设置面板 SSL 证书
configSavePanelSSLReq := &btsdk.ConfigSavePanelSSLRequest{
PrivateKey: privkeyPem,
Certificate: certPem,
}
configSavePanelSSLResp, err := d.sdkClient.ConfigSavePanelSSL(configSavePanelSSLReq)
if err != nil {
return nil, xerrors.Wrap(err, "failed to execute sdk request 'bt.ConfigSavePanelSSL'")
} else {
d.logger.Logt("已设置面板 SSL 证书", configSavePanelSSLResp)
}
if d.config.AutoRestart {
// 重启面板
systemServiceAdminReq := &btsdk.SystemServiceAdminRequest{
Name: "nginx",
Type: "restart",
}
_, err := d.sdkClient.SystemServiceAdmin(systemServiceAdminReq)
if err != nil {
return nil, xerrors.Wrap(err, "failed to execute sdk request 'bt.SystemServiceAdmin'")
}
}
return &deployer.DeployResult{}, nil
}
func createSdkClient(apiUrl, apiKey string) (*btsdk.BaoTaPanelClient, error) {
client := btsdk.NewBaoTaPanelClient(apiUrl, apiKey)
return client, nil
}

View File

@@ -0,0 +1,70 @@
package baotapanelconsole_test
import (
"context"
"flag"
"fmt"
"os"
"strings"
"testing"
provider "github.com/usual2970/certimate/internal/pkg/core/deployer/providers/baotapanel-console"
)
var (
fInputCertPath string
fInputKeyPath string
fApiUrl string
fApiKey string
)
func init() {
argsPrefix := "CERTIMATE_DEPLOYER_BAOTAPANELCONSOLE_"
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
flag.StringVar(&fApiUrl, argsPrefix+"APIURL", "", "")
flag.StringVar(&fApiKey, argsPrefix+"APIKEY", "", "")
}
/*
Shell command to run this test:
go test -v ./baotapanel_console_test.go -args \
--CERTIMATE_DEPLOYER_BAOTAPANELCONSOLE_INPUTCERTPATH="/path/to/your-input-cert.pem" \
--CERTIMATE_DEPLOYER_BAOTAPANELCONSOLE_INPUTKEYPATH="/path/to/your-input-key.pem" \
--CERTIMATE_DEPLOYER_BAOTAPANELCONSOLE_APIURL="your-baota-panel-url" \
--CERTIMATE_DEPLOYER_BAOTAPANELCONSOLE_APIKEY="your-baota-panel-key"
*/
func TestDeploy(t *testing.T) {
flag.Parse()
t.Run("Deploy", func(t *testing.T) {
t.Log(strings.Join([]string{
"args:",
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
fmt.Sprintf("APIURL: %v", fApiUrl),
fmt.Sprintf("APIKEY: %v", fApiKey),
}, "\n"))
deployer, err := provider.New(&provider.BaotaPanelConsoleDeployerConfig{
ApiUrl: fApiUrl,
ApiKey: fApiKey,
})
if err != nil {
t.Errorf("err: %+v", err)
return
}
fInputCertData, _ := os.ReadFile(fInputCertPath)
fInputKeyData, _ := os.ReadFile(fInputKeyPath)
res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData))
if err != nil {
t.Errorf("err: %+v", err)
return
}
t.Logf("ok: %v", res)
})
}

View File

@@ -59,19 +59,19 @@ func (d *BaotaPanelSiteDeployer) Deploy(ctx context.Context, certPem string, pri
}
// 设置站点 SSL 证书
setSiteSSLReq := &btsdk.SetSiteSSLRequest{
SiteName: d.config.SiteName,
Type: "1",
Key: privkeyPem,
Csr: certPem,
siteSetSSLReq := &btsdk.SiteSetSSLRequest{
SiteName: d.config.SiteName,
Type: "1",
PrivateKey: privkeyPem,
Certificate: certPem,
}
setSiteSSLResp, err := d.sdkClient.SetSiteSSL(setSiteSSLReq)
siteSetSSLResp, err := d.sdkClient.SiteSetSSL(siteSetSSLReq)
if err != nil {
return nil, xerrors.Wrap(err, "failed to execute sdk request 'bt.SetSiteSSL'")
return nil, xerrors.Wrap(err, "failed to execute sdk request 'bt.SiteSetSSL'")
} else {
d.logger.Logt("已设置站点 SSL 证书", siteSetSSLResp)
}
d.logger.Logt("已设置站点 SSL 证书", setSiteSSLResp)
return &deployer.DeployResult{}, nil
}

View File

@@ -5,22 +5,44 @@ type BaseResponse interface {
GetMsg() *string
}
type SetSiteSSLRequest struct {
Type string `json:"type"`
SiteName string `json:"siteName"`
Key string `json:"key"`
Csr string `json:"csr"`
}
type SetSiteSSLResponse struct {
type baseResponse struct {
Status *bool `json:"status,omitempty"`
Msg *string `json:"msg,omitempty"`
}
func (r *SetSiteSSLResponse) GetStatus() *bool {
func (r *baseResponse) GetStatus() *bool {
return r.Status
}
func (r *SetSiteSSLResponse) GetMsg() *string {
func (r *baseResponse) GetMsg() *string {
return r.Msg
}
type ConfigSavePanelSSLRequest struct {
PrivateKey string `json:"privateKey"`
Certificate string `json:"certPem"`
}
type ConfigSavePanelSSLResponse struct {
baseResponse
}
type SiteSetSSLRequest struct {
Type string `json:"type"`
SiteName string `json:"siteName"`
PrivateKey string `json:"key"`
Certificate string `json:"csr"`
}
type SiteSetSSLResponse struct {
baseResponse
}
type SystemServiceAdminRequest struct {
Name string `json:"name"`
Type string `json:"type"`
}
type SystemServiceAdminResponse struct {
baseResponse
}

View File

@@ -34,12 +34,25 @@ func (c *BaoTaPanelClient) WithTimeout(timeout time.Duration) *BaoTaPanelClient
return c
}
func (c *BaoTaPanelClient) SetSiteSSL(req *SetSiteSSLRequest) (*SetSiteSSLResponse, error) {
func (c *BaoTaPanelClient) ConfigSavePanelSSL(req *ConfigSavePanelSSLRequest) (*ConfigSavePanelSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SetSiteSSLResponse{}
result := ConfigSavePanelSSLResponse{}
err := c.sendRequestWithResult("/config?action=SavePanelSSL", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *BaoTaPanelClient) SiteSetSSL(req *SiteSetSSLRequest) (*SiteSetSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SiteSetSSLResponse{}
err := c.sendRequestWithResult("/site?action=SetSSL", params, &result)
if err != nil {
return nil, err
@@ -47,6 +60,19 @@ func (c *BaoTaPanelClient) SetSiteSSL(req *SetSiteSSLRequest) (*SetSiteSSLRespon
return &result, nil
}
func (c *BaoTaPanelClient) SystemServiceAdmin(req *SystemServiceAdminRequest) (*SystemServiceAdminResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SystemServiceAdminResponse{}
err := c.sendRequestWithResult("/system?action=ServiceAdmin", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *BaoTaPanelClient) generateSignature(timestamp string) string {
keyMd5 := md5.Sum([]byte(c.apiKey))
keyMd5Hex := strings.ToLower(hex.EncodeToString(keyMd5[:]))