Last active
January 26, 2018 07:29
-
-
Save jpadams/95d06f4451895a3433584284f09bee19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| GET_FACTS_FROM_CERTNAME="win-2016-49.inf.puppet.vm" | |
| CERTNAME_FOR_OUTPUT="mission-3" | |
| FAKE_NODES="fake-nodes.localdomain" | |
| #make sure jq and rhino javascript installed | |
| jq_path=$(which jq 2> /tmp/get-win-facts.log) | |
| if [ -x "$jq_path" ] ; then | |
| echo "jq is installed: $jq_path" >> /tmp/get-win-facts.log | |
| else | |
| echo "jq is NOT installed, installing now..." >> /tmp/get-win-facts.log | |
| /usr/bin/curl -s -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 -o /usr/bin/jq | |
| chmod +x /usr/bin/jq | |
| fi | |
| rhino_path=$(which rhino 2>> /tmp/get-win-facts.log) | |
| if [ -x "$rhino_path" ] ; then | |
| echo "rhino is installed: $rhino_path" >> /tmp/get-win-facts.log | |
| else | |
| echo "rhino is NOT installed, installing now..." >> /tmp/get-win-facts.log | |
| puppet resource package rhino ensure=absent &>> /tmp/get-win-facts.log | |
| puppet resource package rhino ensure=installed &>> /tmp/get-win-facts.log | |
| fi | |
| puppet query 'facts [name,value,environment,certname]{certname = "'$GET_FACTS_FROM_CERTNAME'" order by name}' \ | |
| | jq '[.[] | .certname = "'$CERTNAME_FOR_OUTPUT'" | .id = "\(.certname)-\(.name)" | del(.certname) | if (.value | type)=="object" then (.value=(.value | tojson)) else (.value=.value) end]' \ | |
| | sed "s/$GET_FACTS_FROM_CERTNAME/$CERTNAME_FOR_OUTPUT/g" | sed "s/$(echo $GET_FACTS_FROM_CERTNAME | cut -d. -f1)/$CERTNAME_FOR_OUTPUT/g" \ | |
| | sed "s/$FAKE_NODES/$CERTNAME_FOR_OUTPUT/g" | sed "s/$(echo $FAKE_NODES | cut -d. -f1)/$CERTNAME_FOR_OUTPUT/g" \ | |
| | sed "s/$(echo $GET_FACTS_FROM_CERTNAME | cut -d. -f1 --complement)//g" | sed "s/$(echo $FAKE_NODES | cut -d. -f1 --complement)//g" \ | |
| > /tmp/ready-for-rhino.json | |
| printf "var facts = " > /tmp/add-structured-fact-newlines.js | |
| cat /tmp/ready-for-rhino.json >> /tmp/add-structured-fact-newlines.js | |
| echo >> /tmp/add-structured-fact-newlines.js | |
| cat << EOF >> /tmp/add-structured-fact-newlines.js | |
| function prettyStructured (k,v) { | |
| try {if (k == "value" && typeof(JSON.parse(v)) == "object") { | |
| v = JSON.stringify(JSON.parse(v), null, 2).replace(/\n/g, "\n"); | |
| } } catch(e) {} | |
| return v; | |
| } | |
| var out = JSON.stringify(facts, prettyStructured ,2) | |
| print(out) | |
| EOF | |
| chmod +x /tmp/add-structured-fact-newlines.js | |
| rhino /tmp/add-structured-fact-newlines.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment