Repository: bacalhau-project/bacalhau (language: python)

Issue:
Bacalhau Key already there? Why isn't it reading the existing PEM?
i don’t understand why this keeps happening - 

```
❯ bacalhau config list
16:09:52.601 | INF pkg/repo/fs.go:99 > Initializing repo at 'v1.5.0/data-dir' for environment 'production'
Error: failed to setup repo: failed to reconcile repo: failed to initialize repo: failed to initialize repo: failed to create user key: user key file already exists at path: v1.5.0/data-dir/user_id.pem
Usage:
This is bacalhau 1.4.0 (on my machine) now trying to read bacalhau 1.5.0 config directory.
```

Even if this WAS an overlap, why is it trying to create a key if there’s one already there?



Discussion / hints:

Fix patch:
diff --git a/pkg/repo/legacy.go b/pkg/repo/legacy.go
index ea8cd1c46e..c7dc7873b6 100644
--- a/pkg/repo/legacy.go
+++ b/pkg/repo/legacy.go
@@ -17,8 +17,8 @@ type Version struct {
 	Version int
 }
 
-// readLegacyVersion reads the repo version from the LegacyVersionFile.
-func (fsr *FsRepo) readLegacyVersion() (int, error) {
+// ReadLegacyVersion reads the repo version from the LegacyVersionFile.
+func (fsr *FsRepo) ReadLegacyVersion() (int, error) {
 	versionPath := fsr.join(LegacyVersionFile)
 	versionBytes, err := os.ReadFile(versionPath)
 	if err != nil {
@@ -34,8 +34,8 @@ func (fsr *FsRepo) readLegacyVersion() (int, error) {
 	return version.Version, nil
 }
 
-// writeLegacyVersion writes the repo version to LegacyVersionFile.
-func (fsr *FsRepo) writeLegacyVersion(version int) error {
+// WriteLegacyVersion writes the repo version to LegacyVersionFile.
+func (fsr *FsRepo) WriteLegacyVersion(version int) error {
 	repoVersion := Version{Version: version}
 	versionJSON, err := json.Marshal(repoVersion)
 	if err != nil {
diff --git a/pkg/repo/migrations/v3_4.go b/pkg/repo/migrations/v3_4.go
index d0245a01fc..31aee0e988 100644
--- a/pkg/repo/migrations/v3_4.go
+++ b/pkg/repo/migrations/v3_4.go
@@ -62,9 +62,10 @@ var V3Migration = repo.NewMigration(
 
 			// ignore this error as the file may not exist
 			_ = os.Remove(filepath.Join(repoPath, "update.json"))
-			// remove the legacy repo version file
-			if err := os.Remove(filepath.Join(repoPath, repo.LegacyVersionFile)); err != nil {
-				return fmt.Errorf("removing legacy repo version file: %w", err)
+
+			// update the legacy version file so older versions fail gracefully.
+			if err := r.WriteLegacyVersion(repo.Version4); err != nil {
+				return fmt.Errorf("updating repo.verion: %w", err)
 			}
 		}
 
diff --git a/pkg/repo/sysmeta.go b/pkg/repo/sysmeta.go
index d5393ea09d..42ca2721ac 100644
--- a/pkg/repo/sysmeta.go
+++ b/pkg/repo/sysmeta.go
@@ -43,7 +43,7 @@ type SystemMetadata struct {
 // If the metadata file doesn't exist, it creates a new one.
 func (fsr *FsRepo) WriteVersion(version int) error {
 	if version < Version4 {
-		return fsr.writeLegacyVersion(version)
+		return fsr.WriteLegacyVersion(version)
 	}
 	return fsr.updateOrCreateMetadata(func(m *SystemMetadata) {
 		m.RepoVersion = version
@@ -58,7 +58,7 @@ func (fsr *FsRepo) readVersion() (int, error) {
 	if err != nil {
 		// if the system metadata file does not exist attempt to read the legacy version
 		if os.IsNotExist(err) {
-			return fsr.readLegacyVersion()
+			return fsr.ReadLegacyVersion()
 		}
 		return UnknownVersion, err
 	}


Test patch:
diff --git a/pkg/repo/migrations/v3_4_test.go b/pkg/repo/migrations/v3_4_test.go
index e24a02153b..8b56b229ad 100644
--- a/pkg/repo/migrations/v3_4_test.go
+++ b/pkg/repo/migrations/v3_4_test.go
@@ -118,8 +118,12 @@ Auth:
 	suite.FileExists(filepath.Join(suite.TempDir, types.OrchestratorDirName, types.JobStoreFileName))
 	suite.FileExists(filepath.Join(suite.TempDir, types.ComputeDirName, types.ExecutionStoreFileName))
 
-	// verify old files were removed
-	suite.NoFileExists(filepath.Join(suite.TempDir, "repo.version"))
+	// verify legacy repo version was updated.
+	suite.FileExists(filepath.Join(suite.TempDir, "repo.version"))
+	v, err := suite.repo.ReadLegacyVersion()
+	suite.Require().NoError(err)
+	suite.Require().Equal(repo.Version4, v)
+
 	suite.NoFileExists(filepath.Join(suite.TempDir, "update.json"))
 
 	// verify the new files exist
@@ -190,7 +194,11 @@ func (suite *V3MigrationsTestSuite) TestV3MigrationWithMinimalRepo() {
 
 	suite.NoFileExists(filepath.Join(suite.TempDir, types.OrchestratorDirName, types.JobStoreFileName))
 	suite.NoFileExists(filepath.Join(suite.TempDir, types.ComputeDirName, types.ExecutionStoreFileName))
-	suite.NoFileExists(filepath.Join(suite.TempDir, "repo.version"))
+	// verify legacy repo version was updated.
+	suite.FileExists(filepath.Join(suite.TempDir, "repo.version"))
+	v, err := suite.repo.ReadLegacyVersion()
+	suite.Require().NoError(err)
+	suite.Require().Equal(repo.Version4, v)
 	suite.NoFileExists(filepath.Join(suite.TempDir, "update.json"))
 	suite.NoFileExists(filepath.Join(suite.TempDir, "orchestrator", "state_boltdb.db"))
 	suite.NoDirExists(filepath.Join(suite.TempDir, "orchestrator_store"))
@@ -294,8 +302,12 @@ Auth:
 	suite.FileExists(filepath.Join(suite.TempDir, types.OrchestratorDirName, types.JobStoreFileName))
 	//suite.FileExists(filepath.Join(suite.TempDir, types.ComputeDirName, types.ExecutionStoreFileName))
 
-	// verify old file were removed
-	suite.NoFileExists(filepath.Join(suite.TempDir, "repo.version"))
+	// verify legacy repo version was updated.
+	suite.FileExists(filepath.Join(suite.TempDir, "repo.version"))
+	v, err := suite.repo.ReadLegacyVersion()
+	suite.Require().NoError(err)
+	suite.Require().Equal(repo.Version4, v)
+
 	suite.NoFileExists(filepath.Join(suite.TempDir, "update.json"))
 
 	// verify the new files exists
@@ -406,8 +418,12 @@ Auth:
 	suite.NoFileExists(filepath.Join(suite.TempDir, types.OrchestratorDirName, types.JobStoreFileName))
 	suite.FileExists(filepath.Join(suite.TempDir, types.ComputeDirName, types.ExecutionStoreFileName))
 
+	// verify legacy repo version was updated.
+	suite.FileExists(filepath.Join(suite.TempDir, "repo.version"))
+	v, err := suite.repo.ReadLegacyVersion()
+	suite.Require().NoError(err)
+	suite.Require().Equal(repo.Version4, v)
 	// verify old file were removed
-	suite.NoFileExists(filepath.Join(suite.TempDir, "repo.version"))
 	suite.NoFileExists(filepath.Join(suite.TempDir, "update.json"))
 
 	// verify the new files exists