-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsphere_file_manager.rb
executable file
·57 lines (49 loc) · 2.12 KB
/
vsphere_file_manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
require 'thor'
require 'rbvmomi'
class VsphereFileManager < Thor
desc 'upload local_path remote_path', 'Upload a file to a datastore'
method_option :datacenter, :required => true, :type => :string
method_option :datastore, :required => true, :type => :string
method_option :vcenter, :required => true, :type => :string
method_option :username, :required => true, :type => :string
method_option :password, :required => true, :type => :string
method_option :insecure, :required => false, :type => :boolean, :default => false
method_option :overwrite, :required => false, :type => :boolean, :default => false
def upload(local_path, remote_path)
vim = connect_vcenter options[:vcenter],
options[:username],
options[:password],
options[:insecure]
dc = vim.serviceInstance.find_datacenter(options[:datacenter])
ds = dc.find_datastore(options[:datastore])
raise 'Remote file already exists!' if ds.exists?(remote_path) and not options[:overwrite]
ds.upload remote_path, local_path
end
desc 'download remote_path local_path', 'Download a file from a remote datastore'
method_option :datacenter, :required => true, :type => :string
method_option :datastore, :required => true, :type => :string
method_option :vcenter, :required => true, :type => :string
method_option :username, :required => true, :type => :string
method_option :password, :required => true, :type => :string
method_option :insecure, :required => false, :type => :boolean, :default => false
def download(remote_path, local_path)
vim = connect_vcenter options[:vcenter],
options[:username],
options[:password],
options[:insecure]
dc = vim.serviceInstance.find_datacenter(options[:datacenter])
ds = dc.find_datastore(options[:datastore])
raise 'Remote file does not exist!' if not ds.exists?(remote_path)
ds.download remote_path, local_path
end
no_commands do
def connect_vcenter(host, user, password, insecure)
RbVmomi::VIM.connect host: host,
user: user,
password: password,
insecure: insecure
end
end
end
VsphereFileManager.start