Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Convert to Admin API
Browse files Browse the repository at this point in the history
Make it use Admin API ('qubesadmin' modules instead of 'qubes'). This
will allow seamless migration to GUI VM.

Most of the changes are just renaming imports. The only other change is
events handling - now require running asyncio event loop.
  • Loading branch information
marmarek committed May 19, 2017
1 parent 737ffc0 commit acb9443
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 34 deletions.
4 changes: 2 additions & 2 deletions qubesdbus/device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pprint
import sys

import qubes
import qubesadmin
import systemd.journal
from gi.repository import GLib

Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(self, bus, bus_name, bus_path, data):
def main(args=None): # pylint: disable=unused-argument
''' Main function starting the DomainManager1 service. '''
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()
app.domains[0].devices[
'pci'].available() # HACK this populates dom0 devices
devices = qubesdbus.serialize.devices_data(app)
Expand Down
6 changes: 3 additions & 3 deletions qubesdbus/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
''' D-Bus Domain object '''

import dbus.service
import qubes
import qubesadmin

import qubesdbus.service

Expand Down Expand Up @@ -62,7 +62,7 @@ def StartingSignal(self, name):

@dbus.service.method("org.qubes.Domain", out_signature="b")
def Shutdown(self):
app = qubes.Qubes()
app = qubesadmin.Qubes()
name = str(self.name)
vm = app.domains[name]
vm.shutdown(wait=True)
Expand All @@ -71,7 +71,7 @@ def Shutdown(self):

@dbus.service.method("org.qubes.Domain", out_signature="b")
def Start(self):
app = qubes.Qubes()
app = qubesadmin.Qubes()
name = str(self.name)
vm = app.domains[name]
vm.start()
Expand Down
4 changes: 2 additions & 2 deletions qubesdbus/domain_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import sys

import dbus.service
import qubes
import qubesadmin
from gi.repository import GLib
from systemd.journal import JournalHandler

Expand Down Expand Up @@ -119,7 +119,7 @@ def _proxify_domain(self, vm):
def main(args=None): # pylint: disable=unused-argument
''' Main function starting the DomainManager1 service. '''
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()
data = qubesdbus.serialize.qubes_data(app)
domains = [qubesdbus.serialize.domain_data(vm) for vm in app.domains]
_ = DomainManager(data, domains)
Expand Down
6 changes: 3 additions & 3 deletions qubesdbus/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import sys

import qubes
import qubesadmin
from gi.repository import GLib
from systemd.journal import JournalHandler

Expand Down Expand Up @@ -65,10 +65,10 @@ def __init__(self, bus, bus_name, bus_path, data):
def main(args=None):
''' Main function ''' # pylint: disable=unused-argument
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()

labels_data = [qubesdbus.serialize.label_data(label)
for label in app.labels.values()]
for label in app.labels]
_ = Labels(labels_data)
print("Service running...")
loop.run()
Expand Down
37 changes: 30 additions & 7 deletions qubesdbus/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

from __future__ import absolute_import

import asyncio
import logging

import dbus
from qubes import Qubes
from qubes.ext import Extension, handler
from qubes.vm.qubesvm import QubesVM
from qubesadmin import Qubes
from qubesadmin.events import EventsDispatcher
from qubesadmin.vm import QubesVM
from systemd.journal import JournalHandler

import qubesdbus.serialize
Expand Down Expand Up @@ -62,15 +63,24 @@ def is_garbage(event):
return False


class QubesDbusProxy(Extension):
class QubesDbusProxy(object):
# pylint: disable=too-few-public-methods,no-self-use
def __init__(self):
super(QubesDbusProxy, self).__init__()
self.domains = {} # type: Dict[int, bool]
self.new_vm = [] # type: List[QubesVM]
self.new_vm = [] # type: List[QubesVM]
self.app = Qubes()
self._events_dispatcher = EventsDispatcher(self.app)
self._events_dispatcher.add_handler('*', self.forward_vm_event)
self._events_dispatcher.add_handler('*', self.forward_app_event)

