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

Generate fields.yml using Mage #7670

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG-developer.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ The list below covers the major changes between 6.3.0 and master only.
- Dashboards under _meta/kibana are expected to be decoded. See https://github.com/elastic/beats/pull/7224 for a conversion script. {pull}7265[7265]
- Constructor `(github.com/elastic/beats/libbeat/output/codec/json).New` expects a new `escapeHTML` parameter. {pull}7445[7445]
- Packaging has been refactored and updates are required. See the PR for migration details. {pull}7388[7388]
- `make fields` has been modified to use Mage (https://magefile.org/) in an effort to make
the building a Beat more cross-platform friendly (e.g. Windows). This requires that your Beat
has a magefile.go with a fields target. The `FIELDS_FILE_PATH` make variable is no longer
used because the value is specified in magefile.go. {pull}7670[7670]

==== Bugfixes

Expand Down
1 change: 0 additions & 1 deletion auditbeat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ GOX_OS?=linux windows ## @Building List of all OS to be supported by "make cross
DEV_OS?=linux
TESTING_ENVIRONMENT?=snapshot-noxpack
ES_BEATS?=..
FIELDS_FILE_PATH=module

# Path to the libbeat Makefile
include ${ES_BEATS}/libbeat/scripts/Makefile
Expand Down
5 changes: 5 additions & 0 deletions auditbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("module")
}

// -----------------------------------------------------------------------------
// Customizations specific to Auditbeat.
// - Config files are Go templates.
Expand Down
11 changes: 11 additions & 0 deletions auditbeat/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off

