Skip to content

Commit

Permalink
[Heartbeat] Add Magefile to X-Pack (elastic#20549)
Browse files Browse the repository at this point in the history
This also restructures the main magefile to share code with the x-pack
one
  • Loading branch information
andrewvc authored and melchiormoulin committed Oct 14, 2020
1 parent 78103aa commit 2af2237
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 30 deletions.
36 changes: 6 additions & 30 deletions heartbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"

devtools "github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/generator/common/beatgen"
Expand Down Expand Up @@ -95,7 +94,7 @@ func Package() {
defer func() { fmt.Println("package ran for", time.Since(start)) }()

devtools.UseElasticBeatPackaging()
customizePackaging()
heartbeat.CustomizePackaging()

mg.Deps(Update)
mg.Deps(CrossBuild, CrossBuildXPack, CrossBuildGoDaemon)
Expand All @@ -107,14 +106,13 @@ func TestPackages() error {
return devtools.TestPackages(devtools.WithMonitorsD())
}

// Update updates the generated files (aka make update).
func Update() error {
return sh.Run("make", "update")
func Fields() error {
return heartbeat.Fields()
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return devtools.GenerateFieldsYAML("monitors/active")
// Update updates the generated files (aka make update).
func Update() {
mg.SerialDeps(Fields, Config)
}

// Imports generates an include/list.go file containing
Expand All @@ -127,28 +125,6 @@ func Imports() error {
return devtools.GenerateIncludeListGo(options)
}

func customizePackaging() {
monitorsDTarget := "monitors.d"
unixMonitorsDir := "/etc/{{.BeatName}}/monitors.d"
monitorsD := devtools.PackageFile{
Mode: 0644,
Source: "monitors.d",
}

for _, args := range devtools.Packages {
pkgType := args.Types[0]
switch pkgType {
case devtools.Docker:
args.Spec.ExtraVar("linux_capabilities", "cap_net_raw=eip")
args.Spec.Files[monitorsDTarget] = monitorsD
case devtools.TarGz, devtools.Zip:
args.Spec.Files[monitorsDTarget] = monitorsD
case devtools.Deb, devtools.RPM, devtools.DMG:
args.Spec.Files[unixMonitorsDir] = monitorsD
}
}
}

// Config generates both the short/reference/docker configs.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, heartbeat.ConfigFileParams(), ".")
Expand Down
55 changes: 55 additions & 0 deletions heartbeat/scripts/mage/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package mage

import devtools "github.com/elastic/beats/v7/dev-tools/mage"

const (
dirModuleGenerated = "build/package/module"
dirModulesDGenerated = "build/package/modules.d"
)

// CustomizePackaging modifies the package specs to add the modules and
// modules.d directory. You must declare a dependency on either
// PrepareModulePackagingOSS or PrepareModulePackagingXPack.
func CustomizePackaging() {
monitorsDTarget := "monitors.d"
unixMonitorsDir := "/etc/{{.BeatName}}/monitors.d"
monitorsD := devtools.PackageFile{
Mode: 0644,
Source: "monitors.d",
}

for _, args := range devtools.Packages {
pkgType := args.Types[0]
switch pkgType {
case devtools.Docker:
args.Spec.ExtraVar("linux_capabilities", "cap_net_raw=eip")
args.Spec.Files[monitorsDTarget] = monitorsD
case devtools.TarGz, devtools.Zip:
args.Spec.Files[monitorsDTarget] = monitorsD
case devtools.Deb, devtools.RPM, devtools.DMG:
args.Spec.Files[unixMonitorsDir] = monitorsD
}
}
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return devtools.GenerateFieldsYAML("monitors/active")
}
119 changes: 119 additions & 0 deletions x-pack/heartbeat/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build mage

package main

import (
"fmt"
"time"

"github.com/magefile/mage/mg"

devtools "github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/generator/common/beatgen"
heartbeat "github.com/elastic/beats/v7/heartbeat/scripts/mage"

// mage:import
"github.com/elastic/beats/v7/dev-tools/mage/target/common"
// mage:import
"github.com/elastic/beats/v7/dev-tools/mage/target/unittest"
// mage:import
"github.com/elastic/beats/v7/dev-tools/mage/target/integtest"
// mage:import
_ "github.com/elastic/beats/v7/dev-tools/mage/target/test"
)

func init() {
common.RegisterCheckDeps(Update)
unittest.RegisterPythonTestDeps(Fields)
integtest.RegisterPythonTestDeps(Fields)

devtools.BeatDescription = "Ping remote services for availability and log " +
"results to Elasticsearch or send to Logstash."
devtools.BeatServiceName = "heartbeat-elastic"
devtools.BeatLicense = "Elastic License"
}

// VendorUpdate updates elastic/beats/v7 in the vendor dir
func VendorUpdate() error {
return beatgen.VendorUpdate()
}

// Build builds the Beat binary.
func Build() error {
return devtools.Build(devtools.DefaultBuildArgs())
}

// GolangCrossBuild build the Beat binary inside of the golang-builder.
// Do not use directly, use crossBuild instead.
func GolangCrossBuild() error {
return devtools.GolangCrossBuild(devtools.DefaultGolangCrossBuildArgs())
}

// BuildGoDaemon builds the go-daemon binary (use crossBuildGoDaemon).
func BuildGoDaemon() error {
return devtools.BuildGoDaemon()
}

// CrossBuild cross-builds the beat for all target platforms.
func CrossBuild() error {
return devtools.CrossBuild()
}

// CrossBuildXPack cross-builds the beat with XPack for all target platforms.
func CrossBuildXPack() error {
return devtools.CrossBuildXPack()
}

// CrossBuildGoDaemon cross-builds the go-daemon binary using Docker.
func CrossBuildGoDaemon() error {
return devtools.CrossBuildGoDaemon()
}

// Package packages the Beat for distribution.
// Use SNAPSHOT=true to build snapshots.
// Use PLATFORMS to control the target platforms.
// Use VERSION_QUALIFIER to control the version qualifier.
func Package() {
start := time.Now()
defer func() { fmt.Println("package ran for", time.Since(start)) }()

devtools.UseElasticBeatPackaging()
heartbeat.CustomizePackaging()

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

// TestPackages tests the generated packages (i.e. file modes, owners, groups).
func TestPackages() error {
return devtools.TestPackages(devtools.WithMonitorsD())
}

func Fields() error {
return heartbeat.Fields()
}

// Update updates the generated files (aka make update).
func Update() {
mg.SerialDeps(Fields, Config)
}

// Imports generates an include/list.go file containing
// a import statement for each module and dataset.
func Imports() error {
options := devtools.DefaultIncludeListOptions()
options.ModuleDirs = []string{"monitors"}
options.Outfile = "monitors/defaults/default.go"
options.Pkg = "defaults"
return devtools.GenerateIncludeListGo(options)
}

// Config generates both the short/reference/docker configs.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, heartbeat.ConfigFileParams(), ".")
}
1 change: 1 addition & 0 deletions x-pack/heartbeat/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package main

// This file is mandatory as otherwise the heartbeat.test binary is not generated correctly.
Expand Down

0 comments on commit 2af2237

Please sign in to comment.