From bd36958578571f350a1161052e4122cc9d94853f Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 3 Nov 2022 19:47:52 +0100 Subject: [PATCH] Sync components with state during container start (#1653) * Sync components with state during container start * path approach --- .../pkg/agent/application/paths/common.go | 17 ++++++++---- internal/pkg/agent/cmd/container.go | 27 +------------------ 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/internal/pkg/agent/application/paths/common.go b/internal/pkg/agent/application/paths/common.go index b89a197fdff..41284026091 100644 --- a/internal/pkg/agent/application/paths/common.go +++ b/internal/pkg/agent/application/paths/common.go @@ -35,6 +35,7 @@ var ( configFilePath string logsPath string downloadsPath string + componentsPath string installPath string unversionedHome bool tmpCreator sync.Once @@ -46,14 +47,23 @@ func init() { logsPath = topPath unversionedHome = false // only versioned by container subcommand + // these should never change + versionedHome := VersionedHome(topPath) + downloadsPath = filepath.Join(versionedHome, "downloads") + componentsPath = filepath.Join(versionedHome, "components") + fs := flag.CommandLine fs.StringVar(&topPath, "path.home", topPath, "Agent root path") fs.BoolVar(&unversionedHome, "path.home.unversioned", unversionedHome, "Agent root path is not versioned based on build") fs.StringVar(&configPath, "path.config", configPath, "Config path is the directory Agent looks for its config file") fs.StringVar(&configFilePath, "c", DefaultConfigName, "Configuration file, relative to path.config") fs.StringVar(&logsPath, "path.logs", logsPath, "Logs path contains Agent log output") - fs.StringVar(&downloadsPath, "path.downloads", downloadsPath, "Downloads path contains binaries Agent downloads") fs.StringVar(&installPath, "path.install", installPath, "Install path contains binaries Agent extracts") + + // enable user to download update artifacts to alternative place + // TODO: remove path.downloads support on next major (this can be configured using `agent.download.targetDirectory`) + // `path.download` serves just as init value for `agent.download.targetDirectory` + fs.StringVar(&downloadsPath, "path.downloads", downloadsPath, "Downloads path contains binaries Agent downloads") } // Top returns the top directory for Elastic Agent, all the versioned @@ -146,7 +156,7 @@ func Run() string { // Components returns the component directory for Agent func Components() string { - return filepath.Join(Home(), "components") + return componentsPath } // Logs returns the log directory for Agent @@ -166,9 +176,6 @@ func VersionedHome(base string) string { // Downloads returns the downloads directory for Agent func Downloads() string { - if downloadsPath == "" { - return filepath.Join(Home(), "downloads") - } return downloadsPath } diff --git a/internal/pkg/agent/cmd/container.go b/internal/pkg/agent/cmd/container.go index 06fd9bdf962..91c755bedfc 100644 --- a/internal/pkg/agent/cmd/container.go +++ b/internal/pkg/agent/cmd/container.go @@ -782,11 +782,7 @@ func setPaths(statePath, configPath, logsPath string, writePaths bool) error { return err } } - // sync the downloads to the data directory - destDownloads := filepath.Join(statePath, "data", "downloads") - if err := syncDir(paths.Downloads(), destDownloads); err != nil { - return fmt.Errorf("syncing download directory to STATE_PATH(%s) failed: %w", statePath, err) - } + originalInstall := paths.Install() originalTop := paths.Top() paths.SetTop(topPath) @@ -866,27 +862,6 @@ func tryContainerLoadPaths() error { return setPaths(paths.StatePath, paths.ConfigPath, paths.LogsPath, false) } -func syncDir(src string, dest string) error { - return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { - if err != nil { - if os.IsNotExist(err) { - // source dir exists only if there's agent artifact - return nil - } - return err - } - relativePath := strings.TrimPrefix(path, src) - if info.IsDir() { - err = os.MkdirAll(filepath.Join(dest, relativePath), info.Mode()) - if err != nil { - return err - } - return nil - } - return copyFile(filepath.Join(dest, relativePath), path, info.Mode()) - }) -} - func copyFile(destPath string, srcPath string, mode os.FileMode) error { // if mode is unset; set to the same as the source file if mode == 0 {