Skip to content

Instantly share code, notes, and snippets.

@asieira
Last active September 15, 2017 17:41
Show Gist options
  • Save asieira/f920c639cc5abb105a9601185ca56b51 to your computer and use it in GitHub Desktop.
Save asieira/f920c639cc5abb105a9601185ca56b51 to your computer and use it in GitHub Desktop.
A *nix and OS X Makefile to help package and test Splunk Apps with Python code in them
APPDIR = TA-Niddel_Magnet_Alerts_Add-on
DOCKER_TMP = /tmp/splunk
DOCKER_TAG = 6.6.2
DOCKER_NAME = splunk
SPLUNK_CREDENTIALS = 'admin:changeme'
VERSION = $(shell grep '^version' $(APPDIR)/default/app.conf | egrep -o '[^ ]+$$')
BUILD = $(shell grep '^build' $(APPDIR)/default/app.conf | egrep -o '[^ ]+$$')
FILE = $(APPDIR)-$(VERSION)-$(BUILD).spl
PYTHON_PACKAGES := $(shell find $(APPDIR)/bin/ -maxdepth 1 -type d | tr '\n' ',')
spl: package/$(FILE)
package/$(FILE): $(shell find $(APPDIR)) .gitignore
find $(APPDIR)/bin/ -name "*.py" -exec chmod +x {} \;
mkdir -p package
export COPYFILE_DISABLE=1 ; tar -czvf "package/$(FILE)" -X .gitignore $(APPDIR)/
test: manifest pep8 appinspect
pep8:
pep8 --max-line-length=120 --exclude $(PYTHON_PACKAGES) --ignore=E402,E126 $(APPDIR)/
manifest:
source /usr/local/bin/virtualenvwrapper.sh ; workon slim ; slim validate $(APPDIR)/
appinspect: package/$(FILE)
splunk-appinspect inspect "package/$(FILE)" --mode test --included-tags splunk_appinspect
docker: package/$(FILE) docker_container
docker cp "package/$(FILE)" $(DOCKER_NAME):/tmp/
docker exec $(DOCKER_NAME) entrypoint.sh splunk install app "/tmp/$(FILE)" -update 1 -auth $(SPLUNK_CREDENTIALS)
docker exec $(DOCKER_NAME) entrypoint.sh splunk restart
docker_container: package/eventgen-develop.tar.gz
if [ ! "$$(docker ps -qa -f name=$(DOCKER_NAME))" ]; then \
docker run -d --name $(DOCKER_NAME) --hostname $(DOCKER_NAME) \
-e "SPLUNK_START_ARGS=--accept-license" -e "SPLUNK_USER=root" -p "8000:8000" \
-v $(DOCKER_TMP)/$(APPDIR):/opt/splunk/etc/apps/$(APPDIR) \
-v $(DOCKER_TMP)/log:/opt/splunk/var/log \
splunk/splunk:$(DOCKER_TAG) ;\
wget --retry-connrefused --tries=30 -q --wait=2 --spider http://localhost:8000 ;\
docker cp "package/eventgen-develop.tar.gz" $(DOCKER_NAME):/tmp/ ;\
docker exec $(DOCKER_NAME) entrypoint.sh splunk install app "/tmp/eventgen-develop.tar.gz" -update 1 -auth $(SPLUNK_CREDENTIALS) ;\
elif [ ! "$$(docker ps -q -f name=$(DOCKER_NAME))" ]; then\
docker start $(DOCKER_NAME) ;\
wget --retry-connrefused --tries=30 -q --wait=2 --spider http://localhost:8000 ;\
fi
package/eventgen-develop.tar.gz:
curl -s -S -L -f https://github.com/splunk/eventgen/archive/develop.tar.gz -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@
clean: clean_docker clean_file
clean_docker:
if [ "`docker inspect -f '{{.State.Running}}' $(DOCKER_NAME)`" = "true" ]; then docker stop $(DOCKER_NAME); fi
docker inspect $(DOCKER_NAME) >> /dev/null ; if [ $$? -eq 0 ]; then docker rm $(DOCKER_NAME); fi
rm -rf "$(DOCKER_TMP)/log"
rm -rf "$(DOCKER_TMP)/$(APPDIR)"
clean_file:
rm -rf package/*
@asieira
Copy link
Author

asieira commented Sep 15, 2017

Assumes that the Makefile sits right above a folder that contains the app to be packaged. This sub-folder name should be provided as the APPDIR variable.

Will create a package folder where the .spl file will be saved.

Requirements:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment