Project

General

Profile

Build script » History » Version 5

Luke Murphey, 02/15/2017 06:08 PM

1 1 Luke Murphey
h1. Build script
2 1 Luke Murphey
3 5 Luke Murphey
This page will walk you though the steps to use a build script designed specifically for building Splunk apps. Here are some benefits of using this build script:
4 3 Luke Murphey
5 5 Luke Murphey
# Creates a Splunk app package easily (without having to run a bunch of CLI tools manually). It will also perform minification (to improve performance) and populate the build file with things like the build date and build number (makes it easy to know what the version is of the app when troubleshooting).
6 5 Luke Murphey
# Deploys your changes to a local Splunk install for testing your changes (avoids manually copying the files over from the source code repository so that development is easier).
7 5 Luke Murphey
# Works with Continuous Integration tools to enable automated on-going tests (helps you avoid manually repeating tests).
8 1 Luke Murphey
9 5 Luke Murphey
This build script was designed to make the process as easy as possible with as few steps as possible and good error messages so that you don't waste time.
10 5 Luke Murphey
11 1 Luke Murphey
h2. Getting started with the build script
12 1 Luke Murphey
13 5 Luke Murphey
h3. Step 1:install dependencies
14 1 Luke Murphey
15 5 Luke Murphey
This build script is written using Apache Ant. Thus, you will need to install Ant on your platform. Download Ant from http://ant.apache.org/bindownload.cgi and install it accordingly. Note that many platforms have package managers that make installing Ant very easy. For example, Homebrew can be used to install Ant on Mac using just this command:
16 1 Luke Murphey
17 5 Luke Murphey
<pre>
18 5 Luke Murphey
brew install ant
19 5 Luke Murphey
</pre>
20 5 Luke Murphey
21 5 Luke Murphey
Once installed, you should be able to run the following:
22 5 Luke Murphey
23 5 Luke Murphey
<pre>
24 5 Luke Murphey
ant
25 5 Luke Murphey
</pre>
26 5 Luke Murphey
27 5 Luke Murphey
If successful, you should see a message like this:
28 5 Luke Murphey
29 5 Luke Murphey
<pre>
30 5 Luke Murphey
Buildfile: build.xml does not exist!
31 5 Luke Murphey
Build failed
32 5 Luke Murphey
</pre>
33 5 Luke Murphey
34 5 Luke Murphey
h3. Step 2: add the build script to your project
35 5 Luke Murphey
36 5 Luke Murphey
Copy the file "basebuild.xml" to the root of your app's source code directory from here: https://gist.github.com/LukeMurphey/8fd02337805ae8762afb
37 5 Luke Murphey
38 5 Luke Murphey
Make sure that this file goes into your source code repository (typically goes at the root directory of the project).
39 5 Luke Murphey
40 1 Luke Murphey
Then, run the following to start a new project:
41 4 Luke Murphey
42 4 Luke Murphey
<pre>
43 1 Luke Murphey
ant -f basebuild start_project
44 4 Luke Murphey
</pre>
45 4 Luke Murphey
46 4 Luke Murphey
This will ask you for the name of your app. The name it is asking for is the name app ID which is the folder name of the app (e.g. "website_monitoring"). This build target will make a customized build script for your app and properties files where you can declare custom parameters to change the behavior of the build script.
47 4 Luke Murphey
48 4 Luke Murphey
Now that a new build script was created, you can test it by typing:
49 1 Luke Murphey
50 4 Luke Murphey
<pre>
51 1 Luke Murphey
ant
52 1 Luke Murphey
</pre>
53 1 Luke Murphey
54 1 Luke Murphey
This should produce a package in the tmp/packages directory.
55 1 Luke Murphey
56 5 Luke Murphey
Once done, your project directory include some new files and directories will look something like this:
57 1 Luke Murphey
58 5 Luke Murphey
* *basebuild.xml* (this provides some shared build targets that are useful for building Splunk apps)
59 5 Luke Murphey
* *build.xml* (this is the customized build file specifically for your project; you would make any overrides of basebuild.xml in this file)
60 5 Luke Murphey
* *default.properties* (contains parameters to tailor the functionality of the build script; this file should be committed to the source-code repository since it contains things that everything should use)
61 5 Luke Murphey
* *local.properties* (contains parameters to tailor the functionality of the build script for a particular developer's computer; this file should _*not*_ be committed to the source-code repository since it contains things specific to a particular computer)
62 5 Luke Murphey
* *lib/* (this contains build script dependencies)
63 5 Luke Murphey
* *src/* (the code for your app should be in here)
64 5 Luke Murphey
* *tmp/* (this contains the output of the build script)
65 5 Luke Murphey
** *packages/* (by default, built Splunk app packages will be put here)
66 5 Luke Murphey
67 5 Luke Murphey
h3. Step 3: Deploy your app to a local Splunk install
68 5 Luke Murphey
69 4 Luke Murphey
The build script provides some tools to help you send your code to a local Splunk install for testing. This allows you to send the code from the
70 4 Luke Murphey
app you are writing to a live Splunk install to see the running changes. To do this, edit the "local.properties" file and declare the location of your
71 4 Luke Murphey
Splunk install, like this:
72 4 Luke Murphey
73 4 Luke Murphey
<pre>
74 4 Luke Murphey
value.deploy.splunk_home=/Users/luke_murphey/Applications/Splunk
75 4 Luke Murphey
</pre>
76 4 Luke Murphey
    
77 1 Luke Murphey
Once you do that, run the following build target to have your code sent to your Splunk install:
78 4 Luke Murphey
79 4 Luke Murphey
<pre>
80 4 Luke Murphey
ant deploy
81 4 Luke Murphey
</pre>
82 4 Luke Murphey
83 4 Luke Murphey
Restart your Splunk install in order to see that your app is now recognized by Splunk (it should appear in the list of apps even though the app is just an empty directory).
84 4 Luke Murphey
85 5 Luke Murphey
h3. Step 4: Place your source code in the /src directory
86 4 Luke Murphey
87 4 Luke Murphey
At this point, you can out your app's source-code in the "src" directory. The code in the "src" directory will the be the code that makes up your app. Run "ant deploy" to send the new code to your running Splunk install so that you preview the changes.
88 4 Luke Murphey
89 4 Luke Murphey
h2. File structure that the build script expects
90 4 Luke Murphey
91 4 Luke Murphey
By default, the build script will assume that the source-code is under the src directory. To work with the build script, put your code in the following directory structure. The only directory you must create is the src directory; everything else is optional or will be created for you.
92 4 Luke Murphey
93 4 Luke Murphey
* *src/* (this is where you app code will go)
94 4 Luke Murphey
* *lib/* (this directory will be created by the build script, this is where build dependencies will go)
95 4 Luke Murphey
* *tests/* (optional, this is the directory that contains tests for your app)
96 4 Luke Murphey
** *unit.py* (optional, this is a script where the unit tests should go)
97 4 Luke Murphey
98 4 Luke Murphey
h2. Editing build script properties
99 4 Luke Murphey
100 4 Luke Murphey
h2. Build script targets
101 1 Luke Murphey
102 1 Luke Murphey
Targets for publishing the Splunk app (the main one):
103 1 Luke Murphey
* package
104 1 Luke Murphey
105 1 Luke Murphey
Targets that help you while developing Splunk apps:
106 1 Luke Murphey
* splunk.deploy
107 1 Luke Murphey
* splunk.start, splunk.restart, splunk.stop, splunk.restart_web
108 1 Luke Murphey
* splunk.deploy_and_refresh
109 1 Luke Murphey
110 1 Luke Murphey
Targets for testing:
111 1 Luke Murphey
* test.run_unit