d0d3c068ba
Adds append-only version history on disk (compose/{id}/v{N}/<files>) for
file-based (WorkflowID==0) Compose/Swarm stacks, with rollback to any past
version. Git stacks (versioned by commit) and Kubernetes are untouched.
Backend:
- Stack model: StackFileVersion, PreviousDeploymentInfo, Versions[]; new
StackFileVersionInfo type. APIVersion 2.43.0 -> 2.44.0.
- Versioned multi-file snapshot (entrypoint + AdditionalFiles) into v{N}/;
ProjectPath repointed via GetStackProjectPathByVersion each deploy. Retention
cap (20): Versions[] trimmed in-tx, old dirs deleted only AFTER the tx commits.
- Update handlers: RollbackTo (content read server-side from the target version,
never trusted from the client; validated 1..current & present in Versions).
- Create paths seed v1. stackFile reads ?version= (validated; negative -> 400).
- New GET /stacks/{id}/versions endpoint.
- Migration 2.44.0: move existing file-based stacks' files into v1/ (idempotent,
atomic pre-read of the full file set, skips git/kube/orphans).
Frontend:
- useStackVersions query + stackVersions key; StackEditorTab builds the full
history list; StackVersionSelector shows 'v{N} · date · author'; file/versions
caches invalidated (by prefix) after deploy/rollback.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
162 lines
4.8 KiB
Go
162 lines
4.8 KiB
Go
package stackbuilders
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/dataservices"
|
|
"github.com/portainer/portainer/api/http/security"
|
|
"github.com/portainer/portainer/api/stacks/deployments"
|
|
"github.com/portainer/portainer/api/stacks/stackutils"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type StackBuilder struct {
|
|
stack *portainer.Stack
|
|
endpoint *portainer.Endpoint
|
|
dataStore dataservices.DataStore
|
|
fileService portainer.FileService
|
|
stackDeployer deployments.StackDeployer
|
|
deploymentConfiger deployments.StackDeploymentConfiger
|
|
doCleanUp bool
|
|
}
|
|
|
|
func CreateStackBuilder(dataStore dataservices.DataStore, fileService portainer.FileService, deployer deployments.StackDeployer) StackBuilder {
|
|
return StackBuilder{
|
|
stack: &portainer.Stack{},
|
|
dataStore: dataStore,
|
|
fileService: fileService,
|
|
stackDeployer: deployer,
|
|
doCleanUp: true,
|
|
}
|
|
}
|
|
|
|
func (b *StackBuilder) setGeneralInfo(_ *StackPayload, endpoint *portainer.Endpoint) {
|
|
b.endpoint = endpoint
|
|
stackID := b.dataStore.Stack().GetNextIdentifier()
|
|
b.stack.ID = portainer.StackID(stackID)
|
|
b.stack.EndpointID = endpoint.ID
|
|
now := time.Now().Unix()
|
|
b.stack.CreationDate = now
|
|
stackutils.PrepareStackStatusForDeployment(b.stack)
|
|
}
|
|
|
|
func (b *StackBuilder) deploy(ctx context.Context, _ *portainer.Endpoint) error {
|
|
return b.deploymentConfiger.Deploy(ctx)
|
|
}
|
|
|
|
func (b *StackBuilder) postDeploy(_ context.Context, _ *portainer.Stack) error { return nil }
|
|
|
|
func (b *StackBuilder) saveStack() (*portainer.Stack, error) {
|
|
defer func() { _ = b.cleanUp() }()
|
|
|
|
if err := b.dataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
if err := tx.Stack().Create(b.stack); err != nil {
|
|
return fmt.Errorf("Unable to persist the stack inside the database: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b.doCleanUp = false
|
|
|
|
return b.stack, nil
|
|
}
|
|
|
|
func (b *StackBuilder) cleanUp() error {
|
|
if !b.doCleanUp {
|
|
return nil
|
|
}
|
|
|
|
if b.stack.WorkflowID != 0 {
|
|
if err := b.dataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
err := tx.Workflow().Delete(b.stack.WorkflowID)
|
|
if tx.IsErrObjectNotFound(err) {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}); err != nil {
|
|
log.Error().Err(err).Msg("unable to cleanup orphan workflow records after failed stack creation")
|
|
}
|
|
}
|
|
|
|
if err := b.fileService.RemoveDirectory(b.stack.ProjectPath); err != nil {
|
|
log.Error().Err(err).Msg("unable to cleanup stack creation")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *StackBuilder) initCreatedBy(userID portainer.UserID) error {
|
|
return b.dataStore.ViewTx(func(tx dataservices.DataStoreTx) error {
|
|
user, err := tx.User().Read(userID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to find stack author: %w", err)
|
|
}
|
|
b.stack.CreatedBy = user.Username
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (b *StackBuilder) storeStackFile(content []byte) error {
|
|
stackFolder := strconv.Itoa(int(b.stack.ID))
|
|
projectPath, err := b.fileService.StoreStackFileFromBytes(stackFolder, b.stack.EntryPoint, content)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b.stack.ProjectPath = projectPath
|
|
|
|
return nil
|
|
}
|
|
|
|
// storeStackFileVersioned stores the initial file of a file-based (non-git) Compose/Swarm stack
|
|
// into the v1 version folder and seeds the append-only version history. Note that
|
|
// StoreStackFileFromBytesByVersion returns the base path (not the version path), so ProjectPath
|
|
// is set explicitly via GetStackProjectPathByVersion.
|
|
func (b *StackBuilder) storeStackFileVersioned(content []byte) error {
|
|
stackFolder := strconv.Itoa(int(b.stack.ID))
|
|
if _, err := b.fileService.StoreStackFileFromBytesByVersion(stackFolder, b.stack.EntryPoint, 1, content); err != nil {
|
|
return err
|
|
}
|
|
|
|
b.stack.ProjectPath = b.fileService.GetStackProjectPathByVersion(stackFolder, 1, "")
|
|
b.stack.StackFileVersion = 1
|
|
b.stack.Versions = []portainer.StackFileVersionInfo{{
|
|
Version: 1,
|
|
CreatedAt: time.Now().Unix(),
|
|
CreatedBy: b.stack.CreatedBy,
|
|
}}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *StackBuilder) initComposeDeployment(secCtx *security.RestrictedRequestContext, endpoint *portainer.Endpoint) error {
|
|
config, err := deployments.CreateComposeStackDeploymentConfigTx(b.dataStore, secCtx, b.stack, endpoint, b.fileService, b.stackDeployer, false, false, false)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create compose deployment config: %w", err)
|
|
}
|
|
|
|
b.deploymentConfiger = config
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *StackBuilder) initSwarmDeployment(secCtx *security.RestrictedRequestContext, endpoint *portainer.Endpoint) error {
|
|
config, err := deployments.CreateSwarmStackDeploymentConfigTx(b.dataStore, secCtx, b.stack, endpoint, b.fileService, b.stackDeployer, false, true)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create swarm deployment config: %w", err)
|
|
}
|
|
|
|
b.deploymentConfiger = config
|
|
|
|
return nil
|
|
}
|