ipapply:  Disable for an hour when /tmp/ipapply_disabled exists.

This is useful for gftests which need ipapply not to mess with
statically configured IP addresses.  When the file is at least an hour
old, the effect will be disabled (so as to prevent testbeds from
getting stranded).

Change-Id: I2801415a358daa6852db337e727446e47740b547
diff --git a/fs/skeleton/bin/ipapply b/fs/skeleton/bin/ipapply
index e11d4ef..d2117cf 100755
--- a/fs/skeleton/bin/ipapply
+++ b/fs/skeleton/bin/ipapply
@@ -23,6 +23,8 @@
 import struct
 import subprocess
 import sys
+import time
+
 import options
 
 optspec = """
@@ -38,6 +40,7 @@
 STATIC_IP_CONFIGS_DIR = base_path + '/config/ip/static'
 DYNAMIC_IP_CONFIGS_DIR = base_path + '/tmp/ip/dynamic'
 TMP_CONMAN_DIR = base_path + '/tmp/conman'
+DISABLED_FILE = base_path + '/tmp/ipapply_disabled'
 
 
 def AtomicWrite(filename, data):
@@ -270,7 +273,21 @@
   return ''.join((ApplyNetmask(ip, netmask), '/', netmask))
 
 
+def Disabled():
+  """Returns whether /tmp/ipapply_disabled has been written in the last hour."""
+  if os.path.exists(DISABLED_FILE):
+    disabled_s = (os.stat(DISABLED_FILE).st_mtime + 60 * 60) - time.time()
+    if disabled_s > 0:
+      Log('Disabled for the next %d seconds', disabled_s)
+      return True
+
+  return False
+
+
 def main():
+  if Disabled():
+    return
+
   o = options.Options(optspec)
   (_, _, extra) = o.parse(sys.argv[1:])
   if not extra:
diff --git a/fs/skeleton/bin/ipapply.test b/fs/skeleton/bin/ipapply.test
index c167122..87ea074 100755
--- a/fs/skeleton/bin/ipapply.test
+++ b/fs/skeleton/bin/ipapply.test
@@ -10,6 +10,7 @@
 STATIC_PATH=/config/ip/static
 DYNAMIC_PATH=/tmp/ip/dynamic
 TMP_CONMAN=/tmp/conman
+DISABLED_FILE=/tmp/ipapply_disabled
 
 runnable mktemp &&
 tmpdir=$(mktemp -d ipapplyXXXXXXXXXX) ||
@@ -56,6 +57,8 @@
   rm -rf $tmpdir/$STATIC_PATH
   rm -rf $tmpdir/$DYNAMIC_PATH
   rm -rf $tmpdir/$TMP_CONMAN
+  rm -f $tmpdir/$DISABLED_FILE
+
 
   mkdir -p $tmpdir/$STATIC_PATH
   mkdir -p $tmpdir/$DYNAMIC_PATH
@@ -82,7 +85,7 @@
 WVPASSEQ $? 0
 
 echo
-echo "TEST static config"
+echo "TEST static config and disable"
 export IIU_TEST_UP_INTERFACES="iface0"
 mkdir -p $tmpdir/$STATIC_PATH
 cat >$tmpdir/$STATIC_PATH/iface0 <<-EOF
@@ -93,6 +96,14 @@
 
 
 echo "1.1.1.0/24 dev br0  proto kernel  scope link  src 1.1.1.1" >"$tmpdir/ip_route_show"
+
+touch $tmpdir/$DISABLED_FILE
+output=$(ipapply iface0 2>&1 1>/dev/null)
+WVPASSEQ $? 0
+echo "${output}" | grep "Disabled for the next" >/dev/null
+WVPASSEQ $? 0
+
+rm $tmpdir/$DISABLED_FILE
 output=$(ipapply iface0 2>&1 1>/dev/null)
 WVPASSEQ $? 0
 echo "${output}" | grep "ip addr add 1.1.1.1/24 dev iface0" >/dev/null