Last active
January 27, 2023 19:51
-
-
Save inthuriel/08cf61a3c3514cbafacd9a788c4b50ba to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
## | |
# Simle script to update domain resolve in OC masters | |
## | |
set -eu | |
AWS_PROFILE="" | |
HOSTED_ZONE_ID="" | |
DOMAIN="" | |
HOST_FILE_PATH="" | |
ANSIBLE_INVENTORY_PATH="" | |
function log() { | |
# Sample logging | |
stamp="$(date "+%Y-%m-%d %T")" | |
echo -ne "${stamp} $@" >&2 | |
} | |
function getDomainAlias() { | |
# Get domain info from Route53, in this case it's alias | |
domain_alias="$(aws route53 list-resource-record-sets --profile "${1}" --hosted-zone-id "${2}" \ | |
| jq -rc '.[][] | select(.Name == "'"${3}."'" and .Type == "'"A"'") | .AliasTarget.DNSName' \ | |
| sed 's/\.$//')"; | |
log "Geting details for ${3} form AWS zone ${2} using profile ${1}\n" | |
log "Domain alias for ${3} is ${domain_alias}\n" | |
} | |
function getAliasIp() { | |
# Get IPs of given alias using `host` command | |
domain_ips="$(host -t A "${1}" | grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | sed ':a;N;$!ba;s/\n/ /g')" | |
log "IPs for alias ${1} are ${domain_ips}\n" | |
} | |
function updateIpsInStaticFiles() { | |
# Update /etc/hosts infromation | |
ips=(${1}) | |
command="#!/usr/bin/env bash\nips=(${ips[@]}); for (( i=0; i<\${#ips[*]}; i++ )); do sed -ri ':a;N;\$!ba;s/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ${DOMAIN}/'\"\${ips[\$i]}\"' ${DOMAIN}/'\"\$((i+1))\"'' ${2}; done" | |
tmp_dir_path="$(mktemp -d)" | |
echo -en "$command" > "${tmp_dir_path}/script.sh" | |
log "Changing ${DOMAIN} IPs to: ${ips[@]}\n" | |
ansible -i "${ANSIBLE_INVENTORY_PATH}" 'masters' -b -m script -a "${tmp_dir_path}/script.sh" | |
ansible -i "${ANSIBLE_INVENTORY_PATH}" 'masters' -b -m shell -a 'cat '"${HOST_FILE_PATH}"'' | |
log "Restarting master services\n" | |
ansible -i "${ANSIBLE_INVENTORY_PATH}" 'masters' -b -m shell -a 'systemctl restart master-controllers' | |
} | |
# run functions | |
getDomainAlias "${AWS_PROFILE}" "${HOSTED_ZONE_ID}" "${DOMAIN}" | |
getAliasIp "${domain_alias}" | |
updateIpsInStaticFiles "${domain_ips}" "${HOST_FILE_PATH}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment