Last active
April 21, 2017 12:45
-
-
Save wyrmiyu/dd6de68694ba424bf412e6ae575051f2 to your computer and use it in GitHub Desktop.
Post-install script that can be run during first-boot of a virtual server. Assumes network configuration is in place and working. Goes through all its mac addresses and tries to use them to retrieve its hostname from Satellite. Once done, the hostname is used to retrieve a tailored post-install script from Satellite.
This file contains 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/bash | |
if [[ -e /.configure_satellite_client && $EUID = 0 ]]; then | |
SATELLITE_USERNAME=exampleuser | |
SATELLITE_PASSWORD=examplepass123 | |
SATELLITE_SERVER=satellite.example.com | |
FINISH_SCRIPT=/root/post_install_finish_script.sh | |
LOG_FILE=/root/post_install_finish_script.log | |
satellite_api=https://${SATELLITE_SERVER}/api/v2 | |
hostname= | |
mac_address= | |
exec &> >(tee -a "$LOG_FILE") 2>&1 | |
list_of_macs=$(ip link | grep "link/ether" | awk '{print $2}') | |
echo "Found following MAC addresses: $list_of_macs" | |
function get_json_value() { | |
local file="$1" | |
local key="$2" | |
cat "$file" \ | |
| python -c \ | |
"import sys, json; print(json.load(sys.stdin)['results'][0]['$key'])" \ | |
2>/dev/null | |
} | |
for mac in $list_of_macs; do | |
hostname=$(curl -Lks \ | |
--user "$SATELLITE_USERNAME":"$SATELLITE_PASSWORD" \ | |
-H "Content-Type:application/json" \ | |
-H "Accept:application/json" \ | |
"${satellite_api}/hosts?search=${mac}" \ | |
| get_json_value /dev/stdin name) | |
if [[ $hostname ]]; then | |
mac_address=$mac | |
break | |
fi | |
done | |
if [[ $hostname ]]; then | |
echo "Using following values:" | |
echo " - Hostname: $hostname" | |
echo " - MAC address: $mac_address" | |
else | |
echo "None of these MAC addresses exist in Satellite: $list_of_macs" | |
exit 1 | |
fi | |
curl -Lks -o ${FINISH_SCRIPT} \ | |
--user "$SATELLITE_USERNAME":"$SATELLITE_PASSWORD" \ | |
-H "Content-Type:application/json" \ | |
-H "Accept:application/json" \ | |
https://${SATELLITE_SERVER}/unattended/finish?hostname=${hostname} | |
chmod 0600 "$FINISH_SCRIPT" | |
bash -x "$FINISH_SCRIPT" | |
rm -f /.configure_satellite_client | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment