Created
February 15, 2016 18:59
-
-
Save adamcstephens/5ee9066af32b6e5767e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# we need to register with Red Hat Satellite before we run Puppet | |
# to ensure that we can install packages. | |
# | |
# On a brand new Vagrant installation, we run rhnreg_ks and copy the | |
# systemid file to the host computer. | |
# | |
# If you later run `vagrant destroy; vagrant up`, the systemid file | |
# will be present on the host and copied into the Vagrant. | |
# | |
# This should help reduce the consumption of Red Hat Enterprise Linux | |
# Developer Suite licenses. | |
# | |
REL=`grep -Po '\d{1}' /etc/redhat-release | head -1` | |
KEY="vagrant-${REL}" | |
HOSTNAME=$(hostname -s) | |
BOXNAME=$(echo $HOSTNAME | cut -f1 -d\-) | |
USERNAME=$(echo $HOSTNAME | cut -f2 -d\-) | |
BACKUP_SID="/vagrant/.systemid-${REL}" | |
usage() { | |
cat << EOF | |
$0 [-d] [-f] [-h] | |
Options: | |
-d Debug | |
-f Force system re-registration | |
-h This help | |
EOF | |
} | |
while getopts ":fdh" opt | |
do | |
case $opt in | |
d) | |
set -x | |
;; | |
f) | |
FORCE='yes' | |
;; | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -%{OPTARG}" >&2 | |
echo | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
if [[ "$FORCE" == 'yes' ]] | |
then | |
echo "Forcing re-registration." | |
rm -rfv /vagrant/.systemid $BACKUP_SID /etc/sysconfig/rhn/systemid | |
fi | |
if [ ! -f /etc/sysconfig/rhn/systemid ]; then | |
# this system has not yet been registered with Satellite | |
if [ ! -f $BACKUP_SID ]; then | |
# and there is no systemid file on the host. Register with Satellite. | |
echo "Registering system ${HOSTNAME}" | |
/usr/sbin/rhnreg_ks --activationkey=$KEY --nohardware --nopackages --norhnsd | |
cp /etc/sysconfig/rhn/systemid $BACKUP_SID | |
else | |
# re-use the existing systemid. | |
echo "Reusing existing registration for system ${HOSTNAME}" | |
cp $BACKUP_SID /etc/sysconfig/rhn/systemid | |
fi | |
else | |
if [ ! -f $BACKUP_SID ]; then | |
# the systemid file had not been copied to the host system. | |
# This should only happen on Vagrant instances created before this | |
# script was updated. | |
cp /etc/sysconfig/rhn/systemid $BACKUP_SID | |
fi | |
echo "System registration exists." | |
fi | |
# run an rhn_check to verify this worked | |
echo "Checking registration." | |
rhn_check=$(rhn_check -v) | |
RETVAL=$? | |
if [ $RETVAL -eq 0 ] | |
then | |
echo "Successfully registered system ${HOSTNAME}." | |
else | |
if echo $rhn_check | grep -q "Invalid System Credentials" | |
then | |
echo "System no longer registered!" >&2 | |
echo >&2 | |
echo "Do ONE of the following:" >&2 | |
echo "a) vagrant ssh ${BOXNAME} -c 'sudo /vagrant/scripts/pre-provision.sh -f'" >&2 | |
echo "b) from within the VM, run: sudo /vagrant/scripts/pre-provision.sh -f" >&2 | |
echo >&2 | |
elif echo $rhn_check | grep -q "Possible networking problem?" | |
then | |
echo "Network connection issue. Assuming offline. Any package installations will fail." >&2 | |
RETVAL=0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LGTM