knife-vsphere 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,41 +1,39 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
-
6
- require 'chef/knife'
7
- require 'chef/knife/BaseVsphereCommand'
8
- require 'rbvmomi'
9
-
10
- # Delete a virtual machine from vCenter
11
- class Chef::Knife::VsphereVmDelete < Chef::Knife::BaseVsphereCommand
12
-
13
- banner "knife vsphere vm delete VMNAME"
14
-
15
- get_common_options
16
-
17
- def run
18
- $stdout.sync = true
19
-
20
- vmname = @name_args[0]
21
-
22
- if vmname.nil?
23
- show_usage
24
- fatal_exit("You must specify a virtual machine name")
25
- end
26
-
27
- vim = get_vim_connection
28
-
29
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
30
- dc = vim.serviceInstance.find_datacenter(dcname) or
31
- fatal_exit("datacenter not found")
32
-
33
- vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, vmname) or
34
- fatal_exit("VM #{vmname} not found")
35
-
36
- vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
37
- vm.Destroy_Task
38
- puts "Deleted virtual machine #{vmname}"
39
-
40
- end
41
- end
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'chef/knife'
7
+ require 'chef/knife/BaseVsphereCommand'
8
+ require 'rbvmomi'
9
+
10
+ # Delete a virtual machine from vCenter
11
+ class Chef::Knife::VsphereVmDelete < Chef::Knife::BaseVsphereCommand
12
+
13
+ banner "knife vsphere vm delete VMNAME"
14
+
15
+ get_common_options
16
+
17
+ def run
18
+ $stdout.sync = true
19
+
20
+ vmname = @name_args[0]
21
+
22
+ if vmname.nil?
23
+ show_usage
24
+ fatal_exit("You must specify a virtual machine name")
25
+ end
26
+
27
+ vim = get_vim_connection
28
+
29
+ baseFolder = find_folder(config[:folder]);
30
+
31
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
32
+ fatal_exit("VM #{vmname} not found")
33
+
34
+ vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
35
+ vm.Destroy_Task
36
+ puts "Deleted virtual machine #{vmname}"
37
+
38
+ end
39
+ end
@@ -1,42 +1,28 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- require 'chef/knife'
6
- require 'chef/knife/BaseVsphereCommand'
7
-
8
- # Lists all known virtual machines in the configured datacenter
9
- class Chef::Knife::VsphereVmList < Chef::Knife::BaseVsphereCommand
10
-
11
- banner "knife vsphere vm list"
12
-
13
- get_common_options
14
-
15
- option :folder,
16
- :short => "-f SHOWFOLDER",
17
- :long => "--folder",
18
- :description => "The folder to list VMs in"
19
-
20
- def run
21
-
22
- $stdout.sync = true
23
-
24
- vim = get_vim_connection
25
-
26
-
27
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
28
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
29
-
30
- baseFolder = dc.vmFolder;
31
-
32
- if config[:folder]
33
- baseFolder = get_folders(dc.vmFolder).find { |f| f.name == config[:folder]} or
34
- abort "no such folder #{config[:folder]}"
35
- end
36
-
37
- vms = find_all_in_folders(baseFolder, RbVmomi::VIM::VirtualMachine)
38
- vms.each do |vm|
39
- puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
40
- end
41
- end
42
- end
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/BaseVsphereCommand'
7
+
8
+ # Lists all known virtual machines in the configured datacenter
9
+ class Chef::Knife::VsphereVmList < Chef::Knife::BaseVsphereCommand
10
+
11
+ banner "knife vsphere vm list"
12
+
13
+ get_common_options
14
+
15
+ def run
16
+
17
+ $stdout.sync = true
18
+
19
+ vim = get_vim_connection
20
+
21
+ baseFolder = find_folder(config[:folder]);
22
+
23
+ vms = find_all_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine)
24
+ vms.each do |vm|
25
+ puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
26
+ end
27
+ end
28
+ end
@@ -1,89 +1,85 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
-
6
- require 'chef/knife'
7
- require 'chef/knife/BaseVsphereCommand'
8
- require 'rbvmomi'
9
- require 'netaddr'
10
-
11
- PsOn = 'poweredOn'
12
- PsOff = 'poweredOff'
13
- PsSuspended = 'suspended'
14
-
15
- PowerStates = {
16
- PsOn => 'powered on',
17
- PsOff => 'powered off',
18
- PsSuspended => 'suspended'
19
- }
20
-
21
- # Manage power state of a virtual machine
22
- class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
23
-
24
- banner "knife vsphere vm state VMNAME (options)"
25
-
26
- get_common_options
27
-
28
- option :state,
29
- :short => "-s STATE",
30
- :long => "--state STATE",
31
- :description => "The power state to transition the VM into; one of on|off|suspended"
32
-
33
- def run
34
-
35
- $stdout.sync = true
36
-
37
- vmname = @name_args[0]
38
- if vmname.nil?
39
- show_usage
40
- ui.fatal("You must specify a virtual machine name")
41
- exit 1
42
- end
43
-
44
- vim = get_vim_connection
45
-
46
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
47
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
48
-
49
- hosts = find_all_in_folders(dc.hostFolder, RbVmomi::VIM::ComputeResource)
50
- rp = hosts.first.resourcePool
51
-
52
- vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, vmname) or
53
- abort "VM #{vmname} not found"
54
-
55
- state = vm.runtime.powerState
56
-
57
- if config[:state].nil?
58
- puts "VM #{vmname} is currently " + PowerStates[vm.runtime.powerState]
59
- else
60
-
61
- case config[:state]
62
- when 'on'
63
- if state == PsOn
64
- puts "Virtual machine #{vmname} was already powered on"
65
- else
66
- vm.PowerOnVM_Task.wait_for_completion
67
- puts "Powered on virtual machine #{vmname}"
68
- end
69
- when 'off'
70
- if state == PsOff
71
- puts "Virtual machine #{vmname} was already powered off"
72
- else
73
- vm.PowerOffVM_Task.wait_for_completion
74
- puts "Powered off virtual machine #{vmname}"
75
- end
76
- when 'suspend'
77
- if state == PowerStates['suspended']
78
- puts "Virtual machine #{vmname} was already suspended"
79
- else
80
- vm.SuspendVM_Task.wait_for_completion
81
- puts "Suspended virtual machine #{vmname}"
82
- end
83
- when 'reset'
84
- vm.ResetVM_Task.wait_for_completion
85
- puts "Reset virtual machine #{vmname}"
86
- end
87
- end
88
- end
89
- end
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'chef/knife'
7
+ require 'chef/knife/BaseVsphereCommand'
8
+ require 'rbvmomi'
9
+ require 'netaddr'
10
+
11
+ PsOn = 'poweredOn'
12
+ PsOff = 'poweredOff'
13
+ PsSuspended = 'suspended'
14
+
15
+ PowerStates = {
16
+ PsOn => 'powered on',
17
+ PsOff => 'powered off',
18
+ PsSuspended => 'suspended'
19
+ }
20
+
21
+ # Manage power state of a virtual machine
22
+ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
23
+
24
+ banner "knife vsphere vm state VMNAME (options)"
25
+
26
+ get_common_options
27
+
28
+ option :state,
29
+ :short => "-s STATE",
30
+ :long => "--state STATE",
31
+ :description => "The power state to transition the VM into; one of on|off|suspended"
32
+
33
+ def run
34
+
35
+ $stdout.sync = true
36
+
37
+ vmname = @name_args[0]
38
+ if vmname.nil?
39
+ show_usage
40
+ ui.fatal("You must specify a virtual machine name")
41
+ exit 1
42
+ end
43
+
44
+ vim = get_vim_connection
45
+
46
+ baseFolder = find_folder(config[:folder]);
47
+
48
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
49
+ abort "VM #{vmname} not found"
50
+
51
+ state = vm.runtime.powerState
52
+
53
+ if config[:state].nil?
54
+ puts "VM #{vmname} is currently " + PowerStates[vm.runtime.powerState]
55
+ else
56
+
57
+ case config[:state]
58
+ when 'on'
59
+ if state == PsOn
60
+ puts "Virtual machine #{vmname} was already powered on"
61
+ else
62
+ vm.PowerOnVM_Task.wait_for_completion
63
+ puts "Powered on virtual machine #{vmname}"
64
+ end
65
+ when 'off'
66
+ if state == PsOff
67
+ puts "Virtual machine #{vmname} was already powered off"
68
+ else
69
+ vm.PowerOffVM_Task.wait_for_completion
70
+ puts "Powered off virtual machine #{vmname}"
71
+ end
72
+ when 'suspend'
73
+ if state == PowerStates['suspended']
74
+ puts "Virtual machine #{vmname} was already suspended"
75
+ else
76
+ vm.SuspendVM_Task.wait_for_completion
77
+ puts "Suspended virtual machine #{vmname}"
78
+ end
79
+ when 'reset'
80
+ vm.ResetVM_Task.wait_for_completion
81
+ puts "Reset virtual machine #{vmname}"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,4 +1,4 @@
1
- module KnifeVsphere
2
- VERSION = "0.1.5"
3
- end
4
-
1
+ module KnifeVsphere
2
+ VERSION = "0.1.6"
3
+ end
4
+
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-vsphere
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
5
+ version: 0.1.6
11
6
  platform: ruby
