knife-vsphere 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,167 +1,169 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # Contributor:: Jesse Campbell (<hikeit@gmail.com>)
4
- # License:: Apache License, Version 2.0
5
- #
6
-
7
- require 'chef/knife'
8
- require 'rbvmomi'
9
-
10
- # Base class for vsphere knife commands
11
- class Chef
12
- class Knife
13
- class BaseVsphereCommand < Knife
14
-
15
- deps do
16
- require 'chef/knife/bootstrap'
17
- Chef::Knife::Bootstrap.load_deps
18
- require 'fog'
19
- require 'socket'
20
- require 'net/ssh/multi'
21
- require 'readline'
22
- require 'chef/json_compat'
23
- end
24
-
25
-
26
- def self.get_common_options
27
-
28
- option :vsphere_user,
29
- :short => "-u USERNAME",
30
- :long => "--user USERNAME",
31
- :description => "The username for the host"
32
-
33
- option :vsphere_pass,
34
- :short => "-p PASSWORD",
35
- :long => "--password PASSWORD",
36
- :description => "The password for the host"
37
-
38
- option :datacenter,
39
- :short => "-d DATACENTER",
40
- :long => "--datacenter DATACENTER",
41
- :description => "The Datacenter to create the VM in"
42
-
43
- option :path,
44
- :long => "--path SOAP_PATH",
45
- :description => "The SOAP endpoint path",
46
- :proc => Proc.new { |p| Chef::Config[:knife][:path] = p },
47
- :default => "/sdk"
48
-
49
- option :port,
50
- :long => "--port PORT",
51
- :description => "The VI SDK port number to use",
52
- :proc => Proc.new { |p| Chef::Config[:knife][:port] = p },
53
- :default => 443
54
-
55
- option :use_ssl,
56
- :long => "--ssl USE_SSL",
57
- :description => "Whether to use SSL connection",
58
- :default => true
59
-
60
- option :insecure,
61
- :short => "-i USE_INSECURE_SSL",
62
- :long => "--insecure USE_INSECURE_SSL",
63
- :description => "Determines whether SSL certificate verification is skipped",
64
- :default => true
65
-
66
- option :folder,
67
- :short => "-f FOLDER",
68
- :long => "--folder FOLDER",
69
- :description => "The folder to get VMs from",
70
- :default => ''
71
-
72
- end
73
-
74
- def locate_config_value(key)
75
- key = key.to_sym
76
- Chef::Config[:knife][key] || config[key]
77
- end
78
-
79
- def get_vim_connection
80
-
81
- conn_opts = {
82
- :host => locate_config_value(:vsphere_host),
83
- :path => config[:path],
84
- :port => config[:port],
85
- :use_ssl => config[:ssl],
86
- :user => locate_config_value(:vsphere_user),
87
- :password => locate_config_value(:vsphere_pass),
88
- :insecure => config[:insecure]
89
- }
90
-
91
- # opt :insecure, "don't verify ssl certificate", :short => 'k', :default => (ENV['RBVMOMI_INSECURE'] == '1')
92
- # opt :debug, "Log SOAP messages", :short => 'd', :default => (ENV['RBVMOMI_DEBUG'] || false)
93
-
94
- vim = RbVmomi::VIM.connect conn_opts
95
- config[:vim] = vim
96
- return vim
97
- end
98
-
99
- def find_folder(folderName)
100
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
101
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
102
- baseEntity = dc.vmFolder
103
- entityArray = folderName.split('/')
104
- entityArray.each do |entityArrItem|
105
- if entityArrItem != ''
106
- baseEntity = baseEntity.childEntity.grep(RbVmomi::VIM::Folder).find { |f| f.name == entityArrItem } or
107
- abort "no such folder #{folderName} while looking for #{entityArrItem}"
108
- end
109
- end
110
- baseEntity
111
- end
112
-
113
- def find_network(networkName)
114
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
115
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
116
- baseEntity = dc.network
117
- baseEntity.find { |f| f.name == networkName } or abort "no such network #{networkName}"
118
- end
119
-
120
- def find_pool(poolName)
121
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
122
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
123
- baseEntity = dc.hostFolder
124
- entityArray = poolName.split('/')
125
- entityArray.each do |entityArrItem|
126
- if entityArrItem != ''
127
- if baseEntity.is_a? RbVmomi::VIM::Folder
128
- baseEntity = baseEntity.childEntity.find { |f| f.name == entityArrItem } or
129
- abort "no such pool #{poolName} while looking for #{entityArrItem}"
130
- elsif baseEntity.is_a? RbVmomi::VIM::ClusterComputeResource
131
- baseEntity = baseEntity.resourcePool.resourcePool.find { |f| f.name == entityArrItem } or
132
- abort "no such pool #{poolName} while looking for #{entityArrItem}"
133
- elsif baseEntity.is_a? RbVmomi::VIM::ResourcePool
134
- baseEntity = baseEntity.resourcePool.find { |f| f.name == entityArrItem } or
135
- abort "no such pool #{poolName} while looking for #{entityArrItem}"
136
- else
137
- abort "Unexpected Object type encountered #{baseEntity.type} while finding resourcePool"
138
- end
139
- end
140
- end
141
- baseEntity
142
- end
143
-
144
- def find_datastore(dsName)
145
- dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
146
- dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
147
- baseEntity = dc.datastore
148
- baseEntity.find { |f| f.info.name == dsName } or abort "no such datastore #{dsName}"
149
- end
150
-
151
-
152
- def find_all_in_folder(folder, type)
153
- folder.childEntity.grep(type)
154
- end
155
-
156
- def find_in_folder(folder, type, name)
157
- folder.childEntity.grep(type).find { |o| o.name == name }
158
- end
159
-
160
- def fatal_exit(msg)
161
- ui.fatal(msg)
162
- exit 1
163
- end
164
-
165
- end
166
- end
167
- end
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # Contributor:: Jesse Campbell (<hikeit@gmail.com>)
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+
7
+ require 'chef/knife'
8
+ require 'rbvmomi'
9
+
10
+ # Base class for vsphere knife commands
11
+ class Chef
12
+ class Knife
13
+ class BaseVsphereCommand < Knife
14
+
15
+ deps do
16
+ require 'chef/knife/bootstrap'
17
+ Chef::Knife::Bootstrap.load_deps
18
+ require 'fog'
19
+ require 'socket'
20
+ require 'net/ssh/multi'
21
+ require 'readline'
22
+ require 'chef/json_compat'
23
+ end
24
+
25
+
26
+ def self.get_common_options
27
+
28
+ option :vsphere_user,
29
+ :short => "-u USERNAME",
30
+ :long => "--user USERNAME",
31
+ :description => "The username for the host"
32
+
33
+ option :vsphere_pass,
34
+ :short => "-p PASSWORD",
35
+ :long => "--password PASSWORD",
36
+ :description => "The password for the host"
37
+
38
+ option :datacenter,
39
+ :short => "-d DATACENTER",
40
+ :long => "--datacenter DATACENTER",
41
+ :description => "The Datacenter to create the VM in"
42
+
43
+ option :path,
44
+ :long => "--path SOAP_PATH",
45
+ :description => "The SOAP endpoint path",
46
+ :proc => Proc.new { |p| Chef::Config[:knife][:path] = p },
47
+ :default => "/sdk"
48
+
49
+ option :port,
50
+ :long => "--port PORT",
51
+ :description => "The VI SDK port number to use",
52
+ :proc => Proc.new { |p| Chef::Config[:knife][:port] = p },
53
+ :default => 443
54
+
55
+ option :use_ssl,
56
+ :long => "--ssl USE_SSL",
57
+ :description => "Whether to use SSL connection",
58
+ :default => true
59
+
60
+ option :insecure,
61
+ :short => "-i USE_INSECURE_SSL",
62
+ :long => "--insecure USE_INSECURE_SSL",
63
+ :description => "Determines whether SSL certificate verification is skipped",
64
+ :default => true
65
+
66
+ option :folder,
67
+ :short => "-f FOLDER",
68
+ :long => "--folder FOLDER",
69
+ :description => "The folder to get VMs from",
70
+ :default => ''
71
+
72
+ end
73
+
74
+ def locate_config_value(key)
75
+ key = key.to_sym
76
+ Chef::Config[:knife][key] || config[key]
77
+ end
78
+
79
+ def get_vim_connection
80
+
81
+ conn_opts = {
82
+ :host => locate_config_value(:vsphere_host),
83
+ :path => config[:path],
84
+ :port => config[:port],
85
+ :use_ssl => config[:ssl],
86
+ :user => locate_config_value(:vsphere_user),
87
+ :password => locate_config_value(:vsphere_pass),
88
+ :insecure => config[:insecure]
89
+ }
90
+
91
+ # opt :insecure, "don't verify ssl certificate", :short => 'k', :default => (ENV['RBVMOMI_INSECURE'] == '1')
92
+ # opt :debug, "Log SOAP messages", :short => 'd', :default => (ENV['RBVMOMI_DEBUG'] || false)
93
+
94
+ vim = RbVmomi::VIM.connect conn_opts
95
+ config[:vim] = vim
96
+ return vim
97
+ end
98
+
99
+ def find_folder(folderName)
100
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
101
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
102
+ baseEntity = dc.vmFolder
103
+ entityArray = folderName.split('/')
104
+ entityArray.each do |entityArrItem|
105
+ if entityArrItem != ''
106
+ baseEntity = baseEntity.childEntity.grep(RbVmomi::VIM::Folder).find { |f| f.name == entityArrItem } or
107
+ abort "no such folder #{folderName} while looking for #{entityArrItem}"
108
+ end
109
+ end
110
+ baseEntity
111
+ end
112
+
113
+ def find_network(networkName)
114
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
115
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
116
+ baseEntity = dc.network
117
+ baseEntity.find { |f| f.name == networkName } or abort "no such network #{networkName}"
118
+ end
119
+
120
+ def find_pool(poolName)
121
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
122
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
123
+ baseEntity = dc.hostFolder
124
+ entityArray = poolName.split('/')
125
+ entityArray.each do |entityArrItem|
126
+ if entityArrItem != ''
127
+ if baseEntity.is_a? RbVmomi::VIM::Folder
128
+ baseEntity = baseEntity.childEntity.find { |f| f.name == entityArrItem } or
129
+ abort "no such pool #{poolName} while looking for #{entityArrItem}"
130
+ elsif baseEntity.is_a? RbVmomi::VIM::ClusterComputeResource
131
+ baseEntity = baseEntity.resourcePool.resourcePool.find { |f| f.name == entityArrItem } or
132
+ abort "no such pool #{poolName} while looking for #{entityArrItem}"
133
+ elsif baseEntity.is_a? RbVmomi::VIM::ResourcePool
134
+ baseEntity = baseEntity.resourcePool.find { |f| f.name == entityArrItem } or
135
+ abort "no such pool #{poolName} while looking for #{entityArrItem}"
136
+ else
137
+ abort "Unexpected Object type encountered #{baseEntity.type} while finding resourcePool"
138
+ end
139
+ end
140
+ end
141
+
142
+ baseEntity = baseEntity.resourcePool if not baseEntity.is_a?(RbVmomi::VIM::ResourcePool) and baseEntity.respond_to?(:resourcePool)
143
+ baseEntity
144
+ end
145
+
146
+ def find_datastore(dsName)
147
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
148
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
149
+ baseEntity = dc.datastore
150
+ baseEntity.find { |f| f.info.name == dsName } or abort "no such datastore #{dsName}"
151
+ end
152
+
153
+
154
+ def find_all_in_folder(folder, type)
155
+ folder.childEntity.grep(type)
156
+ end
157
+
158
+ def find_in_folder(folder, type, name)
159
+ folder.childEntity.grep(type).find { |o| o.name == name }
160
+ end
161
+
162
+ def fatal_exit(msg)
163
+ ui.fatal(msg)
164
+ exit 1
165
+ end
166
+
167
+ end
168
+ end
169
+ 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/BaseVsphereCommand'
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/BaseVsphereCommand'
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
+
@@ -0,0 +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/BaseVsphereCommand'
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
+ def run
46
+ $stdout.sync = true
47
+
48
+ vim = get_vim_connection
49
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
50
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
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
+