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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/vagrant-xenserver/action/upload_vhd.rb', line 37
def call(env)
if env[:machine].provider_config.xva_url.nil?
box_vhd_file = env[:machine].box.directory.join('box.vhd').to_s
if File.exist?(box_vhd_file)
hostname = env[:machine].provider_config.xs_host
session = env[:xc].xenapi_session
@logger.info("box name=" + env[:machine].box.name.to_s)
@logger.info("box version=" + env[:machine].box.version.to_s)
md5cmd = 'md5sum'
if `uname` =~ /^Darwin/
md5cmd = 'md5'
end
md5=`dd if=#{box_vhd_file} bs=1048576 count=1 | #{md5cmd} | cut '-d ' -f1`.strip
@logger.info("md5=#{md5}")
@@lock.synchronize do
vdis = env[:xc].VDI.get_all_records
vdi_tag = "vagrant:" + env[:machine].box.name.to_s + "/" + md5
vdi_ref_rec = vdis.find { |reference,record|
@logger.info(record['tags'].to_s)
record['tags'].include?(vdi_tag)
}
if not vdi_ref_rec
virtual_size = get_vhd_size(box_vhd_file)
@logger.info("virtual_size=#{virtual_size}")
pool=env[:xc].pool.get_all
default_sr=env[:xc].pool.get_default_SR(pool[0])
@logger.info("default_SR="+default_sr)
begin
env[:xc].SR.get_uuid(default_sr)
rescue
raise Errors::NoDefaultSR
end
vdi_record = {
'name_label' => 'Vagrant disk',
'name_description' => 'Base disk uploaded for the vagrant box '+env[:machine].box.name.to_s+' v'+env[:machine].box.version.to_s,
'SR' => default_sr,
'virtual_size' => "#{virtual_size}",
'type' => 'user',
'sharable' => false,
'read_only' => false,
'other_config' => {},
'xenstore_data' => {},
'sm_config' => {},
'tags' => [] }
begin
vdi_result=env[:xc].VDI.create(vdi_record)
rescue XenApi::Errors::GenericError => e
MyUtil::Exnhandler.handle_xenapiexn("VDI.create",e,@logger)
end
@logger.info("created VDI: " + vdi_result.to_s)
vdi_uuid = env[:xc].VDI.get_uuid(vdi_result)
@logger.info("uuid: "+vdi_uuid)
task = env[:xc].task.create("vagrant-vhd-upload",
"Task to track progress of the XVA upload from vagrant")
url = "https://#{hostname}/import_raw_vdi?session_id=#{session}&task_id=#{task}&vdi=#{vdi_result}&format=vhd"
uploader_options = {}
uploader_options[:ui] = env[:ui]
uploader_options[:insecure] = true
uploader = MyUtil::Uploader.new(box_vhd_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"
@logger.info("task_status="+task_status)
if task_status != "success"
error_list = env[:xc].task.get_error_info(task)
MyUtil::Exnhandler.handle("import HTTP handler", error_list)
end
task_result = env[:xc].task.get_result(task)
doc = REXML::Document.new(task_result)
doc.elements.each('value/array/data/value') do |ele|
vdi = ele.text
end
@logger.info("task_result=" + task_result)
tag_result=env[:xc].VDI.add_tags(vdi_result,vdi_tag)
@logger.info("Added tags")
env[:box_vdi] = vdi_result
else
(reference,record) = vdi_ref_rec
env[:box_vdi] = reference
@logger.info("box_vdi="+reference)
end
end
end
end
@app.call(env)
end
|