Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rubocop autocorrect
  • Loading branch information
Mike Laxer authored and Mike Laxer committed Apr 12, 2022
commit 53dd3d624f5ccbbfbb6a96a415f16146945e857c
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'http://rubygems.org'

group :development do
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'rspec/core/rake_task'
Expand All @@ -9,12 +11,12 @@ $stdout.sync = true
$stderr.sync = true

# Change to the directory of this file.
Dir.chdir(File.expand_path('../', __FILE__))
Dir.chdir(File.expand_path(__dir__))

Bundler::GemHelper.install_tasks

RSpec::Core::RakeTask.new

RuboCop::RakeTask.new

task default: %w(rubocop spec)
task default: %w[rubocop spec]
14 changes: 8 additions & 6 deletions lib/vSphere/action.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'vagrant'
require 'vagrant/action/builder'

Expand Down Expand Up @@ -224,7 +226,7 @@ def self.action_get_ssh_info
end

# TODO: Remove the if guard when Vagrant 1.8.0 is the minimum version.
# rubocop:disable IndentationWidth
# rubocop:disable Layout/IndentationWidth
if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.8.0')
def self.action_snapshot_delete
Vagrant::Action::Builder.new.tap do |b|
Expand Down Expand Up @@ -292,11 +294,11 @@ def self.action_snapshot_save
b.use CloseVSphere
end
end
end # Vagrant > 1.8.0 guard
# rubocop:enable IndentationWidth
end
# rubocop:enable Layout/IndentationWidth

# autoload
action_root = Pathname.new(File.expand_path('../action', __FILE__))
action_root = Pathname.new(File.expand_path('action', __dir__))
autoload :Clone, action_root.join('clone')
autoload :CloseVSphere, action_root.join('close_vsphere')
autoload :ConnectVSphere, action_root.join('connect_vsphere')
Expand All @@ -317,14 +319,14 @@ def self.action_snapshot_save
autoload :WaitForIPAddress, action_root.join('wait_for_ip_address')

# TODO: Remove the if guard when Vagrant 1.8.0 is the minimum version.
# rubocop:disable IndentationWidth
# rubocop:disable Layout/IndentationWidth
if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.8.0')
autoload :SnapshotDelete, action_root.join('snapshot_delete')
autoload :SnapshotList, action_root.join('snapshot_list')
autoload :SnapshotRestore, action_root.join('snapshot_restore')
autoload :SnapshotSave, action_root.join('snapshot_save')
end
# rubocop:enable IndentationWidth
# rubocop:enable Layout/IndentationWidth
end
end
end
39 changes: 20 additions & 19 deletions lib/vSphere/action/clone.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'i18n'
require 'vSphere/util/vim_helpers'
Expand All @@ -19,14 +21,15 @@ def call(env)
name = get_name machine, config, env[:root_path]
dc = get_datacenter connection, machine
template = dc.find_vm config.template_name
fail Errors::VSphereError, :'missing_template' if template.nil?
raise Errors::VSphereError, :'missing_template' if template.nil?

vm_base_folder = get_vm_base_folder dc, template, config
fail Errors::VSphereError, :'invalid_base_path' if vm_base_folder.nil?
raise Errors::VSphereError, :'invalid_base_path' if vm_base_folder.nil?

begin
# Storage DRS does not support vSphere linked clones. http://www.vmware.com/files/pdf/techpaper/vsphere-storage-drs-interoperability.pdf
ds = get_datastore dc, machine
fail Errors::VSphereError, :'invalid_configuration_linked_clone_with_sdrs' if config.linked_clone && ds.is_a?(RbVmomi::VIM::StoragePod)
raise Errors::VSphereError, :'invalid_configuration_linked_clone_with_sdrs' if config.linked_clone && ds.is_a?(RbVmomi::VIM::StoragePod)

location = get_location ds, dc, machine, template
spec = RbVmomi::VIM.VirtualMachineCloneSpec location: location, powerOn: true, template: false
Expand Down Expand Up @@ -74,9 +77,7 @@ def call(env)

