Skip to content

Commit 006a64d

Browse files
authored
Load stack configuration from profile (#1428)
Commands that require an stack to connect with will try to connect with the stack of the current profile by default. This behaviour can be overridden by setting the usual environment variables provided by `elastic-package shellinit`. With this change `elastic-package shellinit` is not necessary when working with stacks managed by elastic-package. shellinit is still useful to export the configuration for other commands or scripts. The environment variables can also be used to work with stacks not managed by elastic-package.
1 parent f6a0186 commit 006a64d

22 files changed

+222
-68
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,20 @@ Dump stack data for debug purposes.
437437

438438
_Context: global_
439439

440-
Export environment variables.
440+
Use this command to export to the current shell the configuration of the stack managed by elastic-package.
441+
442+
The output of this command is intended to be evaluated by the current shell. For example in bash: 'eval $(elastic-package stack shellinit)'.
443+
444+
Relevant environment variables are:
445+
446+
- ELASTIC_PACKAGE_ELASTICSEARCH_HOST
447+
- ELASTIC_PACKAGE_ELASTICSEARCH_USERNAME
448+
- ELASTIC_PACKAGE_ELASTICSEARCH_PASSWORD
449+
- ELASTIC_PACKAGE_KIBANA_HOST
450+
- ELASTIC_PACKAGE_CA_CERT
451+
452+
You can also provide these environment variables manually. In that case elastic-package commands will use these settings.
453+
441454

442455
### `elastic-package stack status`
443456

cmd/benchmark.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ func pipelineCommandAction(cmd *cobra.Command, args []string) error {
165165
return errors.New("no pipeline benchmarks found")
166166
}
167167

168-
esClient, err := stack.NewElasticsearchClient()
168+
profile, err := cobraext.GetProfileFlag(cmd)
169+
if err != nil {
170+
return err
171+
}
172+
173+
esClient, err := stack.NewElasticsearchClientFromProfile(profile)
169174
if err != nil {
170175
return fmt.Errorf("can't create Elasticsearch client: %w", err)
171176
}
@@ -269,7 +274,7 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
269274

270275
signal.Enable()
271276

272-
esClient, err := stack.NewElasticsearchClient()
277+
esClient, err := stack.NewElasticsearchClientFromProfile(profile)
273278
if err != nil {
274279
return fmt.Errorf("can't create Elasticsearch client: %w", err)
275280
}
@@ -278,7 +283,7 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
278283
return err
279284
}
280285

281-
kc, err := stack.NewKibanaClient()
286+
kc, err := stack.NewKibanaClientFromProfile(profile)
282287
if err != nil {
283288
return fmt.Errorf("can't create Kibana client: %w", err)
284289
}

cmd/dump.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/elastic/elastic-package/internal/cobraext"
1313
"github.com/elastic/elastic-package/internal/dump"
1414
"github.com/elastic/elastic-package/internal/elasticsearch"
15+
"github.com/elastic/elastic-package/internal/install"
1516
"github.com/elastic/elastic-package/internal/kibana"
1617
"github.com/elastic/elastic-package/internal/stack"
1718
)
@@ -58,6 +59,7 @@ func setupDumpCommand() *cobraext.Command {
5859
Long: dumpLongDescription,
5960
}
6061
cmd.PersistentFlags().StringP(cobraext.DumpOutputFlagName, "o", "package-dump", cobraext.DumpOutputFlagDescription)
62+
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
6163

