diff --git a/attributes/default.rb b/attributes/default.rb index 88f6544b..f227e6d2 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -223,6 +223,9 @@ when "fedora", "redhat", "centos", "suse" # :pragma-foodcritic: ~FC024 - won't f if platform == "suse" default["openstack"]["compute"]["platform"]["common_packages"] = ["openstack-nova"] default["openstack"]["compute"]["ceilometer"]["api"]["auth"]["cache_dir"] = "/var/cache/ceilometer" + default["openstack"]["compute"]["platform"]["kvm_packages"] = ["kvm"] + default["openstack"]["compute"]["platform"]["xen_packages"] = ["kernel-xen", "xen", "xen-tools"] + default["openstack"]["compute"]["platform"]["lxc_packages"] = ["lxc"] end when "ubuntu" diff --git a/recipes/libvirt.rb b/recipes/libvirt.rb index 6750ac0f..cfefcef6 100644 --- a/recipes/libvirt.rb +++ b/recipes/libvirt.rb @@ -26,6 +26,85 @@ platform_options["libvirt_packages"].each do |pkg| end end +def set_boot_kernel_and_trigger_reboot(flavor='default') + # only default and xen flavor is supported by this helper right now + default_boot, current_default = 0, nil + + # parse menu.lst, to find boot index for selected flavor + File.open('/boot/grub/menu.lst') do |f| + f.lines.each do |line| + current_default = line.scan(/\d/).first.to_i if line.start_with?('default') + + if line.start_with?('title') + if flavor.eql?('xen') + # found boot index + break if line.include?('Xen') + else + # take first kernel as default, unless we are searching for xen + # kernel + break + end + default_boot += 1 + end + end + end + + # change default option for /boot/grub/menu.lst + unless current_default.eql?(default_boot) + puts "changed grub default to #{default_boot}" + %x[sed -i -e "s;^default.*;default #{default_boot};" /boot/grub/menu.lst] + end + + # trigger reboot through reboot_handler, if kernel-$flavor is not yet + # running + unless %x[uname -r].include?(flavor) + node.run_state["reboot"] = true + end +end + +# on suse nova-compute don't depends on any virtualization mechanism +case node["platform"] +when "suse" + case node["openstack"]["compute"]["libvirt"]["virt_type"] + when "kvm" + node["openstack"]["compute"]["platform"]["kvm_packages"].each do |pkg| + package pkg do + action :install + end + end + execute "loading kvm modules" do + command "grep -q vmx /proc/cpuinfo && /sbin/modprobe kvm-intel; grep -q svm /proc/cpuinfo && /sbin/modprobe kvm-amd; /sbin/modprobe vhost-net" + end + # NOTE(saschpe): Allow switching from XEN to KVM: + set_boot_kernel_and_trigger_reboot + + when "xen" + node["openstack"]["compute"]["platform"]["xen_packages"].each do |pkg| + package pkg do + action :install + end + end + set_boot_kernel_and_trigger_reboot('xen') + + when "qemu" + node["openstack"]["compute"]["platform"]["kvm_packages"].each do |pkg| + package pkg do + action :install + end + end + + when "lxc" + node["openstack"]["compute"]["platform"]["lxc_packages"].each do |pkg| + package pkg do + action :install + end + end + service "boot.cgroup" do + action [:enable, :start] + end + end +end + group node["openstack"]["compute"]["libvirt"]["group"] do append true members [node["openstack"]["compute"]["group"]] diff --git a/spec/libvirt-opensuse_spec.rb b/spec/libvirt-opensuse_spec.rb new file mode 100644 index 00000000..655098d7 --- /dev/null +++ b/spec/libvirt-opensuse_spec.rb @@ -0,0 +1,88 @@ +require "spec_helper" + +describe "openstack-compute::libvirt" do + describe "suse" do + before do + compute_stubs + @chef_run = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS + @chef_run.converge "openstack-compute::libvirt" + end + + it "installs libvirt packages" do + expect(@chef_run).to install_package "libvirt" + end + + it "starts libvirt" do + expect(@chef_run).to start_service "libvirtd" + end + + it "starts libvirt on boot" do + expect(@chef_run).to set_service_to_start_on_boot "libvirtd" + end + + describe "libvirtd" do + before do + @file = @chef_run.template "/etc/sysconfig/libvirtd" + end + + it "has proper owner" do + expect(@file).to be_owned_by "root", "root" + end + + it "has proper modes" do + expect(sprintf("%o", @file.mode)).to eq "644" + end + + it "template contents" do + pending "TODO: implement" + end + + it "notifies libvirt-bin restart" do + expect(@file).to notify "service[libvirt-bin]", :restart + end + end + + it "installs kvm packages" do + expect(@chef_run).to install_package "kvm" + end + + it "installs qemu packages" do + chef_run = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS do |node| + node.set["openstack"]["compute"]["libvirt"]["virt_type"] = "qemu" + end + chef_run.converge "openstack-compute::libvirt" + expect(chef_run).to install_package "kvm" + end + + it "installs xen packages" do + chef_run = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS do |node| + node.set["openstack"]["compute"]["libvirt"]["virt_type"] = "xen" + end + chef_run.converge "openstack-compute::libvirt" + ["kernel-xen", "xen", "xen-tools"].each do |pkg| + expect(chef_run).to install_package pkg + end + end + + describe "lxc" do + before do + @chef_run_lxc = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS do |node| + node.set["openstack"]["compute"]["libvirt"]["virt_type"] = "lxc" + end + @chef_run_lxc.converge "openstack-compute::libvirt" + end + + it "installs packages" do + expect(@chef_run_lxc).to install_package "lxc" + end + + it "starts boot.cgroupslxc" do + expect(@chef_run_lxc).to start_service "boot.cgroup" + end + + it "starts boot.cgroups on boot" do + expect(@chef_run_lxc).to set_service_to_start_on_boot "boot.cgroup" + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d8745a85..7cd57cdb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,11 @@ require "chefspec" ::LOG_LEVEL = :fatal +::OPENSUSE_OPTS = { + :platform => "opensuse", + :version => "12.3", + :log_level => ::LOG_LEVEL +} ::REDHAT_OPTS = { :platform => "redhat", :version => "6.3",