diff --git a/api/filesystem/filesystem.go b/api/filesystem/filesystem.go index 10579cf67..3042f7a8b 100644 --- a/api/filesystem/filesystem.go +++ b/api/filesystem/filesystem.go @@ -302,6 +302,38 @@ func (service *Service) UpdateStoreStackFileFromBytes(stackIdentifier, fileName return service.wrapFileStore(stackStorePath), nil } +// UpdateStoreStackFileFromBytesByVersion makes stack file backup and updates a new file from bytes. +// It returns the path to the folder where the file is stored. +func (service *Service) UpdateStoreStackFileFromBytesByVersion(stackIdentifier, fileName string, version int, commitHash string, data []byte) (string, error) { + stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier) + + versionStr := "" + if version != 0 { + versionStr = fmt.Sprintf("v%d", version) + } + if commitHash != "" { + versionStr = commitHash + } + + if versionStr != "" { + stackStorePath = JoinPaths(stackStorePath, versionStr) + } + + composeFilePath := JoinPaths(stackStorePath, fileName) + err := service.createBackupFileInStore(composeFilePath) + if err != nil { + return "", err + } + + r := bytes.NewReader(data) + err = service.createFileInStore(composeFilePath, r) + if err != nil { + return "", err + } + + return service.wrapFileStore(stackStorePath), nil +} + // RemoveStackFileBackup removes the stack file backup in the ComposeStorePath. func (service *Service) RemoveStackFileBackup(stackIdentifier, fileName string) error { stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier) diff --git a/api/portainer.go b/api/portainer.go index e998976c2..89ae3009e 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -1413,6 +1413,7 @@ type ( StoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error) StoreStackFileFromBytesByVersion(stackIdentifier, fileName string, version int, data []byte) (string, error) UpdateStoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error) + UpdateStoreStackFileFromBytesByVersion(stackIdentifier, fileName string, version int, commitHash string, data []byte) (string, error) RemoveStackFileBackup(stackIdentifier, fileName string) error RemoveStackFileBackupByVersion(stackIdentifier string, version int, fileName string) error RollbackStackFile(stackIdentifier, fileName string) error