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

[Ingest Manager] New agent structure (symlinks) #20400

Merged
merged 34 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b863e2d
init
michalpristas Jul 28, 2020
b964a0c
docker
michalpristas Jul 28, 2020
3e237e4
binary perms
michalpristas Jul 28, 2020
6ce4edb
rpm
michalpristas Jul 28, 2020
9f195de
continue on err
michalpristas Jul 28, 2020
1ee98fd
beat home
michalpristas Jul 28, 2020
03a358d
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Jul 28, 2020
4f59a93
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Jul 29, 2020
bc3d76e
works
michalpristas Jul 29, 2020
80f37b4
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Jul 29, 2020
709ecf4
mod
michalpristas Jul 29, 2020
beda9f5
base name
michalpristas Jul 30, 2020
0a72f3d
windows
michalpristas Jul 30, 2020
0833703
removed debug entries
michalpristas Jul 30, 2020
a54ed0a
windows split
michalpristas Jul 30, 2020
ff43bfc
cancel
michalpristas Jul 30, 2020
6678e5a
symlink
michalpristas Jul 31, 2020
2eaf813
using symlinks
michalpristas Jul 31, 2020
c6c5bcf
windows works
michalpristas Aug 3, 2020
0af2d9d
darwin
michalpristas Aug 3, 2020
9445ade
removed not needed
michalpristas Aug 3, 2020
8d194a1
mod
michalpristas Aug 3, 2020
d78e245
mod
michalpristas Aug 3, 2020
73c0e4d
fixed
michalpristas Aug 26, 2020
84295dc
mac packaging has symlinks"
michalpristas Aug 27, 2020
9792aa2
reduced heartbeat
michalpristas Aug 27, 2020
e948418
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Aug 27, 2020
f0ccd30
symlinks for darwin and linux, windows does the dance
michalpristas Aug 27, 2020
759d923
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Sep 1, 2020
0708e52
fmt
michalpristas Sep 1, 2020
90ee0be
Update dev-tools/mage/dmgbuilder.go
michalpristas Sep 3, 2020
88f89c2
comment
michalpristas Sep 3, 2020
11f3d4b
Merge branch 'agent-new-structure-sym' of github.com:michalpristas/be…
michalpristas Sep 3, 2020
b2dd527
Merge branch 'master' of github.com:elastic/beats into agent-new-stru…
michalpristas Sep 3, 2020
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
5 changes: 5 additions & 0 deletions dev-tools/mage/dmgbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (b *dmgBuilder) buildBeatPkg() error {

// Copy files into the packaging root and set their mode.
for _, f := range b.Files {
if f.Symlink {
// not supported, handling symlink in post/pre install scripts
continue
}

target := filepath.Join(beatPkgRoot, f.Target)
if err := Copy(f.Source, target); err != nil {
if f.SkipOnMissing && errors.Is(err, os.ErrNotExist) {
Expand Down
81 changes: 81 additions & 0 deletions dev-tools/mage/pkgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type PackageFile struct {
Dep func(PackageSpec) error `yaml:"-" hash:"-" json:"-"` // Dependency to invoke during Evaluate.
Owner string `yaml:"owner,omitempty"` // File Owner, for user and group name (rpm only).
SkipOnMissing bool `yaml:"skip_on_missing,omitempty"` // Prevents build failure if the file is missing.
Symlink bool `yaml:"symlink"` // Symlink marks file as a symlink pointing from target to source.
}

// OSArchNames defines the names of architectures for use in packages.
Expand Down Expand Up @@ -476,6 +477,10 @@ func copyInstallScript(spec PackageSpec, script string, local *string) error {
*local = strings.TrimSuffix(*local, ".tmpl")
}

if strings.HasSuffix(*local, "."+spec.Name) {
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to have some docs in the code around the fancy logic that copyInstallScript does as function comment or in line. So if someone touches this in the future he knows why all the special cases are here.

*local = strings.TrimSuffix(*local, "."+spec.Name)
}

if err := spec.ExpandFile(script, createDir(*local)); err != nil {
return errors.Wrap(err, "failed to copy install script to package dir")
}
Expand Down Expand Up @@ -539,6 +544,11 @@ func PackageZip(spec PackageSpec) error {

// Add files to zip.
for _, pkgFile := range spec.Files {
if pkgFile.Symlink {
// not supported on zip archives
continue
}

if err := addFileToZip(w, baseDir, pkgFile); err != nil {
p, _ := filepath.Abs(pkgFile.Source)
return errors.Wrapf(err, "failed adding file=%+v to zip", p)
Expand Down Expand Up @@ -584,11 +594,32 @@ func PackageTarGz(spec PackageSpec) error {

// Add files to tar.
for _, pkgFile := range spec.Files {
if pkgFile.Symlink {
continue
}

if err := addFileToTar(w, baseDir, pkgFile); err != nil {
return errors.Wrapf(err, "failed adding file=%+v to tar", pkgFile)
}
}

// same for symlinks so they can point to files in tar
for _, pkgFile := range spec.Files {
if !pkgFile.Symlink {
continue
}

tmpdir, err := ioutil.TempDir("", "TmpSymlinkDropPath")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)

if err := addSymlinkToTar(tmpdir, w, baseDir, pkgFile); err != nil {
return errors.Wrapf(err, "failed adding file=%+v to tar", pkgFile)
}
}

if err := w.Close(); err != nil {
return err
}
Expand Down Expand Up @@ -882,6 +913,56 @@ func addFileToTar(ar *tar.Writer, baseDir string, pkgFile PackageFile) error {
})
}

// addSymlinkToTar adds a symlink file to a tar archive.
func addSymlinkToTar(tmpdir string, ar *tar.Writer, baseDir string, pkgFile PackageFile) error {
// create symlink we can work with later, header will be updated later
link := filepath.Join(tmpdir, "link")
target := tmpdir
if err := os.Symlink(target, link); err != nil {
return err
}

return filepath.Walk(link, func(path string, info os.FileInfo, err error) error {
if err != nil {
if pkgFile.SkipOnMissing && os.IsNotExist(err) {
return nil
}

return err
}

header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
header.Uname, header.Gname = "root", "root"
header.Uid, header.Gid = 0, 0

if info.Mode().IsRegular() && pkgFile.Mode > 0 {
header.Mode = int64(pkgFile.Mode & os.ModePerm)
} else if info.IsDir() {
header.Mode = int64(0755)
}

header.Name = filepath.Join(baseDir, pkgFile.Target)
if filepath.IsAbs(pkgFile.Target) {
header.Name = pkgFile.Target
}

header.Linkname = pkgFile.Source
header.Typeflag = tar.TypeSymlink

if mg.Verbose() {
log.Println("Adding", os.FileMode(header.Mode), header.Name)
}
if err := ar.WriteHeader(header); err != nil {
return err
}

return nil
})
}

// PackageDMG packages the Beat into a .dmg file containing an installer pkg
// and uninstaller app.
func PackageDMG(spec PackageSpec) error {
Expand Down
10 changes: 10 additions & 0 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var (
"beat_doc_branch": BeatDocBranch,
"beat_version": BeatQualifiedVersion,
"commit": CommitHash,
"commit_short": CommitHashShort,
"date": BuildDate,
"elastic_beats_dir": ElasticBeatsDir,
"go_version": GoVersion,
Expand Down Expand Up @@ -239,6 +240,15 @@ func CommitHash() (string, error) {
return commitHash, err
}

// CommitHashShort returns the short length git commit hash.
func CommitHashShort() (string, error) {
shortHash, err := CommitHash()
if len(shortHash) > 6 {
shortHash = shortHash[:6]
}
return shortHash, err
}

var (
elasticBeatsDirValue string
elasticBeatsDirErr error
Expand Down
Loading