function: 1. check host is the capability of executing tasks? - check libvirt - check qemu-kvm 2. request boot.libvirt and load files based on response 3. generate a domain.xml and start a vm used ruby-libvirt
Signed-off-by: Xiao Shenwei xiaoshenwei96@163.com --- providers/libvirt/run.rb | 68 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-)
diff --git a/providers/libvirt/run.rb b/providers/libvirt/run.rb index a500c31..2883197 100755 --- a/providers/libvirt/run.rb +++ b/providers/libvirt/run.rb @@ -9,6 +9,8 @@ require_relative "#{ENV['CCI_SRC']}/lib/log" require_relative "#{ENV['CCI_SRC']}/lib/sched_client" require_relative "#{ENV['CCI_SRC']}/providers/lib/context" require_relative "#{ENV['CCI_SRC']}/providers/lib/resource" +require_relative "#{ENV['CCI_SRC']}/providers/lib/domain" +require_relative "#{ENV['CCI_SRC']}/providers/lib/libvirt"
def create_logger(hostname) filename = "#{hostname}.log" @@ -48,17 +50,75 @@ def request_job(context, sched_client, logger) return response end
+def parse_response(response, context, resource) + context.merge!(response) + resource.parse_response(response) + context.merge!(resource.info) +end + +def generate_domain(context, logger) + domain = Domain.new(context, logger) + xml_path = '' + begin + domain.generate + xml_path = domain.save_to('domain.xml') + rescue StandardError => e + logger.error(e) + logger.error('please check your xml file format') + ensure + upload_log(context) + end + return xml_path +end + +def upload_log(context, upload_qemu_log: false) + id = context.info['job_id'] + host = context.info['LKP_SERVER'] + port = context.info['RESULT_WEBDAV_PORT'] + result_root = context.info['result_root'] + result_url = "http://#%7Bhost%7D:#%7Bport%7D#%7Bresult_root%7D" + log_name = "#{context.info['hostname']}.log" + system "curl -sSf -T #{log_name} #{result_url}/ --cookie 'JOBID= #{id}'" + return unless upload_qemu_log + + qemu_log_name = "/var/log/libvirt/qemu/#{id}.log" + return unless FileTest.exist?(qemu_log_name) + + system "curl -sSf -T #{qemu_log_name} #{result_url}/ --cookie 'JOBID= #{id}'" +end + +def start(context, logger, libvirt) + xml_path = generate_domain(context, logger) + return if xml_path.empty? + + begin + libvirt.define(xml_path) + libvirt.create + libvirt.wait + rescue StandardError => e + logger.error(e) + ensure + upload_log(context, upload_qemu_log: true) + end +end + +def clean(context, sched_client, libvirt) + sched_client.delete_mac2host(context.info['mac']) + sched_client.delete_host2queues(context.info['hostname']) + libvirt.close +end + def main(hostname, queues) logger = create_logger(hostname) mac = compute_mac hostname context = Context.new(mac, hostname, queues) + libvirt = LibvirtConnect.new resource = Resource.new(hostname, logger) sched_client = SchedClient.new response = request_job(context, sched_client, logger) return if response.nil?
- resource.parse_response(response) - context.merge!(resource.info) - sched_client.delete_mac2host(mac) - sched_client.delete_host2queues(hostname) + parse_response(response, context, resource) + start(context, logger, libvirt) + clean(context, sched_client, libvirt) end