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

Converting generator.py to mage #13610

Merged
merged 34 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f2b4d8b
WiP: converting generator.py to mage
fearful-symmetry Sep 11, 2019
def9e61
quick cleanup, add type check
fearful-symmetry Sep 11, 2019
4540368
more work on moving generator setup
fearful-symmetry Sep 16, 2019
1d9e6ae
port setup over to mage
fearful-symmetry Sep 17, 2019
7dd3d38
more work on copy vendor, clean up
fearful-symmetry Sep 17, 2019
a2985d3
add lic headers
fearful-symmetry Sep 17, 2019
140e032
update and clean up beat template files
fearful-symmetry Sep 18, 2019
762061d
bugfixes
fearful-symmetry Sep 18, 2019
517f198
rebase
fearful-symmetry Sep 18, 2019
cf1a31e
exclude beat template dir
fearful-symmetry Sep 18, 2019
6e8fa67
fix PackageTest
fearful-symmetry Sep 19, 2019
45a456c
work on porting test
fearful-symmetry Sep 19, 2019
492047e
still trying to get CI to work
fearful-symmetry Sep 24, 2019
92f8d1d
add install for mage
fearful-symmetry Sep 24, 2019
4dbd341
I forgot how make targets work
fearful-symmetry Sep 24, 2019
45f7faf
remove old python setup role
fearful-symmetry Sep 24, 2019
4aae953
clean up test makefiles
fearful-symmetry Sep 25, 2019
083fde4
remove old script
fearful-symmetry Sep 25, 2019
c0efb4a
update devguide
fearful-symmetry Sep 25, 2019
5a64748
fix example in Newbeat guide
fearful-symmetry Sep 25, 2019
8a39807
remove lic header in template magefile
fearful-symmetry Sep 25, 2019
b6774b4
code cleanup
fearful-symmetry Sep 25, 2019
b82d74f
error and doc cleanup
fearful-symmetry Oct 9, 2019
de7fd1f
run the imports helper link when we update vendor
fearful-symmetry Oct 9, 2019
48bcaae
print message with newbeat path
fearful-symmetry Oct 10, 2019
5784861
restore old script, move prefix to const
fearful-symmetry Oct 11, 2019
e29c38b
change logic that decides how we copy vendor
fearful-symmetry Oct 14, 2019
e41a523
add deprecation message to old python script
fearful-symmetry Oct 14, 2019
a77cd5b
remove unused make targets
fearful-symmetry Oct 14, 2019
f0d1e7b
add newline to python script
fearful-symmetry Oct 14, 2019
fe7098a
change verbage for users
fearful-symmetry Oct 15, 2019
6c60e05
add legacy make roles to makefiles
fearful-symmetry Oct 15, 2019
e045970
add changelog entry
fearful-symmetry Oct 15, 2019
c7410e9
change changelog entry
fearful-symmetry Oct 16, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- For "metricbeat style" generated custom beats, the mage target `GoTestIntegration` has changed to `GoIntegTest` and `GoTestUnit` has changed to `GoUnitTest`. {pull}13341[13341]
- Build docker and kubernetes features only on supported platforms. {pull}13509[13509]
- Need to register new processors to be used in the JS processor in their `init` functions. {pull}13509[13509]
- The custom beat generator now uses mage instead of python, `mage GenerateCustomBeat` can be used to create a new beat, and `mage vendorUpdate` to update the vendored libbeat in a custom beat. {pull}13610[13610]

==== Bugfixes

Expand Down
8 changes: 8 additions & 0 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ var (
elasticBeatsDirLock sync.Mutex
)

// SetElasticBeatsDir sets the internal elastic beats dir to a preassigned value
func SetElasticBeatsDir(path string) {
elasticBeatsDirLock.Lock()
defer elasticBeatsDirLock.Unlock()

elasticBeatsDirValue = path
}

