From 95a750817f87cbc00c7eff896ca6d6e74b2ca5b1 Mon Sep 17 00:00:00 2001 From: Jake Teton-Landis Date: Sun, 8 Jun 2014 17:52:05 -0700 Subject: [PATCH] adds a `uri` config option The existing system of trying to intelligently generate a libvirt URI from the user's config options was too restrictive for my uses. I have added a `uri` config option that allows the user to directly specify the libvirt resource they wish to connect to. This makes using vagrant-libvirt with existing practices easier, and allows connecting successfully in more environments (such as those that use qemu:///session) --- lib/vagrant-libvirt/action/connect_libvirt.rb | 45 +------------- lib/vagrant-libvirt/config.rb | 58 ++++++++++++++++++- 2 files changed, 57 insertions(+), 46 deletions(-) diff --git a/lib/vagrant-libvirt/action/connect_libvirt.rb b/lib/vagrant-libvirt/action/connect_libvirt.rb index abb879a..6fdb282 100644 --- a/lib/vagrant-libvirt/action/connect_libvirt.rb +++ b/lib/vagrant-libvirt/action/connect_libvirt.rb @@ -11,7 +11,6 @@ module VagrantPlugins end def call(env) - # If already connected to libvirt, just use it and don't connect # again. if ProviderLibvirt.libvirt_connection @@ -21,49 +20,7 @@ module VagrantPlugins # Get config options for libvirt provider. config = env[:machine].provider_config - - # Setup connection uri. - uri = config.driver.dup - virt_path = case uri - when 'qemu', 'openvz', 'uml', 'phyp', 'parallels', 'kvm' - '/system' - when 'xen', 'esx' - '/' - when 'vbox', 'vmwarews', 'hyperv' - '/session' - else - raise "Require specify driver #{uri}" - end - if uri == 'kvm' - uri = 'qemu' # use qemu uri for kvm domain type - end - - if config.connect_via_ssh - uri << '+ssh://' - if config.username - uri << config.username + '@' - end - - if config.host - uri << config.host - else - uri << 'localhost' - end - else - uri << '://' - uri << config.host if config.host - end - - uri << virt_path - uri << '?no_verify=1' - - if config.id_ssh_key_file - # set ssh key for access to libvirt host - home_dir = `echo ${HOME}`.chomp - uri << "\&keyfile=#{home_dir}/.ssh/"+config.id_ssh_key_file - end - # set path to libvirt socket - uri << "\&socket="+config.socket if config.socket + uri = config.uri conn_attr = {} conn_attr[:provider] = 'libvirt' diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index bf23192..a586348 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -12,6 +12,10 @@ end module VagrantPlugins module ProviderLibvirt class Config < Vagrant.plugin('2', :config) + # manually specify URI + # will supercede most other options if provided + attr_accessor :uri + # A hypervisor name to access via Libvirt. attr_accessor :driver @@ -19,8 +23,7 @@ module VagrantPlugins attr_accessor :host # If use ssh tunnel to connect to Libvirt. - attr_accessor :connect_via_ssh - + attr_accessor :connect_via_ssh # Path towards the libvirt socket attr_accessor :socket @@ -122,6 +125,54 @@ module VagrantPlugins end end + # code to generate URI from a config moved out of the connect action + def _generate_uri + # builds the libvirt connection URI from the given driver config + # Setup connection uri. + uri = @driver.dup + virt_path = case uri + when 'qemu', 'openvz', 'uml', 'phyp', 'parallels', 'kvm' + '/system' + when '@en', 'esx' + '/' + when 'vbox', 'vmwarews', 'hyperv' + '/session' + else + raise "Require specify driver #{uri}" + end + if uri == 'kvm' + uri = 'qemu' # use qemu uri for kvm domain type + end + + if @connect_via_ssh + uri << '+ssh://' + if @username + uri << @username + '@' + end + + if @host + uri << @host + else + uri << 'localhost' + end + else + uri << '://' + uri << @host if @host + end + + uri << virt_path + uri << '?no_verify=1' + + if @id_ssh_key_file + # set ssh key for access to libvirt host + home_dir = `echo ${HOME}`.chomp + uri << "\&keyfile=#{home_dir}/.ssh/"+@id_ssh_key_file + end + # set path to libvirt socket + uri << "\&socket="+@socket if @socket + return uri + end + def finalize! @driver = 'kvm' if @driver == UNSET_VALUE @host = nil if @host == UNSET_VALUE @@ -133,6 +184,9 @@ module VagrantPlugins @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 + # generate a URI if none is supplied + @uri = _generate_uri() if @uri == UNSET_VALUE + # Domain specific settings. @memory = 512 if @memory == UNSET_VALUE @cpus = 1 if @cpus == UNSET_VALUE