def launch_instance(image, hardware_profile, opts={})
raise "Image object must be not nil" unless image
raise "HardwareProfile object must be not nil" unless hardware_profile
opts[:name] ||= "i-#{Time.now.to_i}"
mac_addr = @ip_agent.find_free_mac
ip_addr = @ip_agent.find_ip_by_mac(mac_addr) if mac_addr && !mac_addr.empty?
libvirt_xml = "+VM_XML=\"<domain type='kvm'>
<name>{NAME}</name>
<memory>#{hardware_profile.memory.value.to_i * 1024}</memory>
<vcpu>#{hardware_profile.cpu.value}</vcpu>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
<smbios mode='sysinfo'/>
</os>
<sysinfo type='smbios'>
<system>
<entry name='manufacturer'>#{opts[:config_server_address]}</entry>
<entry name='product'>#{opts[:uuid]}</entry>
<entry name='serial'>#{opts[:otp]}</entry>
</system>
</sysinfo>
<features>
<acpi/><apic/><pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<disk type='file' device='disk'>
<source file='{DISK}'/>
<target dev='vda' bus='virtio'/>
<driver name='qemu' type='qcow2'/>
</disk>
<interface type='bridge'>
#{"<mac address='" + mac_addr + "'/>" if mac_addr && !mac_addr.empty?}
<source bridge='#{@config[:default_bridge]}'/>
</interface>
<graphics type='vnc' port='#{@config[:vnc_listen_port]}' autoport='yes' keymap='en-us' listen='#{@config[:vnc_listen_ip]}'/>
</devices>
</domain>\"".gsub(/(\s{2,})/, ' ').gsub(/\>\s\</, '><')
pipe = IO.popen("#{CONDOR_SUBMIT_CMD} 2>&1", "w+")
pipe.puts "universe=vm"
pipe.puts "vm_type=kvm"
pipe.puts "vm_memory=#{hardware_profile.memory.value}"
pipe.puts "request_cpus=#{hardware_profile.cpu.value}"
pipe.puts "vm_disk=#{image.description}:null:null"
pipe.puts "executable=#{image.description}"
pipe.puts "vm_macaddr=#{mac_addr}"
pipe.puts "+vm_ipaddr=\"#{ip_addr}\""
pipe.puts '+HookKeyword="CLOUD"'
pipe.puts "+Cmd=\"#{opts[:name]}\""
pipe.puts "+cloud_image=\"#{File.basename(image.description)}\""
pipe.puts libvirt_xml
pipe.puts "queue"
pipe.puts ""
pipe.close_write
out = pipe.read
pipe.close
if $? != 0
raise "Error starting VM in condor_submit: #{out}"
end
bare_xml = Nokogiri::XML(`#{CONDOR_Q_CMD} -xml`)
parse_condor_q_output(bare_xml, :name => opts[:name])
end