#!/bin/bash

#cd /root/mos
#git pull
PUPPET_RUN_OK=0
PUPPET_RUN_FAILED=1
PUPPET_RUN_OK_RES_CHANGED=2
PUPPET_RUN_OK_RES_FAILED=4
PUPPET_RUN_OK_RES_CHANGED_AND_FAILED=6

function check_puppet_exitcode {
	local exit_code=$1
        if [ "${exit_code}"  == "${PUPPET_RUN_OK}" ]; then
		echo "PUPPET OK"

	elif [ "${exit_code}" == "${PUPPET_RUN_FAILED}" ]; then
		echo "PUPPET Apply  Run Failed"
	elif [ "${exit_code}" == "${PUPPET_RUN_OK_RES_CHANGED}" ]; then
		echo "PUPPET Apply  run succeeded, and some resources were changed."
	elif [ "${exit_code}" == "${PUPPET_RUN_OK_RES_FAILED}" ]; then
		echo "PUPPET Apply  run succeeded, and some resource failed"
	elif [ "${exit_code}" == "${PUPPET_RUN_OK_RES_CHANGED_AND_FAILED}" ]; then
		echo "PUPPET Apply  succeeded, and included both changes and failures."
	fi

}

#config.pp for controller
for i in `fuel node | grep ready | grep controller | cut -f 3 -d '|'`;
do
	echo ">>>>> ${i}"
	ssh ${i}.domain.tld "puppet apply /etc/fuel/plugins/zabbix_monitoring-2.5/puppet/manifests/zabbix_server_config.pp --logdest syslog  --no-report  --modulepath=/etc/puppet/modules:/etc/fuel/plugins/zabbix_monitoring-2.5/puppet/modules --logdest /var/log/puppet.log --noop --detailed-exitcodes" > /dev/null;
	check_puppet_exitcode $?
	echo "<<<<< ${i}"
done;

#agent.pp for all nodes
for i in `fuel node | grep ready | cut -f 3 -d '|'`; 
do 
	echo ">>>>> ${i}"
	ssh ${i}.domain.tld "puppet apply /etc/fuel/plugins/zabbix_monitoring-2.5/puppet/manifests/agent.pp --modulepath=/etc/puppet/modules:/etc/fuel/plugins/zabbix_monitoring-2.5/puppet/modules --logdest /var/log/puppet.log --logdest syslog --no-report  --noop --detailed-exitcodes  --debug" > /dev/null;
	check_puppet_exitcode $?
	echo "<<<<< ${i}"
done;
