Skip to content

Commit

Permalink
Set up DNS in AWS Route53 with -z
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreprinetti committed Oct 14, 2024
1 parent f45eaab commit 095fb16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ openstack-server [-p] -f <flavor> -i <image> -e <external network> -k <key> <NAM
**Optional parameters:**

* `-p`: Do not clean up the server after creation (will print a cleanup script instead of executing it)
* `-z`: AWS Route53 zone ID. If provided, the script will attempt using the aws client to set up a DNS record

## Persistent configuration

The script will source a file in `${XDG_CONFIG_HOME:-${HOME}/.config}/openstack-server/${OS_CLOUD}.conf` before running, if it exists.
Any option passed through the configuration file are not required any more. Any options passed as flags will override the sourced configuration.
Any options passed through the configuration file are not required any more. Any options passed as flags will override the sourced configuration.

Here is an example valid configuration file:

Expand All @@ -33,4 +34,5 @@ server_flavor=m1.xlarge
server_image=fedora-35
external_network=external
key_name=mykey
aws_zone_id=/hostedzone/Z0H230324HJKGFD9HK345
```
71 changes: 69 additions & 2 deletions openstack-server
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ print_help() {
echo -e 'Optional parameters:'
echo -e '\t-d\tUser data to pass to the server on creation.'
echo -e '\t-p\tDo not clean up the server after creation'
echo -e '\t-z\tAWS Route53 zone ID. If provided, the script will attempt using the aws client to set up a DNS record'
echo -e '\t\t(will print a cleanup script instead of executing it).'
}

Expand All @@ -42,6 +43,7 @@ declare \
server_image='' \
key_name='' \
user_data='/dev/null' \
aws_zone_id='' \
external_network='external'

declare -r config_file="${XDG_CONFIG_HOME:-${HOME}/.config}/openstack-server/${OS_CLOUD}.conf"
Expand All @@ -50,14 +52,15 @@ if [[ -f "$config_file" ]]; then
# shellcheck disable=SC1090
source "$config_file"
fi
while getopts pf:i:e:k:d:h opt; do
while getopts pf:i:e:k:d:z:h opt; do
case "$opt" in
p) persistent='yes' ;;
f) server_flavor="$OPTARG" ;;
i) server_image="$OPTARG" ;;
e) external_network="$OPTARG" ;;
k) key_name="$OPTARG" ;;
d) user_data="$OPTARG" ;;
z) aws_zone_id="$OPTARG" ;;
h) print_help; exit 0 ;;
*) exit 1 ;;
esac
Expand All @@ -82,7 +85,13 @@ declare \
router_id='' \
port_id='' \
server_id='' \
fip_id=''
fip_id='' \
fip_ip='' \
aws_domain=''

if [[ -n "$aws_zone_id" ]]; then
aws_domain="$(aws route53 get-hosted-zone --id "${aws_zone_id}" | jq '.HostedZone.Name' -r)"
fi

cleanup() {
>&2 echo
Expand Down Expand Up @@ -110,6 +119,34 @@ cleanup() {
if [ -n "$sg_id" ]; then
openstack security group delete "$sg_id" || >&2 echo "Failed to delete security group $sg_id"
fi

if [[ -n "$aws_zone_id" ]]; then
declare record_name="${name}.${aws_domain}"

aws route53 change-resource-record-sets \
--hosted-zone-id="$aws_zone_id" \
--change-batch='
{
"Comment": "Delete the record for '"$record_name"'",
"Changes": [
{
"Action": "DELETE",
"ResourceRecordSet": {
"Name": "'"$record_name"'",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "'"$fip_ip"'"
}
]
}
}
]
}
'
fi

>&2 echo 'Cleanup done.'
}

Expand Down Expand Up @@ -175,6 +212,36 @@ fip_id="$(openstack floating ip create -f value -c id \
>&2 echo "Created floating IP ${fip_id} $(openstack floating ip show -f value -c floating_ip_address "$fip_id")"
openstack server add floating ip "$server_id" "$fip_id"

if [[ -n "$aws_zone_id" ]]; then
fip_ip="$(openstack floating ip show -f value -c 'floating_ip_address' "${fip_id}")"

declare record_name="${name}.${aws_domain}"

aws route53 change-resource-record-sets \
--hosted-zone-id="$aws_zone_id" \
--change-batch='
{
"Comment": "Upsert the record for '"$record_name"'",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "'"$record_name"'",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "'"$fip_ip"'"
}
]
}
}
]
}
'
>&2 echo "DNS record created: ${record_name} IN A ${fip_ip}"
fi

if [ "$persistent" == 'yes' ]; then
>&2 echo "Server created."
trap true EXIT
Expand Down

0 comments on commit 095fb16

Please sign in to comment.