Class: VagrantPlugins::XenServer::Action::CreateTemplate

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateTemplate

Returns a new instance of CreateTemplate.



12
13
14
15
# File 'lib/vagrant-xenserver/action/create_template.rb', line 12

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  if env[:template].nil?

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

    # No template - that means it wasn't a downloaded XVA.
    # Let's create a VM and attach the uploaded VDI to it.
    # First see if we've done that already:
    
    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
      }

      if template.nil?
        vdi_ref = env[:box_vdi]

        oim = env[:xc].VM.get_by_name_label("Other install media")[0]
      
        template_name = "#{box_name}.#{box_version}"
      
        template_ref = env[:xc].VM.clone(oim,template_name)
      
        vbd_record = {
          'VM' => template_ref,
          'VDI' => env[:box_vdi],
          'userdevice' => '0',
          'bootable' => true,
          'mode' => 'RW',
          'type' => 'Disk',
          'unpluggable' => false,
          'empty' => false,
          'other_config' => {},
          'qos_algorithm_type' => '',
          'qos_algorithm_params' => {}
        }
      
        vbd_res = env[:xc].VBD.create(vbd_record)
      
        @logger.info("vbd_res=" + vbd_res.to_s)

        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)
      
        if env[:machine].provider_config.pv
          env[:xc].VM.set_HVM_boot_policy(template_ref,"")
          env[:xc].VM.set_PV_bootloader(template_ref,"pygrub")
        end

        mem = ((env[:machine].provider_config.memory) * (1024*1024)).to_s
        env[:xc].VM.set_memory_limits(template_ref,mem,mem,mem,mem)

        env[:template] = template_ref
      
      else
        @logger.info("Found pre-existing template for this box")
        (template_ref, template_rec) = template
        env[:template] = template_ref
      end
    end
  end
    
  @app.call env
end