Browse Source

Delete older backups of config files

Andy Teijelo Pérez 6 years ago
parent
commit
cf61ba3500
3 changed files with 20 additions and 8 deletions
  1. 4 1
      dhcp-hosts.dpto2
  2. 1 1
      dhcp-opts.dpto2
  3. 15 6
      main.py

+ 4 - 1
dhcp-hosts.dpto2

@@ -96,10 +96,13 @@ ff:ff:ff:ff:ff:fe,10.6.122.94
 ff:ff:ff:ff:ff:fe,10.6.122.95
 # {"reserved-by": "Andy"}
 00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513
+# {"color": "red", "reserved-by": "Andy"}
 ff:ff:ff:ff:ff:fe,10.6.122.97
+# {"color": "gold", "reserved-by": "Andy"}
 ff:ff:ff:ff:ff:fe,10.6.122.98
-# {"color": "red", "reserved-by": "Zelda"}
+# {"color": "green", "reserved-by": "Zelda"}
 ff:ff:ff:ff:ff:fe,10.6.122.99
+# {"color": "blue", "reserved-by": "Andy"}
 ff:ff:ff:ff:ff:fe,10.6.122.100
 # {"reserved-by": "Andy"}
 12:24:42:23:24:45,10.6.122.101,set:client122442232445

+ 1 - 1
dhcp-opts.dpto2

@@ -1,2 +1,2 @@
-tag:client122442232445,option:router,10.6.122.2
 tag:client0023548c6513,option:router,10.6.122.96
+tag:client122442232445,option:router,10.6.122.2

+ 15 - 6
main.py

@@ -11,6 +11,7 @@ import hashlib
 import random
 import shutil
 import time
+import glob
 import sys
 import re
 import os
@@ -63,7 +64,7 @@ def delete_user(deluser):
             continue
         f.write(line)
     f.close()
-    shutil.move(tempfile, users_file)
+    rename(tempfile, users_file)
     reload_freeradius()
 
 def nthash(password):
@@ -107,7 +108,6 @@ def login():
 
         session['logged_in'] = True
         session['username'] = username
-        print("Redirecting to", request.args.get('redirect_to'))
         return redirect(request.args.get('redirect_to','admin'))
 
 @app.route("/")
@@ -314,6 +314,17 @@ def parse_macs(macs):
             r.append(m.group(0))
     return r
 
+def rename(src, dst):
+    shutil.move(src, dst)
+    shutil.copy(dst, src)
+    copies = glob.glob(dst + ".[0-9]*")
+    # sort from most recent to oldest
+    copies = sorted(copies, key=lambda x: -float(x[len(dst)+1:]))
+    # keep only the most recent 5
+    for c in copies[5:]:
+        print("Deleting", c)
+        os.remove(c)
+
 def write_dhcp_files(ipmap):
     timestamp = time.time()
     tempfile = "{}.{}".format(dhcp_hosts_file, timestamp)
@@ -343,8 +354,7 @@ def write_dhcp_files(ipmap):
                 settag = ",set:" + tag
                 gws[tag] = ip.gw
             print("{},{}.{}{}".format(macs,prefix,i,settag), file=f)
-    shutil.move(tempfile, dhcp_hosts_file)
-    shutil.copy(dhcp_hosts_file, tempfile)
+    rename(tempfile, dhcp_hosts_file)
 
     # Write custom gateways file
     tempfile = "{}.{}".format(dhcp_opts_file, timestamp)
@@ -354,8 +364,7 @@ def write_dhcp_files(ipmap):
                 "tag:{},option:router,{}".format(tag,gws[tag]),
                 file=f,
             )
-    shutil.move(tempfile, dhcp_opts_file)
-    shutil.copy(dhcp_opts_file, tempfile)
+    rename(tempfile, dhcp_opts_file)
 
 @app.route("/ip/<int:last_byte>", methods=['GET', 'POST'])
 @login_required