From 806976ca6e39bee24cb7dc811a8e2da529f93344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Caletka?= Date: Mon, 20 Aug 2018 15:25:27 +0200 Subject: [PATCH] Add smudge filter to replace $UNIXTIME directive on checkout --- README.rst | 24 ++++++++++++++++++++++++ dzonegit.py | 11 +++++++++++ setup.py | 1 + test_dzonegit.py | 9 ++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a347eae..3f24e51 100644 --- a/README.rst +++ b/README.rst @@ -15,6 +15,7 @@ Main features - check if zone file compiles properly using `named-compilezone(8)`_ - autodetect zone name from file name or ``$ORIGIN`` directive - enforce updating serial number when zone content is changed +- optional ``smudge`` filter to replace ``$UNIXTIME`` directive with current UNIX time - both ``pre-commit`` and ``pre-receive``/``update`` hooks to enforce similar checks in the remote repository - ``post-receive`` hook to checkout the working copy from a bare repository, generate config snippets for various DNS server software and reload them - only Python 3.5+ standard library is used @@ -53,6 +54,29 @@ Full instalation and usage documentation on how to add custom hooks`_ - on the server, set up the configuration options for each repository +Support for $UNIXTIME directive +------------------------------- + +If you want to use ``$UNIXTIME`` in your zone files instead of serial number, +you have to install a `smudge` filter on the server, that will replace the +directive with current unix time on checkout. First, set up the filter in +the Git configuration: + +.. code-block:: shell + + $ git config --global filter.dzonegit.smudge $(which dzonegit-smudge-serial) + $ git config --global filter.dzonegit.clean cat + + +Then, apply the filter on all zone files using ``.gitattributes`` file inside +the repository: + +.. code-block:: + + *.zone filter=dzonegit + + + Configuration options --------------------- diff --git a/dzonegit.py b/dzonegit.py index 0825342..b1ce3a2 100644 --- a/dzonegit.py +++ b/dzonegit.py @@ -509,6 +509,15 @@ def post_receive(stdin=sys.stdin): subprocess.run(cmd) +def smudge_serial( + bstdin=sys.stdin.buffer, + bstdout=sys.stdout.buffer, + unixtime=None, +): + """Replace all $UNIXTIME directives with current unix time.""" + bstdout.write(unixtime_directive(bstdin.read(), unixtime)) + + def get_action(argv=sys.argv): name = Path(argv[0]).name if "pre-commit" in name: @@ -519,6 +528,8 @@ def get_action(argv=sys.argv): return pre_receive if "post-receive" in name: return post_receive + if "smudge" in name: + return smudge_serial def main(): diff --git a/setup.py b/setup.py index 8f65d91..f0520ea 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,7 @@ setup( "dzonegit-pre-receive = dzonegit:pre_receive", "dzonegit-post-receive = dzonegit:post_receive", "dzonegit-update = dzonegit:update", + "dzonegit-smudge-serial = dzonegit:smudge_serial", ], }, classifiers=[ diff --git a/test_dzonegit.py b/test_dzonegit.py index 4e0337e..9c64b1d 100644 --- a/test_dzonegit.py +++ b/test_dzonegit.py @@ -4,7 +4,7 @@ import subprocess import time import datetime import os -from io import StringIO +from io import StringIO, BytesIO from pathlib import Path import dzonegit @@ -94,6 +94,13 @@ ns.example.com. 60 IN A 192.0.2.1 assert r.serial == str(123456) +def test_smudge_serial(): + bstdin = BytesIO(b"something $UNIXTIME something") + bstdout = BytesIO() + dzonegit.smudge_serial(bstdin, bstdout, 123456) + assert b"something 123456 something" == bstdout.getvalue() + + def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, "2018010100") assert dzonegit.is_serial_increased("2018010100", "4018010100")