From 2bce9ce5d3fd7d9b3b3775e5124f619f48f789e9 Mon Sep 17 00:00:00 2001 From: Renuka Manavalan <47282725+renukamanavalan@users.noreply.github.com> Date: Thu, 7 Nov 2019 06:55:37 -0800 Subject: [PATCH] Make configlet application script idempotent for updates. (#728) * Make script idempotent, for updates. Delete of non-existing table/keys is a no-op, hence can be ignored. * Name change --- scripts/configlet | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/configlet b/scripts/configlet index 8b58ee52a622..b622f011db37 100755 --- a/scripts/configlet +++ b/scripts/configlet @@ -100,14 +100,27 @@ def init(): def db_update(t, k, lst): init() - db.mod_entry(t, k, lst) + to_upd = False + data = db.get_entry(t, k) + for i in lst.keys(): + if not data.has_key(i) or data[i] != lst[i]: + to_upd = True + break -def db_filtered_upd(t, k, lst): + if to_upd: + db.mod_entry(t, k, lst) + +def db_delete_fields(t, k, lst): init() + to_set = False data = db.get_entry(t, k) for i in lst.keys(): - data.pop(i) - db.set_entry(t, k, data) + if data.has_key(i): + data.pop(i) + to_set = True + + if to_set: + db.set_entry(t, k, data) def db_delete_deep(t, k): @@ -119,7 +132,7 @@ def db_delete_deep(t, k): def db_delete(t, k, lst): init() if lst: - db_filtered_upd(t, k, lst) + db_delete_fields(t, k, lst) else: db_delete_deep(t, k)