139 lines
4.6 KiB
Go
139 lines
4.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sea-vhost/internal/panel"
|
|
"sea-vhost/internal/scope"
|
|
"sea-vhost/internal/store"
|
|
)
|
|
|
|
func (s *Projects) SubmitSiteRuntime(ctx context.Context, u store.User, projectID, siteID uint64, runtime, version, entry string) (uint64, error) {
|
|
p, err := s.authorizedProject(ctx, u, projectID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
ctx = scope.WithProject(ctx, p.ID, p.NodeID)
|
|
site, err := s.store.Site(ctx, p.TenantID, p.ID, siteID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if runtime != site.Runtime && !p.AllowRuntimeChange {
|
|
return 0, errors.New("管理员未允许此主机切换运行环境")
|
|
}
|
|
if err = s.ensureTenantWritable(ctx, p.TenantID); err != nil {
|
|
return 0, err
|
|
}
|
|
if err = s.validateRuntime(ctx, runtime, version); err != nil {
|
|
return 0, err
|
|
}
|
|
if site.Status != "running" && site.Status != "stopped" && site.Status != "awaiting_upload" {
|
|
return 0, errors.New("当前状态不能切换运行环境")
|
|
}
|
|
if site.Runtime == runtime && site.Version == version && (runtime != "Go" || site.Status == "awaiting_upload") {
|
|
return 0, errors.New("网站已使用此版本")
|
|
}
|
|
if runtime == "Go" && site.Runtime == "Go" && site.Status != "awaiting_upload" {
|
|
if entry == "" {
|
|
return 0, errors.New("请上传目标 Go SDK 编译的新程序,并填写独立程序文件名")
|
|
}
|
|
entry, err = normalizeDeploymentEntry("Go", entry)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
ownershipVersion, err := s.store.HostOwnershipVersion(ctx, p.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return s.store.CreateRuntimeJob(ctx, store.RuntimeJob{OwnershipVersion: &ownershipVersion, Action: "runtime_change", Runtime: runtime, FromVersion: site.Version, TargetVersion: version, EntryFile: entry, SiteID: &siteID})
|
|
}
|
|
func (s *Projects) changeRuntimeSite(ctx context.Context, job store.RuntimeJob, phase func(string) error) error {
|
|
if job.SiteID == nil {
|
|
return errors.New("missing site")
|
|
}
|
|
t, err := s.runtimeTarget(ctx, *job.SiteID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx = scope.WithProject(ctx, t.Project.ID, t.Project.NodeID)
|
|
ownershipVersion, err := s.store.HostOwnershipVersion(ctx, t.Project.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if job.OwnershipVersion != nil && ownershipVersion != *job.OwnershipVersion {
|
|
return errors.New("主机归属已变更,原运行环境任务已取消,请由当前管理者重新提交")
|
|
}
|
|
p, err := s.store.Project(ctx, t.Site.TenantID, t.Project.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u := store.User{Role: "host", TenantID: &p.TenantID, ProjectID: &p.ID}
|
|
if _, err = s.authorizedProject(ctx, u, p.ID); err != nil {
|
|
return err
|
|
}
|
|
if err = s.ensureTenantWritable(ctx, p.TenantID); err != nil {
|
|
return err
|
|
}
|
|
if t.Site.Runtime == job.Runtime && t.Site.Version == job.TargetVersion && job.Runtime != "Go" {
|
|
return nil
|
|
}
|
|
if t.Site.Runtime != job.Runtime && !p.AllowRuntimeChange {
|
|
return errors.New("此主机的跨语言切换权限已撤销")
|
|
}
|
|
if err = s.validateRuntime(ctx, job.Runtime, job.TargetVersion); err != nil {
|
|
return err
|
|
}
|
|
if err = phase("switching"); err != nil {
|
|
return err
|
|
}
|
|
if t.Site.Status == "running" {
|
|
t.Site, err = s.SetSiteState(ctx, u, p.ID, t.Site.ID, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if t.Site.Runtime == job.Runtime && t.Site.Status != "awaiting_upload" {
|
|
// The existing migration path verifies the new runtime and retains rollback data.
|
|
return s.migrateRuntimeSite(ctx, job, phase)
|
|
}
|
|
if t.Site.Runtime == "PHP" {
|
|
spec, e := s.phpIdentitySpec(ctx, p, t.Site)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if e = s.isolation.RemovePHPIdentity(ctx, spec); e != nil {
|
|
return s.interruptRuntimeJob(job, e.Error())
|
|
}
|
|
} else if t.Site.Status != "awaiting_upload" {
|
|
adapter, ok := s.panel.(panel.RuntimeResetter)
|
|
if !ok {
|
|
return errors.New("运行环境适配器不支持安全重置")
|
|
}
|
|
if err = adapter.ResetManagedRuntime(ctx, t.Site.PanelSiteID, t.Site.Domain); err != nil {
|
|
return s.interruptRuntimeJob(job, err.Error())
|
|
}
|
|
}
|
|
next := t.Site
|
|
next.Runtime = job.Runtime
|
|
next.Version = job.TargetVersion
|
|
status := "awaiting_upload"
|
|
if next.Runtime == "PHP" {
|
|
spec, e := s.phpIdentitySpec(ctx, p, next)
|
|
if e != nil {
|
|
return s.interruptRuntimeJob(job, e.Error())
|
|
}
|
|
if e = s.isolation.ConfigurePHPIdentity(ctx, spec); e != nil {
|
|
return s.interruptRuntimeJob(job, e.Error())
|
|
}
|
|
if e = s.isolation.SetPHPIdentityState(ctx, spec, false); e != nil {
|
|
return s.interruptRuntimeJob(job, e.Error())
|
|
}
|
|
status = "stopped"
|
|
}
|
|
if err = s.store.ChangeSiteLanguage(ctx, t.Site, next.Runtime, next.Version, status); err != nil {
|
|
return s.interruptRuntimeJob(job, err.Error())
|
|
}
|
|
return nil
|
|
}
|