#!/bin/bash

set -eux
set -o pipefail

function puppet_apply {
    set +e
    $@ 2>&1 | awk '{print strftime("%Y-%m-%d %H:%M:%S") " - "$0}'
    rc=$?
    set -e

    echo "puppet apply exited with exit code $rc"

    if [ $rc != 2 -a $rc != 0 ]; then
        exit $rc
    fi
}

FACTER_step=1 puppet_apply \
    puppet apply --detailed-exitcodes /etc/puppet/manifests/puppet-stack-config.pp
FACTER_step=2 puppet_apply \
    puppet apply --detailed-exitcodes /etc/puppet/manifests/puppet-stack-config.pp
