One of [WebHelpDesk]’s many useful features is the ability to set up discovery connections that pull data from other sources into its own database.
WebHelpDesk comes with a built in JAMF Casper Suite connection (which will be referred to as “JSS” from here on), but it only applies to Computers. If you have Casper for iOS, it doesn’t pull iOS devices. I have Casper only for iOS devices, with no Computers at all, so this discovery connection fails for me.
That’s not a big problem, though, because you can also set up a raw database or table view to pull data from instead. Since WHD can pull data from any database it can access, we just need to find a way to get that JSS data into a nice flat database.
The JSS does have its own MySQL database to store data in, but in general we don’t really want to access that directly. We want to keep our databases separate and isolated from each other, both for safety and robustness. Also, the tables and ways that Casper stores information into its database are private implementation details, and we can’t rely on those being the same between updates. It’s entirely possible that Casper 9.7, or Casper 10, will change existing database structure, and any discovery connection we set up in WebHelpDesk that pulls data from certain tables might break. Not much point setting up an automation that can’t be relied upon.
Instead, Docker makes it easy for us to set up a quick database for WHD to access. All that needs to be done is writing a script that pulls data from Casper and populates the database, so that WHD can use a discovery connection to access it.
Writing the script:
The repo for this script can be found on Github here.
The script requires the presence of two files:
- The database preferences file com.github.nmcspadden.prefs.json , in JSON format
- The JSS preferences file used for Python-JSS (documented on the Python-JSS wiki, in Plist format
The database preferences file needs to have credentials for access to a Postgres database to store the data in for WHD to pull from.
The JSS preferences file needs to have credentials for access to a Casper instance, along with a username and password with API permissions.
Rather than repost the entire script, I’m going to discuss two specific functions:
This function takes an SQL connection (created by the psycopg2 connector) and then creates the basic Casper table that we’re going to populate, if it doesn’t already exist (thus providing idempotence).
SubmitSQLForDevice(thisDevice, conn, j)
This is the main meat of the script – it’s also known as an “upsert” from the Postgres manual. It takes a given device (i.e. a single device record polled from the JSS), creates a new function called
merge_db that contains all the fields from that device that are relevant, and then updates the record in the new database table if it exists, otherwise creates it if not already found.
Since we have a working script that pulls data from the JSS via API and puts it into a Postgres container, it seems only natural to Dockerize it.
Creating a Docker image:
The Docker image can be pulled from the Docker hub here.
FROM debian MAINTAINER Nick McSpadden <firstname.lastname@example.org> ENV APP_DIR /home/jssi ENV DB_HOST db ENV DB_NAME jssimport ENV DB_USER jssdbadmin ENV DB_PASS password ENV JSS_USER user ENV JSS_PASS password ENV JSS_URL https://casper:8443/ RUN apt-get update && apt-get install -y python-setuptools python-psycopg2 && apt-get clean RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ADD https://github.com/sheagcraig/python-jss/tarball/master /usr/local/python-jss/master.tar.gz RUN tar -zxvf /usr/local/python-jss/master.tar.gz --strip-components=1 -C /usr/local/python-jss && rm /usr/local/python-jss/master.tar.gz WORKDIR /usr/local/python-jss RUN python /usr/local/python-jss/setup.py install ADD https://github.com/nmcspadden/JSSImport/tarball/master $APP_DIR/master.tar.gz RUN tar -zxvf /home/jssi/master.tar.gz --strip-components=1 -C /home/jssi/ && rm /home/jssi/master.tar.gz ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"]
Aside from the epic amount of environmental variables, the Dockerfile is fairly simple. Install Python’s setuptools and pyscopg2 module, and then install python-jss by running
setup.py install. Add the JSSImport script.
Finally, add the runtime execution script, which replaces the contents of the two preference files (the JSON and Plist files) with the environmental variables, and then executes the sync.
This container is not intended to be a daemonized running container. It executes the JSSImport script and then stops, so using –rm for each execution is ideal – we don’t really need it to linger around after finishing since it doesn’t do anything on its own.
Running the Docker image:
Start a data-only container for the Postgres database:
docker run --name "jssi-db-data" -d --entrypoint /bin/echo macadmins/postgres Data-only container for jssimport-db
Start the database, change variables as necessary:
docker run --name "jssimport-db" -d --volumes-from jssi-db-data -e DB_NAME=jssimport -e DB_USER=jssdbadmin -e DB_PASS=password --restart="always" macadmins/postgres
Run the container, which will execute the JSSPull script and then delete itself:
docker run --rm --name jssi --link jssimport-db:db -e DB_NAME=jssimport -e DB_USER=jssdbadmin -e DB_PASS=password -e JSS_USER=user -e JSS_PASS=password -e JSS_URL=https://casper.domain.com:8443 macadmins/jssimport
[Note: This assumes you have a running Casper install, or have a JSS container you can link. That’s outside the scope of this post.]
The jssimport-db database container is now populated with the mobile device list from the provided JSS, and can be sourced for WebHelpDesk’s discovery connections.
Configuring WebHelpDesk to use it:
You’ll want to run a WebHelpDesk docker container. For more information about that, see my previous blog post on running WebHelpDesk in Docker.
However, since we’re now adding in a new database, you’ll want to run the WebHelpDesk container with our additional jssimport-db database linked in:
docker run -d -p 8081:8081 --link postgres-whd:db --link jssimport-db:jdb --name "whd" macadmins/whd
Once you’ve done the basic configuration of WebHelpDesk (also covered in the previous post above), you can now configure a discovery connection for our JSSImport database:
- Connection Name: “Casper” (whatever you want)
- Discovery Tool: Database Table or View
- Database Type: PostgreSQL – uncheck Use Embedded Database
- Host: jdb
- Port: 5432
- Database Name: jssimport
- Username: jssdbadmin
- Password: password
- Schema: Public
- Table or View: casperimport
- Sync Column: serial
Next you’ll need to map the fields in the “Attribute Mapping” section so that the appropriate database columns get mapped to the appropriate WHD fields. (Example: “serial” maps to WHD field “Serial No.”, “macaddress” to WHD field “MAC address, etc.”)
Once attributes are mapped, save the connection, go back and hit “Sync Now” and watch the import.
The end result should be that WebHelpDesk now pulls in all the mobile devices from your Casper instance, through the roundabout means of API -> database -> discovery connection.