knife-vsphere 0.9.9 → 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,10 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
30
30
  :long => "--datastore STORE",
31
31
  :description => "The datastore into which to put the cloned VM"
32
32
 
33
+ option :datastorecluster,
34
+ :long => "--datastorecluster STORE",
35
+ :description => "The datastorecluster into which to put the cloned VM"
36
+
33
37
  option :resource_pool,
34
38
  :long => "--resource-pool POOL",
35
39
  :description => "The resource pool into which to put the cloned VM"
@@ -303,10 +307,24 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
303
307
  rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:diskMoveType => :moveChildMostDiskBacking)
304
308
  end
305
309
 
310
+ if get_config(:datastore) && get_config(:datastorecluster)
311
+ abort "Please select either datastore or datastorecluster"
312
+ end
313
+
306
314
  if get_config(:datastore)
307
315
  rspec.datastore = find_datastore(get_config(:datastore))
308
316
  end
309
317
 
318
+ if get_config(:datastorecluster)
319
+ dsc = find_datastorecluster(get_config(:datastorecluster))
320
+
321
+ dsc.childEntity.each do |store|
322
+ if (rspec.datastore == nil or rspec.datastore.summary[:freeSpace] < store.summary[:freeSpace])
323
+ rspec.datastore = store
324
+ end
325
+ end
326
+ end
327
+
310
328
  clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => rspec,
311
329
  :powerOn => false,
312
330
  :template => false)
@@ -393,11 +411,11 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
393
411
  cust_spec.identity = ident
394
412
  end
395
413
 
414
+ clone_spec.customization = cust_spec
415
+
396
416
  if customization_plugin && customization_plugin.respond_to?(:customize_clone_spec)
397
417
  clone_spec = customization_plugin.customize_clone_spec(src_config, clone_spec)
398
418
  end
399
-
400
- clone_spec.customization = cust_spec
401
419
  end
402
420
  clone_spec
403
421
  end
