Make LIBVIRT_DEFAULT_URI a fackball mechanism (#1223)

Switch to only picking up LIBVIRT_DEFAULT_URI if no settings that could
affect the uri have been explicitly set. Some of these may not actually
appear in the URI depending on what is actually set or not set, however
it is important that should only use the env variable if the user has
not explicitly configured options relevant and instead allow them to
configure as needed.
This commit is contained in:
Darragh Bailey
2021-03-17 14:41:14 +00:00
committed by GitHub
parent b15489c557
commit 4ee40728b3
3 changed files with 50 additions and 37 deletions

View File

@@ -404,6 +404,14 @@ URI](http://libvirt.org/uri.html):
vagrant-libvirt should use. Overrides all other connection configuration
options
In the event that none of these are set (excluding the `driver` option) the
provider will attempt to retrieve the uri from the environment variable
`LIBVIRT_DEFAULT_URI` similar to how virsh works. If any of them are set, it
will ignore the environment variable. The reason the driver option is ignored
is that it is not uncommon for this to be explicitly set on the box itself
and there is no easily to determine whether it is being set by the user or
the box packager.
Connection-independent options:
* `storage_pool_name` - Libvirt storage pool name, where box image and instance

View File

@@ -191,6 +191,7 @@ module VagrantPlugins
@username = UNSET_VALUE
@password = UNSET_VALUE
@id_ssh_key_file = UNSET_VALUE
@socket = UNSET_VALUE
@storage_pool_name = UNSET_VALUE
@snapshot_pool_name = UNSET_VALUE
@random_hostname = UNSET_VALUE
@@ -672,15 +673,25 @@ module VagrantPlugins
@qemu_env.merge!(options)
end
def _default_uri
# Determine if any settings except driver provided explicitly, if not
# and the LIBVIRT_DEFAULT_URI var is set, use that.
#
# Skipping driver because that may be set on individual boxes rather
# than by the user.
if [
@connect_via_ssh, @host, @username, @password,
@id_ssh_key_file, @qemu_use_session, @socket,
].none?{ |v| v != UNSET_VALUE }
if ENV.fetch('LIBVIRT_DEFAULT_URI', '') != ""
@uri = ENV['LIBVIRT_DEFAULT_URI']
end
end
end
# 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
@@ -736,27 +747,16 @@ module VagrantPlugins
end
def finalize!
_default_uri if @uri == UNSET_VALUE
# settings which _generate_uri
@driver = 'kvm' if @driver == UNSET_VALUE
@host = nil if @host == UNSET_VALUE
@connect_via_ssh = false if @connect_via_ssh == UNSET_VALUE
@username = nil if @username == UNSET_VALUE
@password = nil if @password == UNSET_VALUE
@id_ssh_key_file = 'id_rsa' if @id_ssh_key_file == UNSET_VALUE
@storage_pool_name = 'default' if @storage_pool_name == UNSET_VALUE
@snapshot_pool_name = @storage_pool_name if @snapshot_pool_name == UNSET_VALUE
@storage_pool_path = nil if @storage_pool_path == UNSET_VALUE
@random_hostname = false if @random_hostname == UNSET_VALUE
@management_network_device = 'virbr0' if @management_network_device == UNSET_VALUE
@management_network_name = 'vagrant-libvirt' if @management_network_name == UNSET_VALUE
@management_network_address = '192.168.121.0/24' if @management_network_address == UNSET_VALUE
@management_network_mode = 'nat' if @management_network_mode == UNSET_VALUE
@management_network_mac = nil if @management_network_mac == UNSET_VALUE
@management_network_guest_ipv6 = 'yes' if @management_network_guest_ipv6 == UNSET_VALUE
@management_network_autostart = false if @management_network_autostart == UNSET_VALUE
@management_network_pci_bus = nil if @management_network_pci_bus == UNSET_VALUE
@management_network_pci_slot = nil if @management_network_pci_slot == UNSET_VALUE
@management_network_domain = nil if @management_network_domain == UNSET_VALUE
@system_uri = 'qemu:///system' if @system_uri == UNSET_VALUE
@socket = nil if @socket == 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.
@@ -782,6 +782,22 @@ module VagrantPlugins
@username = uri.user if @username == nil
end
@storage_pool_name = 'default' if @storage_pool_name == UNSET_VALUE
@snapshot_pool_name = @storage_pool_name if @snapshot_pool_name == UNSET_VALUE
@storage_pool_path = nil if @storage_pool_path == UNSET_VALUE
@random_hostname = false if @random_hostname == UNSET_VALUE
@management_network_device = 'virbr0' if @management_network_device == UNSET_VALUE
@management_network_name = 'vagrant-libvirt' if @management_network_name == UNSET_VALUE
@management_network_address = '192.168.121.0/24' if @management_network_address == UNSET_VALUE
@management_network_mode = 'nat' if @management_network_mode == UNSET_VALUE
@management_network_mac = nil if @management_network_mac == UNSET_VALUE
@management_network_guest_ipv6 = 'yes' if @management_network_guest_ipv6 == UNSET_VALUE
@management_network_autostart = false if @management_network_autostart == UNSET_VALUE
@management_network_pci_bus = nil if @management_network_pci_bus == UNSET_VALUE
@management_network_pci_slot = nil if @management_network_pci_slot == UNSET_VALUE
@management_network_domain = nil if @management_network_domain == UNSET_VALUE
@system_uri = 'qemu:///system' if @system_uri == UNSET_VALUE
# Domain specific settings.
@title = '' if @title == UNSET_VALUE
@description = '' if @description == UNSET_VALUE

View File

@@ -123,41 +123,30 @@ describe VagrantPlugins::ProviderLibvirt::Config do
{:uri => 'qemu:///system'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
],
[ # when driver explicitly set
{:driver => 'qemu'},
{:uri => 'qemu:///system?no_verify=1'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
[ # when host explicitly set
{:host => 'remote'},
{:uri => 'qemu://remote/system?no_verify=1'},
{:uri => 'qemu://remote/system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
[ # when connect_via_ssh explicitly set
{:connect_via_ssh => true},
{:uri => 'qemu+ssh:///system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{:uri => 'qemu+ssh://localhost/system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
[ # when username explicitly set without host
{:username => 'my_user' },
{:uri => 'qemu://my_user@localhost/system?no_verify=1'},
{:uri => 'qemu:///system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
[ # when username explicitly set with host
{:username => 'my_user', :host => 'remote'},
{:uri => 'qemu://my_user@remote/system?no_verify=1'},
{:uri => 'qemu://remote/system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
[ # when password explicitly set
{:password => 'some_password'},
{:uri => 'qemu:///system?no_verify=1'},
{:uri => 'qemu:///system?no_verify=1&keyfile=/home/tests/.ssh/id_rsa'},
{'LIBVIRT_DEFAULT_URI' => 'qemu://session'},
"LIBVIRT_DEFAULT_URI is not yet just a fallback behaviour"
],
# driver settings