REM Windows wrapper for Mage (https://magefile.org/) that installs it
REM to %GOPATH%\bin from the Beats vendor directory.
REM
REM After running this once you may invoke mage.exe directly.

WHERE mage
IF %ERRORLEVEL% NEQ 0 go install github.com/elastic/beats/vendor/github.com/magefile/mage

mage %*
23 changes: 11 additions & 12 deletions dev-tools/jenkins_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ $env:PATH = "$env:GOPATH\bin;C:\tools\mingw64\bin;$env:PATH"
# each run starts from a clean slate.
$env:MAGEFILE_CACHE = "$env:WORKSPACE\.magefile"

exec { go install github.com/elastic/beats/vendor/github.com/magefile/mage }
exec { go get -u github.com/jstemmer/go-junit-report }

echo "Building libbeat fields.yml"
Copy link
Member Author

Choose a reason for hiding this comment

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

This command will go away once the field generation is updated to automatically get the libbeat field values.

cd libbeat
exec { mage fields }
cd ..

if (Test-Path "$env:beat") {
cd "$env:beat"
} else {
Expand All @@ -35,20 +43,11 @@ New-Item -ItemType directory -Path build\coverage | Out-Null
New-Item -ItemType directory -Path build\system-tests | Out-Null
New-Item -ItemType directory -Path build\system-tests\run | Out-Null

exec { go get -u github.com/jstemmer/go-junit-report }
echo "Building fields.yml"
exec { mage fields }

echo "Building $env:beat"
exec { go build } "Build FAILURE"

# always build the libbeat fields
cp ..\libbeat\_meta\fields.common.yml ..\libbeat\_meta\fields.generated.yml
cat ..\libbeat\processors\*\_meta\fields.yml | Out-File -append -encoding UTF8 -filepath ..\libbeat\_meta\fields.generated.yml
cp ..\libbeat\_meta\fields.generated.yml ..\libbeat\fields.yml

if ($env:beat -eq "metricbeat") {
cp .\_meta\fields.common.yml .\_meta\fields.generated.yml
python .\scripts\fields_collector.py | out-file -append -encoding UTF8 -filepath .\_meta\fields.generated.yml
}
exec { mage build } "Build FAILURE"
Copy link
Member

Choose a reason for hiding this comment

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

Nice. This should have been simplified before already when we move to fields collection to golang.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this also mean that .\scripts\fields_collector.py should have been removed?

Copy link
Member

Choose a reason for hiding this comment

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

I would say yes. I remember something like we wanted to keep it around in the PR that introduced the golang for comparing the two and removing it afterwards but seems that was forgotten.


echo "Unit testing $env:beat"
go test -v $(go list ./... | select-string -Pattern "vendor" -NotMatch) 2>&1 | Out-File -encoding UTF8 build/TEST-go-unit.out
Expand Down
48 changes: 48 additions & 0 deletions dev-tools/mage/fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 (
"path/filepath"

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

// GenerateFieldsYAML generates a fields.yml file for a Beat. This will include
// the common fields specified by libbeat, the common fields for the Beat,
// and any additional fields.yml files you specify.
//
// fieldsFiles specifies additional directories to search recursively for files
// named fields.yml. The contents of each fields.yml will be included in the
// generated file.
func GenerateFieldsYAML(fieldsFiles ...string) error {
const globalFieldsCmdPath = "libbeat/scripts/cmd/global_fields/main.go"

beatsDir, err := ElasticBeatsDir()
if err != nil {
return err
}

globalFieldsCmd := sh.RunCmd("go", "run",
filepath.Join(beatsDir, globalFieldsCmdPath),
"-es_beats_path", beatsDir,
"-beat_path", CWD(),
)

return globalFieldsCmd(fieldsFiles...)
}
5 changes: 5 additions & 0 deletions dev-tools/packaging/templates/common/magefile.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ func TestPackages() error {
func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("protos")
}
5 changes: 5 additions & 0 deletions filebeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("module")
}

// -----------------------------------------------------------------------------
// Customizations specific to Filebeat.
// - Include modules directory in packages (minus _meta and test files).
Expand Down
18 changes: 11 additions & 7 deletions filebeat/make.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
REM Batch script to build and test on Windows. You can use this in conjunction
REM with the Vagrant machine.
go build
go test ./...
go test -c -cover -covermode=count -coverpkg ./...
cd tests\system
nosetests
@echo off

REM Windows wrapper for Mage (https://magefile.org/) that installs it
REM to %GOPATH%\bin from the Beats vendor directory.
REM
REM After running this once you may invoke mage.exe directly.

WHERE mage
IF %ERRORLEVEL% NEQ 0 go install github.com/elastic/beats/vendor/github.com/magefile/mage

mage %*
3 changes: 0 additions & 3 deletions generator/beat/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
override FIELDS_FILE_PATH=
export FIELDS_FILE_PATH

include ../common/Makefile
18 changes: 6 additions & 12 deletions generator/beat/{beat}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MAGE_IMPORT_PATH=${BEAT_PATH}/vendor/github.com/magefile/mage

# Initial beat setup
.PHONY: setup
setup: copy-vendor update git-init
setup: copy-vendor git-init update git-add

# Copy beats into vendor directory
.PHONY: copy-vendor
Expand All @@ -27,17 +27,11 @@ copy-vendor:
.PHONY: git-init
git-init:
git init
git add README.md CONTRIBUTING.md
git commit -m "Initial commit"
git add LICENSE.txt
git commit -m "Add the LICENSE"
git add .gitignore
git commit -m "Add git settings"
git add .
git reset -- .travis.yml
git commit -m "Add {beat}"
git add .travis.yml
git commit -m "Add Travis CI"

.PHONY: git-add
git-add:
git add -A
git commit -m "Add generated {beat} files"

# Collects all dependencies and then calls update
.PHONY: collect
Expand Down
5 changes: 5 additions & 0 deletions generator/beat/{beat}/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ func TestPackages() error {
func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML()
}
11 changes: 11 additions & 0 deletions generator/beat/{beat}/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off

REM Windows wrapper for Mage (https://magefile.org/) that installs it
REM to %GOPATH%\bin from the Beats vendor directory.
REM
REM After running this once you may invoke mage.exe directly.

WHERE mage
IF %ERRORLEVEL% NEQ 0 go install {beat_path}/vendor/github.com/magefile/mage

mage %*
19 changes: 16 additions & 3 deletions generator/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,36 @@ PREPARE_COMMAND?=
test: prepare-test
. ${PYTHON_ENV}/bin/activate; \
export GOPATH=${PWD}/build ; \
export PATH=${PATH}:${PWD}/build/bin; \
export PATH=$${GOPATH}/bin:${PATH}; \
Copy link
Member

Choose a reason for hiding this comment

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

$$ on purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It's an escape so that GOPATH is resolve in the shell command rather than by Make. If you don't do this then you get the original GOPATH value and not the one set 2 lines above.

cd ${BEAT_PATH} ; \
$(MAKE) copy-vendor || exit 1 ; \
${PREPARE_COMMAND} \
$(MAKE) git-init || exit 1 ; \
$(MAKE) update || exit 1 ; \
$(MAKE) git-add || exit 1 ; \
$(MAKE) check CHECK_HEADERS_DISABLED=y || exit 1 ; \
$(MAKE) || exit 1 ; \
$(MAKE) unit

.PHONY: prepare-test
prepare-test:: python-env
# Makes sure to use current version of beats for testing
mkdir -p ${BUILD_DIR}/src/github.com/elastic/beats/
rsync -a --exclude=build ${PWD}/../../* ${BUILD_DIR}/src/github.com/elastic/beats/
rsync -a \
--include=vendor/github.com/magefile/mage/build \
--exclude=build/ \
--exclude=.git/ \
${PWD}/../../* ${BUILD_DIR}/src/github.com/elastic/beats/

mkdir -p ${BEAT_PATH}
export GOPATH=${PWD}/build ; \
. ${PYTHON_ENV}/bin/activate && python ${PWD}/build/src/github.com/elastic/beats/script/generate.py --type=${BEAT_TYPE} --project_name=Testbeat --github_name=ruflin --beat_path=beatpath/testbeat --full_name="Nicolas Ruflin"
. ${PYTHON_ENV}/bin/activate && \
python ${PWD}/build/src/github.com/elastic/beats/script/generate.py \
--type=${BEAT_TYPE} \
--project_name=Testbeat \
--github_name=ruflin \
--beat_path=beatpath/testbeat \
--full_name="Nicolas Ruflin"

# Runs test build for the created beat
.PHONY: test-build
Expand Down
10 changes: 4 additions & 6 deletions generator/metricbeat/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
BEAT_TYPE=metricbeat
PREPARE_COMMAND=MODULE=elastic METRICSET=test make create-metricset ; FIELDS_FILE_PATH=module
override FIELDS_FILE_PATH=
export FIELDS_FILE_PATH

BEAT_TYPE = metricbeat
Copy link
Member

Choose a reason for hiding this comment

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

I like the new indentation but thing I wonder is if the after the = could cause some issues? I remember we had a postfix space when it was something like BEAT_TYPE=metricbeat #is a comment, not sure if it also applies to prefix.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm pretty sure the spaces are dropped by make (in bash they would not be). I've used this syntax before without issues (https://github.com/elastic/golang-crossbuild/blob/master/Makefile.common and several other files in this repo).

PREPARE_COMMAND = MODULE=elastic METRICSET=test make create-metricset;

include ../common/Makefile

.PHONY: prepare-test
prepare-test:: python-env

mkdir -p ${BEAT_PATH}/scripts
rsync -a --exclude=build ${PWD}/../../metricbeat/scripts/generate_imports_helper.py ${BEAT_PATH}/scripts

# Collects all dependencies and then calls update
.PHONY: collect
collect: fields

9 changes: 6 additions & 3 deletions generator/metricbeat/{beat}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MAGE_IMPORT_PATH=${BEAT_PATH}/vendor/github.com/magefile/mage

# Initial beat setup
.PHONY: setup
setup: copy-vendor create-metricset collect git-init
setup: copy-vendor git-init create-metricset collect git-add

# Copy beats into vendor directory
.PHONY: copy-vendor
Expand All @@ -28,5 +28,8 @@ copy-vendor:
.PHONY: git-init
git-init:
git init
git add --all
git commit -m 'Initial add by Beat generator'

.PHONY: git-add
git-add:
git add -A
git commit -m "Add generated {beat} files"
5 changes: 5 additions & 0 deletions generator/metricbeat/{beat}/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ func TestPackages() error {
func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("module")
}
11 changes: 11 additions & 0 deletions generator/metricbeat/{beat}/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off

REM Windows wrapper for Mage (https://magefile.org/) that installs it
REM to %GOPATH%\bin from the Beats vendor directory.
REM
REM After running this once you may invoke mage.exe directly.

WHERE mage
IF %ERRORLEVEL% NEQ 0 go install {beat_path}/vendor/github.com/magefile/mage

mage %*
1 change: 0 additions & 1 deletion heartbeat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ BEAT_NAME=heartbeat
BEAT_TITLE=Heartbeat
SYSTEM_TESTS=true
TEST_ENVIRONMENT=false
FIELDS_FILE_PATH=monitors/active

# Path to the libbeat Makefile
-include ../libbeat/scripts/Makefile
Expand Down
5 changes: 5 additions & 0 deletions heartbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ func TestPackages() error {
func Update() error {
return sh.Run("make", "update")
}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("monitors/active")
}
11 changes: 11 additions & 0 deletions heartbeat/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off

REM Windows wrapper for Mage (https://magefile.org/) that installs it
REM to %GOPATH%\bin from the Beats vendor directory.
REM
REM After running this once you may invoke mage.exe directly.

WHERE mage
IF %ERRORLEVEL% NEQ 0 go install github.com/elastic/beats/vendor/github.com/magefile/mage

mage %*
1 change: 0 additions & 1 deletion libbeat/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
BEAT_NAME=libbeat
TEST_ENVIRONMENT?=true
SYSTEM_TESTS=true
FIELDS_FILE_PATH=processors

include scripts/Makefile

Expand Down
Loading