Last active
June 28, 2017 13:25
-
-
Save vholer/acc3c7e467d0657275ab5a52d6695a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'optparse' | |
require 'rbvmomi' | |
# but have | |
OptionParser.new do |opts| | |
opts.on('-h', '--host=HOST', 'vCenter host') { |v| HOST=v } | |
opts.on('-u', '--user=NAME', 'vCenter login user') { |v| USER=v } | |
opts.on('-p', '--password=PASSWORD', 'vCenter password') { |v| PSWD=v } | |
end.parse! | |
# take defaults from env | |
unless defined? HOST | |
HOST=ENV["VC_HOST"] | |
end | |
unless defined? USER | |
USER=ENV["VC_USER"] | |
end | |
unless defined? PSWD | |
PSWD=ENV["VC_PASSWORD"] | |
end | |
unless HOST && USER && PSWD | |
puts <<EOT | |
Usage: #{$0} [arguments] | |
Mandatory arguments: | |
--host=name .... vCenter hostname | |
--user=name .... vCenter login user name | |
--password=text .... vCenter password | |
EOT | |
exit 1 | |
end | |
##### | |
def get(folder, type, entities=[]) | |
return nil if folder == [] | |
folder.childEntity.each do |child| | |
name, junk = child.to_s.split('(') | |
case name | |
when "Folder" | |
get(child, type, entities) | |
when type | |
entities.push(child) | |
end | |
end | |
return entities | |
end | |
# vCenter connection | |
VIM = RbVmomi::VIM | |
begin | |
vim = RbVmomi::VIM.connect( | |
host: HOST, | |
user: USER, | |
password: PSWD, | |
insecure: true) | |
rescue RbVmomi::Fault => e | |
puts "ERROR: #{e.message}" | |
exit 1 | |
end | |
sc = vim.serviceInstance.content | |
rf = sc.rootFolder | |
# about | |
puts "Name: " + sc.about.name.to_s | |
puts "Version: " + sc.about.version.to_s | |
puts "Build: " + sc.about.build.to_s | |
puts "API version: " + sc.about.apiVersion.to_s | |
puts "Login user: " + USER | |
puts | |
dirs = [ | |
'segfault_test/10/test', | |
'segfault_test/11/test', | |
'segfault_test/12/test', | |
'segfault/' | |
] | |
get(rf, 'Datacenter').each { |dc| | |
puts "Datacenter: #{dc.name}" | |
dc.datastore.each { |ds| | |
puts "Datastore: #{ds.name}" | |
# create | |
dirs.each { |n| | |
directory_name = "[#{ds.name}] #{n}" | |
directory_params = { | |
:name => directory_name, | |
:datacenter => dc, | |
:createParentDirectories => true | |
} | |
sc.fileManager.MakeDirectory(directory_params) | |
} | |
# delete | |
dirs.each { |n| | |
directory_name = "[#{ds.name}] #{n}" | |
directory_params = { | |
:name => directory_name, | |
:datacenter => dc, | |
} | |
begin | |
sc.fileManager.DeleteDatastoreFile_Task(directory_params) #.wait_for_completion | |
rescue | |
end | |
} | |
# Note: if there is sleep enough to process the tasks | |
# before exit, the vpxd doesn't crash anymore | |
#sleep 5 | |
sc.sessionManager.Logout | |
vim.close | |
exit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment