chef/cookbooks/provisioner/templates/default/crowbar_join.redhat.sh.erb
#!/bin/bash
# Copyright 2011, Dell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
case $1 in
stop) exit 0;;
status) service chef-client status ; exit $? ;;
start|'') pidof chef-client && exit 0;;
*) echo "Unknown action to crowbar_join.sh."
exit 1;;
esac
if [[ $TERM != screen ]]; then
echo "Running $0 under screen. Attach with screen -r -S crowbar-join" >&2
exec screen -d -m -S crowbar-join "$0"
fi
if [[ ! -d /var/log/crowbar/crowbar_join ]] ; then
mkdir -p /var/log/crowbar/crowbar_join/
fi
exec 2>>/var/log/crowbar/crowbar_join/errlog
export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '
set -x
if [[ -f /etc/crowbar.install.key ]]; then
export CROWBAR_KEY="$(cat /etc/crowbar.install.key)"
fi
# Run a command and log its output.
log_to() {
# $1 = install log to log to
# $@ = rest of args
local __log="/var/log/crowbar/crowbar_join/$1"
local __timestamp="$(date '+%F %T %z')"
shift
printf "\n%s\n" "$__timestamp: Running $*" | \
tee -a "$__log.err" >> "$__log.log"
local _ret=0
if "$@" 2>> "$__log.err" >>"$__log.log"; then
_ret=0
else
_ret="$?"
echo "$__timestamp: $* failed."
echo "See $__log.log and $__log.err for more information."
fi
printf "\n$s\n--------\n" "$(date '+%F %T %z'): Done $*" | \
tee -a "$__log.err" >> "$__log.log"
return $_ret
}
post_state() {
local curlargs=(-o /dev/null --connect-timeout 60 -s \
-L -X POST --data-binary "{ \"name\": \"$1\", \"state\": \"$2\" }" \
-H "Accept: application/json" -H "Content-Type: application/json" \
--max-time 240 --insecure --location)
[[ $CROWBAR_KEY ]] && curlargs+=(-u "$CROWBAR_KEY" --digest --anyauth)
oldumask=`umask`
umask 077
curl "${curlargs[@]}" "http://<%=@admin_ip%>/crowbar/crowbar/1.0/transition/default"
umask $oldumask
}
# Make sure we never install 32 bit packages.
echo 'exclude = *.i?86' >> /etc/yum.conf
yum -q -y erase '*.i?86'
# Spin while we wait for the interface to come up.
echo "Waiting on our network interface to come up..."
while ! ip addr | grep -v " lo" | grep -q "inet "
do
sleep 1
done
# Get our hostname
HOSTNAME=$(hostname)
sync_time() {
# stop ntpd before we run ntpdate, and start it again afterwards.
service ntp stop
killall ntpd
while ! /usr/sbin/ntpdate -b <%=@admin_ip%>; do
echo "Waiting for NTP server"
sleep 1
done
}
# Speed up SSH on redhat -- without this, we can see up to 30 second
# delays.
if ! grep -q '^UseDNS.*no' /etc/ssh/sshd_config; then
sed -i -e 's/^\(GSSAPI\)/#\1/' \
-e 's/#\(UseDNS.*\)yes/\1no/' \
/etc/ssh/sshd_config
service sshd restart
fi
# Clean up install droppings
rm -f /update_system2.sh
rm -f /net-post-install.sh
echo "Synchronizing time (pass 1)"
sync_time
# Mark us as readying, and get our cert.
post_state $HOSTNAME "readying"
final_state="ready"
mkdir -p /etc/chef
curl -o /etc/chef/validation.pem "<%=@provisioner_web%>/validation.pem"
if [[ ! -x /etc/init.d/bluepill ]]; then
# Make sure that the client knows how to talk to the server
echo "chef_server_url \"http://<%=@admin_ip%>:4000\"" >/etc/chef/client.rb
# Install Chef
echo "Installing Chef..."
while ! log_to yum yum -q -y install rubygem-chef patch libxml2-devel zlib-devel gcc make ruby-devel; do
echo "Failed to do yum install, wait and try again"
sleep 1
done
log_to chef service chef-client stop
log_to chef killall chef-client
log_to chef chkconfig chef-client off
chmod 644 /etc/init.d/chef-client
# Install Chef patches Crowbar needs.
mkdir -p /opt/dell/
( cd /opt/dell
wget -q "<%=@provisioner_web%>/patches.tar.gz"
tar xzvf patches.tar.gz
cd patches
./patch.sh
)
# Make rubygems do what we want.
cat >/etc/gemrc <<EOF
:sources:
- <%=@provisioner_web%>/gemsite/
gem: --no-ri --no-rdoc --bindir /usr/local/bin
EOF
gem install xml-simple
gem install libxml-ruby
gem install wsman
gem install cstruct
gem install bluepill
mkdir -p /etc/bluepill
curl -o /etc/bluepill/chef-client.pill <%=@provisioner_web%>/chef-client.pill
# Create an init script for bluepill
cat > /etc/init.d/bluepill <<EOF
#!/bin/bash
# chkconfig: 2345 10 90
# description: Bluepill Daemon runner
PATH=$PATH:/usr/local/bin
case \$1 in
start) for pill in /etc/bluepill/*.pill; do
[[ -f \$pill ]] || continue
bluepill load "\$pill"
done;;
stop) bluepill stop
bluepill quit;;
status) if pidof bluepilld; then
echo "Bluepill is running."
exit 0
else
echo "Bluepill is not running."
exit 1
fi;;
*) echo "\$1: Not supported.";;
esac
EOF
chmod 755 /etc/init.d/bluepill
fi
# Run Chef
echo "Syncing time (pass 2)"
sync_time
# Until we arrange for the network to transisiton from using
# DHCP somewhere else, the first run of chef-client will always die due to
# the networking barclamp changing the IP address from dhcp to static.
# We will try to pick up and run with it.
echo "Running Chef Client (pass 1)"
log_to chef chef-client -l debug
# Make sure our interfaces are as up as we can get them
echo "Ensuring that our network interfaces are up."
log_to ifup /sbin/service network restart
# Only transition to problem state if the second run fails.
echo "Running Chef Client (pass 2)"
if ! log_to chef chef-client -l debug; then
log_to ifup /sbin/service network restart
post_state $HOSTNAME "recovering"
echo "Error Path"
echo "Syncing Time (pass 3)"
sync_time
echo "Removing Chef Cache"
rm -rf /var/cache/chef/*
echo "Running Chef Client (pass 3) - cache cleanup"
if ! log_to chef chef-client -l debug; then
log_to ifup /sbin/service network restart
echo "Error Path"
echo "Syncing Time (pass 4)"
sync_time
echo "Removing Chef Cache"
rm -rf /var/cache/chef/*
echo "Checking Keys"
rm -f /etc/chef/client.pem
post_state $HOSTNAME "installed"
echo "Running Chef Client (pass 4) - password cleanup"
if ! log_to chef chef-client -l debug; then
log_to ifup /sbin/service network restart
echo "chef-client run failed four times, giving up."
echo "Failed"
printf "Our IP address is: %s\n" "$(ip addr show)"
final_state="problem"
else
post_state $HOSTNAME "$final_state"
log_to chef chef-client -l debug
fi
fi
fi
# Transition to our final state
post_state $HOSTNAME "$final_state"
log_to time service ntpd start
# Fire up bluepill, and let it keep chef-client up.
service bluepill start
echo "Done"