chore(stackbuilders): simplify the code BE-12800 (#2230)
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
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/rs/zerolog/log"
|
||||
@@ -12,11 +16,11 @@ import (
|
||||
|
||||
type StackBuilder struct {
|
||||
stack *portainer.Stack
|
||||
endpoint *portainer.Endpoint
|
||||
dataStore dataservices.DataStore
|
||||
fileService portainer.FileService
|
||||
stackDeployer deployments.StackDeployer
|
||||
deploymentConfiger deployments.StackDeploymentConfiger
|
||||
err error
|
||||
doCleanUp bool
|
||||
}
|
||||
|
||||
@@ -30,22 +34,38 @@ func CreateStackBuilder(dataStore dataservices.DataStore, fileService portainer.
|
||||
}
|
||||
}
|
||||
|
||||
func (b *StackBuilder) SaveStack() (*portainer.Stack, error) {
|
||||
defer func() { _ = b.cleanUp() }()
|
||||
|
||||
if b.hasError() {
|
||||
return nil, b.err
|
||||
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
|
||||
b.stack.Status = portainer.StackStatusDeploying
|
||||
b.stack.DeploymentStatus = []portainer.StackDeploymentStatus{
|
||||
{Status: portainer.StackStatusDeploying, Time: now},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *StackBuilder) prepare(_ context.Context, _ *StackPayload) error { return nil }
|
||||
|
||||
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 {
|
||||
b.err = fmt.Errorf("Unable to persist the stack inside the database: %w", err)
|
||||
return b.err
|
||||
return fmt.Errorf("Unable to persist the stack inside the database: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, b.err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.doCleanUp = false
|
||||
@@ -53,10 +73,6 @@ func (b *StackBuilder) SaveStack() (*portainer.Stack, error) {
|
||||
return b.stack, nil
|
||||
}
|
||||
|
||||
func (b *StackBuilder) Error() error {
|
||||
return b.err
|
||||
}
|
||||
|
||||
func (b *StackBuilder) cleanUp() error {
|
||||
if !b.doCleanUp {
|
||||
return nil
|
||||
@@ -69,6 +85,38 @@ func (b *StackBuilder) cleanUp() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *StackBuilder) hasError() bool {
|
||||
return b.err != 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
|
||||
}
|
||||
|
||||
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
|
||||
b.stack.CreatedBy = config.GetUsername()
|
||||
|
||||
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
|
||||
b.stack.CreatedBy = config.GetUsername()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user