@asyncio.coroutine
def run(self):
yield from self._events_dispatcher.listen_for_events()

@handler('*')
def forward_vm_event(self, vm, event, *args, **kwargs):
if vm is None:
return
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
return
Expand Down Expand Up @@ -101,8 +111,9 @@ def forward_vm_event(self, vm, event, *args, **kwargs):
else:
log.warn('Unknown %s from %s %s %s', event, vm, args, kwargs)

@handler('*', system=True)
def forward_app_event(self, vm, event, *args, **kwargs):
if vm is not None:
return
proxy = app_proxy()
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
Expand Down Expand Up @@ -163,3 +174,15 @@ def get_proxy(obj):
else:
log.error("Unknown sender object %s", obj)
return


def main():
proxy = QubesDbusProxy()
loop = asyncio.get_event_loop()
loop.run_until_complete(proxy.run())
loop.stop()
loop.run_forever()
loop.close()

if __name__ == '__main__':
main()
26 changes: 13 additions & 13 deletions qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import re

import dbus
import qubes
import qubes.vm.qubesvm

from libvirt import libvirtError # pylint: disable=import-error
import qubesadmin
import qubesadmin.devices as devices
import qubesadmin.vm

try:
# Check mypy dependencies. pylint: disable=ungrouped-imports,unused-import
from typing import Any, Callable, Tuple, List, Dict
from qubes.vm.qubesvm import QubesVM
from qubes import Qubes, Label
from qubesadmin.vm import QubesVM
from qubesadmin import Qubes
from qubesadmin.label import Label
except ImportError:
pass

Expand All @@ -46,7 +46,7 @@ def qubes_data(app):
# type: (Qubes) -> Dict[dbus.String, Any]
result = {}
for prop in app.property_list():
name = prop.__name__
name = str(prop)
key = dbus.String(name)
try:
result[key] = serialize_val(getattr(app, name))
Expand All @@ -61,10 +61,10 @@ def domain_data(vm):
# type: (QubesVM) -> Dict[dbus.String, Any]
result = dbus.Dictionary({}, signature='sv')
for prop in vm.property_list():
name = prop.__name__
name = str(prop)
try:
value = serialize_val(getattr(vm, name))
except (AttributeError, libvirtError):
except AttributeError:
value = dbus.String('')
result[name] = value

Expand Down Expand Up @@ -110,13 +110,13 @@ def serialize_val(value):
return dbus.Int32(value)
elif callable(value):
return serialize_val(value())
elif isinstance(value, qubes.Label):
elif isinstance(value, qubesadmin.label.Label):
return label_path(value)
elif isinstance(value, qubes.vm.qubesvm.QubesVM):
elif isinstance(value, qubesadmin.vm.QubesVM):
return domain_path(value)
elif isinstance(value, qubes.devices.DeviceCollection):
elif isinstance(value, devices.DeviceCollection):
return dbus.Array(device_collection_data(value), signature='a{sv}')
elif isinstance(value, qubes.devices.DeviceInfo):
elif isinstance(value, devices.DeviceInfo):
return dbus.Dictionary(device_data(value), signature='sv')
elif isinstance(value, re._pattern_type):
return dbus.String(value.pattern)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
url='https://www.qubes-os.org/',
packages=['qubesdbus'],
entry_points={
'qubes.ext': [
'qubesdbus = qubesdbus:QubesDbusProxy'
'console_scripts': [
'qubes-dbus-proxy = qubesdbus.proxy:main'
]
})
4 changes: 2 additions & 2 deletions stubs/qubes/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import (List)
import qubes.vm
import qubesadmin.vm
class Qubes(object):
domains = ... # type: List[qubes.vm.BaseVM]
domains = ... # type: List[qubesadmin.vm.QubesVM]
labels = ... # type: Dict[str,qubes.Label]

class Label(object):
Expand Down

0 comments on commit acb9443

Please sign in to comment.