Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* 'Safer' template creation via API using imported VHD

  • Loading branch information...
commit d0050c2b21ca66e4609f9107baf69a26061c12d2 1 parent aad38c6
@rubiojr rubiojr authored
View
1  knife-xenserver.gemspec
@@ -31,5 +31,6 @@ Gem::Specification.new do |s|
s.add_dependency('colored')
s.add_dependency('ruby-hmac')
s.add_dependency('alchemist')
+ s.add_dependency('uuidtools')
s.require_paths = ["lib"]
end
View
42 lib/chef/knife/xenserver_template_create.rb
@@ -18,6 +18,7 @@
require 'chef/knife/xenserver_base'
require 'net/scp'
+require 'uuidtools'
class Chef
class Knife
@@ -105,12 +106,33 @@ def run
# We will create the VDI in this SR
sr = connection.storage_repositories.find { |sr| sr.name == config[:storage_repository] }
+ # Upload and replace the VDI with our template
+ uuid = UUIDTools::UUID.random_create.to_s
+ dest = "/var/run/sr-mount/#{sr.uuid}/#{uuid}.vhd"
+ Net::SSH.start(host, user, :password => password) do |ssh|
+ puts "Uploading file #{File.basename(source).yellow}..."
+ puts "Destination SR #{sr.name.yellow}"
+ ssh.scp.upload!(source, dest) do |ch, name, sent, total|
+ p = (sent.to_f * 100 / total.to_f).to_i.to_s
+ print "\rProgress: #{p.yellow.bold}% completed"
+ end
+ end
+ sr.scan
# Create a ~8GB VDI in storage repository 'Local Storage'
- vdi = connection.vdis.create :name => "#{vm_name}-disk1",
- :storage_repository => sr,
- :description => "#{vm_name}-disk1",
- :virtual_size => '8589934592' # ~8GB in bytes
+ #vdi = connection.vdis.create :name => "#{vm_name}-disk1",
+ # :storage_repository => sr,
+ # :description => "#{vm_name}-disk1",
+ # :virtual_size => '8589934592' # ~8GB in bytes
+
+ vdi = nil
+ sr.vdis.each do |v|
+ if v.uuid == uuid
+ v.set_attribute 'name_label', "#{vm_name}-template"
+ vdi = v
+ break
+ end
+ end
# Create the VM but do not start/provision it
if config[:hvm]
@@ -142,18 +164,8 @@ def run
end
# Add the required VBD to the VM
connection.vbds.create :server => vm, :vdi => vdi
+ puts "Done."
- # Upload and replace the VDI with our template
- dest = "/var/run/sr-mount/#{sr.uuid}/#{vdi.uuid}.vhd"
- Net::SSH.start(host, user, :password => password) do |ssh|
- puts "Uploading file #{File.basename(source).yellow}..."
- puts "Destination SR #{sr.name.yellow}"
- ssh.scp.upload!(source, dest) do |ch, name, sent, total|
- p = (sent.to_f * 100 / total.to_f).to_i.to_s
- print "\rProgress: #{p.yellow.bold}% completed"
- end
- end
- puts "\ndone."
end
def create_nics(networks, macs, vm)
View
2  lib/chef/knife/xenserver_vm_create.rb
@@ -222,7 +222,7 @@ def run
end
print "\n#{ui.color("Waiting for sshd... ", :magenta)}"
vm.guest_metrics.networks.each do |k,v|
- print "\n#{ui.color("Trying to SSH to #{v}... ", :yellow)}"
+ print "\nTrying to #{'SSH'.yellow} to #{v.yellow}... "
print(".") until tcp_test_ssh(v) do
sleep @initial_sleep_delay ||= 10; puts(" done")
@ssh_ip = v
View
2  lib/chef/knife/xenserver_vm_delete.rb
@@ -41,7 +41,7 @@ def run
end
vm.destroy
deleted << vm_name
- ui.info("Deleted virtual machine #{vm.name.yellow} [uuid: #{vm.uuid}]")
+ ui.info("#{'Deleted'.yellow} virtual machine #{vm.name.yellow} [uuid: #{vm.uuid}]")
end
end
end
View
1  vendor/fog/lib/fog/xenserver/compute.rb
@@ -58,6 +58,7 @@ class XenServer < Fog::Service
request :set_attribute
request :reboot_server
request :provision_server
+ request :scan_sr
class Real
View
2  vendor/fog/lib/fog/xenserver/models/compute/server.rb
@@ -78,7 +78,7 @@ def destroy
end
def set_attribute(name, val)
- data = connection.set_attribute( reference, name, val )
+ data = connection.set_attribute( 'VM', reference, name, val )
# Do not reload automatically for performance reasons
# We can set multiple attributes at the same time and
# then reload manually
View
5 vendor/fog/lib/fog/xenserver/models/compute/storage_repository.rb
@@ -33,6 +33,11 @@ def pbds
__pbds.collect { |pbd| connection.pbds.get pbd }
end
+ def scan
+ connection.scan_sr reference
+ reload
+ end
+
end
end
View
4 vendor/fog/lib/fog/xenserver/models/compute/vdi.rb
@@ -52,6 +52,10 @@ def initialize(attributes = {})
self.other_config ||= {} unless attributes[:other_config]
super
end
+
+ def set_attribute(name, val)
+ data = connection.set_attribute( 'VDI', reference, name, val )
+ end
def snapshot_of
connection.vdis.get __sr
View
22 vendor/fog/lib/fog/xenserver/requests/compute/scan_sr.rb
@@ -0,0 +1,22 @@
+module Fog
+ module Compute
+ class XenServer
+
+ class Real
+
+ def scan_sr( ref, extra_args = {})
+ @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'SR.scan'}, ref)
+ end
+
+ end
+
+ class Mock
+
+ def scan_sr(ref, extra_args = {})
+ Fog::Mock.not_implemented
+ end
+
+ end
+ end
+ end
+end
View
6 vendor/fog/lib/fog/xenserver/requests/compute/set_attribute.rb
@@ -6,15 +6,15 @@ class Real
require 'fog/xenserver/parser'
- def set_attribute( ref, attr_name, value )
- @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "VM.set_#{attr_name.gsub('-','_')}"}, ref, value)
+ def set_attribute( klass, ref, attr_name, value )
+ @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "#{klass}.set_#{attr_name.gsub('-','_')}"}, ref, value)
end
end
class Mock
- def set_attribute( ref, attr_name, value )
+ def set_attribute( klass, ref, attr_name, value )
Fog::Mock.not_implemented
end

0 comments on commit d0050c2

Please sign in to comment.
Something went wrong with that request. Please try again.