Skip to content

Commit

Permalink
Sync components with state during container start (#1653)
Browse files Browse the repository at this point in the history
* Sync components with state during container start

* path approach
  • Loading branch information
michalpristas authored Nov 3, 2022
1 parent fc3eba3 commit bd36958
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
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
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

0 comments on commit bd36958

Please sign in to comment.