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,140 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type OwnershipChange struct {
|
||||
ProjectID uint64 `json:"projectId"`
|
||||
TargetOwnerID *uint64 `json:"targetOwnerId"`
|
||||
ExpectedVersion uint64 `json:"expectedVersion"`
|
||||
CredentialMode string `json:"credentialMode"`
|
||||
StopServices bool `json:"stopServices"`
|
||||
State string `json:"state"`
|
||||
CredentialsCipher []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (s *Store) OwnershipChange(ctx context.Context, id uint64) (OwnershipChange, error) {
|
||||
var value OwnershipChange
|
||||
err := s.db.QueryRowContext(ctx, "SELECT project_id,target_owner_id,expected_version,credential_mode,stop_services,state,credentials_cipher FROM host_ownership_changes WHERE project_id=?", id).Scan(&value.ProjectID, &value.TargetOwnerID, &value.ExpectedVersion, &value.CredentialMode, &value.StopServices, &value.State, &value.CredentialsCipher)
|
||||
return value, err
|
||||
}
|
||||
func sameOwner(a *uint64, b uint64) bool { return (a == nil && b == 0) || (a != nil && *a == b) }
|
||||
func (s *Store) BeginOwnershipChange(ctx context.Context, id, version, target uint64, mode string, stop, resume bool, cipher []byte) (OwnershipChange, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return OwnershipChange{}, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var owner *uint64
|
||||
var actual uint64
|
||||
var locked bool
|
||||
if err = tx.QueryRowContext(ctx, "SELECT owner_tenant_id,ownership_version,management_locked FROM projects WHERE id=? AND manual_recycled_at IS NULL FOR UPDATE", id).Scan(&owner, &actual, &locked); err != nil {
|
||||
return OwnershipChange{}, err
|
||||
}
|
||||
if actual != version {
|
||||
return OwnershipChange{}, errors.New("主机归属已变化,请重新预览")
|
||||
}
|
||||
if locked {
|
||||
if !resume {
|
||||
return OwnershipChange{}, ErrManagementLocked
|
||||
}
|
||||
var pending OwnershipChange
|
||||
err = tx.QueryRowContext(ctx, "SELECT project_id,target_owner_id,expected_version,credential_mode,stop_services,state,credentials_cipher FROM host_ownership_changes WHERE project_id=?", id).Scan(&pending.ProjectID, &pending.TargetOwnerID, &pending.ExpectedVersion, &pending.CredentialMode, &pending.StopServices, &pending.State, &pending.CredentialsCipher)
|
||||
if err != nil || pending.State != "pending" || !sameOwner(pending.TargetOwnerID, target) || pending.ExpectedVersion != version || pending.CredentialMode != mode || pending.StopServices != stop {
|
||||
return pending, errors.New("恢复任务必须与未完成任务的目标用户和策略一致")
|
||||
}
|
||||
return pending, tx.Commit()
|
||||
}
|
||||
if target > 0 {
|
||||
var found uint64
|
||||
if err = tx.QueryRowContext(ctx, "SELECT t.id FROM tenants t JOIN users u ON u.tenant_id=t.id AND u.role='tenant' WHERE t.id=? AND t.status='active' AND t.resource_namespace=FALSE AND u.status='active' FOR UPDATE", target).Scan(&found); err != nil {
|
||||
return OwnershipChange{}, errors.New("目标账号不可用")
|
||||
}
|
||||
}
|
||||
var targetValue any
|
||||
if target > 0 {
|
||||
targetValue = target
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO host_ownership_changes(project_id,source_owner_id,target_owner_id,expected_version,credential_mode,stop_services,state,credentials_cipher) VALUES(?,?,?,?,?,?,'pending',?) ON DUPLICATE KEY UPDATE source_owner_id=VALUES(source_owner_id),target_owner_id=VALUES(target_owner_id),expected_version=VALUES(expected_version),credential_mode=VALUES(credential_mode),stop_services=VALUES(stop_services),state='pending',credentials_cipher=VALUES(credentials_cipher)`, id, owner, targetValue, version, mode, stop, cipher)
|
||||
if err != nil {
|
||||
return OwnershipChange{}, err
|
||||
}
|
||||
for _, statement := range []string{"UPDATE user_schedules SET enabled=FALSE,run_requested=FALSE,last_state='ownership_paused' WHERE project_id=?", "UPDATE projects SET management_locked=TRUE WHERE id=?", "UPDATE users SET status='disabled' WHERE project_id=? AND role='host'", "DELETE i FROM impersonation_sessions i JOIN users u ON u.id=i.target_user_id WHERE u.project_id=?", "DELETE s FROM sessions s JOIN users u ON u.id=s.user_id WHERE u.project_id=?"} {
|
||||
if _, err = tx.ExecContext(ctx, statement, id); err != nil {
|
||||
return OwnershipChange{}, err
|
||||
}
|
||||
}
|
||||
result := OwnershipChange{ProjectID: id, ExpectedVersion: version, CredentialMode: mode, StopServices: stop, State: "pending", CredentialsCipher: cipher}
|
||||
if target > 0 {
|
||||
result.TargetOwnerID = &target
|
||||
}
|
||||
return result, tx.Commit()
|
||||
}
|
||||
func (s *Store) CompleteOwnershipChange(ctx context.Context, id, version, target uint64, mode, hostHash string, ftpCipher, dbCipher []byte) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var actual uint64
|
||||
var locked bool
|
||||
if err = tx.QueryRowContext(ctx, "SELECT ownership_version,management_locked FROM projects WHERE id=? FOR UPDATE", id).Scan(&actual, &locked); err != nil {
|
||||
return err
|
||||
}
|
||||
if !locked || actual != version {
|
||||
return errors.New("归属核验状态已变化")
|
||||
}
|
||||
var pendingTarget *uint64
|
||||
var pendingMode, pendingState string
|
||||
if err = tx.QueryRowContext(ctx, "SELECT target_owner_id,credential_mode,state FROM host_ownership_changes WHERE project_id=? FOR UPDATE", id).Scan(&pendingTarget, &pendingMode, &pendingState); err != nil {
|
||||
return err
|
||||
}
|
||||
if !sameOwner(pendingTarget, target) || pendingMode != mode || pendingState != "pending" {
|
||||
return errors.New("归属变更策略已变化")
|
||||
}
|
||||
if target > 0 {
|
||||
var found uint64
|
||||
if err = tx.QueryRowContext(ctx, "SELECT id FROM users WHERE tenant_id=? AND role='tenant' AND status='active' FOR UPDATE", target).Scan(&found); err != nil {
|
||||
return errors.New("目标用户在任务执行期间已停用")
|
||||
}
|
||||
}
|
||||
if mode == "reset" {
|
||||
if hostHash == "" || len(ftpCipher) == 0 || len(dbCipher) == 0 {
|
||||
return errors.New("重置凭据不完整")
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE users SET password_hash=? WHERE project_id=? AND role='host'", hostHash, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE ftp_accounts SET password_ciphertext=? WHERE project_id=?", ftpCipher, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE `databases` SET password_ciphertext=? WHERE project_id=?", dbCipher, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE certificate_automations SET mode='off',next_attempt_at=NULL WHERE site_id IN (SELECT id FROM sites WHERE project_id=?)", id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE sites SET auto_ssl=FALSE WHERE project_id=?", id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE domains d JOIN dns_credentials c ON c.id=d.dns_credential_id SET d.dns_credential_id=NULL,d.dns_record_id=NULL,d.dns_record_type=NULL,d.dns_last_error=NULL WHERE d.project_id=? AND c.tenant_id IS NOT NULL", id); err != nil {
|
||||
return err
|
||||
}
|
||||
var targetValue any
|
||||
if target > 0 {
|
||||
targetValue = target
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE projects SET owner_tenant_id=?,ownership_version=ownership_version+1,management_locked=FALSE,auto_ssl=FALSE WHERE id=?", targetValue, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE users SET status='active' WHERE project_id=? AND role='host'", id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "UPDATE host_ownership_changes SET state='completed',credentials_cipher=NULL WHERE project_id=?", id); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
Reference in new issue
Block a user