Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f491083
Use profile in tests
mrodm May 9, 2023
74d0851
Add method to check errors in logs
mrodm May 9, 2023
7a565a6
Do no run check errors if there are no patterns
mrodm May 9, 2023
a392d52
Update README
mrodm May 9, 2023
3434fa1
Add service name into reason
mrodm May 9, 2023
889473c
Add stack parse logs
mrodm May 9, 2023
b1c8d3f
Add comments
mrodm May 9, 2023
7e44426
Use regex
mrodm May 9, 2023
9e6d45e
Comments from code review
mrodm May 10, 2023
49069bd
Move helpers to internal/cobraext
mrodm May 10, 2023
2c8e538
Add missing license header
mrodm May 10, 2023
8d20ebf
Remove debug messages
mrodm May 10, 2023
a580527
Fix errors related to helpers
mrodm May 10, 2023
64513fb
Make public the helpers
mrodm May 10, 2023
65784c7
Use logs file path instead of service name
mrodm May 10, 2023
039d672
Comment panic/runtime errors
mrodm May 10, 2023
58fb1bd
Support check logs from several services
mrodm May 11, 2023
4d48fd9
Use just errorPatterns to check services
mrodm May 11, 2023
1ffcafa
Remove debug patterns
mrodm May 11, 2023
d5f338b
Add a new regex for elastic-agent
mrodm May 11, 2023
4e8b885
Add includes and excludes regex
mrodm May 15, 2023
5e91558
Rename function
mrodm May 16, 2023
acfe286
Add test for checkAgentLogs
mrodm May 16, 2023
e483b4f
Change struct to ensure same order for logs found
mrodm May 16, 2023
1167bcd
Remove debug regexes
mrodm May 16, 2023
f2dca7a
Add test using excludes
mrodm May 16, 2023
57b2764
Merge remote-tracking branch 'upstream/main' into check_logs_from_age…
mrodm May 29, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ The command ensures that the package is aligned with the package spec and the RE
_Context: global_

Use this command to add, remove, and manage multiple config profiles.
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.

Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable.
User profiles can be configured with a "config.yml" file in the profile directory.

Expand Down
22 changes: 2 additions & 20 deletions cmd/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const tableFormat = "table"

func setupProfilesCommand() *cobraext.Command {
profilesLongDescription := `Use this command to add, remove, and manage multiple config profiles.
Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.

Individual user profiles appear in ~/.elastic-package/stack, and contain all the config files needed by the "stack" subcommand.
Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable.
User profiles can be configured with a "config.yml" file in the profile directory.`

Expand Down Expand Up @@ -247,21 +247,3 @@ func profileToList(profiles []profile.Metadata, currentProfile string) [][]strin

return profileList
}

func availableProfilesAsAList() ([]string, error) {
loc, err := locations.NewLocationManager()
if err != nil {
return []string{}, errors.Wrap(err, "error fetching profile path")
}

profileNames := []string{}
profileList, err := profile.FetchAllProfiles(loc.ProfileDir())
if err != nil {
return profileNames, errors.Wrap(err, "error fetching all profiles")
}
for _, prof := range profileList {
profileNames = append(profileNames, prof.Name)
}

return profileNames, nil
}
77 changes: 11 additions & 66 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/profile"
"github.com/elastic/elastic-package/internal/stack"
)

Expand Down Expand Up @@ -73,12 +72,12 @@ func setupStackCommand() *cobraext.Command {
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
}

profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

provider, err := getProviderFromProfile(cmd, profile, true)
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,12 +111,12 @@ func setupStackCommand() *cobraext.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Println("Take down the Elastic stack")

profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

provider, err := getProviderFromProfile(cmd, profile, false)
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
if err != nil {
return err
}
Expand All @@ -141,12 +140,12 @@ func setupStackCommand() *cobraext.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Println("Update the Elastic stack")

profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

provider, err := getProviderFromProfile(cmd, profile, false)
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,7 +183,7 @@ func setupStackCommand() *cobraext.Command {
fmt.Fprintf(cmd.OutOrStderr(), "Detected shell: %s\n", shellName)
}

profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}
Expand All @@ -209,12 +208,12 @@ func setupStackCommand() *cobraext.Command {
return cobraext.FlagParsingError(err, cobraext.StackDumpOutputFlagName)
}

profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

