Class: VagrantPlugins::XenServer::Action::DownloadXVA

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ DownloadXVA

Returns a new instance of DownloadXVA.



14
15
16
17
# File 'lib/vagrant-xenserver/action/download_xva.rb', line 14

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

Instance Method Details

#call(env) ⇒ Object



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
97
# File 'lib/vagrant-xenserver/action/download_xva.rb', line 19

def call(env)
  xva_url = env[:machine].provider_config.xva_url

  box_name = env[:machine].box.name.to_s
  box_version = env[:machine].box.version.to_s

  @logger.info("xva_url="+xva_url.to_s)
  # Check whether we've already downloaded a VM from this URL
  # When we do, we set an other_config key 'xva_url', so we
  # can just scan through the VMs looking for it.

  template = nil

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

    @logger.info("template="+template.to_s)

    if template.nil? && (not xva_url.nil?)
      # No template, let's download it.
      pool=env[:xc].pool.get_all
      default_sr=env[:xc].pool.get_default_SR(pool[0])

      env[:ui].output("Downloading XVA. This may take some time. Source URL: "+xva_url)
      task = env[:xc].Async.VM.import(xva_url, default_sr, false, false)

      begin
        sleep(2.0)
        task_status = env[:xc].task.get_status(task)
        task_progress = env[:xc].task.get_progress(task) * 100.0
        output = "Progress: #{task_progress.round(0)}%"
        env[:ui].clear_line
        env[:ui].detail(output, new_line: false)
      end while task_status == "pending"

      env[:ui].clear_line

      if task_status != "success"
     # Task failed - let's find out why:
       error_list = env[:xc].task.get_error_info(task)
        MyUtil::Exnhandler.handle("Async.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

      # 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,"xva_url",xva_url)
      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
    else
      (template_ref, template_rec) = template
      env[:template] = template_ref
    end
  end

  @app.call(env)
end