// Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: BUSL-1.1 package database import ( "fmt" "os" "strconv" "strings" "github.com/hashicorp/boundary/internal/cmd/base" "github.com/hashicorp/boundary/internal/cmd/config" "github.com/hashicorp/boundary/internal/db/schema" "github.com/hashicorp/boundary/internal/errors" "github.com/hashicorp/boundary/internal/event" boundary_plugin_assets "github.com/hashicorp/boundary/plugins/boundary" external_plugins "github.com/hashicorp/boundary/sdk/plugins" "github.com/hashicorp/go-secure-stdlib/mlock" "github.com/hashicorp/go-secure-stdlib/parseutil" "github.com/hashicorp/go-secure-stdlib/pluginutil/v2" "github.com/mitchellh/cli" "github.com/posener/complete" ) var ( _ cli.Command = (*MigrateCommand)(nil) _ cli.CommandAutocomplete = (*MigrateCommand)(nil) ) type MigrateCommand struct { *base.Command srv *base.Server SighupCh chan struct{} ReloadedCh chan struct{} SigUSR2Ch chan struct{} Config *config.Config // This will be intialized, if needed, in ParseFlagsAndConfig when // instantiating a config wrapper, if requested. It's then called as a // deferred function on the Run method. configWrapperCleanupFunc func() error selectedRepairs schema.RepairMigrations flagConfig []string flagConfigKms string flagLogLevel string flagLogFormat string flagMigrationUrl string flagRepairMigrations []string flagAllowDevMigrations bool } func (c *MigrateCommand) Synopsis() string { return "Migrate Boundary's database to the most recent schema supported by this binary." } func (c *MigrateCommand) Help() string { return base.WrapForHelpText([]string{ "Usage: boundary database migrate [options]", "", " Migrate Boundary's database:", "", " $ boundary database migrate -config=/etc/boundary/controller.hcl", "", " For a full list of examples, please see the documentation.", }) + c.Flags().Help() } func (c *MigrateCommand) Flags() *base.FlagSets { set := c.FlagSet(base.FlagSetHTTP) f := set.NewFlagSet("Command options") f.StringSliceVar(&base.StringSliceVar{ Name: "config", Target: &c.flagConfig, Completion: complete.PredictOr( complete.PredictFiles("*.hcl"), complete.PredictFiles("*.json"), ), Usage: "Path to the configuration file.", }) f.StringVar(&base.StringVar{ Name: "config-kms", Target: &c.flagConfigKms, Completion: complete.PredictOr( complete.PredictFiles("*.hcl"), complete.PredictFiles("*.json"), ), Usage: `Path to a configuration file containing a "kms" block marked for "config" purpose, to perform decryption of the main configuration file. If not set, will look for such a block in the main configuration file, which has some drawbacks; see the help output for "boundary config encrypt -h" for details.`, }) f.StringVar(&base.StringVar{ Name: "log-level", Target: &c.flagLogLevel, EnvVar: "BOUNDARY_LOG_LEVEL", Completion: complete.PredictSet("trace", "debug", "info", "warn", "err"), Usage: "Log verbosity level. Supported values (in order of more detail to less) are " + "\"trace\", \"debug\", \"info\", \"warn\", and \"err\".", }) f.StringVar(&base.StringVar{ Name: "log-format", Target: &c.flagLogFormat, Completion: complete.PredictSet("standard", "json"), Usage: `Log format. Supported values are "standard" and "json".`, }) f = set.NewFlagSet("Migration options") f.StringVar(&base.StringVar{ Name: "migration-url", Target: &c.flagMigrationUrl, Usage: `If set, overrides a migration URL set in config, and specifies the URL used to connect to the database for migration. This can allow different permissions for the user running initialization or migration vs. normal operation. This can refer to a file on disk (file://) from which a URL will be read; an env var (env://) from which the URL will be read; or a direct database URL.`, }) f.StringSliceVar(&base.StringSliceVar{ Name: "repair", Target: &c.flagRepairMigrations, Usage: `Run the repair function for the provided migration version.`, }) return set } func (c *MigrateCommand) AutocompleteArgs() complete.Predictor { return complete.PredictNothing } func (c *MigrateCommand) AutocompleteFlags() complete.Flags { return c.Flags().Completions() } func (c *MigrateCommand) Run(args []string) (retCode int) { if result := c.ParseFlagsAndConfig(args); result > 0 { return result } if c.configWrapperCleanupFunc != nil { defer func() { if err := c.configWrapperCleanupFunc(); err != nil { c.PrintCliError(fmt.Errorf("Error finalizing config kms: %w", err)) } }() } dialect := "postgres" c.srv = base.NewServer(&base.Command{UI: c.UI}) if err := c.srv.SetupLogging(c.flagLogLevel, c.flagLogFormat, c.Config.LogLevel, c.Config.LogFormat); err != nil { c.UI.Error(err.Error()) return base.CommandCliError } serverName, err := os.Hostname() if err != nil { c.UI.Error(fmt.Errorf("Unable to determine hostname: %w", err).Error()) return base.CommandCliError } serverName = fmt.Sprintf("%s/boundary-database-migrate", serverName) if err := c.srv.SetupEventing( c.Context, c.srv.Logger, c.srv.StderrLock, serverName, base.WithEventerConfig(c.Config.Eventing)); err != nil { c.UI.Error(err.Error()) return base.CommandCliError } pluginLogger, err := event.NewHclogLogger(c.Context, c.srv.Eventer) if err != nil { c.UI.Error(fmt.Sprintf("Error creating host catalog plugin logger: %v", err)) return base.CommandCliError } _, plgCleanup, err := external_plugins.CreateHostPlugin( c.Context, "azure", external_plugins.WithPluginOptions( pluginutil.WithPluginExecutionDirectory(c.Config.Plugins.ExecutionDir), pluginutil.WithPluginsFilesystem(boundary_plugin_assets.PluginPrefix, boundary_plugin_assets.FileSystem()), ), external_plugins.WithLogger(pluginLogger.Named("azure")), ) if err != nil { c.UI.Error(fmt.Errorf("Error creating dynamic host plugin: %w", err).Error()) c.UI.Warn(base.WrapAtLength( "Starting in Boundary 0.7.0, plugins are being introduced for " + "various parts of the system. These plugins are created by writing " + "out and executing plugin binaries. The migration function performed " + "a check to ensure this system is capable of running plugins and " + "encountered an error. The ability to run plugins will eventually " + "become mandatory (for instance, for KMS implementations), so we are " + "ensuring that it's feasible on a given system before migrating the " + "database to the new version of Boundary that requires this capability. " + "If your temporary directory is not writable and/or you cannot execute " + "binaries in that directory, try setting the field " + `"execution_dir" in the "plugins" block in the configuration file:`)) c.UI.Warn(` plugins { execution_dir =