migrations

package
v1.5.0-alpha3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var V1Migration = repo.NewMigration(
	repo.Version1,
	repo.Version2,
	func(r repo.FsRepo) error {
		configExist, err := configExists(r)
		if err != nil {
			return err
		}
		if !configExist {
			return nil
		}
		v, _, err := readConfig(r)
		if err != nil {
			return err
		}

		doWrite := false
		if v.Get(legacyIPFSSwarmAddressesKey) != nil {
			v.Set(legacyIPFSSwarmAddressesKey, []string{})
			doWrite = true
		}
		if v.Get(legacyBootstrapAddressesKey) != nil {
			v.Set(legacyBootstrapAddressesKey, []string{})
			doWrite = true
		}
		if doWrite {
			return v.WriteConfig()
		}
		return nil
	})
View Source
var V2Migration = repo.NewMigration(
	repo.Version2,
	repo.Version3,
	func(r repo.FsRepo) error {
		v, fileCfg, err := readConfig(r)
		if err != nil {
			return err
		}
		repoPath, err := r.Path()
		if err != nil {
			return err
		}

		opts := []config_legacy.Option{config_legacy.WithValues(viper.AllSettings())}
		if _, err := os.Stat(filepath.Join(repoPath, config_legacy.FileName)); err != nil {

			if !errors.Is(err, os.ErrNotExist) {
				return fmt.Errorf("loading config from repo: %w", err)
			}
		} else {
			opts = append(opts, config_legacy.WithPaths(filepath.Join(repoPath, config_legacy.FileName)))
		}
		c, err := config_legacy.New(
			opts...,
		)
		if err != nil {
			return err
		}

		r.EnsureRepoPathsConfigured(c)
		resolvedCfg, err := c.Current()
		if err != nil {
			return err
		}
		libp2pNodeID, err := getLibp2pNodeID(repoPath)
		if err != nil {
			return err
		}

		doWrite := false
		var logMessage strings.Builder
		set := func(key string, value interface{}) {
			v.Set(key, value)
			logMessage.WriteString(fmt.Sprintf("\n%s:\t%v", key, value))
			doWrite = true
		}

		if fileCfg.Node.Compute.ExecutionStore.Path == "" {

			executionStore := resolvedCfg.Node.Compute.ExecutionStore

			if executionStore.Path == "" {
				executionStore.Path = filepath.Join(repoPath, "compute_store", "executions.db")
			}

			legacyStoreName := filepath.Join(repoPath, libp2pNodeID+"-compute")
			newStorePath := filepath.Dir(executionStore.Path)
			if _, err := os.Stat(legacyStoreName); err == nil {
				if err := os.Rename(legacyStoreName, newStorePath); err != nil {
					return err
				}
			} else if err = os.MkdirAll(newStorePath, util.OS_USER_RWX); err != nil {
				return err
			}
			set(legacy_types.NodeComputeExecutionStore, executionStore)
		}

		if fileCfg.Node.Requester.JobStore.Path == "" {

			jobStore := resolvedCfg.Node.Requester.JobStore

			if jobStore.Path == "" {
				jobStore.Path = filepath.Join(repoPath, "orchestrator_store", "jobs.db")
			}

			legacyStoreName := filepath.Join(repoPath, libp2pNodeID+"-requester")
			newStorePath := filepath.Dir(jobStore.Path)
			if _, err := os.Stat(legacyStoreName); err == nil {
				if err := os.Rename(legacyStoreName, newStorePath); err != nil {
					return err
				}
			} else if err = os.MkdirAll(newStorePath, util.OS_USER_RWX); err != nil {
				return err
			}
			set(legacy_types.NodeRequesterJobStore, jobStore)
		}

		if fileCfg.Node.Name == "" {
			set(legacy_types.NodeName, libp2pNodeID)
		}

		if doWrite {
			return v.WriteConfig()
		}
		return nil
	})

V2Migration updates the repo so that nodeID is no longer part of the execution and job store paths. It does the following: - Generates and persists the nodeID in the config if it is missing, which is the case for v2 repos - Adds the execution and job store paths to the config if they are missing, which is the case for v3 repos - Renames the execution and job store directories to the new name if they exist

