Skip to content

Commit

Permalink
[Agent] Introduce mage demo command (#17312)
Browse files Browse the repository at this point in the history
[Agent] Introduce mage demo command (#17312)
  • Loading branch information
michalpristas authored Apr 14, 2020
1 parent 07ed0da commit 9557fe1
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 36 deletions.
7 changes: 7 additions & 0 deletions dev-tools/mage/pkgspecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func UseElasticAgentPackaging() {
MustUsePackaging("elastic_beat_agent_binaries", packageSpecFile)
}

// UseElasticAgentDemoPackaging configures the package target to build packages for
// an Elastic Agent demo purposes.
func UseElasticAgentDemoPackaging() {
// Prepare binaries so they can be packed into agent
MustUsePackaging("elastic_beat_agent_demo_binaries", packageSpecFile)
}

// UseElasticBeatPackaging configures the package target to build packages for
// an Elastic Beat. This means it will generate two sets of packages -- one
// that is purely OSS under Apache 2.0 and one that is licensed under the
Expand Down
25 changes: 25 additions & 0 deletions dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,28 @@ specs:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}


# Elastic Beat with Elastic License and binary taken from the x-pack dir.
elastic_beat_agent_demo_binaries:
###
# Elastic Licensed Packages
###

- os: linux
types: [tgz]
spec:
<<: *agent_binary_spec
<<: *elastic_license_for_binaries
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

- os: linux
types: [docker]
spec:
<<: *agent_docker_spec
<<: *elastic_docker_spec
<<: *elastic_license_for_binaries
files:
'{{.BeatName}}{{.BinaryExt}}':
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
- OS agnostic default configuration {pull}17016[17016]
- Introduced post install hooks {pull}17241[17241]
- Support for config constraints {pull}17112[17112]
- Introduced `mage demo` command {pull}17312[17312]
- Display the stability of the agent at enroll and start. {pull}17336[17336]
157 changes: 121 additions & 36 deletions x-pack/elastic-agent/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
// Aliases for commands required by master makefile
var Aliases = map[string]interface{}{
"build": Build.All,
"demo": Demo.Enroll,
}

func init() {
Expand Down Expand Up @@ -70,6 +71,9 @@ type Prepare mg.Namespace
// Format automatically format the code.
type Format mg.Namespace

// Demo runs agent out of container.
type Demo mg.Namespace

// Env returns information about the environment.
func (Prepare) Env() {
mg.Deps(Mkdir("build"), Build.GenerateConfig)
Expand Down Expand Up @@ -267,47 +271,13 @@ func Package() {
start := time.Now()
defer func() { fmt.Println("package ran for", time.Since(start)) }()

version, found := os.LookupEnv("BEAT_VERSION")
if !found {
version = release.Version()
}

packedBeats := []string{"filebeat", "metricbeat"}
requiredPackages := []string{
packageAgent([]string{
"darwin-x86_64.tar.gz",
"linux-x86.tar.gz",
"linux-x86_64.tar.gz",
"windows-x86.zip",
"windows-x86_64.zip",
}

for _, b := range packedBeats {
pwd, err := filepath.Abs(filepath.Join("..", b))
if err != nil {
panic(err)
}

if requiredPackagesPresent(pwd, b, version, requiredPackages) {
continue
}

cmd := exec.Command("mage", "package")
cmd.Dir = pwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), fmt.Sprintf("PWD=%s", pwd), "AGENT_PACKAGING=on")

if err := cmd.Run(); err != nil {
panic(err)
}
}

// package agent
devtools.UseElasticAgentPackaging()

mg.Deps(Update)
mg.Deps(CrossBuild, CrossBuildGoDaemon)
mg.SerialDeps(devtools.Package)
}, devtools.UseElasticAgentPackaging)
}

func requiredPackagesPresent(basePath, beat, version string, requiredPackages []string) bool {
Expand Down Expand Up @@ -480,3 +450,118 @@ func BuildFleetCfg() error {
func Fields() error {
return nil
}

// Enroll runs agent which enrolls before running.
func (Demo) Enroll() error {
env := map[string]string{
"FLEET_ENROLL": "1",
}
return runAgent(env)
}

// Enroll runs agent which does not enroll before running.
func (Demo) NoEnroll() error {
env := map[string]string{
"FLEET_ENROLL": "0",
}
return runAgent(env)
}

func runAgent(env map[string]string) error {
supportedEnvs := map[string]int{"FLEET_ADMIN_PASSWORD": 0, "FLEET_ADMIN_USERNAME": 0, "FLEET_CONFIG_ID": 0, "FLEET_ENROLLMENT_TOKEN": 0, "FLEET_ENROLL": 0, "FLEET_SETUP": 0, "FLEET_TOKEN_NAME": 0, "KIBANA_HOST": 0, "KIBANA_PASSWORD": 0, "KIBANA_USERNAME": 0}

tag := dockerTag()
dockerImageOut, err := sh.Output("docker", "image", "ls")
if err != nil {
return err
}

// docker does not exists for this commit, build it
if !strings.Contains(dockerImageOut, tag) {
// produce docker package
packageAgent([]string{
"linux-x86.tar.gz",
"linux-x86_64.tar.gz",
}, devtools.UseElasticAgentDemoPackaging)

dockerPackagePath := filepath.Join("build", "package", "elastic-agent", "elastic-agent-linux-amd64.docker", "docker-build")
if err := os.Chdir(dockerPackagePath); err != nil {
return err
}

// build docker image
if err := sh.Run("docker", "build", "-t", tag, "."); err != nil {
return err
}
}

// prepare env variables
var envs []string
envs = append(envs, os.Environ()...)
for k, v := range env {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}

// run docker cmd
dockerCmdArgs := []string{"run", "--network", "host"}
for _, e := range envs {
parts := strings.SplitN(e, "=", 2)
if _, isSupported := supportedEnvs[parts[0]]; !isSupported {
continue
}

dockerCmdArgs = append(dockerCmdArgs, "-e", e)
}

dockerCmdArgs = append(dockerCmdArgs, tag)
return sh.Run("docker", dockerCmdArgs...)
}

func packageAgent(requiredPackages []string, packagingFn func()) {
version, found := os.LookupEnv("BEAT_VERSION")
if !found {
version = release.Version()
}

packedBeats := []string{"filebeat", "metricbeat"}

for _, b := range packedBeats {
pwd, err := filepath.Abs(filepath.Join("..", b))
if err != nil {
panic(err)
}

if requiredPackagesPresent(pwd, b, version, requiredPackages) {
continue
}

cmd := exec.Command("mage", "package")
cmd.Dir = pwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), fmt.Sprintf("PWD=%s", pwd), "AGENT_PACKAGING=on")

if err := cmd.Run(); err != nil {
panic(err)
}
}

// package agent
packagingFn()

mg.Deps(Update)
mg.Deps(CrossBuild, CrossBuildGoDaemon)
mg.SerialDeps(devtools.Package)
}

func dockerTag() string {
const commitLen = 7
tagBase := "elastic-agent"

commit, err := devtools.CommitHash()
if err == nil && len(commit) > commitLen {
return fmt.Sprintf("%s-%s", tagBase, commit[:commitLen])
}

return tagBase
}

0 comments on commit 9557fe1

Please sign in to comment.