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

Sync components with state during container start #1653

Merged
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
17 changes: 12 additions & 5 deletions internal/pkg/agent/application/paths/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
configFilePath string
logsPath string
downloadsPath string
componentsPath string
installPath string
unversionedHome bool
tmpCreator sync.Once
Expand All @@ -46,14 +47,23 @@ func init() {
logsPath = topPath
unversionedHome = false // only versioned by container subcommand

// these should never change
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain why? For example, mentioning that components are bundled with the agent binary and we don't download anything anymore.

It might specifically be worth mentioning that these directories should not be moved to any newly configured state path.

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
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
27 changes: 1 addition & 26 deletions internal/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down