knife-vsphere 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ #
2
+ # Author:: Brian Dupras (<bdupras@rallydev.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+
8
+ # Lists all known virtual machines in the configured datacenter
9
+ class Chef::Knife::VsphereVmMove < Chef::Knife::BaseVsphereCommand
10
+
11
+ banner "knife vsphere vm move"
12
+
13
+ get_common_options
14
+
15
+ option :dest_name,
16
+ :long => "--dest-name NAME",
17
+ :short => "-r",
18
+ :description => "Destination name of the VM or template"
19
+
20
+ option :dest_folder,
21
+ :long => "--dest-folder FOLDER",
22
+ :description => "The destination folder into which the VM or template should be moved"
23
+
24
+
25
+ def run
26
+ $stdout.sync = true
27
+ vmname = @name_args[0]
28
+ if vmname.nil?
29
+ show_usage
30
+ fatal_exit("You must specify a virtual machine name")
31
+ end
32
+
33
+ vim = get_vim_connection
34
+ dcname = get_config(:vsphere_dc)
35
+ dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
36
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
37
+
38
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
39
+ abort "VM #{vmname} not found"
40
+
41
+ dest_name = config[:dest_name] || vmname
42
+ dest_folder = config[:dest_folder].nil? ? (vm.parent) : (find_folder(get_config(:dest_folder)))
43
+
44
+ vm.Rename_Task(:newName => dest_name).wait_for_completion unless vmname == dest_name
45
+ dest_folder.MoveIntoFolder_Task(:list => [vm]).wait_for_completion unless folder == dest_folder
46
+ end
47
+ end
@@ -1,29 +1,29 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- require 'chef/knife'
6
- require 'chef/knife/base_vsphere_command'
7
-
8
- # Lists all customization specifications in the configured datacenter
9
- class Chef::Knife::VsphereCustomizationList < Chef::Knife::BaseVsphereCommand
10
-
11
- banner "knife vsphere customization list"
12
-
13
- get_common_options
14
-
15
- def run
16
-
17
- $stdout.sync = true
18
-
19
- vim = get_vim_connection
20
-
21
- csm = vim.serviceContent.customizationSpecManager
22
- csm.info.each do |c|
23
- puts "#{ui.color("Customization Name", :cyan)}: #{c.name}"
24
-
25
- end
26
-
27
- end
28
- end
29
-
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+
8
+ # Lists all customization specifications in the configured datacenter
9
+ class Chef::Knife::VsphereCustomizationList < Chef::Knife::BaseVsphereCommand
10
+
11
+ banner "knife vsphere customization list"
12
+
13
+ get_common_options
14
+
15
+ def run
16
+
17
+ $stdout.sync = true
18
+
19
+ vim = get_vim_connection
20
+
21
+ csm = vim.serviceContent.customizationSpecManager
22
+ csm.info.each do |c|
23
+ puts "#{ui.color("Customization Name", :cyan)}: #{c.name}"
24
+
25
+ end
26
+
27
+ end
28
+ end
29
+
@@ -1,59 +1,58 @@
1
- # Copyright (C) 2012, SCM Ventures AB
2
- # Author: Ian Delahorne <ian@scmventures.se>
3
- #
4
- # Permission to use, copy, modify, and/or distribute this software for
5
- # any purpose with or without fee is hereby granted, provided that the
6
- # above copyright notice and this permission notice appear in all
7
- # copies.
8
- #
9
- # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10
- # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11
- # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12
- # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13
- # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
14
- # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15
- # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
- # PERFORMANCE OF THIS SOFTWARE
17
-
18
- require 'chef/knife'
19
- require 'chef/knife/base_vsphere_command'
20
-
21
- def number_to_human_size(number)
22
- number = number.to_f
23
- storage_units_fmt = ["byte", "kB", "MB", "GB", "TB"]
24
- base = 1024
25
- if number.to_i < base
26
- unit = storage_units_fmt[0]
27
- else
28
- max_exp = storage_units_fmt.size - 1
29
- exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
30
- exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
31
- number /= base ** exponent
32
- unit = storage_units_fmt[exponent]
33
- end
34
-
35
- return sprintf("%0.2f %s", number, unit)
36
- end
37
-
38
-
39
- # Lists all known data stores in datacenter with sizes
40
- class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
41
-
42
- banner "knife vsphere datastore list"
43
-
44
- get_common_options
45
-
46
- def run
47
- $stdout.sync = true
48
-
49
- vim = get_vim_connection
50
- dcname = get_config(:vsphere_dc)
51
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
52
- dc.datastore.each do |store|
53
- avail = number_to_human_size(store.summary[:freeSpace])
54
- cap = number_to_human_size(store.summary[:capacity])
55
- puts "#{ui.color("Datastore", :cyan)}: #{store.name} (#{avail} / #{cap})"
56
- end
57
- end
58
- end
59
-
1
+ # Copyright (C) 2012, SCM Ventures AB
2
+ # Author: Ian Delahorne <ian@scmventures.se>
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for
5
+ # any purpose with or without fee is hereby granted, provided that the
6
+ # above copyright notice and this permission notice appear in all
7
+ # copies.
8
+ #
9
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10
+ # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12
+ # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13
+ # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
14
+ # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15
+ # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ # PERFORMANCE OF THIS SOFTWARE
17
+
18
+ require 'chef/knife'
19
+ require 'chef/knife/base_vsphere_command'
20
+
21
+ def number_to_human_size(number)
22
+ number = number.to_f
23
+ storage_units_fmt = ["byte", "kB", "MB", "GB", "TB"]
24
+ base = 1024
25
+ if number.to_i < base
26
+ unit = storage_units_fmt[0]
27
+ else
28
+ max_exp = storage_units_fmt.size - 1
29
+ exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
30
+ exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
31
+ number /= base ** exponent
32
+ unit = storage_units_fmt[exponent]
33
+ end
34
+
35
+ return sprintf("%0.2f %s", number, unit)
36
+ end
37
+
38
+
39
+ # Lists all known data stores in datacenter with sizes
40
+ class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
41
+
42
+ banner "knife vsphere datastore list"
43
+
44
+ get_common_options
45
+
46
+ def run
47
+ $stdout.sync = true
48
+
49
+ vim = get_vim_connection
50
+ dc = get_datacenter
51
+ dc.datastore.each do |store|
52
+ avail = number_to_human_size(store.summary[:freeSpace])
53
+ cap = number_to_human_size(store.summary[:capacity])
54
+ puts "#{ui.color("Datastore", :cyan)}: #{store.name} (#{avail} / #{cap})"
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,49 @@
1
+ # Copyright (C) 2012, SCM Ventures AB
2
+ # Author: Ian Delahorne <ian@scmventures.se>
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for
5
+ # any purpose with or without fee is hereby granted, provided that the
6
+ # above copyright notice and this permission notice appear in all
7
+ # copies.
8
+ #
9
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10
+ # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12
+ # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13
+ # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
14
+ # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15
+ # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ # PERFORMANCE OF THIS SOFTWARE
17
+
18
+ require 'chef/knife'
19
+ require 'chef/knife/base_vsphere_command'
20
+
21
+ # Gets the data store with the most free space in datacenter
22
+ class Chef::Knife::VsphereDatastoreMaxfree < Chef::Knife::BaseVsphereCommand
23
+
24
+ banner "knife vsphere datastore maxfree"
25
+
26
+ option :regex,
27
+ :short => "-r REGEX",
28
+ :long => "--regex REGEX",
29
+ :description => "Regex to match the datastore name"
30
+ $default[:regex] = ''
31
+
32
+ get_common_options
33
+
34
+ def run
35
+ $stdout.sync = true
36
+
37
+ vim = get_vim_connection
38
+ dcname = get_config(:vsphere_dc)
39
+ regex = /#{Regexp.escape( get_config(:regex))}/
40
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
41
+ max = nil
42
+ dc.datastore.each do |store|
43
+ if regex.match(store.name) and (max == nil or max.summary[:freeSpace] < store.summary[:freeSpace])
44
+ max = store
45
+ end
46
+ end
47
+ puts max ? max.name : ""
48
+ end
49
+ end
@@ -1,45 +1,44 @@
1
- #
2
- # Author:: Jesse Campbell (<hikeit@gmail.com>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- require 'chef/knife'
6
- require 'chef/knife/base_vsphere_command'
7
-
8
- # Lists all known pools in the configured datacenter
9
- class Chef::Knife::VspherePoolList < Chef::Knife::BaseVsphereCommand
10
-
11
- banner "knife vsphere pool list"
12
-
13
- get_common_options
14
-
15
- def traverse_folders(folder)
16
- puts "#{ui.color("#{folder.class}", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
17
- folders = find_all_in_folder(folder, RbVmomi::VIM::ManagedObject)
18
- unless folders.nil?
19
- folders.each do |child|
20
- traverse_folders(child)
21
- end
22
- end
23
- end
24
-
25
- def find_pool_folder(folderName)
26
- dcname = get_config(:vsphere_dc)
27
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
28
- baseEntity = dc.hostFolder
29
- entityArray = folderName.split('/')
30
- entityArray.each do |entityArrItem|
31
- if entityArrItem != ''
32
- baseEntity = baseEntity.childEntity.grep(RbVmomi::VIM::ManagedObject).find { |f| f.name == entityArrItem } or
33
- abort "no such folder #{folderName} while looking for #{entityArrItem}"
34
- end
35
- end
36
- baseEntity
37
- end
38
-
39
- def run
40
- $stdout.sync = true
41
- vim = get_vim_connection
42
- baseFolder = find_pool_folder(get_config(:folder));
43
- traverse_folders(baseFolder)
44
- end
45
- end
1
+ #
2
+ # Author:: Jesse Campbell (<hikeit@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+
8
+ # Lists all known pools in the configured datacenter
9
+ class Chef::Knife::VspherePoolList < Chef::Knife::BaseVsphereCommand
10
+
11
+ banner "knife vsphere pool list"
12
+
13
+ get_common_options
14
+
15
+ def traverse_folders(folder)
16
+ puts "#{ui.color("#{folder.class}", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
17
+ folders = find_all_in_folder(folder, RbVmomi::VIM::ManagedObject)
18
+ unless folders.nil?
19
+ folders.each do |child|
20
+ traverse_folders(child)
21
+ end
22
+ end
23
+ end
24
+
25
+ def find_pool_folder(folderName)
26
+ dc = get_datacenter
27
+ baseEntity = dc.hostFolder
28
+ entityArray = folderName.split('/')
29
+ entityArray.each do |entityArrItem|
30
+ if entityArrItem != ''
31
+ baseEntity = baseEntity.childEntity.grep(RbVmomi::VIM::ManagedObject).find { |f| f.name == entityArrItem } or
32
+ abort "no such folder #{folderName} while looking for #{entityArrItem}"
33
+ end
34
+ end
35
+ baseEntity
36
+ end
37
+
38
+ def run
39
+ $stdout.sync = true
40
+ vim = get_vim_connection
41
+ baseFolder = find_pool_folder(get_config(:folder));
42
+ traverse_folders(baseFolder)
43
+ end
44
+ end
@@ -1,32 +1,32 @@
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/base_vsphere_command'
8
-
9
- # Lists all known VM templates in the configured datacenter
10
- class Chef::Knife::VsphereTemplateList < Chef::Knife::BaseVsphereCommand
11
-
12
- banner "knife vsphere template list"
13
-
14
- get_common_options
15
-
16
- def run
17
-
18
- $stdout.sync = true
19
- $stderr.sync = true
20
-
21
- vim = get_vim_connection
22
-
23
- baseFolder = find_folder(get_config(:folder));
24
-
25
- vms = find_all_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine).
26
- select { |v| !v.config.nil? && v.config.template == true }
27
-
28
- vms.each do |vm|
29
- puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
30
- end
31
- end
32
- 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/base_vsphere_command'
8
+
9
+ # Lists all known VM templates in the configured datacenter
10
+ class Chef::Knife::VsphereTemplateList < Chef::Knife::BaseVsphereCommand
11
+
12
+ banner "knife vsphere template list"
13
+
14
+ get_common_options
15
+
16
+ def run
17
+
18
+ $stdout.sync = true
19
+ $stderr.sync = true
20
+
21
+ vim = get_vim_connection
22
+
23
+ baseFolder = find_folder(get_config(:folder));
24
+
25
+ vms = find_all_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine).
26
+ select { |v| !v.config.nil? && v.config.template == true }
27
+
28
+ vms.each do |vm|
29
+ puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
30
+ end
31
+ end
32
+ end
@@ -1,38 +1,37 @@
1
- # Author: Jesse Campbell
2
- #
3
- # Permission to use, copy, modify, and/or distribute this software for
4
- # any purpose with or without fee is hereby granted, provided that the
5
- # above copyright notice and this permission notice appear in all
6
- # copies.
7
- #
8
- # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9
- # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10
- # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
11
- # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12
- # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
13
- # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
- # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- # PERFORMANCE OF THIS SOFTWARE
16
-
17
- require 'chef/knife'
18
- require 'chef/knife/base_vsphere_command'
19
-
20
- # Lists all known data stores in datacenter with sizes
21
- class Chef::Knife::VsphereVlanList < Chef::Knife::BaseVsphereCommand
22
-
23
- banner "knife vsphere vlan list"
24
-
25
- get_common_options
26
-
27
- def run
28
- $stdout.sync = true
29
-
30
- vim = get_vim_connection
31
- dcname = get_config(:vsphere_dc)
32
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
33
- dc.network.each do |network|
34
- puts "#{ui.color("VLAN", :cyan)}: #{network.name}"
35
- end
36
- end
37
- end
38
-
1
+ # Author: Jesse Campbell
2
+ #
3
+ # Permission to use, copy, modify, and/or distribute this software for
4
+ # any purpose with or without fee is hereby granted, provided that the
5
+ # above copyright notice and this permission notice appear in all
6
+ # copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9
+ # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
11
+ # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12
+ # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
13
+ # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
+ # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ # PERFORMANCE OF THIS SOFTWARE
16
+
17
+ require 'chef/knife'
18
+ require 'chef/knife/base_vsphere_command'
19
+
20
+ # Lists all known data stores in datacenter with sizes
21
+ class Chef::Knife::VsphereVlanList < Chef::Knife::BaseVsphereCommand
22
+
23
+ banner "knife vsphere vlan list"
24
+
25
+ get_common_options
26
+
27
+ def run
28
+ $stdout.sync = true
29
+
30
+ vim = get_vim_connection
31
+ dc = get_datacenter
32
+ dc.network.each do |network|
33
+ puts "#{ui.color("VLAN", :cyan)}: #{network.name}"
34
+ end
35
+ end
36
+ end
37
+