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

vm vmdk add gets undefined method `each' for nil:NilClass (NoMethodError) #444

Open
amitcohen5 opened this issue Jul 17, 2018 · 5 comments
Labels
Type: Enhancement Adds new functionality.

Comments

@amitcohen5
Copy link

Worked few days ago but now we came across this issue, tried new version , old but nothing really helps , appreciate your assistance.

Versions:

  • Version of knife-vsphere: Tried latest (2.1.1) and 1.2.23
  • Version of chef: chef-client 12.12 and chefdk 2.4.17

Platform Details

  • Version of vSphere: 6.5.0
  • Version of ESXi: 6.5.0

Scenario:

knife vsphere vm vmdk add

Please paste the command and output (run with -VV). Don't forget to X out any passwords or sensitive information:

2018-07-17T16:36:58.5881285Z INFO: Using configuration from D:/Agents/Agent2/_work/87/s/knife.rb
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_host: *******
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_path: /sdk
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_port: 443
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_nossl:
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_user: ********
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_pass: ********
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_pass: ********
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_pass: ********
2018-07-17T16:36:58.6662514Z DEBUG: value for config item vsphere_insecure: true
2018-07-17T16:36:58.6662514Z DEBUG: value for config item proxy_host:
2018-07-17T16:36:58.6662514Z DEBUG: value for config item proxy_port:
2018-07-17T16:36:58.9687282Z DEBUG: value for config item folder:
2018-07-17T16:36:58.9687282Z DEBUG: value for config item vsphere_dc: QA and DEV
2018-07-17T16:37:00.1574818Z DEBUG: value for config item target_lun:
2018-07-17T16:37:00.2325364Z Datastore: K2-QA-3 (2.45 TB(2697227927552) / 8.00 TB)
2018-07-17T16:37:00.2991042Z Choosing: K2-QA-3
2018-07-17T16:37:01.6030146Z C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-1.2.23/lib/chef/knife/vsphere_vm_vmdk_add.rb:80:in block in run': undefined method each' for nil:NilClass (NoMethodError)
2018-07-17T16:37:01.6030146Z from C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-1.2.23/lib/chef/knife/vsphere_vm_vmdk_add.rb:79:in each' 2018-07-17T16:37:01.6030146Z from C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-1.2.23/lib/chef/knife/vsphere_vm_vmdk_add.rb:79:in run'
2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/lib/chef/knife.rb:442:in block in run_with_pretty_exceptions' 2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/lib/chef/local_mode.rb:44:in with_server_connectivity'
2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/lib/chef/knife.rb:441:in run_with_pretty_exceptions' 2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/lib/chef/knife.rb:219:in run'
2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/lib/chef/application/knife.rb:156:in run' 2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/bin/knife:25:in <top (required)>'
2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/bin/knife:267:in load' 2018-07-17T16:37:01.6030146Z from C:/opscode/chefdk/bin/knife:267:in

'

@swalberg
Copy link
Collaborator

Hi, thanks for providing a stack trace. It looks like it's from the older 1.2.23 though, can you re-run using the latest and paste the stack trace please?

@amitcohen5
Copy link
Author

amitcohen5 commented Jul 17, 2018

Hi,
@swalberg
Below is the stack trace from 2.1.1 , thanks for the quick response and waiting for a solution its really useful tool and our provisioning flow is stuck at the moment -

WARNING: No knife configuration file found
DEBUG: value for config item vsphere_host: **************

DEBUG: value for config item vsphere_path: /sdk
DEBUG: value for config item vsphere_port: 443
DEBUG: value for config item vsphere_nossl: 
DEBUG: value for config item vsphere_user: **************

DEBUG: value for config item vsphere_pass: **************
DEBUG: value for config item vsphere_pass: **************

DEBUG: value for config item vsphere_pass: **************

DEBUG: value for config item vsphere_insecure: true

DEBUG: value for config item proxy_host: 

DEBUG: value for config item proxy_port: 

DEBUG: value for config item folder: 

DEBUG: value for config item vsphere_dc: QA and DEV

DEBUG: value for config item target_lun: 

Datastore: K2-QA-3 (2.45 TB(2697227927552) / 8.00 TB)
Choosing: K2-QA-3
C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-2.1.1/lib/chef/k
nife/vsphere_vm_vmdk_add.rb:86:in `block in run
': 
undefined method `each' for nil:NilClass
 (NoMethodError)
	from C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-2.1.1/lib/
chef/knife/vsphere_vm_vmdk_add.rb:85:in `each'
	from C:/Users/tfsagent/AppData/Local/chefdk/gem/ruby/2.4.0/gems/knife-vsphere-2.1.1/lib/
