feat(stack): introduce versioning for stack file [EE-5674] (#9184)

pull/9202/head
Oscar Zhou 2023-07-13 11:06:24 +12:00 committed by GitHub
parent a216a1e960
commit b93aced176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 1 deletions

View File

@ -159,6 +159,20 @@ func (service *Service) GetStackProjectPath(stackIdentifier string) string {
return JoinPaths(service.wrapFileStore(ComposeStorePath), stackIdentifier) return JoinPaths(service.wrapFileStore(ComposeStorePath), stackIdentifier)
} }
// GetStackProjectPathByVersion returns the absolute path on the FS for a stack based
// on its identifier and version.
func (service *Service) GetStackProjectPathByVersion(stackIdentifier 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(ComposeStorePath), stackIdentifier, versionStr)
}
// Copy copies the file on fromFilePath to toFilePath // Copy copies the file on fromFilePath to toFilePath
// if toFilePath exists func will fail unless deleteIfExists is true // if toFilePath exists func will fail unless deleteIfExists is true
func (service *Service) Copy(fromFilePath string, toFilePath string, deleteIfExists bool) error { func (service *Service) Copy(fromFilePath string, toFilePath string, deleteIfExists bool) error {
@ -239,6 +253,31 @@ func (service *Service) StoreStackFileFromBytes(stackIdentifier, fileName string
return service.wrapFileStore(stackStorePath), nil return service.wrapFileStore(stackStorePath), nil
} }
// StoreStackFileFromBytesByVersion creates a version subfolder in the ComposeStorePath and stores a new file from bytes.
// It returns the path to the folder where version folders are stored.
func (service *Service) StoreStackFileFromBytesByVersion(stackIdentifier, fileName string, version int, data []byte) (string, error) {
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier)
stackVersionPath := JoinPaths(stackStorePath, versionStr)
err := service.createDirectoryInStore(stackVersionPath)
if err != nil {
return "", err
}
composeFilePath := JoinPaths(stackVersionPath, fileName)
r := bytes.NewReader(data)
err = service.createFileInStore(composeFilePath, r)
if err != nil {
return "", err
}
return service.wrapFileStore(stackStorePath), nil
}
// UpdateStoreStackFileFromBytes makes stack file backup and updates a new file from bytes. // UpdateStoreStackFileFromBytes makes stack file backup and updates a new file from bytes.
// It returns the path to the folder where the file is stored. // It returns the path to the folder where the file is stored.
func (service *Service) UpdateStoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error) { func (service *Service) UpdateStoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error) {
@ -266,6 +305,18 @@ func (service *Service) RemoveStackFileBackup(stackIdentifier, fileName string)
return service.removeBackupFileInStore(composeFilePath) return service.removeBackupFileInStore(composeFilePath)
} }
// RemoveStackFileBackupByVersion removes the stack file backup by version in the ComposeStorePath.
func (service *Service) RemoveStackFileBackupByVersion(stackIdentifier string, version int, fileName string) error {
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier, versionStr)
composeFilePath := JoinPaths(stackStorePath, fileName)
return service.removeBackupFileInStore(composeFilePath)
}
// RollbackStackFile rollbacks the stack file backup in the ComposeStorePath. // RollbackStackFile rollbacks the stack file backup in the ComposeStorePath.
func (service *Service) RollbackStackFile(stackIdentifier, fileName string) error { func (service *Service) RollbackStackFile(stackIdentifier, fileName string) error {
stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier) stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier)
@ -291,6 +342,35 @@ func (service *Service) RollbackStackFile(stackIdentifier, fileName string) erro
return os.Remove(backupPath) return os.Remove(backupPath)
} }
// RollbackStackFileByVersion rollbacks the stack file backup by version in the ComposeStorePath.
func (service *Service) RollbackStackFileByVersion(stackIdentifier string, version int, fileName string) error {
versionStr := ""
if version != 0 {
versionStr = fmt.Sprintf("v%d", version)
}
stackStorePath := JoinPaths(ComposeStorePath, stackIdentifier, versionStr)
composeFilePath := JoinPaths(stackStorePath, fileName)
path := service.wrapFileStore(composeFilePath)
backupPath := fmt.Sprintf("%s.bak", path)
exists, err := service.FileExists(backupPath)
if err != nil {
return err
}
if !exists {
// keep the updated/failed stack file
return nil
}
err = service.Copy(backupPath, path, true)
if err != nil {
return err
}
return os.Remove(backupPath)
}
// GetEdgeStackProjectPath returns the absolute path on the FS for a edge stack based // GetEdgeStackProjectPath returns the absolute path on the FS for a edge stack based
// on its identifier. // on its identifier.
func (service *Service) GetEdgeStackProjectPath(edgeStackIdentifier string) string { func (service *Service) GetEdgeStackProjectPath(edgeStackIdentifier string) string {

View File

@ -1384,15 +1384,19 @@ type (
DeleteTLSFile(folder string, fileType TLSFileType) error DeleteTLSFile(folder string, fileType TLSFileType) error
DeleteTLSFiles(folder string) error DeleteTLSFiles(folder string) error
GetStackProjectPath(stackIdentifier string) string GetStackProjectPath(stackIdentifier string) string
GetStackProjectPathByVersion(stackIdentifier string, version int, commitHash string) string
StoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error) 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) UpdateStoreStackFileFromBytes(stackIdentifier, fileName string, data []byte) (string, error)
RemoveStackFileBackup(stackIdentifier, fileName string) error RemoveStackFileBackup(stackIdentifier, fileName string) error
RemoveStackFileBackupByVersion(stackIdentifier string, version int, fileName string) error
RollbackStackFile(stackIdentifier, fileName string) error RollbackStackFile(stackIdentifier, fileName string) error
RollbackStackFileByVersion(stackIdentifier string, version int, fileName string) error
GetEdgeStackProjectPath(edgeStackIdentifier string) string GetEdgeStackProjectPath(edgeStackIdentifier string) string
StoreEdgeStackFileFromBytes(edgeStackIdentifier, fileName string, data []byte) (string, error) StoreEdgeStackFileFromBytes(edgeStackIdentifier, fileName string, data []byte) (string, error)
GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int, commitHash string) string GetEdgeStackProjectPathByVersion(edgeStackIdentifier string, version int, commitHash string) string
StoreEdgeStackFileFromBytesByVersion(edgeStackIdentifier, fileName string, version int, data []byte) (string, error) StoreEdgeStackFileFromBytesByVersion(edgeStackIdentifier, fileName string, version int, data []byte) (string, error)
FormProjectPathByVersion(projectIdentifier string, version int, commitHash string) string FormProjectPathByVersion(projectPath string, version int, commitHash string) string
SafeMoveDirectory(src, dst string) error SafeMoveDirectory(src, dst string) error
StoreRegistryManagementFileFromBytes(folder, fileName string, data []byte) (string, error) StoreRegistryManagementFileFromBytes(folder, fileName string, data []byte) (string, error)
KeyPairFilesExist() (bool, error) KeyPairFilesExist() (bool, error)