]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
make template for custom repo a callable
authorAlfredo Deza <alfredo.deza@inktank.com>
Wed, 4 Jun 2014 20:38:37 +0000 (16:38 -0400)
committerAlfredo Deza <alfredo.deza@inktank.com>
Wed, 4 Jun 2014 20:38:37 +0000 (16:38 -0400)
Signed-off-by: Alfredo Deza <alfredo.deza@inktank.com>
ceph_deploy/util/templates.py

index 4f9eef44d54403db2c9f783693c89bcf5846842e..e00107062e5022c091001cc8010988cded91121f 100644 (file)
@@ -26,13 +26,58 @@ type=rpm-md
 gpgkey={gpg_url}
 """
 
-custom_repo = """
-[{repo_name}]
-name={name}
-baseurl={baseurl}
-enabled={enabled}
-gpgcheck={gpgcheck}
-type={_type}
-gpgkey={gpgkey}
-proxy={proxy}
-"""
+
+def custom_repo(**kw):
+    """
+    Repo files need special care in that a whole line should not be present
+    if there is no value for it. Because we were using `format()` we could
+    not conditionally add a line for a repo file. So the end result would
+    contain a key with a missing value (say if we were passing `None`).
+
+    For example, it could look like::
+
+        [ceph repo]
+        name= ceph repo
+        proxy=
+        gpgcheck=
+
+    Which breaks. This function allows us to conditionally add lines,
+    preserving an order and be more careful.
+
+    Previously, and for historical purposes, this is how the template used
+    to look::
+
+        custom_repo = """
+        [{repo_name}]
+        name={name}
+        baseurl={baseurl}
+        enabled={enabled}
+        gpgcheck={gpgcheck}
+        type={_type}
+        gpgkey={gpgkey}
+        proxy={proxy}
+        """
+
+    """
+    lines = []
+
+    # by using tuples (vs a dict) we preserve the order of what we want to
+    # return, like starting with a [repo name]
+    tmpl = (
+        ('reponame', '[%s]'),
+        ('baseurl', 'baseurl=%s'),
+        ('enabled', 'enabled=%s'),
+        ('gpgcheck', 'gpgcheck=%s'),
+        ('_type', 'type=%s'),
+        ('gpgkey', 'gpgkey=%s'),
+        ('proxy', 'proxy=%s'),
+    )
+
+    for line in tmpl:
+        tmpl_key, tmpl_value = line  # key values from tmpl
+
+        # ensure that there is an actual value (not None nor empty string)
+        if tmpl_key in kw and kw.get(tmpl_key) not in (None, ''):
+            lines.append(tmpl_value % kw.get(tmpl_key))
+
+    return '\n'.join(lines)