Skip to content

Commit 3264ed1

Browse files
authored
Refactor profiles to decouple them from stack resources (#1140)
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.
1 parent dd5c458 commit 3264ed1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1030
-1506
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ _Context: global_
287287

288288

289289

290+
### `elastic-package profiles use`
291+
292+
_Context: global_
293+
294+
295+
290296
### `elastic-package promote`
291297

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

384390
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.
385391

386-
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.
392+
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.
387393

388394
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).
389395

cmd/profiles.go

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/elastic/elastic-package/internal/cobraext"
1818
"github.com/elastic/elastic-package/internal/configuration/locations"
19-
"github.com/elastic/elastic-package/internal/environment"
19+
"github.com/elastic/elastic-package/internal/install"
2020
"github.com/elastic/elastic-package/internal/profile"
2121
)
2222

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

29-
// profileNameEnvVar is the name of the environment variable to set the default profile
30-
var profileNameEnvVar = environment.WithElasticPackagePrefix("PROFILE")
31-
3229
func setupProfilesCommand() *cobraext.Command {
3330
profilesLongDescription := `Use this command to add, remove, and manage multiple config profiles.
3431
@@ -65,12 +62,16 @@ User profiles are not overwritten on upgrade of elastic-stack, and can be freely
6562
return errors.Wrapf(err, "error creating profile %s from profile %s", newProfileName, fromName)
6663
}
6764

68-
fmt.Printf("Created profile %s from %s.\n", newProfileName, fromName)
65+
if fromName == "" {
66+
fmt.Printf("Created profile %s.\n", newProfileName)
67+
} else {
68+
fmt.Printf("Created profile %s from %s.\n", newProfileName, fromName)
69+
}
6970

7071
return nil
7172
},
7273
}
73-
profileNewCommand.Flags().String(cobraext.ProfileFromFlagName, "default", cobraext.ProfileFromFlagDescription)
74+
profileNewCommand.Flags().String(cobraext.ProfileFromFlagName, "", cobraext.ProfileFromFlagDescription)
7475

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

108113
format, err := cmd.Flags().GetString(cobraext.ProfileFormatFlagName)
109114
if err != nil {
110-
return cobraext.FlagParsingError(err, cobraext.ProfileFromFlagName)
115+
return cobraext.FlagParsingError(err, cobraext.ProfileFormatFlagName)
111116
}
112117

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

125-
profileCommand.AddCommand(profileNewCommand, profileDeleteCommand, profileListCommand)
130+
profileUseCommand := &cobra.Command{
131+
Use: "use",
132+
Short: "Sets the profile to use when no other is specified",
133+
RunE: func(cmd *cobra.Command, args []string) error {
134+
if len(args) == 0 {
135+
return errors.New("use requires an argument")
136+
}
137+
profileName := args[0]
138+
139+
_, err := profile.LoadProfile(profileName)
140+
if err != nil {
141+
return fmt.Errorf("cannot use profile %q: %v", profileName, err)
142+
}
143+
144+
location, err := locations.NewLocationManager()
145+
if err != nil {
146+
return fmt.Errorf("error fetching profile: %w", err)
147+
}
148+
149+
config, err := install.Configuration()
150+
if err != nil {
151+
return fmt.Errorf("failed to load current configuration: %w", err)
152+
}
153+
config.SetCurrentProfile(profileName)
154+
155+
err = install.WriteConfigFile(location, config)
156+
if err != nil {
157+
return fmt.Errorf("failed to store configuration: %w", err)
158+
}
159+
return nil
160+
},
161+
}
162+
163+
profileCommand.AddCommand(
164+
profileNewCommand,
165+
profileDeleteCommand,
166+
profileListCommand,
167+
profileUseCommand,
168+
)
126169

127170
return cobraext.NewCommand(profileCommand, cobraext.ContextGlobal)
128171
}
@@ -175,15 +218,6 @@ func profileToList(profiles []profile.Metadata) [][]string {
175218
return profileList
176219
}
177220

178-
func lookupEnv() string {
179-
env := os.Getenv(profileNameEnvVar)
180-
if env == "" {
181-
return profile.DefaultProfile
182-
}
183-
return env
184-
185-
}
186-
187221
func availableProfilesAsAList() ([]string, error) {
188222
loc, err := locations.NewLocationManager()
189223
if err != nil {

0 commit comments

Comments
 (0)