fuel-library/deployment/puppet/rpmcache/templates/build_rpm_cache.erb
2013-10-21 13:45:55 +04:00

200 lines
6.8 KiB
Plaintext

#!/bin/bash -xe
function revert_back_to_centos() {
# Removes redhat-release-server rpm and replaces it with centos-release
echo "Removing Red Hat temporary configuration."
if rpm -q redhat-release-server &>/dev/null; then
rpm -Uvh --force /root/centos-release/centos-release*.rpm
rm -f /etc/yum.repos.d/CentOS*
rpm -e --nodeps --noscripts redhat-release-server
fi
#Disable subscription-manager and rhn plugins
for plugin in $rhsm_plugins $rhn_plugins; do
sed -i 's/^enabled.*/enabled=0/' "/etc/yum/pluginconf.d/${plugin}.conf" 2>/dev/null || :
done
rm -f /etc/yum/vars/releasever
#Remove libqb hack
sed -i '/^exclude=libqb-0.16/d' /etc/yum.conf
yum-config-manager --enable 'nailgun' &> /dev/null
echo "Unsubscribing from RHSM..."
subscription-manager unregister || :
}
rhsm_plugins="product-id subscription-manager"
rhn_plugins="rhnplugin"
#Enable subscription-manager plugins
for plugin in $rhsm_plugins; do
sed -i 's/^enabled.*/enabled=1/' "/etc/yum/pluginconf.d/${plugin}.conf" 2>/dev/null || :
done
#Register
exitcode=0
rhsmoutput=$(subscription-manager register "--username=<%= rh_username %>" "--password=<%= rh_password %>" --autosubscribe --force 2>&1) || exitcode=$?
exitcode=$?
case $exitcode in
0) echo "Register succeeded"
;;
1) echo "Register succeeded"
;;
*) echo -e "Register failed: $rhmsoutput"
exit $exitcode
;;
esac
#Attach to RHOS product
poolid="$(subscription-manager list --available | grep -A2 "OpenStack" | tail -1 | cut -c15- | tr -d ' \t')"
subscription-manager attach "--pool=$poolid"
trap revert_back_to_centos EXIT
#Set releasever and refresh repos
echo 6Server > /etc/yum/vars/releasever
yum clean expire-cache
#Enable channels
for channel in <%= rh_base_channels %> <%= rh_openstack_channel %>; do
yum-config-manager --enable "$channel" &> /dev/null
done
#Tell RHSM to let CentOS accept packages from Red Hat
#Generate /etc/yum.repos.d/redhat.repo
yum --releasever=<%= releasever %> repolist || :
<% if use_satellite %>
#Save centos-release RPM so it can be reinstalled later
mkdir -p /root/centos-release
yumdownloader --destdir /root/centos-release --disablerepo='*rhel*' centos-release
if ! [ -f /root/centos-release/centos-release*.rpm ]; then
echo "Unable to download centos-release RPM. Aborting." 1>&2
exit 1
fi
#Download RHN packages needed for RHN Satellite registration
rhnpackages="m2crypto rhn-check rhn-client-tools rhnlib rhnsd rhn-setup yum-rhn-plugin redhat-release-server"
rm -rf /root/rhnpackages
mkdir -p /root/rhnpackages
yum-config-manager --enable rhel-6-server-rpms &> /dev/null
success="false"
for tries in 1 2 3; do
yumdownloader --releasever=<%= releasever %> --disablerepo='nailgun*' --disablerepo='fuel*' --setopt='*.skip_if_unavailable=1' --resolve --destdir /root/rhnpackages $rhnpackages
#Verify we have packages downloaded
if [[ "$(find /root/rhnpackages | wc -l)" > 2 ]]; then
success="true"
break
fi
yum clean expire-cache
done
if [[ "$success" == "false" ]];then
echo "Unable to download rhn-setup and redhat-release-server from RHSM." 1>&2
echo "This may be due to a network error or yum configuration." 1>&2
fi
#Delete in case yumdownloader gets extra packages
repomanage -o /root/rhnpackages | xargs rm -f
rpm -ivh --force /root/rhnpackages/*.rpm
#Download RHN Satellite GPG key
mkdir -p /etc/sysconfig/rhn
wget "<%= sat_hostname %>/pub/RHN-ORG-TRUSTED-SSL-CERT" -O /etc/sysconfig/rhn/RHNS-CA-CERT
if [ "$(file -b /etc/sysconfig/rhn/RHNS-CA-CERT)" != "ASCII text" ]; then
echo "GPG Key download failed. Looking for URL <%= sat_hostname %>/pub/RHN-ORG-TRUSTED-SSL-CERT. Cannot proceed"
exit 1
fi
#Disable subscription-manager
subscription-manager unregister
rm -rf /etc/yum.repos.d/redhat.repo
#Disable subscription-manager plugins
for plugin in $rhsm_plugins; do
sed -i 's/^enabled.*/enabled=0/' "/etc/yum/pluginconf.d/${plugin}.conf" 2>/dev/null || :
done
#Run registration
rhnreg_ks "--serverUrl=http://<%= sat_hostname %>/XMLRPC" --profilename=fuelweb "--activationkey=<%= activation_key %>" --sslCACert=/etc/sysconfig/rhn/RHNS-CA-CERT --nopackages --nohardware --novirtinfo --norhnsd --force
if [ $? -ne 0 ]; then
echo "Registration failed. Aborting."
exit 1
fi
#Ensure channels are enabled:
errors=0
rhnchanneloutput="$(rhn-channel -l)"
for channel in <%= sat_base_channels %> <%= sat_openstack_channel %>; do
if grep -q "$channel" <<< "$rhnchanneloutput"; then
echo "Found $channel."
else
echo "ERROR: $channel not found. Fix this in RHN Satellite manually or run the following command:"
echo "rhn-channel -a $channel"
((errors++))
fi
done
if [ $errors -gt 0 ]; then
echo "There were problems enabling channels. Please check puppet manifests and your RHN Satellite channel configuration to ensure the necessary channels are enabled." 1>&2
exit 1
fi
<% end %>
#Download packages
mkdir -p <%= pkgdir %>/repodata <%= pkgdir %>/Packages
yum-config-manager --disable 'nailgun' &> /dev/null
yum-config-manager --disable 'centos' --disable 'extras' --disable 'updates' &> /dev/null
yum-config-manager --disable 'base' &> /dev/null
echo "Building initial cache. This may take several minutes."
yum --releasever=<%= releasever %> makecache
#Hack to exclude libqb-0.16.x because it breaks Pacemaker
echo 'exclude=libqb-0.16*' >> /etc/yum.conf
for tries in $(seq 1 <%= numtries %>); do
#Retry if repotrack fails
/usr/local/bin/repotrack -a x86_64,noarch -p "<%= pkgdir %>/Packages" $(cat /etc/nailgun/required-rpms.txt | xargs echo -en) 2>&1 || continue
status=$?
#Purge any corrupt downloaded RPMs
# FIXME: There is a error with a path substitution
# At least hide path if there is no packages.
shopt -s nullglob
before=`ls -1 "<%= pkgdir %>/Packages/" |wc -l`
echo Packages in local repo: $before
env LANG=C rpm --checksig <%= pkgdir %>/Packages/* | grep 'md5 NOT OK' | cut -d':' -f1| xargs rm -f
after=`ls -1 "<%= pkgdir %>/Packages/" |wc -l`
echo Correct packages in local repo: $after
if [ $before -eq $after -a $status -eq 0 ]; then
break
fi
done
if [ $status -ne 0 ]; then
echo "ERROR: Repotrack did not exit cleanly after <%= numtries %> tries." 1>&2
exit 1
fi
if [ $before -ne $after ]; then
echo "ERROR: There are still some packages with broken md5 sum after last (<%= numtries %>) retry to download them." 1>&2
exit 1
fi
rm -f /etc/yum/vars/releasever
yum-config-manager --enable 'nailgun' &> /dev/null
#Disable all RHEL repos so you don't accidentally update and break master node
yum-config-manager --disable '*rhel*' &> /dev/null || :
#Purge Django 1.3 RPM
rm -f <%= pkgdir %>/Packages/Django-1.3*
rpm -i <%= pkgdir %>/Packages/rhel-boot-image-6.4-20130130.0.el6ost.noarch.rpm
mkdir -p /mnt/rhel_iso
mount -o loop /usr/share/rhel-boot-image/rhel-boot-image-6.4-20130130.0.el6ost.iso /mnt/rhel_iso
cp -R /mnt/rhel_iso/. <%= pkgdir %>
umount /mnt/rhel_iso
rpm -e rhel-boot-image-6.4-20130130.0.el6ost.noarch
exit 0