knife-vsphere 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -100,6 +100,26 @@ class Chef
100
100
  vim = RbVmomi::VIM.connect conn_opts
101
101
  return vim
102
102
  end
103
+
104
+ def get_folders(folder)
105
+ folder.childEntity.grep(RbVmomi::VIM::Folder) << folder
106
+ end
107
+
108
+ def find_all_in_folders(folder, type)
109
+ get_folders(folder).
110
+ collect { |f| f.childEntity.grep(type) }.
111
+ flatten
112
+ end
113
+
114
+ def find_in_folders(folder, type, name)
115
+ get_folders(folder).
116
+ collect { |f| f.childEntity.grep(type) }.
117
+ flatten.
118
+ find { |o| o.name == name }
119
+ end
120
+
121
+
122
+
103
123
  end
104
124
  end
105
125
  end
@@ -34,16 +34,14 @@ class Chef
34
34
 
35
35
  dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
36
36
  dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
37
- vmFolder = dc.vmFolder
38
-
39
- vms = vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine)
40
- if (!vms.nil?)
41
- templates = vms.find_all{ |v| !v.config.nil? && v.config.template == true }
42
- if (!templates.nil?)
43
- templates.each do |vm|
44
- puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
45
- end
46
- end
37
+
38
+ vmFolders = get_folders(dc.vmFolder)
39
+
40
+ vms = find_all_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine).
41
+ select {|v| !v.config.nil? && v.config.template == true }
42
+
43
+ vms.each do |vm|
44
+ puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
47
45
  end
48
46
  end
49
47
  end
@@ -44,16 +44,15 @@ class Chef
44
44
 
45
45
  dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
46
46
  dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
47
- vmFolder = dc.vmFolder
48
- hosts = dc.hostFolder.children
47
+
48
+ hosts = find_all_in_folders(dc.hostFolder, RbVmomi::VIM::ComputeResource)
49
49
  rp = hosts.first.resourcePool
50
50
 
51
51
  template = config[:template] or abort "source template name required"
52
52
  vmname = config[:vmname] or abort "destination vm name required"
53
53
 
54
- puts "searching for template #{template}"
55
-
56
- src_vm = dc.find_vm(template) or abort "VM not found"
54
+ src_vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, template) or
55
+ abort "VM/Template not found"
57
56
 
58
57
  rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp)
59
58
  spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => rspec,
@@ -66,6 +65,7 @@ class Chef
66
65
  puts "Finished creating virtual machine #{vmname}"
67
66
 
68
67
  end
68
+
69
69
  end
70
70
  end
71
71
  end
@@ -22,6 +22,7 @@ class Chef
22
22
  class Knife
23
23
  class VsphereVmDelete < VsphereBaseCommand
24
24
 
25
+
25
26
  banner "knife vsphere vm delete (options)"
26
27
 
27
28
  get_common_options
@@ -42,7 +43,8 @@ class Chef
42
43
  dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
43
44
  dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
44
45
 
45
- vm = dc.find_vm(vmname) or abort "VM not found"
46
+ vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, vmname) or
47
+ abort "VM not found"
46
48
 
47
49
  vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
48
50
  vm.UnregisterVM
@@ -1,5 +1,4 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
1
+ #r# Author:: Ezra Pagel (<ezra@cpan.org>)
3
2
  # License:: Apache License, Version 2.0
4
3
  #
5
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +24,10 @@ class Chef
25
24
 
26
25
  get_common_options
27
26
 
27
+ option :folder,
28
+ :short => "-f SHOWFOLDER",
29
+ :long => "--folder",
30
+ :description => "The folder to list VMs in"
28
31
 
29
32
  def run
30
33
 
@@ -32,12 +35,20 @@ class Chef
32
35
 
33
36
  vim = get_vim_connection
34
37
 
38
+
35
39
  dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
36
40
  dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
