Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby update #296

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.13.5
current_version = 1.14.0
tag = true
commit = true

Expand All @@ -8,3 +8,4 @@ commit = true
[bumpversion:file:README.md]
parse = version: (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
serialize = version: {major}.{minor}.{patch}

15 changes: 15 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ Metrics/ModuleLength:

Metrics/PerceivedComplexity:
Enabled: false

# Disabled these cops upgrading rubocop to 1.25.0
# TODO:Correct the issues found by these cops someday
Lint/MissingSuper:
Enabled: false

Metrics/BlockLength:
Enabled: false

Naming:
Enabled: false

# TODO: Add required ruby version to gemspec
Gemspec/RequiredRubyVersion:
Enabled: false
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.6.5
ruby-3.0.2
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [1.14.0 (2022-08-01)](https://github.com/nsidc/vagrant-vsphere/releases/tag/v1.14.0.pre1)

- Update gem to ruby 3.0.2
- Support for vagrant >=2.2.17
- Update nokogiri dependency (1.13.4) to take care of dependabot alerts
- Update rbvmomi, rake, rubocop dependencies

## [1.13.5 (2021-01-05)](https://github.com/nsidc/vagrant-vsphere/releases/tag/v1.13.5)
- Pin nokogiri to 1.10.10. This fixes an issue where vagrant-vsphere failed to install due to
nokogiri requiring Ruby >=2.5. This is a workaround until the vagrant-nsidc plugin is updated
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

source 'http://rubygems.org'

group :development do
# We depend on Vagrant for development, but we don't add it as a
# gem dependency because we expect to be installed within the
# Vagrant environment itself using `vagrant plugin`.
gem 'vagrant', github: 'mitchellh/vagrant', ref: 'v2.2.6'
gem 'vagrant', github: 'mitchellh/vagrant', ref: 'v2.2.19'
end

group :plugins do
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ This provider is built on top of the
* libxml2, libxml2-dev, libxslt, libxslt-dev

## Current Version
**version: 1.13.5**
**version: 1.14.0.pre1**

vagrant-vsphere (**version: 1.13.5**) is available from
vagrant-vsphere (**version: 1.14.0**) is available from
[RubyGems.org](https://rubygems.org/gems/vagrant-vsphere)

## Installation
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
Loading