6264
cmd.AddCommand(dumpInstalledObjectsCmd)
6365
cmd.AddCommand(dumpAgentPoliciesCmd)
@@ -82,7 +84,13 @@ func dumpInstalledObjectsCmdAction(cmd *cobra.Command, args []string) error {
8284
if tlsSkipVerify {
8385
clientOptions = append(clientOptions, elasticsearch.OptionWithSkipTLSVerify())
8486
}
85-
client, err := stack.NewElasticsearchClient(clientOptions...)
87+
88+
profile, err := cobraext.GetProfileFlag(cmd)
89+
if err != nil {
90+
return err
91+
}
92+
93+
client, err := stack.NewElasticsearchClientFromProfile(profile, clientOptions...)
8694
if err != nil {
8795
return fmt.Errorf("failed to initialize Elasticsearch client: %w", err)
8896
}
@@ -122,7 +130,13 @@ func dumpAgentPoliciesCmdAction(cmd *cobra.Command, args []string) error {
122130
if tlsSkipVerify {
123131
clientOptions = append(clientOptions, kibana.TLSSkipVerify())
124132
}
125-
kibanaClient, err := stack.NewKibanaClient(clientOptions...)
133+
134+
profile, err := cobraext.GetProfileFlag(cmd)
135+
if err != nil {
136+
return err
137+
}
138+
139+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile, clientOptions...)
126140
if err != nil {
127141
return fmt.Errorf("failed to initialize Kibana client: %w", err)
128142
}

cmd/export.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/elastic/elastic-package/internal/cobraext"
1515
"github.com/elastic/elastic-package/internal/common"
1616
"github.com/elastic/elastic-package/internal/export"
17+
"github.com/elastic/elastic-package/internal/install"
1718
"github.com/elastic/elastic-package/internal/kibana"
1819
"github.com/elastic/elastic-package/internal/stack"
1920
)
@@ -42,6 +43,7 @@ func setupExportCommand() *cobraext.Command {
4243
Long: exportLongDescription,
4344
}
4445
cmd.AddCommand(exportDashboardCmd)
46+
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
4547

4648
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
4749
}
@@ -67,7 +69,12 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {
6769
return cobraext.FlagParsingError(err, cobraext.AllowSnapshotFlagName)
6870
}
6971

70-
kibanaClient, err := stack.NewKibanaClient(opts...)
72+
profile, err := cobraext.GetProfileFlag(cmd)
73+
if err != nil {
74+
return err
75+
}
76+
77+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile, opts...)
7178
if err != nil {
7279
return fmt.Errorf("can't create Kibana client: %w", err)
7380
}

cmd/install.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/cobra"
1212

1313
"github.com/elastic/elastic-package/internal/cobraext"
14+
"github.com/elastic/elastic-package/internal/install"
1415
"github.com/elastic/elastic-package/internal/packages"
1516
"github.com/elastic/elastic-package/internal/packages/installer"
1617
"github.com/elastic/elastic-package/internal/stack"
@@ -32,6 +33,7 @@ func setupInstallCommand() *cobraext.Command {
3233
cmd.Flags().StringP(cobraext.PackageRootFlagName, cobraext.PackageRootFlagShorthand, "", cobraext.PackageRootFlagDescription)
3334
cmd.Flags().StringP(cobraext.ZipPackageFilePathFlagName, cobraext.ZipPackageFilePathFlagShorthand, "", cobraext.ZipPackageFilePathFlagDescription)
3435
cmd.Flags().Bool(cobraext.BuildSkipValidationFlagName, false, cobraext.BuildSkipValidationFlagDescription)
36+
cmd.Flags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
3537

3638
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
3739
}
@@ -50,7 +52,12 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
5052
return cobraext.FlagParsingError(err, cobraext.BuildSkipValidationFlagName)
5153
}
5254

53-
kibanaClient, err := stack.NewKibanaClient()
55+
profile, err := cobraext.GetProfileFlag(cmd)
56+
if err != nil {
57+
return err
58+
}
59+
60+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile)
5461
if err != nil {
5562
return fmt.Errorf("could not create kibana client: %w", err)
5663
}

cmd/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func upCommandAction(cmd *cobra.Command, args []string) error {
6868
return err
6969
}
7070

71-
kibanaClient, err := stack.NewKibanaClient()
71+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile)
7272
if err != nil {
7373
return fmt.Errorf("cannot create Kibana client: %w", err)
7474
}

