From b0fda502f427aeb1dc66fa3ee893d9c4a9020c8c Mon Sep 17 00:00:00 2001
From: Nicolas Wavrant <nicolas.wavrant@nexedi.com>
Date: Wed, 31 Aug 2016 09:41:00 +0200
Subject: [PATCH] resilient: takeover CGI doesn't fail if no backup has been
 received yet.

Also makes the interface more readable
---
 stack/resilient/buildout.cfg                  |  2 +-
 .../resilient-web-takeover-cgi-script.py.in   | 33 +++++++++++++------
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/stack/resilient/buildout.cfg b/stack/resilient/buildout.cfg
index 2b7300aa2..a05e09913 100644
--- a/stack/resilient/buildout.cfg
+++ b/stack/resilient/buildout.cfg
@@ -95,7 +95,7 @@ output = ${buildout:directory}/instance-frozen.cfg
 [resilient-web-takeover-cgi-script-download]
 recipe = slapos.recipe.build:download
 url = ${:_profile_base_location_}/resilient-web-takeover-cgi-script.py.in
-md5sum = 3aa7624af1196062d7d01946d4de9f0e
+md5sum = 38a4f723924a674c4bc1be1b9ffd2c3c
 mode = 0644
 destination = ${buildout:directory}/resilient-web-takeover-cgi-script.py.in
 
diff --git a/stack/resilient/resilient-web-takeover-cgi-script.py.in b/stack/resilient/resilient-web-takeover-cgi-script.py.in
index 894e2bdc2..a27a6153c 100644
--- a/stack/resilient/resilient-web-takeover-cgi-script.py.in
+++ b/stack/resilient/resilient-web-takeover-cgi-script.py.in
@@ -27,12 +27,18 @@ def getLatestBackupDate():
   # Usually, there is only one callback (so only one key
   # in the db), but if there are several:
   # Take the "oldest" one (oldest value).
-  last_backup = db[db.keys()[0]]
-  for callback in db.keys():
-    timestamp = float(db[callback])
-    if timestamp < last_backup:
-      last_backup = timestamp
-  return datetime.datetime.fromtimestamp(last_backup)
+  if not db.keys():
+    result = False
+  else:
+    last_backup = db[db.keys()[0]]
+    for callback in db.keys():
+      timestamp = float(db[callback])
+      if timestamp < last_backup:
+        last_backup = timestamp
+    result = datetime.datetime.fromtimestamp(last_backup)
+  db.close()
+  shutil.rmtree(temporary_directory)
+  return result
 
 def isBackupInProgress():
   """
@@ -42,6 +48,12 @@ def isBackupInProgress():
   # XXX: check if file is valid
   return os.path.exists(equeue_lockfile)
 
+latest_backup_date = getLatestBackupDate()
+if latest_backup_date == False:
+  latest_backup_message = "No backup downloaded yet, takeover should not happen now."
+else:
+  latest_backup_message = latest_backup_date.strftime('%Y-%m-%d %H:%M:%S')
+
 print "Content-Type: text/html"
 print
 
@@ -51,16 +63,17 @@ if "password" not in form:
 <body>
   <h1>This is takeover web interface.</h1>
   <p>Calling takeover will stop and freeze the current main instance, and make this clone instance the new main instance, replacing the old one.</p>
-  <p><b>Warning: submit the form only if you understand what you are doing.</b></p>
+  <p><font size=\"+2\"><b>Warning: submit the form only if you understand what you are doing.</b></font></p>
   <p>Note: the password asked here can be found within the parameters of your SlapOS instance page.</p>
-  <p>Last valid backup: %s</p>
-  <p>Importer script(s) of backup in progress: %s</p>
+  <hr />
+  <p><b>Last valid backup:</b> %s</p>
+  <p><b>Importer script(s) of backup in progress:</b> %s</p>
   <form action="/">
     Password: <input type="text" name="password">
     <input type="submit" value="Take over" style="background: red;">
   </form>
 </body>
-</html>""" % (getLatestBackupDate().strftime('%Y-%m-%d %H:%M:%S'), isBackupInProgress())
+</html>""" % (latest_backup_message, isBackupInProgress())
   sys.exit(0)
 
 if form['password'].value != '${:password}':
-- 
2.30.9