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

[Heartbeat] Add Magefile to X-Pack #20549

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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")
}
19 changes: 16 additions & 3 deletions x-pack/heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// 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.
// Licensed to Elasticsearch B.V. under one or more contributor
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
// 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 cmd

Expand Down
131 changes: 131 additions & 0 deletions x-pack/heartbeat/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Licensed to Elasticsearch B.V. under one or more contributor
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
// 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.

// +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"
}

// 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(), ".")
}
19 changes: 16 additions & 3 deletions x-pack/heartbeat/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// 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 main

Expand Down
20 changes: 17 additions & 3 deletions x-pack/heartbeat/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
andrewvc marked this conversation as resolved.
Show resolved Hide resolved
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// 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 main

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