From 41bcae26e480cb051a790bc96b364bca326b1083 Mon Sep 17 00:00:00 2001 From: Dusty Mabe Date: Fri, 15 Nov 2019 17:11:36 -0500 Subject: [PATCH] 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. --- lib/vagrant-libvirt/config.rb | 46 +++++++++++++++++++++++++++----- spec/unit/config_spec.rb | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index c8e93ba..66504c6 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -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" diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 1708a72..c202d01 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -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