[lago-devel] 'Persistent' IP addresses for the hosts

Barak Korren bkorren at redhat.com
Thu Mar 31 05:59:11 UTC 2016


On 30 March 2016 at 22:31, Yaniv Kaul <ykaul at redhat.com> wrote:
> On Wed, Mar 30, 2016 at 5:33 PM, Barak Korren <bkorren at redhat.com> wrote:

>> Maybe we can live-generate the file from the lago status output?
>
>
> Indeed...
>
> <ugly-hack-not-tested-yet>
> with open('../robo.conf.in') as f:
>         robo_conf_in = f.read()
>
>     replacements = {
>         '@HOST0@': prefix.virt_env.get_vm(_get_prefixed_name('host0').ip(),
>         '@HOST1@': prefix.virt_env.get_vm(_get_prefixed_name('host1').ip(),
>         '@HOST2@': prefix.virt_env.get_vm(_get_prefixed_name('host2').ip(),
>     }
>     for k, v in replacements.items():
>         robo_conf_out = robo_conf_in.replace(k, v, 1)
>
>     with open('../robo.conf', 'w') as out:
>         out.write(robo_conf_out)
>         out.close()
>
> </ugly-hack-not-tested-yet>


If you don't expect robo.conf.in to come from an untrusted source,
consider using string.format with a dict rather then looping with
string.replace.

Also consider constructing the dict with a mapping comprehension expression
looping over the hosts (See [1] for tutorial of the syntax, you can use the
{..foo..} syntax if you don't care about Python 2.6 compatibility or the
dict(...) syntax if you do)

[1]: http://www.bogotobogo.com/python/python_dictionary_comprehension_with_zip_from_list.php

-- 
Barak Korren
bkorren at redhat.com
RHEV-CI Team



More information about the lago-devel mailing list