feat(edgestack): git stack versioning [EE-5458] (#9126)

pull/9138/head
Oscar Zhou 2023-06-30 16:49:38 +12:00 committed by GitHub
parent ceabb2884b
commit bc47061624
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 10 deletions

View File

@ -320,8 +320,16 @@ func (service *Service) StoreEdgeStackFileFromBytes(edgeStackIdentifier, fileNam
// GetEdgeStackProjectPathByVersion returns the absolute path on the FS for a edge stack based
// on its identifier and version.
// EE only feature
func (service *Service) GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int) string {
versionStr := fmt.Sprintf("v%d", version)
func (service *Service) GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int, commitHash string) string {
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
if commitHash != "" {
versionStr = fmt.Sprintf("%s", commitHash)
}
return JoinPaths(service.wrapFileStore(EdgeStackStorePath), edgeStackIdentifier, versionStr)
}
@ -329,7 +337,10 @@ func (service *Service) GetEdgeStackProjectPathByVersion(edgeStackIdentifier str
// It returns the path to the folder where the file is stored.
// EE only feature
func (service *Service) StoreEdgeStackFileFromBytesByVersion(edgeStackIdentifier, fileName string, version int, data []byte) (string, error) {
versionStr := fmt.Sprintf("v%d", version)
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
stackStorePath := JoinPaths(EdgeStackStorePath, edgeStackIdentifier, versionStr)
err := service.createDirectoryInStore(stackStorePath)
@ -349,8 +360,16 @@ func (service *Service) StoreEdgeStackFileFromBytesByVersion(edgeStackIdentifier
}
// FormProjectPathByVersion returns the absolute path on the FS for a project based with version
func (service *Service) FormProjectPathByVersion(path string, version int) string {
versionStr := fmt.Sprintf("v%d", version)
func (service *Service) FormProjectPathByVersion(path string, version int, commitHash string) string {
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
if commitHash != "" {
versionStr = fmt.Sprintf("%s", commitHash)
}
return JoinPaths(path, versionStr)
}

View File

@ -6,13 +6,14 @@ import (
"github.com/pkg/errors"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/filesystem"
"github.com/portainer/portainer/api/git"
gittypes "github.com/portainer/portainer/api/git/types"
"github.com/rs/zerolog/log"
)
// UpdateGitObject updates a git object based on its config
func UpdateGitObject(gitService portainer.GitService, objId string, gitConfig *gittypes.RepoConfig, forceUpdate bool, projectPath string) (bool, string, error) {
func UpdateGitObject(gitService portainer.GitService, objId string, gitConfig *gittypes.RepoConfig, forceUpdate, enableVersionFolder bool, projectPath string) (bool, string, error) {
if gitConfig == nil {
return false, "", nil
}
@ -46,10 +47,15 @@ func UpdateGitObject(gitService portainer.GitService, objId string, gitConfig *g
return false, newHash, nil
}
toDir := projectPath
if enableVersionFolder {
toDir = filesystem.JoinPaths(projectPath, newHash)
}
cloneParams := &cloneRepositoryParameters{
url: gitConfig.URL,
ref: gitConfig.ReferenceName,
toDir: projectPath,
toDir: toDir,
tlsSkipVerify: gitConfig.TLSSkipVerify,
}
if gitConfig.Authentication != nil {

View File

@ -1388,9 +1388,9 @@ type (
RollbackStackFile(stackIdentifier, fileName string) error
GetEdgeStackProjectPath(edgeStackIdentifier string) string
StoreEdgeStackFileFromBytes(edgeStackIdentifier, fileName string, data []byte) (string, error)
GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int) string
GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int, commitHash string) string
StoreEdgeStackFileFromBytesByVersion(edgeStackIdentifier, fileName string, version int, data []byte) (string, error)
FormProjectPathByVersion(projectIdentifier string, version int) string
FormProjectPathByVersion(projectIdentifier string, version int, commitHash string) string
SafeMoveDirectory(src, dst string) error
StoreRegistryManagementFileFromBytes(folder, fileName string, data []byte) (string, error)
KeyPairFilesExist() (bool, error)

View File

@ -61,7 +61,7 @@ func RedeployWhenChanged(stackID portainer.StackID, deployer StackDeployer, data
var gitCommitChangedOrForceUpdate bool
if !stack.FromAppTemplate {
updated, newHash, err := update.UpdateGitObject(gitService, fmt.Sprintf("stack:%d", stackID), stack.GitConfig, false, stack.ProjectPath)
updated, newHash, err := update.UpdateGitObject(gitService, fmt.Sprintf("stack:%d", stackID), stack.GitConfig, false, false, stack.ProjectPath)
if err != nil {
return err
}