feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,285 @@
|
||||
package panel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BrokerCredentials struct {
|
||||
URL string `json:"url"`
|
||||
APIKey string `json:"apiKey"`
|
||||
TLSInsecure bool `json:"tlsInsecure"`
|
||||
}
|
||||
|
||||
type PanelInspection struct {
|
||||
Versions RuntimeCatalog `json:"versions"`
|
||||
Runtimes RuntimeCapabilities `json:"runtimes"`
|
||||
}
|
||||
|
||||
type BrokerClient struct {
|
||||
http *http.Client
|
||||
internalToken string
|
||||
}
|
||||
|
||||
func NewTransportClient(transport http.RoundTripper, timeout time.Duration) *BrokerClient {
|
||||
return &BrokerClient{http: &http.Client{Transport: transport, Timeout: timeout}}
|
||||
}
|
||||
|
||||
type brokerRequest struct {
|
||||
Operation string `json:"operation"`
|
||||
Site *SiteSpec `json:"site,omitempty"`
|
||||
Runtime *RuntimeSpec `json:"runtime,omitempty"`
|
||||
ID int64 `json:"id,omitempty"`
|
||||
SiteID int64 `json:"siteId,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Domain string `json:"domain,omitempty"`
|
||||
MaxBytes int64 `json:"maxBytes,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
Domains []string `json:"domains,omitempty"`
|
||||
PrivateKey string `json:"privateKey,omitempty"`
|
||||
Certificate string `json:"certificate,omitempty"`
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
Credentials *BrokerCredentials `json:"credentials,omitempty"`
|
||||
SourcePath string `json:"sourcePath,omitempty"`
|
||||
}
|
||||
|
||||
type brokerResponse struct {
|
||||
Runtime *RuntimeSpec `json:"runtime,omitempty"`
|
||||
Pending bool `json:"pending"`
|
||||
ID int64 `json:"id,omitempty"`
|
||||
Bytes uint64 `json:"bytes,omitempty"`
|
||||
Catalog RuntimeCatalog `json:"catalog,omitempty"`
|
||||
Capabilities RuntimeCapabilities `json:"capabilities,omitempty"`
|
||||
Environments []RuntimeEnvironment `json:"environments,omitempty"`
|
||||
Sites []ExistingSite `json:"sites,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func NewBrokerClient(socket string, timeout time.Duration, internalToken ...string) *BrokerClient {
|
||||
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, "unix", socket)
|
||||
}}
|
||||
token := ""
|
||||
if len(internalToken) > 0 {
|
||||
token = strings.TrimSpace(internalToken[0])
|
||||
}
|
||||
return &BrokerClient{http: &http.Client{Transport: transport, Timeout: timeout}, internalToken: token}
|
||||
}
|
||||
|
||||
func (c *BrokerClient) call(ctx context.Context, path, setupToken string, input any, output any) error {
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://unix"+path, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
if c.internalToken != "" {
|
||||
request.Header.Set("Authorization", "Bearer "+c.internalToken)
|
||||
}
|
||||
if setupToken != "" {
|
||||
request.Header.Set("X-Setup-Token", setupToken)
|
||||
}
|
||||
response, err := c.http.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("panel broker: %w", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
data, err := io.ReadAll(io.LimitReader(response.Body, 128<<10))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode > 299 {
|
||||
var payload struct {
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if json.Unmarshal(data, &payload) == nil && strings.TrimSpace(payload.Error.Message) != "" {
|
||||
return errors.New(payload.Error.Message)
|
||||
}
|
||||
return fmt.Errorf("panel broker rejected request (%d)", response.StatusCode)
|
||||
}
|
||||
if output != nil && len(data) > 0 {
|
||||
return json.Unmarshal(data, output)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *BrokerClient) TestCredentials(ctx context.Context, credentials BrokerCredentials, setupToken string) error {
|
||||
_, err := c.InspectCredentials(ctx, credentials, setupToken)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) InspectCredentials(ctx context.Context, credentials BrokerCredentials, setupToken string) (PanelInspection, error) {
|
||||
var inspection PanelInspection
|
||||
err := c.call(ctx, "/v1/setup/test", setupToken, credentials, &inspection)
|
||||
return inspection, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) Configure(ctx context.Context, credentials BrokerCredentials, setupToken string) error {
|
||||
return c.call(ctx, "/v1/setup/configure", setupToken, credentials, nil)
|
||||
}
|
||||
|
||||
func (c *BrokerClient) operation(ctx context.Context, request brokerRequest) (brokerResponse, error) {
|
||||
var response brokerResponse
|
||||
err := c.call(ctx, "/v1/operations", "", request, &response)
|
||||
return response, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) Ping(ctx context.Context) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "ping"})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) RuntimeCatalog(ctx context.Context) (RuntimeCatalog, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "runtime_catalog"})
|
||||
return response.Catalog, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) RuntimeCapabilities(ctx context.Context) (RuntimeCapabilities, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "runtime_capabilities"})
|
||||
return response.Capabilities, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) RuntimeEnvironments(ctx context.Context) ([]RuntimeEnvironment, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "runtime_environments"})
|
||||
return response.Environments, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) InstallRuntimeComponent(ctx context.Context, runtimeName string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "install_runtime_component", Name: runtimeName})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) InstallRuntimeEnvironment(ctx context.Context, runtimeName, version string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "install_runtime_environment", Name: runtimeName, Version: version})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) CreateSite(ctx context.Context, spec SiteSpec) (int64, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "create_site", Site: &spec})
|
||||
return response.ID, err
|
||||
}
|
||||
func (c *BrokerClient) ListUnmanagedSites(ctx context.Context) ([]ExistingSite, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "list_unmanaged_sites"})
|
||||
return response.Sites, err
|
||||
}
|
||||
func (c *BrokerClient) AdoptSite(ctx context.Context, id int64, sourcePath string, spec SiteSpec) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "adopt_site", ID: id, SourcePath: sourcePath, Site: &spec})
|
||||
return err
|
||||
}
|
||||
func (c *BrokerClient) RollbackAdoption(ctx context.Context, id int64, name, sourcePath string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "rollback_adoption", ID: id, Name: name, SourcePath: sourcePath})
|
||||
return err
|
||||
}
|
||||
func (c *BrokerClient) SetSitePath(context.Context, int64, string) error {
|
||||
return errors.New("site path changes are only available through managed adoption")
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DeleteSite(ctx context.Context, id int64, name string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "delete_site", ID: id, Name: name})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) AddDomain(ctx context.Context, siteID int64, siteName, domain string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "add_domain", SiteID: siteID, Name: siteName, Domain: domain})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DeleteDomain(ctx context.Context, siteID int64, siteName, domain string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "delete_domain", SiteID: siteID, Name: siteName, Domain: domain})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) CreateFTP(ctx context.Context, username, password, path string, maxBytes int64) (int64, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "create_ftp", Username: username, Password: password, Path: path, MaxBytes: maxBytes})
|
||||
return response.ID, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DeleteFTP(ctx context.Context, id int64, username string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "delete_ftp", ID: id, Username: username})
|
||||
return err
|
||||
}
|
||||
func (c *BrokerClient) SetFTPState(ctx context.Context, id int64, username string, enabled bool) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "set_ftp_state", ID: id, Username: username, Enabled: enabled})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) CreateDatabase(ctx context.Context, name, username, password string, siteID int64) (int64, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "create_database", Name: name, Username: username, Password: password, SiteID: siteID})
|
||||
return response.ID, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DeleteDatabase(ctx context.Context, id int64, name string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "delete_database", ID: id, Name: name})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DatabaseSize(ctx context.Context, id int64, name string) (uint64, error) {
|
||||
response, err := c.operation(ctx, brokerRequest{Operation: "database_size", ID: id, Name: name})
|
||||
return response.Bytes, err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) ConfigureRuntime(ctx context.Context, spec RuntimeSpec) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "configure_runtime", Runtime: &spec})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) RemoveRuntime(ctx context.Context, spec RuntimeSpec) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "remove_runtime", Runtime: &spec})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) SetRuntimeState(ctx context.Context, spec RuntimeSpec, enabled bool) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "set_runtime_state", Runtime: &spec, Enabled: enabled})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) ConfigureProxy(ctx context.Context, siteID int64, domain string, port int) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "configure_proxy", SiteID: siteID, Domain: domain, Port: port})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) RequestCertificate(ctx context.Context, siteID int64, path string, domains []string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "request_certificate", SiteID: siteID, Path: path, Domains: domains})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) InstallCertificate(ctx context.Context, siteID int64, siteName, privateKey, certificate string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "install_certificate", SiteID: siteID, Name: siteName, PrivateKey: privateKey, Certificate: certificate})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) DisableCertificate(ctx context.Context, siteID int64, siteName string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "disable_certificate", SiteID: siteID, Name: siteName})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) SetSiteState(ctx context.Context, siteID int64, name string, enabled bool) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "set_site_state", SiteID: siteID, Name: name, Enabled: enabled})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *BrokerClient) SetSiteRunPath(ctx context.Context, siteID int64, runPath string) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "set_site_run_path", SiteID: siteID, Path: runPath})
|
||||
return err
|
||||
}
|
||||
func (c *BrokerClient) MigrateRuntimeIdentity(ctx context.Context, siteID int64, runUser string, enabled bool) error {
|
||||
_, err := c.operation(ctx, brokerRequest{Operation: "migrate_runtime_identity", SiteID: siteID, Username: runUser, Enabled: enabled})
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user