12
7
  authors:
13
8
  - Ezra Pagel
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-07-06 00:00:00 -05:00
13
+ date: 2012-01-17 00:00:00 -06:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,11 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ~>
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 1
32
- - 5
33
- - 0
34
24
  version: 1.5.0
35
25
  type: :runtime
36
26
  version_requirements: *id001
@@ -42,11 +32,6 @@ dependencies:
42
32
  requirements:
43
33
  - - ~>
44
34
  - !ruby/object:Gem::Version
45
- hash: 55
46
- segments:
47
- - 0
48
- - 10
49
- - 0
50
35
  version: 0.10.0
51
36
  type: :runtime
52
37
  version_requirements: *id002
@@ -58,11 +43,6 @@ dependencies:
58
43
  requirements:
59
44
  - - ~>
60
45
  - !ruby/object:Gem::Version
61
- hash: 25
62
- segments:
63
- - 1
64
- - 2
65
- - 3
66
46
  version: 1.2.3
67
47
  type: :runtime
68
48
  version_requirements: *id003
@@ -97,18 +77,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
77
  requirements:
98
78
  - - ">="
99
79
  - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
80
  version: "0"
104
81
  required_rubygems_version: !ruby/object:Gem::Requirement
105
82
  none: false
106
83
  requirements:
107
84
  - - ">="
108
85
  - !ruby/object:Gem::Version
109
- hash: 3
110
- segments:
111
- - 0
112
86
  version: "0"
113
87
  requirements: []
114
88