From bc47061624f8e2c742a908243d6ae998247722d1 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:49:38 +1200 Subject: [PATCH] feat(edgestack): git stack versioning [EE-5458] (#9126) --- api/filesystem/filesystem.go | 29 ++++++++++++++++++++++++----- api/git/update/update.go | 10 ++++++++-- api/portainer.go | 4 ++-- api/stacks/deployments/deploy.go | 2 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/api/filesystem/filesystem.go b/api/filesystem/filesystem.go index c5f2867b4..6a0d24c5a 100644 --- a/api/filesystem/filesystem.go +++ b/api/filesystem/filesystem.go @@ -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) } diff --git a/api/git/update/update.go b/api/git/update/update.go index 3b81fae4a..203e361dd 100644 --- a/api/git/update/update.go +++ b/api/git/update/update.go @@ -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 { diff --git a/api/portainer.go b/api/portainer.go index 568b61921..eb6749bd7 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -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) diff --git a/api/stacks/deployments/deploy.go b/api/stacks/deployments/deploy.go index 89db745c9..8f2708415 100644 --- a/api/stacks/deployments/deploy.go +++ b/api/stacks/deployments/deploy.go @@ -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 }