lib/kytoon/providers/libvirt/server_group.rb in kytoon-1.3.3 vs lib/kytoon/providers/libvirt/server_group.rb in kytoon-1.3.4
- old
+ new
@@ -98,22 +98,22 @@
end
def server_names
- names=[]
+ names=[]
servers.each do |server|
if block_given? then
yield server['hostname']
else
names << server['hostname']
- end
+ end
end
names
-
+
end
def cache_to_disk
sg_hash = {
@@ -295,12 +295,11 @@
raise KytoonException, "Unable to find disk path for instance."
end
def self.create_instance(group_id, inst_name, memory_gigs, original, original_xml, disk_path, create_cow, selinux_enabled, ssh_public_key, sudo)
- selinux_enabled = selinux_enabled =~ /(true|t|yes|y|1)$/i ? "true" : ""
-
+ selinux_enabled, guestfs_selinux_arg = selinux_enabled =~ /(true|t|yes|y|1)$/i ? ["true", '--selinux']: ["", '']
puts "Creating instance: #{inst_name}"
instance_memory = (KIB_PER_GIG * memory_gigs.to_f).to_i
original_disk_path = source_disk_filename(original, original_xml) #cow only
domain_name="#{group_id}_#{inst_name}"
@@ -324,11 +323,13 @@
--force \
$ORIGIN \
--preserve-data \
|| { echo "failed to virt-clone"; exit 1; }
- #{sudo} qemu-img create -f qcow2 -o backing_file=#{original_disk_path} "#{disk_path}"
+ #{sudo} qemu-img create -f qcow2 -o backing_file=#{original_disk_path} "#{disk_path}" || { \
+ echo 'Failed to create a copy-on-write image of #{original_disk_path }'; exit 1;
+ }
else
#{sudo} virt-clone --connect=qemu:///system \
--name '#{domain_name}' \
@@ -337,45 +338,42 @@
$ORIGIN \
|| { echo "failed to virt-clone"; exit 1; }
fi
-LV_ROOT=$(#{sudo} virt-filesystems -a #{disk_path} --logical-volumes | grep root)
-# If using LVM we inject the ssh key this way
-if [ -n "$LV_ROOT" ]; then
- if [ -n "#{selinux_enabled}" ]; then
- #{sudo} guestfish --selinux add #{disk_path} : \
- run : \
- mount $LV_ROOT / : \
- sh "/bin/mkdir -p /root/.ssh" : \
- write-append /root/.ssh/authorized_keys "#{ssh_public_key}\n" : \
- sh "/bin/chmod -R 700 /root/.ssh" : \
- sh "load_policy -i" : \
- sh "chcon unconfined_u:object_r:user_home_t:s0 /root/.ssh" : \
- sh "chcon system_u:object_r:ssh_home_t /root/.ssh/authorized_keys"
- else
- #{sudo} guestfish add #{disk_path} : \
- run : \
- mount $LV_ROOT / : \
- sh "/bin/mkdir -p /root/.ssh" : \
- write-append /root/.ssh/authorized_keys "#{ssh_public_key}\n" : \
- sh "/bin/chmod -R 700 /root/.ssh"
- fi
+#Copy the ssh-key
+#{sudo} guestfish -a #{disk_path} -i <<- __EOF__
+ mkdir-p /root/.ssh
+ write-append /root/.ssh/authorized_keys "#{ssh_public_key}"
+ write-append /root/.ssh/authorized_keys \"\\n\"
+ chmod 0700 /root/.ssh
+__EOF__
+
+[ $? -eq 0 ] || { echo 'Error: unable to inject keys into the image #{disk_path}'; exit 1; }
+
+#Extra magic if selinux is enabled
+if [ -n "#{selinux_enabled}" ]; then
+ #{sudo} guestfish #{guestfs_selinux_arg} -a #{disk_path} -i <<- __EOF__
+ sh 'load_policy -i'
+ sh 'chcon unconfined_u:object_r:user_home_t:s0 /root/.ssh'
+ sh 'chcon system_u:object_r:ssh_home_t /root/.ssh/authorized_keys'
+__EOF__
+ [ $? -eq 0 ] || { echo 'Error: unable to perform selinux operations on #{disk_path}'; exit 1; }
fi
#{sudo} virsh --connect=qemu:///system setmaxmem #{domain_name} #{instance_memory}
#{sudo} virsh --connect=qemu:///system start #{domain_name}
#{sudo} virsh --connect=qemu:///system setmem #{domain_name} #{instance_memory}
}
retval=$?
- if not retval.success?
+ if not retval.success?
puts out
raise KytoonException, "Failed to create instance #{inst_name}."
end
- # lookup server IP here...
+ # lookup server IP here...
mac_addr = nil
network_name = nil
dom_xml = %x{#{sudo} virsh --connect=qemu:///system dumpxml #{domain_name}}
dom = REXML::Document.new(dom_xml)
REXML::XPath.each(dom, "//interface/mac") do |interface_xml|
@@ -417,10 +415,10 @@
#{sudo} rm -f "#{disk_path}"
fi
}
puts out
retval=$?
- if not retval.success?
+ if not retval.success?
puts out
raise KytoonException, "Failed to cleanup instances."
end
end