Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
storagedriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dbfb53e6
authored
Jul 09, 2014
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite merge task
parent
5b820fc0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
34 deletions
+38
-34
disk.py
+38
-34
No files found.
disk.py
View file @
dbfb53e6
...
...
@@ -4,10 +4,11 @@ import subprocess
import
re
import
logging
import
magic
from
shutil
import
move
,
copy
,
copy
fileobj
from
shutil
import
move
,
copyfileobj
from
zipfile
import
ZipFile
,
is_zipfile
from
zlib
import
decompressobj
,
MAX_WBITS
from
bz2
import
BZ2Decompressor
from
time
import
sleep
import
requests
...
...
@@ -18,6 +19,10 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
r'backing file: \S+ \(actual path: (?P<base>\S+)\))$'
)
class
AbortException
(
Exception
):
pass
class
Disk
(
object
):
''' Storage driver DISK object.
...
...
@@ -143,8 +148,6 @@ class Disk(object):
raise
Exception
(
"Invalid response status code:
%
s at
%
s"
%
(
r
.
status_code
,
url
))
class
AbortException
(
Exception
):
pass
if
task
.
is_aborted
():
raise
AbortException
()
if
parent_id
is
None
:
...
...
@@ -248,28 +251,11 @@ class Disk(object):
# Call subprocess
subprocess
.
check_output
(
cmdline
)
def
merge
(
self
,
task
,
new_disk
):
""" Merging a new_disk from the actual disk and its base.
"""
from
time
import
sleep
import
threading
class
AbortException
(
Exception
):
pass
if
task
.
is_aborted
():
raise
AbortException
()
# Check if file already exists
if
os
.
path
.
isfile
(
new_disk
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
if
self
.
format
==
"iso"
:
os
.
symlink
(
self
.
get_path
(),
new_disk
.
get_path
())
elif
self
.
base_name
:
def
merge_disk_with_base
(
self
,
task
,
new_disk
):
try
:
cmdline
=
[
'qemu-img'
,
'convert'
,
self
.
get_path
(),
'-O'
,
new_disk
.
format
,
new_disk
.
get_path
()]
cmdline
=
[
'qemu-img'
,
'convert'
,
self
.
get_path
(),
'-O'
,
new_disk
.
format
,
new_disk
.
get_path
()]
# Call subprocess
logger
.
debug
(
"Merging
%
s into
%
s."
,
self
.
get_path
(),
...
...
@@ -282,32 +268,50 @@ class Disk(object):
logger
.
warning
(
"Merging new disk
%
s is aborted by user."
,
new_disk
.
get_path
())
raise
AbortException
raise
AbortException
()
sleep
(
1
)
except
AbortException
:
proc
.
terminate
()
logger
.
warning
(
"Aborted remove
%
s"
,
new_disk
.
get_path
())
os
.
unlink
(
new_disk
.
get_path
())
else
:
def
merge_disk_without_base
(
self
,
task
,
new_disk
,
length
=
1024
*
1024
):
try
:
t
=
threading
.
Thread
(
target
=
copy
,
args
=
(
self
.
get_path
(),
new_disk
.
get_path
()))
fsrc
=
open
(
self
.
get_path
(),
'rb'
)
fdst
=
open
(
new_disk
.
get_path
(),
'wb'
)
with
fsrc
,
fdst
:
while
True
:
if
not
t
.
isAlive
():
buf
=
fsrc
.
read
(
length
)
if
not
buf
:
break
fdst
.
write
(
buf
)
if
task
.
is_aborted
():
logger
.
warning
(
"Merging new disk
%
s is aborted by user."
,
new_disk
.
get_path
())
raise
AbortException
raise
AbortException
()
except
AbortException
:
t
.
_Thread__stop
()
logger
.
warning
(
"Aborted remove
%
s"
,
new_disk
.
get_path
())
os
.
unlink
(
new_disk
.
get_path
())
def
merge
(
self
,
task
,
new_disk
):
""" Merging a new_disk from the actual disk and its base.
"""
if
task
.
is_aborted
():
raise
AbortException
()
# Check if file already exists
if
os
.
path
.
isfile
(
new_disk
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
if
self
.
format
==
"iso"
:
os
.
symlink
(
self
.
get_path
(),
new_disk
.
get_path
())
elif
self
.
base_name
:
self
.
merge_disk_with_base
(
task
,
new_disk
)
else
:
self
.
merge_disk_without_base
(
task
,
new_disk
)
def
delete
(
self
):
""" Delete file. """
if
os
.
path
.
isfile
(
self
.
get_path
()):
...
...
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