cmd/stack.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ For details on how to connect the service with the Elastic stack, see the [servi
4848
4949
You can customize your stack using profile settings, see [Elastic Package profiles](https://github.com/elastic/elastic-package/blob/main/README.md#elastic-package-profiles-1) section. These settings can be also overriden with the --parameter flag. Settings configured this way are not persisted.`
5050

51+
const stackShellinitLongDescription = `Use this command to export to the current shell the configuration of the stack managed by elastic-package.
52+
53+
The output of this command is intended to be evaluated by the current shell. For example in bash: 'eval $(elastic-package stack shellinit)'.
54+
55+
Relevant environment variables are:
56+
57+
- ELASTIC_PACKAGE_ELASTICSEARCH_HOST
58+
- ELASTIC_PACKAGE_ELASTICSEARCH_USERNAME
59+
- ELASTIC_PACKAGE_ELASTICSEARCH_PASSWORD
60+
- ELASTIC_PACKAGE_KIBANA_HOST
61+
- ELASTIC_PACKAGE_CA_CERT
62+
63+
You can also provide these environment variables manually. In that case elastic-package commands will use these settings.
64+
`
65+
5166
func setupStackCommand() *cobraext.Command {
5267
upCommand := &cobra.Command{
5368
Use: "up",
@@ -99,7 +114,6 @@ func setupStackCommand() *cobraext.Command {
99114
profile.RuntimeOverrides(userParameters)
100115

101116
cmd.Printf("Using profile %s.\n", profile.ProfilePath)
102-
cmd.Println(`Remember to load stack environment variables using 'eval "$(elastic-package stack shellinit)"'.`)
103117
err = provider.BootUp(stack.Options{
104118
DaemonMode: daemonMode,
105119
StackVersion: stackVersion,
@@ -192,6 +206,7 @@ func setupStackCommand() *cobraext.Command {
192206
shellInitCommand := &cobra.Command{
193207
Use: "shellinit",
194208
Short: "Export environment variables",
209+
Long: stackShellinitLongDescription,
195210
Args: cobra.NoArgs,
196211
RunE: func(cmd *cobra.Command, args []string) error {
197212
shellName, err := cmd.Flags().GetString(cobraext.ShellInitShellFlagName)

cmd/testrunner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
215215
return err
216216
}
217217

218-
esClient, err := stack.NewElasticsearchClient()
218+
esClient, err := stack.NewElasticsearchClientFromProfile(profile)
219219
if err != nil {
220220
return fmt.Errorf("can't create Elasticsearch client: %w", err)
221221
}
@@ -224,7 +224,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
224224
return err
225225
}
226226

227-
kibanaClient, err := stack.NewKibanaClient()
227+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile)
228228
if err != nil {
229229
return fmt.Errorf("can't create Kibana client: %w", err)
230230
}

cmd/uninstall.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/cobra"
1212

1313
"github.com/elastic/elastic-package/internal/cobraext"
14+
"github.com/elastic/elastic-package/internal/install"
1415
"github.com/elastic/elastic-package/internal/packages"
1516
"github.com/elastic/elastic-package/internal/packages/installer"
1617
"github.com/elastic/elastic-package/internal/stack"
@@ -28,6 +29,7 @@ func setupUninstallCommand() *cobraext.Command {
2829
Args: cobra.NoArgs,
2930
RunE: uninstallCommandAction,
3031
}
32+
cmd.Flags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
3133

3234
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
3335
}
@@ -41,7 +43,12 @@ func uninstallCommandAction(cmd *cobra.Command, args []string) error {
4143
return fmt.Errorf("locating package root failed: %w", err)
4244
}
4345

44-
kibanaClient, err := stack.NewKibanaClient()
46+
profile, err := cobraext.GetProfileFlag(cmd)
47+
if err != nil {
48+
return err
49+
}
50+
51+
kibanaClient, err := stack.NewKibanaClientFromProfile(profile)
4552
if err != nil {
4653
return fmt.Errorf("could not create kibana client: %w", err)
4754
}

docs/howto/asset_testing.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ elastic-package stack up -d
3636

3737
For a complete listing of options available for this command, run `elastic-package stack up -h` or `elastic-package help stack up`.
3838

39-
Next, you must set environment variables needed for further `elastic-package` commands.
40-
41-
```
42-
$(elastic-package stack shellinit)
43-
```
44-
4539
Next, you must invoke the asset loading test runner. This corresponds to steps 3 through 5 as described in the [_Conceptual process_](#Conceptual-process) section.
4640

4741
Navigate to the package's root folder (or any sub-folder under it) and run the following command.

0 commit comments

Comments
 (0)