Triggering R10k With Serf

By Bill Ward | June 25, 2017

This post is the next in a series of posts that shows how to integrate a complete production puppet environment with serf. In this post we learn how to use serf to fire off r10k updates on your compile masters. This takes the place of running mco r10k synchronize.

r10k Custom Serf Query

This post builds off the my last serf post SERF CUSTOM QUERIES, but this time we are going to add this functionality to the puppetserver(s).

Create the following directory structure on each of your compile masters where you have r10k running,

/etc/serf
├── handlers
│   └── query-r10k
└── handler.sh

The code for the handler.sh file is exactly the same as the last post:

#!/bin/sh
HANDLER_DIR="/etc/serf/handlers"

if [ "$SERF_EVENT" = "user" ]; then
    EVENT="user-$SERF_USER_EVENT"
elif [ "$SERF_EVENT" = "query" ]; then
    EVENT="query-$SERF_QUERY_NAME"
else
    EVENT=$SERF_EVENT
fi

HANDLER="$HANDLER_DIR/$EVENT"
[ -f "$HANDLER" -a -x "$HANDLER" ] && exec "$HANDLER" || :

Custom r10k Query

Now we need to write our custom query script query-r10k. This will be triggered when serf receives a query called r10k.

#!/bin/bash                                                                                                                                                               

/opt/runr10k.sh $>/dev/null $disown

echo "Starting r10k Update"

This calls runr10k.sh in another process because we need to get a response back to serf within 15 seconds. Here is the code for runr10k:

#!/bin/bash                                                                                                                                                               

/opt/puppetlabs/puppet/bin/r10k deploy environment -p -v

This will run r10k for us to deploy all environments.

Trigger r10k Update

Now that we have everything ready we can trigger r10k updates on all our compile masters with a single command:

# serf query r10k

This a basic example of what we can do with serf and r10k. We could update it by sending something in the serf payload to send a module name. The query-r10k script could pick this up and know to deploy/update a single module. Maybe that will be another post. That would be very useful if you are deploying application updates to a server(s) from your CI/CD solution like jenkins.

I hope you enjoyed this post. If it was helpful or if it was way off then please comment and let me know.

Subscribe to our mailing list

indicates required
Email Format

comments powered by Disqus
Google Analytics Alternative