Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.90 KB | None | 0 0
  1. #!/bin/bash
  2. while [[ $# -gt 1 ]]
  3. do
  4. key="$1"
  5.  
  6. case $key in
  7.     -u|--user)
  8.     HAMMER_USER="$2"
  9.     shift # past argument
  10.     ;;
  11.     -p|--pass)
  12.     HAMMER_PASS="$2"
  13.     shift # past argument
  14.     ;;
  15.     -e|--environment)
  16.     ENVIRONMENT="$2"
  17.     shift # past argument
  18.     ;;
  19.     *)
  20.             # unknown option
  21.     ;;
  22. esac
  23. shift # past argument or value
  24. done
  25.  
  26. MASTER="prd-fmas01.war.de.beryju.org"
  27. PUPPET_ROOT="/etc/puppetlabs/code/environments"
  28. PUPPET_MODULE="beryjuorg"
  29. HAMMER_ARGS="-u $HAMMER_USER -p $HAMMER_PASS -s $MASTER"
  30. SSH_USER="puppetrun"
  31. SSH_COMMAND="/usr/bin/sudo /opt/puppetlabs/bin/puppet agent --color false --onetime --verbose --ignorecache --no-daemonize --no-usecacheonfailure --no-splay --show_diff"
  32.  
  33. if [ -z "${ENVIRONMENT}" ]; then
  34.   echo "Environment missing"
  35.   exit 1
  36. fi
  37.  
  38. # Copy Puppetfile over and run r10k
  39. scp -o StrictHostKeyChecking=no "r10k/$ENVIRONMENT.ppfile" $SSH_USER@$MASTER:"Puppetfile"
  40. 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"
  41.  
  42. HOSTS=()
  43. #FILES=$(git show --format=%d --name-only 2fc0fee8ac02be4c8b3962b09843c59c694d194e | tail -n +3)
  44. FILES=$(git show --format=%d --name-only | tail -n +3)
  45. # Check if any pp files were updated
  46. PP=$(echo $FILES | grep -P 'manifests(.*).pp')
  47. if [ -z "${PP}" ]; then
  48.   echo "No Manifests updated, skipping run"
  49.   exit 0
  50. fi
  51.  
  52. echo "Importing Puppet Classes"
  53. hammer $HAMMER_ARGS proxy import-classes --id=1 --environment $ENVIRONMENT
  54.  
  55. while read -r file; do
  56.   if [[ $file =~ ^manifests(.*).pp$ ]]; then
  57.     echo "Moddified Manifest $file"
  58.     CLASS=$(echo $file | sed -e "s/manifests/$PUPPET_MODULE/g; s/\.pp//g; s/\//::/g; s/::init//g")
  59.     echo -e "\t-> Translates into Class $CLASS"
  60.     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')
  61.     # Check if class listing failed
  62.     # Since class listings would not filter out any hosts
  63.     if [[ $HGS != *"Error: puppetclass not found"* ]]; then
  64.       while read -r hg; do
  65.         echo -e "\t\t-> Fetching Host Group $hg"
  66.         HG_HOSTS=$(hammer $HAMMER_ARGS host list  --environment "$ENVIRONMENT" | grep "$hg" | grep -P '^(\d+)' | awk -F"|" '{print $2}' | sed -e 's/\s/\n/g')
  67.         HOSTS+=$HG_HOSTS
  68.       done <<< "$HGS"
  69.     fi
  70.   fi
  71. done <<< "$FILES"
  72.  
  73. HOSTS=$(echo $HOSTS | xargs -n1 | sort -u | xargs | sed -e 's/\s/\n/g')
  74. if [ -z "${HOSTS}" ]; then
  75.   echo "No matching hosts found, we're done"
  76.   exit 0
  77. fi
  78. echo "Running Puppet"
  79. while read -r host; do
  80.   ssh -oStrictHostKeyChecking=no -l "$SSH_USER" "$host" -C "$SSH_COMMAND" </dev/null 2>&1 | sed "s/^/\t-> $host: /"
  81.   echo -e "\t-> $host: Exited with $?"
  82.   if [ $? -ne 0 ]; then
  83.     echo -e "\t-> $host: FAILED PUPPET RUN"
  84.     exit 1
  85.   fi
  86. done <<< "$HOSTS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement