-
Notifications
You must be signed in to change notification settings - Fork 32
/
ImageTransferService.java
347 lines (335 loc) · 10.3 KB
/
ImageTransferService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright (c) 2016 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package services;
import mixins.Follow;
import org.ovirt.api.metamodel.annotations.Out;
import org.ovirt.api.metamodel.annotations.Service;
import types.ImageTransfer;
import annotations.Area;
/**
* This service provides a mechanism to control an image transfer. The client will have
* to create a transfer by using xref:services-image_transfers-methods-add[add]
* of the xref:services-image_transfers[image transfers] service, stating the image to transfer
* data to/from.
*
* After doing that, the transfer is managed by this service.
*
* *Using oVirt's Python's SDK:*
*
* Uploading a `disk` with id `123` (on a random host in the data center):
*
* [source,python]
* ----
* transfers_service = system_service.image_transfers_service()
* transfer = transfers_service.add(
* types.ImageTransfer(
* disk=types.Disk(
* id='123'
* )
* )
* )
* ----
*
* Uploading a `disk` with id `123` on `host` id `456`:
*
* [source,python]
* ----
* transfers_service = system_service.image_transfers_service()
* transfer = transfers_service.add(
* types.ImageTransfer(
* disk=types.Disk(
* id='123'
* ),
* host=types.Host(
* id='456'
* )
* )
* )
* ----
*
* If the user wishes to download a disk rather than upload, he/she should specify
* `download` as the xref:types-image_transfer_direction[direction] attribute of the transfer.
* This will grant a read permission from the image, instead of a write permission.
*
* E.g:
*
* [source,python]
* ----
* transfers_service = system_service.image_transfers_service()
* transfer = transfers_service.add(
* types.ImageTransfer(
* disk=types.Disk(
* id='123'
* ),
* direction=types.ImageTransferDirection.DOWNLOAD
* )
* )
* ----
*
* Transfers have phases, which govern the flow of the upload/download.
* A client implementing such a flow should poll/check the transfer's phase and
* act accordingly. All the possible phases can be found in
* xref:types-image_transfer_phase[ImageTransferPhase].
*
* After adding a new transfer, its phase will be xref:types-image_transfer_phase[initializing].
* The client will have to poll on the transfer's phase until it changes.
* When the phase becomes xref:types-image_transfer_phase[transferring],
* the session is ready to start the transfer.
*
* For example:
*
* [source,python]
* ----
* transfer_service = transfers_service.image_transfer_service(transfer.id)
* while transfer.phase == types.ImageTransferPhase.INITIALIZING:
* time.sleep(3)
* transfer = transfer_service.get()
* ----
*
* At that stage, if the phase of the transfer is xref:types-image_transfer_phase[paused_system], the session was
* not successfully established. This can happen if ovirt-imageio is not running in the selected host.
* @author Donna DaCosta <ddacosta@redhat.com>
* @date 29 July 2022
* @status updated_by_docs
* @since 4.5.1
* The transfer can be resumed by calling xref:services-image_transfer-methods-resume[resume]
* of the service that manages it.
*
* If the session was successfully established - the returned transfer entity will
* contain the xref:types-image_transfer[transfer_url] and xref:types-image_transfer[proxy_url] attributes,
* which the client needs to use in order to transfer the required data. The client can choose whatever
* technique and tool for sending the HTTPS request with the image's data.
*
* - `transfer_url` is the address of an imageio server running on one of the hypervisors.
* - `proxy_url` is the address of an imageio proxy server that can be used if
* you cannot access transfer_url.
*
* To transfer the image, it is recommended to use the imageio client python library.
*
* [source,python]
* ----
* from ovirt_imageio import client
*
* # Upload qcow2 image to virtual disk:
* client.upload("disk.qcow2", transfer.transfer_url)
*
* # Download virtual disk to qcow2 image:
* client.download(transfer.transfer_url, "disk.qcow2")
* ----
*
* You can also upload and download using imageio REST API. For more info
* on this, see imageio API documentation:
*
* http://ovirt.github.io/ovirt-imageio/images.html
*
* When finishing the transfer, the user should call
* xref:services-image_transfer-methods-finalize[finalize]. This will make the
* final adjustments and verifications for finishing the transfer process.
*
* For example:
*
* [source,python]
* ----
* transfer_service.finalize()
* ----
*
* In case of an error, the transfer's phase will be changed to
* xref:types-image_transfer_phase[finished_failure], and
* the disk's status will be changed to `Illegal`. Otherwise it will be changed to
* xref:types-image_transfer_phase[finished_success], and the disk will be ready
* to be used. In both cases, the transfer entity will be removed shortly after.
*
*
* *Using HTTP and cURL calls:*
*
* - For upload, create a new disk first:
* * Specify 'initial_size' and 'provisioned_size' in bytes.
* * 'initial_size' must be bigger or the same as the size of the uploaded data.
*
* [source]
* ----
* POST /ovirt-engine/api/disks
* ----
*
* With a request body as follows:
*
* [source,xml]
* ----
* <disk>
* <storage_domains>
* <storage_domain id="123"/>
* </storage_domains>
* <alias>mydisk</alias>
* <initial_size>1073741824</initial_size>
* <provisioned_size>1073741824</provisioned_size>
* <format>raw</format>
* </disk>
* ----
*
*
* - Create a new image transfer for downloading/uploading a `disk` with id `456`:
*
*
* [source]
* ----
* POST /ovirt-engine/api/imagetransfers
* ----
*
* With a request body as follows:
*
* [source,xml]
* ----
* <image_transfer>
* <disk id="456"/>
* <direction>upload|download</direction>
* </image_transfer>
* ----
*
* Will respond:
*
* [source,xml]
* ----
* <image_transfer id="123">
* <direction>download|upload</direction>
* <phase>initializing|transferring</phase>
* <proxy_url>https://proxy_fqdn:54323/images/41c732d4-2210-4e7b-9e5c-4e2805baadbb</proxy_url>
* <transfer_url>https://daemon_fqdn:54322/images/41c732d4-2210-4e7b-9e5c-4e2805baadbb</transfer_url>
* ...
* </image_transfer>
* ----
*
* Note: If the phase is 'initializing', poll the `image_transfer` till its phase changes to 'transferring'.
*
* - Use the 'transfer_url' or 'proxy_url' to invoke a curl command:
* - use 'transfer_url' for transferring directly from/to ovirt-imageio,
* or, use 'proxy_url' for transferring from/to ovirt-imageio.
* Note: using the proxy would mitigate scenarios where there's no direct connectivity
* to the daemon machine, e.g. vdsm machines are on a different network than the engine.
* @author Donna DaCosta <ddacosta@redhat.com>
* @date 26 July 2022
* @status updated_by_docs
* @since 4.5.1
* -- Download:
*
* [source,shell]
* ----
* $ curl --cacert /etc/pki/ovirt-engine/ca.pem https://daemon_fqdn:54322/images/41c732d4-2210-4e7b-9e5c-4e2805baadbb -o <output_file>
* ----
*
* -- Upload:
*
* [source,shell]
* ----
* $ curl --cacert /etc/pki/ovirt-engine/ca.pem --upload-file <file_to_upload> -X PUT https://daemon_fqdn:54322/images/41c732d4-2210-4e7b-9e5c-4e2805baadbb
* ----
*
* - Finalize the image transfer by invoking the action:
*
* [source]
* ----
* POST /ovirt-engine/api/imagetransfers/123/finalize
* ----
*
* With a request body as follows:
*
* [source,xml]
* ----
* <action />
* ----
*
*
* @author Amit Aviram <aaviram@redhat.com>
* @author Eli Marcus <emarcus@redhat.com>
* @date 24 Aug 2020
* @status updated_by_docs
* @since 4.0.4
*/
@Service
@Area("Storage")
public interface ImageTransferService {
/**
* Get the image transfer entity.
*
* @author Amit Aviram <aaviram@redhat.com>
* @author Billy Burmester <bburmest@redhat.com>
* @date 12 Jun 2018
* @status updated_by_docs
* @since 4.0.4
*/
interface Get extends Follow {
@Out ImageTransfer imageTransfer();
}
/**
* Extend the image transfer session.
*
* @author Amit Aviram <aaviram@redhat.com>
* @date 30 Aug 2016
* @status added
* @since 4.0.4
*/
interface Extend {}
/**
* Pause the image transfer session.
*
* @author Amit Aviram <aaviram@redhat.com>
* @date 30 Aug 2016
* @status added
* @since 4.0.4
*/
interface Pause {}
/**
* Resume the image transfer session. The client will need to poll the transfer's phase until
* it is different than `resuming`. For example:
*
* [source,python]
* ----
* transfer_service = transfers_service.image_transfer_service(transfer.id)
* transfer_service.resume()
* transfer = transfer_service.get()
*
* while transfer.phase == types.ImageTransferPhase.RESUMING:
* time.sleep(1)
* transfer = transfer_service.get()
* ----
*
* @author Amit Aviram <aaviram@redhat.com>
* @date 30 Aug 2016
* @status added
* @since 4.0.4
*/
interface Resume {}
/**
* Cancel the image transfer session. This terminates the transfer operation and removes the partial image.
*
* @author Arik Hadas <ahadas@redhat.com>
* @author Billy Burmester <bburmest@redhat.com>
* @date 28 Mar 2018
* @status update_by_docs
* @since 4.2.3
*/
interface Cancel {}
/**
* After finishing to transfer the data, finalize the transfer.
*
* This will make sure that the data being transferred is valid and fits the
* image entity that was targeted in the transfer. Specifically, will verify that
* if the image entity is a QCOW disk, the data uploaded is indeed a QCOW file,
* and that the image doesn't have a backing file.
*
* @author Amit Aviram <aaviram@redhat.com>
* @date 30 Aug 2016
* @status added
* @since 4.0.4
*/
interface Finalize {}
}