Skip to content

Instantly share code, notes, and snippets.

@LukeMurphey
Last active December 16, 2015 06:19
Show Gist options
  • Save LukeMurphey/5390484 to your computer and use it in GitHub Desktop.
Save LukeMurphey/5390484 to your computer and use it in GitHub Desktop.
A basic Ant build file that contains targets useful for building Splunk apps. Tags: #splunk
<!--
This Ant build script contains operations that are useful for building Splunk apps. To use it with you app, do the following:
1) Set the name of the project in the project node
2) Define a default.properties file to specify default parameters. The default.properties ought to be checked in the source-code repository.
3) Define a local.properties file to override the default parameters. This file should be install specific and thus should NOT be checked into the source-code repository.
Below are the parameters supported:
* value.build.packageoutput.directory: indicates where created packages should go (defaults to tmp/packages)
* value.build.number: indicates the build number specified in app.conf (defaults to 1)
* value.src.directory: indicates where the app code is (defaults to src)
To use the generated build number, put ${value.build.number} in your file where you want the string replacement to occur. For example, below a snippet from app.conf that will be populated with the build number:
[install]
build = ${value.build.number}
Make sure to replace SET_APP_NAME_HERE with your project name. This should match the name of your app and the directory of app in /etc/apps.
-->
<!-- Set the name of you app below: -->
<project default="package" name="SET_APP_NAME_HERE">
<!--
Load the properties files, local is loaded first since properties are immutable (cannot be changed
by later property files) and we want the local properties to override the default properties
-->
<property file="local.properties" />
<property file="default.properties" />
<!-- Set up some basic parameters -->
<property name="value.src.directory" value="src" />
<property name="value.temp.directory" value="${java.io.tmpdir}/${user.name}/${ant.project.name}" />
<property name="value.build.appname" value="${ant.project.name}" />
<property name="value.build.number" value="1" />
<property name="value.build.packageoutput.directory" value="tmp/packages" />
<!-- Set up the ant classpath -->
<path id="ant.classpath">
<fileset dir="ant">
<include name="*.jar" />
</fileset>
</path>
<!-- =================================
target: clean
================================= -->
<target name="clean" description="Clean up temporary files and directories created by this build script" >
<!-- Delete the temporary directory -->
<delete quiet="true" includeEmptyDirs="true">
<fileset dir="${value.temp.directory}" />
</delete>
<!-- Delete the local directory where packages are placed -->
<delete quiet="true" includeEmptyDirs="true">
<fileset dir="tmp" />
</delete>
</target>
<!-- =================================
target: increment_build_number
================================= -->
<target name="increment_build_number">
<propertyfile file="default.properties">
<entry key="value.build.number" type="int" operation="+" value="1" pattern="0"/>
</propertyfile>
</target>
<!-- =================================
target: clean_packages
================================= -->
<target name="clean_packages" description="Clean up the packages created by this build script" >
<delete quiet="true" includeEmptyDirs="true">
<fileset dir="tmp/packages" />
</delete>
</target>
<!-- =================================
target: setup_tmp_directory_for_export
================================= -->
<target name="setup_tmp_directory_for_export">
<!-- Create a temporary directory to send the files to -->
<property name="export_dir" value="${value.temp.directory}/package" />
<!-- Create the temporary directory -->
<mkdir dir="${export_dir}"/>
</target>
<!-- =================================
target: populate_export_dir
================================= -->
<target name="populate_export_dir">
<!-- Copy the files over that need substitution. This should only be
applied to text files since Ant may corrupt binary files otherwise. -->
<copy todir="${export_dir}/${value.build.appname}">
<fileset dir="${value.src.directory}">
<include name="**/*.conf" />
<include name="**/*.txt" />
<include name="**/*.xml" />
</fileset>
<!-- Perform the substitution of the build information -->
<filterset begintoken="${" endtoken="}">
<filter token="value.build.number" value="${value.build.number}" />
</filterset>
</copy>
<!-- Copy the binary files over. -->
<copy todir="${export_dir}/${value.build.appname}">
<fileset dir="${value.src.directory}">
<exclude name="**/*.conf" />
<exclude name="**/*.txt" />
<exclude name="**/*.xml" />
</fileset>
</copy>
</target>
<!-- =================================
target: package
================================= -->
<target name="package" depends="setup_tmp_directory_for_export,populate_export_dir" description="Create the Splunk package of the app">
<!-- Make the directory where we will store the files -->
<mkdir dir="${value.build.packageoutput.directory}" />
<!-- Define where the tar file will go -->
<property name="value.temp.tar_package.file" value="${value.temp.directory}/${value.build.appname}.tar" />
<!-- Tar the files -->
<tar destfile="${value.temp.tar_package.file}"
basedir="${export_dir}"
excludes=" **/*.tmp, **/*.pyc"
/>
<!-- Gzip the files -->
<gzip src="${value.temp.tar_package.file}" destfile="${value.build.packageoutput.directory}/${value.build.appname}.tar.gz"/>
<!-- Delete the temporary location so that old files do not get streamed in -->
<delete dir="${value.temp.directory}" />
<echo>App ${value.build.appname} build ${value.build.number} created: ${value.build.packageoutput.directory}/${value.build.appname}.tar.gz</echo>
</target>
<!-- =================================
target: setup_tmp_directory_for_deployment
================================= -->
<target name="setup_tmp_directory_for_deployment">
<!-- Create a reference to the directory to send the files to -->
<property name="export_dir" value="${value.deploy.splunk_home}/etc/apps/" />
<!-- Make the app directory if it does not yet exist -->
<mkdir dir="${export_dir}" />
</target>
<!-- =================================
target: deploy
================================= -->
<target name="deploy" depends="bump_splunk_if_necessary,setup_tmp_directory_for_deployment,populate_export_dir" description="Deploys the app to an instance of Splunk" >
<echo>App ${value.build.appname} build ${value.build.number} deployed to ${export_dir}</echo>
</target>
<!-- =================================
target: stop_splunk
================================= -->
<target name="stop_splunk" description="Stop Splunk">
<exec executable="${value.deploy.splunk_home}/bin/splunk">
<arg line="stop" />
<arg line="--accept-license" />
</exec>
</target>
<!-- ===================================================================
target: splunkweb_conf
=================================================================== -->
<target name="splunkweb_conf" description="Configure SplunkWeb for easier web development">
<mkdir dir="${value.deploy.splunk_home}/etc/system/local/"/>
<echo file="${value.deploy.splunk_home}/etc/system/local/web.conf">[settings]
enable_gzip=True
minify_js=False
minify_css=False
js_no_cache=True
</echo>
</target>
<!-- =================================
target: does_appserver_exist
================================= -->
<target name="does_appserver_exist">
<condition property="appserver_dir_exists">
<available file="${basedir}/${value.src.directory}/appserver/" type="dir"/>
</condition>
</target>
<!-- =================================
target: is_appserver_up_to_date
================================= -->
<target name="is_appserver_up_to_date" description="Determine if the code in the appserver directory of the Splunk install is outdated" depends="does_appserver_exist" if="appserver_dir_exists">
<uptodate property="appserver_up_to_date">
<!-- target should point to the source files -->
<mapper type="glob" from="*" to="${value.deploy.splunk_home}/etc/apps/${value.build.appname}/appserver/*" />
<!-- srcfiles should point to the deployed files -->
<srcfiles dir="${basedir}/${value.src.directory}/appserver/" includes="**/*" />
</uptodate>
</target>
<!-- =================================
target: bump_splunk_if_necessary
================================= -->
<target name="bump_splunk_if_necessary" unless="appserver_up_to_date" if="appserver_dir_exists" depends="does_appserver_exist,is_appserver_up_to_date">
<antcall target="bump_splunk" />
</target>
<!-- =================================
target: bump_splunk
================================= -->
<target name="bump_splunk" description="Bump Splunk">
<!-- Get the current value of bump -->
<loadfile
property="value.deploy.bump"
srcFile="${value.deploy.splunk_home}/var/run/splunk/push-version.txt">
<filterchain>
<striplinebreaks/>
</filterchain>
</loadfile>
<!-- Increment the value -->
<script language="javascript">
var currentValue = parseInt(project.getProperty("value.deploy.bump"), 10);
var newBump = currentValue + 1;
project.setProperty("value.deploy.bump_updated", newBump);
</script>
<!-- Set the current value of bump -->
<echo file="${value.deploy.splunk_home}/var/run/splunk/push-version.txt">${value.deploy.bump_updated}</echo>
<echo>The push version was updated from ${value.deploy.bump} to ${value.deploy.bump_updated}</echo>
<echo>Restarting Splunk's web interface...</echo>
<antcall target="restart_splunk_web" />
</target>
<!-- =================================
target: start_splunk
================================= -->
<target name="start_splunk" description="Start Splunk">
<exec executable="${value.deploy.splunk_home}/bin/splunk">
<arg line="start" />
<arg line="--accept-license" />
</exec>
</target>
<!-- =================================
target: restart_splunk
================================= -->
<target name="restart_splunk" description="Start Splunk">
<exec executable="${value.deploy.splunk_home}/bin/splunk">
<arg line="restart" />
<arg line="--accept-license" />
</exec>
</target>
<!-- =================================
target: restart_splunk_web
================================= -->
<target name="restart_splunk_web" description="Restart Splunk">
<exec executable="${value.deploy.splunk_home}/bin/splunk">
<arg line="restart" />
<arg line="splunkweb" />
<arg line="--accept-license" />
<arg line="-auth" />
<arg line="admin:changeme" />
</exec>
</target>
<!-- =================================
target: deploy_and_refresh_splunk
================================= -->
<target name="deploy_and_refresh_splunk" description="Deploys the application and forces Splunk to refresh" depends="deploy,refresh_splunk" />
<!-- =================================
target: deploy_and_restart_splunk
================================= -->
<target name="deploy_and_restart_splunk" description="Deploys the application and restarts Splunk" depends="deploy,restart_splunk" />
<!-- =================================
target: refresh_splunk
================================= -->
<target name="refresh_splunk" description="Refresh Splunk">
<!-- Define the username and password if not already defined -->
<property name="splunk_username" value="admin"/>
<property name="splunk_password" value="changeme"/>
<!-- Define a macro that can be used for refreshing Splunk endpoints -->
<macrodef name="reload_conf">
<attribute name="endpoint"/>
<sequential>
<exec failonerror="true" executable="${value.deploy.splunk_home}/bin/splunk"> <!-- Fail on error is set to true -->
<arg value="_internal"/>
<arg value="call"/>
<arg value="/admin/@{endpoint}/_reload"/>
<arg value="-auth"/>
<arg value="${splunk_username}:${splunk_password}"/>
<arg value="--accept-license"/>
</exec>
</sequential>
</macrodef>
<reload_conf endpoint="savedsearch" />
<reload_conf endpoint="nav" />
<reload_conf endpoint="views" />
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment