-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create libvirt xml files from templates with python #343
Create libvirt xml files from templates with python #343
Conversation
96cad47
to
daf1cf1
Compare
import argparse | ||
import libvirt_helpers | ||
from string import Template | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are using flake8, I would suggest to follow some 'second-line' rules:
- Put first (in alphabetically order) the Python libraries (
argparse
andstring
) - Separated with a line, put the other imports (also sorted) non-python libs (
libvirt
,libvirt_helpers
)
daf1cf1
to
cd1e5d1
Compare
def main(): | ||
cloud = args.cloud | ||
cpuflags = libvirt_helpers.cpuflags() | ||
fout = open("/tmp/{0}-admin.xml".format(cloud), "w") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is not used until the end. I would suggest to use with
statement:
with open("/tmp/{0}-admin.xml".format(cloud), "w") as f:
f.write(xml)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for @aplanas's suggestion.
cd1e5d1
to
72738b5
Compare
libvirt_onhost_create_adminnode_config | ||
libvirt_onhost_create_admin_network_config | ||
onhost_local_repository_mount | ||
python ${mkcloud_lib_dir}/libvirt/admin-config.py $cloud $admin_node_memory $adminvcpus $emulator $admin_node_disk "$local_repository_mount" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can chmod a+x admin-config.py
and call as a normal command. In this case even I would recommend removing the .py
extension too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, these scripts should definitely be executable. +1 for @aplanas's suggestion here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Remember that git tracks file permissions too, not just contents.)
72738b5
to
854d209
Compare
fout.write(xml) | ||
fout.close() | ||
|
||
if __name__ == "__main__": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same in this file - currently this __name__
check is useless because your CLI-handling code is in the wrong place.
Great work, but please cleanly separate the CLI code from the library code like I suggested. |
eab4112
to
d313b98
Compare
thanks for all the input :) i will continue after lunch |
@aplanas @aspiers I would definitely need some more help on that from the python side, due to lack of expertise. |
0d91e4d
to
0032245
Compare
@MaximilianMeister I see. So far the logic behind this code is not big. Initially I would put all the functions in a single Python package, and create several CLI (as you did) that parse the args and call the appropriate (and slim) library. This approach can be used if mkcloud is finally written in Bash or Ruby. But if is done in Python we will need to refactor the library into several new packages with coupled functionality, because it will grow up a lot : ) Do we have a final decision about mkcloud? |
@aplanas No, we don't. And this is IMHO not related (direclty). The overall goal is to split mkcloud into separate tools (not just libraries). It should become separate tools that can be used individually, and mkcloud is using all of them. |
@jdsn @MaximilianMeister I can help to refactor this to create subcommands. I will do it after the merge of this PR. Can we provide some test file for this PR, so we can start the refactoring in a good way? |
I will try to setup some basic unittests as well for now. And maybe extract some of the logic in each CLI into the |
b4927d4
to
c0f91f5
Compare
No problem, but since then I explained how you can do it :-)
The bash -> python interface you have done is already good. You only need to change the scripts so they're also usable as modules.
Big -1 for a single script. Code should always be in small, clean chunks, where each chunk does one thing really well. I think you're too focused on the CLI aspect. Sure we can support a CLI as long as we need one, but the focus should be on writing reusable, testable, maintainable code, and the right way to do that is to ensure clean separation of concerns into separate modules, and to keep the CLI code separate from the business logic.
I suggest:
|
|
||
cloud = args.cloud | ||
cpuflags = libvirt_helpers.cpuflags() | ||
fin = libvirt_helpers.readfile("templates/admin-node.xml") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still not quite right. The template name is not specific to the CLI code so it doesn't belong here. The goal is ultimately to be able to write something like this from a Python-based mkcloud
caller:
import mkcloud.libvirt
...
mkcloud.libvirt.write_admin_node_xml(output_file, values)
targetbus = "virtio" | ||
else: | ||
nicmodel = "virtio" | ||
targetdev = "sda" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should cover all vd* sd*
591ea52
to
c9d91ec
Compare
libvirt_modprobe_kvm | ||
libvirt_start_daemon | ||
python ${mkcloud_lib_dir}/libvirt/net-start.py $cloud-admin || exit $? | ||
python ${mkcloud_lib_dir}/libvirt/vm-start.py $cloud-admin || exit $? | ||
${mkcloud_lib_dir}/libvirt/net-start $cloud-admin || exit $? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use full path to xml file here as well
3a18f72
to
3f5e763
Compare
👍 but waiting for results of final test run |
Module Fuctions: * helper methods * VM + Network XML file creation methods * VM + Network define/start methods * cleanup method Templates: * XML Templates for VM's, Admin Network, CPU-Flags Unittests and fixtures: * helper methods * VM + Network XML file creation methods Fixes SUSE-Cloud#345
* remove .py extension and call it without python * use libvirt_setup module for the business logic
* remove .py extension and call it without python * use libvirt_setup module for the business logic
* remove .py extension and call it without python * use libvirt_setup module for the business logic
3f5e763
to
425b50e
Compare
|
||
suseinstall: | ||
sudo zypper install perl-JSON-XS perl-libxml-perl | ||
sudo zypper install perl-JSON-XS perl-libxml-perl libvirt-python | ||
|
||
genericinstall: | ||
sudo pip install bashate flake8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may also install libvirt-python in the generic install via. pip
$ pip install libvirt-python
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I find this ugly. There are installations via zypper and others via pip as root. I would never install anything with pip as root in my system. I would suggest two options here:
- Because python-lake8 is in Factory, so I would submit python-bashate from Cloud:OpenStack:Master for Factory too.
- and/or use virtualenv to install all the python dependencies that are not in any repository.
I would suggest making the code more modular as Adam did. I would like to note that keeping the test cases in the same folder is a bad idea. May be after splitting mkcloud into multiple repositories, the test cases can lie in their special tests/ folder (but suggest to do it sooner than later). Some hard coding in the XML files, and some other minor issues which may be carried out in following patches. Looks good to me. +1 |
Create libvirt xml files from templates with python
The <features> was missing from the VM's libvirt config on AMD hosts, and this was causing a lack of ACPI support, which in turn meant that virsh shutdown etc. didn't work, and hence that mkcloud didn't work properly. Some history: A long time ago, 5696c16 originally introduced some logic so that if the 'npt' (AMD Nested Page Tables) flag was present in /proc/cpuinfo on the host, the <cpu> section would not be included. Later, PR SUSE-Cloud#343 switched libvirt VM creation code from bash to Python with templates, but at this point <features> was always included. Much more recently, 7fe19ff from PR SUSE-Cloud#1049 moved the <features> section into the CPU templates, resulting in it missing when the AMD npt flag was present. 668a415 then added support for other architectures, but still left AMD broken.
No description provided.