mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
config: allow for setting URI from environment
Users are allowed to set a LIBVIRT_DEFAULT_URI environment variable that controls tools (i.e. virsh, virt-install, etc) that communicate with libvirt. Let's allow for that mechanism to be used here.
This commit is contained in:
committed by
Darragh Bailey
parent
d16bdcc1dc
commit
41bcae26e4
@@ -622,14 +622,21 @@ module VagrantPlugins
|
||||
@qemu_env.merge!(options)
|
||||
end
|
||||
|
||||
# code to generate URI from a config moved out of the connect action
|
||||
def _generate_uri
|
||||
# code to generate URI from from either the LIBVIRT_URI environment
|
||||
# variable or a config moved out of the connect action
|
||||
def _generate_uri(qemu_use_session)
|
||||
|
||||
# If the LIBVIRT_DEFAULT_URI var is set, we'll use that
|
||||
if ENV.fetch('LIBVIRT_DEFAULT_URI', '') != ""
|
||||
return ENV['LIBVIRT_DEFAULT_URI']
|
||||
end
|
||||
|
||||
# builds the Libvirt connection URI from the given driver config
|
||||
# Setup connection uri.
|
||||
uri = @driver.dup
|
||||
virt_path = case uri
|
||||
when 'qemu', 'kvm'
|
||||
@qemu_use_session ? '/session' : '/system'
|
||||
qemu_use_session ? '/session' : '/system'
|
||||
when 'openvz', 'uml', 'phyp', 'parallels'
|
||||
'/system'
|
||||
when '@en', 'esx'
|
||||
@@ -670,6 +677,14 @@ module VagrantPlugins
|
||||
uri
|
||||
end
|
||||
|
||||
def _parse_uri(uri)
|
||||
begin
|
||||
URI.parse(uri)
|
||||
rescue
|
||||
raise "@uri set to invalid uri '#{uri}'"
|
||||
end
|
||||
end
|
||||
|
||||
def finalize!
|
||||
@driver = 'kvm' if @driver == UNSET_VALUE
|
||||
@host = nil if @host == UNSET_VALUE
|
||||
@@ -693,10 +708,21 @@ module VagrantPlugins
|
||||
@management_network_domain = nil if @management_network_domain == UNSET_VALUE
|
||||
@system_uri = 'qemu:///system' if @system_uri == UNSET_VALUE
|
||||
|
||||
@qemu_use_session = false if @qemu_use_session == UNSET_VALUE
|
||||
# If uri isn't set then let's build one from various sources.
|
||||
# Default to passing false for qemu_use_session if it's not set.
|
||||
if @uri == UNSET_VALUE
|
||||
@uri = _generate_uri(@qemu_use_session == UNSET_VALUE ? false : @qemu_use_session)
|
||||
end
|
||||
|
||||
# generate a URI if none is supplied
|
||||
@uri = _generate_uri if @uri == UNSET_VALUE
|
||||
# Set qemu_use_session based on the URI if it wasn't set by the user
|
||||
if @qemu_use_session == UNSET_VALUE
|
||||
uri = _parse_uri(@uri)
|
||||
if (uri.scheme.start_with? "qemu") && (uri.path.include? "session")
|
||||
@qemu_use_session = true
|
||||
else
|
||||
@qemu_use_session = false
|
||||
end
|
||||
end
|
||||
|
||||
# Domain specific settings.
|
||||
@title = '' if @title == UNSET_VALUE
|
||||
@@ -818,6 +844,14 @@ module VagrantPlugins
|
||||
def validate(machine)
|
||||
errors = _detected_errors
|
||||
|
||||
# The @uri and @qemu_use_session should not conflict
|
||||
uri = _parse_uri(@uri)
|
||||
if (uri.scheme.start_with? "qemu") && (uri.path.include? "session")
|
||||
if @qemu_use_session != true
|
||||
errors << "the URI and qemu_use_session configuration conflict: uri:'#{@uri}' qemu_use_session:'#{@qemu_use_session}'"
|
||||
end
|
||||
end
|
||||
|
||||
machine.provider_config.disks.each do |disk|
|
||||
if disk[:path] && (disk[:path][0] == '/')
|
||||
errors << "absolute volume paths like '#{disk[:path]}' not yet supported"
|
||||
|
||||
@@ -18,6 +18,54 @@ describe VagrantPlugins::ProviderLibvirt::Config do
|
||||
stub_const("ENV", fake_env)
|
||||
end
|
||||
|
||||
context 'when LIBVIRT_DEFAULT_URI is defined' do
|
||||
it 'should always use this value' do
|
||||
fake_env['LIBVIRT_DEFAULT_URI'] = "custom:///custom_path"
|
||||
|
||||
subject.finalize!
|
||||
expect(subject.uri).to eq("custom:///custom_path")
|
||||
expect(subject.qemu_use_session).to eq(false)
|
||||
end
|
||||
|
||||
context 'when LIBVIRT_DEFAULT_URI contains "qemu"' do
|
||||
[
|
||||
[
|
||||
'set qemu_use_session if "session" present',
|
||||
'qemu:///session',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'handle different protocol additions',
|
||||
'qemu+ssh:///session',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'handle options before and after path',
|
||||
'qemu://remote/session?keyfile=my_id_rsa',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'identify when session not set',
|
||||
'qemu://remote/system',
|
||||
false,
|
||||
],
|
||||
[
|
||||
'handle session appearing elsewhere',
|
||||
'qemu://remote/system?keyfile=my_session_id',
|
||||
false,
|
||||
],
|
||||
].each do |title, uri, session|
|
||||
it "should #{title}" do
|
||||
fake_env['LIBVIRT_DEFAULT_URI'] = uri
|
||||
|
||||
subject.finalize!
|
||||
expect(subject.uri).to eq(uri)
|
||||
expect(subject.qemu_use_session).to eq(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when @driver is defined' do
|
||||
defaults = {'id_ssh_key_file' => nil}
|
||||
[
|
||||
@@ -130,11 +178,13 @@ describe VagrantPlugins::ProviderLibvirt::Config do
|
||||
end
|
||||
|
||||
def assert_invalid
|
||||
subject.finalize!
|
||||
errors = subject.validate(machine)
|
||||
raise "No errors: #{errors.inspect}" if errors.values.all?(&:empty?)
|
||||
end
|
||||
|
||||
def assert_valid
|
||||
subject.finalize!
|
||||
errors = subject.validate(machine)
|
||||
raise "Errors: #{errors.inspect}" unless errors.values.all?(&:empty?)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user