// ElasticBeatsDir returns the path to Elastic beats dir.
func ElasticBeatsDir() (string, error) {
elasticBeatsDirLock.Lock()
Expand Down
27 changes: 7 additions & 20 deletions docs/devguide/creating-beat-from-metricbeat.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ must be set up correctly. In addition, the following tools are required:

* https://www.python.org/downloads/[python]
* https://virtualenv.pypa.io/en/stable/[virtualenv]
* https://github.com/magefile/mage[mage]

Virtualenv is easiest installed with your package manager or https://pip.pypa.io/en/stable/[pip].

Expand Down Expand Up @@ -43,34 +44,20 @@ This directory is normally located under `$GOPATH/src/github.com/{your-github-na
[float]
==== Step 2 - Create the Beat

Run the command:
Run the command from the root `beats` directory:

[source,bash]
----
python ${GOPATH}/src/github.com/elastic/beats/script/generate.py --type=metricbeat
NEWBEAT_TYPE=metricbeat mage GenerateCustomBeat
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
----

When prompted, enter the Beat name and path.
When prompted, enter the Beat name and path, along with an initial module and metricset name for your beat. For more details about creating a metricset, see the docs <<creating-metricsets>>.


[float]
==== Step 3 - Init and create the metricset

After creating the Beat, change the directory to `$GOPATH/src/github.com/{your-github-name}/{beat}` and run:

[source,bash]
----
make setup
----

This will do the initial setup for your Beat and also run `make create-metricset`, which will ask you for the
module name and metricset name of your Beat.

For more details about creating a metricset, see the docs <<creating-metricsets>>.
After creating the Beat, change the directory to `$GOPATH/src/github.com/{your-github-name}/{beat}`
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved


[float]
==== Step 4 - Build & Run
==== Step 3 - Build & Run

To create a binary run the `make` command. This will create the binary in your beats directory.

Expand All @@ -84,7 +71,7 @@ To run it, execute the binary. This will automatically load the default configur
This will run the beat with debug output enabled to the console to directly see what is happening. Stop the beat with `CTRL-C`.

[float]
==== Step 5 - Package
==== Step 4 - Package

To create packages and binaries for different platforms, https://www.docker.com/[docker] is required.
The first step is to get the most recent packaging tools into your beat:
Expand Down
15 changes: 7 additions & 8 deletions docs/devguide/newbeat.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,23 @@ mkdir ${GOPATH}/src/github.com/{user}
cd ${GOPATH}/src/github.com/{user}
--------------------

Run python and specify the path to the Beat generator:
Run the mage script to generate the custom beat:

NOTE: Python 2 is required (Python 3 will not work).

[source,shell]
--------------------
python $GOPATH/src/github.com/elastic/beats/script/generate.py
mage GenerateCustomBeat
--------------------

Python will prompt you to enter information about your Beat. For the `project_name`, enter `Countbeat`.
The mage script will prompt you to enter information about your Beat. For the `project_name`, enter `Countbeat`.
For the `github_name`, enter your github id. The `beat` and `beat_path` are set to the correct values automatically (just press Enter to accept each default). For the `full_name`, enter your firstname and lastname.

[source,shell]
---------
Beat Name [Examplebeat]: Countbeat
Your Github Name [your-github-name]: {username}
Beat Path [github.com/{github id}/{beat name}]:
Firstname Lastname: {Full Name}
Enter a project name [examplebeat]: Countbeat
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
Enter a github name [your-github-name]: {username}
Enter a beat path [github.com/{username}/countbeat]:
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
Enter a full name [Firstname Lastname]: {Full Name}
---------

The Beat generator creates a directory called `countbeat` inside of your project folder (e.g. {project folder}/github.com/{github id}/countbeat).
Expand Down
30 changes: 2 additions & 28 deletions generator/beat/{beat}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@ CHECK_HEADERS_DISABLED=true
# Path to the libbeat Makefile
-include $(LIBBEAT_MAKEFILE)

# Initial beat setup
.PHONY: setup
setup: pre-setup git-add

pre-setup: copy-vendor git-init
$(MAKE) -f $(LIBBEAT_MAKEFILE) mage ES_BEATS=$(ES_BEATS)
$(MAKE) -f $(LIBBEAT_MAKEFILE) update BEAT_NAME=$(BEAT_NAME) ES_BEATS=$(ES_BEATS) NO_COLLECT=$(NO_COLLECT)

# Copy beats into vendor directory
.PHONY: copy-vendor
copy-vendor: vendor-check
fearful-symmetry marked this conversation as resolved.
Show resolved Hide resolved
mkdir -p vendor/github.com/elastic/beats
git archive --remote ${BEAT_GOPATH}/src/github.com/elastic/beats HEAD | tar -x --exclude=x-pack -C vendor/github.com/elastic/beats
mkdir -p vendor/github.com/magefile
cp -R vendor/github.com/elastic/beats/vendor/github.com/magefile/mage vendor/github.com/magefile

.PHONY: git-init
git-init:
git init

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


.PHONY: vendor-check
vendor-check:
@if output=$$(git -C ${BEAT_GOPATH}/src/github.com/elastic/beats status --porcelain) && [ ! -z "$${output}" ]; then printf "\033[31mWARNING: elastic/beats has uncommitted changes, these will not be in the vendor directory!\033[0m\n"; fi
copy-vendor:
mage vendorUpdate
10 changes: 5 additions & 5 deletions generator/beat/{beat}/beater/{beat}.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"{beat_path}/config"
)

// {Beat} configuration.
type {Beat} struct {
// {beat} configuration.
type {beat} struct {
done chan struct{}
config config.Config
client beat.Client
Expand All @@ -25,15 +25,15 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
return nil, fmt.Errorf("Error reading config file: %v", err)
}

bt := &{Beat}{
bt := &{beat}{
done: make(chan struct{}),
config: c,
}
return bt, nil
}

// Run starts {beat}.
func (bt *{Beat}) Run(b *beat.Beat) error {
func (bt *{beat}) Run(b *beat.Beat) error {
logp.Info("{beat} is running! Hit CTRL-C to stop it.")

var err error
Expand Down Expand Up @@ -65,7 +65,7 @@ func (bt *{Beat}) Run(b *beat.Beat) error {
}

// Stop stops {beat}.
func (bt *{Beat}) Stop() {
func (bt *{beat}) Stop() {
bt.client.Close()
close(bt.done)
}
28 changes: 20 additions & 8 deletions generator/beat/{beat}/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import (
"time"

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

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

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

func init() {
Expand All @@ -31,6 +33,11 @@ func init() {
devtools.BeatVendor = "{full_name}"
}

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

// Package packages the Beat for distribution.
// Use SNAPSHOT=true to build snapshots.
// Use PLATFORMS to control the target platforms.
Expand All @@ -40,17 +47,22 @@ func Package() {

devtools.UseCommunityBeatPackaging()

mg.Deps(update.Update)
mg.Deps(Update)
mg.Deps(build.CrossBuild, build.CrossBuildGoDaemon)
mg.SerialDeps(devtools.Package, pkg.PackageTest)
}

// Config generates both the short/reference/docker configs.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, devtools.ConfigFileParams{}, ".")
// Update updates the generated files (aka make update).
func Update() error {
return sh.Run("make", "update")
}

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

// Config generates both the short/reference/docker configs.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, devtools.ConfigFileParams{}, ".")
}
52 changes: 20 additions & 32 deletions generator/common/Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
BUILD_DIR?=build
PWD=$(shell pwd)
PYTHON_ENV?=${BUILD_DIR}/python-env
BEAT_TYPE?=beat
BEAT_PATH=${BUILD_DIR}/src/beatpath/testbeat
BEAT_NAME?=beatpath/test${BEAT_TYPE}
BEAT_PATH=${GOPATH}/src/${BEAT_NAME}
ES_BEATS=${GOPATH}/src/github.com/elastic/beats
PREPARE_COMMAND?=

# Runs test build for mock beat
.PHONY: test
test: prepare-test
. ${PYTHON_ENV}/bin/activate; \
export GOPATH=${PWD}/build ; \
export PATH=$${GOPATH}/bin:${PATH}; \
cd ${BEAT_PATH} ; \
$(MAKE) copy-vendor || exit 1 ; \
${PREPARE_COMMAND} \
$(MAKE) git-init || exit 1 ; \
$(MAKE) update fmt || exit 1 ; \
export PATH=$${GOPATH}/bin:$${PATH}; \
git config user.email "beats-jenkins@test.com" || exit 1 ; \
git config user.name "beats-jenkins" || 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
rm -fr ${BUILD_DIR}/src/github.com/elastic/beats/
git clone -s ${PWD}/../../ ${BUILD_DIR}/src/github.com/elastic/beats/

prepare-test:: ${GOPATH}/bin/mage
rm -fr ${BEAT_PATH}
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"
cd ${GOPATH}/src/github.com/elastic/beats/ ; \
export MODULE=elastic ; \
export METRICSET=test ; \
export GO111MODULE=off; \
export NEWBEAT_PROJECT_NAME=Testbeat ; \
export NEWBEAT_GITHUB_NAME=ruflin ; \
export NEWBEAT_BEAT_PATH=${BEAT_NAME} ; \
export NEWBEAT_FULL_NAME="Nicolas Ruflin" ; \
export NEWBEAT_TYPE=${BEAT_TYPE} ; \
export NEWBEAT_DEV=1 ; \
mage GenerateCustomBeat

# Runs test build for the created beat
.PHONY: test-build
Expand All @@ -51,15 +42,12 @@ test-build: test
$(MAKE) deps ; \
$(MAKE) images

# Sets up the virtual python environment
.PHONY: python-env
python-env:
@test -d ${PYTHON_ENV} || virtualenv ${PYTHON_ENV}
@${PYTHON_ENV}/bin/pip install --upgrade pip PyYAML
@# Work around pip bug. See: https://github.com/pypa/pip/issues/4464
@find $(PYTHON_ENV) -type d -name dist-packages -exec sh -c "echo dist-packages > {}.pth" ';'
${GOPATH}/bin/mage:
go get -u -d github.com/magefile/mage
cd $${GOPATH}/src/github.com/magefile/mage; \
go run bootstrap.go

# Cleans up environment
.PHONY: clean
clean:
@rm -rf build
rm -rf ${BEAT_PATH}
Loading