@@ -1,66 +1,66 @@
1
- # Author:: Ian Delahorne (<ian@delahorne.com>)
2
- # License:: Apache License, Version 2.0
3
-
4
- require 'chef/knife'
5
- require 'chef/knife/base_vsphere_command'
6
- require 'rbvmomi'
7
- require 'netaddr'
8
-
9
- class Chef::Knife::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
10
- banner "knife vsphere vm execute VMNAME COMMAND ARGS"
11
-
12
- option :exec_user,
13
- :long => "--exec-user USER",
14
- :description => "User to execute as",
15
- :required => true
16
-
17
- option :exec_passwd,
18
- :long => "--exec-passwd PASSWORD",
19
- :description => "Password for execute user",
20
- :required => true
21
-
22
- option :exec_dir,
23
- :long => "--exec-dir DIRECTORY",
24
- :description => "Working directory to execute in"
25
-
26
- get_common_options
27
-
28
- def run
29
- $stdout.sync = true
30
- vmname = @name_args[0]
31
- if vmname.nil?
32
- show_usage
33
- fatal_exit("You must specify a virtual machine name")
34
- end
35
- command = @name_args[1]
36
- if command.nil?
37
- show_usage
38
- fatal_exit("You must specify a command to execute")
39
- end
40
-
41
- args = @name_args[2]
42
- if args.nil?
43
- args = ""
44
- end
45
-
46
- vim = get_vim_connection
47
-
48
- dc = get_datacenter
49
- folder = find_folder(get_config(:folder)) || dc.vmFolder
50
-
51
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
52
- abort "VM #{vmname} not found"
53
-
54
- gom = vim.serviceContent.guestOperationsManager
55
-
56
- guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
57
- :username => config[:exec_user],
58
- :password => config[:exec_passwd])
59
- prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
60
- :arguments => args,
61
- :workingDirectory => get_config(:exec_dir))
62
-
63
- gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
64
-
65
- end
66
- end
1
+ # Author:: Ian Delahorne (<ian@delahorne.com>)
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'chef/knife'
5
+ require 'chef/knife/base_vsphere_command'
6
+ require 'rbvmomi'
7
+ require 'netaddr'
8
+
9
+ class Chef::Knife::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
10
+ banner "knife vsphere vm execute VMNAME COMMAND ARGS"
11
+
12
+ option :exec_user,
13
+ :long => "--exec-user USER",
14
+ :description => "User to execute as",
15
+ :required => true
16
+
17
+ option :exec_passwd,
18
+ :long => "--exec-passwd PASSWORD",
19
+ :description => "Password for execute user",
20
+ :required => true
21
+
22
+ option :exec_dir,
23
+ :long => "--exec-dir DIRECTORY",
24
+ :description => "Working directory to execute in"
25
+
26
+ get_common_options
27
+
28
+ def run
29
+ $stdout.sync = true
30
+ vmname = @name_args[0]
31
+ if vmname.nil?
32
+ show_usage
33
+ fatal_exit("You must specify a virtual machine name")
34
+ end
35
+ command = @name_args[1]
36
+ if command.nil?
37
+ show_usage
38
+ fatal_exit("You must specify a command to execute")
39
+ end
40
+
41
+ args = @name_args[2]
42
+ if args.nil?
43
+ args = ""
44
+ end
45
+
46
+ vim = get_vim_connection
47
+
48
+ dc = get_datacenter
49
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
50
+
51
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
52
+ abort "VM #{vmname} not found"
53
+
54
+ gom = vim.serviceContent.guestOperationsManager
55
+
56
+ guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
57
+ :username => config[:exec_user],
58
+ :password => config[:exec_passwd])
59
+ prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
60
+ :arguments => args,
61
+ :workingDirectory => get_config(:exec_dir))
62
+
63
+ gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
64
+
65
+ end
66
+ end
@@ -0,0 +1,54 @@
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # Contributor:: Jesse Campbell (<hikeit@gmail.com>)
4
+ # Contributor:: Bethany Erskine (<bethany@paperlesspost.com>)
5
+ # Contributor:: Adrian Stanila (https://github.com/sacx)
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+
9
+ require 'chef/knife'
10
+ require 'chef/knife/base_vsphere_command'
11
+ require 'rbvmomi'
12
+
13
+ # Clone an existing template into a new VM, optionally applying a customization specification.
14
+ # usage:
15
+ # knife vsphere vm markastemplate MyVM --folder /templates
16
+ class Chef::Knife::VsphereVmMarkastemplate < Chef::Knife::BaseVsphereCommand
17
+
18
+ banner "knife vsphere vm markastemplate VMNAME"
19
+
20
+ get_common_options
21
+
22
+ option :folder,
23
+ :long => "--folder FOLDER",
24
+ :description => "The folder which contains the VM"
25
+
26
+ def run
27
+ $stdout.sync = true
28
+
29
+ vmname = @name_args[0]
30
+ if vmname.nil?
31
+ show_usage
32
+ fatal_exit("You must specify a virtual machine name")
33
+ end
34
+ config[:chef_node_name] = vmname unless config[:chef_node_name]
35
+ config[:vmname] = vmname
36
+
37
+ if get_config(:bootstrap) && get_config(:distro) && !@@chef_config_dir
38
+ fatal_exit("Can't find .chef for bootstrap files. chdir to a location with a .chef directory and try again")
39
+ end
40
+
41
+ vim = get_vim_connection
42
+
43
+ dc = get_datacenter
44
+
45
+ src_folder = find_folder(get_config(:folder)) || dc.vmFolder
46
+
47
+ vm = find_in_folder(src_folder, RbVmomi::VIM::VirtualMachine, config[:vmname]) or
48
+ abort "VM not found"
49
+
50
+ puts "Marking VM #{vmname} as template"
51
+ vm.MarkAsTemplate()
52
+ puts "Finished marking VM #{vmname} as template"
53
+ end
54
+ end
@@ -1,129 +1,129 @@
1
- #
2
- # License:: Apache License, Version 2.0
3
- #
4
-
5
- require 'chef/knife'
6
- require 'chef/knife/base_vsphere_command'
7
- require 'rbvmomi'
8
- require 'netaddr'
9
-
10
-
11
- # Manage snapshots of a virtual machine
12
- class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
13
-
14
- banner "knife vsphere vm snapshot VMNAME (options)"
15
-
16
- get_common_options
17
-
18
- option :list,
19
- :long => "--list",
20
- :description => "The current tree of snapshots"
21
-
22
- option :create_new_snapshot,
23
- :long => "--create SNAPSHOT",
24
- :description => "Create a new snapshot off of the current snapshot."
25
-
26
- option :remove_named_snapshot,
27
- :long => "--remove SNAPSHOT",
28
- :description => "Remove a named snapshot."
29
-
30
- option :revert_snapshot,
31
- :long => "--revert SNAPSHOT",
32
- :description => "Revert to a named snapshot."
33
-
34
- option :revert_current_snapshot,
35
- :long => "--revert-current",
36
- :description => "Revert to current snapshot.",
37
- :boolean => false
38
-
39
- option :power,
40
- :long => "--start",
41
- :description => "Indicates whether to start the VM after a successful revert",
42
- :boolean => false
43
-
44
- def run
45
-
46
- $stdout.sync = true
47
-
48
- vmname = @name_args[0]
49
- if vmname.nil?
50
- show_usage
51
- ui.fatal("You must specify a virtual machine name")
52
- exit 1
53
- end
54
-
55
- vim = get_vim_connection
56
-
57
- baseFolder = find_folder(get_config(:folder));
58
-
59
- vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
60
- abort "VM #{vmname} not found"
61
-
62
- if vm.snapshot
63
- snapshot_list = vm.snapshot.rootSnapshotList
64
- current_snapshot = vm.snapshot.currentSnapshot
65
- end
66
-
67
- if config[:list] && vm.snapshot
68
- puts "Current snapshot tree: "
69
- puts "#{vmname}"
70
- snapshot_list.each { |i| puts display_node(i, current_snapshot) }
71
- end
72
-
73
- if config[:create_new_snapshot]
74
- vm.CreateSnapshot_Task(:name => config[:create_new_snapshot], :description => "", :memory => false, :quiesce => false)
75
- end
76
-
77
- if config[:remove_named_snapshot]
78
- ss_name = config[:remove_named_snapshot]
79
- snapshot = find_node(snapshot_list, ss_name)
80
- puts "Found snapshot #{ss_name} removing."
81
- snapshot.RemoveSnapshot_Task(:removeChildren => false)
82
- end
83
-
84
- if config[:revert_current_snapshot]
85
- puts "Reverting to Current Snapshot"
86
- vm.RevertToCurrentSnapshot_Task(:suppressPowerOn => false).wait_for_completion
87
- if get_config(:power)
88
- vm.PowerOnVM_Task.wait_for_completion
89
- puts "Powered on virtual machine #{vmname}"
90
- end
91
- end
92
-
93
- if config[:revert_snapshot]
94
- ss_name = config[:revert_snapshot]
95
- snapshot = find_node(snapshot_list, ss_name)
96
- snapshot.RevertToSnapshot_Task(:suppressPowerOn => false).wait_for_completion
97
- if get_config(:power)
98
- vm.PowerOnVM_Task.wait_for_completion
99
- puts "Powered on virtual machine #{vmname}"
100
- end
101
- end
102
- end
103
-
104
- def find_node(tree, name)
105
- snapshot = nil
106
- tree.each do |node|
107
- if node.name == name
108
- snapshot = node.snapshot
109
- elsif !node.childSnapshotList.empty?
110
- snapshot = find_node(node.childSnapshotList, name)
111
- end
112
- end
113
- return snapshot
114
- end
115
-
116
- def display_node(node, current, shift=1)
117
- out = ""
118
- out << "+--"*shift
119
- if node.snapshot == current
120
- out << "#{ui.color(node.name, :cyan)}" << "\n"
121
- else
122
- out << "#{node.name}" << "\n"
123
- end
124
- if !node.childSnapshotList.empty?
125
- node.childSnapshotList.each { |item| out << display_node(item, current, shift+1) }
126
- end
127
- out
128
- end
129
- end
1
+ #
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+ require 'rbvmomi'
8
+ require 'netaddr'
9
+
10
+
11
+ # Manage snapshots of a virtual machine
12
+ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
13
+
14
+ banner "knife vsphere vm snapshot VMNAME (options)"
15
+
16
+ get_common_options
17
+
18
+ option :list,
19
+ :long => "--list",
20
+ :description => "The current tree of snapshots"
21
+
22
+ option :create_new_snapshot,
23
+ :long => "--create SNAPSHOT",
24
+ :description => "Create a new snapshot off of the current snapshot."
25
+
26
+ option :remove_named_snapshot,
27
+ :long => "--remove SNAPSHOT",
28
+ :description => "Remove a named snapshot."
29
+
30
+ option :revert_snapshot,
31
+ :long => "--revert SNAPSHOT",
32
+ :description => "Revert to a named snapshot."
33
+
34
+ option :revert_current_snapshot,
35
+ :long => "--revert-current",
36
+ :description => "Revert to current snapshot.",
37
+ :boolean => false
38
+
39
+ option :power,
40
+ :long => "--start",
41
+ :description => "Indicates whether to start the VM after a successful revert",
42
+ :boolean => false
43
+
44
+ def run
45
+
46
+ $stdout.sync = true
47
+
48
+ vmname = @name_args[0]
49
+ if vmname.nil?
50
+ show_usage
51
+ ui.fatal("You must specify a virtual machine name")
52
+ exit 1
53
+ end
54
+
55
+ vim = get_vim_connection
56
+
57
+ baseFolder = find_folder(get_config(:folder));
58
+
59
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
60
+ abort "VM #{vmname} not found"
61
+
62
+ if vm.snapshot
63
+ snapshot_list = vm.snapshot.rootSnapshotList
64
+ current_snapshot = vm.snapshot.currentSnapshot
65
+ end
66
+
67
+ if config[:list] && vm.snapshot
68
+ puts "Current snapshot tree: "
69
+ puts "#{vmname}"
70
+ snapshot_list.each { |i| puts display_node(i, current_snapshot) }
71
+ end
72
+
73
+ if config[:create_new_snapshot]
74
+ vm.CreateSnapshot_Task(:name => config[:create_new_snapshot], :description => "", :memory => false, :quiesce => false)
75
+ end
76
+
77
+ if config[:remove_named_snapshot]
78
+ ss_name = config[:remove_named_snapshot]
79
+ snapshot = find_node(snapshot_list, ss_name)
80
+ puts "Found snapshot #{ss_name} removing."
81
+ snapshot.RemoveSnapshot_Task(:removeChildren => false)
82
+ end
83
+
84
+ if config[:revert_current_snapshot]
85
+ puts "Reverting to Current Snapshot"
86
+ vm.RevertToCurrentSnapshot_Task(:suppressPowerOn => false).wait_for_completion
87
+ if get_config(:power)
88
+ vm.PowerOnVM_Task.wait_for_completion
89
+ puts "Powered on virtual machine #{vmname}"
90
+ end
91
+ end
92
+
93
+ if config[:revert_snapshot]
94
+ ss_name = config[:revert_snapshot]
95
+ snapshot = find_node(snapshot_list, ss_name)
96
+ snapshot.RevertToSnapshot_Task(:suppressPowerOn => false).wait_for_completion
97
+ if get_config(:power)
98
+ vm.PowerOnVM_Task.wait_for_completion
99
+ puts "Powered on virtual machine #{vmname}"
100
+ end
101
+ end
102
+ end
103
+
104
+ def find_node(tree, name)
105
+ snapshot = nil
106
+ tree.each do |node|
107
+ if node.name == name
108
+ snapshot = node.snapshot
109
+ elsif !node.childSnapshotList.empty?
110
+ snapshot = find_node(node.childSnapshotList, name)
111
+ end
112
+ end
113
+ return snapshot
114
+ end
115
+
116
+ def display_node(node, current, shift=1)
117
+ out = ""
118
+ out << "+--"*shift
119
+ if node.snapshot == current
120
+ out << "#{ui.color(node.name, :cyan)}" << "\n"
121
+ else
122
+ out << "#{node.name}" << "\n"
123
+ end
124
+ if !node.childSnapshotList.empty?
125
+ node.childSnapshotList.each { |item| out << display_node(item, current, shift+1) }
126
+ end
127
+ out
128
+ end
129
+ end