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
0c651ac2
authored
Jul 24, 2013
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor Disk and StorageDriver to different modules
parent
4ae1ab30
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
101 deletions
+102
-101
__init__.py
+0
-0
disk.py
+84
-0
storagedriver.py
+18
-101
No files found.
__init__.py
0 → 100644
View file @
0c651ac2
disk.py
0 → 100644
View file @
0c651ac2
import
json
import
os
import
subprocess
import
re
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+)\))$'
)
class
Disk
(
object
):
def
__init__
(
self
,
dir
,
name
,
format
,
size
,
base_name
,
type
):
# TODO: tests
self
.
name
=
name
self
.
dir
=
os
.
path
.
realpath
(
dir
)
if
format
not
in
[
'qcow2'
,
'raw'
]:
raise
Exception
(
'Invalid format:
%
s'
%
format
)
self
.
format
=
format
self
.
size
=
int
(
size
)
self
.
base_name
=
base_name
if
type
not
in
[
'normal'
,
'snapshot'
]:
raise
Exception
(
'Invalid type:
%
s'
%
type
)
self
.
type
=
type
def
get_path
(
self
):
return
os
.
path
.
realpath
(
self
.
dir
+
'/'
+
self
.
name
)
def
get_base
(
self
):
return
os
.
path
.
realpath
(
self
.
dir
+
'/'
+
self
.
base_name
)
def
__unicode__
(
self
):
return
u'
%
s
%
s
%
s
%
s'
%
(
self
.
get_path
(),
self
.
format
,
self
.
size
,
self
.
get_base
())
@classmethod
def
import_from_json
(
cls
,
json_data
):
obj
=
json
.
loads
(
json_data
)
return
Disk
(
obj
[
'dir'
],
obj
[
'name'
],
obj
[
'format'
],
obj
[
'size'
],
obj
[
'base_name'
],
obj
[
'type'
])
@classmethod
def
get
(
cls
,
dir
,
name
):
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
output
=
subprocess
.
check_output
([
'qemu-img'
,
'info'
,
path
])
type
=
'normal'
base
=
None
for
line
in
output
.
split
(
'
\n
'
):
m
=
re_qemu_img
.
search
(
line
)
if
m
:
res
=
m
.
groupdict
()
if
res
.
get
(
'format'
,
None
)
is
not
None
:
format
=
res
[
'format'
]
if
res
.
get
(
'size'
,
None
)
is
not
None
:
size
=
res
[
'size'
]
if
res
.
get
(
'base'
,
None
)
is
not
None
:
base
=
os
.
path
.
basename
(
res
[
'base'
])
type
=
'snapshot'
return
Disk
(
dir
,
name
,
format
,
size
,
base
,
type
)
def
create
(
self
):
if
os
.
path
.
isfile
(
self
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
cmdline
=
[
'qemu-img'
,
'create'
,
'-f'
,
self
.
format
]
if
self
.
type
==
'snapshot'
:
cmdline
.
append
(
'-b'
)
cmdline
.
append
(
self
.
get_base
())
cmdline
.
append
(
self
.
get_path
())
if
self
.
type
!=
'snapshot'
:
cmdline
.
append
(
str
(
self
.
size
))
print
' '
.
join
(
cmdline
)
subprocess
.
check_output
(
cmdline
)
def
delete
(
self
):
if
os
.
path
.
isfile
(
self
.
get_path
()):
os
.
unlink
(
self
.
get_path
())
@classmethod
def
list
(
cls
,
dir
):
return
[
cls
.
get
(
dir
,
file
)
for
file
in
os
.
listdir
(
dir
)]
storagedriver.py
View file @
0c651ac2
#!/usr/bin/env python
# import logging
import
json
import
os
import
re
import
subprocess
import
jsonpickle
import
jsonpickle
from
celery.contrib.methods
import
task_method
from
celery
import
Celery
from
celery
import
Celery
re_qemu_img
=
re
.
compile
(
r'(file format: (?P<format>(qcow2|raw))|'
from
disk
import
Disk
r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
r'backing file: \S+ \(actual path: (?P<base>\S+)\))$'
)
BROKER_URL
=
'amqp://nyuszi:teszt@localhost:5672/django'
BROKER_URL
=
'amqp://nyuszi:teszt@localhost:5672/django'
celery
=
Celery
(
'tasks'
,
broker
=
BROKER_URL
,
backend
=
'amqp'
)
celery
=
Celery
(
'tasks'
,
broker
=
BROKER_URL
,
backend
=
'amqp'
)
celery
.
config_from_object
(
'celeryconfig'
)
celery
.
config_from_object
(
'celeryconfig'
)
class
Disk
():
@celery.task
()
def
list_disks
():
def
__init__
(
self
,
dir
,
name
,
format
,
size
,
base_name
,
type
):
return
jsonpickle
.
encode
(
Disk
.
list
(
'/home/cloud/images'
),
# TODO: tests
unpicklable
=
False
)
self
.
name
=
name
self
.
dir
=
os
.
path
.
realpath
(
dir
)
if
format
not
in
[
'qcow2'
,
'raw'
]:
raise
Exception
(
'Invalid format:
%
s'
%
format
)
self
.
format
=
format
self
.
size
=
int
(
size
)
self
.
base_name
=
base_name
if
type
not
in
[
'normal'
,
'snapshot'
]:
raise
Exception
(
'Invalid type:
%
s'
%
type
)
self
.
type
=
type
def
get_path
(
self
):
return
os
.
path
.
realpath
(
self
.
dir
+
'/'
+
self
.
name
)
def
get_base
(
self
):
return
os
.
path
.
realpath
(
self
.
dir
+
'/'
+
self
.
base_name
)
def
__unicode__
(
self
):
return
u'
%
s
%
s
%
s
%
s'
%
(
self
.
get_path
(),
self
.
format
,
self
.
size
,
self
.
get_base
())
@classmethod
def
import_from_json
(
cls
,
json_data
):
obj
=
json
.
loads
(
json_data
)
return
Disk
(
obj
[
'dir'
],
obj
[
'name'
],
obj
[
'format'
],
obj
[
'size'
],
obj
[
'base_name'
],
obj
[
'type'
])
@classmethod
def
get
(
cls
,
dir
,
name
):
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
output
=
subprocess
.
check_output
([
'qemu-img'
,
'info'
,
path
])
type
=
'normal'
base
=
None
for
line
in
output
.
split
(
'
\n
'
):
m
=
re_qemu_img
.
search
(
line
)
if
m
:
res
=
m
.
groupdict
()
if
res
.
get
(
'format'
,
None
)
is
not
None
:
format
=
res
[
'format'
]
if
res
.
get
(
'size'
,
None
)
is
not
None
:
size
=
res
[
'size'
]
if
res
.
get
(
'base'
,
None
)
is
not
None
:
base
=
os
.
path
.
basename
(
res
[
'base'
])
type
=
'snapshot'
return
Disk
(
dir
,
name
,
format
,
size
,
base
,
type
)
def
create
(
self
):
if
os
.
path
.
isfile
(
self
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
cmdline
=
[
'qemu-img'
,
'create'
,
'-f'
,
self
.
format
]
if
self
.
type
==
'snapshot'
:
cmdline
.
append
(
'-b'
)
cmdline
.
append
(
self
.
get_base
())
cmdline
.
append
(
self
.
get_path
())
if
self
.
type
!=
'snapshot'
:
cmdline
.
append
(
str
(
self
.
size
))
print
' '
.
join
(
cmdline
)
subprocess
.
check_output
(
cmdline
)
def
delete
(
self
):
if
os
.
path
.
isfile
(
self
.
get_path
()):
os
.
unlink
(
self
.
get_path
())
@classmethod
def
list
(
cls
,
dir
):
return
[
cls
.
get
(
dir
,
file
)
for
file
in
os
.
listdir
(
dir
)]
@celery.task
()
def
create_disk
(
json_data
):
disk
=
Disk
.
import_from_json
(
json_data
)
disk
.
create
()
class
StorageDriver
:
@celery.task
(
filter
=
task_method
)
def
list_disks
():
return
jsonpickle
.
encode
(
Disk
.
list
(
'/home/cloud/images'
),
unpicklable
=
False
)
@celery.task
(
filter
=
task_method
)
@celery.task
(
)
def
crea
te_disk
(
json_data
):
def
dele
te_disk
(
json_data
):
disk
=
Disk
.
import_from_json
(
json_data
)
disk
=
Disk
.
import_from_json
(
json_data
)
disk
.
crea
te
()
disk
.
dele
te
()
@celery.task
(
filter
=
task_method
)
def
delete_disk
(
json_data
):
disk
=
Disk
.
import_from_json
(
json_data
)
disk
.
delete
()
@celery.task
(
filter
=
task_method
)
@celery.task
(
)
def
get_disk
(
json_data
):
def
get_disk
(
json_data
):
disk
=
Disk
.
import_from_json
(
json_data
)
disk
=
Disk
.
import_from_json
(
json_data
)
return
jsonpickle
.
encode
(
disk
,
unpicklable
=
False
)
return
jsonpickle
.
encode
(
disk
,
unpicklable
=
False
)
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