From 981ca18b5ead0937a6b7ae6e1dce1bdd7761fba1 Mon Sep 17 00:00:00 2001 From: dima Date: Tue, 6 Dec 2016 16:59:28 +0100 Subject: [PATCH] amazing rubocop -a autofix --- lib/vagrant-libvirt/action/create_domain.rb | 51 ++++++------- .../action/create_domain_volume.rb | 24 +++--- .../action/create_network_interfaces.rb | 76 +++++++++---------- lib/vagrant-libvirt/action/create_networks.rb | 60 +++++++-------- 4 files changed, 101 insertions(+), 110 deletions(-) diff --git a/lib/vagrant-libvirt/action/create_domain.rb b/lib/vagrant-libvirt/action/create_domain.rb index 8873220..74c4e8c 100644 --- a/lib/vagrant-libvirt/action/create_domain.rb +++ b/lib/vagrant-libvirt/action/create_domain.rb @@ -12,7 +12,7 @@ module VagrantPlugins end def _disk_name(name, disk) - "#{name}-#{disk[:device]}.#{disk[:type]}" # disk name + "#{name}-#{disk[:device]}.#{disk[:type]}" # disk name end def _disks_print(disks) @@ -55,10 +55,10 @@ module VagrantPlugins @graphics_autoport = config.graphics_autoport @graphics_port = config.graphics_port @graphics_ip = config.graphics_ip - @graphics_passwd = if config.graphics_passwd.to_s.empty? - '' - else - "passwd='#{config.graphics_passwd}'" + @graphics_passwd = if config.graphics_passwd.to_s.empty? + '' + else + "passwd='#{config.graphics_passwd}'" end @video_type = config.video_type @video_vram = config.video_vram @@ -88,13 +88,13 @@ module VagrantPlugins # USB device passthrough @usbs = config.usbs - + # Redirected devices @redirdevs = config.redirdevs @redirfilters = config.redirfilters # RNG device passthrough - @rng =config.rng + @rng = config.rng config = env[:machine].provider_config @domain_type = config.driver @@ -108,7 +108,8 @@ module VagrantPlugins x.pool_name == @storage_pool_name end domain_volume = ProviderLibvirt::Util::Collection.find_matching( - actual_volumes,"#{@name}.img") + actual_volumes, "#{@name}.img" + ) raise Errors::DomainVolumeExists if domain_volume.nil? @domain_volume_path = domain_volume.path end @@ -117,12 +118,12 @@ module VagrantPlugins # If not, we dump the storage pool xml to get its defined path. # the default storage prefix is typically: /var/lib/libvirt/images/ if env[:machine].config.vm.box - storage_prefix = File.dirname(@domain_volume_path) + '/' # steal + storage_prefix = File.dirname(@domain_volume_path) + '/' # steal else storage_pool = env[:machine].provider.driver.connection.client.lookup_storage_pool_by_name(@storage_pool_name) raise Errors::NoStoragePool if storage_pool.nil? xml = Nokogiri::XML(storage_pool.xml_desc) - storage_prefix = xml.xpath("/pool/target/path").inner_text.to_s + '/' + storage_prefix = xml.xpath('/pool/target/path').inner_text.to_s + '/' end @disks.each do |disk| @@ -148,10 +149,11 @@ module VagrantPlugins path: disk[:absolute_path], capacity: disk[:size], #:allocation => ?, - pool_name: @storage_pool_name) + pool_name: @storage_pool_name + ) rescue Fog::Errors::Error => e raise Errors::FogDomainVolumeCreateError, - error_message: e.message + error_message: e.message end else disk[:preexisting] = true @@ -161,9 +163,7 @@ module VagrantPlugins # Output the settings we're going to use to the user env[:ui].info(I18n.t('vagrant_libvirt.creating_domain')) env[:ui].info(" -- Name: #{@name}") - if @uuid != '' - env[:ui].info(" -- Forced UUID: #{@uuid}") - end + env[:ui].info(" -- Forced UUID: #{@uuid}") if @uuid != '' env[:ui].info(" -- Domain type: #{@domain_type}") env[:ui].info(" -- Cpus: #{@cpus}") @cpu_features.each do |cpu_feature| @@ -193,7 +193,7 @@ module VagrantPlugins env[:ui].info(" -- Boot device: #{device}") end - if @disks.length > 0 + unless @disks.empty? env[:ui].info(" -- Disks: #{_disks_print(@disks)}") end @@ -205,7 +205,7 @@ module VagrantPlugins env[:ui].info(msg) end - if @cdroms.length > 0 + unless @cdroms.empty? env[:ui].info(" -- CDROMS: #{_cdroms_print(@cdroms)}") end @@ -226,7 +226,7 @@ module VagrantPlugins env[:ui].info(" -- PCI passthrough: #{pci[:bus]}:#{pci[:slot]}.#{pci[:function]}") end - if !@rng[:model].nil? + unless @rng[:model].nil? env[:ui].info(" -- RNG device model: #{@rng[:model]}") end @@ -239,17 +239,16 @@ module VagrantPlugins env[:ui].info(" -- USB passthrough: #{usb_dev.join(', ')}") end - if not @redirdevs.empty? - env[:ui].info(" -- Redirected Devices: ") + unless @redirdevs.empty? + env[:ui].info(' -- Redirected Devices: ') @redirdevs.each do |redirdev| msg = " -> bus=usb, type=#{redirdev[:type]}" env[:ui].info(msg) end end - - if not @redirfilters.empty? - env[:ui].info(" -- USB Device filter for Redirected Devices: ") + unless @redirfilters.empty? + env[:ui].info(' -- USB Device filter for Redirected Devices: ') @redirfilters.each do |redirfilter| msg = " -> class=#{redirfilter[:class]}, " msg += "vendor=#{redirfilter[:vendor]}, " @@ -260,7 +259,6 @@ module VagrantPlugins end end - env[:ui].info(" -- Command line : #{@cmd_line}") # Create libvirt domain. @@ -268,9 +266,10 @@ module VagrantPlugins # existing volume? Use domain creation from template.. begin server = env[:machine].provider.driver.connection.servers.create( - xml: to_xml('domain')) + xml: to_xml('domain') + ) rescue Fog::Errors::Error => e - raise Errors::FogCreateServerError, error_message: e.message + raise Errors::FogCreateServerError, error_message: e.message end # Immediately save the ID since it is created at this point. diff --git a/lib/vagrant-libvirt/action/create_domain_volume.rb b/lib/vagrant-libvirt/action/create_domain_volume.rb index e5c1acf..02077e9 100644 --- a/lib/vagrant-libvirt/action/create_domain_volume.rb +++ b/lib/vagrant-libvirt/action/create_domain_volume.rb @@ -3,20 +3,19 @@ require 'log4r' module VagrantPlugins module ProviderLibvirt module Action - # Create a snapshot of base box image. This new snapshot is just new # cow image with backing storage pointing to base box image. Use this # image as new domain volume. class CreateDomainVolume include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate - def initialize(app, env) - @logger = Log4r::Logger.new("vagrant_libvirt::action::create_domain_volume") + def initialize(app, _env) + @logger = Log4r::Logger.new('vagrant_libvirt::action::create_domain_volume') @app = app end def call(env) - env[:ui].info(I18n.t("vagrant_libvirt.creating_domain_volume")) + env[:ui].info(I18n.t('vagrant_libvirt.creating_domain_volume')) # Get config options. config = env[:machine].provider_config @@ -26,33 +25,34 @@ module VagrantPlugins # Verify the volume doesn't exist already. domain_volume = ProviderLibvirt::Util::Collection.find_matching( - env[:machine].provider.driver.connection.volumes.all, @name) + env[:machine].provider.driver.connection.volumes.all, @name + ) raise Errors::DomainVolumeExists if domain_volume # Get path to backing image - box volume. box_volume = ProviderLibvirt::Util::Collection.find_matching( - env[:machine].provider.driver.connection.volumes.all, env[:box_volume_name]) + env[:machine].provider.driver.connection.volumes.all, env[:box_volume_name] + ) @backing_file = box_volume.path # Virtual size of image. Take value worked out by HandleBoxImage - @capacity = env[:box_virtual_size] #G + @capacity = env[:box_virtual_size] # G # Create new volume from xml template. Fog currently doesn't support # volume snapshots directly. begin domain_volume = env[:machine].provider.driver.connection.volumes.create( - :xml => to_xml('volume_snapshot'), - :pool_name => config.storage_pool_name) + xml: to_xml('volume_snapshot'), + pool_name: config.storage_pool_name + ) rescue Fog::Errors::Error => e raise Errors::FogDomainVolumeCreateError, - :error_message => e.message + error_message: e.message end @app.call(env) end end - end end end - diff --git a/lib/vagrant-libvirt/action/create_network_interfaces.rb b/lib/vagrant-libvirt/action/create_network_interfaces.rb index e5dc6dc..aef97d7 100644 --- a/lib/vagrant-libvirt/action/create_network_interfaces.rb +++ b/lib/vagrant-libvirt/action/create_network_interfaces.rb @@ -5,7 +5,6 @@ require 'vagrant/util/scoped_hash_override' module VagrantPlugins module ProviderLibvirt module Action - # Create network interfaces for domain, before domain is running. # Networks for connecting those interfaces should be already prepared. class CreateNetworkInterfaces @@ -27,10 +26,11 @@ module VagrantPlugins # Get domain first. begin domain = env[:machine].provider.driver.connection.client.lookup_domain_by_uuid( - env[:machine].id.to_s) + env[:machine].id.to_s + ) rescue => e raise Errors::NoDomainError, - :error_message => e.message + error_message: e.message end # Setup list of interfaces before creating them. @@ -39,11 +39,10 @@ module VagrantPlugins # Vagrant gives you adapter 0 by default # Assign interfaces to slots. configured_networks(env, @logger).each do |options| - # dont need to create interface for this type next if options[:iface_type] == :forwarded_port - # TODO fill first ifaces with adapter option specified. + # TODO: fill first ifaces with adapter option specified. if options[:adapter] if adapters[options[:adapter]] raise Errors::InterfaceSlotNotAvailable @@ -54,13 +53,14 @@ module VagrantPlugins else free_slot = find_empty(adapters) @logger.debug "Adapter not specified so found slot #{free_slot}" - raise Errors::InterfaceSlotNotAvailable if free_slot == nil + raise Errors::InterfaceSlotNotAvailable if free_slot.nil? end # We have slot for interface, fill it with interface configuration. adapters[free_slot] = options adapters[free_slot][:network_name] = interface_network( - env[:machine].provider.driver.connection.client, adapters[free_slot]) + env[:machine].provider.driver.connection.client, adapters[free_slot] + ) end # Create each interface as new domain device. @@ -95,18 +95,17 @@ module VagrantPlugins end # default mcast tunnel to 239.255.1.1. Web search says this # 239.255.x.x is a safe range to use for general use mcast - if @type == 'mcast' - default_ip = '239.255.1.1' - else - default_ip = '127.0.0.1' - end + default_ip = if @type == 'mcast' + '239.255.1.1' + else + '127.0.0.1' + end @tunnel_ip = iface_configuration.fetch(:tunnel_ip, default_ip) @model_type = iface_configuration.fetch(:model_type, @nic_model_type) template_name = 'tunnel_interface' @logger.info("Setting up #{@type} tunnel interface using #{@tunnel_ip} port #{@tunnel_port}") end - message = "Creating network interface eth#{@iface_number}" message << " connected to network #{@network_name}." if @mac @@ -119,31 +118,29 @@ module VagrantPlugins domain.attach_device(to_xml(template_name)) rescue => e raise Errors::AttachDeviceError, - :error_message => e.message + error_message: e.message end # Re-read the network configuration and grab the MAC address - unless @mac - xml = Nokogiri::XML(domain.xml_desc) - if iface_configuration[:iface_type] == :public_network - if @type == 'direct' - @mac = xml.xpath("/domain/devices/interface[source[@dev='#{@device}']]/mac/@address") - elsif !@portgroup.nil? - @mac = xml.xpath("/domain/devices/interface[source[@network='#{@network_name}']]/mac/@address") - else - @mac = xml.xpath("/domain/devices/interface[source[@bridge='#{@device}']]/mac/@address") - end - else + next if @mac + xml = Nokogiri::XML(domain.xml_desc) + if iface_configuration[:iface_type] == :public_network + if @type == 'direct' + @mac = xml.xpath("/domain/devices/interface[source[@dev='#{@device}']]/mac/@address") + elsif !@portgroup.nil? @mac = xml.xpath("/domain/devices/interface[source[@network='#{@network_name}']]/mac/@address") + else + @mac = xml.xpath("/domain/devices/interface[source[@bridge='#{@device}']]/mac/@address") end - iface_configuration[:mac] = @mac.to_s + else + @mac = xml.xpath("/domain/devices/interface[source[@network='#{@network_name}']]/mac/@address") end + iface_configuration[:mac] = @mac.to_s end # Continue the middleware chain. @app.call(env) - if env[:machine].config.vm.box # Configure interfaces that user requested. Machine should be up and # running now. @@ -158,17 +155,17 @@ module VagrantPlugins @logger.debug "Configuring interface slot_number #{slot_number} options #{options}" network = { - :interface => slot_number, - :use_dhcp_assigned_default_route => options[:use_dhcp_assigned_default_route], - :mac_address => options[:mac], + interface: slot_number, + use_dhcp_assigned_default_route: options[:use_dhcp_assigned_default_route], + mac_address: options[:mac] } if options[:ip] network = { - :type => :static, - :ip => options[:ip], - :netmask => options[:netmask], - :gateway => options[:gateway], + type: :static, + ip: options[:ip], + netmask: options[:netmask], + gateway: options[:gateway] }.merge(network) else network[:type] = :dhcp @@ -182,18 +179,19 @@ module VagrantPlugins env[:ui].info I18n.t('vagrant.actions.vm.network.configuring') env[:machine].guest.capability( - :configure_networks, networks_to_configure) + :configure_networks, networks_to_configure + ) end end private - def find_empty(array, start=0, stop=@nic_adapter_count) + def find_empty(array, start = 0, stop = @nic_adapter_count) (start..stop).each do |i| return i unless array[i] end - return nil + nil end # Return network name according to interface options. @@ -202,7 +200,7 @@ module VagrantPlugins return 'tunnel_interface' if options.fetch(:tunnel_type, nil) if options[:network_name] - @logger.debug "Found network by name" + @logger.debug 'Found network by name' return options[:network_name] end @@ -215,7 +213,7 @@ module VagrantPlugins address = network_address(options[:ip], options[:netmask]) available_networks.each do |network| if address == network[:network_address] - @logger.debug "Found network by ip" + @logger.debug 'Found network by ip' return network[:name] end end diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index 3b988e6..ab21011 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -36,7 +36,7 @@ module VagrantPlugins configured_networks(env, @logger).each do |options| # Only need to create private networks next if options[:iface_type] != :private_network || - options.fetch(:tunnel_type, nil) + options.fetch(:tunnel_type, nil) @logger.debug "Searching for network with options #{options}" # should fix other methods so this doesn't have to be instance var @@ -46,7 +46,8 @@ module VagrantPlugins # list is used throughout this class and should be easier to # process than libvirt API calls. @available_networks = libvirt_networks( - env[:machine].provider.driver.connection.client) + env[:machine].provider.driver.connection.client + ) # Prepare a hash describing network for this specific interface. @interface_network = { @@ -129,24 +130,22 @@ module VagrantPlugins @interface_network[:network_address] = net_address # if network is veryisolated, search by name - if @options[:libvirt__forward_mode] == "veryisolated" - network = lookup_network_by_name(@options[:network_name]) - elsif net_address - # otherwise, search by ip (if set) - network = lookup_network_by_ip(net_address) - else - # leaving this here to mimic prior behavior. If we get - # here, something's probably broken. - network = lookup_network_by_name(@options[:network_name]) - end + network = if @options[:libvirt__forward_mode] == 'veryisolated' + lookup_network_by_name(@options[:network_name]) + elsif net_address + # otherwise, search by ip (if set) + lookup_network_by_ip(net_address) + else + # leaving this here to mimic prior behavior. If we get + # here, something's probably broken. + lookup_network_by_name(@options[:network_name]) + end @interface_network = network if network - if @interface_network[:created] - verify_dhcp - end + verify_dhcp if @interface_network[:created] if @options[:network_name] - @logger.debug "Checking that network name does not clash with ip" + @logger.debug 'Checking that network name does not clash with ip' if @interface_network[:created] # Just check for mismatch error here - if name and ip from # config match together. @@ -178,7 +177,7 @@ module VagrantPlugins # Is name for new network set? If not, generate a unique one. count = 0 while @interface_network[:name].nil? - @logger.debug "generating name for network" + @logger.debug 'generating name for network' # Generate a network name. network_name = env[:root_path].basename.to_s.dup @@ -205,12 +204,12 @@ module VagrantPlugins def handle_network_name_option(env) return if @options[:ip] || \ !@options[:network_name] || \ - !@options[:libvirt__forward_mode] == "veryisolated" + !@options[:libvirt__forward_mode] == 'veryisolated' network = lookup_network_by_name(@options[:network_name]) @interface_network = network if network - if @options[:libvirt__forward_mode] == "veryisolated" + if @options[:libvirt__forward_mode] == 'veryisolated' # if this interface has a network address, something's wrong. if @interface_network[:network_address] raise Errors::NetworkNotAvailableError, @@ -266,7 +265,7 @@ module VagrantPlugins # Return the first available virbr interface name def generate_bridge_name - @logger.debug "generating name for bridge" + @logger.debug 'generating name for bridge' count = 0 while lookup_bridge_by_name(bridge_name = "virbr#{count}") count += 1 @@ -290,7 +289,7 @@ module VagrantPlugins if @options[:dhcp_enabled] # Find out DHCP addresses pool range. network_address = "#{@interface_network[:network_address]}/" - network_address << "#{@interface_network[:netmask]}" + network_address << (@interface_network[:netmask]).to_s net = @interface_network[:network_address] ? IPAddr.new(network_address) : nil # First is address of network, second is gateway (by default). @@ -313,7 +312,7 @@ module VagrantPlugins begin @interface_network[:libvirt_network] = \ @libvirt_client.define_network_xml(to_xml('private_network')) - @logger.debug "created network" + @logger.debug 'created network' rescue => e raise Errors::CreateNetworkError, error_message: e.message end @@ -332,21 +331,16 @@ module VagrantPlugins end def autostart_network - begin - @interface_network[:libvirt_network].autostart = true - rescue => e - raise Errors::AutostartNetworkError, error_message: e.message - end + @interface_network[:libvirt_network].autostart = true + rescue => e + raise Errors::AutostartNetworkError, error_message: e.message end def activate_network - begin - @interface_network[:libvirt_network].create - rescue => e - raise Errors::ActivateNetworkError, error_message: e.message - end + @interface_network[:libvirt_network].create + rescue => e + raise Errors::ActivateNetworkError, error_message: e.message end - end end end