-
Notifications
You must be signed in to change notification settings - Fork 72
Add libvirt support #182
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
Draft
sbernhard
wants to merge
1
commit into
theforeman:master
Choose a base branch
from
ATIX-AG:add_libvirt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add libvirt support #182
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/models/concerns/foreman_bootdisk/compute_resources/libvirt.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ForemanBootdisk | ||
| module ComputeResources | ||
| module Libvirt | ||
| def capabilities | ||
| super + [:bootdisk] | ||
| end | ||
|
|
||
| def set_boot_order(attr = {}) | ||
| order = super(attr) | ||
| order.unshift 'cdrom' if attr[:provision_method] == 'bootdisk' | ||
| order | ||
| end | ||
|
|
||
| def iso_upload(iso, _vm_uuid) | ||
| client.upload_iso(libvirt_iso_pool_name, File.basename(iso), iso) | ||
| end | ||
|
|
||
| def iso_attach(iso, vm_uuid) | ||
| client.attach_iso(vm_uuid, File.basename(iso)) | ||
| end | ||
|
|
||
| def iso_detach(vm_uuid) | ||
| client.detach_iso(vm_uuid) | ||
| end | ||
|
|
||
| def iso_delete(iso, _vm_uuid = nil) | ||
| client.destroy_iso(libvirt_iso_pool_name, File.basename(iso)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def libvirt_iso_pool_name | ||
| if respond_to?(:volumes) && volumes.present? | ||
| pool = volumes.first.try(:pool_name) || volumes.first.try(:[], 'pool_name') | ||
| return pool if pool.present? | ||
| end | ||
|
|
||
| 'default' | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_plugin_helper' | ||
|
|
||
| module ForemanBootdisk | ||
| class LibvirtTest < ActiveSupport::TestCase | ||
| class DummyLibvirtComputeResource | ||
| prepend ForemanBootdisk::ComputeResources::Libvirt | ||
|
|
||
| attr_reader :client | ||
|
|
||
| def initialize(client, volumes = []) | ||
| @client = client | ||
| @volumes = volumes | ||
| end | ||
|
|
||
| def capabilities | ||
| [:image] | ||
| end | ||
|
|
||
| def volumes | ||
| @volumes | ||
| end | ||
|
|
||
| def set_boot_order(_attr = {}) | ||
| %w[hd] | ||
| end | ||
| end | ||
|
|
||
| setup do | ||
| @client = mock('client') | ||
| @cr = DummyLibvirtComputeResource.new(@client, [OpenStruct.new(pool_name: 'bootdisk')]) | ||
| end | ||
|
|
||
| describe '#capabilities' do | ||
| test 'should include bootdisk' do | ||
| assert_includes @cr.capabilities, :bootdisk | ||
| end | ||
| end | ||
|
|
||
| describe '#iso_upload' do | ||
| test 'calls fog-libvirt upload_iso' do | ||
| @client.expects(:upload_iso).with('bootdisk', 'test.iso', '/tmp/test.iso') | ||
| @cr.iso_upload('/tmp/test.iso', '1234') | ||
| end | ||
| end | ||
|
|
||
| describe '#iso_attach' do | ||
| test 'calls fog-libvirt attach_iso' do | ||
| @client.expects(:attach_iso).with('1234', 'test.iso') | ||
| @cr.iso_attach('test.iso', '1234') | ||
| end | ||
| end | ||
|
|
||
| describe '#iso_detach' do | ||
| test 'calls fog-libvirt detach_iso' do | ||
| @client.expects(:detach_iso).with('1234', nil) | ||
| @cr.iso_detach('1234') | ||
| end | ||
| end | ||
|
|
||
| describe '#iso_delete' do | ||
| test 'calls fog-libvirt destroy_iso' do | ||
| @client.expects(:destroy_iso).with('bootdisk', 'test.iso') | ||
| @cr.iso_delete('test.iso') | ||
| end | ||
| end | ||
|
|
||
| describe '#set_boot_order' do | ||
| test 'prepends cdrom when provision_method is bootdisk' do | ||
| order = @cr.set_boot_order(provision_method: 'bootdisk') | ||
| assert_equal %w[cdrom network hd], order | ||
| end | ||
|
|
||
| test 'returns base order when provision_method is not bootdisk' do | ||
| order = @cr.set_boot_order(provision_method: 'image') | ||
| assert_equal %w[hd], order | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nitpick: Is the
respond_to?(:volumes)necessary?The libvirt.rb heavily relies on it, so I think it's safe to assume that Libvirt responds to volumes.