Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
storagedriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
2
Merge Requests
4
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e78ee037
authored
Dec 21, 2020
by
Máhonfai Bálint
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add progress and handle abort for import disk
parent
349cb3f5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
20 deletions
+53
-20
disk.py
+40
-15
storagedriver.py
+13
-5
No files found.
disk.py
View file @
e78ee037
...
@@ -266,26 +266,51 @@ class Disk(object):
...
@@ -266,26 +266,51 @@ class Disk(object):
raise
Exception
(
"Invalid file format. Only qcow and "
raise
Exception
(
"Invalid file format. Only qcow and "
"iso files are allowed. Image from:
%
s"
%
url
)
"iso files are allowed. Image from:
%
s"
%
url
)
def
import_disk
(
self
,
url
):
def
import_disk
(
self
,
task
,
url
,
parent_id
):
r
=
requests
.
get
(
url
,
stream
=
True
)
r
=
requests
.
get
(
url
,
stream
=
True
)
clen
=
int
(
r
.
headers
.
get
(
'content-length'
))
downloaded_file
=
os
.
path
.
join
(
self
.
dir
,
url
.
split
(
'/'
)[
-
1
])
downloaded_file
=
os
.
path
.
join
(
self
.
dir
,
url
.
split
(
'/'
)[
-
1
])
with
open
(
downloaded_file
,
'wb'
)
as
f
:
percent
=
0
f
.
write
(
r
.
content
)
try
:
with
open
(
downloaded_file
,
'wb'
)
as
f
:
cmdline
=
[
'qemu-img'
,
for
chunk
in
r
.
iter_content
(
chunk_size
=
256
*
1024
):
'convert'
,
f
.
write
(
chunk
)
'-O'
,
'qcow2'
,
current_size
=
f
.
tell
()
downloaded_file
,
new_percent
=
current_size
*
100
/
clen
self
.
get_path
()]
if
task
.
is_aborted
():
subprocess
.
check_output
(
cmdline
)
raise
AbortException
()
if
new_percent
>
percent
:
percent
=
new_percent
task
.
update_state
(
task_id
=
parent_id
,
state
=
task
.
AsyncResult
(
parent_id
)
.
state
,
meta
=
{
'percent'
:
percent
}
)
os
.
unlink
(
downloaded_file
)
cmdline
=
[
'qemu-img'
,
'convert'
,
'-O'
,
'qcow2'
,
downloaded_file
,
self
.
get_path
()]
subprocess
.
check_output
(
cmdline
)
except
AbortException
:
os
.
unlink
(
downloaded_file
)
if
os
.
path
.
exists
(
self
.
get_path
()):
os
.
unlink
(
self
.
get_path
())
logger
.
info
(
"Import of disk
%
s aborted"
%
self
.
name
)
except
:
os
.
unlink
(
downloaded_file
)
if
os
.
path
.
exists
(
self
.
get_path
()):
os
.
unlink
(
self
.
get_path
())
raise
else
:
os
.
unlink
(
downloaded_file
)
if
not
self
.
check_valid_image
():
if
not
self
.
check_valid_image
():
os
.
unlink
(
self
.
get_path
())
os
.
unlink
(
self
.
get_path
())
raise
Exception
(
"Invalid file format."
)
raise
Exception
(
"Invalid file format."
)
self
.
size
=
Disk
.
get
(
self
.
dir
,
self
.
name
)
.
size
self
.
size
=
Disk
.
get
(
self
.
dir
,
self
.
name
)
.
size
def
export
(
self
,
format
,
exported_name
,
upload_link
):
def
export
(
self
,
format
,
exported_name
,
upload_link
):
exported_path
=
self
.
get_path
()
+
'.'
+
format
exported_path
=
self
.
get_path
()
+
'.'
+
format
...
...
storagedriver.py
View file @
e78ee037
...
@@ -41,11 +41,19 @@ class download(AbortableTask):
...
@@ -41,11 +41,19 @@ class download(AbortableTask):
'checksum'
:
disk
.
checksum
,
}
'checksum'
:
disk
.
checksum
,
}
@celery.task
()
class
import_disk
(
AbortableTask
):
def
import_disk
(
disk_desc
,
url
):
time_limit
=
18000
disk
=
Disk
.
deserialize
(
disk_desc
)
disk
.
import_disk
(
url
)
def
run
(
self
,
**
kwargs
):
return
disk
.
size
disk_desc
=
kwargs
[
"disk_desc"
]
url
=
kwargs
[
"url"
]
parent_id
=
kwargs
[
"task"
]
disk
=
Disk
.
deserialize
(
disk_desc
)
disk
.
import_disk
(
self
,
url
,
parent_id
)
return
{
"size"
:
disk
.
size
,
"checksum"
:
disk
.
checksum
}
@celery.task
()
@celery.task
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment