From 8a81e921fd4bbeab99f7ed97337d00fabcf96d4f Mon Sep 17 00:00:00 2001 From: ruflin Date: Fri, 11 Nov 2016 15:49:34 +0100 Subject: [PATCH] Replace cookiecutter to generate beats by internal script Currently cookiecutter is needed to generate a beat. This adds an additional dependency to generate a beat and does not give us a lot of control over which checks should be executed during execution. As our template is quite simple it can be done with basic string replacements. A simple python script is now done for the task which leads to less inputs needed by the user and allows us to do checks like GOPATH setup etc. The script could potentially be done in Golang but the setup of python is needed anyways for development. Further changes: * Rename .go files to .go.tmpl so they are not checked for correct syntax * Update documentation NOTES: * Changes for the metricbeat generator will follow in a second PR. * Blog post about creating a beat should be updated to point to 5.1. See https://github.com/elastic/beats/pull/3197 --- generate/beat.py | 107 ++++++++++++++++++ generate/beat/CHANGELOG.md | 1 + generate/beat/Makefile | 16 +-- generate/beat/cookiecutter.json | 7 -- generate/beat/tests/cookiecutter.json | 7 -- generate/beat/{beat}/.gitignore | 7 ++ .../.travis.yml | 8 +- .../CONTRIBUTING.md | 0 .../{{{cookiecutter.beat}} => {beat}}/LICENSE | 2 +- .../Makefile | 6 +- .../README.md | 36 +++--- generate/beat/{beat}/_meta/beat.yml | 7 ++ .../_meta/fields.yml | 4 +- .../beater/{beat}.go.tmpl} | 14 +-- .../config/config.go.tmpl} | 0 .../config/config_test.go.tmpl} | 0 generate/beat/{beat}/docs/index.asciidoc | 5 + .../main.go => {beat}/main.go.tmpl} | 4 +- .../main_test.go => {beat}/main_test.go.tmpl} | 2 +- .../tests/system/config/{beat}.yml.j2} | 10 +- .../tests/system/requirements.txt | 0 .../beat/{beat}/tests/system/test_base.py | 19 ++++ .../tests/system/{beat}.py} | 4 +- .../beat/{{cookiecutter.beat}}/.gitignore | 7 -- .../beat/{{cookiecutter.beat}}/_meta/beat.yml | 7 -- .../{{cookiecutter.beat}}/docs/index.asciidoc | 5 - .../tests/system/test_base.py | 19 ---- .../{{cookiecutter.beat}}.template.json | 0 libbeat/docs/newbeat.asciidoc | 14 +-- 29 files changed, 202 insertions(+), 116 deletions(-) create mode 100644 generate/beat.py delete mode 100644 generate/beat/cookiecutter.json delete mode 100644 generate/beat/tests/cookiecutter.json create mode 100644 generate/beat/{beat}/.gitignore rename generate/beat/{{{cookiecutter.beat}} => {beat}}/.travis.yml (63%) rename generate/beat/{{{cookiecutter.beat}} => {beat}}/CONTRIBUTING.md (100%) rename generate/beat/{{{cookiecutter.beat}} => {beat}}/LICENSE (91%) rename generate/beat/{{{cookiecutter.beat}} => {beat}}/Makefile (88%) rename generate/beat/{{{cookiecutter.beat}} => {beat}}/README.md (56%) create mode 100644 generate/beat/{beat}/_meta/beat.yml rename generate/beat/{{{cookiecutter.beat}} => {beat}}/_meta/fields.yml (68%) rename generate/beat/{{{cookiecutter.beat}}/beater/{{cookiecutter.beat}}.go => {beat}/beater/{beat}.go.tmpl} (72%) rename generate/beat/{{{cookiecutter.beat}}/config/config.go => {beat}/config/config.go.tmpl} (100%) rename generate/beat/{{{cookiecutter.beat}}/config/config_test.go => {beat}/config/config_test.go.tmpl} (100%) create mode 100644 generate/beat/{beat}/docs/index.asciidoc rename generate/beat/{{{cookiecutter.beat}}/main.go => {beat}/main.go.tmpl} (51%) rename generate/beat/{{{cookiecutter.beat}}/main_test.go => {beat}/main_test.go.tmpl} (74%) rename generate/beat/{{{cookiecutter.beat}}/tests/system/config/{{cookiecutter.beat}}.yml.j2 => {beat}/tests/system/config/{beat}.yml.j2} (86%) rename generate/beat/{{{cookiecutter.beat}} => {beat}}/tests/system/requirements.txt (100%) create mode 100644 generate/beat/{beat}/tests/system/test_base.py rename generate/beat/{{{cookiecutter.beat}}/tests/system/{{cookiecutter.beat}}.py => {beat}/tests/system/{beat}.py} (69%) delete mode 100644 generate/beat/{{cookiecutter.beat}}/.gitignore delete mode 100644 generate/beat/{{cookiecutter.beat}}/_meta/beat.yml delete mode 100644 generate/beat/{{cookiecutter.beat}}/docs/index.asciidoc delete mode 100644 generate/beat/{{cookiecutter.beat}}/tests/system/test_base.py delete mode 100644 generate/beat/{{cookiecutter.beat}}/{{cookiecutter.beat}}.template.json diff --git a/generate/beat.py b/generate/beat.py new file mode 100644 index 00000000000..f4602fe3108 --- /dev/null +++ b/generate/beat.py @@ -0,0 +1,107 @@ +import os +import argparse + +# Creates a new beat based on the given parameters + +project_name = "" +github_name = "" +beat = "" +beat_path = "" +full_name = "" + +def generate_beat(): + read_input() + process_file() + +def read_input(): + """Requests input form the command line for empty variables if needed. + """ + global project_name, github_name, beat, beat_path, full_name + + if project_name == "": + project_name = raw_input("Beat Name [Examplebeat]: ") or "examplebeat" + + if github_name == "": + github_name = raw_input("Your Github Name [your-github-name]: ") or "your-github-name" + beat = project_name.lower() + + if beat_path == "": + beat_path = raw_input("Beat Path [github.com/" + github_name + "]: ") or "github.com/" + github_name + + if full_name == "": + full_name = raw_input("Firstname Lastname: ") or "Firstname Lastname" + +def process_file(): + + # Load path information + generator_path = os.path.dirname(os.path.realpath(__file__)) + go_path = os.environ['GOPATH'] + + for root, dirs, files in os.walk(generator_path + '/beat/{beat}'): + + for file in files: + + full_path = root + "/" + file + + ## load file + content = "" + with open(full_path) as f: + content = f.read() + + # process content + content = replace_variables(content) + + # Write new path + new_path = replace_variables(full_path).replace(".go.tmpl", ".go") + + # remove generator info from path + file_path = new_path.replace(generator_path + "/beat/", "") + + # New file path to write file content to + write_file = go_path + "/src/" + beat_path + "/" + file_path + + # Create parent directory if it does not exist yet + dir = os.path.dirname(write_file) + if not os.path.exists(dir): + os.makedirs(dir) + + # Write file to new location + with open(write_file, 'w') as f: + f.write(content) + +def replace_variables(content): + """Replace all template variables with the actual values + """ + return content.replace("{project_name}", project_name) \ + .replace("{github_name}", github_name) \ + .replace("{beat}", beat) \ + .replace("{Beat}", beat.capitalize()) \ + .replace("{beat_path}", beat_path) \ + .replace("{full_name}", full_name) + + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description="Creates a beat") + parser.add_argument("--project_name", help="Project name") + parser.add_argument("--github_name", help="Github name") + parser.add_argument("--beat_path", help="Beat path") + parser.add_argument("--full_name", help="Full name") + + args = parser.parse_args() + + + if args.project_name is not None: + project_name = args.project_name + + if args.github_name is not None: + github_name = args.github_name + + if args.beat_path is not None: + beat_path = args.beat_path + + if args.full_name is not None: + full_name = args.full_name + + generate_beat() + diff --git a/generate/beat/CHANGELOG.md b/generate/beat/CHANGELOG.md index eca3f510b45..f5955695243 100644 --- a/generate/beat/CHANGELOG.md +++ b/generate/beat/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file based on the - Make testing dependent on local version of beats ### Backward Compatibility Breaks +- Remove dependency on cookiecutter ### Bugfixes diff --git a/generate/beat/Makefile b/generate/beat/Makefile index 2cf887b891a..ae52948da55 100644 --- a/generate/beat/Makefile +++ b/generate/beat/Makefile @@ -8,17 +8,13 @@ ES_BEATS=${GOPATH}/src/github.com/elastic/beats .PHONY: test test: python-env - # Create copy of cookiecutter for building with defaults - mkdir -p build/src/beatpath - cp -r \{\{cookiecutter.beat\}\} build - cp tests/cookiecutter.json build/ - - mkdir -p ${BEAT_PATH} - . ${PYTHON_ENV}/bin/activate && cookiecutter --no-input -o build/src/beatpath -f build ; - # Makes sure to use current version of beats for testing mkdir -p ${BUILD_DIR}/src/github.com/elastic/beats/ - rsync -a --exclude=generate ${PWD}/../../* ${BUILD_DIR}/src/github.com/elastic/beats/ + rsync -a --exclude=build ${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/generate/beat.py --project_name=Testbeat --github_name=ruflin --beat_path=beatpath --full_name="Nicolas Ruflin" . ${PYTHON_ENV}/bin/activate; \ export GOPATH=${PWD}/build ; \ @@ -45,7 +41,7 @@ test-build: test .PHONY: python-env python-env: test -d ${PYTHON_ENV} || virtualenv ${PYTHON_ENV} - . ${PYTHON_ENV}/bin/activate && pip install --upgrade pip cookiecutter PyYAML + . ${PYTHON_ENV}/bin/activate && pip install --upgrade pip PyYAML # Cleans up environment .PHONY: clean diff --git a/generate/beat/cookiecutter.json b/generate/beat/cookiecutter.json deleted file mode 100644 index a6fcaf047bb..00000000000 --- a/generate/beat/cookiecutter.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "project_name": "Examplebeat", - "github_name": "your-github-name", - "beat": "{{ cookiecutter.project_name|lower }}", - "beat_path": "github.com/{{ cookiecutter.github_name|lower }}", - "full_name": "Firstname Lastname" -} diff --git a/generate/beat/tests/cookiecutter.json b/generate/beat/tests/cookiecutter.json deleted file mode 100644 index 2d658a84353..00000000000 --- a/generate/beat/tests/cookiecutter.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "project_name": "Testbeat", - "github_name": "ruflin", - "beat": "{{ cookiecutter.project_name|lower }}", - "beat_path": "beatpath", - "full_name": "Nicolas Ruflin" -} diff --git a/generate/beat/{beat}/.gitignore b/generate/beat/{beat}/.gitignore new file mode 100644 index 00000000000..3c32225685d --- /dev/null +++ b/generate/beat/{beat}/.gitignore @@ -0,0 +1,7 @@ +/.idea +/build + +.DS_Store +/{beat} +/{beat}.test +*.pyc diff --git a/generate/beat/{{cookiecutter.beat}}/.travis.yml b/generate/beat/{beat}/.travis.yml similarity index 63% rename from generate/beat/{{cookiecutter.beat}}/.travis.yml rename to generate/beat/{beat}/.travis.yml index 1bf1abd5ee0..71d8c577a4c 100644 --- a/generate/beat/{{cookiecutter.beat}}/.travis.yml +++ b/generate/beat/{beat}/.travis.yml @@ -28,10 +28,10 @@ addons: before_install: # Redo the travis setup but with the elastic/libbeat path. This is needed so the package path is correct - - mkdir -p $HOME/gopath/src/github.com/{{cookiecutter.github_name|lower}}/{{cookiecutter.beat}}/ - - rsync -az ${TRAVIS_BUILD_DIR}/ $HOME/gopath/src/github.com/{{cookiecutter.github_name|lower}}/{{cookiecutter.beat}}/ - - export TRAVIS_BUILD_DIR=$HOME/gopath/src/github.com/{{cookiecutter.github_name|lower}}/{{cookiecutter.beat}}/ - - cd $HOME/gopath/src/github.com/{{cookiecutter.github_name|lower}}/{{cookiecutter.beat}}/ + - mkdir -p $HOME/gopath/src/github.com/{github_name}/{beat}/ + - rsync -az ${TRAVIS_BUILD_DIR}/ $HOME/gopath/src/github.com/{github_name}/{beat}/ + - export TRAVIS_BUILD_DIR=$HOME/gopath/src/github.com/{github_name}/{beat}/ + - cd $HOME/gopath/src/github.com/{github_name}/{beat}/ install: - true diff --git a/generate/beat/{{cookiecutter.beat}}/CONTRIBUTING.md b/generate/beat/{beat}/CONTRIBUTING.md similarity index 100% rename from generate/beat/{{cookiecutter.beat}}/CONTRIBUTING.md rename to generate/beat/{beat}/CONTRIBUTING.md diff --git a/generate/beat/{{cookiecutter.beat}}/LICENSE b/generate/beat/{beat}/LICENSE similarity index 91% rename from generate/beat/{{cookiecutter.beat}}/LICENSE rename to generate/beat/{beat}/LICENSE index acb13bbf6d4..fff2fbe40eb 100644 --- a/generate/beat/{{cookiecutter.beat}}/LICENSE +++ b/generate/beat/{beat}/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2016 {{cookiecutter.full_name}} +Copyright (c) 2016 {full_name} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/generate/beat/{{cookiecutter.beat}}/Makefile b/generate/beat/{beat}/Makefile similarity index 88% rename from generate/beat/{{cookiecutter.beat}}/Makefile rename to generate/beat/{beat}/Makefile index 03850820d19..2345643e45e 100644 --- a/generate/beat/{{cookiecutter.beat}}/Makefile +++ b/generate/beat/{beat}/Makefile @@ -1,5 +1,5 @@ -BEATNAME={{cookiecutter.beat}} -BEAT_DIR={{cookiecutter.beat_path}}/{{cookiecutter.beat}} +BEATNAME={beat} +BEAT_DIR={beat_path}/{beat} BEAT_GOPATH=$(firstword $(subst :, ,${GOPATH})) SYSTEM_TESTS=false TEST_ENVIRONMENT=false @@ -33,7 +33,7 @@ git-init: git commit -m "Add git settings" git add . git reset -- .travis.yml - git commit -m "Add {{cookiecutter.beat}}" + git commit -m "Add {beat}" git add .travis.yml git commit -m "Add Travis CI" diff --git a/generate/beat/{{cookiecutter.beat}}/README.md b/generate/beat/{beat}/README.md similarity index 56% rename from generate/beat/{{cookiecutter.beat}}/README.md rename to generate/beat/{beat}/README.md index 9bc83becb26..0963d4dc3a0 100644 --- a/generate/beat/{{cookiecutter.beat}}/README.md +++ b/generate/beat/{beat}/README.md @@ -1,18 +1,18 @@ -# {{cookiecutter.beat|capitalize}} +# {Beat} -Welcome to {{cookiecutter.beat|capitalize}}. +Welcome to {Beat}. Ensure that this folder is at the following location: -`${GOPATH}/{{cookiecutter.beat_path}}` +`${GOPATH}/{beat_path}` -## Getting Started with {{cookiecutter.beat|capitalize}} +## Getting Started with {Beat} ### Requirements * [Golang](https://golang.org/dl/) 1.7 ### Init Project -To get running with {{cookiecutter.beat|capitalize}} and also install the +To get running with {Beat} and also install the dependencies, run the following command: ``` @@ -21,10 +21,10 @@ make setup It will create a clean git history for each major step. Note that you can always rewrite the history if you wish before pushing your changes. -To push {{cookiecutter.beat|capitalize}} in the git repository, run the following commands: +To push {Beat} in the git repository, run the following commands: ``` -git remote set-url origin https://{{cookiecutter.beat_path}}/{{cookiecutter.beat}} +git remote set-url origin https://{beat_path}/{beat} git push origin master ``` @@ -32,8 +32,8 @@ For further development, check out the [beat developer guide](https://www.elasti ### Build -To build the binary for {{cookiecutter.beat|capitalize}} run the command below. This will generate a binary -in the same directory with the name {{cookiecutter.beat}}. +To build the binary for {Beat} run the command below. This will generate a binary +in the same directory with the name {beat}. ``` make @@ -42,16 +42,16 @@ make ### Run -To run {{cookiecutter.beat|capitalize}} with debugging output enabled, run: +To run {Beat} with debugging output enabled, run: ``` -./{{cookiecutter.beat}} -c {{cookiecutter.beat}}.yml -e -d "*" +./{beat} -c {beat}.yml -e -d "*" ``` ### Test -To test {{cookiecutter.beat|capitalize}}, run the following command: +To test {Beat}, run the following command: ``` make testsuite @@ -71,7 +71,7 @@ The test coverage is reported in the folder `./build/coverage/` Each beat has a template for the mapping in elasticsearch and a documentation for the fields which is automatically generated based on `etc/fields.yml`. -To generate etc/{{cookiecutter.beat}}.template.json and etc/{{cookiecutter.beat}}.asciidoc +To generate etc/{beat}.template.json and etc/{beat}.asciidoc ``` make update @@ -80,7 +80,7 @@ make update ### Cleanup -To clean {{cookiecutter.beat|capitalize}} source code, run the following commands: +To clean {Beat} source code, run the following commands: ``` make fmt @@ -96,12 +96,12 @@ make clean ### Clone -To clone {{cookiecutter.beat|capitalize}} from the git repository, run the following commands: +To clone {Beat} from the git repository, run the following commands: ``` -mkdir -p ${GOPATH%%:*}/{{cookiecutter.beat_path}} -cd ${GOPATH%%:*}/{{cookiecutter.beat_path}} -git clone https://{{cookiecutter.beat_path}}/{{cookiecutter.beat}} +mkdir -p ${GOPATH}/{beat_path} +cd ${GOPATH}/{beat_path} +git clone https://{beat_path}/{beat} ``` diff --git a/generate/beat/{beat}/_meta/beat.yml b/generate/beat/{beat}/_meta/beat.yml new file mode 100644 index 00000000000..7d9bb3942c3 --- /dev/null +++ b/generate/beat/{beat}/_meta/beat.yml @@ -0,0 +1,7 @@ +################### {Beat} Configuration Example ######################### + +############################# {Beat} ###################################### + +{beat}: + # Defines how often an event is sent to the output + period: 1s diff --git a/generate/beat/{{cookiecutter.beat}}/_meta/fields.yml b/generate/beat/{beat}/_meta/fields.yml similarity index 68% rename from generate/beat/{{cookiecutter.beat}}/_meta/fields.yml rename to generate/beat/{beat}/_meta/fields.yml index a65947a7821..a2ffb457d8a 100644 --- a/generate/beat/{{cookiecutter.beat}}/_meta/fields.yml +++ b/generate/beat/{beat}/_meta/fields.yml @@ -1,5 +1,5 @@ -- key: {{ cookiecutter.beat }} - title: {{ cookiecutter.beat }} +- key: {beat} + title: {beat} description: fields: - name: counter diff --git a/generate/beat/{{cookiecutter.beat}}/beater/{{cookiecutter.beat}}.go b/generate/beat/{beat}/beater/{beat}.go.tmpl similarity index 72% rename from generate/beat/{{cookiecutter.beat}}/beater/{{cookiecutter.beat}}.go rename to generate/beat/{beat}/beater/{beat}.go.tmpl index cc8c8794fa7..6f946f5e0d3 100644 --- a/generate/beat/{{cookiecutter.beat}}/beater/{{cookiecutter.beat}}.go +++ b/generate/beat/{beat}/beater/{beat}.go.tmpl @@ -9,10 +9,10 @@ import ( "github.com/elastic/beats/libbeat/logp" "github.com/elastic/beats/libbeat/publisher" - "{{cookiecutter.beat_path}}/{{cookiecutter.beat}}/config" + "{beat_path}/{beat}/config" ) -type {{cookiecutter.beat|capitalize}} struct { +type {Beat} struct { done chan struct{} config config.Config client publisher.Client @@ -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 := &{{cookiecutter.beat|capitalize}}{ - done: make(chan struct{}), + bt := &{Beat}{ + done: make(chan struct{}), config: config, } return bt, nil } -func (bt *{{cookiecutter.beat|capitalize}}) Run(b *beat.Beat) error { - logp.Info("{{cookiecutter.beat}} is running! Hit CTRL-C to stop it.") +func (bt *{Beat}) Run(b *beat.Beat) error { + logp.Info("{beat} is running! Hit CTRL-C to stop it.") bt.client = b.Publisher.Connect() ticker := time.NewTicker(bt.config.Period) @@ -56,7 +56,7 @@ func (bt *{{cookiecutter.beat|capitalize}}) Run(b *beat.Beat) error { } } -func (bt *{{cookiecutter.beat|capitalize}}) Stop() { +func (bt *{Beat}) Stop() { bt.client.Close() close(bt.done) } diff --git a/generate/beat/{{cookiecutter.beat}}/config/config.go b/generate/beat/{beat}/config/config.go.tmpl similarity index 100% rename from generate/beat/{{cookiecutter.beat}}/config/config.go rename to generate/beat/{beat}/config/config.go.tmpl diff --git a/generate/beat/{{cookiecutter.beat}}/config/config_test.go b/generate/beat/{beat}/config/config_test.go.tmpl similarity index 100% rename from generate/beat/{{cookiecutter.beat}}/config/config_test.go rename to generate/beat/{beat}/config/config_test.go.tmpl diff --git a/generate/beat/{beat}/docs/index.asciidoc b/generate/beat/{beat}/docs/index.asciidoc new file mode 100644 index 00000000000..3475e7d069c --- /dev/null +++ b/generate/beat/{beat}/docs/index.asciidoc @@ -0,0 +1,5 @@ += {Beat} Docs + +Welcome to the {Beat} documentation. + + diff --git a/generate/beat/{{cookiecutter.beat}}/main.go b/generate/beat/{beat}/main.go.tmpl similarity index 51% rename from generate/beat/{{cookiecutter.beat}}/main.go rename to generate/beat/{beat}/main.go.tmpl index 5da85f1beef..6919fddbb24 100644 --- a/generate/beat/{{cookiecutter.beat}}/main.go +++ b/generate/beat/{beat}/main.go.tmpl @@ -5,11 +5,11 @@ import ( "github.com/elastic/beats/libbeat/beat" - "{{cookiecutter.beat_path}}/{{cookiecutter.beat}}/beater" + "{beat_path}/{beat}/beater" ) func main() { - err := beat.Run("{{cookiecutter.beat}}", "", beater.New) + err := beat.Run("{beat}", "", beater.New) if err != nil { os.Exit(1) } diff --git a/generate/beat/{{cookiecutter.beat}}/main_test.go b/generate/beat/{beat}/main_test.go.tmpl similarity index 74% rename from generate/beat/{{cookiecutter.beat}}/main_test.go rename to generate/beat/{beat}/main_test.go.tmpl index 310ff50fece..9000c23e1bd 100644 --- a/generate/beat/{{cookiecutter.beat}}/main_test.go +++ b/generate/beat/{beat}/main_test.go.tmpl @@ -1,6 +1,6 @@ package main -// This file is mandatory as otherwise the {{cookiecutter.beat}}.test binary is not generated correctly. +// This file is mandatory as otherwise the {beat}.test binary is not generated correctly. import ( "flag" diff --git a/generate/beat/{{cookiecutter.beat}}/tests/system/config/{{cookiecutter.beat}}.yml.j2 b/generate/beat/{beat}/tests/system/config/{beat}.yml.j2 similarity index 86% rename from generate/beat/{{cookiecutter.beat}}/tests/system/config/{{cookiecutter.beat}}.yml.j2 rename to generate/beat/{beat}/tests/system/config/{beat}.yml.j2 index e9507274067..9a2bc9223e4 100644 --- a/generate/beat/{{cookiecutter.beat}}/tests/system/config/{{cookiecutter.beat}}.yml.j2 +++ b/generate/beat/{beat}/tests/system/config/{beat}.yml.j2 @@ -17,9 +17,9 @@ output: path: {{ '{{' }} output_file_path|default(beat.working_dir + "/output") {{ '}}' }} - # Name of the generated files. The default is `{{cookiecutter.beat}}` and it generates - # files: `{{cookiecutter.beat}}`, `{{cookiecutter.beat}}.1`, `{{cookiecutter.beat}}.2`, etc. - filename: "{{ '{{' }} output_file_filename|default("{{cookiecutter.beat}}") {{ '}}' }}" + # Name of the generated files. The default is `{beat}` and it generates + # files: `{beat}`, `{beat}.1`, `{beat}.2`, etc. + filename: "{{ '{{' }} output_file_filename|default("{beat}") {{ '}}' }}" # Maximum size in kilobytes of each file. When this size is reached, the files are # rotated. The default value is 10 MB. @@ -65,10 +65,10 @@ output: #files: # The directory where the log files will written to. - #path: /var/log/{{cookiecutter.beat}} + #path: /var/log/{beat} # The name of the files where the logs are written to. - #name: {{cookiecutter.beat}} + #name: {beat} # Configure log file size limit. If limit is reached, log file will be # automatically rotated diff --git a/generate/beat/{{cookiecutter.beat}}/tests/system/requirements.txt b/generate/beat/{beat}/tests/system/requirements.txt similarity index 100% rename from generate/beat/{{cookiecutter.beat}}/tests/system/requirements.txt rename to generate/beat/{beat}/tests/system/requirements.txt diff --git a/generate/beat/{beat}/tests/system/test_base.py b/generate/beat/{beat}/tests/system/test_base.py new file mode 100644 index 00000000000..c8501419124 --- /dev/null +++ b/generate/beat/{beat}/tests/system/test_base.py @@ -0,0 +1,19 @@ +from {beat} import BaseTest + +import os + + +class Test(BaseTest): + + def test_base(self): + """ + Basic test with exiting {Beat} normally + """ + self.render_config_template( + path=os.path.abspath(self.working_dir) + "/log/*" + ) + + {beat}_proc = self.start_beat() + self.wait_until( lambda: self.log_contains("{beat} is running")) + exit_code = {beat}_proc.kill_and_wait() + assert exit_code == 0 diff --git a/generate/beat/{{cookiecutter.beat}}/tests/system/{{cookiecutter.beat}}.py b/generate/beat/{beat}/tests/system/{beat}.py similarity index 69% rename from generate/beat/{{cookiecutter.beat}}/tests/system/{{cookiecutter.beat}}.py rename to generate/beat/{beat}/tests/system/{beat}.py index 460906dbe11..1e83d93370d 100644 --- a/generate/beat/{{cookiecutter.beat}}/tests/system/{{cookiecutter.beat}}.py +++ b/generate/beat/{beat}/tests/system/{beat}.py @@ -6,6 +6,6 @@ class BaseTest(TestCase): @classmethod def setUpClass(self): - self.beat_name = "{{cookiecutter.beat}}" + self.beat_name = "{beat}" self.build_path = "../../build/system-tests/" - self.beat_path = "../../{{cookiecutter.beat}}.test" + self.beat_path = "../../{beat}.test" diff --git a/generate/beat/{{cookiecutter.beat}}/.gitignore b/generate/beat/{{cookiecutter.beat}}/.gitignore deleted file mode 100644 index f00cc78194a..00000000000 --- a/generate/beat/{{cookiecutter.beat}}/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -/.idea -/build - -.DS_Store -/{{cookiecutter.beat}} -/{{cookiecutter.beat}}.test -*.pyc diff --git a/generate/beat/{{cookiecutter.beat}}/_meta/beat.yml b/generate/beat/{{cookiecutter.beat}}/_meta/beat.yml deleted file mode 100644 index d30ebde55b4..00000000000 --- a/generate/beat/{{cookiecutter.beat}}/_meta/beat.yml +++ /dev/null @@ -1,7 +0,0 @@ -################### {{cookiecutter.beat|capitalize}} Configuration Example ######################### - -############################# {{cookiecutter.beat|capitalize}} ###################################### - -{{cookiecutter.beat}}: - # Defines how often an event is sent to the output - period: 1s diff --git a/generate/beat/{{cookiecutter.beat}}/docs/index.asciidoc b/generate/beat/{{cookiecutter.beat}}/docs/index.asciidoc deleted file mode 100644 index e4ee80284dc..00000000000 --- a/generate/beat/{{cookiecutter.beat}}/docs/index.asciidoc +++ /dev/null @@ -1,5 +0,0 @@ -= {{cookiecutter.beat|capitalize}} Docs - -Welcome to the {{cookiecutter.beat|capitalize}} documentation. - - diff --git a/generate/beat/{{cookiecutter.beat}}/tests/system/test_base.py b/generate/beat/{{cookiecutter.beat}}/tests/system/test_base.py deleted file mode 100644 index fd3d7c88ebf..00000000000 --- a/generate/beat/{{cookiecutter.beat}}/tests/system/test_base.py +++ /dev/null @@ -1,19 +0,0 @@ -from {{cookiecutter.beat}} import BaseTest - -import os - - -class Test(BaseTest): - - def test_base(self): - """ - Basic test with exiting {{cookiecutter.beat|capitalize}} normally - """ - self.render_config_template( - path=os.path.abspath(self.working_dir) + "/log/*" - ) - - {{cookiecutter.beat|lower}}_proc = self.start_beat() - self.wait_until( lambda: self.log_contains("{{cookiecutter.beat}} is running")) - exit_code = {{cookiecutter.beat|lower}}_proc.kill_and_wait() - assert exit_code == 0 diff --git a/generate/beat/{{cookiecutter.beat}}/{{cookiecutter.beat}}.template.json b/generate/beat/{{cookiecutter.beat}}/{{cookiecutter.beat}}.template.json deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/libbeat/docs/newbeat.asciidoc b/libbeat/docs/newbeat.asciidoc index f9d6e931400..a10c8bb5b82 100644 --- a/libbeat/docs/newbeat.asciidoc +++ b/libbeat/docs/newbeat.asciidoc @@ -125,11 +125,8 @@ Now that you have the big picture, let's dig into the code. To generate your own Beat, you use the Beat generator available in the beats repo on GitHub. If you haven't downloaded the Beats source code yet, follow the instructions in <>. -Before running the Beat generator, you must install https://github.com/audreyr/cookiecutter[cookiecutter], a -command-line utility that creates projects from project templates. Check out the -http://cookiecutter.readthedocs.io/en/latest/installation.html[installation guide]. After installing cookiecutter, -decide on a name for your beat. The name should be one word with the first letter capitalized. In our example, -we use `Countbeat`. +Before running beat generator, you must decide on a name for your beat. The name should be one word with +the first letter capitalized. In our example, we use `Countbeat`. Now create a directory under $GOPATH for your repository and change to the new directory: @@ -139,21 +136,20 @@ mkdir ${GOPATH%%:*}/src/github.com/{user} cd ${GOPATH%%:*}/src/github.com/{user} -------------------- -Run cookiecutter and specify the path to the Beat generator: +Run python and specify the path to the Beat generator: [source,shell] -------------------- -cookiecutter ${GOPATH%%:*}/src/github.com/elastic/beats/generate/beat +python $GOPATH/src/github.com/elastic/beats/generate/beat.py -------------------- -Cookiecutter will prompt you to enter information about your Beat. For the `project_name`, enter `Countbeat`. +Python 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] --------- project_name [Examplebeat]: Countbeat github_name [your-github-name]: {username} -beat [countbeat]: beat_path [github.com/{github id}]: full_name [Firstname Lastname]: {Full Name} ---------