37
- vmFolder = dc.vmFolder
38
-
39
- vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine).each do |vm|
40
- puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
41
+
42
+ baseFolder = dc.vmFolder;
43
+
44
+ if config[:folder]
45
+ baseFolder = get_folders(dc.vmFolder).find { |f| f.name == config[:folder]} or
46
+ abort "no such folder #{config[:folder]}"
47
+ end
48
+
49
+ vms = find_all_in_folders(baseFolder, RbVmomi::VIM::VirtualMachine)
50
+ vms.each do |vm|
51
+ puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
41
52
  end
42
53
  end
43
54
  end
@@ -1,4 +1,4 @@
1
1
  module KnifeVsphere
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-vsphere
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ezra Pagel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-29 00:00:00 -05:00
18
+ date: 2011-06-30 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -59,11 +59,9 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
 
61
61
  files:
62
- - lib/chef/knife/#vsphere_vm_clone.rb#
63
62
  - lib/chef/knife/vsphere_template_list.rb
64
63
  - lib/chef/knife/vsphere_vm_clone.rb
65
64
  - lib/chef/knife/vsphere_vm_delete.rb
66
- - lib/chef/knife/vsphere_vm_delete.rb~
67
65
  - lib/chef/knife/vsphere_vm_list.rb
68
66
  - lib/chef/knife/VsphereBaseCommand.rb
69
67
  - lib/knife-vsphere/version.rb
@@ -1,71 +0,0 @@
1
- V#
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'chef/knife'
18
- require 'chef/knife/VsphereBaseCommand'
19
- require 'rbvmomi'
20
-
21
- class Chef
22
- class Knife
23
- class VsphereVmClone < VsphereBaseCommand
24
-
25
- banner "knife vsphere vm clone (options)"
26
-
27
- get_common_options
28
-
29
- option :template,
30
- :short => "-t TEMPLATE",
31
- :long => "--template TEMPLATE",
32
- :description => "The template to create the VM from"
33
-
34
- option :vmname,
35
- :short => "-N VMNAME",
36
- :long => "--vmname VMNAME",
37
- :description => "The name for the new virtual machine"
38
-
39
- def run
40
-
41
- $stdout.sync = true
42
-
43
- vim = get_vim_connection
44
-
45
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
46
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
47
- vmFolder = dc.vmFolder
48
- hosts = dc.hostFolder.children
49
- rp = hosts.first.resourcePool
50
-
51
- template = config[:template] or abort "source template name required"
52
- vmname = config[:vmname] or abort "destination vm name required"
53
-
54
- puts "searching for template #{template}"
55
-
56
- src_vm = dc.find_vm(template) or abort "VM not found"
57
-
58
- rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp)
59
- spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => rspec,
60
- :powerOn => false,
61
- :template => false)
62
-
63
- task = src_vm.CloneVM_Task(:folder => src_vm.parent, :name => vmname, :spec => spec)
64
- puts "Cloning template #{template} to new VM #{vmname}"
65
- task.wait_for_completion
66
- puts "Finished creating virtual machine #{vmname}"
67
-
68
- end
69
- end
70
- end
71
- end
@@ -1,54 +0,0 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'chef/knife'
18
- require 'chef/knife/VsphereBaseCommand'
19
- require 'rbvmomi'
20
-
21
- class Chef
22
- class Knife
23
- class VsphereVmDelete < VsphereBaseCommand
24
-
25
- banner "knife vsphere vm delete (options)"
26
-
27
- get_common_options
28
-
29
- option :vmname,
30
- :short => "-N VMNAME",
31
- :long => "--vmname VMNAME",
32
- :description => "The name of the virtual machine to delete"
33
-
34
- def run
35
-
36
- $stdout.sync = true
37
-
38
- vmname = config[:vmname] or abort "virtual machine name required"
39
-
40
- vim = get_vim_connection
41
-
42
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
43
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
44
-
45
- vm = dc.find_vm(vmname) or abort "VM not found"
46
-
47
- vm.PowerOffVM_Task.wait_for_completion
48
- vm.UnregisterVM
49
- puts "Finished unregistering virtual machine #{vmname}"
50
-
51
- end
52
- end
53
- end
54
- end