Skip to content

Commit 1bf9810

Browse files
committed
rollback maniftest
1 parent 263b747 commit 1bf9810

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

internal/file/file_manager_service.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,24 @@ type FileManagerService struct {
9696
// map of the contents of files which have been updated or deleted during config apply, used during rollback
9797
rollbackFileContents map[string][]byte // key is file path
9898
// map of the files currently on disk, used to determine the file action during config apply
99-
currentFilesOnDisk map[string]*mpi.File // key is file path
100-
filesMutex sync.RWMutex
99+
currentFilesOnDisk map[string]*mpi.File // key is file path
100+
previousManifestFiles map[string]*model.ManifestFile
101+
filesMutex sync.RWMutex
101102
}
102103

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

107108
return &FileManagerService{
108-
fileServiceClient: fileServiceClient,
109-
agentConfig: agentConfig,
110-
fileOperator: NewFileOperator(),
111-
fileActions: make(map[string]*model.FileCache),
112-
rollbackFileContents: make(map[string][]byte),
113-
currentFilesOnDisk: make(map[string]*mpi.File),
114-
isConnected: isConnected,
109+
fileServiceClient: fileServiceClient,
110+
agentConfig: agentConfig,
111+
fileOperator: NewFileOperator(),
112+
fileActions: make(map[string]*model.FileCache),
113+
rollbackFileContents: make(map[string][]byte),
114+
currentFilesOnDisk: make(map[string]*mpi.File),
115+
previousManifestFiles: make(map[string]*model.ManifestFile),
116+
isConnected: isConnected,
115117
}
116118
}
117119

@@ -540,12 +542,13 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
540542
func (fms *FileManagerService) ClearCache() {
541543
clear(fms.rollbackFileContents)
542544
clear(fms.fileActions)
545+
clear(fms.previousManifestFiles)
543546
}
544547

545548
// nolint:revive,cyclop
546549
func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string) error {
547550
slog.InfoContext(ctx, "Rolling back config for instance", "instanceid", instanceID)
548-
areFilesUpdated := false
551+
549552
fms.filesMutex.Lock()
550553
defer fms.filesMutex.Unlock()
551554
for _, fileAction := range fms.fileActions {
@@ -557,7 +560,7 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
557560

558561
// currentFilesOnDisk needs to be updated after rollback action is performed
559562
delete(fms.currentFilesOnDisk, fileAction.File.GetFileMeta().GetName())
560-
areFilesUpdated = true
563+
//areFilesUpdated = true
561564

562565
continue
563566
case model.Delete, model.Update:
@@ -570,19 +573,17 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
570573
// currentFilesOnDisk needs to be updated after rollback action is performed
571574
fileAction.File.GetFileMeta().Hash = files.GenerateHash(content)
572575
fms.currentFilesOnDisk[fileAction.File.GetFileMeta().GetName()] = fileAction.File
573-
areFilesUpdated = true
576+
//areFilesUpdated = true
574577
case model.Unchanged:
575578
fallthrough
576579
default:
577580
slog.DebugContext(ctx, "File Action not implemented")
578581
}
579582
}
580583

581-
if areFilesUpdated {
582-
manifestFileErr := fms.UpdateManifestFile(fms.currentFilesOnDisk, true)
583-
if manifestFileErr != nil {
584-
return manifestFileErr
585-
}
584+
manifestFileErr := fms.writeManifestFile(fms.previousManifestFiles)
585+
if manifestFileErr != nil {
586+
return manifestFileErr
586587
}
587588

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

0 commit comments

Comments
 (0)