Add --clean to kolla-mergepwd

to clean old keys on merge.

Change-Id: Ifcc99e7c737707eea9e951db066dc94fd85bd9f7
This commit is contained in:
Radosław Piliszek 2020-02-08 16:24:53 +01:00
parent 4200089716
commit ed225a36d8
3 changed files with 23 additions and 3 deletions

View File

@ -217,3 +217,8 @@ For example:
kolla-genpwd -p passwords.yml.new
kolla-mergepwd --old passwords.yml.old --new passwords.yml.new --final /etc/kolla/passwords.yml
.. note::
``kolla-mergepwd``, by default, keeps old, unused passwords intact.
To alter this behavior, and remove such entries, use the ``--clean``
argument when invoking ``kolla-mergepwd``.

View File

@ -16,14 +16,21 @@ import argparse
import yaml
def mergepwd(old, new, final):
def mergepwd(old, new, final, clean=False):
with open(old, "r") as old_file:
old_passwords = yaml.safe_load(old_file)
with open(new, "r") as new_file:
new_passwords = yaml.safe_load(new_file)
new_passwords.update(old_passwords)
if clean:
# keep only new keys
for key in new_passwords:
if key in old_passwords:
new_passwords[key] = old_passwords[key]
else:
# old behavior
new_passwords.update(old_passwords)
with open(final, "w") as destination:
yaml.safe_dump(new_passwords, destination, default_flow_style=False)
@ -34,8 +41,11 @@ def main():
parser.add_argument("--old", help="old password file", required=True)
parser.add_argument("--new", help="new password file", required=True)
parser.add_argument("--final", help="merged password file", required=True)
parser.add_argument("--clean",
help="clean (keep only new keys)",
action='store_true')
args = parser.parse_args()
mergepwd(args.old, args.new, args.final)
mergepwd(args.old, args.new, args.final, args.clean)
if __name__ == '__main__':

View File

@ -0,0 +1,5 @@
---
features:
- |
Adds ``--clean`` argument to ``kolla-mergepwd``. It allows to clean old
(not used anymore) keys from the passwords file.