* usr/lib/byobu/ec2_cost, usr/lib/byobu/hostname,

usr/lib/byobu/include/shutil, usr/lib/byobu/ip_address: LP: #894038
  - background all wgets, as these can be expensive (cough OpenStack cough)
  - sleep for 0.02 seconds after each wget, as most EC2 wgets take less
    than 0.01 seconds, but some might take longer
  - after backgrounded wget and sleep, attempt to read the results of the
    wget from a cache file;  if this succeeds, proceed and use it
  - if not, fail through gracefully, and pick up the results from the
    cach file on the next run
This commit is contained in:
Dustin Kirkland 2011-11-23 12:50:58 -06:00
commit 3aaa914c97
5 changed files with 54 additions and 22 deletions

9
debian/changelog vendored
View file

@ -10,6 +10,15 @@ byobu (4.50) unreleased; urgency=low
* usr/share/byobu/profiles/tmux:
- fix action bar color
* usr/lib/byobu/include/mondrian:
* usr/lib/byobu/ec2_cost, usr/lib/byobu/hostname,
usr/lib/byobu/include/shutil, usr/lib/byobu/ip_address: LP: #894038
- background all wgets, as these can be expensive (cough OpenStack cough)
- sleep for 0.02 seconds after each wget, as most EC2 wgets take less
than 0.01 seconds, but some might take longer
- after backgrounded wget and sleep, attempt to read the results of the
wget from a cache file; if this succeeds, proceed and use it
- if not, fail through gracefully, and pick up the results from the
cach file on the next run
-- Dustin Kirkland <kirkland@ubuntu.com> Fri, 18 Nov 2011 15:41:36 -0600

View file

@ -32,18 +32,19 @@ __ec2_cost_detail() {
}
__ec2_cost() {
local zone type file_to_stat cpu mem rate hours tx_gb rx_gb network_cost uptime_cost total_cost interface
local zone type file_to_stat cpu mem rate hours tx_gb rx_gb network_cost uptime_cost total_cost interface cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ec2_cost"
# Try to use metadata service
if metadata_available; then
zone=$(wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone)
if [ ! -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ]; then
zone="us-east-1d"
fi
[ -s "$cache.zone" ] || wget -q -O "$cache.zone" http://169.254.169.254/latest/meta-data/placement/availability-zone 2>/dev/null &
sleep 0.02
[ -s "$cache.zone" ] && read zone < "$cache.zone"
[ -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ] || zone="us-east-1d"
. "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}"
type=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-type | $SED -e "s/\./_/g")
[ -s "$cache.type" ] || wget -q -O "$cache.type" http://169.254.169.254/latest/meta-data/instance-type 2>/dev/null &
sleep 0.02
[ -s "$cache.type" ] && type=$($SED -e "s/\./_/g" "$cache.type")
eval rate="\$$type"
file_to_stat="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ec2_cost.instance_type"
[ -f "$file_to_stat" ] || wget -q -O "$file_to_stat" http://169.254.169.254/latest/meta-data/instance-type
file_to_stat="$cache.type"
fi
if [ -z "$rate" ]; then
. "$BYOBU_PREFIX/share/$PKG/ec2/rates."*

View file

@ -24,9 +24,13 @@ __hostname_detail() {
}
__hostname() {
local h=
if metadata_available; then
# We're in EC2, so get our public hostname
h=$(wget -q -O - http://169.254.169.254/latest/meta-data/public-hostname)
local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/hostname"
# Background a retrieval of our public hostname
wget -q -O "$cache" http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null &
sleep 0.02
[ -s "$cache" ] && read h < "$cache"
else
h=$(hostname -s 2>/dev/null || hostname)
fi

View file

@ -209,11 +209,21 @@ metadata_available() {
# This is really ugly. We need a reliable, fast way of determining
# if a metadata service is available, that does NOT slow down non-ec2
# machines.
if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then
wget -q -O- --timeout=1 --tries=1 http://169.254.169.254 >/dev/null 2>&1
local x=0 cache="$BYOBU_CONFIG_DIR/.metadata_available"
# First, check the cache
if [ -s "$cache" ]; then
# Metadata is non-empty, so we have metadata available
x=1
else
false
# Must seed the cache
if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then
# This *looks* like a machine with metadata, so background a potentially slow check
wget -q -O "$cache" --timeout=10 --tries=1 http://169.254.169.254 2>/dev/null &
sleep 0.02
[ -s "$cache" ] && x=1
fi
fi
[ "$x" = "1" ]
}
status_freq() {

View file

@ -26,7 +26,7 @@ __ip_address_detail() {
}
__ip_address() {
local interface ipaddr
local interface ipaddr cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ip_address"
# Allow interface overrides in $BYOBU_CONFIG_DIR/statusrc
if [ -n "$MONITORED_NETWORK" ]; then
interface="$MONITORED_NETWORK"
@ -39,21 +39,29 @@ __ip_address() {
case "$IPV6" in
1|true|yes)
if [ "$IP_EXTERNAL" = "1" ]; then
ipaddr=$(wget -O- -q http://v6.ipv6-test.com/api/myip.php)
# Background an update
wget -q -O "$cache" http://v6.ipv6-test.com/api/myip.php 2>/dev/null &
sleep 0.02
else
ipaddr=$(LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global 2>/dev/null)
# Background an update
LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global >"$cache" 2>/dev/null &
fi
[ -s "$cache" ] && read ipaddr < "$cache"
# Print 'None' if we have no global address
[ -z "$ipaddr" ] && ipaddr="None"
ipaddr=${ipaddr#* inet6 }
ipaddr=${ipaddr%%/*}
fi
;;
*)
if [ "$IP_EXTERNAL" = "1" ]; then
ipaddr=$(wget -O- -q http://v4.ipv6-test.com/api/myip.php)
wget -q -O "$cache" http://v4.ipv6-test.com/api/myip.php 2>/dev/null &
sleep 0.02
[ -s "$cache" ] && read ipaddr < "$cache"
elif metadata_available; then
# We're in EC2, so get our public IP address
ipaddr=$(wget -q -O- http://169.254.169.254/latest/meta-data/public-ipv4)
wget -q -O "$cache" http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null &
sleep 0.02
[ -s "$cache" ] && read ipaddr < "$cache"
else
ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global 2>/dev/null)
ipaddr=${ipaddr#* inet }