Class: VagrantPlugins::XenServer::Action::UploadXVA

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-xenserver/action/upload_xva.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ UploadXVA

Returns a new instance of UploadXVA.



10
11
12
13
# File 'lib/vagrant-xenserver/action/upload_xva.rb', line 10

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("vagrant::xenserver::actions::upload_xva")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vagrant-xenserver/action/upload_xva.rb', line 15

def call(env)
  box_name = env[:machine].box.name.to_s
  box_version = env[:machine].box.version.to_s

  Action.getlock.synchronize do
    templates = env[:xc].VM.get_all_records_where("field \"is_a_template\"=\"true\"")
    template = templates.detect { |vm,vmr|
      vmr["other_config"]["box_name"] == box_name &&
         vmr["other_config"]["box_version"] == box_version
    }

    box_xva_file = env[:machine].box.directory.join('box.xva').to_s

    if File.exist?(box_xva_file) && template.nil?
      #box_image_file = env[:machine].box.directory.join('export.xva').to_s
      hostname = env[:machine].provider_config.xs_host
      session = env[:session]

      @logger.info("box name=" + env[:machine].box.name.to_s)
      @logger.info("box version=" + env[:machine].box.version.to_s)

      # Create a task to so we can get the result of the upload
      task = env[:xc].task.create("vagrant-xva-upload",
                                  "Task to track progress of the XVA upload from vagrant")

      url = "https://#{hostname}/import?session_id=#{env[:xc].xenapi_session}&task_id=#{task}"

      uploader_options = {}
      uploader_options[:ui] = env[:ui]
      uploader_options[:insecure] = true

      uploader = MyUtil::Uploader.new(box_xva_file, url, uploader_options)

      begin
        uploader.upload!
      rescue
        env[:xc].task.cancel(task)
      end

      task_status = ""

      begin
        sleep(0.2)
        task_status = env[:xc].task.get_status(task)
      end while task_status == "pending"

      if task_status != "success"
     # Task failed - let's find out why:
       error_list = env[:xc].task.get_error_info(task)
        MyUtil::Exnhandler.handle("VM.import", error_list)
      end

      task_result = env[:xc].task.get_result(task)

      doc = REXML::Document.new(task_result)

      @logger.debug("task_result=\"#{task_result}\"")
      template_ref = doc.elements['value/array/data/value'].text

      @logger.info("template_ref=" + template_ref)

      # Make sure it's really a template, and add the xva_url to other_config:
      env[:xc].VM.set_is_a_template(template_ref,true)
      env[:xc].VM.add_to_other_config(template_ref,"box_name",box_name)
      env[:xc].VM.add_to_other_config(template_ref,"box_version",box_version)   

      # Hackity hack: HVM booting guests don't need to set the bootable flag
      # on their VBDs, but PV do. Let's set bootable=true on VBD device=0
      # just in case.

      vbds = env[:xc].VM.get_VBDs(template_ref)
      vbds.each { |vbd|
        if env[:xc].VBD.get_userdevice(vbd) == "0"
          env[:xc].VBD.set_bootable(vbd, true)
        end
      }
      env[:template] = template_ref
    end
  end

  @app.call(env)
end