Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
vmdriver
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
f815ffdf
authored
Aug 01, 2013
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding screenshot and sendkey fixing decorator docstring and connection handle
parent
6581a2b5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
3 deletions
+52
-3
vmdriver.py
+52
-3
No files found.
vmdriver.py
View file @
f815ffdf
...
@@ -2,7 +2,8 @@
...
@@ -2,7 +2,8 @@
import
libvirt
import
libvirt
import
logging
import
logging
import
os
import
decorator
connection
=
None
connection
=
None
...
@@ -16,11 +17,10 @@ state_dict = {0: 'NOSTATE',
...
@@ -16,11 +17,10 @@ state_dict = {0: 'NOSTATE',
7
:
'PMSUSPENDED'
7
:
'PMSUSPENDED'
}
}
@decorator.decorator
def
req_connection
(
original_function
):
def
req_connection
(
original_function
):
'''Connection checking decorator for libvirt.
'''Connection checking decorator for libvirt.
'''
'''
def
new_function
(
*
args
,
**
kwargs
):
def
new_function
(
*
args
,
**
kwargs
):
logging
.
debug
(
"Decorator running"
)
logging
.
debug
(
"Decorator running"
)
global
connection
global
connection
...
@@ -33,6 +33,12 @@ def req_connection(original_function):
...
@@ -33,6 +33,12 @@ def req_connection(original_function):
logging
.
debug
(
"Finally part of decorator"
)
logging
.
debug
(
"Finally part of decorator"
)
disconnect
()
disconnect
()
return
return_value
return
return_value
else
:
logging
.
debug
(
"Decorator calling original
\
function with active connection"
)
return_value
=
original_function
(
*
args
,
**
kwargs
)
return
return_value
new_function
.
__dict__
.
update
(
original_function
.
__dict__
)
return
new_function
return
new_function
...
@@ -199,4 +205,47 @@ def domain_info(name):
...
@@ -199,4 +205,47 @@ def domain_info(name):
info
=
dict
(
zip
(
keys
,
values
))
info
=
dict
(
zip
(
keys
,
values
))
info
[
'state'
]
=
state_dict
[
info
[
'state'
]]
info
[
'state'
]
=
state_dict
[
info
[
'state'
]]
return
info
return
info
@req_connection
def
send_key
(
name
,
key_code
):
''' Sending linux key_code to the name vm
key_code can be optained from linux_keys.py
e.x: linuxkeys.KEY_RIGHT_CTRL
'''
domain
=
lookupByName
(
name
)
domain
.
sendKey
(
libvirt
.
VIR_KEYCODE_SET_LINUX
,
100
,
[
key_code
],
1
,
0
)
def
_stream_handler
(
stream
,
buf
,
opaque
):
fd
=
opaque
os
.
write
(
fd
,
buf
)
@req_connection
def
screenshot
(
name
,
path
):
"""Save screenshot of virtual machine
to the path as name-screenshot.ppm
"""
# Import linuxkeys to get defines
import
linuxkeys
# Connection need for the stream object
global
connection
domain
=
lookupByName
(
name
)
# Send key to wake up console
domain
.
sendKey
(
libvirt
.
VIR_KEYCODE_SET_LINUX
,
100
,
[
linuxkeys
.
KEY_RIGHTCTRL
],
1
,
0
)
# Create Stream to get data
stream
=
connection
.
newStream
(
0
)
# Take screenshot accessible by stream (return mimetype)
domain
.
screenshot
(
stream
,
0
,
0
)
# Get file to save data (TODO: send on AMQP?)
try
:
fd
=
os
.
open
(
path
+
name
+
"-screenshot.ppm"
,
os
.
O_WRONLY
|
os
.
O_TRUNC
|
os
.
O_CREAT
,
0
o644
)
# Save data with handler
stream
.
recvAll
(
_stream_handler
,
fd
)
finally
:
stream
.
finish
()
os
.
close
(
fd
)
# virDomainResume
# virDomainResume
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