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
6ac9d76c
authored
Aug 15, 2019
by
Bálint Máhonfai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add export operation to storage driver
parent
e3730928
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
15 deletions
+38
-15
disk.py
+25
-8
storagedriver.py
+13
-7
No files found.
disk.py
View file @
6ac9d76c
...
...
@@ -19,7 +19,7 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
r'backing file: \S+ \(actual path: (?P<base>\S+)\))$'
)
maximum_size
=
float
(
os
.
getenv
(
"DOWNLOAD_MAX_SIZE"
,
1024
*
1024
*
1024
*
10
))
maximum_size
=
float
(
os
.
getenv
(
"DOWNLOAD_MAX_SIZE"
,
1024
*
1024
*
1024
*
10
))
class
AbortException
(
Exception
):
...
...
@@ -31,14 +31,14 @@ class FileTooBig(Exception):
class
Disk
(
object
):
''' Storage driver DISK object.
""" Storage driver DISK object.
Handle qcow2, raw and iso images.
TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions.
'''
"""
TYPES
=
[
'snapshot'
,
'normal'
]
FORMATS
=
[
'qcow2'
,
'raw'
,
'iso'
]
CREATE_FORMATS
=
[
'qcow2'
,
'raw'
]
EXPORT_FORMATS
=
[
'vdi'
,
'vpc'
,
'vmdk'
]
def
__init__
(
self
,
dir
,
name
,
format
,
type
,
size
,
base_name
,
actual_size
=
0
):
...
...
@@ -100,8 +100,8 @@ class Disk(object):
@classmethod
def
get_legacy
(
cls
,
dir
,
name
):
'''
Create disk from path
'''
"""
Create disk from path
"""
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
output
=
subprocess
.
check_output
([
'qemu-img'
,
'info'
,
path
])
...
...
@@ -266,6 +266,23 @@ class Disk(object):
raise
Exception
(
"Invalid file format. Only qcow and "
"iso files are allowed. Image from:
%
s"
%
url
)
def
export
(
self
,
format
):
if
format
not
in
self
.
EXPORT_FORMATS
:
raise
Exception
(
'Invalid export format:
%
s'
%
format
)
format_dict
=
{
'vdi'
:
'vdi'
,
'vpc'
:
'vhd'
,
'vmdk'
:
'vmdk'
,
}
cmdline
=
[
'qemu-img'
,
'convert'
,
'-O'
,
format
,
self
.
get_path
(),
self
.
get_path
()
+
'.'
+
format_dict
[
format
]]
subprocess
.
check_output
(
cmdline
)
def
extract_iso_from_zip
(
self
,
disk_path
):
with
ZipFile
(
disk_path
,
'r'
)
as
z
:
isos
=
z
.
namelist
()
...
...
@@ -285,8 +302,8 @@ class Disk(object):
disk_path
)
def
snapshot
(
self
):
'''
Creating qcow2 snapshot with base image.
'''
"""
Creating qcow2 snapshot with base image.
"""
# Check if snapshot type and qcow2 format matchmatch
if
self
.
type
!=
'snapshot'
:
raise
Exception
(
'Invalid type:
%
s'
%
self
.
type
)
...
...
storagedriver.py
View file @
6ac9d76c
...
...
@@ -42,6 +42,12 @@ class download(AbortableTask):
@celery.task
()
def
export
(
disk_desc
,
format
):
disk
=
Disk
.
deserialize
(
disk_desc
)
disk
.
export
(
format
)
@celery.task
()
def
delete
(
json_data
):
disk
=
Disk
.
deserialize
(
json_data
)
disk
.
delete
()
...
...
@@ -79,7 +85,7 @@ def get(json_data):
@celery.task
()
def
get_storage_stat
(
path
):
''' Return free disk space avaliable at path in bytes and percent.'''
""" Return free disk space avaliable at path in bytes and percent."""
s
=
statvfs
(
path
)
all_space
=
s
.
f_bsize
*
s
.
f_blocks
free_space
=
s
.
f_bavail
*
s
.
f_frsize
...
...
@@ -110,8 +116,8 @@ def get_file_statistics(datastore):
@celery.task
def
move_to_trash
(
datastore
,
disk_name
):
'''
Move path to the trash directory.
'''
"""
Move path to the trash directory.
"""
trash_path
=
path
.
join
(
datastore
,
trash_directory
)
disk_path
=
path
.
join
(
datastore
,
disk_name
)
if
not
path
.
isdir
(
trash_path
):
...
...
@@ -122,8 +128,8 @@ def move_to_trash(datastore, disk_name):
@celery.task
def
recover_from_trash
(
datastore
,
disk_name
):
'''
Recover named disk from the trash directory.
'''
"""
Recover named disk from the trash directory.
"""
if
path
.
exists
(
path
.
join
(
datastore
,
disk_name
)):
return
False
disk_path
=
path
.
join
(
datastore
,
trash_directory
,
disk_name
)
...
...
@@ -134,10 +140,10 @@ def recover_from_trash(datastore, disk_name):
@celery.task
def
make_free_space
(
datastore
,
percent
=
10
):
'''
Check for free space on datastore.
"""
Check for free space on datastore.
If free space is less than the given percent
removes oldest files to satisfy the given requirement.
'''
"""
trash_path
=
path
.
join
(
datastore
,
trash_directory
)
def
comp
(
filename
):
...
...
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