Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func setupStackCommand() *cobraext.Command {
return cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
}

usrProfile, err := profile.LoadProfile(profileName)
userProfile, err := profile.LoadProfile(profileName)
if errors.Is(err, profile.ErrNotAProfile) {
pList, err := availableProfilesAsAList()
if err != nil {
Expand All @@ -88,14 +88,21 @@ func setupStackCommand() *cobraext.Command {
if err != nil {
return errors.Wrap(err, "error loading profile")
}
cmd.Printf("Using profile %s.\n", usrProfile.ProfilePath)

// Print information before starting the stack, for cases where
// this is executed in the foreground, without daemon mode.
cmd.Printf("Using profile %s.\n", userProfile.ProfilePath)
cmd.Println(`Remember to load stack environment variables using 'eval "$(elastic-package stack shellinit)"'.`)
err = printInitConfig(cmd, userProfile)
if err != nil {
return err
}

err = stack.BootUp(stack.Options{
DaemonMode: daemonMode,
StackVersion: stackVersion,
Services: services,
Profile: usrProfile,
Profile: userProfile,
})
if err != nil {
return errors.Wrap(err, "booting up the stack failed")
Expand All @@ -121,7 +128,7 @@ func setupStackCommand() *cobraext.Command {
return cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
}

usrProfile, err := profile.LoadProfile(profileName)
userProfile, err := profile.LoadProfile(profileName)
if errors.Is(err, profile.ErrNotAProfile) {
pList, err := availableProfilesAsAList()
if err != nil {
Expand All @@ -135,7 +142,7 @@ func setupStackCommand() *cobraext.Command {
}

err = stack.TearDown(stack.Options{
Profile: usrProfile,
Profile: userProfile,
})
if err != nil {
return errors.Wrap(err, "tearing down the stack failed")
Expand Down Expand Up @@ -281,3 +288,15 @@ func validateServicesFlag(services []string) error {
}
return nil
}

func printInitConfig(cmd *cobra.Command, profile *profile.Profile) error {
initConfig, err := stack.StackInitConfig(profile)
if err != nil {
return nil
}
cmd.Printf("Elasticsearch host: %s\n", initConfig.ElasticsearchHostPort)
cmd.Printf("Kibana host: %s\n", initConfig.KibanaHostPort)
cmd.Printf("Username: %s\n", initConfig.ElasticsearchUsername)
cmd.Printf("Password: %s\n", initConfig.ElasticsearchPassword)
return nil
}
1 change: 1 addition & 0 deletions internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func BootUp(options Options) error {
if err != nil {
return errors.Wrap(err, "running docker-compose failed")
}

return nil
}

Expand Down
77 changes: 77 additions & 0 deletions internal/stack/initconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stack

import (
"fmt"
"os"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/profile"
)

type InitConfig struct {
ElasticsearchHostPort string
ElasticsearchUsername string
ElasticsearchPassword string
KibanaHostPort string
}

func StackInitConfig(elasticStackProfile *profile.Profile) (*InitConfig, error) {
// Read Elasticsearch username and password from Kibana configuration file.
// FIXME read credentials from correct Kibana config file, not default
body, err := os.ReadFile(elasticStackProfile.FetchPath(profile.KibanaConfigDefaultFile))
if err != nil {
return nil, errors.Wrap(err, "error reading Kibana config file")
}

var kibanaCfg struct {
ElasticsearchUsername string `yaml:"elasticsearch.username"`
ElasticsearchPassword string `yaml:"elasticsearch.password"`
}
err = yaml.Unmarshal(body, &kibanaCfg)
if err != nil {
return nil, errors.Wrap(err, "unmarshalling Kibana configuration failed")
}

// Read Elasticsearch and Kibana hostnames from Elastic Stack Docker Compose configuration file.
p, err := compose.NewProject(DockerComposeProjectName, elasticStackProfile.FetchPath(profile.SnapshotFile))
if err != nil {
return nil, errors.Wrap(err, "could not create docker compose project")
}

appConfig, err := install.Configuration()
if err != nil {
return nil, errors.Wrap(err, "can't read application configuration")
}

serviceComposeConfig, err := p.Config(compose.CommandOptions{
Env: newEnvBuilder().
withEnvs(appConfig.StackImageRefs(install.DefaultStackVersion).AsEnv()).
withEnvs(elasticStackProfile.ComposeEnvVars()).
withEnv(stackVariantAsEnv(install.DefaultStackVersion)).
build(),
})
if err != nil {
return nil, errors.Wrap(err, "could not get Docker Compose configuration for service")
}

kib := serviceComposeConfig.Services["kibana"]
kibHostPort := fmt.Sprintf("http://%s:%d", kib.Ports[0].ExternalIP, kib.Ports[0].ExternalPort)

es := serviceComposeConfig.Services["elasticsearch"]
esHostPort := fmt.Sprintf("http://%s:%d", es.Ports[0].ExternalIP, es.Ports[0].ExternalPort)

return &InitConfig{
ElasticsearchHostPort: esHostPort,
ElasticsearchUsername: kibanaCfg.ElasticsearchUsername,
ElasticsearchPassword: kibanaCfg.ElasticsearchPassword,
KibanaHostPort: kibHostPort,
}, nil
}
59 changes: 6 additions & 53 deletions internal/stack/shellinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ package stack

import (
"fmt"
"os"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/profile"
)

Expand All @@ -32,57 +26,16 @@ var (
var shellInitFormat = "export " + ElasticsearchHostEnv + "=%s\nexport " + ElasticsearchUsernameEnv + "=%s\nexport " +
ElasticsearchPasswordEnv + "=%s\nexport " + KibanaHostEnv + "=%s"

type kibanaConfiguration struct {
ElasticsearchUsername string `yaml:"elasticsearch.username"`
ElasticsearchPassword string `yaml:"elasticsearch.password"`
}

// ShellInit method exposes environment variables that can be used for testing purposes.
func ShellInit(elasticStackProfile *profile.Profile) (string, error) {
// Read Elasticsearch username and password from Kibana configuration file.
// FIXME read credentials from correct Kibana config file, not default
body, err := os.ReadFile(elasticStackProfile.FetchPath(profile.KibanaConfigDefaultFile))
if err != nil {
return "", errors.Wrap(err, "error reading Kibana config file")
}

var kibanaCfg kibanaConfiguration
err = yaml.Unmarshal(body, &kibanaCfg)
if err != nil {
return "", errors.Wrap(err, "unmarshalling Kibana configuration failed")
}

// Read Elasticsearch and Kibana hostnames from Elastic Stack Docker Compose configuration file.
p, err := compose.NewProject(DockerComposeProjectName, elasticStackProfile.FetchPath(profile.SnapshotFile))
config, err := StackInitConfig(elasticStackProfile)
if err != nil {
return "", errors.Wrap(err, "could not create docker compose project")
return "", nil
}

appConfig, err := install.Configuration()
if err != nil {
return "", errors.Wrap(err, "can't read application configuration")
}

serviceComposeConfig, err := p.Config(compose.CommandOptions{
Env: newEnvBuilder().
withEnvs(appConfig.StackImageRefs(install.DefaultStackVersion).AsEnv()).
withEnvs(elasticStackProfile.ComposeEnvVars()).
withEnv(stackVariantAsEnv(install.DefaultStackVersion)).
build(),
})
if err != nil {
return "", errors.Wrap(err, "could not get Docker Compose configuration for service")
}

kib := serviceComposeConfig.Services["kibana"]
kibHostPort := fmt.Sprintf("http://%s:%d", kib.Ports[0].ExternalIP, kib.Ports[0].ExternalPort)

es := serviceComposeConfig.Services["elasticsearch"]
esHostPort := fmt.Sprintf("http://%s:%d", es.Ports[0].ExternalIP, es.Ports[0].ExternalPort)

return fmt.Sprintf(shellInitFormat,
esHostPort,
kibanaCfg.ElasticsearchUsername,
kibanaCfg.ElasticsearchPassword,
kibHostPort), nil
config.ElasticsearchHostPort,
config.ElasticsearchUsername,
config.ElasticsearchPassword,
config.KibanaHostPort), nil
}