knife-xapi 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,10 @@
19
19
  # See the License for the specific language governing permissions and
20
20
  # limitations under the License.
21
21
 
22
- XAPI_TEMP_REGEX ||= /^CentOS 5.*\(64-bit\)/
22
+ # ruby 1.8.7 doesn't like ||= with Constants
23
+ unless defined?(XAPI_TEMP_REGEX)
24
+ XAPI_TEMP_REGEX = /^CentOS 5.*\(64-bit\)/
25
+ end
23
26
 
24
27
  require 'chef/knife'
25
28
  require 'units/standard'
@@ -27,9 +30,7 @@ require 'units/standard'
27
30
  class Chef::Knife
28
31
  module XapiBase
29
32
 
30
-
31
33
  def self.included(includer)
32
-
33
34
  includer.class_eval do
34
35
  deps do
35
36
  require 'xenapi'
@@ -183,6 +184,7 @@ class Chef::Knife
183
184
  end
184
185
 
185
186
  # convert 1g/1m/1t to bytes
187
+ # rounds to whole numbers
186
188
  def input_to_bytes(size)
187
189
  case size
188
190
  when /g|gb/i
@@ -208,12 +210,41 @@ class Chef::Knife
208
210
  "read_only" => false,
209
211
  "other_config" => {},
210
212
  }
211
-
212
- vdi_ref = xapi.VDI.create( vdi_record )
213
- ui.msg "VDI: #{h.color( name, :cyan )} #{h.color( vdi_ref, :cyan )} created"
214
- vdi_ref
213
+
214
+ # Async create the VDI
215
+ task = xapi.Async.VDI.create(vdi_record)
216
+ ui.msg "waiting for VDI Create"
217
+ vdi_ref = get_task_ref(task)
215
218
  end
216
219
 
220
+
221
+ # sit and wait for taks to exit pending state
222
+ def wait_on_task(task)
223
+ while xapi.task.get_status(task) == "pending"
224
+ progress = xapi.task.get_progress(task)
225
+ sleep 1
226
+ end
227
+ end
228
+
229
+ # return the opaque ref of the task that was run by a task record if it succeded.
230
+ # else it returns nil
231
+ def get_task_ref(task)
232
+ wait_on_task(task)
233
+ case xapi.task.get_status(task)
234
+ when "success"
235
+ # xapi task record returns result as <value>OpaqueRef:....</value>
236
+ # we want the ref. this way it will work if they fix it to return jsut the ref
237
+ ref = xapi.task.get_result(task).match(/OpaqueRef:[^<]+/).to_s
238
+ #cleanup our task
239
+ xapi.task.destroy(task)
240
+ return ref
241
+ else
242
+ ui.msg( "#{h.color 'ERROR:', :red } Task returned: #{xapi.task.get_result(task)}" )
243
+ return nil
244
+ end
245
+ end
246
+
247
+
217
248
  # create vbd and return a ref
218
249
  def create_vbd(vm_ref, vdi_ref, position)
219
250
  vbd_record = {
@@ -230,9 +261,9 @@ class Chef::Knife
230
261
  "type" => "Disk"
231
262
  }
232
263
 
233
- vbd_ref = xapi.VBD.create(vbd_record)
234
- ui.msg "VBD: #{h.color vbd_ref, :cyan } created"
235
- vbd_ref
264
+ task = xapi.Async.VBD.create(vbd_record)
265
+ ui.msg "Waiting for VBD create"
266
+ vbd_ref = get_task_ref(task)
236
267
  end
237
268
 
238
269
  end
@@ -105,9 +105,8 @@ class Chef
105
105
  xapi.VM.set_other_config(vm_ref, { "install-repository" => repo } )
106
106
 
107
107
  cpus = Chef::Config[:knife][:xapi_cpus] || 2
108
- ui.msg "Setting up CPUS #{ h.color cpus, :cyan }"
109
- xapi.VM.set_VCPUs_max( vm_ref, cpus.to_s )
110
- xapi.VM.set_VCPUs_at_startup( vm_ref, cpus.to_s )
108
+ xapi.VM.set_VCPUs_max( vm_ref, cpus )
109
+ xapi.VM.set_VCPUs_at_startup( vm_ref, cpus )
111
110
 
112
111
  memory_size = input_to_bytes( Chef::Config[:knife][:xapi_mem] || "1g" ).to_s
113
112
  ui.msg "Mem size: #{ h.color( memory_size, :cyan)}"
@@ -144,19 +143,27 @@ class Chef
144
143
  # Create the VDI
145
144
  disk_size = Chef::Config[:knife][:xapi_disk_size] || "8g"
146
145
  vdi_ref = create_vdi("#{server_name}-root", sr_ref, disk_size )
146
+ # if vdi_ref is nill we need to bail/cleanup
147
+ cleanup(vm_ref) unless vdi_ref
148
+ ui.msg( "#{ h.color "OK", :green}" )
147
149
 
148
150
  # Attach the VDI to the VM
149
151
  vbd_ref = create_vbd(vm_ref, vdi_ref, 0)
150
-
152
+ cleanup(vm_ref) unless vbd_ref
153
+ ui.msg( "#{ h.color "OK", :green}" )
154
+
151
155
  ui.msg "Provisioning new Guest: #{h.color(vm_ref, :bold, :cyan )}"
152
156
  provisioned = xapi.VM.provision(vm_ref)
153
157
 
154
158
  ui.msg "Starting new Guest: #{h.color( provisioned, :cyan)} "
155
- xapi.Async.VM.start(vm_ref, false, true)
159
+
160
+ task = xapi.Async.VM.start(vm_ref, false, true)
161
+ wait_on_task(task)
162
+ ui.msg( "#{ h.color "Done!", :green}" )
156
163
 
157
164
  rescue Exception => e
158
165
  ui.msg "#{h.color 'ERROR:'} #{h.color( e.message, :red )}"
159
- ui.error "#{h.color( e.backtrace, :yellow)}"
166
+ ui.msg "#{h.color( e.backtrace, :yellow)}"
160
167
 
161
168
  cleanup(vm_ref)
162
169
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeXenserver
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-xapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000 Z
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef