205 lines
6.2 KiB
Go
205 lines
6.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sea-vhost/internal/hosting"
|
|
"sea-vhost/internal/isolation"
|
|
"sea-vhost/internal/panel"
|
|
"sea-vhost/internal/scope"
|
|
"sea-vhost/internal/store"
|
|
"time"
|
|
)
|
|
|
|
func (s *Projects) loadStartup(ctx context.Context, site store.Site) (hosting.Startup, error) {
|
|
v := hosting.Startup{WorkDir: ".", Arguments: []string{}, Environment: map[string]string{}}
|
|
raw, e := s.store.StartupCipher(ctx, site.ID)
|
|
if e != nil {
|
|
return v, e
|
|
}
|
|
if len(raw) > 0 {
|
|
plain, e := s.credentials.Open(fmt.Sprintf("site-startup:%d", site.ID), raw)
|
|
if e != nil {
|
|
return v, e
|
|
}
|
|
e = json.Unmarshal([]byte(plain), &v)
|
|
if e != nil {
|
|
return v, e
|
|
}
|
|
if site.Runtime != "PHP" {
|
|
if manager, ok := s.panel.(panel.RuntimeMaintenance); ok {
|
|
if runtime, e := manager.ManagedRuntime(ctx, site.PanelSiteID); e == nil {
|
|
v.EntryFile = runtime.EntryFile
|
|
}
|
|
}
|
|
}
|
|
return v, nil
|
|
}
|
|
if site.Runtime != "PHP" {
|
|
if manager, ok := s.panel.(panel.RuntimeMaintenance); ok {
|
|
if runtime, e := manager.ManagedRuntime(ctx, site.PanelSiteID); e == nil {
|
|
v.EntryFile = runtime.EntryFile
|
|
}
|
|
}
|
|
}
|
|
return v, nil
|
|
}
|
|
func (s *Projects) prepareSavedRunner(ctx context.Context, p store.Project, site store.Site, entry, version string) (bool, error) {
|
|
cipher, e := s.store.StartupCipher(ctx, site.ID)
|
|
if e != nil || len(cipher) == 0 {
|
|
return false, e
|
|
}
|
|
v, e := s.loadStartup(ctx, site)
|
|
if e != nil {
|
|
return false, e
|
|
}
|
|
v.EntryFile = entry
|
|
plan, _, e := s.store.PlanAndUsage(ctx, p.TenantID)
|
|
if e != nil {
|
|
return false, e
|
|
}
|
|
manager, ok := s.isolation.(isolation.StartupManager)
|
|
if !ok {
|
|
return false, errors.New("节点不支持应用启动器")
|
|
}
|
|
e = manager.ConfigureStartup(ctx, isolation.StartupSpec{Project: isolation.ProjectSpec{TenantID: p.TenantID, HostName: p.HostName, RunPath: site.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(site.ProcessLimit)}, SiteName: lifecycleSiteName(p, site), Runtime: site.Runtime, Version: version, Port: site.BackendPort, Config: v})
|
|
return e == nil, e
|
|
}
|
|
func (s *Projects) Startup(ctx context.Context, user store.User, projectID, siteID uint64) (hosting.Startup, error) {
|
|
p, e := s.authorizedProject(ctx, user, projectID)
|
|
if e != nil {
|
|
return hosting.Startup{}, e
|
|
}
|
|
site, e := s.store.Site(ctx, p.TenantID, p.ID, siteID)
|
|
if e != nil {
|
|
return hosting.Startup{}, e
|
|
}
|
|
return s.loadStartup(ctx, site)
|
|
}
|
|
func (s *Projects) SetStartup(ctx context.Context, user store.User, projectID, siteID uint64, v hosting.Startup) error {
|
|
if e := v.Validate(); e != nil {
|
|
return e
|
|
}
|
|
p, e := s.authorizedProject(ctx, user, projectID)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
ctx = scope.WithProject(ctx, p.ID, p.NodeID)
|
|
if e = s.ensureTenantWritable(ctx, p.TenantID); e != nil {
|
|
return e
|
|
}
|
|
site, e := s.store.Site(ctx, p.TenantID, p.ID, siteID)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if site.Runtime == "PHP" && (v.EntryFile != "" || len(v.Arguments) > 0 || v.WorkDir != ".") {
|
|
return errors.New("PHP 使用网站运行目录,仅需配置环境变量")
|
|
}
|
|
if site.Runtime != "PHP" {
|
|
entry, e := normalizeDeploymentEntry(site.Runtime, v.EntryFile)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
v.EntryFile = entry
|
|
}
|
|
old, e := s.loadStartup(ctx, site)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
manager, ok := s.isolation.(isolation.StartupManager)
|
|
if !ok {
|
|
return errors.New("节点暂不支持启动配置")
|
|
}
|
|
plan, _, e := s.store.PlanAndUsage(ctx, p.TenantID)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
spec := isolation.StartupSpec{Project: isolation.ProjectSpec{TenantID: p.TenantID, HostName: p.HostName, RunPath: site.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(site.ProcessLimit)}, SiteName: lifecycleSiteName(p, site), Runtime: site.Runtime, Version: site.Version, Port: site.BackendPort, Config: v}
|
|
if site.Runtime != "PHP" && site.Status == "awaiting_upload" {
|
|
raw, _ := json.Marshal(v)
|
|
cipher, e := s.credentials.Seal(fmt.Sprintf("site-startup:%d", site.ID), string(raw))
|
|
if e != nil {
|
|
return e
|
|
}
|
|
return s.store.SaveStartupCipher(ctx, site.ID, cipher)
|
|
}
|
|
if e = manager.ConfigureStartup(ctx, spec); e != nil {
|
|
return e
|
|
}
|
|
var restoreRuntime func(context.Context) error
|
|
restore := func() error {
|
|
rollback, cancel := context.WithTimeout(context.WithoutCancel(ctx), 60*time.Second)
|
|
defer cancel()
|
|
spec.Config = old
|
|
err := manager.ConfigureStartup(rollback, spec)
|
|
if restoreRuntime != nil {
|
|
err = errors.Join(err, restoreRuntime(rollback))
|
|
}
|
|
return err
|
|
}
|
|
if site.Runtime == "PHP" {
|
|
identity, e := s.phpIdentitySpec(ctx, p, site)
|
|
if e != nil {
|
|
restore()
|
|
return e
|
|
}
|
|
restoreRuntime = func(rollback context.Context) error {
|
|
err := s.isolation.ConfigurePHPIdentity(rollback, identity)
|
|
if site.Status != "running" {
|
|
err = errors.Join(err, s.isolation.SetPHPIdentityState(rollback, identity, false))
|
|
}
|
|
return err
|
|
}
|
|
if e = s.isolation.ConfigurePHPIdentity(ctx, identity); e != nil {
|
|
restore()
|
|
return e
|
|
}
|
|
if site.Status != "running" {
|
|
if e = s.isolation.SetPHPIdentityState(ctx, identity, false); e != nil {
|
|
return errors.Join(e, restore())
|
|
}
|
|
}
|
|
} else {
|
|
adapter, ok := s.panel.(panel.RuntimeMaintenance)
|
|
if !ok {
|
|
restore()
|
|
return errors.New("面板暂不支持启动配置替换")
|
|
}
|
|
runtime, e := adapter.ManagedRuntime(ctx, site.PanelSiteID)
|
|
if e != nil {
|
|
restore()
|
|
return e
|
|
}
|
|
next := runtime
|
|
restoreRuntime = func(rollback context.Context) error {
|
|
err := adapter.ReplaceManagedRuntime(rollback, site.PanelSiteID, runtime)
|
|
if site.Status != "running" {
|
|
err = errors.Join(err, s.panel.SetSiteState(rollback, site.PanelSiteID, site.Domain, false))
|
|
}
|
|
return err
|
|
}
|
|
next.Runner = true
|
|
next.EntryFile = v.EntryFile
|
|
if e = adapter.ReplaceManagedRuntime(ctx, site.PanelSiteID, next); e != nil {
|
|
restore()
|
|
return e
|
|
}
|
|
if site.Status != "running" {
|
|
if e = s.panel.SetSiteState(ctx, site.PanelSiteID, site.Domain, false); e != nil {
|
|
return errors.Join(e, restore())
|
|
}
|
|
}
|
|
}
|
|
raw, _ := json.Marshal(v)
|
|
cipher, e := s.credentials.Seal(fmt.Sprintf("site-startup:%d", site.ID), string(raw))
|
|
if e != nil {
|
|
return errors.Join(e, restore())
|
|
}
|
|
if e = s.store.SaveStartupCipher(ctx, site.ID, cipher); e != nil {
|
|
return errors.Join(e, restore())
|
|
}
|
|
return nil
|
|
}
|