diff --git a/lib/vSphere/action.rb b/lib/vSphere/action.rb index 2c8367f6..0e2aa543 100644 --- a/lib/vSphere/action.rb +++ b/lib/vSphere/action.rb @@ -150,7 +150,6 @@ def self.action_reload end end - # TODO: Remove the if guard when Vagrant 1.8.0 is the minimum version. # rubocop:disable IndentationWidth if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.8.0') diff --git a/lib/vSphere/action/destroy.rb b/lib/vSphere/action/destroy.rb index 7c3102e5..56a9222b 100644 --- a/lib/vSphere/action/destroy.rb +++ b/lib/vSphere/action/destroy.rb @@ -19,18 +19,16 @@ def call(env) private def destroy_vm(env) - begin - env[:ui].info I18n.t('vsphere.destroy_vm') + env[:ui].info I18n.t('vsphere.destroy_vm') - env[:machine].provider.driver.destroy do |progress| - env[:ui].clear_line - env[:ui].report_progress(progress, 100, false) - end - rescue Errors::VSphereError - raise - rescue StandardError => e - raise Errors::VSphereError.new, e.message + env[:machine].provider.driver.destroy do |progress| + env[:ui].clear_line + env[:ui].report_progress(progress, 100, false) end + rescue Errors::VSphereError + raise + rescue StandardError => e + raise Errors::VSphereError.new, e.message end end end diff --git a/lib/vSphere/action/snapshot_delete.rb b/lib/vSphere/action/snapshot_delete.rb index 8fde5cf7..2a9b58aa 100644 --- a/lib/vSphere/action/snapshot_delete.rb +++ b/lib/vSphere/action/snapshot_delete.rb @@ -8,7 +8,7 @@ def initialize(app, _env) def call(env) snapshot_name = env[:snapshot_name] - + env[:ui].info(I18n.t("vagrant.actions.vm.snapshot.deleting", name: snapshot_name)) env[:machine].provider.driver.delete_snapshot(snapshot_name) do |progress| diff --git a/lib/vSphere/action/snapshot_restore.rb b/lib/vSphere/action/snapshot_restore.rb index 276f322f..a5016993 100644 --- a/lib/vSphere/action/snapshot_restore.rb +++ b/lib/vSphere/action/snapshot_restore.rb @@ -17,7 +17,7 @@ def call(env) end env[:ui].clear_line - #env[:ui].success(I18n.t("vagrant.actions.vm.snapshot.restored", name: snapshot_name)) + # env[:ui].success(I18n.t("vagrant.actions.vm.snapshot.restored", name: snapshot_name)) @app.call env end diff --git a/lib/vSphere/action/snapshot_save.rb b/lib/vSphere/action/snapshot_save.rb index ea428c24..558134e0 100644 --- a/lib/vSphere/action/snapshot_save.rb +++ b/lib/vSphere/action/snapshot_save.rb @@ -10,7 +10,7 @@ def call(env) snapshot_name = env[:snapshot_name] env[:ui].info(I18n.t("vagrant.actions.vm.snapshot.saving", name: snapshot_name)) - + env[:machine].provider.driver.create_snapshot(snapshot_name) do |progress| env[:ui].clear_line env[:ui].report_progress(progress, 100, false) @@ -18,7 +18,7 @@ def call(env) env[:ui].clear_line env[:ui].success(I18n.t("vagrant.actions.vm.snapshot.saved", name: snapshot_name)) - + @app.call env end end diff --git a/lib/vSphere/config.rb b/lib/vSphere/config.rb index eb5b7548..812fd7ef 100644 --- a/lib/vSphere/config.rb +++ b/lib/vSphere/config.rb @@ -3,7 +3,6 @@ module VagrantPlugins module VSphere class Config < Vagrant.plugin('2', :config) - class NetworkConfiguration attr_accessor :allow_guest_control attr_accessor :connected @@ -39,67 +38,67 @@ def initialize(network_config) end class SerialPortConfiguration - attr_accessor :yield_on_poll - attr_accessor :connected - attr_accessor :start_connected - attr_accessor :backing - - attr_accessor :direction - attr_accessor :proxy_uri - attr_accessor :service_uri - - attr_accessor :endpoint - attr_accessor :no_rx_loss - - attr_accessor :file_name - - attr_accessor :device_name - attr_accessor :use_auto_detect - - def initialize(serial_port_config) - @yield_on_poll = true - @connected = true - @start_connected = true - @backing = '' - - @direction = '' - @proxy_uri = '' - @service_uri = '' - - @endpoint = '' - @no_rx_loss = true - - @file_name = '' - - @device_name = '' - @use_auto_detect = false - - @yield_on_poll = serial_port_config[:yield_on_poll] if serial_port_config.key?(:yield_on_poll) - @connected = network_config[:connected] if network_config.key?(:connected) - @start_connected = network_config[:start_connected] if network_config.key?(:start_connected) - @backing = serial_port_config[:backing] if serial_port_config.key?(:backing) - if !(@backing == 'uri' || @backing == 'pipe' || @backing == 'file' || @backing == 'device') - raise "The only valid values allowed for backing are 'uri', 'pipe', 'file', 'device'" - end - - @direction = serial_port_config[:direction] if serial_port_config.key?(:direction) - if @backing == 'uri' && !(@direction == 'client' || @direction == 'server') - raise "The only valid values allowed for direction are 'client', 'server'" - end - @proxy_uri = serial_port_config[:proxy_uri] if serial_port_config.key?(:proxy_uri) - @service_uri = serial_port_config[:service_uri] if serial_port_config.key?(:service_uri) - - @endpoint = serial_port_config[:endpoint] if serial_port_config.key?(:endpoint) - if @backing == 'pipe' && !(@endpoint == 'client' || @endpoint == 'server') - raise "The only valid values allowed for endpoint are 'client', 'server'" - end - @no_rx_loss = serial_port_config[:no_rx_loss] if serial_port_config.key?(:no_rx_loss) - - @file_name = serial_port_config[:file_name] if serial_port_config.key?(:file_name) - - @device_name = serial_port_config[:device_name] if serial_port_config.key?(:device_name) - @use_auto_detect = serial_port_config[:use_auto_detect] if serial_port_config.key?(:use_auto_detect) + attr_accessor :yield_on_poll + attr_accessor :connected + attr_accessor :start_connected + attr_accessor :backing + + attr_accessor :direction + attr_accessor :proxy_uri + attr_accessor :service_uri + + attr_accessor :endpoint + attr_accessor :no_rx_loss + + attr_accessor :file_name + + attr_accessor :device_name + attr_accessor :use_auto_detect + + def initialize(serial_port_config) + @yield_on_poll = true + @connected = true + @start_connected = true + @backing = '' + + @direction = '' + @proxy_uri = '' + @service_uri = '' + + @endpoint = '' + @no_rx_loss = true + + @file_name = '' + + @device_name = '' + @use_auto_detect = false + + @yield_on_poll = serial_port_config[:yield_on_poll] if serial_port_config.key?(:yield_on_poll) + @connected = network_config[:connected] if network_config.key?(:connected) + @start_connected = network_config[:start_connected] if network_config.key?(:start_connected) + @backing = serial_port_config[:backing] if serial_port_config.key?(:backing) + unless @backing == 'uri' || @backing == 'pipe' || @backing == 'file' || @backing == 'device' + fail "The only valid values allowed for backing are 'uri', 'pipe', 'file', 'device'" + end + + @direction = serial_port_config[:direction] if serial_port_config.key?(:direction) + if @backing == 'uri' && !(@direction == 'client' || @direction == 'server') + fail "The only valid values allowed for direction are 'client', 'server'" + end + @proxy_uri = serial_port_config[:proxy_uri] if serial_port_config.key?(:proxy_uri) + @service_uri = serial_port_config[:service_uri] if serial_port_config.key?(:service_uri) + + @endpoint = serial_port_config[:endpoint] if serial_port_config.key?(:endpoint) + if @backing == 'pipe' && !(@endpoint == 'client' || @endpoint == 'server') + fail "The only valid values allowed for endpoint are 'client', 'server'" end + @no_rx_loss = serial_port_config[:no_rx_loss] if serial_port_config.key?(:no_rx_loss) + + @file_name = serial_port_config[:file_name] if serial_port_config.key?(:file_name) + + @device_name = serial_port_config[:device_name] if serial_port_config.key?(:device_name) + @use_auto_detect = serial_port_config[:use_auto_detect] if serial_port_config.key?(:use_auto_detect) + end end attr_accessor :host @@ -131,9 +130,9 @@ def initialize(serial_port_config) attr_accessor :destroy_unused_serial_ports attr_accessor :management_network_adapter_slot attr_accessor :management_network_adapter_address_family - attr_reader :network_adapters - attr_reader :serial_ports - attr_reader :custom_attributes + attr_reader :network_adapters + attr_reader :serial_ports + attr_reader :custom_attributes def initialize @wait_for_customization_timeout = 600 diff --git a/lib/vSphere/driver.rb b/lib/vSphere/driver.rb index d41b7fdb..d18f3545 100644 --- a/lib/vSphere/driver.rb +++ b/lib/vSphere/driver.rb @@ -20,15 +20,15 @@ def initialize(machine) end def connection - raise "connection be called from a code block!" if !block_given? + raise "connection be called from a code block!" unless block_given? begin config = @machine.provider_config current_connection = RbVmomi::VIM.connect host: config.host, - user: config.user, password: config.password, - insecure: config.insecure, proxyHost: config.proxy_host, - proxyPort: config.proxy_port + user: config.user, password: config.password, + insecure: config.insecure, proxyHost: config.proxy_host, + proxyPort: config.proxy_port yield current_connection rescue @@ -48,8 +48,8 @@ def ssh_info ip_address = filter_guest_nic(vm, @machine) return nil if ip_address.nil? || ip_address.empty? { - host: ip_address, - port: 22 + host: ip_address, + port: 22 } end end @@ -242,8 +242,8 @@ def clone(root_path) end rescue Errors::VSphereError raise - #rescue StandardError => e - # raise Errors::VSphereError.new, e.message + # rescue StandardError => e + # raise Errors::VSphereError.new, e.message end # TODO: handle interrupted status in the environment, should the vm be destroyed? @@ -431,7 +431,7 @@ def filter_guest_nic(vm, machine) end return nil if ipAddress.nil? - return ipAddress.ipAddress + return ipAddress.ipAddress else return config.network_adapters[config.management_network_adapter_slot].ip_address.to_s if config.network_adapters[config.management_network_adapter_slot].ip_address.is_a?(IPAddr) return config.network_adapters[config.management_network_adapter_slot].ip_address @@ -535,11 +535,11 @@ def get_network_by_name(dc, name) entity_array = name.split('/').reject(&:empty?) entity_array.each do |item| case base - when RbVmomi::VIM::Folder - base = base.find(item) - when RbVmomi::VIM::VmwareDistributedVirtualSwitch - idx = base.summary.portgroupName.find_index(item) - base = idx.nil? ? nil : base.portgroup[idx] + when RbVmomi::VIM::Folder + base = base.find(item) + when RbVmomi::VIM::VmwareDistributedVirtualSwitch + idx = base.summary.portgroupName.find_index(item) + base = idx.nil? ? nil : base.portgroup[idx] end end @@ -548,7 +548,7 @@ def get_network_by_name(dc, name) base end - #Cloning + # Cloning def get_customization_spec(machine, spec_info) customization_spec = spec_info.spec.clone @@ -629,28 +629,28 @@ def get_vm_base_folder(dc, template, config) end def configure_serial_ports(spec, dc, template, config) - #Enumerate serial ports + # Enumerate serial ports spec[:config][:deviceChange] ||= [] current_ports = Hash.new - template.config.hardware.device.grep(RbVmomi::VIM::VirtualSerialPort).each_with_index { |item, index| + template.config.hardware.device.grep(RbVmomi::VIM::VirtualSerialPort).each_with_index do |item, index| current_ports[index] = item - } + end puts "config.serial_ports=#{config.serial_ports.inspect}" current_ports_length = current_ports.length config_serial_ports_length = -1 - config.serial_ports.each_with_index { |_item, index| + config.serial_ports.each_with_index do |_item, index| if index > config_serial_ports_length config_serial_ports_length = index end - } + end config_serial_ports_length += 1 - #remove unused serial ports + # remove unused serial ports if config.destroy_unused_serial_ports if current_ports_length-1 > config_serial_ports_length-1 for index in (current_ports_length-1).downto(config_serial_ports_length-1+1) @@ -667,25 +667,25 @@ def configure_serial_ports(spec, dc, template, config) end end - #we have 5 ports but want 8 ports - #add 3 ports - #edit first 5 ports + # we have 5 ports but want 8 ports + # add 3 ports + # edit first 5 ports number_of_existing_ports = current_ports_length if current_ports_length > config_serial_ports_length - #we have 5 ports but want 3 ports - #remove 2 ports - #edit first 3 ports + # we have 5 ports but want 3 ports + # remove 2 ports + # edit first 3 ports number_of_existing_ports = config_serial_ports_length end - #edit existing network interfaces - if (number_of_existing_ports > 0) + # edit existing network interfaces + if number_of_existing_ports > 0 for index in (0).upto(number_of_existing_ports) port_configuration = config.serial_ports[index] puts "port_configuration[#{index}]=#{port_configuration.inspect}" - #there may be no configuration for this port so dont change it, if this is the case - if !port_configuration.nil? + # there may be no configuration for this port so dont change it, if this is the case + unless port_configuration.nil? port = current_ports[index] port = configure_serial_port(dc, port_configuration, port) @@ -700,7 +700,7 @@ def configure_serial_ports(spec, dc, template, config) end end - #add extra network interfaces + # add extra network interfaces for index in (number_of_existing_ports).upto(config_serial_ports_length-1) port_configuration = config.serial_ports[index] adapter = RbVmomi::VIM::VirtualSerialPort( @@ -752,14 +752,14 @@ def configure_serial_port(_dc, port_configuration, port) end def configure_network_cards(spec, dc, template, config) - #Enumerate lan cards + # Enumerate lan cards spec[:config][:deviceChange] ||= [] current_adapters = Hash.new - template.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).each_with_index { |item, index| + template.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).each_with_index do |item, index| current_adapters[index] = item - } + end puts "config.network_adapters=#{config.network_adapters.inspect}" @@ -767,17 +767,17 @@ def configure_network_cards(spec, dc, template, config) # configuration may have gaps in it, so configuration may look like this: # vsphere.network_adapter 0, vlan: "vlan0" - # vsphere.network_adapter 1, vlan: "vlan1" - # vsphere.network_adapter 9, vlan: "vlan9" + # vsphere.network_adapter 1, vlan: "vlan1" + # vsphere.network_adapter 9, vlan: "vlan9" config_network_adapters_length = -1 - config.network_adapters.each_with_index { |_item, index| + config.network_adapters.each_with_index do |_item, index| if index > config_network_adapters_length config_network_adapters_length = index end - } + end config_network_adapters_length += 1 - #remove unused network interfaces + # remove unused network interfaces if config.destroy_unused_network_interfaces if current_adapters_length-1 > config_network_adapters_length-1 for index in (current_adapters_length-1).downto(config_network_adapters_length-1+1) @@ -794,25 +794,25 @@ def configure_network_cards(spec, dc, template, config) end end - #we have 5 cards but want 8 cards - #add 3 cards - #edit first 5 cards + # we have 5 cards but want 8 cards + # add 3 cards + # edit first 5 cards number_of_existing_adapters = current_adapters_length if current_adapters_length > config_network_adapters_length - #we have 5 cards but want 3 cards - #remove 2 cards - #edit first 3 cards + # we have 5 cards but want 3 cards + # remove 2 cards + # edit first 3 cards number_of_existing_adapters = config_network_adapters_length end - #edit existing network interfaces - if (number_of_existing_adapters > 0) + # edit existing network interfaces + if number_of_existing_adapters > 0 for index in (0).upto(number_of_existing_adapters) adapter_configuration = config.network_adapters[index] puts "adapter_configuration[#{index}]=#{adapter_configuration.inspect}" - #there may be no configuration for this card so dont change it, if this is the case - if !adapter_configuration.nil? + # there may be no configuration for this card so dont change it, if this is the case + unless adapter_configuration.nil? adapter = current_adapters[index] label = "Ethernet #{index+1}" @@ -832,7 +832,7 @@ def configure_network_cards(spec, dc, template, config) end end - #add extra network interfaces + # add extra network interfaces for index in (number_of_existing_adapters).upto(config_network_adapters_length-1) adapter_configuration = config.network_adapters[index] adapter = RbVmomi::VIM::VirtualVmxnet3( @@ -862,7 +862,7 @@ def configure_network_cards(spec, dc, template, config) end def configure_network_card(dc, adapter_configuration, adapter, label, summary) - if !adapter_configuration.vlan.nil? + unless adapter_configuration.vlan.nil? network = get_network_by_name(dc, adapter_configuration.vlan) if network.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) diff --git a/lib/vSphere/provider.rb b/lib/vSphere/provider.rb index 10ff3e1a..a03e3774 100644 --- a/lib/vSphere/provider.rb +++ b/lib/vSphere/provider.rb @@ -10,7 +10,7 @@ class Provider < Vagrant.plugin('2', :provider) def initialize(machine) @logger = Log4r::Logger.new('vagrant::provider::vsphere') @machine = machine - + # This method will load in our driver, so we call it now to # initialize it. machine_id_changed @@ -63,4 +63,4 @@ def to_s end end end -end \ No newline at end of file +end