Skip to content

Commit

Permalink
Refactor profiles to decouple them from stack resources (#1140)
Browse files Browse the repository at this point in the history
Changes included here:
* New `elastic-package profile use` command to select profile to use.
* Configuration files are not rewritten on upgrade.
* Stack and test runner files are managed by their own providers.
* Stack and test runner files are only written when the services are
  started, and only if they are different.
* Stack files are templates now. Same files are used for multiple
  versions. Runtime configuration can be passed now as data to
  templates instead of requiring environment variables.
* Support for multiple stack providers.
  • Loading branch information
jsoriano authored Apr 4, 2023
1 parent dd5c458 commit 3264ed1
Show file tree
Hide file tree
Showing 53 changed files with 1,030 additions and 1,506 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ _Context: global_



### `elastic-package profiles use`

_Context: global_



### `elastic-package promote`

_Context: global_
Expand Down Expand Up @@ -383,7 +389,7 @@ By default the latest released version of the stack is spun up but it is possibl

Be aware that a common issue while trying to boot up the stack is that your Docker environments settings are too low in terms of memory threshold.

To ęxpose local packages in the Package Registry, build them first and boot up the stack from inside of the Git repository containing the package (e.g. elastic/integrations). They will be copied to the development stack (~/.elastic-package/stack/development) and used to build a custom Docker image of the Package Registry.
To expose local packages in the Package Registry, build them first and boot up the stack from inside of the Git repository containing the package (e.g. elastic/integrations). They will be copied to the development stack (~/.elastic-package/stack/development) and used to build a custom Docker image of the Package Registry.

For details on how to connect the service with the Elastic stack, see the [service command](https://github.com/elastic/elastic-package/blob/main/README.md#elastic-package-service).

Expand Down
68 changes: 51 additions & 17 deletions cmd/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/environment"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/profile"
)

Expand All @@ -26,9 +26,6 @@ const jsonFormat = "json"
// tableFormat is the format for table output
const tableFormat = "table"

// profileNameEnvVar is the name of the environment variable to set the default profile
var profileNameEnvVar = environment.WithElasticPackagePrefix("PROFILE")

func setupProfilesCommand() *cobraext.Command {
profilesLongDescription := `Use this command to add, remove, and manage multiple config profiles.
Expand Down Expand Up @@ -65,12 +62,16 @@ User profiles are not overwritten on upgrade of elastic-stack, and can be freely
return errors.Wrapf(err, "error creating profile %s from profile %s", newProfileName, fromName)
}

fmt.Printf("Created profile %s from %s.\n", newProfileName, fromName)
if fromName == "" {
fmt.Printf("Created profile %s.\n", newProfileName)
} else {
fmt.Printf("Created profile %s from %s.\n", newProfileName, fromName)
}

return nil
},
}
profileNewCommand.Flags().String(cobraext.ProfileFromFlagName, "default", cobraext.ProfileFromFlagDescription)
profileNewCommand.Flags().String(cobraext.ProfileFromFlagName, "", cobraext.ProfileFromFlagDescription)

profileDeleteCommand := &cobra.Command{
Use: "delete",
Expand Down Expand Up @@ -104,10 +105,14 @@ User profiles are not overwritten on upgrade of elastic-stack, and can be freely
if err != nil {
return errors.Wrap(err, "error listing all profiles")
}
if len(profileList) == 0 {
fmt.Println("There are no profiles yet.")
return nil
}

format, err := cmd.Flags().GetString(cobraext.ProfileFormatFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.ProfileFromFlagName)
return cobraext.FlagParsingError(err, cobraext.ProfileFormatFlagName)
}

switch format {
Expand All @@ -122,7 +127,45 @@ User profiles are not overwritten on upgrade of elastic-stack, and can be freely
}
profileListCommand.Flags().String(cobraext.ProfileFormatFlagName, tableFormat, cobraext.ProfileFormatFlagDescription)

profileCommand.AddCommand(profileNewCommand, profileDeleteCommand, profileListCommand)
profileUseCommand := &cobra.Command{
Use: "use",
Short: "Sets the profile to use when no other is specified",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("use requires an argument")
}
profileName := args[0]

_, err := profile.LoadProfile(profileName)
if err != nil {
return fmt.Errorf("cannot use profile %q: %v", profileName, err)
}

location, err := locations.NewLocationManager()
if err != nil {
return fmt.Errorf("error fetching profile: %w", err)
}

config, err := install.Configuration()
if err != nil {
return fmt.Errorf("failed to load current configuration: %w", err)
}
config.SetCurrentProfile(profileName)

err = install.WriteConfigFile(location, config)
if err != nil {
return fmt.Errorf("failed to store configuration: %w", err)
}
return nil
},
}

profileCommand.AddCommand(
profileNewCommand,
profileDeleteCommand,
profileListCommand,
profileUseCommand,
)

return cobraext.NewCommand(profileCommand, cobraext.ContextGlobal)
}
Expand Down Expand Up @@ -175,15 +218,6 @@ func profileToList(profiles []profile.Metadata) [][]string {
return profileList
}

func lookupEnv() string {
env := os.Getenv(profileNameEnvVar)
if env == "" {
return profile.DefaultProfile
}
return env

}

func availableProfilesAsAList() ([]string, error) {
loc, err := locations.NewLocationManager()
if err != nil {
Expand Down
Loading

0 comments on commit 3264ed1

Please sign in to comment.