#!/bin/bash

# write out chef configuration if necessary
if [[ "{{ .deis_controller_cmModule }}" == "cm.chef" ]]; then

	# write out chef configuration
	sudo mkdir -p /etc/chef
	sudo chown deis /etc/chef
	echo "{{ .deis_chef_clientKey }}" | base64 -d > /etc/chef/client.pem
	echo "{{ .deis_chef_validationKey }}" | base64 -d > /etc/chef/validation.pem
	cat > /etc/chef/client.rb <<EOF
log_level        :auto
log_location     STDOUT
chef_server_url  "{{ .deis_chef_url }}"
validation_client_name "{{ .deis_chef_validationName }}"
node_name        "{{ .deis_chef_clientName }}"
EOF

fi

# gracefully reload celery and gunicorn
test -e /tmp/celery.pid && kill -HUP `cat /tmp/celery.pid`
test -e /tmp/gunicorn.pid && kill -HUP `cat /tmp/gunicorn.pid`

exit 0
