Script for setting path for plugin testing

I have been going slightly nuts setting and resetting my path for plugin asset testing, so I threw together this quick script. I’m not sure if it will be of use to anyone else, but I threw it out here just in case. I have only used this on Ubuntu, you may need to change the path in the script for others OSs.

I typically use it by logging in as the sensu user and running something like (in bash):
eval sensu-setassetpath sensu-ruby-runtime sensu-plugins-http
That will set my PATH and LD_LIBRARY_PATH to include those two assets, then I can run commands like:
check-http.rb -a MyStrongPassword -U MyUser -u http://www.example.com/ -r
and try to figure out why they the plugin isn’t returning what I want it to.

#!/bin/bash
#
# Update path and library path for a particular plugin asset
#

CACHEDIR="/var/cache/sensu/sensu-agent"

usage() {
  if [ -n "$1" ]; then
    echo $*
  fi
  echo "usage: eval \"\$($0 asset)\""
  exit 1
}

# If no plugin specified, output list and usage message
if [ -z "$1" ]; then
  sensuctl asset list | awk '{print $1}'
  usage "You must specify one of the above assets"
fi

newpath=""
newlibpath=""
for i in $*; do
  sha=$(sensuctl asset info $i --format json | jq -r .sha512)
  if [ -d ${CACHEDIR}/${sha}/bin ]; then
    if [ -z "$newpath" ]; then
      newpath=${CACHEDIR}/${sha}/bin
    else
      newpath=${newpath}:${CACHEDIR}/${sha}/bin
    fi
  fi
  if [ -d ${CACHEDIR}/${sha}/lib ]; then
    if [ -z "$newlibpath" ]; then
      newlibpath=${CACHEDIR}/${sha}/lib
    else
      newlibpath=${newlibpath}:${CACHEDIR}/${sha}/lib
    fi
  fi
done

echo "export PATH=${newpath}:${PATH}"
echo "export LD_LIBRARY_PATH=${newlibpath}:${LD_LIBRARY_PATH}"
3 Likes

Nice! Thanks for sharing. Also fully noted that a first-class utility or test harness would be helpful. I think we have a GitHub issue floating around somewhere for this, but I’d welcome a new issue that captures your use case - especially since you’ve spent some time prototyping a workaround.

Thanks again!

Thanks a bunch. I noted that this must be run as a user that has read access to those directories. :slight_smile: