Support provider URI per machine definition (#1363)

Save the variables on the instance to allow for different connection
settings to be specified on a per machine basis.

Provide some rudimentary tests that ensure that the different URIs are
used when constructing connections, and additionally the connection is
created only once, no matter how many times it is subsequently called.

Fixes: #1358
This commit is contained in:
Darragh Bailey
2021-09-27 18:11:29 +01:00
committed by GitHub
parent de4721f62e
commit 605aabcd98
2 changed files with 96 additions and 11 deletions

View File

@@ -8,13 +8,15 @@ require 'json'
module VagrantPlugins
module ProviderLibvirt
class Driver
# store the connection at the process level
# store the connection at the instance level as this will be per
# thread and allows for individual machines to use different
# connection settings.
#
# possibly this should be a connection pool using the connection
# settings as a key to allow per machine connection attributes
# to be used.
@@connection = nil
@@system_connection = nil
# settings as a key to allow identical connections to be reused
# across machines.
@connection = nil
@system_connection = nil
def initialize(machine)
@logger = Log4r::Logger.new('vagrant_libvirt::driver')
@@ -24,7 +26,7 @@ module VagrantPlugins
def connection
# If already connected to Libvirt, just use it and don't connect
# again.
return @@connection if @@connection
return @connection if @connection
# Get config options for Libvirt provider.
config = @machine.provider_config
@@ -43,24 +45,24 @@ module VagrantPlugins
@logger.info("Connecting to Libvirt (#{uri}) ...")
begin
@@connection = Fog::Compute.new(conn_attr)
@connection = Fog::Compute.new(conn_attr)
rescue Fog::Errors::Error => e
raise Errors::FogLibvirtConnectionError,
error_message: e.message
end
@@connection
@connection
end
def system_connection
# If already connected to Libvirt, just use it and don't connect
# again.
return @@system_connection if @@system_connection
return @system_connection if @system_connection
config = @machine.provider_config
@@system_connection = Libvirt::open(config.system_uri)
@@system_connection
@system_connection = Libvirt::open(config.system_uri)
@system_connection
end
def get_domain(machine)