recommendation = result.recommendations[0]
key = recommendation.key ||= ''
if key == ''
fail Errors::VSphereError, :missing_datastore_recommendation
end
raise Errors::VSphereError, :missing_datastore_recommendation if key == ''

env[:ui].info I18n.t('vsphere.creating_cloned_vm_sdrs')
env[:ui].info " -- Storage DRS recommendation: #{recommendation.target.name} #{recommendation.reasonText}"
Expand Down Expand Up @@ -125,13 +126,13 @@ def wait_for_sysprep(env, vm, vim_connection, timeout, sleep_time)
while wait
events = query_customization_succeeded(vm, vem)

if events.size > 0
if events.size.positive?
events.each do |e|
env[:ui].info e.fullFormattedMessage
end
wait = false
elsif waited_seconds >= timeout
fail Errors::VSphereError, :'sysprep_timeout'
raise Errors::VSphereError, :'sysprep_timeout'
else
sleep(sleep_time)
waited_seconds += sleep_time
Expand All @@ -154,7 +155,7 @@ def get_customization_spec(machine, spec_info)
return customization_spec if private_networks.nil?

# make sure we have enough NIC settings to override with the private network settings
fail Errors::VSphereError, :'too_many_private_networks' if private_networks.length > customization_spec.nicSettingMap.length
raise Errors::VSphereError, :'too_many_private_networks' if private_networks.length > customization_spec.nicSettingMap.length

# assign the private network IP to the NIC
private_networks.each_index do |idx|
Expand Down Expand Up @@ -212,7 +213,7 @@ def get_name(machine, config, root_path)
return config.name unless config.name.nil?

prefix = "#{root_path.basename}_#{machine.name}"
prefix.gsub!(/[^-a-z0-9_\.]/i, '')
prefix.gsub!(/[^-a-z0-9_.]/i, '')
# milliseconds + random number suffix to allow for simultaneous `vagrant up` of the same box in different dirs
prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100_000)}"
end
Expand All @@ -229,7 +230,7 @@ def modify_network_card(template, spec)
spec[:config][:deviceChange] ||= []
@card ||= template.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first

fail Errors::VSphereError, :missing_network_card if @card.nil?
raise Errors::VSphereError, :missing_network_card if @card.nil?

yield(@card)

Expand All @@ -241,7 +242,7 @@ def modify_network_card(template, spec)
def add_custom_address_type(template, spec, addressType)
spec[:config][:deviceChange] = []
config = template.config
card = config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first || fail(Errors::VSphereError, :missing_network_card)
card = config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first || raise(Errors::VSphereError, :missing_network_card)
card.addressType = addressType
card_spec = { :deviceChange => [{ :operation => :edit, :device => card }] }
template.ReconfigVM_Task(:spec => card_spec).wait_for_completion
Expand All @@ -257,13 +258,13 @@ def add_custom_vlan(template, dc, spec, vlan)
network = get_network_by_name(dc, vlan)

modify_network_card(template, spec) do |card|
begin
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(switchUuid: network.config.distributedVirtualSwitch.uuid, portgroupKey: network.key)
card.backing = RbVmomi::VIM::VirtualEthernetCardDistributedVirtualPortBackingInfo(port: switch_port)
rescue
# not connected to a distibuted switch?
card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(network: network, deviceName: network.name)
end

switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(switchUuid: network.config.distributedVirtualSwitch.uuid, portgroupKey: network.key)
card.backing = RbVmomi::VIM::VirtualEthernetCardDistributedVirtualPortBackingInfo(port: switch_port)
rescue StandardError
# not connected to a distibuted switch?
card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(network: network, deviceName: network.name)

end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/close_vsphere.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'

module VagrantPlugins
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/connect_vsphere.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'

module VagrantPlugins
Expand Down
3 changes: 3 additions & 0 deletions lib/vSphere/action/destroy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'i18n'
require 'vSphere/util/vim_helpers'
Expand All @@ -23,6 +25,7 @@ def call(env)

def destroy_vm(env)
return if env[:machine].state.id == :not_created

vm = get_vm_by_uuid env[:vSphere_connection], env[:machine]
return if vm.nil?

Expand Down
9 changes: 7 additions & 2 deletions lib/vSphere/action/get_ssh_info.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'vSphere/util/vim_helpers'