provider, err := getProviderFromProfile(cmd, profile, false)
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
if err != nil {
return err
}
Expand All @@ -239,12 +238,12 @@ func setupStackCommand() *cobraext.Command {
Use: "status",
Short: "Show status of the stack services",
RunE: func(cmd *cobra.Command, args []string) error {
profile, err := getProfileFlag(cmd)
profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

provider, err := getProviderFromProfile(cmd, profile, false)
provider, err := cobraext.GetStackProviderFromProfile(cmd, profile, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -321,57 +320,3 @@ func printStatus(cmd *cobra.Command, servicesStatus []stack.ServiceStatus) {
t.SetStyle(table.StyleRounded)
cmd.Println(t.Render())
}

func getProfileFlag(cmd *cobra.Command) (*profile.Profile, error) {
profileName, err := cmd.Flags().GetString(cobraext.ProfileFlagName)
if err != nil {
return nil, cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
}
if profileName == "" {
config, err := install.Configuration()
if err != nil {
return nil, fmt.Errorf("cannot read configuration: %w", err)
}
profileName = config.CurrentProfile()
}

p, err := profile.LoadProfile(profileName)
if errors.Is(err, profile.ErrNotAProfile) {
list, err := availableProfilesAsAList()
if err != nil {
return nil, errors.Wrap(err, "error listing known profiles")
}
if len(list) == 0 {
return nil, fmt.Errorf("%s is not a valid profile", profileName)
}
return nil, fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(list, ", "))
}
if err != nil {
return nil, errors.Wrap(err, "error loading profile")
}

return p, nil
}

func getProviderFromProfile(cmd *cobra.Command, profile *profile.Profile, checkFlag bool) (stack.Provider, error) {
var providerName = stack.DefaultProvider
stackConfig, err := stack.LoadConfig(profile)
if err != nil {
return nil, err
}
if stackConfig.Provider != "" {
providerName = stackConfig.Provider
}

if checkFlag {
providerFlag, err := cmd.Flags().GetString(cobraext.StackProviderFlagName)
if err != nil {
return nil, cobraext.FlagParsingError(err, cobraext.StackProviderFlagName)
}
if providerFlag != "" {
providerName = providerFlag
}
}

return stack.BuildProvider(providerName, profile)
}
8 changes: 8 additions & 0 deletions cmd/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/elasticsearch"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/signal"
"github.com/elastic/elastic-package/internal/testrunner"
Expand Down Expand Up @@ -71,6 +72,7 @@ func setupTestCommand() *cobraext.Command {
cmd.PersistentFlags().BoolP(cobraext.TestCoverageFlagName, "", false, cobraext.TestCoverageFlagDescription)
cmd.PersistentFlags().DurationP(cobraext.DeferCleanupFlagName, "", 0, cobraext.DeferCleanupFlagDescription)
cmd.PersistentFlags().String(cobraext.VariantFlagName, "", cobraext.VariantFlagDescription)
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added profile parameter into elastic-package test command


for testType, runner := range testrunner.TestRunners() {
action := testTypeCommandActionFactory(runner)
Expand Down Expand Up @@ -207,6 +209,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command

variantFlag, _ := cmd.Flags().GetString(cobraext.VariantFlagName)

profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return err
}

esClient, err := elasticsearch.NewClient()
if err != nil {
return errors.Wrap(err, "can't create Elasticsearch client")
Expand All @@ -226,6 +233,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
DeferCleanup: deferCleanup,
ServiceVariant: variantFlag,
WithCoverage: testCoverage,
Profile: profile,
})

results = append(results, r...)
Expand Down
92 changes: 92 additions & 0 deletions internal/cobraext/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 cobraext

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"

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

// GetProfileFlag returns the profile information
func GetProfileFlag(cmd *cobra.Command) (*profile.Profile, error) {
profileName, err := cmd.Flags().GetString(ProfileFlagName)
if err != nil {
return nil, FlagParsingError(err, ProfileFlagName)
}
if profileName == "" {
config, err := install.Configuration()
if err != nil {
return nil, fmt.Errorf("cannot read configuration: %w", err)
}
profileName = config.CurrentProfile()
}

p, err := profile.LoadProfile(profileName)
if errors.Is(err, profile.ErrNotAProfile) {
list, err := availableProfilesAsAList()
if err != nil {
return nil, errors.Wrap(err, "error listing known profiles")
}
if len(list) == 0 {
return nil, fmt.Errorf("%s is not a valid profile", profileName)
}
return nil, fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(list, ", "))
}
if err != nil {
return nil, errors.Wrap(err, "error loading profile")
}

return p, nil
}

func availableProfilesAsAList() ([]string, error) {
loc, err := locations.NewLocationManager()
if err != nil {
return []string{}, errors.Wrap(err, "error fetching profile path")
}

profileNames := []string{}
profileList, err := profile.FetchAllProfiles(loc.ProfileDir())
if err != nil {
return profileNames, errors.Wrap(err, "error fetching all profiles")
}
for _, prof := range profileList {
profileNames = append(profileNames, prof.Name)
}

return profileNames, nil
}

// GetStackProviderFromProfile returns the provider related to the given profile
func GetStackProviderFromProfile(cmd *cobra.Command, profile *profile.Profile, checkFlag bool) (stack.Provider, error) {
var providerName = stack.DefaultProvider
stackConfig, err := stack.LoadConfig(profile)
if err != nil {
return nil, err
}
if stackConfig.Provider != "" {
providerName = stackConfig.Provider
}

if checkFlag {
providerFlag, err := cmd.Flags().GetString(StackProviderFlagName)
if err != nil {
return nil, FlagParsingError(err, StackProviderFlagName)
}
if providerFlag != "" {
providerName = providerFlag
}
}

return stack.BuildProvider(providerName, profile)
}
5 changes: 5 additions & 0 deletions internal/stack/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ func writeLogFiles(logsPath, serviceName string, content []byte) {
logger.Errorf("can't write service logs (service: %s): %v", serviceName, err)
}
}

// DumpLogsFile returns the file path to the logs of a given service
func DumpLogsFile(options DumpOptions, serviceName string) string {
return filepath.Join(options.Output, "logs", fmt.Sprintf("%s.log", serviceName))
}
Loading