From: Alfredo Deza Date: Wed, 4 Jun 2014 20:38:37 +0000 (-0400) Subject: make template for custom repo a callable X-Git-Tag: v1.5.4~4^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8980d926615affe16bb61ff0be5fe055d7117b03;p=ceph-deploy.git make template for custom repo a callable Signed-off-by: Alfredo Deza --- diff --git a/ceph_deploy/util/templates.py b/ceph_deploy/util/templates.py index 4f9eef4..e001070 100644 --- a/ceph_deploy/util/templates.py +++ b/ceph_deploy/util/templates.py @@ -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)