Skip to content

Rollback Manifest File #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,24 @@ type FileManagerService struct {
// map of the contents of files which have been updated or deleted during config apply, used during rollback
rollbackFileContents map[string][]byte // key is file path
// map of the files currently on disk, used to determine the file action during config apply
currentFilesOnDisk map[string]*mpi.File // key is file path
filesMutex sync.RWMutex
currentFilesOnDisk map[string]*mpi.File // key is file path
previousManifestFiles map[string]*model.ManifestFile
filesMutex sync.RWMutex
}

func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig *config.Config) *FileManagerService {
isConnected := &atomic.Bool{}
isConnected.Store(false)

return &FileManagerService{
fileServiceClient: fileServiceClient,
agentConfig: agentConfig,
fileOperator: NewFileOperator(),
fileActions: make(map[string]*model.FileCache),
rollbackFileContents: make(map[string][]byte),
currentFilesOnDisk: make(map[string]*mpi.File),
isConnected: isConnected,
fileServiceClient: fileServiceClient,
agentConfig: agentConfig,
fileOperator: NewFileOperator(),
fileActions: make(map[string]*model.FileCache),
rollbackFileContents: make(map[string][]byte),
currentFilesOnDisk: make(map[string]*mpi.File),
previousManifestFiles: make(map[string]*model.ManifestFile),
isConnected: isConnected,
}
}

Expand Down Expand Up @@ -540,12 +542,13 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
func (fms *FileManagerService) ClearCache() {
clear(fms.rollbackFileContents)
clear(fms.fileActions)
clear(fms.previousManifestFiles)
}

// nolint:revive,cyclop
func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string) error {
slog.InfoContext(ctx, "Rolling back config for instance", "instanceid", instanceID)
areFilesUpdated := false

fms.filesMutex.Lock()
defer fms.filesMutex.Unlock()
for _, fileAction := range fms.fileActions {
Expand All @@ -557,7 +560,6 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)

// currentFilesOnDisk needs to be updated after rollback action is performed
delete(fms.currentFilesOnDisk, fileAction.File.GetFileMeta().GetName())
areFilesUpdated = true

continue
case model.Delete, model.Update:
Expand All @@ -570,19 +572,16 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
// currentFilesOnDisk needs to be updated after rollback action is performed
fileAction.File.GetFileMeta().Hash = files.GenerateHash(content)
fms.currentFilesOnDisk[fileAction.File.GetFileMeta().GetName()] = fileAction.File
areFilesUpdated = true
case model.Unchanged:
fallthrough
default:
slog.DebugContext(ctx, "File Action not implemented")
}
}

if areFilesUpdated {
manifestFileErr := fms.UpdateManifestFile(fms.currentFilesOnDisk, true)
if manifestFileErr != nil {
return manifestFileErr
}
manifestFileErr := fms.writeManifestFile(fms.previousManifestFiles)
if manifestFileErr != nil {
return manifestFileErr
}

return nil
Expand Down Expand Up @@ -819,6 +818,7 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(
// nolint: revive
func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.File, referenced bool) (err error) {
currentManifestFiles, _, readError := fms.manifestFile()
fms.previousManifestFiles = currentManifestFiles
if readError != nil && !errors.Is(readError, os.ErrNotExist) {
return fmt.Errorf("unable to read manifest file: %w", readError)
}
Expand Down