Expand All @@ -21,12 +23,13 @@ def call(env)
def filter_guest_nic(vm, machine)
return vm.guest.ipAddress unless machine.provider_config.real_nic_ip

interfaces = vm.guest.net.select { |g| g.deviceConfigId > 0 }
interfaces = vm.guest.net.select { |g| g.deviceConfigId.positive? }
ip_addresses = interfaces.map { |i| i.ipConfig.ipAddress.select { |a| a.state == 'preferred' } }.flatten

return (vm.guest.ipAddress || nil) if ip_addresses.empty?

fail Errors::VSphereError.new, :'multiple_interface_with_real_nic_ip_set' if ip_addresses.size > 1
raise Errors::VSphereError.new, :'multiple_interface_with_real_nic_ip_set' if ip_addresses.size > 1

ip_addresses.first.ipAddress
end

Expand All @@ -35,8 +38,10 @@ def get_ssh_info(connection, machine)

vm = get_vm_by_uuid connection, machine
return nil if vm.nil?

ip_address = filter_guest_nic(vm, machine)
return nil if ip_address.nil? || ip_address.empty?

{
host: ip_address,
port: 22
Expand Down
4 changes: 3 additions & 1 deletion lib/vSphere/action/get_state.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'vSphere/util/vim_helpers'
require 'vSphere/util/vm_helpers'
Expand All @@ -22,7 +24,7 @@ def call(env)
private

def get_state(connection, machine)
return :not_created if machine.id.nil?
return :not_created if machine.id.nil?

vm = get_vm_by_uuid connection, machine

Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/is_created.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module VagrantPlugins
module VSphere
module Action
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/is_running.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module VagrantPlugins
module VSphere
module Action
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/is_suspended.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module VagrantPlugins
module VSphere
module Action
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/message_already_created.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'i18n'

module VagrantPlugins
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/message_not_created.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'i18n'

module VagrantPlugins
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/message_not_running.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'i18n'

module VagrantPlugins
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/message_not_suspended.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'i18n'

module VagrantPlugins
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/power_off.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'i18n'
require 'vSphere/util/vim_helpers'
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/power_on.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'i18n'
require 'vSphere/util/vim_helpers'
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/resume.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rbvmomi'
require 'i18n'
require 'vSphere/util/vim_helpers'
Expand Down
12 changes: 8 additions & 4 deletions lib/vSphere/action/snapshot_delete.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'vSphere/util/vim_helpers'
require 'vSphere/util/vm_helpers'

Expand All @@ -16,8 +18,9 @@ def call(env)
vm = get_vm_by_uuid(env[:vSphere_connection], env[:machine])

env[:ui].info(I18n.t(
"vagrant.actions.vm.snapshot.deleting",
name: env[:snapshot_name]))
"vagrant.actions.vm.snapshot.deleting",
name: env[:snapshot_name]
))

delete_snapshot(vm, env[:snapshot_name]) do |progress|
env[:ui].clear_line
Expand All @@ -27,8 +30,9 @@ def call(env)
env[:ui].clear_line

env[:ui].info(I18n.t(
"vagrant.actions.vm.snapshot.deleted",
name: env[:snapshot_name]))
"vagrant.actions.vm.snapshot.deleted",
name: env[:snapshot_name]
))

@app.call env
end
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/action/snapshot_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'vSphere/util/vim_helpers'
require 'vSphere/util/vm_helpers'

Expand Down
7 changes: 5 additions & 2 deletions lib/vSphere/action/snapshot_restore.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'vSphere/util/vim_helpers'
require 'vSphere/util/vm_helpers'

Expand All @@ -16,8 +18,9 @@ def call(env)
vm = get_vm_by_uuid(env[:vSphere_connection], env[:machine])

env[:ui].info(I18n.t(
"vagrant.actions.vm.snapshot.restoring",
name: env[:snapshot_name]))
"vagrant.actions.vm.snapshot.restoring",
name: env[:snapshot_name]
))

restore_snapshot(vm, env[:snapshot_name]) do |progress|
env[:ui].clear_line
Expand Down
Loading