Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cleanup to remove service logs from independent Elastic Agents #2033

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ _Context: package_

Use this command to clean resources used for building the package.

The command will remove built package files (in build/), files needed for managing the development stack (in ~/.elastic-package/stack/development) and stack service logs (in ~/.elastic-package/tmp/service_logs).
The command will remove built package files (in build/), files needed for managing the development stack (in ~/.elastic-package/stack/development) and stack service logs (in ~/.elastic-package/tmp/service_logs and ~/.elastic-package/profiles/<profile>/service_logs/).

### `elastic-package create`

Expand Down
17 changes: 16 additions & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (

"github.com/elastic/elastic-package/internal/cleanup"
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/install"
)

const cleanLongDescription = `Use this command to clean resources used for building the package.

The command will remove built package files (in build/), files needed for managing the development stack (in ~/.elastic-package/stack/development) and stack service logs (in ~/.elastic-package/tmp/service_logs).`
The command will remove built package files (in build/), files needed for managing the development stack (in ~/.elastic-package/stack/development) and stack service logs (in ~/.elastic-package/tmp/service_logs and ~/.elastic-package/profiles/<profile>/service_logs/).`

func setupCleanCommand() *cobraext.Command {
cmd := &cobra.Command{
Expand All @@ -25,13 +26,19 @@ func setupCleanCommand() *cobraext.Command {
Args: cobra.NoArgs,
RunE: cleanCommandAction,
}
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))

return cobraext.NewCommand(cmd, cobraext.ContextPackage)
}

func cleanCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Clean used resources")

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

target, err := cleanup.Build()
if err != nil {
return fmt.Errorf("can't clean build resources: %w", err)
Expand All @@ -57,6 +64,14 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("Temporary service logs removed: %s\n", target)
}

target, err = cleanup.ServiceLogsIndependentAgents(profile)
if err != nil {
return fmt.Errorf("can't clean temporary service logs: %w", err)
}
if target != "" {
cmd.Printf("Temporary service logs (independent Elastic Agents) removed: %s\n", target)
}

cmd.Println("Done")
return nil
}
3 changes: 1 addition & 2 deletions cmd/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func setupTestCommand() *cobraext.Command {
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))

// Just used in pipeline and system tests
// Keep it here for backwards compatbility
// Keep it here for backwards compatibility
cmd.PersistentFlags().DurationP(cobraext.DeferCleanupFlagName, "", 0, cobraext.DeferCleanupFlagDescription)

assetCmd := getTestRunnerAssetCommand()
Expand Down Expand Up @@ -743,7 +743,6 @@ func processResults(results []testrunner.TestResult, testType testrunner.TestTyp
}
}
return nil

}

func validateDataStreamsFlag(packageRootPath string, dataStreams []string) error {
Expand Down
6 changes: 5 additions & 1 deletion internal/agentdeployer/directories.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ const (
deployerDir = "deployer"
)

func ServiceLogsDir(profile *profile.Profile) string {
return filepath.Join(profile.ProfilePath, serviceLogsDir)
}

func CreateServiceLogsDir(profile *profile.Profile, name string) (string, error) {
dirPath := filepath.Join(profile.ProfilePath, serviceLogsDir, name)
dirPath := filepath.Join(ServiceLogsDir(profile), name)
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return "", fmt.Errorf("mkdir failed for service logs (path: %s): %w", dirPath, err)
Expand Down
33 changes: 33 additions & 0 deletions internal/cleanup/service_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ package cleanup

import (
"fmt"
"os"
"path/filepath"

"github.com/elastic/elastic-package/internal/agentdeployer"
"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/profile"
)

// ServiceLogs function removes service logs from temporary directory in the `~/.elastic-package`.
Expand All @@ -26,5 +31,33 @@ func ServiceLogs() (string, error) {
if err != nil {
return "", fmt.Errorf("can't remove content (path: %s): %w", locationManager.ServiceLogDir(), err)
}

return locationManager.ServiceLogDir(), nil
}

// ServiceLogs function removes service logs from temporary directory in the `~/.elastic-package`.
mrodm marked this conversation as resolved.
Show resolved Hide resolved
func ServiceLogsIndependentAgents(profile *profile.Profile) (string, error) {
logger.Debug("Clean all service logs from independent Elastic Agents")

packageRootPath, err := packages.MustFindPackageRoot()
if err != nil {
return "", fmt.Errorf("locating package root failed: %w", err)
}

packageFolder := filepath.Base(packageRootPath)
serviceLogsDir := agentdeployer.ServiceLogsDir(profile)

folderGlob := fmt.Sprintf("%s/agent-%s-*", serviceLogsDir, packageFolder)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this package removes the built packages of the given package (folder), I tried to remove just the service logs folders for the given package.

Copy link
Member

Choose a reason for hiding this comment

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

Do we use the name of the directory for these logs directories or the name of the package in the manifest? (There are packages with names different to their directories).
Maybe we could have a function to obtain this directory path, so we use the same when creating and when deleting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the code, it was the folder package name what it was used.
But it is true, that this was not clear and easy to know from the code.

I tried to refactor the code a little in 031c2cc to help about that.

folders, err := filepath.Glob(folderGlob)
if err != nil {
return "", fmt.Errorf("pattern malformed: %w", err)
}
for _, f := range folders {
logger.Debugf("Remove folder (path: %s)", f)
if err := os.RemoveAll(f); err != nil {
return "", fmt.Errorf("can't remove folder (path: %s): %w", f, err)
}
}

return folderGlob, nil
}
2 changes: 1 addition & 1 deletion internal/stack/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func NewKibanaClientFromProfile(profile *profile.Profile, customOptions ...kiban

kibanaHost, found := os.LookupEnv(KibanaHostEnv)
if !found {
// Using backgound context on initial call to avoid context cancellation.
// Using background context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
if err != nil {
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
Expand Down