chef/knife/vsphere_vm_vmdk_add.rb:85:in `run'
	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/l
ib/chef/knife.rb:442:in `block in run_with_pretty_exceptions'
	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/l
ib/chef/local_mode.rb:44:in `with_server_connectivity'
	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/l
ib/chef/knife.rb:441:in `run_with_pretty_exceptions'

	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/l
ib/chef/knife.rb:219:in `run'

	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/l
ib/chef/application/knife.rb:156:in `run'

	from C:/opscode/chefdk/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4-universal-mingw32/b
in/knife:25:in `<top (required)>'

	from C:/opscode/chefdk/bin/knife:267:in `load'

	from C:/opscode/chefdk/bin/knife:267:in `<main>'

@swalberg
Copy link
Collaborator

Thanks for the updated stack trace.

It's dying in code like this:

    pc = vmdk_datastore._connection.serviceContent.propertyCollector
    vms = vmdk_datastore.vm
    vm_files = pc.collectMultiple vms, 'layoutEx.file'
    vm_files.keys.each do |vmFile|
      vm_files[vmFile]['layoutEx.file'].each do |layout|
        if layout.name.match(/^\[#{vmdk_datastore.name}\] #{vmname}\/#{vmname}_([0-9]+).vmdk/)
          num = Regexp.last_match(1)
          next_vmdk = num.to_i + 1 if next_vmdk <= num.to_i
        end
      end
    end

Here's it's finding all the VMs on the chosen datastore (K2-QA-3) and looking for their associated files, and then filtering out to find the ones for the VM in question.

It seems that vm_files.keys is returning nil instead of an array of files. I'm not sure what would change to break this now, and I'm unable to reproduce this here. Is this a new datastore?

So two ideas to get you going:

  1. Choose a new datastore.
  2. Change vm_files.keys.each do |vmFile| to (vm_files.keys || []).each do |vmFile| so that if we get that nil it basically skips over that block and falls through to creating the file.

Please let me know how this works out.

@amitcohen5
Copy link
Author

@swalberg

The workaround works!

changing (vm_files.keys || []).each do |vmFile| was not enough , i had to change this one also
(vm_files[vmFile]['layoutEx.file'] || []).each do |layout|

However, i don't think i understood what is the impact of this workaround ?

We moved to this new vCenter few days ago and our provisioning worked for 2-3 first machines (same datastore was picked according to logs ) , the datastore itself was used before for testing and some machine creation before we moved.. so i can't say its really new.

Thanks again for the quick response and solution and please let us know if we can keep it this way.

@swalberg
Copy link
Collaborator

Hi, the code as is makes certain assumptions that when it asks for files it gets back a list of files, but in your case, it's getting nothing and then treating that nothing as if it were a list of zero files. That (foo || []) just makes foo into an Array (I guess you could have also done Array(foo)).

The changes will go away if you update. I'll try to understand what's happening here and come up with a cleaner way of doing it. That section of code happens to be on my hit list as it's asking VMWare for all the files on all the VMs on the datastore, only to filter out for the ones to do with the VM it already has. There has to be a more efficient way.

@tas50 tas50 added Type: Enhancement Adds new functionality. and removed Enhancement labels Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Adds new functionality.
Projects
None yet
Development

No branches or pull requests

3 participants