View Source
var V3Migration = repo.NewMigration(
	repo.Version3,
	repo.Version4,
	func(r repo.FsRepo) error {
		repoPath, err := r.Path()
		if err != nil {
			return err
		}
		_, fileCfg, err := readConfig(r)
		if err != nil {
			return err
		}

		{

			if err := r.WriteVersion(repo.Version4); err != nil {
				return err
			}
			if err := r.WriteLastUpdateCheck(time.UnixMilli(0)); err != nil {
				return err
			}
			if fileCfg.User.InstallationID != "" {
				if err := r.WriteInstallationID(fileCfg.User.InstallationID); err != nil {
					return err
				}
			}

			_ = os.Remove(filepath.Join(repoPath, "update.json"))

			if err := os.Remove(filepath.Join(repoPath, repo.LegacyVersionFile)); err != nil {
				return fmt.Errorf("removing legacy repo version file: %w", err)
			}
		}

		{

			if fileCfg.User.KeyPath != "" {
				if err := copyFile(fileCfg.User.KeyPath, filepath.Join(repoPath, types.UserKeyFileName)); err != nil {
					return fmt.Errorf("copying user key file: %w", err)
				}
			}

			if fileCfg.Auth.TokensPath != "" {
				if err := copyFile(fileCfg.Auth.TokensPath, filepath.Join(repoPath, types.AuthTokensFileName)); err != nil {
					return fmt.Errorf("copying auth tokens file: %w", err)
				}
			}

			if err := migrateOrchestratorStore(repoPath, fileCfg.Node.Requester.JobStore); err != nil {
				return err
			}

			if err := migrateComputeStore(repoPath, fileCfg.Node.Compute.ExecutionStore); err != nil {
				return err
			}
		}

		{
			oldConfigFilePath := filepath.Join(repoPath, config_legacy.FileName)
			if _, err := os.Stat(oldConfigFilePath); err == nil {
				if err := r.WriteInstallationID(fileCfg.User.InstallationID); err != nil {
					return fmt.Errorf("migrating installation id: %w", err)
				}
				if err := r.WriteNodeName(fileCfg.Node.Name); err != nil {
					return fmt.Errorf("migrating node name: %w", err)
				}
				newConfigType, err := config.MigrateV1(fileCfg)
				if err != nil {
					return fmt.Errorf("migrating to new config: %w", err)
				}

				newConfigType.DataDir = repoPath
				userConfigDir, err := os.UserConfigDir()
				if err == nil {
					newConfigDir := filepath.Join(userConfigDir, "bacalhau")
					if err := os.MkdirAll(newConfigDir, util.OS_USER_RWX); err != nil {
						return err
					}
					newConfigFilePath := filepath.Join(newConfigDir, config_legacy.FileName)
					if err := os.Rename(oldConfigFilePath, newConfigFilePath); err != nil {
						return err
					}
					newConfigBytes, err := yaml.Marshal(&newConfigType)
					if err != nil {
						return err
					}
					newConfigFile, err := os.OpenFile(newConfigFilePath, os.O_RDWR|os.O_TRUNC, util.OS_USER_RWX)
					if err != nil {
						return err
					}
					defer newConfigFile.Close()
					if _, err := newConfigFile.Write(newConfigBytes); err != nil {
						return err
					}
				}
			} else if !os.IsNotExist(err) {

				return fmt.Errorf("failed to read config file %s while migrating: %w", oldConfigFilePath, err)
			}

		}
		return nil
	},
)

V3Migration updates the repo, replacing repo.version and update.json with system_metadata.yaml. It does the following: - Creates system_metadata.yaml with repo version 4. - Sets the last update check time in system_metadata.yaml to Unix time zero. - If an installationID is present in the config, its value is persisted to system_metadata.yaml. - Removes update.json if the file is present. - Removes repo.version. - Creates a new directory .bacalhau/orchestrator. - Moves contents of .bacalhau/orchestrator_store to .bacalhau/orchestrator and renames jobs.db to state_boltdb.db. - Removes .bacalhau/orchestrator_store. - Creates a new directory .bacalhau/compute. - Moves executions.db from .bacalhau/compute_store to .bacalhau/compute/state_boltdb.db. - Creates a new directory .bacalhau/compute/executions. - Moves contents of .bacalhau/execution_store to .bacalhau/compute/executions. - Removes ./bacalhau/execution_store. - If a user has configured a custom user key path, the configured value is copied to .bacalhau/user_id.pem. - If a user has configured a custom auth tokens path, the configured value is copied to .bacalhau/tokens.json.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL