Skip to content

Commit

Permalink
Merge pull request crowbar#19 from rha-cb/pull-req-feature-suse-integ…
Browse files Browse the repository at this point in the history
…ration-cleanup-master-87ff517a2f

Initial changes for SUSE support in provisioner, logging and network [2/7]
  • Loading branch information
galthaus committed May 16, 2012
2 parents f6d9df2 + 87ff517 commit 32fb543
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 2 deletions.
27 changes: 26 additions & 1 deletion chef/cookbooks/provisioner/recipes/setup_base_images.rb
@@ -1,4 +1,5 @@
# Copyright 2011, Dell
# Copyright 2012, SUSE Linux Products GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License
Expand Down Expand Up @@ -220,7 +221,7 @@
node[:provisioner][:repositories][os_token][f] = case
when os_token =~ /ubuntu/
"deb http://#{admin_ip}:#{web_port}/#{os_token}/crowbar-extra/#{f} /"
when os_token =~ /(redhat|centos)/
when os_token =~ /(redhat|centos|suse)/
"baseurl=http://#{admin_ip}:#{web_port}/#{os_token}/crowbar-extra/#{f}"
else
raise ::RangeError.new("Cannot handle repos for #{os_token}")
Expand All @@ -240,6 +241,30 @@

# These should really be made libraries or something.
case
when /^(suse)/ =~ os
# Add base OS install repo for suse
node[:provisioner][:repositories][os_token]["base"] = "baseurl=http://#{admin_ip}:#{web_port}/#{os_token}/install"
template "#{os_dir}/autoyast.xml" do
mode 0644
source "autoyast.xml.erb"
owner "root"
group "root"
variables(
:admin_node_ip => admin_ip,
:web_port => web_port,
:repos => node[:provisioner][:repositories][os_token],
:admin_web => admin_web,
:crowbar_join => "#{web_path}/crowbar_join.sh")
end

template "#{os_dir}/crowbar_join.sh" do
mode 0644
owner "root"
group "root"
source "crowbar_join.suse.sh.erb"
variables(:admin_ip => admin_ip)
end

when /^(redhat|centos)/ =~ os
# Add base OS install repo for redhat/centos
if ::File.exists? "/tftpboot/#{os_token}/install/repodata"
Expand Down
165 changes: 165 additions & 0 deletions chef/cookbooks/provisioner/templates/default/autoyast.xml.erb
@@ -0,0 +1,165 @@
<?xml version="1.0"?>
<!DOCTYPE profile>
<profile xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns">
<deploy_image>
<image_installation config:type="boolean">false</image_installation>
</deploy_image>
<general>
<ask-list config:type="list"/>
<mode>
<confirm config:type="boolean">false</confirm>
<final_halt config:type="boolean">false</final_halt>
<final_reboot config:type="boolean">true</final_reboot>
<halt config:type="boolean">false</halt>
<second_stage config:type="boolean">true</second_stage>
</mode>
<mouse>
<id>none</id>
</mouse>
<proposals config:type="list"/>
<signature-handling>
<accept_file_without_checksum config:type="boolean">true</accept_file_without_checksum>
<accept_non_trusted_gpg_key config:type="boolean">true</accept_non_trusted_gpg_key>
<accept_unknown_gpg_key config:type="boolean">true</accept_unknown_gpg_key>
<accept_unsigned_file config:type="boolean">true</accept_unsigned_file>
<accept_verification_failed config:type="boolean">true</accept_verification_failed>
<import_gpg_key config:type="boolean">true</import_gpg_key>
</signature-handling>
<storage/>
</general>
<users config:type="list">
<user>
<username>root</username>
<user_password>root</user_password>
<encrypted config:type="boolean">false</encrypted>
<forename/>
<surname/>
</user>
</users>
<networking>
<keep_install_network config:type="boolean">true</keep_install_network>
<dns>
<dhcp_hostname config:type="boolean">true</dhcp_hostname>
<domain>crowbar.site</domain>
<resolv_conf_policy>auto</resolv_conf_policy>
<write_hostname config:type="boolean">false</write_hostname>
</dns>
<managed config:type="boolean">false</managed>
<routing>
<ip_forward config:type="boolean">false</ip_forward>
</routing>
</networking>
<partitioning config:type="list">
<drive>
<initialize config:type="boolean">true</initialize>
<partitions config:type="list"/>
<type config:type="symbol">CT_DISK</type>
<use>all</use>
</drive>
</partitioning>
<scripts>
<pre-scripts config:type="list">
<script>
<debug config:type="boolean">true</debug>
<feedback config:type="boolean">false</feedback>
<notification>Please wait while script is running...</notification>
<source>
<![CDATA[
export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '
HTTP_SERVER="<%= @admin_node_ip %>:<%= @web_port %>"
IP="<%= @admin_node_ip %>"
HOSTNAME=$(hostname)

key_re='crowbar\.install\.key=([^ ]+)'
if [[ $(cat /proc/cmdline) =~ $key_re ]]; then
export CROWBAR_KEY="${BASH_REMATCH[1]}"
echo "$CROWBAR_KEY" >/mnt/etc/crowbar.install.key
elif [[ -f /mnt/etc/crowbar.install.key ]]; then
export CROWBAR_KEY="$(cat /mnt/etc/crowbar.install.key)"
fi

post_state() {
local curlargs=(-o "/var/log/$1-$2.json" --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)
[[ $CROWBAR_KEY ]] && curlargs+=(-u "$CROWBAR_KEY" --digest --anyauth)
curl "${curlargs[@]}" "http://$IP:3000/crowbar/crowbar/1.0/transition/default"
}

post_state $HOSTNAME "installing"
]]>
</source>
</script>
</pre-scripts>
<chroot-scripts config:type="list">
<script>
<chrooted config:type="boolean">false</chrooted>
<debug config:type="boolean">true</debug>
<filename>crowbar_post</filename>
<source>
<![CDATA[
export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '
HTTP_SERVER="<%= @admin_node_ip %>:<%= @web_port %>"
IP="<%= @admin_node_ip %>"
HOSTNAME=$(hostname)

key_re='crowbar\.install\.key=([^ ]+)'
if [[ $(cat /proc/cmdline) =~ $key_re ]]; then
export CROWBAR_KEY="${BASH_REMATCH[1]}"
echo "$CROWBAR_KEY" >/mnt/etc/crowbar.install.key
elif [[ -f /mnt/etc/crowbar.install.key ]]; then
export CROWBAR_KEY="$(cat /mnt/etc/crowbar.install.key)"
fi

post_state() {
local curlargs=(-o "/var/log/$1-$2.json" --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)
[[ $CROWBAR_KEY ]] && curlargs+=(-u "$CROWBAR_KEY" --digest --anyauth)
curl "${curlargs[@]}" "http://$IP:3000/crowbar/crowbar/1.0/transition/default"
}

mkdir -p /mnt/root/.ssh
chmod 700 /mnt/root/.ssh
if ! curl -s -o /mnt/root/.ssh/authorized_keys.wget \
http://$HTTP_SERVER/authorized_keys; then
rm -f /mnt/root/.ssh/authorized_keys.wget
else
chmod 644 /mnt/root/.ssh/authorized_keys
cat /mnt/root/.ssh/authorized_keys.wget >> /mnt/root/.ssh/authorized_keys
rm -f /mnt/root/.ssh/authorized_keys.wget
fi

curl -s -o /mnt/etc/init.d/crowbar_join.sh <%= @crowbar_join %>
chmod +x /mnt/etc/init.d/crowbar_join.sh

sleep 30

post_state $HOSTNAME "installed"

# Wait for DHCP to update - this is mainly for virtual environments or really large deploys
sleep 30
sync
]]>
</source>
</script>
</chroot-scripts>
<init-scripts config:type="list">
<script>
<filename>crowbar_join</filename>
<location>http://<%= @admin_node_ip %>:8091/suse-11.2/crowbar_join.sh</location>
</script>
</init-scripts>
</scripts>
<software>
<packages config:type="list">
<package>autoyast2-installation</package>
</packages>
<patterns config:type="list">
<pattern>Minimal</pattern>
<pattern>base</pattern>
</patterns>
</software>
</profile>
170 changes: 170 additions & 0 deletions chef/cookbooks/provisioner/templates/default/crowbar_join.suse.sh.erb
@@ -0,0 +1,170 @@
#!/bin/bash
# Copyright 2011, Dell
# Copyright 2012, SUSE Linux Products GmbH
#
# 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.
#

HTTP_SERVER="<%= @admin_ip %>:8091"
IP=${HTTP_SERVER%:*}

exec 2>>/var/log/crowbar-join.errlog

export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '
set -x

if [[ ! -d /var/log/chef/ ]]; then
mkdir -p /var/log/chef/
fi

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-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 "/var/log/$1-$2.json" --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)
[[ $CROWBAR_KEY ]] && curlargs+=(-u "$CROWBAR_KEY" --digest --anyauth)
curl "${curlargs[@]}" "http://$IP:3000/crowbar/crowbar/1.0/transition/default"
}

# 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 -f)

sync_time() {
# stop ntpd before we run ntpdate, and start it again afterwards.
service ntp stop
# Warning! Newer sntp releases use different options (e.g. the one
# on openSUSE 12.1
while ! sntp -P no -r $IP; do
echo "Waiting for NTP server"
sleep 1
done
}

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 \
"http://$HTTP_SERVER/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://$IP:4000\"" >/etc/chef/client.rb

# Install Chef
echo "Installing Chef..."

while ! log_to zypper zypper -n refresh ; do
echo "Failed to do zypper refresh, wait and try again"
sleep 1
done
while ! log_to zypper zypper -n install rubygem-chef ; do
echo "Failed to do zypper install, wait and try again"
sleep 1
done

log_to chef chkconfig -a chef-client
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 "hardware-updated"
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"
fi
fi
fi

# Transition to our final state
post_state $HOSTNAME "$final_state"

log_to time service ntp start
# Fire up bluepill, and let it keep chef-client up.
service chef-client start

echo "Done"
8 changes: 7 additions & 1 deletion chef/data_bags/crowbar/bc-template-provisioner.json
Expand Up @@ -50,7 +50,13 @@
"initrd": "images/pxeboot/initrd.img",
"kernel": "images/pxeboot/vmlinuz",
"append": "method=%os_install_site% ks=%os_site%/compute.ks ksdevice=bootif"
}
},
"suse-11.2": {
"initrd": "boot/x86_64/loader/initrd",
"kernel": "boot/x86_64/loader/linux",
"append": "install=%os_install_site% autoyast=%os_site%/autoyast.xml"
}

},
"root": "/tftpboot",
"web_port": 8091,
Expand Down

0 comments on commit 32fb543

Please sign in to comment.