Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- while [[ $# -gt 1 ]]
- do
- key="$1"
- case $key in
- -u|--user)
- HAMMER_USER="$2"
- shift # past argument
- ;;
- -p|--pass)
- HAMMER_PASS="$2"
- shift # past argument
- ;;
- -e|--environment)
- ENVIRONMENT="$2"
- shift # past argument
- ;;
- *)
- # unknown option
- ;;
- esac
- shift # past argument or value
- done
- MASTER="prd-fmas01.war.de.beryju.org"
- PUPPET_ROOT="/etc/puppetlabs/code/environments"
- PUPPET_MODULE="beryjuorg"
- HAMMER_ARGS="-u $HAMMER_USER -p $HAMMER_PASS -s $MASTER"
- SSH_USER="puppetrun"
- SSH_COMMAND="/usr/bin/sudo /opt/puppetlabs/bin/puppet agent --color false --onetime --verbose --ignorecache --no-daemonize --no-usecacheonfailure --no-splay --show_diff"
- if [ -z "${ENVIRONMENT}" ]; then
- echo "Environment missing"
- exit 1
- fi
- # Copy Puppetfile over and run r10k
- scp -o StrictHostKeyChecking=no "r10k/$ENVIRONMENT.ppfile" $SSH_USER@$MASTER:"Puppetfile"
- ssh -o StrictHostKeyChecking=no -l "$SSH_USER" "$MASTER" "/usr/bin/sudo /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir \"$PUPPET_ROOT/$ENVIRONMENT/modules/\" -v debug"
- HOSTS=()
- #FILES=$(git show --format=%d --name-only 2fc0fee8ac02be4c8b3962b09843c59c694d194e | tail -n +3)
- FILES=$(git show --format=%d --name-only | tail -n +3)
- # Check if any pp files were updated
- PP=$(echo $FILES | grep -P 'manifests(.*).pp')
- if [ -z "${PP}" ]; then
- echo "No Manifests updated, skipping run"
- exit 0
- fi
- echo "Importing Puppet Classes"
- hammer $HAMMER_ARGS proxy import-classes --id=1 --environment $ENVIRONMENT
- while read -r file; do
- if [[ $file =~ ^manifests(.*).pp$ ]]; then
- echo "Moddified Manifest $file"
- CLASS=$(echo $file | sed -e "s/manifests/$PUPPET_MODULE/g; s/\.pp//g; s/\//::/g; s/::init//g")
- echo -e "\t-> Translates into Class $CLASS"
- HGS=$(hammer $HAMMER_ARGS puppet-class info --name "$CLASS" 2>&1| sed -n "/Hostgroups:/, /Environments:/ p" | sed '$ d' | tail -n +2 | xargs | sed -e 's/ /\n/g')
- # Check if class listing failed
- # Since class listings would not filter out any hosts
- if [[ $HGS != *"Error: puppetclass not found"* ]]; then
- while read -r hg; do
- echo -e "\t\t-> Fetching Host Group $hg"
- HG_HOSTS=$(hammer $HAMMER_ARGS host list --environment "$ENVIRONMENT" | grep "$hg" | grep -P '^(\d+)' | awk -F"|" '{print $2}' | sed -e 's/\s/\n/g')
- HOSTS+=$HG_HOSTS
- done <<< "$HGS"
- fi
- fi
- done <<< "$FILES"
- HOSTS=$(echo $HOSTS | xargs -n1 | sort -u | xargs | sed -e 's/\s/\n/g')
- if [ -z "${HOSTS}" ]; then
- echo "No matching hosts found, we're done"
- exit 0
- fi
- echo "Running Puppet"
- while read -r host; do
- ssh -oStrictHostKeyChecking=no -l "$SSH_USER" "$host" -C "$SSH_COMMAND" </dev/null 2>&1 | sed "s/^/\t-> $host: /"
- echo -e "\t-> $host: Exited with $?"
- if [ $? -ne 0 ]; then
- echo -e "\t-> $host: FAILED PUPPET RUN"
- exit 1
- fi
- done <<< "$HOSTS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement