aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2017-02-15 14:09:46 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2017-02-15 14:26:48 +0200
commite847289f33de82eb4ef3d3213b13d93ff3087e8c (patch)
treeed66191eed5a71fa5d9b5fbdf4c783dbc8dd16cc
parent2decd27f66297e03b75c31923570ac5096a80cca (diff)
downloadscripts-e847289f33de82eb4ef3d3213b13d93ff3087e8c.tar.gz
scripts-e847289f33de82eb4ef3d3213b13d93ff3087e8c.tar.bz2
ec2setup: handle multiple dyndns domains; register RAM and CPU information
* ec2setup (machine_up): Register RAM and CPU information. (get_nsupdate_value): New function. (register_hostname,deregister_hostname) (register_cnames,deregister_cnames): Use get_nsupdate_value. proceed only if it succeeds. * ec2setup.def: Document changes to EC2_SETUP_NSUPDATE_SERVER and EC2_SETUP_NSUPDATE_KEY.
-rwxr-xr-xec2setup146
-rw-r--r--ec2setup.def15
2 files changed, 119 insertions, 42 deletions
diff --git a/ec2setup b/ec2setup
index 03a60e3..9d960bb 100755
--- a/ec2setup
+++ b/ec2setup
@@ -146,6 +146,14 @@ ip_status() {
echo "Assigned address $ip"
}
+memsize() {
+ free|sed -r -n 's/Mem:[[:space:]]+([0-9]+).*/\1/p'
+}
+
+numcpus() {
+ egrep -c '^processor[[:space:]]+:' /proc/cpuinfo
+}
+
# uses globals: $hostname, $descr, $commonname
machine_up() {
local cn=$commonname
@@ -186,6 +194,10 @@ else {
dump(.);
}' describe-instances instance-id=$id |
(cat -
+ echo "grayHostName: $hostname"
+ echo "ipHostNumber: $ip"
+ echo "grayRAMSize: " `memsize`
+ echo "grayCPUCount: " `numcpus`
test -n "$descr" && echo "description: $descr"
test -n "$EC2_SETUP_STATIC_INFO" && echo "$EC2_SETUP_STATIC_INFO") |
eval ldapadd $EC2_SETUP_LDAP_OPTIONS
@@ -212,7 +224,7 @@ update_dns() {
echo "$0: ignoring DNS updates"
cat >/dev/null
else
- nsupdate -k $EC2_SETUP_NSUPDATE_KEY 2>&1 |
+ nsupdate -k $1 2>&1 |
grep -v "update failed: NXRRSET"
fi
}
@@ -232,21 +244,56 @@ nsfilter() {
test -z "$s" || test "$s" = "$(makesig)" || test "$s" = "$id"
}
+get_nsupdate_value() {
+ local value="$1" domain="$2" ret
+ set -- $value
+ if [ $# -eq 1 ]; then
+ if ! echo "$value" | grep -q '^='; then
+ set -- ".=$value"
+ fi
+ fi
+ if ! echo "$domain" | grep -q '\.$'; then
+ domain="$domain."
+ fi
+
+ for i
+ do
+ x=$domain
+ while :
+ do
+ case $i in
+ ${x:-.}=*)
+ echo ${i##${x:-.}=}
+ return;;
+ esac
+
+ if [ -z "$x" ]; then
+ break
+ fi
+ x=${x#*.}
+ done
+ done
+}
+
# register_hostname
# Uses globals: $hostname, $ip, $descr, $id
register_hostname() {
local localzone
+ echo "$0: registering hostname $hostname, ip $ip"
if test -n "$EC2_SETUP_NSUPDATE_SERVER" &&
test -n "$EC2_SETUP_NSUPDATE_KEY"; then
if ! nsfilter "$hostname"; then
echo >&2 "$0: cannot update A record for $hostname: signature mismatch"
return
fi
-
- echo "$0: Updating DNS A records"
- (cat <<EOT
-server $EC2_SETUP_NSUPDATE_SERVER
+
+ server=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_SERVER" "$hostname")
+ key=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_KEY" "$hostname")
+ if test -n "$server" && test -n "$key"; then
+ echo "$0: Updating DNS A records on $server (key $key)"
+ (cat <<EOT
+server $server
prereq yxrrset _sig.$hostname TXT
update delete _sig.$hostname TXT
send
@@ -259,22 +306,24 @@ send
update add _sig.$hostname ${EC2_SETUP_TTL:-86400} TXT "$(makesig)"
update add $hostname ${EC2_SETUP_TTL:-86400} IN A $ip
EOT
- if test -n "$descr"; then
-# descr=$(echo $descr | sed 's/"/\"/g')
- echo "update add $hostname ${EC2_SETUP_TTL:-86400} IN TXT \"$descr\""
- fi
-
- echo send
- if test -n "$EC2_SETUP_LOCAL_ZONE"; then
+ if test -n "$descr"; then
+ # descr=$(echo $descr | sed 's/"/\"/g')
+ echo "update add $hostname ${EC2_SETUP_TTL:-86400} IN TXT \"$descr\""
+ fi
+
+ echo send) | update_dns $key
+ fi
+ if test -n "$EC2_SETUP_LOCAL_ZONE"; then
localzone=$(ec2_get_tag $EC2_SETUP_LOCAL_ZONE)
- else
- localzone=
- fi
-
- if test -n "$localzone"; then
- local_ip=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
- localname=${hostname%%.*}.$localzone
- cat <<EOT
+ server=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_SERVER" "$localzone")
+ key=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_KEY" "$localzone")
+ if test -n "$server" && test -n "$key"; then
+ echo "$0: Updating local DNS A records on $server (key $key)"
+
+ local_ip=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
+ localname=${hostname%%.*}.$localzone
+ (cat <<EOT
+server $server
prereq yxrrset $localname A
update delete $localname A
send
@@ -283,11 +332,12 @@ update delete $localname TXT
send
update add $localname ${EC2_SETUP_TTL:-86400} IN A $local_ip
EOT
- if test -n "$descr"; then
- echo "update add $localname ${EC2_SETUP_TTL:-86400} IN TXT \"$descr\""
- fi
- echo send
- fi) | update_dns
+ if test -n "$descr"; then
+ echo "update add $localname ${EC2_SETUP_TTL:-86400} IN TXT \"$descr\""
+ fi
+ echo send) | update_dns $key
+ fi
+ fi
fi
}
@@ -299,8 +349,11 @@ deregister_hostname() {
echo >&2 "$0: cannot update A record for $hostname: signature mismatch"
return
fi
- (cat <<EOT
-server $EC2_SETUP_NSUPDATE_SERVER
+ server=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_SERVER" "$hostname")
+ key=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_KEY" "$hostname")
+ if test -n "$server" && test -n "$key"; then
+ (cat <<EOT
+server $server
prereq yxrrset $hostname A
update delete $hostname A
send
@@ -312,14 +365,14 @@ update delete $hostname TXT
update add $hostname ${EC2_SETUP_TTL:-86400} IN TXT "Shut down on $(date)"
send
EOT
- if test -n "$EC2_SETUP_LOCAL_ZONE"; then
- localzone=$(ec2_get_tag $EC2_SETUP_LOCAL_ZONE)
- else
- localzone=
- fi
- if test -n "$EC2_SETUP_LOCAL_ZONE"; then
- localname=${hostname%%.*}.$localzone
- cat <<EOT
+ if test -n "$EC2_SETUP_LOCAL_ZONE"; then
+ localzone=$(ec2_get_tag $EC2_SETUP_LOCAL_ZONE)
+ else
+ localzone=
+ fi
+ if test -n "$EC2_SETUP_LOCAL_ZONE"; then
+ localname=${hostname%%.*}.$localzone
+ cat <<EOT
prereq yxrrset $localname A
update delete $localname A
send
@@ -327,7 +380,8 @@ prereq yxrrset $localname TXT
update delete $localname TXT
send
EOT
- fi) | update_dns
+ fi) | update_dns $key
+ fi
fi
}
@@ -387,7 +441,11 @@ register_cnames() {
continue
fi
- cat <<EOT
+ server=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_SERVER" "$cname")
+ key=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_KEY" "$cname")
+ if test -n "$server" && test -n "$key"; then
+ (cat <<EOT
+server $server
prereq yxrrset _sig.$cname TXT
update delete _sig.$cname TXT
send
@@ -398,7 +456,9 @@ update add _sig.$cname ${EC2_SETUP_TTL:-86400} TXT "$(makesig)"
update add $cname ${EC2_SETUP_TTL:-86400} CNAME $hostname
send
EOT
- done < $cnameslist | update_dns
+ ) | update_dns $key
+ fi
+ done < $cnameslist
fi
fi
}
@@ -418,7 +478,11 @@ deregister_cnames() {
continue
fi
- cat <<EOT
+ server=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_SERVER" "$cname")
+ key=$(get_nsupdate_value "$EC2_SETUP_NSUPDATE_KEY" "$cname")
+ if test -n "$server" && test -n "$key"; then
+ (cat <<EOT
+server $server
prereq yxrrset _sig.$cname TXT
update delete _sig.$cname TXT
send
@@ -426,7 +490,9 @@ prereq yxrrset $cname CNAME
update delete $cname CNAME
send
EOT
- done < $cnameslist | update_dns
+ ) | update_dns $key
+ fi
+ done < $cnameslist
rm $cnameslist
fi
fi
diff --git a/ec2setup.def b/ec2setup.def
index dba219d..04d235e 100644
--- a/ec2setup.def
+++ b/ec2setup.def
@@ -67,10 +67,21 @@ EC2_SETUP_TAG_IP=ipaddr
# Variables in this session configure access to the dynamic DNS. #
# ######################################################################## #
-# IP address of the name server
+# IP address of the name server. If you have several domains with different
+# servers, use the following syntax:
+#
+# "DOMAIN1=SERVER1 DOMAIN2=SERVER2"
+#
+# Each DOMAIN[X] should end with a dot.
+#
+# When looking for a match, ec2setup will continuously strip leading
+# name components from the domain name, until the remaining string
+# matches one of the domains in the EC2_SETUP_NSUPDATE_SERVER value.
+# Thus, the entry .=SERVER will match any domain name.
EC2_SETUP_NSUPDATE_SERVER=
# Nmae of the key file, suitable as an argument to the -k option of
-# nsupdate(1). E.g.:
+# nsupdate(1). If you have several domains, use the same syntax as
+# for EC2_SETUP_NSUPDATE_SERVER.
#EC2_SETUP_NSUPDATE_KEY=/etc/ec2setup/Kfoobar.+157+12345
# If this tag is specified, its value controls whether or not to update the

Return to:

Send suggestions and report system problems to the System administrator.