Skip to content

Commit a2385bd

Browse files
[Agent] Fix installers (#17077)
[Agent] Fix installers for snapshots (#17077)
1 parent 1d3bc8c commit a2385bd

File tree

15 files changed

+112
-37
lines changed

15 files changed

+112
-37
lines changed

dev-tools/mage/crossbuild.go

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ func (b GolangCrossBuilder) Build() error {
268268
"--rm",
269269
"--env", "MAGEFILE_VERBOSE="+verbose,
270270
"--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""),
271+
"--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot),
271272
"-v", repoInfo.RootDir+":"+mountPoint,
272273
"-w", workDir,
273274
image,

dev-tools/packaging/packages.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ shared:
5454
/etc/init.d/{{.BeatServiceName}}:
5555
template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/{{.PackageType}}/init.sh.tmpl'
5656
mode: 0755
57-
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
57+
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
5858
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
5959
mode: 0644
60-
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
60+
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
6161
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
6262
mode: 0644
6363

@@ -97,10 +97,10 @@ shared:
9797
source: 'agent.yml'
9898
mode: 0600
9999
config: true
100-
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
100+
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
101101
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
102102
mode: 0644
103-
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
103+
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
104104
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
105105
mode: 0644
106106

@@ -131,10 +131,10 @@ shared:
131131
<<: *common
132132
files:
133133
<<: *agent_binary_files
134-
'data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz':
134+
'data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz':
135135
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
136136
mode: 0644
137-
'data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz':
137+
'data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz':
138138
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
139139
mode: 0644
140140

@@ -149,10 +149,10 @@ shared:
149149
uninstall-service-{{.BeatName}}.ps1:
150150
template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/windows/uninstall-service.ps1.tmpl'
151151
mode: 0755
152-
'data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.zip':
152+
'data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip':
153153
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip'
154154
mode: 0644
155-
'data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.zip':
155+
'data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip':
156156
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip'
157157
mode: 0644
158158

x-pack/agent/CHANGELOG.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
==== Bugfixes
1414

1515
- Fixed tests on windows {pull}16922[16922]
16+
- Fixed installers for SNAPSHOTs and windows {pull}17077[17077]
1617

1718
==== New features
1819

x-pack/agent/_meta/agent.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
outputs:
55
default:
66
type: elasticsearch
7-
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
7+
hosts: [127.0.0.1:9200]
88
username: elastic
99
password: changeme
1010

x-pack/agent/_meta/common.p2.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
outputs:
55
default:
66
type: elasticsearch
7-
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
7+
hosts: [127.0.0.1:9200]
88
username: elastic
99
password: changeme
1010

x-pack/agent/_meta/common.reference.p2.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
outputs:
55
default:
66
type: elasticsearch
7-
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
7+
hosts: [127.0.0.1:9200]
88
username: elastic
99
password: changeme
1010

x-pack/agent/agent.reference.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
outputs:
1010
default:
1111
type: elasticsearch
12-
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
12+
hosts: [127.0.0.1:9200]
1313
username: elastic
1414
password: changeme
1515

x-pack/agent/agent.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
outputs:
1010
default:
1111
type: elasticsearch
12-
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
12+
hosts: [127.0.0.1:9200]
1313
username: elastic
1414
password: changeme
1515

x-pack/agent/magefile.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
goLicenserRepo = "github.com/elastic/go-licenser"
3737
buildDir = "build"
3838
metaDir = "_meta"
39+
snapshotEnv = "SNAPSHOT"
3940
)
4041

4142
// Aliases for commands required by master makefile
@@ -101,6 +102,7 @@ func (Build) GenerateConfig() error {
101102
func GolangCrossBuildOSS() error {
102103
params := devtools.DefaultGolangCrossBuildArgs()
103104
params.InputFiles = []string{"cmd/agent/agent.go"}
105+
params.LDFlags = flagsSet()
104106
return devtools.GolangCrossBuild(params)
105107
}
106108

@@ -110,6 +112,7 @@ func GolangCrossBuild() error {
110112
params := devtools.DefaultGolangCrossBuildArgs()
111113
params.InputFiles = []string{"cmd/agent/agent.go"}
112114
params.OutputDir = "build/golang-crossbuild"
115+
params.LDFlags = flagsSet()
113116
if err := devtools.GolangCrossBuild(params); err != nil {
114117
return err
115118
}
@@ -210,7 +213,7 @@ func (Check) License() error {
210213
)
211214
}
212215

213-
// Changes run git status --porcelain and return an error if we have changes or uncommited files.
216+
// Changes run git status --porcelain and return an error if we have changes or uncommitted files.
214217
func (Check) Changes() error {
215218
out, err := sh.Output("git", "status", "--porcelain")
216219
if err != nil {
@@ -354,14 +357,19 @@ func commitID() string {
354357
}
355358

356359
func flags() string {
360+
return strings.Join(flagsSet(), " ")
361+
}
362+
363+
func flagsSet() []string {
357364
ts := time.Now().Format(time.RFC3339)
358365
commitID := commitID()
366+
isSnapshot, _ := os.LookupEnv(snapshotEnv)
359367

360-
return fmt.Sprintf(
361-
`-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.buildTime=%s" -X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.commit=%s"`,
362-
ts,
363-
commitID,
364-
)
368+
return []string{
369+
fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.buildTime=%s"`, ts),
370+
fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.commit=%s"`, commitID),
371+
fmt.Sprintf(` -X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.snapshot=%s"`, isSnapshot),
372+
}
365373
}
366374

367375
// Update is an alias for executing fields, dashboards, config, includes.

x-pack/agent/pkg/agent/operation/operator_handlers.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors"
1212
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact"
1313
"github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app"
14+
"github.com/elastic/beats/v7/x-pack/agent/pkg/release"
1415
)
1516

1617
type handleFunc func(step configrequest.Step) error
@@ -66,7 +67,12 @@ func getProgramFromStepWithTags(step configrequest.Step, artifactConfig *artifac
6667
return nil, nil, err
6768
}
6869

69-
p := app.NewDescriptor(step.Process, step.Version, artifactConfig, tags)
70+
version := step.Version
71+
if release.Snapshot() {
72+
version = fmt.Sprintf("%s-SNAPSHOT", version)
73+
}
74+
75+
p := app.NewDescriptor(step.Process, version, artifactConfig, tags)
7076
return p, config, nil
7177
}
7278

x-pack/agent/pkg/artifact/install/installer.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ func NewInstaller(config *artifact.Config) (Installer, error) {
3939
if runtime.GOOS == "windows" {
4040
return zip.NewInstaller(config)
4141
}
42+
4243
return tar.NewInstaller(config)
4344
}

x-pack/agent/pkg/artifact/install/tar/tar_installer.go

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func unpack(r io.Reader, dir string) error {
5454
}
5555

5656
tr := tar.NewReader(zr)
57+
var rootDir string
5758

5859
for {
5960
f, err := tr.Next()
@@ -70,6 +71,11 @@ func unpack(r io.Reader, dir string) error {
7071
rel := filepath.FromSlash(f.Name)
7172
abs := filepath.Join(dir, rel)
7273

74+
// find the root dir
75+
if currentDir := filepath.Dir(abs); rootDir == "" || len(filepath.Dir(rootDir)) > len(currentDir) {
76+
rootDir = currentDir
77+
}
78+
7379
fi := f.FileInfo()
7480
mode := fi.Mode()
7581
switch {

x-pack/agent/pkg/artifact/install/zip/zip_installer.go

+49-14
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
package zip
66

77
import (
8+
"archive/zip"
89
"fmt"
910
"os"
1011
"os/exec"
1112
"path/filepath"
12-
"strings"
1313

1414
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors"
1515
"github.com/elastic/beats/v7/x-pack/agent/pkg/artifact"
@@ -35,37 +35,72 @@ func NewInstaller(config *artifact.Config) (*Installer, error) {
3535
// Install performs installation of program in a specific version.
3636
// It expects package to be already downloaded.
3737
func (i *Installer) Install(programName, version, installDir string) error {
38-
if err := i.unzip(programName, version, installDir); err != nil {
38+
artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory)
39+
if err != nil {
3940
return err
4041
}
4142

42-
oldPath := filepath.Join(installDir, fmt.Sprintf("%s-%s-windows", programName, version))
43-
newPath := filepath.Join(installDir, strings.Title(programName))
44-
if err := os.Rename(oldPath, newPath); err != nil {
45-
return errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, newPath))
43+
if err := i.unzip(artifactPath, programName, version); err != nil {
44+
return err
4645
}
4746

48-
return i.runInstall(programName, installDir)
49-
}
50-
51-
func (i *Installer) unzip(programName, version, installPath string) error {
52-
artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory)
47+
rootDir, err := i.getRootDir(artifactPath)
5348
if err != nil {
5449
return err
5550
}
5651

52+
// if root directory is not the same as desired directory rename
53+
// e.g contains `-windows-` or `-SNAPSHOT-`
54+
if rootDir != installDir {
55+
if err := os.Rename(rootDir, installDir); err != nil {
56+
return errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir))
57+
}
58+
}
59+
60+
return i.runInstall(programName, version, installDir)
61+
}
62+
63+
func (i *Installer) unzip(artifactPath, programName, version string) error {
5764
if _, err := os.Stat(artifactPath); err != nil {
5865
return errors.New(fmt.Sprintf("artifact for '%s' version '%s' could not be found at '%s'", programName, version, artifactPath), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, artifactPath))
5966
}
6067

61-
powershellArg := fmt.Sprintf("Expand-Archive -Path \"%s\" -DestinationPath \"%s\"", artifactPath, installPath)
68+
powershellArg := fmt.Sprintf("Expand-Archive -LiteralPath \"%s\" -DestinationPath \"%s\"", artifactPath, i.config.InstallPath)
6269
installCmd := exec.Command("powershell", "-command", powershellArg)
6370
return installCmd.Run()
6471
}
6572

66-
func (i *Installer) runInstall(programName, installPath string) error {
73+
func (i *Installer) runInstall(programName, version, installPath string) error {
6774
powershellCmd := fmt.Sprintf(powershellCmdTemplate, installPath, programName)
68-
6975
installCmd := exec.Command("powershell", "-command", powershellCmd)
76+
7077
return installCmd.Run()
7178
}
79+
80+
// retrieves root directory from zip archive
81+
func (i *Installer) getRootDir(zipPath string) (dir string, err error) {
82+
defer func() {
83+
if dir != "" {
84+
dir = filepath.Join(i.config.InstallPath, dir)
85+
}
86+
}()
87+
88+
zipReader, err := zip.OpenReader(zipPath)
89+
if err != nil {
90+
return "", err
91+
}
92+
defer zipReader.Close()
93+
94+
var rootDir string
95+
for _, f := range zipReader.File {
96+
if filepath.Base(f.Name) == filepath.Dir(f.Name) {
97+
return f.Name, nil
98+
}
99+
100+
if currentDir := filepath.Dir(f.Name); rootDir == "" || len(currentDir) < len(rootDir) {
101+
rootDir = currentDir
102+
}
103+
}
104+
105+
return rootDir, nil
106+
}

x-pack/agent/pkg/basecmd/version/cmd.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ func NewCommandWithArgs(streams *cli.IOStreams) *cobra.Command {
1919
Use: "version",
2020
Short: "Display the version of the agent.",
2121
Run: func(_ *cobra.Command, _ []string) {
22+
version := release.Version()
23+
if release.Snapshot() {
24+
version = version + "-SNAPSHOT"
25+
}
26+
2227
fmt.Fprintf(
2328
streams.Out,
2429
"Agent version is %s (build: %s at %s)\n",
25-
release.Version(),
30+
version,
2631
release.Commit(),
2732
release.BuildTime(),
2833
)

x-pack/agent/pkg/release/version.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package release
66

7-
import "time"
7+
import (
8+
"strconv"
9+
"time"
10+
)
811

912
// version is the current version of the agent.
1013
var version = "8.0.0"
@@ -18,7 +21,10 @@ var buildTime = "<unknown>"
1821
// qualifier returns the version qualifier like alpha1.
1922
var qualifier = ""
2023

21-
// Commit returns the current build hash or unkown if it was not injected in the build process.
24+
// snapshot is a flag marking build as a snapshot.
25+
var snapshot = ""
26+
27+
// Commit returns the current build hash or unknown if it was not injected in the build process.
2228
func Commit() string {
2329
return commit
2430
}
@@ -39,3 +45,9 @@ func Version() string {
3945
}
4046
return version + "-" + qualifier
4147
}
48+
49+
// Snapshot returns true if binary was built as snapshot.
50+
func Snapshot() bool {
51+
val, err := strconv.ParseBool(snapshot)
52+
return err == nil && val
53+
}

0 commit comments

Comments
 (0)